Explorar el Código

Change JSON marshaling for SCEP provisioners

Instead of the old method that redacted sensitive information
by overriding the value of the property and changing it back
to the original, the API now uses a model specifically meant
for API responses. This prevents potential race conditions.

This may be iterated on a bit so that we don't need to rely
on the [provisioner.Interface] interface, which requires the
API model to implement unnecessary methods.
Herman Slatman hace 3 años
padre
commit
4186b2c2d0
Se han modificado 3 ficheros con 143 adiciones y 20 borrados
  1. 26 19
      api/api.go
  2. 1 1
      api/api_test.go
  3. 116 0
      api/models/scep.go

+ 26 - 19
api/api.go

@@ -25,6 +25,7 @@ import (
 	"golang.org/x/crypto/ssh"
 
 	"github.com/smallstep/certificates/api/log"
+	"github.com/smallstep/certificates/api/models"
 	"github.com/smallstep/certificates/api/render"
 	"github.com/smallstep/certificates/authority"
 	"github.com/smallstep/certificates/authority/config"
@@ -231,6 +232,27 @@ type ProvisionersResponse struct {
 	NextCursor   string
 }
 
+const redacted = "*** REDACTED ***"
+
+func scepFromProvisioner(p *provisioner.SCEP) *models.SCEP {
+	return &models.SCEP{
+		ID:                            p.ID,
+		Type:                          p.Type,
+		Name:                          p.Name,
+		ForceCN:                       p.ForceCN,
+		ChallengePassword:             redacted,
+		Capabilities:                  p.Capabilities,
+		IncludeRoot:                   p.IncludeRoot,
+		MinimumPublicKeyLength:        p.MinimumPublicKeyLength,
+		DecrypterCertificate:          redacted,
+		DecrypterKey:                  redacted,
+		DecrypterKeyPassword:          redacted,
+		EncryptionAlgorithmIdentifier: p.EncryptionAlgorithmIdentifier,
+		Options:                       p.Options,
+		Claims:                        p.Claims,
+	}
+}
+
 // MarshalJSON implements json.Marshaler. It marshals the ProvisionersResponse
 // into a byte slice.
 //
@@ -238,37 +260,22 @@ type ProvisionersResponse struct {
 // challenge secret that MUST NOT be leaked in (public) HTTP responses. The
 // challenge value is thus redacted in HTTP responses.
 func (p ProvisionersResponse) MarshalJSON() ([]byte, error) {
+	var responseProvisioners provisioner.List
 	for _, item := range p.Provisioners {
 		scepProv, ok := item.(*provisioner.SCEP)
 		if !ok {
+			responseProvisioners = append(responseProvisioners, item)
 			continue
 		}
 
-		type old struct {
-			challengePassword    string
-			decrypterCertificate []byte
-			decrypterKey         string
-			decrypterKeyPassword string
-		}
-		o := old{scepProv.ChallengePassword, scepProv.DecrypterCertificate, scepProv.DecrypterKey, scepProv.DecrypterKeyPassword}
-		scepProv.ChallengePassword = "*** REDACTED ***"
-		scepProv.DecrypterCertificate = []byte("*** REDACTED ***")
-		scepProv.DecrypterKey = "*** REDACTED ***"
-		scepProv.DecrypterKeyPassword = "*** REDACTED ***"
-
-		defer func(o old) { //nolint:gocritic // defer in loop required to restore initial state of provisioners
-			scepProv.ChallengePassword = o.challengePassword
-			scepProv.DecrypterCertificate = o.decrypterCertificate
-			scepProv.DecrypterKey = o.decrypterKey
-			scepProv.DecrypterKeyPassword = o.decrypterKeyPassword
-		}(o)
+		responseProvisioners = append(responseProvisioners, scepFromProvisioner(scepProv))
 	}
 
 	var list = struct {
 		Provisioners []provisioner.Interface `json:"provisioners"`
 		NextCursor   string                  `json:"nextCursor"`
 	}{
-		Provisioners: []provisioner.Interface(p.Provisioners),
+		Provisioners: []provisioner.Interface(responseProvisioners),
 		NextCursor:   p.NextCursor,
 	}
 

+ 1 - 1
api/api_test.go

@@ -1613,7 +1613,7 @@ func TestProvisionersResponse_MarshalJSON(t *testing.T) {
 				"type":                          "scep",
 				"name":                          "scep",
 				"challenge":                     "*** REDACTED ***",
-				"decrypterCertificate":          "KioqIFJFREFDVEVEICoqKg==", // base64 representation of "*** REDACTED ***""
+				"decrypterCertificate":          "*** REDACTED ***",
 				"decrypterKey":                  "*** REDACTED ***",
 				"decrypterKeyPassword":          "*** REDACTED ***",
 				"minimumPublicKeyLength":        2048,

+ 116 - 0
api/models/scep.go

@@ -0,0 +1,116 @@
+package models
+
+import (
+	"context"
+	"crypto/x509"
+	"errors"
+
+	"github.com/smallstep/certificates/authority/provisioner"
+	"golang.org/x/crypto/ssh"
+)
+
+var errDummyImplementation = errors.New("dummy implementation")
+
+// SCEP is the SCEP provisioner model used solely in CA API
+// responses. All methods for the [provisioner.Interface] interface
+// are implemented, but return a dummy error.
+// TODO(hs): remove reliance on the interface for the API responses
+type SCEP struct {
+	ID                            string               `json:"-"`
+	Type                          string               `json:"type"`
+	Name                          string               `json:"name"`
+	ForceCN                       bool                 `json:"forceCN,omitempty"`
+	ChallengePassword             string               `json:"challenge,omitempty"`
+	Capabilities                  []string             `json:"capabilities,omitempty"`
+	IncludeRoot                   bool                 `json:"includeRoot,omitempty"`
+	MinimumPublicKeyLength        int                  `json:"minimumPublicKeyLength,omitempty"`
+	DecrypterCertificate          string               `json:"decrypterCertificate"`
+	DecrypterKey                  string               `json:"decrypterKey"`
+	DecrypterKeyPassword          string               `json:"decrypterKeyPassword"`
+	EncryptionAlgorithmIdentifier int                  `json:"encryptionAlgorithmIdentifier,omitempty"`
+	Options                       *provisioner.Options `json:"options,omitempty"`
+	Claims                        *provisioner.Claims  `json:"claims,omitempty"`
+}
+
+// GetID returns the provisioner unique identifier.
+func (s *SCEP) GetID() string {
+	if s.ID != "" {
+		return s.ID
+	}
+	return s.GetIDForToken()
+}
+
+// GetIDForToken returns an identifier that will be used to load the provisioner
+// from a token.
+func (s *SCEP) GetIDForToken() string {
+	return "scep/" + s.Name
+}
+
+// GetName returns the name of the provisioner.
+func (s *SCEP) GetName() string {
+	return s.Name
+}
+
+// GetType returns the type of provisioner.
+func (s *SCEP) GetType() provisioner.Type {
+	return provisioner.TypeSCEP
+}
+
+// GetEncryptedKey returns the base provisioner encrypted key if it's defined.
+func (s *SCEP) GetEncryptedKey() (string, string, bool) {
+	return "", "", false
+}
+
+// GetTokenID returns the identifier of the token.
+func (s *SCEP) GetTokenID(string) (string, error) {
+	return "", errDummyImplementation
+}
+
+// Init initializes and validates the fields of a SCEP type.
+func (s *SCEP) Init(config provisioner.Config) (err error) {
+	return errDummyImplementation
+}
+
+// AuthorizeSign returns an unimplemented error. Provisioners should overwrite
+// this method if they will support authorizing tokens for signing x509 Certificates.
+func (s *SCEP) AuthorizeSign(context.Context, string) ([]provisioner.SignOption, error) {
+	return nil, errDummyImplementation
+}
+
+// AuthorizeRevoke returns an unimplemented error. Provisioners should overwrite
+// this method if they will support authorizing tokens for revoking x509 Certificates.
+func (s *SCEP) AuthorizeRevoke(context.Context, string) error {
+	return errDummyImplementation
+}
+
+// AuthorizeRenew returns an unimplemented error. Provisioners should overwrite
+// this method if they will support authorizing tokens for renewing x509 Certificates.
+func (s *SCEP) AuthorizeRenew(context.Context, *x509.Certificate) error {
+	return errDummyImplementation
+}
+
+// AuthorizeSSHSign returns an unimplemented error. Provisioners should overwrite
+// this method if they will support authorizing tokens for signing SSH Certificates.
+func (s *SCEP) AuthorizeSSHSign(context.Context, string) ([]provisioner.SignOption, error) {
+	return nil, errDummyImplementation
+}
+
+// AuthorizeRevoke returns an unimplemented error. Provisioners should overwrite
+// this method if they will support authorizing tokens for revoking SSH Certificates.
+func (s *SCEP) AuthorizeSSHRevoke(context.Context, string) error {
+	return errDummyImplementation
+}
+
+// AuthorizeSSHRenew returns an unimplemented error. Provisioners should overwrite
+// this method if they will support authorizing tokens for renewing SSH Certificates.
+func (s *SCEP) AuthorizeSSHRenew(context.Context, string) (*ssh.Certificate, error) {
+	return nil, errDummyImplementation
+}
+
+// AuthorizeSSHRekey returns an unimplemented error. Provisioners should overwrite
+// this method if they will support authorizing tokens for rekeying SSH Certificates.
+func (s *SCEP) AuthorizeSSHRekey(context.Context, string) (*ssh.Certificate, []provisioner.SignOption, error) {
+	return nil, nil, errDummyImplementation
+}
+
+var _ provisioner.Interface = (*SCEP)(nil)