admin.go 4.4 KB

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