Selaa lähdekoodia

Merge pull request #2517 from smallstep/herman/fix-cloudcas-scep-ra

Fix CA startup when configured with SCEP and Google Cloud CAS
Herman Slatman 11 kuukautta sitten
vanhempi
sitoutus
031cc55b82
3 muutettua tiedostoa jossa 90 lisäystä ja 17 poistoa
  1. 5 1
      authority/authority.go
  2. 18 5
      cas/cloudcas/cloudcas.go
  3. 67 11
      cas/cloudcas/cloudcas_test.go

+ 5 - 1
authority/authority.go

@@ -757,7 +757,11 @@ func (a *Authority) init() error {
 						// only pass the decrypter down when it was successfully created,
 						// only pass the decrypter down when it was successfully created,
 						// meaning it's an RSA key, and `CreateDecrypter` did not fail.
 						// meaning it's an RSA key, and `CreateDecrypter` did not fail.
 						options.Decrypter = decrypter
 						options.Decrypter = decrypter
-						options.DecrypterCert = options.Intermediates[0]
+
+						// intermediate certificates can be empty in RA mode
+						if len(options.Intermediates) > 0 {
+							options.DecrypterCert = options.Intermediates[0]
+						}
 					}
 					}
 				}
 				}
 			}
 			}

+ 18 - 5
cas/cloudcas/cloudcas.go

@@ -15,12 +15,14 @@ import (
 	"github.com/google/uuid"
 	"github.com/google/uuid"
 	gax "github.com/googleapis/gax-go/v2"
 	gax "github.com/googleapis/gax-go/v2"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
-	"github.com/smallstep/certificates/cas/apiv1"
-	"go.step.sm/crypto/x509util"
 	"google.golang.org/api/option"
 	"google.golang.org/api/option"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
 	"google.golang.org/grpc/status"
 	durationpb "google.golang.org/protobuf/types/known/durationpb"
 	durationpb "google.golang.org/protobuf/types/known/durationpb"
+
+	"go.step.sm/crypto/x509util"
+
+	"github.com/smallstep/certificates/cas/apiv1"
 )
 )
 
 
 func init() {
 func init() {
@@ -181,14 +183,25 @@ func (c *CloudCAS) GetCertificateAuthority(req *apiv1.GetCertificateAuthorityReq
 		return nil, errors.New("cloudCAS GetCertificateAuthority: PemCACertificate should not be empty")
 		return nil, errors.New("cloudCAS GetCertificateAuthority: PemCACertificate should not be empty")
 	}
 	}
 
 
-	// Last certificate in the chain is the root.
+	// Parse intermediate certificates
+	var intermediates = make([]*x509.Certificate, len(resp.PemCaCertificates)-1)
+	for i := 0; i < len(resp.PemCaCertificates)-1; i++ {
+		intermediate, err := parseCertificate(resp.PemCaCertificates[i])
+		if err != nil {
+			return nil, errors.Wrap(err, "parsing cloudCAS intermediates failed")
+		}
+		intermediates[i] = intermediate
+	}
+
+	// Last certificate in the chain is the root
 	root, err := parseCertificate(resp.PemCaCertificates[len(resp.PemCaCertificates)-1])
 	root, err := parseCertificate(resp.PemCaCertificates[len(resp.PemCaCertificates)-1])
 	if err != nil {
 	if err != nil {
-		return nil, err
+		return nil, errors.Wrap(err, "parsing cloudCAS root failed")
 	}
 	}
 
 
 	return &apiv1.GetCertificateAuthorityResponse{
 	return &apiv1.GetCertificateAuthorityResponse{
-		RootCertificate: root,
+		RootCertificate:          root,
+		IntermediateCertificates: intermediates,
 	}, nil
 	}, nil
 }
 }
 
 

+ 67 - 11
cas/cloudcas/cloudcas_test.go

@@ -29,8 +29,6 @@ import (
 	"github.com/google/uuid"
 	"github.com/google/uuid"
 	"github.com/googleapis/gax-go/v2"
 	"github.com/googleapis/gax-go/v2"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
-	"github.com/smallstep/certificates/cas/apiv1"
-	kmsapi "go.step.sm/crypto/kms/apiv1"
 	"go.uber.org/mock/gomock"
 	"go.uber.org/mock/gomock"
 	"google.golang.org/api/option"
 	"google.golang.org/api/option"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc"
@@ -39,6 +37,10 @@ import (
 	"google.golang.org/grpc/status"
 	"google.golang.org/grpc/status"
 	"google.golang.org/grpc/test/bufconn"
 	"google.golang.org/grpc/test/bufconn"
 	"google.golang.org/protobuf/types/known/anypb"
 	"google.golang.org/protobuf/types/known/anypb"
+
+	kmsapi "go.step.sm/crypto/kms/apiv1"
+
+	"github.com/smallstep/certificates/cas/apiv1"
 )
 )
 
 
 var (
 var (
@@ -145,6 +147,34 @@ func okTestClient() *testClient {
 	}
 	}
 }
 }
 
 
+func okTestClientRootOnly() *testClient {
+	return &testClient{
+		credentialsFile: "testdata/credentials.json",
+		certificate: &pb.Certificate{
+			Name:                testCertificateName,
+			PemCertificate:      testSignedCertificate,
+			PemCertificateChain: []string{testRootCertificate},
+		},
+		certificateAuthority: &pb.CertificateAuthority{
+			PemCaCertificates: []string{testRootCertificate},
+		},
+	}
+}
+
+func okTestClientWithMultipleIntermediates() *testClient {
+	return &testClient{
+		credentialsFile: "testdata/credentials.json",
+		certificate: &pb.Certificate{
+			Name:                testCertificateName,
+			PemCertificate:      testSignedCertificate,
+			PemCertificateChain: []string{testIntermediateCertificate, testIntermediateCertificate, testIntermediateCertificate, testRootCertificate},
+		},
+		certificateAuthority: &pb.CertificateAuthority{
+			PemCaCertificates: []string{testIntermediateCertificate, testIntermediateCertificate, testIntermediateCertificate, testRootCertificate},
+		},
+	}
+}
+
 func failTestClient() *testClient {
 func failTestClient() *testClient {
 	return &testClient{
 	return &testClient{
 		credentialsFile: "testdata/credentials.json",
 		credentialsFile: "testdata/credentials.json",
@@ -152,7 +182,7 @@ func failTestClient() *testClient {
 	}
 	}
 }
 }
 
 
-func badTestClient() *testClient {
+func badRootTestClient() *testClient {
 	return &testClient{
 	return &testClient{
 		credentialsFile: "testdata/credentials.json",
 		credentialsFile: "testdata/credentials.json",
 		certificate: &pb.Certificate{
 		certificate: &pb.Certificate{
@@ -166,6 +196,20 @@ func badTestClient() *testClient {
 	}
 	}
 }
 }
 
 
+func badIntermediateTestClient() *testClient {
+	return &testClient{
+		credentialsFile: "testdata/credentials.json",
+		certificate: &pb.Certificate{
+			Name:                testCertificateName,
+			PemCertificate:      "this is not a pem",
+			PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
+		},
+		certificateAuthority: &pb.CertificateAuthority{
+			PemCaCertificates: []string{"this intermediate is not a pem", testRootCertificate},
+		},
+	}
+}
+
 func setTeeReader(t *testing.T, w *bytes.Buffer) {
 func setTeeReader(t *testing.T, w *bytes.Buffer) {
 	t.Helper()
 	t.Helper()
 	reader := rand.Reader
 	reader := rand.Reader
@@ -462,6 +506,7 @@ func TestCloudCAS_Type(t *testing.T) {
 }
 }
 
 
 func TestCloudCAS_GetCertificateAuthority(t *testing.T) {
 func TestCloudCAS_GetCertificateAuthority(t *testing.T) {
+	intermediate := mustParseCertificate(t, testIntermediateCertificate)
 	root := mustParseCertificate(t, testRootCertificate)
 	root := mustParseCertificate(t, testRootCertificate)
 	type fields struct {
 	type fields struct {
 		client               CertificateAuthorityClient
 		client               CertificateAuthorityClient
@@ -478,15 +523,26 @@ func TestCloudCAS_GetCertificateAuthority(t *testing.T) {
 		wantErr bool
 		wantErr bool
 	}{
 	}{
 		{"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, &apiv1.GetCertificateAuthorityResponse{
 		{"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, &apiv1.GetCertificateAuthorityResponse{
-			RootCertificate: root,
+			RootCertificate:          root,
+			IntermediateCertificates: []*x509.Certificate{intermediate},
 		}, false},
 		}, false},
 		{"ok with name", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{
 		{"ok with name", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{
 			Name: testCertificateName,
 			Name: testCertificateName,
 		}}, &apiv1.GetCertificateAuthorityResponse{
 		}}, &apiv1.GetCertificateAuthorityResponse{
-			RootCertificate: root,
+			RootCertificate:          root,
+			IntermediateCertificates: []*x509.Certificate{intermediate},
+		}, false},
+		{"ok with root only", fields{okTestClientRootOnly(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, &apiv1.GetCertificateAuthorityResponse{
+			RootCertificate:          root,
+			IntermediateCertificates: []*x509.Certificate{},
+		}, false},
+		{"ok with multiple intermediates", fields{okTestClientWithMultipleIntermediates(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, &apiv1.GetCertificateAuthorityResponse{
+			RootCertificate:          root,
+			IntermediateCertificates: []*x509.Certificate{intermediate, intermediate, intermediate},
 		}, false},
 		}, false},
 		{"fail GetCertificateAuthority", fields{failTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
 		{"fail GetCertificateAuthority", fields{failTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
-		{"fail bad root", fields{badTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
+		{"fail bad root", fields{badRootTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
+		{"fail bad intermediate", fields{badIntermediateTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
 		{"fail no pems", fields{&testClient{certificateAuthority: &pb.CertificateAuthority{}}, testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
 		{"fail no pems", fields{&testClient{certificateAuthority: &pb.CertificateAuthority{}}, testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
@@ -539,7 +595,7 @@ func TestCloudCAS_CreateCertificate(t *testing.T) {
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Lifetime: 24 * time.Hour,
 			Lifetime: 24 * time.Hour,
 		}}, nil, true},
 		}}, nil, true},
-		{"fail Certificate", fields{badTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
+		{"fail Certificate", fields{badRootTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Lifetime: 24 * time.Hour,
 			Lifetime: 24 * time.Hour,
 		}}, nil, true},
 		}}, nil, true},
@@ -587,7 +643,7 @@ func TestCloudCAS_createCertificate(t *testing.T) {
 		{"ok", fields{okTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, signed, chain, false},
 		{"ok", fields{okTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, signed, chain, false},
 		{"fail CertificateConfig", fields{okTestClient(), testAuthorityName}, args{&x509.Certificate{}, 24 * time.Hour, "request-id"}, nil, nil, true},
 		{"fail CertificateConfig", fields{okTestClient(), testAuthorityName}, args{&x509.Certificate{}, 24 * time.Hour, "request-id"}, nil, nil, true},
 		{"fail CreateCertificate", fields{failTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
 		{"fail CreateCertificate", fields{failTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
-		{"fail ParseCertificates", fields{badTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
+		{"fail ParseCertificates", fields{badRootTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
 		{"fail create id", fields{okTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
 		{"fail create id", fields{okTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
 	}
 	}
 
 
@@ -655,7 +711,7 @@ func TestCloudCAS_RenewCertificate(t *testing.T) {
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Lifetime: 24 * time.Hour,
 			Lifetime: 24 * time.Hour,
 		}}, nil, true},
 		}}, nil, true},
-		{"fail Certificate", fields{badTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
+		{"fail Certificate", fields{badRootTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Template: mustParseCertificate(t, testLeafCertificate),
 			Lifetime: 24 * time.Hour,
 			Lifetime: 24 * time.Hour,
 		}}, nil, true},
 		}}, nil, true},
@@ -734,7 +790,7 @@ func TestCloudCAS_RevokeCertificate(t *testing.T) {
 			Certificate: mustParseCertificate(t, testSignedCertificate),
 			Certificate: mustParseCertificate(t, testSignedCertificate),
 			ReasonCode:  1,
 			ReasonCode:  1,
 		}}, nil, true},
 		}}, nil, true},
-		{"fail ParseCertificate", fields{badTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
+		{"fail ParseCertificate", fields{badRootTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
 			Certificate: mustParseCertificate(t, testSignedCertificate),
 			Certificate: mustParseCertificate(t, testSignedCertificate),
 			ReasonCode:  1,
 			ReasonCode:  1,
 		}}, nil, true},
 		}}, nil, true},
@@ -862,7 +918,7 @@ func Test_getCertificateAndChain(t *testing.T) {
 }
 }
 
 
 func TestCloudCAS_CreateCertificateAuthority(t *testing.T) {
 func TestCloudCAS_CreateCertificateAuthority(t *testing.T) {
-	must := func(a, b interface{}) interface{} {
+	must := func(a, _ any) any {
 		return a
 		return a
 	}
 	}