Bläddra i källkod

Parse intermediates from Google Cloud CAS

Herman Slatman 11 månader sedan
förälder
incheckning
cdcd89b9a5
2 ändrade filer med 76 tillägg och 11 borttagningar
  1. 13 2
      cas/cloudcas/cloudcas.go
  2. 63 9
      cas/cloudcas/cloudcas_test.go

+ 13 - 2
cas/cloudcas/cloudcas.go

@@ -181,14 +181,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, err
+		}
+		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, err
 	}
 	}
 
 
 	return &apiv1.GetCertificateAuthorityResponse{
 	return &apiv1.GetCertificateAuthorityResponse{
-		RootCertificate: root,
+		RootCertificate:          root,
+		IntermediateCertificates: intermediates,
 	}, nil
 	}, nil
 }
 }
 
 

+ 63 - 9
cas/cloudcas/cloudcas_test.go

@@ -145,6 +145,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 +180,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 +194,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 +504,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 +521,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 +593,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 +641,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 +709,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 +788,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 +916,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
 	}
 	}