admin.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package api
  2. import (
  3. "fmt"
  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/mgmt"
  9. "github.com/smallstep/certificates/authority/status"
  10. )
  11. // CreateAdminRequest represents the body for a CreateAdmin request.
  12. type CreateAdminRequest struct {
  13. Subject string `json:"subject"`
  14. Provisioner string `json:"provisioner"`
  15. Type admin.Type `json:"type"`
  16. }
  17. // Validate validates a new-admin request body.
  18. func (car *CreateAdminRequest) Validate(c *admin.Collection) error {
  19. if _, ok := c.LoadBySubProv(car.Subject, car.Provisioner); ok {
  20. return mgmt.NewError(mgmt.ErrorBadRequestType,
  21. "admin with subject: '%s' and provisioner: '%s' already exists", car.Subject, car.Provisioner)
  22. }
  23. return nil
  24. }
  25. // GetAdminsResponse for returning a list of admins.
  26. type GetAdminsResponse struct {
  27. Admins []*admin.Admin `json:"admins"`
  28. NextCursor string `json:"nextCursor"`
  29. }
  30. // UpdateAdminRequest represents the body for a UpdateAdmin request.
  31. type UpdateAdminRequest struct {
  32. Type admin.Type `json:"type"`
  33. }
  34. // Validate validates a new-admin request body.
  35. func (uar *UpdateAdminRequest) Validate() error {
  36. return nil
  37. }
  38. // DeleteResponse is the resource for successful DELETE responses.
  39. type DeleteResponse struct {
  40. Status string `json:"status"`
  41. }
  42. // GetAdmin returns the requested admin, or an error.
  43. func (h *Handler) GetAdmin(w http.ResponseWriter, r *http.Request) {
  44. id := chi.URLParam(r, "id")
  45. adm, ok := h.auth.GetAdminCollection().LoadByID(id)
  46. if !ok {
  47. api.WriteError(w, mgmt.NewError(mgmt.ErrorNotFoundType,
  48. "admin %s not found", id))
  49. return
  50. }
  51. api.JSON(w, adm)
  52. }
  53. // GetAdmins returns all admins associated with the authority.
  54. func (h *Handler) GetAdmins(w http.ResponseWriter, r *http.Request) {
  55. cursor, limit, err := api.ParseCursor(r)
  56. if err != nil {
  57. api.WriteError(w, mgmt.WrapError(mgmt.ErrorBadRequestType, err,
  58. "error parsing cursor and limit from query params"))
  59. return
  60. }
  61. admins, nextCursor := h.auth.GetAdminCollection().Find(cursor, limit)
  62. api.JSON(w, &GetAdminsResponse{
  63. Admins: admins,
  64. NextCursor: nextCursor,
  65. })
  66. }
  67. // CreateAdmin creates a new admin.
  68. func (h *Handler) CreateAdmin(w http.ResponseWriter, r *http.Request) {
  69. ctx := r.Context()
  70. var body CreateAdminRequest
  71. if err := api.ReadJSON(r.Body, &body); err != nil {
  72. api.WriteError(w, mgmt.WrapError(mgmt.ErrorBadRequestType, err, "error reading request body"))
  73. return
  74. }
  75. if err := body.Validate(h.auth.GetAdminCollection()); err != nil {
  76. api.WriteError(w, err)
  77. return
  78. }
  79. p, ok := h.auth.GetProvisionerCollection().LoadByName(body.Provisioner)
  80. if !ok {
  81. api.WriteError(w, mgmt.NewError(mgmt.ErrorNotFoundType, "provisioner %s not found", body.Provisioner))
  82. return
  83. }
  84. adm := &mgmt.Admin{
  85. ProvisionerID: p.GetID(),
  86. Subject: body.Subject,
  87. Type: body.Type,
  88. Status: status.Active,
  89. }
  90. if err := h.db.CreateAdmin(ctx, adm); err != nil {
  91. api.WriteError(w, mgmt.WrapErrorISE(err, "error creating admin"))
  92. return
  93. }
  94. adm.ProvisionerName = p.GetName()
  95. adm.ProvisionerType = p.GetType().String()
  96. api.JSON(w, adm)
  97. if err := h.auth.ReloadAuthConfig(); err != nil {
  98. fmt.Printf("err = %+v\n", err)
  99. }
  100. }
  101. // DeleteAdmin deletes admin.
  102. func (h *Handler) DeleteAdmin(w http.ResponseWriter, r *http.Request) {
  103. id := chi.URLParam(r, "id")
  104. if h.auth.GetAdminCollection().Count() == 1 {
  105. api.WriteError(w, mgmt.NewError(mgmt.ErrorBadRequestType, "cannot remove last admin"))
  106. return
  107. }
  108. ctx := r.Context()
  109. adm, err := h.db.GetAdmin(ctx, id)
  110. if err != nil {
  111. api.WriteError(w, mgmt.WrapErrorISE(err, "error retrieiving admin %s", id))
  112. return
  113. }
  114. adm.Status = status.Deleted
  115. if err := h.db.UpdateAdmin(ctx, adm); err != nil {
  116. api.WriteError(w, mgmt.WrapErrorISE(err, "error updating admin %s", id))
  117. return
  118. }
  119. api.JSON(w, &DeleteResponse{Status: "ok"})
  120. if err := h.auth.ReloadAuthConfig(); err != nil {
  121. fmt.Printf("err = %+v\n", err)
  122. }
  123. }
  124. // UpdateAdmin updates an existing admin.
  125. func (h *Handler) UpdateAdmin(w http.ResponseWriter, r *http.Request) {
  126. ctx := r.Context()
  127. var body UpdateAdminRequest
  128. if err := api.ReadJSON(r.Body, &body); err != nil {
  129. api.WriteError(w, mgmt.WrapError(mgmt.ErrorBadRequestType, err, "error reading request body"))
  130. return
  131. }
  132. id := chi.URLParam(r, "id")
  133. adm, ok := h.auth.GetAdminCollection().LoadByID(id)
  134. if !ok {
  135. api.WriteError(w, mgmt.NewError(mgmt.ErrorNotFoundType, "admin %s not found", id))
  136. return
  137. }
  138. if adm.Type == body.Type {
  139. api.WriteError(w, mgmt.NewError(mgmt.ErrorBadRequestType, "admin %s already has type %s", id, adm.Type))
  140. return
  141. }
  142. adm.Type = body.Type
  143. if err := h.db.UpdateAdmin(ctx, (*mgmt.Admin)(adm)); err != nil {
  144. api.WriteError(w, mgmt.WrapErrorISE(err, "error updating admin %s", id))
  145. return
  146. }
  147. api.JSON(w, adm)
  148. if err := h.auth.ReloadAuthConfig(); err != nil {
  149. fmt.Printf("err = %+v\n", err)
  150. }
  151. }