| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590 |
- //go:generate go run go.uber.org/mock/mockgen -package cloudcas -mock_names=CertificateAuthorityClient=MockCertificateAuthorityClient -destination mock_client_test.go github.com/smallstep/certificates/cas/cloudcas CertificateAuthorityClient
- //go:generate go run go.uber.org/mock/mockgen -package cloudcas -mock_names=OperationsServer=MockOperationsServer -destination mock_operation_server_test.go cloud.google.com/go/longrunning/autogen/longrunningpb OperationsServer
- package cloudcas
- import (
- "bytes"
- "context"
- "crypto"
- "crypto/ecdsa"
- "crypto/ed25519"
- "crypto/rand"
- "crypto/x509"
- "encoding/asn1"
- "encoding/pem"
- "fmt"
- "io"
- "net"
- "os"
- "path/filepath"
- "reflect"
- "testing"
- "time"
- lroauto "cloud.google.com/go/longrunning/autogen"
- "cloud.google.com/go/longrunning/autogen/longrunningpb"
- privateca "cloud.google.com/go/security/privateca/apiv1"
- pb "cloud.google.com/go/security/privateca/apiv1/privatecapb"
- "github.com/google/uuid"
- "github.com/googleapis/gax-go/v2"
- "github.com/pkg/errors"
- "go.uber.org/mock/gomock"
- "google.golang.org/api/option"
- "google.golang.org/grpc"
- "google.golang.org/grpc/codes"
- "google.golang.org/grpc/credentials/insecure"
- "google.golang.org/grpc/status"
- "google.golang.org/grpc/test/bufconn"
- "google.golang.org/protobuf/types/known/anypb"
- kmsapi "go.step.sm/crypto/kms/apiv1"
- "github.com/smallstep/certificates/cas/apiv1"
- )
- var (
- errTest = errors.New("test error")
- testCaPoolName = "projects/test-project/locations/us-west1/caPools/test-capool"
- testAuthorityName = "projects/test-project/locations/us-west1/caPools/test-capool/certificateAuthorities/test-ca"
- testCertificateName = "projects/test-project/locations/us-west1/caPools/test-capool/certificateAuthorities/test-ca/certificates/test-certificate"
- testProject = "test-project"
- testLocation = "us-west1"
- testCaPool = "test-capool"
- testRootCertificate = `-----BEGIN CERTIFICATE-----
- MIIBeDCCAR+gAwIBAgIQcXWWjtSZ/PAyH8D1Ou4L9jAKBggqhkjOPQQDAjAbMRkw
- FwYDVQQDExBDbG91ZENBUyBSb290IENBMB4XDTIwMTAyNzIyNTM1NFoXDTMwMTAy
- NzIyNTM1NFowGzEZMBcGA1UEAxMQQ2xvdWRDQVMgUm9vdCBDQTBZMBMGByqGSM49
- AgEGCCqGSM49AwEHA0IABIySHA4b78Yu4LuGhZIlv/PhNwXz4ZoV1OUZQ0LrK3vj
- B13O12DLZC5uj1z3kxdQzXUttSbtRv49clMpBiTpsZKjRTBDMA4GA1UdDwEB/wQE
- AwIBBjASBgNVHRMBAf8ECDAGAQH/AgEBMB0GA1UdDgQWBBSZ+t9RMHbFTl5BatM3
- 5bJlHPOu3DAKBggqhkjOPQQDAgNHADBEAiASah6gg0tVM3WI0meCQ4SEKk7Mjhbv
- +SmhuZHWV1QlXQIgRXNyWcpVUrAoG6Uy1KQg07LDpF5dFeK9InrDxSJAkVo=
- -----END CERTIFICATE-----`
- testIntermediateCertificate = `-----BEGIN CERTIFICATE-----
- MIIBpDCCAUmgAwIBAgIRALLKxnxyl0GBeKevIcbx02wwCgYIKoZIzj0EAwIwGzEZ
- MBcGA1UEAxMQQ2xvdWRDQVMgUm9vdCBDQTAeFw0yMDEwMjcyMjUzNTRaFw0zMDEw
- MjcyMjUzNTRaMCMxITAfBgNVBAMTGENsb3VkQ0FTIEludGVybWVkaWF0ZSBDQTBZ
- MBMGByqGSM49AgEGCCqGSM49AwEHA0IABPLuqxgBY+QmaXc8zKIC8FMgjJ6dF/cL
- b+Dig0XKc5GH/T1ORrhgOkRayrQcjPMu+jkjg25qn6vvp43LRtUKPXOjZjBkMA4G
- A1UdDwEB/wQEAwIBBjASBgNVHRMBAf8ECDAGAQH/AgEAMB0GA1UdDgQWBBQ8RVQI
- VgXAmRNDX8qItalVpSBEGjAfBgNVHSMEGDAWgBSZ+t9RMHbFTl5BatM35bJlHPOu
- 3DAKBggqhkjOPQQDAgNJADBGAiEA70MVYVqjm8SBHJf5cOlWfiXXOfHUsctTJ+/F
- pLsKBogCIQDJJkoQqYl9B59Dq3zydl8bpJevQxsoaa4Wqg+ZBMkvbQ==
- -----END CERTIFICATE-----`
- testLeafCertificate = `-----BEGIN CERTIFICATE-----
- MIIB1jCCAX2gAwIBAgIQQfOn+COMeuD8VYF1TiDkEzAKBggqhkjOPQQDAjAqMSgw
- JgYDVQQDEx9Hb29nbGUgQ0FTIFRlc3QgSW50ZXJtZWRpYXRlIENBMB4XDTIwMDkx
- NDIyNTE1NVoXDTMwMDkxMjIyNTE1MlowHTEbMBkGA1UEAxMSdGVzdC5zbWFsbHN0
- ZXAuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEAdUSRBrpgHFilN4eaGlN
- nX2+xfjXa1Iwk2/+AensjFTXJi1UAIB0e+4pqi7Sen5E2QVBhntEHCrA3xOf7czg
- P6OBkTCBjjAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG
- AQUFBwMCMB0GA1UdDgQWBBSYPbu4Tmm7Zze/hCePeZH1Avoj+jAfBgNVHSMEGDAW
- gBRIOVqyLDSlErJLuWWEvRm5UU1r1TAdBgNVHREEFjAUghJ0ZXN0LnNtYWxsc3Rl
- cC5jb20wCgYIKoZIzj0EAwIDRwAwRAIgY+nTc+RHn31/BOhht4JpxCmJPHxqFT3S
- ojnictBudV0CIB87ipY5HV3c8FLVEzTA0wFwdDZvQraQYsthwbg2kQFb
- -----END CERTIFICATE-----`
- testSignedCertificate = `-----BEGIN CERTIFICATE-----
- MIIB/DCCAaKgAwIBAgIQHHFuGMz0cClfde5kqP5prTAKBggqhkjOPQQDAjAqMSgw
- JgYDVQQDEx9Hb29nbGUgQ0FTIFRlc3QgSW50ZXJtZWRpYXRlIENBMB4XDTIwMDkx
- NTAwMDQ0M1oXDTMwMDkxMzAwMDQ0MFowHTEbMBkGA1UEAxMSdGVzdC5zbWFsbHN0
- ZXAuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEMqNCiXMvbn74LsHzRv+8
- 17m9vEzH6RHrg3m82e0uEc36+fZWV/zJ9SKuONmnl5VP79LsjL5SVH0RDj73U2XO
- DKOBtjCBszAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG
- AQUFBwMCMB0GA1UdDgQWBBRTA2cTs7PCNjnps/+T0dS8diqv0DAfBgNVHSMEGDAW
- gBRIOVqyLDSlErJLuWWEvRm5UU1r1TBCBgwrBgEEAYKkZMYoQAIEMjAwEwhjbG91
- ZGNhcxMkZDhkMThhNjgtNTI5Ni00YWYzLWFlNGItMmY4NzdkYTNmYmQ5MAoGCCqG
- SM49BAMCA0gAMEUCIGxl+pqJ50WYWUqK2l4V1FHoXSi0Nht5kwTxFxnWZu1xAiEA
- zemu3bhWLFaGg3s8i+HTEhw4RqkHP74vF7AVYp88bAw=
- -----END CERTIFICATE-----`
- testIntermediateCsr = `-----BEGIN CERTIFICATE REQUEST-----
- MIHeMIGFAgEAMCMxITAfBgNVBAMTGENsb3VkQ0FTIEludGVybWVkaWF0ZSBDQTBZ
- MBMGByqGSM49AgEGCCqGSM49AwEHA0IABPLuqxgBY+QmaXc8zKIC8FMgjJ6dF/cL
- b+Dig0XKc5GH/T1ORrhgOkRayrQcjPMu+jkjg25qn6vvp43LRtUKPXOgADAKBggq
- hkjOPQQDAgNIADBFAiEAn3pkYXb2OzoQZ+AExFqd7qZ7pg2nyP2kBZZ01Pl8KfcC
- IHKplBXDR79/i7kjOtv1iWfgf5S/XQHrz178gXA0YQe7
- -----END CERTIFICATE REQUEST-----`
- testRootKey = `-----BEGIN EC PRIVATE KEY-----
- MHcCAQEEIN51Rgg6YcQVLeCRzumdw4pjM3VWqFIdCbnsV3Up1e/goAoGCCqGSM49
- AwEHoUQDQgAEjJIcDhvvxi7gu4aFkiW/8+E3BfPhmhXU5RlDQusre+MHXc7XYMtk
- Lm6PXPeTF1DNdS21Ju1G/j1yUykGJOmxkg==
- -----END EC PRIVATE KEY-----`
- //nolint:unused,gocritic,varcheck
- testIntermediateKey = `-----BEGIN EC PRIVATE KEY-----
- MHcCAQEEIMMX/XkXGnRDD4fYu7Z4rHACdJn/iyOy2UTwsv+oZ0C+oAoGCCqGSM49
- AwEHoUQDQgAE8u6rGAFj5CZpdzzMogLwUyCMnp0X9wtv4OKDRcpzkYf9PU5GuGA6
- RFrKtByM8y76OSODbmqfq++njctG1Qo9cw==
- -----END EC PRIVATE KEY-----`
- )
- type testClient struct {
- credentialsFile string
- certificate *pb.Certificate
- certificateAuthority *pb.CertificateAuthority
- err error
- }
- func newTestClient(credentialsFile string) (CertificateAuthorityClient, error) {
- if credentialsFile == "testdata/error.json" {
- return nil, errTest
- }
- return &testClient{
- credentialsFile: credentialsFile,
- }, nil
- }
- func okTestClient() *testClient {
- return &testClient{
- credentialsFile: "testdata/credentials.json",
- certificate: &pb.Certificate{
- Name: testCertificateName,
- PemCertificate: testSignedCertificate,
- PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
- },
- certificateAuthority: &pb.CertificateAuthority{
- PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
- },
- }
- }
- 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 {
- return &testClient{
- credentialsFile: "testdata/credentials.json",
- err: errTest,
- }
- }
- func badRootTestClient() *testClient {
- return &testClient{
- credentialsFile: "testdata/credentials.json",
- certificate: &pb.Certificate{
- Name: testCertificateName,
- PemCertificate: "not a pem cert",
- PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
- },
- certificateAuthority: &pb.CertificateAuthority{
- PemCaCertificates: []string{testIntermediateCertificate, "not a pem cert"},
- },
- }
- }
- 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) {
- t.Helper()
- reader := rand.Reader
- t.Cleanup(func() {
- rand.Reader = reader
- })
- rand.Reader = io.TeeReader(reader, w)
- }
- type badSigner struct {
- pub crypto.PublicKey
- }
- func createBadSigner(t *testing.T) *badSigner {
- t.Helper()
- pub, _, err := ed25519.GenerateKey(rand.Reader)
- if err != nil {
- t.Fatal(err)
- }
- return &badSigner{
- pub: pub,
- }
- }
- func (b *badSigner) Public() crypto.PublicKey {
- return b.pub
- }
- func (b *badSigner) Sign(io.Reader, []byte, crypto.SignerOpts) ([]byte, error) {
- return nil, fmt.Errorf("💥")
- }
- func (c *testClient) CreateCertificate(context.Context, *pb.CreateCertificateRequest, ...gax.CallOption) (*pb.Certificate, error) {
- return c.certificate, c.err
- }
- func (c *testClient) RevokeCertificate(context.Context, *pb.RevokeCertificateRequest, ...gax.CallOption) (*pb.Certificate, error) {
- return c.certificate, c.err
- }
- func (c *testClient) GetCertificateAuthority(context.Context, *pb.GetCertificateAuthorityRequest, ...gax.CallOption) (*pb.CertificateAuthority, error) {
- return c.certificateAuthority, c.err
- }
- func (c *testClient) CreateCertificateAuthority(context.Context, *pb.CreateCertificateAuthorityRequest, ...gax.CallOption) (*privateca.CreateCertificateAuthorityOperation, error) {
- return nil, errors.New("use NewMockCertificateAuthorityClient")
- }
- func (c *testClient) FetchCertificateAuthorityCsr(context.Context, *pb.FetchCertificateAuthorityCsrRequest, ...gax.CallOption) (*pb.FetchCertificateAuthorityCsrResponse, error) {
- return nil, errors.New("use NewMockCertificateAuthorityClient")
- }
- func (c *testClient) ActivateCertificateAuthority(context.Context, *pb.ActivateCertificateAuthorityRequest, ...gax.CallOption) (*privateca.ActivateCertificateAuthorityOperation, error) {
- return nil, errors.New("use NewMockCertificateAuthorityClient")
- }
- func (c *testClient) EnableCertificateAuthority(context.Context, *pb.EnableCertificateAuthorityRequest, ...gax.CallOption) (*privateca.EnableCertificateAuthorityOperation, error) {
- return nil, errors.New("use NewMockCertificateAuthorityClient")
- }
- func (c *testClient) GetCaPool(context.Context, *pb.GetCaPoolRequest, ...gax.CallOption) (*pb.CaPool, error) {
- return nil, errors.New("use NewMockCertificateAuthorityClient")
- }
- func (c *testClient) CreateCaPool(context.Context, *pb.CreateCaPoolRequest, ...gax.CallOption) (*privateca.CreateCaPoolOperation, error) {
- return nil, errors.New("use NewMockCertificateAuthorityClient")
- }
- func mustParseCertificate(t *testing.T, pemCert string) *x509.Certificate {
- t.Helper()
- crt, err := parseCertificate(pemCert)
- if err != nil {
- t.Fatal(err)
- }
- return crt
- }
- func mustParseECKey(t *testing.T, pemKey string) *ecdsa.PrivateKey {
- t.Helper()
- block, _ := pem.Decode([]byte(pemKey))
- if block == nil {
- t.Fatal("failed to parse key")
- return nil
- }
- key, err := x509.ParseECPrivateKey(block.Bytes)
- if err != nil {
- t.Fatal(err)
- }
- return key
- }
- func TestNew(t *testing.T) {
- tmp := newCertificateAuthorityClient
- newCertificateAuthorityClient = func(ctx context.Context, credentialsFile string) (CertificateAuthorityClient, error) {
- return newTestClient(credentialsFile)
- }
- t.Cleanup(func() {
- newCertificateAuthorityClient = tmp
- })
- type args struct {
- ctx context.Context
- opts apiv1.Options
- }
- tests := []struct {
- name string
- args args
- want *CloudCAS
- wantErr bool
- }{
- {"ok", args{context.Background(), apiv1.Options{
- CertificateAuthority: testAuthorityName,
- }}, &CloudCAS{
- client: &testClient{},
- certificateAuthority: testAuthorityName,
- project: testProject,
- location: testLocation,
- caPool: testCaPool,
- caPoolTier: 0,
- }, false},
- {"ok authority and creator", args{context.Background(), apiv1.Options{
- CertificateAuthority: testAuthorityName, IsCreator: true,
- }}, &CloudCAS{
- client: &testClient{},
- certificateAuthority: testAuthorityName,
- project: testProject,
- location: testLocation,
- caPool: testCaPool,
- caPoolTier: 0,
- }, false},
- {"ok with credentials", args{context.Background(), apiv1.Options{
- CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/credentials.json",
- }}, &CloudCAS{
- client: &testClient{credentialsFile: "testdata/credentials.json"},
- certificateAuthority: testAuthorityName,
- project: testProject,
- location: testLocation,
- caPool: testCaPool,
- caPoolTier: 0,
- }, false},
- {"ok creator", args{context.Background(), apiv1.Options{
- IsCreator: true, Project: testProject, Location: testLocation, CaPool: testCaPool,
- }}, &CloudCAS{
- client: &testClient{},
- project: testProject,
- location: testLocation,
- caPool: testCaPool,
- caPoolTier: pb.CaPool_DEVOPS,
- }, false},
- {"ok creator devops", args{context.Background(), apiv1.Options{
- IsCreator: true, Project: testProject, Location: testLocation, CaPool: testCaPool, CaPoolTier: "DevOps",
- }}, &CloudCAS{
- client: &testClient{},
- project: testProject,
- location: testLocation,
- caPool: testCaPool,
- caPoolTier: pb.CaPool_DEVOPS,
- }, false},
- {"ok creator enterprise", args{context.Background(), apiv1.Options{
- IsCreator: true, Project: testProject, Location: testLocation, CaPool: testCaPool, CaPoolTier: "ENTERPRISE",
- }}, &CloudCAS{
- client: &testClient{},
- project: testProject,
- location: testLocation,
- caPool: testCaPool,
- caPoolTier: pb.CaPool_ENTERPRISE,
- }, false},
- {"fail certificate authority", args{context.Background(), apiv1.Options{
- CertificateAuthority: "projects/ok1234/locations/ok1234/caPools/ok1234/certificateAuthorities/ok1234/bad",
- }}, nil, true},
- {"fail certificate authority regex", args{context.Background(), apiv1.Options{}}, nil, true},
- {"fail with credentials", args{context.Background(), apiv1.Options{
- CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/error.json",
- }}, nil, true},
- {"fail creator project", args{context.Background(), apiv1.Options{
- IsCreator: true, Project: "", Location: testLocation,
- }}, nil, true},
- {"fail creator location", args{context.Background(), apiv1.Options{
- IsCreator: true, Project: testProject, Location: "",
- }}, nil, true},
- {"fail caPool", args{context.Background(), apiv1.Options{
- IsCreator: true, Project: testProject, Location: testLocation, CaPool: "",
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := New(tt.args.ctx, tt.args.opts)
- if (err != nil) != tt.wantErr {
- t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("New() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestNew_register(t *testing.T) {
- tmp := newCertificateAuthorityClient
- newCertificateAuthorityClient = func(ctx context.Context, credentialsFile string) (CertificateAuthorityClient, error) {
- return newTestClient(credentialsFile)
- }
- t.Cleanup(func() {
- newCertificateAuthorityClient = tmp
- })
- want := &CloudCAS{
- client: &testClient{credentialsFile: "testdata/credentials.json"},
- certificateAuthority: testAuthorityName,
- project: testProject,
- location: testLocation,
- caPool: testCaPool,
- }
- newFn, ok := apiv1.LoadCertificateAuthorityServiceNewFunc(apiv1.CloudCAS)
- if !ok {
- t.Error("apiv1.LoadCertificateAuthorityServiceNewFunc(apiv1.CloudCAS) was not found")
- return
- }
- got, err := newFn(context.Background(), apiv1.Options{
- CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/credentials.json",
- })
- if err != nil {
- t.Errorf("New() error = %v", err)
- return
- }
- if !reflect.DeepEqual(got, want) {
- t.Errorf("New() = %v, want %v", got, want)
- }
- }
- func TestNew_real(t *testing.T) {
- if v, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS"); ok {
- os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS")
- t.Cleanup(func() {
- t.Setenv("GOOGLE_APPLICATION_CREDENTIALS", v)
- })
- }
- failDefaultCredentials := true
- if home, err := os.UserHomeDir(); err == nil {
- file := filepath.Join(home, ".config", "gcloud", "application_default_credentials.json")
- if _, err := os.Stat(file); err == nil {
- failDefaultCredentials = false
- }
- }
- type args struct {
- ctx context.Context
- opts apiv1.Options
- }
- tests := []struct {
- name string
- skipOnCI bool
- args args
- wantErr bool
- }{
- {"fail default credentials", true, args{context.Background(), apiv1.Options{CertificateAuthority: testAuthorityName}}, failDefaultCredentials},
- {"fail certificate authority", false, args{context.Background(), apiv1.Options{}}, true},
- {"fail with credentials", false, args{context.Background(), apiv1.Options{
- CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/missing.json",
- }}, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if tt.skipOnCI && os.Getenv("CI") == "true" {
- t.SkipNow()
- }
- _, err := New(tt.args.ctx, tt.args.opts)
- if (err != nil) != tt.wantErr {
- t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
- func TestCloudCAS_Type(t *testing.T) {
- tests := []struct {
- name string
- want apiv1.Type
- }{
- {"ok", apiv1.CloudCAS},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &CloudCAS{}
- if got := c.Type(); got != tt.want {
- t.Errorf("CloudCAS.Type() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestCloudCAS_GetCertificateAuthority(t *testing.T) {
- intermediate := mustParseCertificate(t, testIntermediateCertificate)
- root := mustParseCertificate(t, testRootCertificate)
- type fields struct {
- client CertificateAuthorityClient
- certificateAuthority string
- }
- type args struct {
- req *apiv1.GetCertificateAuthorityRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.GetCertificateAuthorityResponse
- wantErr bool
- }{
- {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, &apiv1.GetCertificateAuthorityResponse{
- RootCertificate: root,
- IntermediateCertificates: []*x509.Certificate{intermediate},
- }, false},
- {"ok with name", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{
- Name: testCertificateName,
- }}, &apiv1.GetCertificateAuthorityResponse{
- 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},
- {"fail GetCertificateAuthority", fields{failTestClient(), 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},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &CloudCAS{
- client: tt.fields.client,
- certificateAuthority: tt.fields.certificateAuthority,
- }
- got, err := c.GetCertificateAuthority(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("CloudCAS.GetCertificateAuthority() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("CloudCAS.GetCertificateAuthority() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestCloudCAS_CreateCertificate(t *testing.T) {
- type fields struct {
- client CertificateAuthorityClient
- certificateAuthority string
- }
- type args struct {
- req *apiv1.CreateCertificateRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.CreateCertificateResponse
- wantErr bool
- }{
- {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- Lifetime: 24 * time.Hour,
- }}, &apiv1.CreateCertificateResponse{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- CertificateChain: []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)},
- }, false},
- {"fail Template", fields{okTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail Lifetime", fields{okTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- }}, nil, true},
- {"fail CreateCertificate", fields{failTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail Certificate", fields{badRootTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &CloudCAS{
- client: tt.fields.client,
- certificateAuthority: tt.fields.certificateAuthority,
- }
- got, err := c.CreateCertificate(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("CloudCAS.CreateCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("CloudCAS.CreateCertificate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestCloudCAS_createCertificate(t *testing.T) {
- leaf := mustParseCertificate(t, testLeafCertificate)
- signed := mustParseCertificate(t, testSignedCertificate)
- chain := []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)}
- type fields struct {
- client CertificateAuthorityClient
- certificateAuthority string
- }
- type args struct {
- tpl *x509.Certificate
- lifetime time.Duration
- requestID string
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *x509.Certificate
- want1 []*x509.Certificate
- wantErr bool
- }{
- {"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 CreateCertificate", fields{failTestClient(), 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},
- }
- // Pre-calculate rand.Random
- buf := new(bytes.Buffer)
- setTeeReader(t, buf)
- for i := 0; i < len(tests)-1; i++ {
- _, err := uuid.NewRandomFromReader(rand.Reader)
- if err != nil {
- t.Fatal(err)
- }
- }
- rand.Reader = buf
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &CloudCAS{
- client: tt.fields.client,
- certificateAuthority: tt.fields.certificateAuthority,
- }
- got, got1, err := c.createCertificate(tt.args.tpl, tt.args.lifetime, tt.args.requestID)
- if (err != nil) != tt.wantErr {
- t.Errorf("CloudCAS.createCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("CloudCAS.createCertificate() got = %v, want %v", got, tt.want)
- }
- if !reflect.DeepEqual(got1, tt.want1) {
- t.Errorf("CloudCAS.createCertificate() got1 = %v, want %v", got1, tt.want1)
- }
- })
- }
- }
- func TestCloudCAS_RenewCertificate(t *testing.T) {
- type fields struct {
- client CertificateAuthorityClient
- certificateAuthority string
- }
- type args struct {
- req *apiv1.RenewCertificateRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.RenewCertificateResponse
- wantErr bool
- }{
- {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- Lifetime: 24 * time.Hour,
- }}, &apiv1.RenewCertificateResponse{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- CertificateChain: []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)},
- }, false},
- {"fail Template", fields{okTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail Lifetime", fields{okTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- }}, nil, true},
- {"fail CreateCertificate", fields{failTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail Certificate", fields{badRootTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
- Template: mustParseCertificate(t, testLeafCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &CloudCAS{
- client: tt.fields.client,
- certificateAuthority: tt.fields.certificateAuthority,
- }
- got, err := c.RenewCertificate(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("CloudCAS.RenewCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("CloudCAS.RenewCertificate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestCloudCAS_RevokeCertificate(t *testing.T) {
- badExtensionCert := mustParseCertificate(t, testSignedCertificate)
- for i, ext := range badExtensionCert.Extensions {
- if ext.Id.Equal(asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 37476, 9000, 64, 2}) {
- badExtensionCert.Extensions[i].Value = []byte("bad-data")
- }
- }
- type fields struct {
- client CertificateAuthorityClient
- certificateAuthority string
- }
- type args struct {
- req *apiv1.RevokeCertificateRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.RevokeCertificateResponse
- wantErr bool
- }{
- {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- ReasonCode: 1,
- }}, &apiv1.RevokeCertificateResponse{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- CertificateChain: []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)},
- }, false},
- {"fail Extension", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: mustParseCertificate(t, testLeafCertificate),
- ReasonCode: 1,
- }}, nil, true},
- {"fail Extension Value", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: badExtensionCert,
- ReasonCode: 1,
- }}, nil, true},
- {"fail Certificate", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- ReasonCode: 2,
- }}, nil, true},
- {"fail ReasonCode", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- ReasonCode: 100,
- }}, nil, true},
- {"fail ReasonCode 7", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- ReasonCode: 7,
- }}, nil, true},
- {"fail ReasonCode 8", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- ReasonCode: 8,
- }}, nil, true},
- {"fail RevokeCertificate", fields{failTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- ReasonCode: 1,
- }}, nil, true},
- {"fail ParseCertificate", fields{badRootTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
- Certificate: mustParseCertificate(t, testSignedCertificate),
- ReasonCode: 1,
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &CloudCAS{
- client: tt.fields.client,
- certificateAuthority: tt.fields.certificateAuthority,
- }
- got, err := c.RevokeCertificate(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("CloudCAS.RevokeCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("CloudCAS.RevokeCertificate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_createCertificateID(t *testing.T) {
- buf := new(bytes.Buffer)
- setTeeReader(t, buf)
- id, err := uuid.NewRandomFromReader(rand.Reader)
- if err != nil {
- t.Fatal(err)
- }
- rand.Reader = buf
- tests := []struct {
- name string
- want string
- wantErr bool
- }{
- {"ok", id.String(), false},
- {"fail", "", true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := createCertificateID()
- if (err != nil) != tt.wantErr {
- t.Errorf("createCertificateID() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if got != tt.want {
- t.Errorf("createCertificateID() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_parseCertificate(t *testing.T) {
- type args struct {
- pemCert string
- }
- tests := []struct {
- name string
- args args
- want *x509.Certificate
- wantErr bool
- }{
- {"ok", args{testLeafCertificate}, mustParseCertificate(t, testLeafCertificate), false},
- {"ok intermediate", args{testIntermediateCertificate}, mustParseCertificate(t, testIntermediateCertificate), false},
- {"fail pem", args{"not pem"}, nil, true},
- {"fail parseCertificate", args{"-----BEGIN CERTIFICATE-----\nZm9vYmFyCg==\n-----END CERTIFICATE-----\n"}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := parseCertificate(tt.args.pemCert)
- if (err != nil) != tt.wantErr {
- t.Errorf("parseCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("parseCertificate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_getCertificateAndChain(t *testing.T) {
- type args struct {
- certpb *pb.Certificate
- }
- tests := []struct {
- name string
- args args
- want *x509.Certificate
- want1 []*x509.Certificate
- wantErr bool
- }{
- {"ok", args{&pb.Certificate{
- Name: testCertificateName,
- PemCertificate: testSignedCertificate,
- PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
- }}, mustParseCertificate(t, testSignedCertificate), []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)}, false},
- {"fail PemCertificate", args{&pb.Certificate{
- Name: testCertificateName,
- PemCertificate: "foobar",
- PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
- }}, nil, nil, true},
- {"fail PemCertificateChain", args{&pb.Certificate{
- Name: testCertificateName,
- PemCertificate: testSignedCertificate,
- PemCertificateChain: []string{"foobar", testRootCertificate},
- }}, nil, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, got1, err := getCertificateAndChain(tt.args.certpb)
- if (err != nil) != tt.wantErr {
- t.Errorf("getCertificateAndChain() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("getCertificateAndChain() got = %v, want %v", got, tt.want)
- }
- if !reflect.DeepEqual(got1, tt.want1) {
- t.Errorf("getCertificateAndChain() got1 = %v, want %v", got1, tt.want1)
- }
- })
- }
- }
- func TestCloudCAS_CreateCertificateAuthority(t *testing.T) {
- must := func(a, _ any) any {
- return a
- }
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- mosCtrl := gomock.NewController(t)
- defer mosCtrl.Finish()
- m := NewMockCertificateAuthorityClient(ctrl)
- mos := NewMockOperationsServer(mosCtrl)
- // Create operation server
- srv := grpc.NewServer()
- longrunningpb.RegisterOperationsServer(srv, mos)
- lis := bufconn.Listen(2)
- go srv.Serve(lis)
- defer srv.Stop()
- // Create fake privateca client
- conn, err := grpc.NewClient("localhost", grpc.WithTransportCredentials(insecure.NewCredentials()),
- grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
- return lis.Dial()
- }))
- if err != nil {
- t.Fatal(err)
- }
- client, err := lroauto.NewOperationsClient(context.Background(), option.WithGRPCConn(conn))
- if err != nil {
- t.Fatal(err)
- }
- fake, err := privateca.NewCertificateAuthorityClient(context.Background(), option.WithGRPCConn(conn))
- if err != nil {
- t.Fatal(err)
- }
- fake.LROClient = client
- // Configure mocks
- anee := gomock.Any()
- // ok root
- m.EXPECT().GetCaPool(anee, anee).Return(nil, status.Error(codes.NotFound, "not found"))
- m.EXPECT().CreateCaPool(anee, anee).Return(fake.CreateCaPoolOperation("CreateCaPool"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCaPool",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CaPool{
- Name: testCaPoolName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "EnableCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- // ok intermediate
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- m.EXPECT().CreateCertificate(anee, anee).Return(&pb.Certificate{
- PemCertificate: testIntermediateCertificate,
- PemCertificateChain: []string{testRootCertificate},
- }, nil)
- m.EXPECT().ActivateCertificateAuthority(anee, anee).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "ActivateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "EnableCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- // ok intermediate local signer
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- m.EXPECT().ActivateCertificateAuthority(anee, anee).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "ActivateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "EnableCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- // ok create key
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "EnableCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- // fail GetCaPool
- m.EXPECT().GetCaPool(anee, anee).Return(nil, errTest)
- // fail CreateCaPool
- m.EXPECT().GetCaPool(anee, anee).Return(nil, status.Error(codes.NotFound, "not found"))
- m.EXPECT().CreateCaPool(anee, anee).Return(nil, errTest)
- // fail CreateCaPool.Wait
- m.EXPECT().GetCaPool(anee, anee).Return(nil, status.Error(codes.NotFound, "not found"))
- m.EXPECT().CreateCaPool(anee, anee).Return(fake.CreateCaPoolOperation("CreateCaPool"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(nil, errTest)
- // fail CreateCertificateAuthority
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(nil, errTest)
- // fail CreateCertificateAuthority.Wait
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(nil, errTest)
- // fail EnableCertificateAuthority
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(nil, errTest)
- // fail EnableCertificateAuthority.Wait
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(nil, errTest)
- // fail EnableCertificateAuthority intermediate
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- m.EXPECT().CreateCertificate(anee, anee).Return(&pb.Certificate{
- PemCertificate: testIntermediateCertificate,
- PemCertificateChain: []string{testRootCertificate},
- }, nil)
- m.EXPECT().ActivateCertificateAuthority(anee, anee).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "ActivateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(nil, errTest)
- // fail EnableCertificateAuthority.Wait intermediate
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- m.EXPECT().CreateCertificate(anee, anee).Return(&pb.Certificate{
- PemCertificate: testIntermediateCertificate,
- PemCertificateChain: []string{testRootCertificate},
- }, nil)
- m.EXPECT().ActivateCertificateAuthority(anee, anee).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "ActivateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().EnableCertificateAuthority(anee, anee).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(nil, errTest)
- // fail FetchCertificateAuthorityCsr
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(nil, errTest)
- // fail CreateCertificate
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- m.EXPECT().CreateCertificate(anee, anee).Return(nil, errTest)
- // fail ActivateCertificateAuthority
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- m.EXPECT().CreateCertificate(anee, anee).Return(&pb.Certificate{
- PemCertificate: testIntermediateCertificate,
- PemCertificateChain: []string{testRootCertificate},
- }, nil)
- m.EXPECT().ActivateCertificateAuthority(anee, anee).Return(nil, errTest)
- // fail ActivateCertificateAuthority.Wait
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- m.EXPECT().CreateCertificate(anee, anee).Return(&pb.Certificate{
- PemCertificate: testIntermediateCertificate,
- PemCertificateChain: []string{testRootCertificate},
- }, nil)
- m.EXPECT().ActivateCertificateAuthority(anee, anee).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(nil, errTest)
- // fail x509util.CreateCertificate
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: testIntermediateCsr,
- }, nil)
- // fail parseCertificateRequest
- m.EXPECT().GetCaPool(anee, anee).Return(&pb.CaPool{Name: testCaPoolName}, nil)
- m.EXPECT().CreateCertificateAuthority(anee, anee).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
- mos.EXPECT().GetOperation(anee, anee).Return(&longrunningpb.Operation{
- Name: "CreateCertificateAuthority",
- Done: true,
- Result: &longrunningpb.Operation_Response{
- Response: must(anypb.New(&pb.CertificateAuthority{
- Name: testAuthorityName,
- })).(*anypb.Any),
- },
- }, nil)
- m.EXPECT().FetchCertificateAuthorityCsr(anee, anee).Return(&pb.FetchCertificateAuthorityCsrResponse{
- PemCsr: "Not a CSR",
- }, nil)
- rootCrt := mustParseCertificate(t, testRootCertificate)
- intCrt := mustParseCertificate(t, testIntermediateCertificate)
- type fields struct {
- client CertificateAuthorityClient
- certificateAuthority string
- project string
- location string
- caPool string
- caPoolTier pb.CaPool_Tier
- }
- type args struct {
- req *apiv1.CreateCertificateAuthorityRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.CreateCertificateAuthorityResponse
- wantErr bool
- }{
- {"ok root", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_ENTERPRISE}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- }, false},
- {"ok intermediate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- },
- }}, &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: intCrt,
- CertificateChain: []*x509.Certificate{rootCrt},
- }, false},
- {"ok intermediate local signer", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_ENTERPRISE}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Certificate: rootCrt,
- Signer: mustParseECKey(t, testRootKey),
- },
- }}, &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: intCrt,
- CertificateChain: []*x509.Certificate{rootCrt},
- }, false},
- {"ok create key", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- CreateKey: &kmsapi.CreateKeyRequest{
- SignatureAlgorithm: kmsapi.ECDSAWithSHA256,
- },
- }}, &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- }, false},
- {"fail project", fields{m, "", "", testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail location", fields{m, "", testProject, "", testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail caPool", fields{m, "", testProject, testLocation, "", pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail template", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail lifetime", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- }}, nil, true},
- {"fail parent", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail parent name", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{},
- }}, nil, true},
- {"fail type", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: 0,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail create key", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- CreateKey: &kmsapi.CreateKeyRequest{
- SignatureAlgorithm: kmsapi.PureEd25519,
- },
- }}, nil, true},
- {"fail GetCaPool", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail CreateCaPool", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail CreateCaPool.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail CreateCertificateAuthority", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail CreateCertificateAuthority.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail EnableCertificateAuthority", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail EnableCertificateAuthority.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.RootCA,
- Template: mustParseCertificate(t, testRootCertificate),
- Lifetime: 24 * time.Hour,
- }}, nil, true},
- {"fail EnableCertificateAuthority intermediate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- },
- }}, nil, true},
- {"fail EnableCertificateAuthority.Wait intermediate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- },
- }}, nil, true},
- {"fail FetchCertificateAuthorityCsr", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- },
- }}, nil, true},
- {"fail CreateCertificate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- },
- }}, nil, true},
- {"fail ActivateCertificateAuthority", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- },
- }}, nil, true},
- {"fail ActivateCertificateAuthority.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Name: testAuthorityName,
- Certificate: rootCrt,
- },
- }}, nil, true},
- {"fail x509util.CreateCertificate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Certificate: rootCrt,
- Signer: createBadSigner(t),
- },
- }}, nil, true},
- {"fail parseCertificateRequest", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
- Type: apiv1.IntermediateCA,
- Template: mustParseCertificate(t, testIntermediateCertificate),
- Lifetime: 24 * time.Hour,
- Parent: &apiv1.CreateCertificateAuthorityResponse{
- Certificate: rootCrt,
- Signer: createBadSigner(t),
- },
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &CloudCAS{
- client: tt.fields.client,
- certificateAuthority: tt.fields.certificateAuthority,
- project: tt.fields.project,
- location: tt.fields.location,
- caPool: tt.fields.caPool,
- caPoolTier: tt.fields.caPoolTier,
- }
- got, err := c.CreateCertificateAuthority(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("CloudCAS.CreateCertificateAuthority() error = %+v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("CloudCAS.CreateCertificateAuthority() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_normalizeCertificateAuthorityName(t *testing.T) {
- type args struct {
- name string
- }
- tests := []struct {
- name string
- args args
- want string
- }{
- {"ok", args{"Test-CA-Name_1234"}, "Test-CA-Name_1234"},
- {"change", args{"💥 CA"}, "--CA"},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := normalizeCertificateAuthorityName(tt.args.name); got != tt.want {
- t.Errorf("normalizeCertificateAuthorityName() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|