admin.go 5.6 KB

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