4
0

admin.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package api
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/go-chi/chi"
  6. "go.step.sm/linkedca"
  7. "github.com/smallstep/certificates/api"
  8. "github.com/smallstep/certificates/api/read"
  9. "github.com/smallstep/certificates/api/render"
  10. "github.com/smallstep/certificates/authority/admin"
  11. "github.com/smallstep/certificates/authority/provisioner"
  12. )
  13. type adminAuthority interface {
  14. LoadProvisionerByName(string) (provisioner.Interface, error)
  15. GetProvisioners(cursor string, limit int) (provisioner.List, string, error)
  16. IsAdminAPIEnabled() bool
  17. LoadAdminByID(id string) (*linkedca.Admin, bool)
  18. GetAdmins(cursor string, limit int) ([]*linkedca.Admin, string, error)
  19. StoreAdmin(ctx context.Context, adm *linkedca.Admin, prov provisioner.Interface) error
  20. UpdateAdmin(ctx context.Context, id string, nu *linkedca.Admin) (*linkedca.Admin, error)
  21. RemoveAdmin(ctx context.Context, id string) error
  22. AuthorizeAdminToken(r *http.Request, token string) (*linkedca.Admin, error)
  23. StoreProvisioner(ctx context.Context, prov *linkedca.Provisioner) error
  24. LoadProvisionerByID(id string) (provisioner.Interface, error)
  25. UpdateProvisioner(ctx context.Context, nu *linkedca.Provisioner) error
  26. RemoveProvisioner(ctx context.Context, id string) error
  27. }
  28. // CreateAdminRequest represents the body for a CreateAdmin request.
  29. type CreateAdminRequest struct {
  30. Subject string `json:"subject"`
  31. Provisioner string `json:"provisioner"`
  32. Type linkedca.Admin_Type `json:"type"`
  33. }
  34. // Validate validates a new-admin request body.
  35. func (car *CreateAdminRequest) Validate() error {
  36. if car.Subject == "" {
  37. return admin.NewError(admin.ErrorBadRequestType, "subject cannot be empty")
  38. }
  39. if car.Provisioner == "" {
  40. return admin.NewError(admin.ErrorBadRequestType, "provisioner cannot be empty")
  41. }
  42. switch car.Type {
  43. case linkedca.Admin_SUPER_ADMIN, linkedca.Admin_ADMIN:
  44. default:
  45. return admin.NewError(admin.ErrorBadRequestType, "invalid value for admin type")
  46. }
  47. return nil
  48. }
  49. // GetAdminsResponse for returning a list of admins.
  50. type GetAdminsResponse struct {
  51. Admins []*linkedca.Admin `json:"admins"`
  52. NextCursor string `json:"nextCursor"`
  53. }
  54. // UpdateAdminRequest represents the body for a UpdateAdmin request.
  55. type UpdateAdminRequest struct {
  56. Type linkedca.Admin_Type `json:"type"`
  57. }
  58. // Validate validates a new-admin request body.
  59. func (uar *UpdateAdminRequest) Validate() error {
  60. switch uar.Type {
  61. case linkedca.Admin_SUPER_ADMIN, linkedca.Admin_ADMIN:
  62. default:
  63. return admin.NewError(admin.ErrorBadRequestType, "invalid value for admin type")
  64. }
  65. return nil
  66. }
  67. // DeleteResponse is the resource for successful DELETE responses.
  68. type DeleteResponse struct {
  69. Status string `json:"status"`
  70. }
  71. // GetAdmin returns the requested admin, or an error.
  72. func (h *Handler) GetAdmin(w http.ResponseWriter, r *http.Request) {
  73. id := chi.URLParam(r, "id")
  74. adm, ok := h.auth.LoadAdminByID(id)
  75. if !ok {
  76. render.Error(w, admin.NewError(admin.ErrorNotFoundType,
  77. "admin %s not found", id))
  78. return
  79. }
  80. render.ProtoJSON(w, adm)
  81. }
  82. // GetAdmins returns a segment of admins associated with the authority.
  83. func (h *Handler) GetAdmins(w http.ResponseWriter, r *http.Request) {
  84. cursor, limit, err := api.ParseCursor(r)
  85. if err != nil {
  86. render.Error(w, admin.WrapError(admin.ErrorBadRequestType, err,
  87. "error parsing cursor and limit from query params"))
  88. return
  89. }
  90. admins, nextCursor, err := h.auth.GetAdmins(cursor, limit)
  91. if err != nil {
  92. render.Error(w, admin.WrapErrorISE(err, "error retrieving paginated admins"))
  93. return
  94. }
  95. render.JSON(w, &GetAdminsResponse{
  96. Admins: admins,
  97. NextCursor: nextCursor,
  98. })
  99. }
  100. // CreateAdmin creates a new admin.
  101. func (h *Handler) CreateAdmin(w http.ResponseWriter, r *http.Request) {
  102. var body CreateAdminRequest
  103. if err := read.JSON(r.Body, &body); err != nil {
  104. render.Error(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
  105. return
  106. }
  107. if err := body.Validate(); err != nil {
  108. render.Error(w, err)
  109. return
  110. }
  111. p, err := h.auth.LoadProvisionerByName(body.Provisioner)
  112. if err != nil {
  113. render.Error(w, admin.WrapErrorISE(err, "error loading provisioner %s", body.Provisioner))
  114. return
  115. }
  116. adm := &linkedca.Admin{
  117. ProvisionerId: p.GetID(),
  118. Subject: body.Subject,
  119. Type: body.Type,
  120. }
  121. // Store to authority collection.
  122. if err := h.auth.StoreAdmin(r.Context(), adm, p); err != nil {
  123. render.Error(w, admin.WrapErrorISE(err, "error storing admin"))
  124. return
  125. }
  126. render.ProtoJSONStatus(w, adm, http.StatusCreated)
  127. }
  128. // DeleteAdmin deletes admin.
  129. func (h *Handler) DeleteAdmin(w http.ResponseWriter, r *http.Request) {
  130. id := chi.URLParam(r, "id")
  131. if err := h.auth.RemoveAdmin(r.Context(), id); err != nil {
  132. render.Error(w, admin.WrapErrorISE(err, "error deleting admin %s", id))
  133. return
  134. }
  135. render.JSON(w, &DeleteResponse{Status: "ok"})
  136. }
  137. // UpdateAdmin updates an existing admin.
  138. func (h *Handler) UpdateAdmin(w http.ResponseWriter, r *http.Request) {
  139. var body UpdateAdminRequest
  140. if err := read.JSON(r.Body, &body); err != nil {
  141. render.Error(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
  142. return
  143. }
  144. if err := body.Validate(); err != nil {
  145. render.Error(w, err)
  146. return
  147. }
  148. id := chi.URLParam(r, "id")
  149. adm, err := h.auth.UpdateAdmin(r.Context(), id, &linkedca.Admin{Type: body.Type})
  150. if err != nil {
  151. render.Error(w, admin.WrapErrorISE(err, "error updating admin %s", id))
  152. return
  153. }
  154. render.ProtoJSON(w, adm)
  155. }