2021-07-23 13:41:24 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-09-16 21:09:24 +00:00
|
|
|
"context"
|
2021-12-09 08:36:03 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-07-23 13:41:24 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-08-27 12:10:00 +00:00
|
|
|
"github.com/go-chi/chi"
|
2021-09-17 15:08:02 +00:00
|
|
|
"github.com/smallstep/certificates/acme"
|
2021-07-23 13:41:24 +00:00
|
|
|
"github.com/smallstep/certificates/api"
|
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
2021-09-16 21:09:24 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2021-08-27 10:39:37 +00:00
|
|
|
"go.step.sm/linkedca"
|
2021-08-27 14:58:04 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2021-07-23 13:41:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
|
|
|
|
type CreateExternalAccountKeyRequest struct {
|
2021-10-08 12:29:44 +00:00
|
|
|
Reference string `json:"reference"`
|
2021-07-23 13:41:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 21:09:24 +00:00
|
|
|
// Validate validates a new ACME EAB Key request body.
|
2021-08-27 10:39:37 +00:00
|
|
|
func (r *CreateExternalAccountKeyRequest) Validate() error {
|
2021-12-09 08:36:03 +00:00
|
|
|
if len(r.Reference) > 256 { // an arbitrary, but sensible (IMO), limit
|
|
|
|
return fmt.Errorf("reference length %d exceeds the maximum (256)", len(r.Reference))
|
|
|
|
}
|
2021-08-27 10:39:37 +00:00
|
|
|
return nil
|
2021-07-23 13:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
|
|
|
|
type GetExternalAccountKeysResponse struct {
|
2021-08-27 10:39:37 +00:00
|
|
|
EAKs []*linkedca.EABKey `json:"eaks"`
|
|
|
|
NextCursor string `json:"nextCursor"`
|
2021-07-23 13:41:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:29:44 +00:00
|
|
|
// requireEABEnabled is a middleware that ensures ACME EAB is enabled
|
|
|
|
// before serving requests that act on ACME EAB credentials.
|
|
|
|
func (h *Handler) requireEABEnabled(next nextHTTP) nextHTTP {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-10-11 21:34:23 +00:00
|
|
|
prov := chi.URLParam(r, "prov")
|
|
|
|
eabEnabled, err := h.provisionerHasEABEnabled(r.Context(), prov)
|
2021-10-08 12:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !eabEnabled {
|
2021-10-11 21:34:23 +00:00
|
|
|
api.WriteError(w, admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner %s", prov))
|
2021-10-08 12:29:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
next(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:09:24 +00:00
|
|
|
// provisionerHasEABEnabled determines if the "requireEAB" setting for an ACME
|
|
|
|
// provisioner is set to true and thus has EAB enabled.
|
2021-12-08 14:19:38 +00:00
|
|
|
func (h *Handler) provisionerHasEABEnabled(ctx context.Context, provisionerName string) (bool, *admin.Error) {
|
2021-09-16 21:09:24 +00:00
|
|
|
var (
|
|
|
|
p provisioner.Interface
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if p, err = h.auth.LoadProvisionerByName(provisionerName); err != nil {
|
|
|
|
return false, admin.WrapErrorISE(err, "error loading provisioner %s", provisionerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
prov, err := h.db.GetProvisioner(ctx, p.GetID())
|
|
|
|
if err != nil {
|
|
|
|
return false, admin.WrapErrorISE(err, "error getting provisioner with ID: %s", p.GetID())
|
|
|
|
}
|
|
|
|
|
|
|
|
details := prov.GetDetails()
|
|
|
|
if details == nil {
|
|
|
|
return false, admin.NewErrorISE("error getting details for provisioner with ID: %s", p.GetID())
|
|
|
|
}
|
|
|
|
|
2021-10-11 21:34:23 +00:00
|
|
|
acmeProvisioner := details.GetACME()
|
|
|
|
if acmeProvisioner == nil {
|
2021-09-16 21:09:24 +00:00
|
|
|
return false, admin.NewErrorISE("error getting ACME details for provisioner with ID: %s", p.GetID())
|
|
|
|
}
|
|
|
|
|
2021-10-11 21:34:23 +00:00
|
|
|
return acmeProvisioner.GetRequireEab(), nil
|
2021-09-16 21:09:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 13:41:24 +00:00
|
|
|
// CreateExternalAccountKey creates a new External Account Binding key
|
|
|
|
func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var body CreateExternalAccountKeyRequest
|
2021-08-27 14:58:04 +00:00
|
|
|
if err := api.ReadJSON(r.Body, &body); err != nil {
|
2021-08-27 10:39:37 +00:00
|
|
|
api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
|
2021-07-23 13:41:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:39:37 +00:00
|
|
|
if err := body.Validate(); err != nil {
|
2021-12-09 08:36:03 +00:00
|
|
|
api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err, "error validating request body"))
|
2021-08-27 10:39:37 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-23 13:41:24 +00:00
|
|
|
|
2021-10-11 21:34:23 +00:00
|
|
|
prov := chi.URLParam(r, "prov")
|
2021-09-17 15:25:19 +00:00
|
|
|
reference := body.Reference
|
|
|
|
|
2021-10-16 12:44:56 +00:00
|
|
|
// check if a key with the reference does not exist (only when a reference was in the request)
|
2021-09-17 15:25:19 +00:00
|
|
|
if reference != "" {
|
2021-10-11 21:34:23 +00:00
|
|
|
k, err := h.acmeDB.GetExternalAccountKeyByReference(r.Context(), prov, reference)
|
2021-10-16 12:44:56 +00:00
|
|
|
// retrieving an EAB key from DB results in an error if it doesn't exist, which is what we're looking for,
|
|
|
|
// but other errors can also happen. Return early if that happens; continuing if it was acme.ErrNotFound.
|
2021-12-20 13:30:01 +00:00
|
|
|
if shouldWriteError := err != nil && !errors.Is(err, acme.ErrNotFound); shouldWriteError {
|
2021-12-09 08:36:03 +00:00
|
|
|
api.WriteError(w, admin.WrapErrorISE(err, "could not lookup external account key by reference"))
|
2021-10-16 12:44:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// if a key was found, return HTTP 409 conflict
|
|
|
|
if k != nil {
|
2021-10-11 21:34:23 +00:00
|
|
|
err := admin.NewError(admin.ErrorBadRequestType, "an ACME EAB key for provisioner %s with reference %s already exists", prov, reference)
|
2021-09-17 15:25:19 +00:00
|
|
|
err.Status = 409
|
|
|
|
api.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-16 12:44:56 +00:00
|
|
|
// continue execution if no key was found for the reference
|
2021-09-17 15:25:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 21:34:23 +00:00
|
|
|
eak, err := h.acmeDB.CreateExternalAccountKey(r.Context(), prov, reference)
|
2021-07-23 13:41:24 +00:00
|
|
|
if err != nil {
|
2021-12-09 08:36:03 +00:00
|
|
|
msg := fmt.Sprintf("error creating ACME EAB key for provisioner '%s'", prov)
|
|
|
|
if reference != "" {
|
|
|
|
msg += fmt.Sprintf(" and reference '%s'", reference)
|
|
|
|
}
|
|
|
|
api.WriteError(w, admin.WrapErrorISE(err, msg))
|
2021-07-23 13:41:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:39:37 +00:00
|
|
|
response := &linkedca.EABKey{
|
2021-09-16 21:09:24 +00:00
|
|
|
Id: eak.ID,
|
|
|
|
HmacKey: eak.KeyBytes,
|
|
|
|
Provisioner: eak.Provisioner,
|
|
|
|
Reference: eak.Reference,
|
2021-07-23 13:41:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 10:39:37 +00:00
|
|
|
api.ProtoJSONStatus(w, response, http.StatusCreated)
|
2021-07-23 13:41:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 12:10:00 +00:00
|
|
|
// DeleteExternalAccountKey deletes an ACME External Account Key.
|
|
|
|
func (h *Handler) DeleteExternalAccountKey(w http.ResponseWriter, r *http.Request) {
|
2021-10-11 21:34:23 +00:00
|
|
|
prov := chi.URLParam(r, "prov")
|
2021-09-17 15:48:09 +00:00
|
|
|
keyID := chi.URLParam(r, "id")
|
2021-08-27 12:10:00 +00:00
|
|
|
|
2021-10-11 21:34:23 +00:00
|
|
|
if err := h.acmeDB.DeleteExternalAccountKey(r.Context(), prov, keyID); err != nil {
|
2021-12-09 08:36:03 +00:00
|
|
|
api.WriteError(w, admin.WrapErrorISE(err, "error deleting ACME EAB Key '%s'", keyID))
|
2021-08-27 12:10:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.JSON(w, &DeleteResponse{Status: "ok"})
|
|
|
|
}
|
|
|
|
|
2021-07-23 13:41:24 +00:00
|
|
|
// GetExternalAccountKeys returns a segment of ACME EAB Keys.
|
|
|
|
func (h *Handler) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request) {
|
2021-08-27 14:58:04 +00:00
|
|
|
prov := chi.URLParam(r, "prov")
|
2021-09-17 15:08:02 +00:00
|
|
|
reference := chi.URLParam(r, "ref")
|
2021-08-27 14:58:04 +00:00
|
|
|
|
2021-09-17 15:08:02 +00:00
|
|
|
var (
|
2021-10-17 20:42:36 +00:00
|
|
|
key *acme.ExternalAccountKey
|
|
|
|
keys []*acme.ExternalAccountKey
|
|
|
|
err error
|
|
|
|
cursor string
|
|
|
|
nextCursor string
|
|
|
|
limit int
|
2021-09-17 15:08:02 +00:00
|
|
|
)
|
2021-10-17 20:42:36 +00:00
|
|
|
|
2021-12-20 13:30:01 +00:00
|
|
|
if cursor, limit, err = api.ParseCursor(r); err != nil {
|
2021-10-17 20:42:36 +00:00
|
|
|
api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err,
|
|
|
|
"error parsing cursor and limit from query params"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-17 15:08:02 +00:00
|
|
|
if reference != "" {
|
2021-12-20 13:30:01 +00:00
|
|
|
if key, err = h.acmeDB.GetExternalAccountKeyByReference(r.Context(), prov, reference); err != nil {
|
2021-12-09 08:36:03 +00:00
|
|
|
api.WriteError(w, admin.WrapErrorISE(err, "error retrieving external account key with reference '%s'", reference))
|
2021-09-17 15:08:02 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-09 08:36:03 +00:00
|
|
|
if key != nil {
|
|
|
|
keys = []*acme.ExternalAccountKey{key}
|
|
|
|
}
|
2021-09-17 15:08:02 +00:00
|
|
|
} else {
|
2021-12-20 13:30:01 +00:00
|
|
|
if keys, nextCursor, err = h.acmeDB.GetExternalAccountKeys(r.Context(), prov, cursor, limit); err != nil {
|
2021-12-09 08:36:03 +00:00
|
|
|
api.WriteError(w, admin.WrapErrorISE(err, "error retrieving external account keys"))
|
2021-09-17 15:08:02 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-27 14:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eaks := make([]*linkedca.EABKey, len(keys))
|
|
|
|
for i, k := range keys {
|
|
|
|
eaks[i] = &linkedca.EABKey{
|
2021-09-16 21:09:24 +00:00
|
|
|
Id: k.ID,
|
|
|
|
HmacKey: []byte{},
|
|
|
|
Provisioner: k.Provisioner,
|
|
|
|
Reference: k.Reference,
|
|
|
|
Account: k.AccountID,
|
|
|
|
CreatedAt: timestamppb.New(k.CreatedAt),
|
|
|
|
BoundAt: timestamppb.New(k.BoundAt),
|
2021-08-27 14:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
api.JSON(w, &GetExternalAccountKeysResponse{
|
|
|
|
EAKs: eaks,
|
|
|
|
NextCursor: nextCursor,
|
|
|
|
})
|
2021-07-23 13:41:24 +00:00
|
|
|
}
|