2021-05-03 19:48:20 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-04-15 08:43:10 +00:00
|
|
|
"errors"
|
2021-05-03 19:48:20 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-03-30 12:21:39 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
|
2022-03-30 12:50:14 +00:00
|
|
|
"go.step.sm/linkedca"
|
|
|
|
|
2022-04-07 12:11:53 +00:00
|
|
|
"github.com/smallstep/certificates/acme"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2021-05-03 19:48:20 +00:00
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
2022-03-15 14:51:45 +00:00
|
|
|
"github.com/smallstep/certificates/authority/admin/db/nosql"
|
2022-03-30 12:21:39 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2021-05-03 19:48:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// requireAPIEnabled is a middleware that ensures the Administration API
|
|
|
|
// is enabled before servicing requests.
|
2022-05-06 20:21:41 +00:00
|
|
|
func requireAPIEnabled(next http.HandlerFunc) http.HandlerFunc {
|
2021-05-03 19:48:20 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-04-27 18:59:32 +00:00
|
|
|
if !mustAuthority(r.Context()).IsAdminAPIEnabled() {
|
|
|
|
render.Error(w, admin.NewError(admin.ErrorNotImplementedType, "administration API not enabled"))
|
2021-05-03 19:48:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
next(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractAuthorizeTokenAdmin is a middleware that extracts and caches the bearer token.
|
2022-05-06 20:21:41 +00:00
|
|
|
func extractAuthorizeTokenAdmin(next http.HandlerFunc) http.HandlerFunc {
|
2021-05-03 19:48:20 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tok := r.Header.Get("Authorization")
|
2021-10-08 18:59:57 +00:00
|
|
|
if tok == "" {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, admin.NewError(admin.ErrorUnauthorizedType,
|
2021-05-03 19:48:20 +00:00
|
|
|
"missing authorization header token"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-27 18:59:32 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
adm, err := mustAuthority(ctx).AuthorizeAdminToken(r, tok)
|
2021-05-03 19:48:20 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2021-05-03 19:48:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-06 20:21:41 +00:00
|
|
|
ctx = linkedca.NewContextWithAdmin(ctx, adm)
|
2021-05-03 19:48:20 +00:00
|
|
|
next(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:29 +00:00
|
|
|
// loadProvisionerByName is a middleware that searches for a provisioner
|
2022-03-30 12:21:39 +00:00
|
|
|
// by name and stores it in the context.
|
2022-05-06 20:21:41 +00:00
|
|
|
func loadProvisionerByName(next http.HandlerFunc) http.HandlerFunc {
|
2022-03-30 12:21:39 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
p provisioner.Interface
|
|
|
|
err error
|
|
|
|
)
|
2021-05-03 19:48:20 +00:00
|
|
|
|
2022-05-06 20:21:41 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
auth := mustAuthority(ctx)
|
|
|
|
adminDB := admin.MustFromContext(ctx)
|
|
|
|
name := chi.URLParam(r, "provisionerName")
|
|
|
|
|
2022-03-30 16:21:25 +00:00
|
|
|
// TODO(hs): distinguish 404 vs. 500
|
2022-05-06 20:21:41 +00:00
|
|
|
if p, err = auth.LoadProvisionerByName(name); err != nil {
|
2022-03-30 12:50:14 +00:00
|
|
|
render.Error(w, admin.WrapErrorISE(err, "error loading provisioner %s", name))
|
2022-03-30 12:21:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-06 20:21:41 +00:00
|
|
|
prov, err := adminDB.GetProvisioner(ctx, p.GetID())
|
2022-03-30 12:21:39 +00:00
|
|
|
if err != nil {
|
2022-03-30 16:21:25 +00:00
|
|
|
render.Error(w, admin.WrapErrorISE(err, "error retrieving provisioner %s", name))
|
2022-03-30 12:21:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = linkedca.NewContextWithProvisioner(ctx, prov)
|
|
|
|
next(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 14:51:45 +00:00
|
|
|
// checkAction checks if an action is supported in standalone or not
|
2022-05-06 20:21:41 +00:00
|
|
|
func checkAction(next http.HandlerFunc, supportedInStandalone bool) http.HandlerFunc {
|
2022-03-15 14:51:45 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-03-21 14:53:59 +00:00
|
|
|
// actions allowed in standalone mode are always supported
|
2022-03-15 14:51:45 +00:00
|
|
|
if supportedInStandalone {
|
|
|
|
next(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:34:04 +00:00
|
|
|
// when an action is not supported in standalone mode and when
|
|
|
|
// using a nosql.DB backend, actions are not supported
|
2022-05-06 20:21:41 +00:00
|
|
|
if _, ok := admin.MustFromContext(r.Context()).(*nosql.DB); ok {
|
2022-03-30 12:50:14 +00:00
|
|
|
render.Error(w, admin.NewError(admin.ErrorNotImplementedType,
|
2022-03-15 14:51:45 +00:00
|
|
|
"operation not supported in standalone mode"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue to next http handler
|
|
|
|
next(w, r)
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 12:11:53 +00:00
|
|
|
|
|
|
|
// loadExternalAccountKey is a middleware that searches for an ACME
|
2022-04-21 14:18:55 +00:00
|
|
|
// External Account Key by reference or keyID and stores it in the context.
|
2022-05-06 20:21:41 +00:00
|
|
|
func loadExternalAccountKey(next http.HandlerFunc) http.HandlerFunc {
|
2022-04-07 12:11:53 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2022-05-05 09:05:57 +00:00
|
|
|
prov := linkedca.MustProvisionerFromContext(ctx)
|
2022-05-06 20:21:41 +00:00
|
|
|
acmeDB := acme.MustDatabaseFromContext(ctx)
|
2022-04-07 12:11:53 +00:00
|
|
|
|
|
|
|
reference := chi.URLParam(r, "reference")
|
|
|
|
keyID := chi.URLParam(r, "keyID")
|
|
|
|
|
|
|
|
var (
|
|
|
|
eak *acme.ExternalAccountKey
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if keyID != "" {
|
2022-05-06 20:21:41 +00:00
|
|
|
eak, err = acmeDB.GetExternalAccountKey(ctx, prov.GetId(), keyID)
|
2022-04-07 12:11:53 +00:00
|
|
|
} else {
|
2022-05-06 20:21:41 +00:00
|
|
|
eak, err = acmeDB.GetExternalAccountKeyByReference(ctx, prov.GetId(), reference)
|
2022-04-07 12:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 08:43:10 +00:00
|
|
|
if errors.Is(err, acme.ErrNotFound) {
|
|
|
|
render.Error(w, admin.NewError(admin.ErrorNotFoundType, "ACME External Account Key not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.Error(w, admin.WrapErrorISE(err, "error retrieving ACME External Account Key"))
|
2022-04-07 12:11:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if eak == nil {
|
2022-04-15 08:43:10 +00:00
|
|
|
render.Error(w, admin.NewError(admin.ErrorNotFoundType, "ACME External Account Key not found"))
|
2022-04-07 12:11:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
linkedEAK := eakToLinked(eak)
|
|
|
|
|
|
|
|
ctx = linkedca.NewContextWithExternalAccountKey(ctx, linkedEAK)
|
|
|
|
|
|
|
|
next(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
}
|