smallstep-certificates/authority/admin/api/admin.go

140 lines
3.7 KiB
Go
Raw Normal View History

2021-05-03 19:48:20 +00:00
package api
import (
"net/http"
"github.com/go-chi/chi"
"github.com/smallstep/certificates/api"
2021-05-18 04:07:25 +00:00
"github.com/smallstep/certificates/authority/admin"
"go.step.sm/linkedca"
2021-05-03 19:48:20 +00:00
)
// CreateAdminRequest represents the body for a CreateAdmin request.
type CreateAdminRequest struct {
2021-05-26 04:13:01 +00:00
Subject string `json:"subject"`
Provisioner string `json:"provisioner"`
Type linkedca.Admin_Type `json:"type"`
2021-05-03 19:48:20 +00:00
}
// Validate validates a new-admin request body.
func (car *CreateAdminRequest) Validate() error {
2021-05-03 19:48:20 +00:00
return nil
}
2021-05-18 23:50:54 +00:00
// GetAdminsResponse for returning a list of admins.
type GetAdminsResponse struct {
2021-05-26 04:13:01 +00:00
Admins []*linkedca.Admin `json:"admins"`
NextCursor string `json:"nextCursor"`
2021-05-18 23:50:54 +00:00
}
2021-05-03 19:48:20 +00:00
// UpdateAdminRequest represents the body for a UpdateAdmin request.
type UpdateAdminRequest struct {
2021-05-26 04:13:01 +00:00
Type linkedca.Admin_Type `json:"type"`
2021-05-03 19:48:20 +00:00
}
// Validate validates a new-admin request body.
func (uar *UpdateAdminRequest) Validate() error {
return nil
}
2021-05-12 07:03:40 +00:00
// DeleteResponse is the resource for successful DELETE responses.
type DeleteResponse struct {
Status string `json:"status"`
}
2021-05-03 19:48:20 +00:00
// GetAdmin returns the requested admin, or an error.
func (h *Handler) GetAdmin(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
adm, ok := h.auth.LoadAdminByID(id)
2021-05-18 23:50:54 +00:00
if !ok {
api.WriteError(w, admin.NewError(admin.ErrorNotFoundType,
2021-05-18 23:50:54 +00:00
"admin %s not found", id))
2021-05-03 19:48:20 +00:00
return
}
2021-05-18 23:50:54 +00:00
api.JSON(w, adm)
2021-05-03 19:48:20 +00:00
}
// GetAdmins returns a segment of admins associated with the authority.
2021-05-03 19:48:20 +00:00
func (h *Handler) GetAdmins(w http.ResponseWriter, r *http.Request) {
2021-05-18 23:50:54 +00:00
cursor, limit, err := api.ParseCursor(r)
2021-05-03 19:48:20 +00:00
if err != nil {
api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err,
2021-05-18 23:50:54 +00:00
"error parsing cursor and limit from query params"))
2021-05-03 19:48:20 +00:00
return
}
2021-05-18 23:50:54 +00:00
admins, nextCursor, err := h.auth.GetAdmins(cursor, limit)
if err != nil {
api.WriteError(w, admin.WrapErrorISE(err, "error retrieving paginated admins"))
return
}
2021-05-18 23:50:54 +00:00
api.JSON(w, &GetAdminsResponse{
Admins: admins,
NextCursor: nextCursor,
})
2021-05-03 19:48:20 +00:00
}
// CreateAdmin creates a new admin.
func (h *Handler) CreateAdmin(w http.ResponseWriter, r *http.Request) {
2021-05-12 07:03:40 +00:00
var body CreateAdminRequest
if err := api.ReadJSON(r.Body, &body); err != nil {
api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
2021-05-12 07:03:40 +00:00
return
}
if err := body.Validate(); err != nil {
2021-05-18 04:07:25 +00:00
api.WriteError(w, err)
return
}
2021-05-12 07:03:40 +00:00
p, err := h.auth.LoadProvisionerByName(body.Provisioner)
if err != nil {
api.WriteError(w, admin.WrapErrorISE(err, "error loading provisioner %s", body.Provisioner))
2021-05-18 23:50:54 +00:00
return
}
2021-05-26 04:13:01 +00:00
adm := &linkedca.Admin{
ProvisionerId: p.GetID(),
2021-05-18 23:50:54 +00:00
Subject: body.Subject,
Type: body.Type,
2021-05-12 07:03:40 +00:00
}
// Store to authority collection.
if err := h.auth.StoreAdmin(r.Context(), adm, p); err != nil {
api.WriteError(w, admin.WrapErrorISE(err, "error storing admin"))
2021-05-12 07:03:40 +00:00
return
}
2021-05-12 07:03:40 +00:00
api.JSON(w, adm)
}
// DeleteAdmin deletes admin.
func (h *Handler) DeleteAdmin(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if err := h.auth.RemoveAdmin(r.Context(), id); err != nil {
api.WriteError(w, admin.WrapErrorISE(err, "error deleting admin %s", id))
2021-05-18 04:07:25 +00:00
return
}
2021-05-12 07:03:40 +00:00
api.JSON(w, &DeleteResponse{Status: "ok"})
2021-05-03 19:48:20 +00:00
}
// UpdateAdmin updates an existing admin.
func (h *Handler) UpdateAdmin(w http.ResponseWriter, r *http.Request) {
2021-05-12 07:03:40 +00:00
var body UpdateAdminRequest
if err := api.ReadJSON(r.Body, &body); err != nil {
api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
2021-05-12 07:03:40 +00:00
return
}
id := chi.URLParam(r, "id")
2021-05-07 00:03:12 +00:00
adm, err := h.auth.UpdateAdmin(r.Context(), id, &linkedca.Admin{Type: body.Type})
if err != nil {
api.WriteError(w, admin.WrapErrorISE(err, "error updating admin %s", id))
2021-05-12 07:03:40 +00:00
return
}
api.JSON(w, adm)
2021-05-03 19:48:20 +00:00
}