Просмотр исходного кода

Serve EST endpoints on `/.well-known/{label}/est`

The EST RFC describes that EST is always served on a well known path:

  `https://host:port/.well-known/est`

It also supports paths that contain a label, supporting multiple
authorities:

  `https://host:port/.well-known/some-label/est`

Clients may be forming the URL to use based on just the base host
URL, and may or may not support using a label. We'll thus have to
keep that into account when serving EST.

The fact that the label comes after `.well-known` is not great, as
the label is then not namespaced. Anything else that we may want to
serve there could now be pointing to an EST provisioner too.

We currently don't know if EST clients support `/.well-known` to not
sit at the root, or whether they support suffix path parts and/or
query parameters. For now this is the conservative choice.
Herman Slatman 3 месяцев назад
Родитель
Сommit
6c4515dfff
2 измененных файлов с 29 добавлено и 28 удалено
  1. 14 9
      ca/ca.go
  2. 15 19
      est/api/api.go

+ 14 - 9
ca/ca.go

@@ -34,13 +34,13 @@ import (
 	"github.com/smallstep/certificates/authority/config"
 	"github.com/smallstep/certificates/cas/apiv1"
 	"github.com/smallstep/certificates/db"
+	"github.com/smallstep/certificates/est"
+	estAPI "github.com/smallstep/certificates/est/api"
 	"github.com/smallstep/certificates/internal/httptransport"
 	"github.com/smallstep/certificates/internal/metrix"
 	"github.com/smallstep/certificates/logging"
 	"github.com/smallstep/certificates/middleware/requestid"
 	"github.com/smallstep/certificates/monitoring"
-	"github.com/smallstep/certificates/est"
-	estAPI "github.com/smallstep/certificates/est/api"
 	"github.com/smallstep/certificates/scep"
 	scepAPI "github.com/smallstep/certificates/scep/api"
 	"github.com/smallstep/certificates/server"
@@ -302,7 +302,6 @@ func (ca *CA) Init(cfg *config.Config) (*CA, error) {
 	}
 
 	var scepAuthority *scep.Authority
-	var estAuthority *est.Authority
 	if ca.shouldServeSCEPEndpoints() {
 		// get the SCEP authority configuration. Validation is
 		// performed within the authority instantiation process.
@@ -327,19 +326,25 @@ func (ca *CA) Init(cfg *config.Config) (*CA, error) {
 		})
 	}
 
-	// helpful routine for logging all routes
-	//dumpRoutes(mux)
-	//dumpRoutes(insecureMux)
-
 	// EST endpoints (HTTPS only)
-	estPrefix := ".well-known/est"
+	var estAuthority *est.Authority
 	if estAuth := auth.GetEST(); estAuth != nil {
+		// EST is served on /.well-known/<optional-label>/est; we use the provisioner
+		// name as label.
+		// TODO(hs): decide whether we want to go with this, or also want to support
+		// some form of default EST provisioner that can be requested at the path without
+		// a label set.
+		estPrefix := "/.well-known/{label}/est"
 		estAuthority = estAuth
-		mux.Route("/"+estPrefix, func(r chi.Router) {
+		mux.Route(estPrefix, func(r chi.Router) {
 			estAPI.Route(r)
 		})
 	}
 
+	// helpful routine for logging all routes
+	//dumpRoutes(mux)
+	//dumpRoutes(insecureMux)
+
 	// Add monitoring if configured
 	if len(cfg.Monitoring) > 0 {
 		m, err := monitoring.New(cfg.Monitoring)

+ 15 - 19
est/api/api.go

@@ -19,16 +19,15 @@ import (
 	"github.com/smallstep/certificates/api/log"
 	"github.com/smallstep/certificates/authority"
 	"github.com/smallstep/certificates/authority/provisioner"
-	"github.com/smallstep/certificates/est"
-
 	provest "github.com/smallstep/certificates/authority/provisioner/est"
+	"github.com/smallstep/certificates/est"
 )
 
 const (
 	maxPayloadSize = 2 << 20
 )
 
-// Util to extract bearer token from request
+// BearerToken extracts a bearer token from an [*http.Request].
 func BearerToken(r *http.Request) (string, bool) {
 	auth := r.Header.Get("Authorization")
 	const prefix = "Bearer "
@@ -39,27 +38,24 @@ func BearerToken(r *http.Request) (string, bool) {
 	return auth[len(prefix):], true
 }
 
-// Route configures the EST routes under the provided router.
+// Route configures the EST routes under the provided [api.Router].
 func Route(r api.Router) {
-	r.MethodFunc(http.MethodGet, "/{provisionerName}/cacerts", getCACerts)
-	r.MethodFunc(http.MethodGet, "/{provisionerName}/csrattrs", getCSRAttrs)
-	r.MethodFunc(http.MethodPost, "/{provisionerName}/simpleenroll", enroll)
-	r.MethodFunc(http.MethodPost, "/{provisionerName}/simplereenroll", enroll)
+	r.MethodFunc(http.MethodGet, "/cacerts", getCACerts)
+	r.MethodFunc(http.MethodGet, "/csrattrs", getCSRAttrs)
+	r.MethodFunc(http.MethodPost, "/simpleenroll", enroll)
+	r.MethodFunc(http.MethodPost, "/simplereenroll", enroll)
 }
 
 func lookupProvisioner(next http.HandlerFunc) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
-		name := chi.URLParam(r, "provisionerName")
-		if name == "" || name == "/" {
-			name = r.URL.Query().Get("provisioner")
-		}
+		name := chi.URLParam(r, "label")
 		if name == "" {
-			fail(w, r, errors.New("missing provisioner name"))
+			failWithStatus(w, r, http.StatusBadRequest, errors.New("missing provisioner name"))
 			return
 		}
 		provisionerName, err := url.PathUnescape(name)
 		if err != nil {
-			fail(w, r, fmt.Errorf("error url unescaping provisioner name '%s'", name))
+			failWithStatus(w, r, http.StatusBadRequest, fmt.Errorf("error url unescaping provisioner name '%s'", name))
 			return
 		}
 
@@ -67,13 +63,13 @@ func lookupProvisioner(next http.HandlerFunc) http.HandlerFunc {
 		auth := authority.MustFromContext(ctx)
 		p, err := auth.LoadProvisionerByName(provisionerName)
 		if err != nil {
-			fail(w, r, err)
+			failWithStatus(w, r, http.StatusNotFound, err)
 			return
 		}
 
 		prov, ok := p.(*provisioner.EST)
 		if !ok {
-			fail(w, r, errors.New("provisioner must be of type EST"))
+			failWithStatus(w, r, http.StatusBadRequest, errors.New("provisioner must be of type EST"))
 			return
 		}
 
@@ -173,19 +169,19 @@ func enrollHandler(w http.ResponseWriter, r *http.Request) {
 	r = r.WithContext(ctx)
 	auth := est.MustFromContext(ctx)
 
-	issued, err := auth.SignCSR(ctx, csr, opts...)
+	cert, err := auth.SignCSR(ctx, csr, opts...)
 	if err != nil {
 		failWithStatus(w, r, http.StatusInternalServerError, fmt.Errorf("failed issuing certificate: %w", err))
 		return
 	}
 
-	signed, err := auth.BuildResponse(ctx, []*x509.Certificate{issued})
+	response, err := auth.BuildResponse(ctx, []*x509.Certificate{cert})
 	if err != nil {
 		failWithStatus(w, r, http.StatusInternalServerError, fmt.Errorf("failed encoding issued certificate: %w", err))
 		return
 	}
 
-	writeResponse(w, signed, "application/pkcs7-mime; smime-type=certs-only", http.StatusOK)
+	writeResponse(w, response, "application/pkcs7-mime; smime-type=certs-only", http.StatusOK)
 }
 
 var errMissingAuth = errors.New("missing authentication material")