admin.go 5.7 KB

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