admin.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 GetAdmin(w http.ResponseWriter, r *http.Request) {
  73. id := chi.URLParam(r, "id")
  74. adm, ok := mustAuthority(r.Context()).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 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 := mustAuthority(r.Context()).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 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. auth := mustAuthority(r.Context())
  112. p, err := auth.LoadProvisionerByName(body.Provisioner)
  113. if err != nil {
  114. render.Error(w, admin.WrapErrorISE(err, "error loading provisioner %s", body.Provisioner))
  115. return
  116. }
  117. adm := &linkedca.Admin{
  118. ProvisionerId: p.GetID(),
  119. Subject: body.Subject,
  120. Type: body.Type,
  121. }
  122. // Store to authority collection.
  123. if err := auth.StoreAdmin(r.Context(), adm, p); err != nil {
  124. render.Error(w, admin.WrapErrorISE(err, "error storing admin"))
  125. return
  126. }
  127. render.ProtoJSONStatus(w, adm, http.StatusCreated)
  128. }
  129. // DeleteAdmin deletes admin.
  130. func DeleteAdmin(w http.ResponseWriter, r *http.Request) {
  131. id := chi.URLParam(r, "id")
  132. if err := mustAuthority(r.Context()).RemoveAdmin(r.Context(), id); err != nil {
  133. render.Error(w, admin.WrapErrorISE(err, "error deleting admin %s", id))
  134. return
  135. }
  136. render.JSON(w, &DeleteResponse{Status: "ok"})
  137. }
  138. // UpdateAdmin updates an existing admin.
  139. func UpdateAdmin(w http.ResponseWriter, r *http.Request) {
  140. var body UpdateAdminRequest
  141. if err := read.JSON(r.Body, &body); err != nil {
  142. render.Error(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
  143. return
  144. }
  145. if err := body.Validate(); err != nil {
  146. render.Error(w, err)
  147. return
  148. }
  149. id := chi.URLParam(r, "id")
  150. auth := mustAuthority(r.Context())
  151. adm, err := auth.UpdateAdmin(r.Context(), id, &linkedca.Admin{Type: body.Type})
  152. if err != nil {
  153. render.Error(w, admin.WrapErrorISE(err, "error updating admin %s", id))
  154. return
  155. }
  156. render.ProtoJSON(w, adm)
  157. }