admin.go 5.8 KB

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