cloudcas_test.go 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. package cloudcas
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto"
  6. "crypto/ecdsa"
  7. "crypto/ed25519"
  8. "crypto/rand"
  9. "crypto/x509"
  10. "encoding/asn1"
  11. "encoding/pem"
  12. "fmt"
  13. "io"
  14. "net"
  15. "os"
  16. "reflect"
  17. "testing"
  18. "time"
  19. lroauto "cloud.google.com/go/longrunning/autogen"
  20. privateca "cloud.google.com/go/security/privateca/apiv1"
  21. gomock "github.com/golang/mock/gomock"
  22. "github.com/google/uuid"
  23. gax "github.com/googleapis/gax-go/v2"
  24. "github.com/pkg/errors"
  25. "github.com/smallstep/certificates/cas/apiv1"
  26. kmsapi "github.com/smallstep/certificates/kms/apiv1"
  27. "google.golang.org/api/option"
  28. pb "google.golang.org/genproto/googleapis/cloud/security/privateca/v1"
  29. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  30. "google.golang.org/grpc"
  31. "google.golang.org/grpc/codes"
  32. "google.golang.org/grpc/status"
  33. "google.golang.org/grpc/test/bufconn"
  34. "google.golang.org/protobuf/types/known/anypb"
  35. )
  36. var (
  37. errTest = errors.New("test error")
  38. testCaPoolName = "projects/test-project/locations/us-west1/caPools/test-capool"
  39. testAuthorityName = "projects/test-project/locations/us-west1/caPools/test-capool/certificateAuthorities/test-ca"
  40. testCertificateName = "projects/test-project/locations/us-west1/caPools/test-capool/certificateAuthorities/test-ca/certificates/test-certificate"
  41. testProject = "test-project"
  42. testLocation = "us-west1"
  43. testCaPool = "test-capool"
  44. testRootCertificate = `-----BEGIN CERTIFICATE-----
  45. MIIBeDCCAR+gAwIBAgIQcXWWjtSZ/PAyH8D1Ou4L9jAKBggqhkjOPQQDAjAbMRkw
  46. FwYDVQQDExBDbG91ZENBUyBSb290IENBMB4XDTIwMTAyNzIyNTM1NFoXDTMwMTAy
  47. NzIyNTM1NFowGzEZMBcGA1UEAxMQQ2xvdWRDQVMgUm9vdCBDQTBZMBMGByqGSM49
  48. AgEGCCqGSM49AwEHA0IABIySHA4b78Yu4LuGhZIlv/PhNwXz4ZoV1OUZQ0LrK3vj
  49. B13O12DLZC5uj1z3kxdQzXUttSbtRv49clMpBiTpsZKjRTBDMA4GA1UdDwEB/wQE
  50. AwIBBjASBgNVHRMBAf8ECDAGAQH/AgEBMB0GA1UdDgQWBBSZ+t9RMHbFTl5BatM3
  51. 5bJlHPOu3DAKBggqhkjOPQQDAgNHADBEAiASah6gg0tVM3WI0meCQ4SEKk7Mjhbv
  52. +SmhuZHWV1QlXQIgRXNyWcpVUrAoG6Uy1KQg07LDpF5dFeK9InrDxSJAkVo=
  53. -----END CERTIFICATE-----`
  54. testIntermediateCertificate = `-----BEGIN CERTIFICATE-----
  55. MIIBpDCCAUmgAwIBAgIRALLKxnxyl0GBeKevIcbx02wwCgYIKoZIzj0EAwIwGzEZ
  56. MBcGA1UEAxMQQ2xvdWRDQVMgUm9vdCBDQTAeFw0yMDEwMjcyMjUzNTRaFw0zMDEw
  57. MjcyMjUzNTRaMCMxITAfBgNVBAMTGENsb3VkQ0FTIEludGVybWVkaWF0ZSBDQTBZ
  58. MBMGByqGSM49AgEGCCqGSM49AwEHA0IABPLuqxgBY+QmaXc8zKIC8FMgjJ6dF/cL
  59. b+Dig0XKc5GH/T1ORrhgOkRayrQcjPMu+jkjg25qn6vvp43LRtUKPXOjZjBkMA4G
  60. A1UdDwEB/wQEAwIBBjASBgNVHRMBAf8ECDAGAQH/AgEAMB0GA1UdDgQWBBQ8RVQI
  61. VgXAmRNDX8qItalVpSBEGjAfBgNVHSMEGDAWgBSZ+t9RMHbFTl5BatM35bJlHPOu
  62. 3DAKBggqhkjOPQQDAgNJADBGAiEA70MVYVqjm8SBHJf5cOlWfiXXOfHUsctTJ+/F
  63. pLsKBogCIQDJJkoQqYl9B59Dq3zydl8bpJevQxsoaa4Wqg+ZBMkvbQ==
  64. -----END CERTIFICATE-----`
  65. testLeafCertificate = `-----BEGIN CERTIFICATE-----
  66. MIIB1jCCAX2gAwIBAgIQQfOn+COMeuD8VYF1TiDkEzAKBggqhkjOPQQDAjAqMSgw
  67. JgYDVQQDEx9Hb29nbGUgQ0FTIFRlc3QgSW50ZXJtZWRpYXRlIENBMB4XDTIwMDkx
  68. NDIyNTE1NVoXDTMwMDkxMjIyNTE1MlowHTEbMBkGA1UEAxMSdGVzdC5zbWFsbHN0
  69. ZXAuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEAdUSRBrpgHFilN4eaGlN
  70. nX2+xfjXa1Iwk2/+AensjFTXJi1UAIB0e+4pqi7Sen5E2QVBhntEHCrA3xOf7czg
  71. P6OBkTCBjjAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG
  72. AQUFBwMCMB0GA1UdDgQWBBSYPbu4Tmm7Zze/hCePeZH1Avoj+jAfBgNVHSMEGDAW
  73. gBRIOVqyLDSlErJLuWWEvRm5UU1r1TAdBgNVHREEFjAUghJ0ZXN0LnNtYWxsc3Rl
  74. cC5jb20wCgYIKoZIzj0EAwIDRwAwRAIgY+nTc+RHn31/BOhht4JpxCmJPHxqFT3S
  75. ojnictBudV0CIB87ipY5HV3c8FLVEzTA0wFwdDZvQraQYsthwbg2kQFb
  76. -----END CERTIFICATE-----`
  77. testSignedCertificate = `-----BEGIN CERTIFICATE-----
  78. MIIB/DCCAaKgAwIBAgIQHHFuGMz0cClfde5kqP5prTAKBggqhkjOPQQDAjAqMSgw
  79. JgYDVQQDEx9Hb29nbGUgQ0FTIFRlc3QgSW50ZXJtZWRpYXRlIENBMB4XDTIwMDkx
  80. NTAwMDQ0M1oXDTMwMDkxMzAwMDQ0MFowHTEbMBkGA1UEAxMSdGVzdC5zbWFsbHN0
  81. ZXAuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEMqNCiXMvbn74LsHzRv+8
  82. 17m9vEzH6RHrg3m82e0uEc36+fZWV/zJ9SKuONmnl5VP79LsjL5SVH0RDj73U2XO
  83. DKOBtjCBszAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG
  84. AQUFBwMCMB0GA1UdDgQWBBRTA2cTs7PCNjnps/+T0dS8diqv0DAfBgNVHSMEGDAW
  85. gBRIOVqyLDSlErJLuWWEvRm5UU1r1TBCBgwrBgEEAYKkZMYoQAIEMjAwEwhjbG91
  86. ZGNhcxMkZDhkMThhNjgtNTI5Ni00YWYzLWFlNGItMmY4NzdkYTNmYmQ5MAoGCCqG
  87. SM49BAMCA0gAMEUCIGxl+pqJ50WYWUqK2l4V1FHoXSi0Nht5kwTxFxnWZu1xAiEA
  88. zemu3bhWLFaGg3s8i+HTEhw4RqkHP74vF7AVYp88bAw=
  89. -----END CERTIFICATE-----`
  90. testIntermediateCsr = `-----BEGIN CERTIFICATE REQUEST-----
  91. MIHeMIGFAgEAMCMxITAfBgNVBAMTGENsb3VkQ0FTIEludGVybWVkaWF0ZSBDQTBZ
  92. MBMGByqGSM49AgEGCCqGSM49AwEHA0IABPLuqxgBY+QmaXc8zKIC8FMgjJ6dF/cL
  93. b+Dig0XKc5GH/T1ORrhgOkRayrQcjPMu+jkjg25qn6vvp43LRtUKPXOgADAKBggq
  94. hkjOPQQDAgNIADBFAiEAn3pkYXb2OzoQZ+AExFqd7qZ7pg2nyP2kBZZ01Pl8KfcC
  95. IHKplBXDR79/i7kjOtv1iWfgf5S/XQHrz178gXA0YQe7
  96. -----END CERTIFICATE REQUEST-----`
  97. testRootKey = `-----BEGIN EC PRIVATE KEY-----
  98. MHcCAQEEIN51Rgg6YcQVLeCRzumdw4pjM3VWqFIdCbnsV3Up1e/goAoGCCqGSM49
  99. AwEHoUQDQgAEjJIcDhvvxi7gu4aFkiW/8+E3BfPhmhXU5RlDQusre+MHXc7XYMtk
  100. Lm6PXPeTF1DNdS21Ju1G/j1yUykGJOmxkg==
  101. -----END EC PRIVATE KEY-----`
  102. // nolint:unused,deadcode,gocritic
  103. testIntermediateKey = `-----BEGIN EC PRIVATE KEY-----
  104. MHcCAQEEIMMX/XkXGnRDD4fYu7Z4rHACdJn/iyOy2UTwsv+oZ0C+oAoGCCqGSM49
  105. AwEHoUQDQgAE8u6rGAFj5CZpdzzMogLwUyCMnp0X9wtv4OKDRcpzkYf9PU5GuGA6
  106. RFrKtByM8y76OSODbmqfq++njctG1Qo9cw==
  107. -----END EC PRIVATE KEY-----`
  108. )
  109. type testClient struct {
  110. credentialsFile string
  111. certificate *pb.Certificate
  112. certificateAuthority *pb.CertificateAuthority
  113. err error
  114. }
  115. func newTestClient(credentialsFile string) (CertificateAuthorityClient, error) {
  116. if credentialsFile == "testdata/error.json" {
  117. return nil, errTest
  118. }
  119. return &testClient{
  120. credentialsFile: credentialsFile,
  121. }, nil
  122. }
  123. func okTestClient() *testClient {
  124. return &testClient{
  125. credentialsFile: "testdata/credentials.json",
  126. certificate: &pb.Certificate{
  127. Name: testCertificateName,
  128. PemCertificate: testSignedCertificate,
  129. PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
  130. },
  131. certificateAuthority: &pb.CertificateAuthority{
  132. PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
  133. },
  134. }
  135. }
  136. func failTestClient() *testClient {
  137. return &testClient{
  138. credentialsFile: "testdata/credentials.json",
  139. err: errTest,
  140. }
  141. }
  142. func badTestClient() *testClient {
  143. return &testClient{
  144. credentialsFile: "testdata/credentials.json",
  145. certificate: &pb.Certificate{
  146. Name: testCertificateName,
  147. PemCertificate: "not a pem cert",
  148. PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
  149. },
  150. certificateAuthority: &pb.CertificateAuthority{
  151. PemCaCertificates: []string{testIntermediateCertificate, "not a pem cert"},
  152. },
  153. }
  154. }
  155. func setTeeReader(t *testing.T, w *bytes.Buffer) {
  156. t.Helper()
  157. reader := rand.Reader
  158. t.Cleanup(func() {
  159. rand.Reader = reader
  160. })
  161. rand.Reader = io.TeeReader(reader, w)
  162. }
  163. type badSigner struct {
  164. pub crypto.PublicKey
  165. }
  166. func createBadSigner(t *testing.T) *badSigner {
  167. t.Helper()
  168. pub, _, err := ed25519.GenerateKey(rand.Reader)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. return &badSigner{
  173. pub: pub,
  174. }
  175. }
  176. func (b *badSigner) Public() crypto.PublicKey {
  177. return b.pub
  178. }
  179. func (b *badSigner) Sign(rnd io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
  180. return nil, fmt.Errorf("💥")
  181. }
  182. func (c *testClient) CreateCertificate(ctx context.Context, req *pb.CreateCertificateRequest, opts ...gax.CallOption) (*pb.Certificate, error) {
  183. return c.certificate, c.err
  184. }
  185. func (c *testClient) RevokeCertificate(ctx context.Context, req *pb.RevokeCertificateRequest, opts ...gax.CallOption) (*pb.Certificate, error) {
  186. return c.certificate, c.err
  187. }
  188. func (c *testClient) GetCertificateAuthority(ctx context.Context, req *pb.GetCertificateAuthorityRequest, opts ...gax.CallOption) (*pb.CertificateAuthority, error) {
  189. return c.certificateAuthority, c.err
  190. }
  191. func (c *testClient) CreateCertificateAuthority(ctx context.Context, req *pb.CreateCertificateAuthorityRequest, opts ...gax.CallOption) (*privateca.CreateCertificateAuthorityOperation, error) {
  192. return nil, errors.New("use NewMockCertificateAuthorityClient")
  193. }
  194. func (c *testClient) FetchCertificateAuthorityCsr(ctx context.Context, req *pb.FetchCertificateAuthorityCsrRequest, opts ...gax.CallOption) (*pb.FetchCertificateAuthorityCsrResponse, error) {
  195. return nil, errors.New("use NewMockCertificateAuthorityClient")
  196. }
  197. func (c *testClient) ActivateCertificateAuthority(ctx context.Context, req *pb.ActivateCertificateAuthorityRequest, opts ...gax.CallOption) (*privateca.ActivateCertificateAuthorityOperation, error) {
  198. return nil, errors.New("use NewMockCertificateAuthorityClient")
  199. }
  200. func (c *testClient) EnableCertificateAuthority(ctx context.Context, req *pb.EnableCertificateAuthorityRequest, opts ...gax.CallOption) (*privateca.EnableCertificateAuthorityOperation, error) {
  201. return nil, errors.New("use NewMockCertificateAuthorityClient")
  202. }
  203. func (c *testClient) GetCaPool(ctx context.Context, req *pb.GetCaPoolRequest, opts ...gax.CallOption) (*pb.CaPool, error) {
  204. return nil, errors.New("use NewMockCertificateAuthorityClient")
  205. }
  206. func (c *testClient) CreateCaPool(ctx context.Context, req *pb.CreateCaPoolRequest, opts ...gax.CallOption) (*privateca.CreateCaPoolOperation, error) {
  207. return nil, errors.New("use NewMockCertificateAuthorityClient")
  208. }
  209. func mustParseCertificate(t *testing.T, pemCert string) *x509.Certificate {
  210. t.Helper()
  211. crt, err := parseCertificate(pemCert)
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. return crt
  216. }
  217. func mustParseECKey(t *testing.T, pemKey string) *ecdsa.PrivateKey {
  218. t.Helper()
  219. block, _ := pem.Decode([]byte(pemKey))
  220. if block == nil {
  221. t.Fatal("failed to parse key")
  222. }
  223. key, err := x509.ParseECPrivateKey(block.Bytes)
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. return key
  228. }
  229. func TestNew(t *testing.T) {
  230. tmp := newCertificateAuthorityClient
  231. newCertificateAuthorityClient = func(ctx context.Context, credentialsFile string) (CertificateAuthorityClient, error) {
  232. return newTestClient(credentialsFile)
  233. }
  234. t.Cleanup(func() {
  235. newCertificateAuthorityClient = tmp
  236. })
  237. type args struct {
  238. ctx context.Context
  239. opts apiv1.Options
  240. }
  241. tests := []struct {
  242. name string
  243. args args
  244. want *CloudCAS
  245. wantErr bool
  246. }{
  247. {"ok", args{context.Background(), apiv1.Options{
  248. CertificateAuthority: testAuthorityName,
  249. }}, &CloudCAS{
  250. client: &testClient{},
  251. certificateAuthority: testAuthorityName,
  252. project: testProject,
  253. location: testLocation,
  254. caPool: testCaPool,
  255. caPoolTier: 0,
  256. }, false},
  257. {"ok authority and creator", args{context.Background(), apiv1.Options{
  258. CertificateAuthority: testAuthorityName, IsCreator: true,
  259. }}, &CloudCAS{
  260. client: &testClient{},
  261. certificateAuthority: testAuthorityName,
  262. project: testProject,
  263. location: testLocation,
  264. caPool: testCaPool,
  265. caPoolTier: 0,
  266. }, false},
  267. {"ok with credentials", args{context.Background(), apiv1.Options{
  268. CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/credentials.json",
  269. }}, &CloudCAS{
  270. client: &testClient{credentialsFile: "testdata/credentials.json"},
  271. certificateAuthority: testAuthorityName,
  272. project: testProject,
  273. location: testLocation,
  274. caPool: testCaPool,
  275. caPoolTier: 0,
  276. }, false},
  277. {"ok creator", args{context.Background(), apiv1.Options{
  278. IsCreator: true, Project: testProject, Location: testLocation, CaPool: testCaPool,
  279. }}, &CloudCAS{
  280. client: &testClient{},
  281. project: testProject,
  282. location: testLocation,
  283. caPool: testCaPool,
  284. caPoolTier: pb.CaPool_DEVOPS,
  285. }, false},
  286. {"ok creator devops", args{context.Background(), apiv1.Options{
  287. IsCreator: true, Project: testProject, Location: testLocation, CaPool: testCaPool, CaPoolTier: "DevOps",
  288. }}, &CloudCAS{
  289. client: &testClient{},
  290. project: testProject,
  291. location: testLocation,
  292. caPool: testCaPool,
  293. caPoolTier: pb.CaPool_DEVOPS,
  294. }, false},
  295. {"ok creator enterprise", args{context.Background(), apiv1.Options{
  296. IsCreator: true, Project: testProject, Location: testLocation, CaPool: testCaPool, CaPoolTier: "ENTERPRISE",
  297. }}, &CloudCAS{
  298. client: &testClient{},
  299. project: testProject,
  300. location: testLocation,
  301. caPool: testCaPool,
  302. caPoolTier: pb.CaPool_ENTERPRISE,
  303. }, false},
  304. {"fail certificate authority", args{context.Background(), apiv1.Options{
  305. CertificateAuthority: "projects/ok1234/locations/ok1234/caPools/ok1234/certificateAuthorities/ok1234/bad",
  306. }}, nil, true},
  307. {"fail certificate authority regex", args{context.Background(), apiv1.Options{}}, nil, true},
  308. {"fail with credentials", args{context.Background(), apiv1.Options{
  309. CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/error.json",
  310. }}, nil, true},
  311. {"fail creator project", args{context.Background(), apiv1.Options{
  312. IsCreator: true, Project: "", Location: testLocation,
  313. }}, nil, true},
  314. {"fail creator location", args{context.Background(), apiv1.Options{
  315. IsCreator: true, Project: testProject, Location: "",
  316. }}, nil, true},
  317. {"fail caPool", args{context.Background(), apiv1.Options{
  318. IsCreator: true, Project: testProject, Location: testLocation, CaPool: "",
  319. }}, nil, true},
  320. }
  321. for _, tt := range tests {
  322. t.Run(tt.name, func(t *testing.T) {
  323. got, err := New(tt.args.ctx, tt.args.opts)
  324. if (err != nil) != tt.wantErr {
  325. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  326. return
  327. }
  328. if !reflect.DeepEqual(got, tt.want) {
  329. t.Errorf("New() = %v, want %v", got, tt.want)
  330. }
  331. })
  332. }
  333. }
  334. func TestNew_register(t *testing.T) {
  335. tmp := newCertificateAuthorityClient
  336. newCertificateAuthorityClient = func(ctx context.Context, credentialsFile string) (CertificateAuthorityClient, error) {
  337. return newTestClient(credentialsFile)
  338. }
  339. t.Cleanup(func() {
  340. newCertificateAuthorityClient = tmp
  341. })
  342. want := &CloudCAS{
  343. client: &testClient{credentialsFile: "testdata/credentials.json"},
  344. certificateAuthority: testAuthorityName,
  345. project: testProject,
  346. location: testLocation,
  347. caPool: testCaPool,
  348. }
  349. newFn, ok := apiv1.LoadCertificateAuthorityServiceNewFunc(apiv1.CloudCAS)
  350. if !ok {
  351. t.Error("apiv1.LoadCertificateAuthorityServiceNewFunc(apiv1.CloudCAS) was not found")
  352. return
  353. }
  354. got, err := newFn(context.Background(), apiv1.Options{
  355. CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/credentials.json",
  356. })
  357. if err != nil {
  358. t.Errorf("New() error = %v", err)
  359. return
  360. }
  361. if !reflect.DeepEqual(got, want) {
  362. t.Errorf("New() = %v, want %v", got, want)
  363. }
  364. }
  365. func TestNew_real(t *testing.T) {
  366. if v, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS"); ok {
  367. os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS")
  368. t.Cleanup(func() {
  369. os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", v)
  370. })
  371. }
  372. type args struct {
  373. ctx context.Context
  374. opts apiv1.Options
  375. }
  376. tests := []struct {
  377. name string
  378. skipOnCI bool
  379. args args
  380. wantErr bool
  381. }{
  382. {"fail default credentials", true, args{context.Background(), apiv1.Options{CertificateAuthority: testAuthorityName}}, true},
  383. {"fail certificate authority", false, args{context.Background(), apiv1.Options{}}, true},
  384. {"fail with credentials", false, args{context.Background(), apiv1.Options{
  385. CertificateAuthority: testAuthorityName, CredentialsFile: "testdata/missing.json",
  386. }}, true},
  387. }
  388. for _, tt := range tests {
  389. t.Run(tt.name, func(t *testing.T) {
  390. if tt.skipOnCI && os.Getenv("CI") == "true" {
  391. t.SkipNow()
  392. }
  393. _, err := New(tt.args.ctx, tt.args.opts)
  394. if (err != nil) != tt.wantErr {
  395. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  396. }
  397. })
  398. }
  399. }
  400. func TestCloudCAS_GetCertificateAuthority(t *testing.T) {
  401. root := mustParseCertificate(t, testRootCertificate)
  402. type fields struct {
  403. client CertificateAuthorityClient
  404. certificateAuthority string
  405. }
  406. type args struct {
  407. req *apiv1.GetCertificateAuthorityRequest
  408. }
  409. tests := []struct {
  410. name string
  411. fields fields
  412. args args
  413. want *apiv1.GetCertificateAuthorityResponse
  414. wantErr bool
  415. }{
  416. {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, &apiv1.GetCertificateAuthorityResponse{
  417. RootCertificate: root,
  418. }, false},
  419. {"ok with name", fields{okTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{
  420. Name: testCertificateName,
  421. }}, &apiv1.GetCertificateAuthorityResponse{
  422. RootCertificate: root,
  423. }, false},
  424. {"fail GetCertificateAuthority", fields{failTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
  425. {"fail bad root", fields{badTestClient(), testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
  426. {"fail no pems", fields{&testClient{certificateAuthority: &pb.CertificateAuthority{}}, testCertificateName}, args{&apiv1.GetCertificateAuthorityRequest{}}, nil, true},
  427. }
  428. for _, tt := range tests {
  429. t.Run(tt.name, func(t *testing.T) {
  430. c := &CloudCAS{
  431. client: tt.fields.client,
  432. certificateAuthority: tt.fields.certificateAuthority,
  433. }
  434. got, err := c.GetCertificateAuthority(tt.args.req)
  435. if (err != nil) != tt.wantErr {
  436. t.Errorf("CloudCAS.GetCertificateAuthority() error = %v, wantErr %v", err, tt.wantErr)
  437. return
  438. }
  439. if !reflect.DeepEqual(got, tt.want) {
  440. t.Errorf("CloudCAS.GetCertificateAuthority() = %v, want %v", got, tt.want)
  441. }
  442. })
  443. }
  444. }
  445. func TestCloudCAS_CreateCertificate(t *testing.T) {
  446. type fields struct {
  447. client CertificateAuthorityClient
  448. certificateAuthority string
  449. }
  450. type args struct {
  451. req *apiv1.CreateCertificateRequest
  452. }
  453. tests := []struct {
  454. name string
  455. fields fields
  456. args args
  457. want *apiv1.CreateCertificateResponse
  458. wantErr bool
  459. }{
  460. {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
  461. Template: mustParseCertificate(t, testLeafCertificate),
  462. Lifetime: 24 * time.Hour,
  463. }}, &apiv1.CreateCertificateResponse{
  464. Certificate: mustParseCertificate(t, testSignedCertificate),
  465. CertificateChain: []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)},
  466. }, false},
  467. {"fail Template", fields{okTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
  468. Lifetime: 24 * time.Hour,
  469. }}, nil, true},
  470. {"fail Lifetime", fields{okTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
  471. Template: mustParseCertificate(t, testLeafCertificate),
  472. }}, nil, true},
  473. {"fail CreateCertificate", fields{failTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
  474. Template: mustParseCertificate(t, testLeafCertificate),
  475. Lifetime: 24 * time.Hour,
  476. }}, nil, true},
  477. {"fail Certificate", fields{badTestClient(), testCertificateName}, args{&apiv1.CreateCertificateRequest{
  478. Template: mustParseCertificate(t, testLeafCertificate),
  479. Lifetime: 24 * time.Hour,
  480. }}, nil, true},
  481. }
  482. for _, tt := range tests {
  483. t.Run(tt.name, func(t *testing.T) {
  484. c := &CloudCAS{
  485. client: tt.fields.client,
  486. certificateAuthority: tt.fields.certificateAuthority,
  487. }
  488. got, err := c.CreateCertificate(tt.args.req)
  489. if (err != nil) != tt.wantErr {
  490. t.Errorf("CloudCAS.CreateCertificate() error = %v, wantErr %v", err, tt.wantErr)
  491. return
  492. }
  493. if !reflect.DeepEqual(got, tt.want) {
  494. t.Errorf("CloudCAS.CreateCertificate() = %v, want %v", got, tt.want)
  495. }
  496. })
  497. }
  498. }
  499. func TestCloudCAS_createCertificate(t *testing.T) {
  500. leaf := mustParseCertificate(t, testLeafCertificate)
  501. signed := mustParseCertificate(t, testSignedCertificate)
  502. chain := []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)}
  503. type fields struct {
  504. client CertificateAuthorityClient
  505. certificateAuthority string
  506. }
  507. type args struct {
  508. tpl *x509.Certificate
  509. lifetime time.Duration
  510. requestID string
  511. }
  512. tests := []struct {
  513. name string
  514. fields fields
  515. args args
  516. want *x509.Certificate
  517. want1 []*x509.Certificate
  518. wantErr bool
  519. }{
  520. {"ok", fields{okTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, signed, chain, false},
  521. {"fail CertificateConfig", fields{okTestClient(), testAuthorityName}, args{&x509.Certificate{}, 24 * time.Hour, "request-id"}, nil, nil, true},
  522. {"fail CreateCertificate", fields{failTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
  523. {"fail ParseCertificates", fields{badTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
  524. {"fail create id", fields{okTestClient(), testAuthorityName}, args{leaf, 24 * time.Hour, "request-id"}, nil, nil, true},
  525. }
  526. // Pre-calculate rand.Random
  527. buf := new(bytes.Buffer)
  528. setTeeReader(t, buf)
  529. for i := 0; i < len(tests)-1; i++ {
  530. _, err := uuid.NewRandomFromReader(rand.Reader)
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. }
  535. rand.Reader = buf
  536. for _, tt := range tests {
  537. t.Run(tt.name, func(t *testing.T) {
  538. c := &CloudCAS{
  539. client: tt.fields.client,
  540. certificateAuthority: tt.fields.certificateAuthority,
  541. }
  542. got, got1, err := c.createCertificate(tt.args.tpl, tt.args.lifetime, tt.args.requestID)
  543. if (err != nil) != tt.wantErr {
  544. t.Errorf("CloudCAS.createCertificate() error = %v, wantErr %v", err, tt.wantErr)
  545. return
  546. }
  547. if !reflect.DeepEqual(got, tt.want) {
  548. t.Errorf("CloudCAS.createCertificate() got = %v, want %v", got, tt.want)
  549. }
  550. if !reflect.DeepEqual(got1, tt.want1) {
  551. t.Errorf("CloudCAS.createCertificate() got1 = %v, want %v", got1, tt.want1)
  552. }
  553. })
  554. }
  555. }
  556. func TestCloudCAS_RenewCertificate(t *testing.T) {
  557. type fields struct {
  558. client CertificateAuthorityClient
  559. certificateAuthority string
  560. }
  561. type args struct {
  562. req *apiv1.RenewCertificateRequest
  563. }
  564. tests := []struct {
  565. name string
  566. fields fields
  567. args args
  568. want *apiv1.RenewCertificateResponse
  569. wantErr bool
  570. }{
  571. {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
  572. Template: mustParseCertificate(t, testLeafCertificate),
  573. Lifetime: 24 * time.Hour,
  574. }}, &apiv1.RenewCertificateResponse{
  575. Certificate: mustParseCertificate(t, testSignedCertificate),
  576. CertificateChain: []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)},
  577. }, false},
  578. {"fail Template", fields{okTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
  579. Lifetime: 24 * time.Hour,
  580. }}, nil, true},
  581. {"fail Lifetime", fields{okTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
  582. Template: mustParseCertificate(t, testLeafCertificate),
  583. }}, nil, true},
  584. {"fail CreateCertificate", fields{failTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
  585. Template: mustParseCertificate(t, testLeafCertificate),
  586. Lifetime: 24 * time.Hour,
  587. }}, nil, true},
  588. {"fail Certificate", fields{badTestClient(), testCertificateName}, args{&apiv1.RenewCertificateRequest{
  589. Template: mustParseCertificate(t, testLeafCertificate),
  590. Lifetime: 24 * time.Hour,
  591. }}, nil, true},
  592. }
  593. for _, tt := range tests {
  594. t.Run(tt.name, func(t *testing.T) {
  595. c := &CloudCAS{
  596. client: tt.fields.client,
  597. certificateAuthority: tt.fields.certificateAuthority,
  598. }
  599. got, err := c.RenewCertificate(tt.args.req)
  600. if (err != nil) != tt.wantErr {
  601. t.Errorf("CloudCAS.RenewCertificate() error = %v, wantErr %v", err, tt.wantErr)
  602. return
  603. }
  604. if !reflect.DeepEqual(got, tt.want) {
  605. t.Errorf("CloudCAS.RenewCertificate() = %v, want %v", got, tt.want)
  606. }
  607. })
  608. }
  609. }
  610. func TestCloudCAS_RevokeCertificate(t *testing.T) {
  611. badExtensionCert := mustParseCertificate(t, testSignedCertificate)
  612. for i, ext := range badExtensionCert.Extensions {
  613. if ext.Id.Equal(asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 37476, 9000, 64, 2}) {
  614. badExtensionCert.Extensions[i].Value = []byte("bad-data")
  615. }
  616. }
  617. type fields struct {
  618. client CertificateAuthorityClient
  619. certificateAuthority string
  620. }
  621. type args struct {
  622. req *apiv1.RevokeCertificateRequest
  623. }
  624. tests := []struct {
  625. name string
  626. fields fields
  627. args args
  628. want *apiv1.RevokeCertificateResponse
  629. wantErr bool
  630. }{
  631. {"ok", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  632. Certificate: mustParseCertificate(t, testSignedCertificate),
  633. ReasonCode: 1,
  634. }}, &apiv1.RevokeCertificateResponse{
  635. Certificate: mustParseCertificate(t, testSignedCertificate),
  636. CertificateChain: []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)},
  637. }, false},
  638. {"fail Extension", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  639. Certificate: mustParseCertificate(t, testLeafCertificate),
  640. ReasonCode: 1,
  641. }}, nil, true},
  642. {"fail Extension Value", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  643. Certificate: badExtensionCert,
  644. ReasonCode: 1,
  645. }}, nil, true},
  646. {"fail Certificate", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  647. ReasonCode: 2,
  648. }}, nil, true},
  649. {"fail ReasonCode", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  650. Certificate: mustParseCertificate(t, testSignedCertificate),
  651. ReasonCode: 100,
  652. }}, nil, true},
  653. {"fail ReasonCode 7", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  654. Certificate: mustParseCertificate(t, testSignedCertificate),
  655. ReasonCode: 7,
  656. }}, nil, true},
  657. {"fail ReasonCode 8", fields{okTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  658. Certificate: mustParseCertificate(t, testSignedCertificate),
  659. ReasonCode: 8,
  660. }}, nil, true},
  661. {"fail RevokeCertificate", fields{failTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  662. Certificate: mustParseCertificate(t, testSignedCertificate),
  663. ReasonCode: 1,
  664. }}, nil, true},
  665. {"fail ParseCertificate", fields{badTestClient(), testCertificateName}, args{&apiv1.RevokeCertificateRequest{
  666. Certificate: mustParseCertificate(t, testSignedCertificate),
  667. ReasonCode: 1,
  668. }}, nil, true},
  669. }
  670. for _, tt := range tests {
  671. t.Run(tt.name, func(t *testing.T) {
  672. c := &CloudCAS{
  673. client: tt.fields.client,
  674. certificateAuthority: tt.fields.certificateAuthority,
  675. }
  676. got, err := c.RevokeCertificate(tt.args.req)
  677. if (err != nil) != tt.wantErr {
  678. t.Errorf("CloudCAS.RevokeCertificate() error = %v, wantErr %v", err, tt.wantErr)
  679. return
  680. }
  681. if !reflect.DeepEqual(got, tt.want) {
  682. t.Errorf("CloudCAS.RevokeCertificate() = %v, want %v", got, tt.want)
  683. }
  684. })
  685. }
  686. }
  687. func Test_createCertificateID(t *testing.T) {
  688. buf := new(bytes.Buffer)
  689. setTeeReader(t, buf)
  690. id, err := uuid.NewRandomFromReader(rand.Reader)
  691. if err != nil {
  692. t.Fatal(err)
  693. }
  694. rand.Reader = buf
  695. tests := []struct {
  696. name string
  697. want string
  698. wantErr bool
  699. }{
  700. {"ok", id.String(), false},
  701. {"fail", "", true},
  702. }
  703. for _, tt := range tests {
  704. t.Run(tt.name, func(t *testing.T) {
  705. got, err := createCertificateID()
  706. if (err != nil) != tt.wantErr {
  707. t.Errorf("createCertificateID() error = %v, wantErr %v", err, tt.wantErr)
  708. return
  709. }
  710. if got != tt.want {
  711. t.Errorf("createCertificateID() = %v, want %v", got, tt.want)
  712. }
  713. })
  714. }
  715. }
  716. func Test_parseCertificate(t *testing.T) {
  717. type args struct {
  718. pemCert string
  719. }
  720. tests := []struct {
  721. name string
  722. args args
  723. want *x509.Certificate
  724. wantErr bool
  725. }{
  726. {"ok", args{testLeafCertificate}, mustParseCertificate(t, testLeafCertificate), false},
  727. {"ok intermediate", args{testIntermediateCertificate}, mustParseCertificate(t, testIntermediateCertificate), false},
  728. {"fail pem", args{"not pem"}, nil, true},
  729. {"fail parseCertificate", args{"-----BEGIN CERTIFICATE-----\nZm9vYmFyCg==\n-----END CERTIFICATE-----\n"}, nil, true},
  730. }
  731. for _, tt := range tests {
  732. t.Run(tt.name, func(t *testing.T) {
  733. got, err := parseCertificate(tt.args.pemCert)
  734. if (err != nil) != tt.wantErr {
  735. t.Errorf("parseCertificate() error = %v, wantErr %v", err, tt.wantErr)
  736. return
  737. }
  738. if !reflect.DeepEqual(got, tt.want) {
  739. t.Errorf("parseCertificate() = %v, want %v", got, tt.want)
  740. }
  741. })
  742. }
  743. }
  744. func Test_getCertificateAndChain(t *testing.T) {
  745. type args struct {
  746. certpb *pb.Certificate
  747. }
  748. tests := []struct {
  749. name string
  750. args args
  751. want *x509.Certificate
  752. want1 []*x509.Certificate
  753. wantErr bool
  754. }{
  755. {"ok", args{&pb.Certificate{
  756. Name: testCertificateName,
  757. PemCertificate: testSignedCertificate,
  758. PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
  759. }}, mustParseCertificate(t, testSignedCertificate), []*x509.Certificate{mustParseCertificate(t, testIntermediateCertificate)}, false},
  760. {"fail PemCertificate", args{&pb.Certificate{
  761. Name: testCertificateName,
  762. PemCertificate: "foobar",
  763. PemCertificateChain: []string{testIntermediateCertificate, testRootCertificate},
  764. }}, nil, nil, true},
  765. {"fail PemCertificateChain", args{&pb.Certificate{
  766. Name: testCertificateName,
  767. PemCertificate: testSignedCertificate,
  768. PemCertificateChain: []string{"foobar", testRootCertificate},
  769. }}, nil, nil, true},
  770. }
  771. for _, tt := range tests {
  772. t.Run(tt.name, func(t *testing.T) {
  773. got, got1, err := getCertificateAndChain(tt.args.certpb)
  774. if (err != nil) != tt.wantErr {
  775. t.Errorf("getCertificateAndChain() error = %v, wantErr %v", err, tt.wantErr)
  776. return
  777. }
  778. if !reflect.DeepEqual(got, tt.want) {
  779. t.Errorf("getCertificateAndChain() got = %v, want %v", got, tt.want)
  780. }
  781. if !reflect.DeepEqual(got1, tt.want1) {
  782. t.Errorf("getCertificateAndChain() got1 = %v, want %v", got1, tt.want1)
  783. }
  784. })
  785. }
  786. }
  787. func TestCloudCAS_CreateCertificateAuthority(t *testing.T) {
  788. must := func(a, b interface{}) interface{} {
  789. return a
  790. }
  791. ctrl := gomock.NewController(t)
  792. defer ctrl.Finish()
  793. mosCtrl := gomock.NewController(t)
  794. defer mosCtrl.Finish()
  795. m := NewMockCertificateAuthorityClient(ctrl)
  796. mos := NewMockOperationsServer(mosCtrl)
  797. // Create operation server
  798. srv := grpc.NewServer()
  799. longrunningpb.RegisterOperationsServer(srv, mos)
  800. lis := bufconn.Listen(2)
  801. go srv.Serve(lis)
  802. defer srv.Stop()
  803. // Create fake privateca client
  804. conn, err := grpc.DialContext(context.Background(), "", grpc.WithInsecure(),
  805. grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
  806. return lis.Dial()
  807. }))
  808. if err != nil {
  809. t.Fatal(err)
  810. }
  811. client, err := lroauto.NewOperationsClient(context.Background(), option.WithGRPCConn(conn))
  812. if err != nil {
  813. t.Fatal(err)
  814. }
  815. fake, err := privateca.NewCertificateAuthorityClient(context.Background(), option.WithGRPCConn(conn))
  816. if err != nil {
  817. t.Fatal(err)
  818. }
  819. fake.LROClient = client
  820. // Configure mocks
  821. any := gomock.Any()
  822. // ok root
  823. m.EXPECT().GetCaPool(any, any).Return(nil, status.Error(codes.NotFound, "not found"))
  824. m.EXPECT().CreateCaPool(any, any).Return(fake.CreateCaPoolOperation("CreateCaPool"), nil)
  825. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  826. Name: "CreateCaPool",
  827. Done: true,
  828. Result: &longrunningpb.Operation_Response{
  829. Response: must(anypb.New(&pb.CaPool{
  830. Name: testCaPoolName,
  831. })).(*anypb.Any),
  832. },
  833. }, nil)
  834. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  835. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  836. Name: "CreateCertificateAuthority",
  837. Done: true,
  838. Result: &longrunningpb.Operation_Response{
  839. Response: must(anypb.New(&pb.CertificateAuthority{
  840. Name: testAuthorityName,
  841. PemCaCertificates: []string{testRootCertificate},
  842. })).(*anypb.Any),
  843. },
  844. }, nil)
  845. m.EXPECT().EnableCertificateAuthority(any, any).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
  846. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  847. Name: "EnableCertificateAuthority",
  848. Done: true,
  849. Result: &longrunningpb.Operation_Response{
  850. Response: must(anypb.New(&pb.CertificateAuthority{
  851. Name: testAuthorityName,
  852. PemCaCertificates: []string{testRootCertificate},
  853. })).(*anypb.Any),
  854. },
  855. }, nil)
  856. // ok intermediate
  857. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  858. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  859. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  860. Name: "CreateCertificateAuthority",
  861. Done: true,
  862. Result: &longrunningpb.Operation_Response{
  863. Response: must(anypb.New(&pb.CertificateAuthority{
  864. Name: testAuthorityName,
  865. })).(*anypb.Any),
  866. },
  867. }, nil)
  868. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  869. PemCsr: testIntermediateCsr,
  870. }, nil)
  871. m.EXPECT().CreateCertificate(any, any).Return(&pb.Certificate{
  872. PemCertificate: testIntermediateCertificate,
  873. PemCertificateChain: []string{testRootCertificate},
  874. }, nil)
  875. m.EXPECT().ActivateCertificateAuthority(any, any).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
  876. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  877. Name: "ActivateCertificateAuthority",
  878. Done: true,
  879. Result: &longrunningpb.Operation_Response{
  880. Response: must(anypb.New(&pb.CertificateAuthority{
  881. Name: testAuthorityName,
  882. PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
  883. })).(*anypb.Any),
  884. },
  885. }, nil)
  886. m.EXPECT().EnableCertificateAuthority(any, any).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
  887. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  888. Name: "EnableCertificateAuthority",
  889. Done: true,
  890. Result: &longrunningpb.Operation_Response{
  891. Response: must(anypb.New(&pb.CertificateAuthority{
  892. Name: testAuthorityName,
  893. PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
  894. })).(*anypb.Any),
  895. },
  896. }, nil)
  897. // ok intermediate local signer
  898. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  899. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  900. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  901. Name: "CreateCertificateAuthority",
  902. Done: true,
  903. Result: &longrunningpb.Operation_Response{
  904. Response: must(anypb.New(&pb.CertificateAuthority{
  905. Name: testAuthorityName,
  906. })).(*anypb.Any),
  907. },
  908. }, nil)
  909. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  910. PemCsr: testIntermediateCsr,
  911. }, nil)
  912. m.EXPECT().ActivateCertificateAuthority(any, any).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
  913. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  914. Name: "ActivateCertificateAuthority",
  915. Done: true,
  916. Result: &longrunningpb.Operation_Response{
  917. Response: must(anypb.New(&pb.CertificateAuthority{
  918. Name: testAuthorityName,
  919. PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
  920. })).(*anypb.Any),
  921. },
  922. }, nil)
  923. m.EXPECT().EnableCertificateAuthority(any, any).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
  924. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  925. Name: "EnableCertificateAuthority",
  926. Done: true,
  927. Result: &longrunningpb.Operation_Response{
  928. Response: must(anypb.New(&pb.CertificateAuthority{
  929. Name: testAuthorityName,
  930. PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
  931. })).(*anypb.Any),
  932. },
  933. }, nil)
  934. // ok create key
  935. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  936. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  937. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  938. Name: "CreateCertificateAuthority",
  939. Done: true,
  940. Result: &longrunningpb.Operation_Response{
  941. Response: must(anypb.New(&pb.CertificateAuthority{
  942. Name: testAuthorityName,
  943. PemCaCertificates: []string{testRootCertificate},
  944. })).(*anypb.Any),
  945. },
  946. }, nil)
  947. m.EXPECT().EnableCertificateAuthority(any, any).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
  948. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  949. Name: "EnableCertificateAuthority",
  950. Done: true,
  951. Result: &longrunningpb.Operation_Response{
  952. Response: must(anypb.New(&pb.CertificateAuthority{
  953. Name: testAuthorityName,
  954. PemCaCertificates: []string{testRootCertificate},
  955. })).(*anypb.Any),
  956. },
  957. }, nil)
  958. // fail GetCaPool
  959. m.EXPECT().GetCaPool(any, any).Return(nil, errTest)
  960. // fail CreateCaPool
  961. m.EXPECT().GetCaPool(any, any).Return(nil, status.Error(codes.NotFound, "not found"))
  962. m.EXPECT().CreateCaPool(any, any).Return(nil, errTest)
  963. // fail CreateCaPool.Wait
  964. m.EXPECT().GetCaPool(any, any).Return(nil, status.Error(codes.NotFound, "not found"))
  965. m.EXPECT().CreateCaPool(any, any).Return(fake.CreateCaPoolOperation("CreateCaPool"), nil)
  966. mos.EXPECT().GetOperation(any, any).Return(nil, errTest)
  967. // fail CreateCertificateAuthority
  968. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  969. m.EXPECT().CreateCertificateAuthority(any, any).Return(nil, errTest)
  970. // fail CreateCertificateAuthority.Wait
  971. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  972. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  973. mos.EXPECT().GetOperation(any, any).Return(nil, errTest)
  974. // fail EnableCertificateAuthority
  975. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  976. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  977. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  978. Name: "CreateCertificateAuthority",
  979. Done: true,
  980. Result: &longrunningpb.Operation_Response{
  981. Response: must(anypb.New(&pb.CertificateAuthority{
  982. Name: testAuthorityName,
  983. PemCaCertificates: []string{testRootCertificate},
  984. })).(*anypb.Any),
  985. },
  986. }, nil)
  987. m.EXPECT().EnableCertificateAuthority(any, any).Return(nil, errTest)
  988. // fail EnableCertificateAuthority.Wait
  989. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  990. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  991. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  992. Name: "CreateCertificateAuthority",
  993. Done: true,
  994. Result: &longrunningpb.Operation_Response{
  995. Response: must(anypb.New(&pb.CertificateAuthority{
  996. Name: testAuthorityName,
  997. PemCaCertificates: []string{testRootCertificate},
  998. })).(*anypb.Any),
  999. },
  1000. }, nil)
  1001. m.EXPECT().EnableCertificateAuthority(any, any).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
  1002. mos.EXPECT().GetOperation(any, any).Return(nil, errTest)
  1003. // fail EnableCertificateAuthority intermediate
  1004. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1005. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1006. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1007. Name: "CreateCertificateAuthority",
  1008. Done: true,
  1009. Result: &longrunningpb.Operation_Response{
  1010. Response: must(anypb.New(&pb.CertificateAuthority{
  1011. Name: testAuthorityName,
  1012. })).(*anypb.Any),
  1013. },
  1014. }, nil)
  1015. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  1016. PemCsr: testIntermediateCsr,
  1017. }, nil)
  1018. m.EXPECT().CreateCertificate(any, any).Return(&pb.Certificate{
  1019. PemCertificate: testIntermediateCertificate,
  1020. PemCertificateChain: []string{testRootCertificate},
  1021. }, nil)
  1022. m.EXPECT().ActivateCertificateAuthority(any, any).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
  1023. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1024. Name: "ActivateCertificateAuthority",
  1025. Done: true,
  1026. Result: &longrunningpb.Operation_Response{
  1027. Response: must(anypb.New(&pb.CertificateAuthority{
  1028. Name: testAuthorityName,
  1029. PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
  1030. })).(*anypb.Any),
  1031. },
  1032. }, nil)
  1033. m.EXPECT().EnableCertificateAuthority(any, any).Return(nil, errTest)
  1034. // fail EnableCertificateAuthority.Wait intermediate
  1035. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1036. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1037. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1038. Name: "CreateCertificateAuthority",
  1039. Done: true,
  1040. Result: &longrunningpb.Operation_Response{
  1041. Response: must(anypb.New(&pb.CertificateAuthority{
  1042. Name: testAuthorityName,
  1043. })).(*anypb.Any),
  1044. },
  1045. }, nil)
  1046. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  1047. PemCsr: testIntermediateCsr,
  1048. }, nil)
  1049. m.EXPECT().CreateCertificate(any, any).Return(&pb.Certificate{
  1050. PemCertificate: testIntermediateCertificate,
  1051. PemCertificateChain: []string{testRootCertificate},
  1052. }, nil)
  1053. m.EXPECT().ActivateCertificateAuthority(any, any).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
  1054. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1055. Name: "ActivateCertificateAuthority",
  1056. Done: true,
  1057. Result: &longrunningpb.Operation_Response{
  1058. Response: must(anypb.New(&pb.CertificateAuthority{
  1059. Name: testAuthorityName,
  1060. PemCaCertificates: []string{testIntermediateCertificate, testRootCertificate},
  1061. })).(*anypb.Any),
  1062. },
  1063. }, nil)
  1064. m.EXPECT().EnableCertificateAuthority(any, any).Return(fake.EnableCertificateAuthorityOperation("EnableCertificateAuthorityOperation"), nil)
  1065. mos.EXPECT().GetOperation(any, any).Return(nil, errTest)
  1066. // fail FetchCertificateAuthorityCsr
  1067. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1068. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1069. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1070. Name: "CreateCertificateAuthority",
  1071. Done: true,
  1072. Result: &longrunningpb.Operation_Response{
  1073. Response: must(anypb.New(&pb.CertificateAuthority{
  1074. Name: testAuthorityName,
  1075. })).(*anypb.Any),
  1076. },
  1077. }, nil)
  1078. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(nil, errTest)
  1079. // fail CreateCertificate
  1080. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1081. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1082. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1083. Name: "CreateCertificateAuthority",
  1084. Done: true,
  1085. Result: &longrunningpb.Operation_Response{
  1086. Response: must(anypb.New(&pb.CertificateAuthority{
  1087. Name: testAuthorityName,
  1088. })).(*anypb.Any),
  1089. },
  1090. }, nil)
  1091. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  1092. PemCsr: testIntermediateCsr,
  1093. }, nil)
  1094. m.EXPECT().CreateCertificate(any, any).Return(nil, errTest)
  1095. // fail ActivateCertificateAuthority
  1096. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1097. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1098. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1099. Name: "CreateCertificateAuthority",
  1100. Done: true,
  1101. Result: &longrunningpb.Operation_Response{
  1102. Response: must(anypb.New(&pb.CertificateAuthority{
  1103. Name: testAuthorityName,
  1104. })).(*anypb.Any),
  1105. },
  1106. }, nil)
  1107. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  1108. PemCsr: testIntermediateCsr,
  1109. }, nil)
  1110. m.EXPECT().CreateCertificate(any, any).Return(&pb.Certificate{
  1111. PemCertificate: testIntermediateCertificate,
  1112. PemCertificateChain: []string{testRootCertificate},
  1113. }, nil)
  1114. m.EXPECT().ActivateCertificateAuthority(any, any).Return(nil, errTest)
  1115. // fail ActivateCertificateAuthority.Wait
  1116. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1117. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1118. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1119. Name: "CreateCertificateAuthority",
  1120. Done: true,
  1121. Result: &longrunningpb.Operation_Response{
  1122. Response: must(anypb.New(&pb.CertificateAuthority{
  1123. Name: testAuthorityName,
  1124. })).(*anypb.Any),
  1125. },
  1126. }, nil)
  1127. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  1128. PemCsr: testIntermediateCsr,
  1129. }, nil)
  1130. m.EXPECT().CreateCertificate(any, any).Return(&pb.Certificate{
  1131. PemCertificate: testIntermediateCertificate,
  1132. PemCertificateChain: []string{testRootCertificate},
  1133. }, nil)
  1134. m.EXPECT().ActivateCertificateAuthority(any, any).Return(fake.ActivateCertificateAuthorityOperation("ActivateCertificateAuthority"), nil)
  1135. mos.EXPECT().GetOperation(any, any).Return(nil, errTest)
  1136. // fail x509util.CreateCertificate
  1137. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1138. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1139. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1140. Name: "CreateCertificateAuthority",
  1141. Done: true,
  1142. Result: &longrunningpb.Operation_Response{
  1143. Response: must(anypb.New(&pb.CertificateAuthority{
  1144. Name: testAuthorityName,
  1145. })).(*anypb.Any),
  1146. },
  1147. }, nil)
  1148. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  1149. PemCsr: testIntermediateCsr,
  1150. }, nil)
  1151. // fail parseCertificateRequest
  1152. m.EXPECT().GetCaPool(any, any).Return(&pb.CaPool{Name: testCaPoolName}, nil)
  1153. m.EXPECT().CreateCertificateAuthority(any, any).Return(fake.CreateCertificateAuthorityOperation("CreateCertificateAuthority"), nil)
  1154. mos.EXPECT().GetOperation(any, any).Return(&longrunningpb.Operation{
  1155. Name: "CreateCertificateAuthority",
  1156. Done: true,
  1157. Result: &longrunningpb.Operation_Response{
  1158. Response: must(anypb.New(&pb.CertificateAuthority{
  1159. Name: testAuthorityName,
  1160. })).(*anypb.Any),
  1161. },
  1162. }, nil)
  1163. m.EXPECT().FetchCertificateAuthorityCsr(any, any).Return(&pb.FetchCertificateAuthorityCsrResponse{
  1164. PemCsr: "Not a CSR",
  1165. }, nil)
  1166. rootCrt := mustParseCertificate(t, testRootCertificate)
  1167. intCrt := mustParseCertificate(t, testIntermediateCertificate)
  1168. type fields struct {
  1169. client CertificateAuthorityClient
  1170. certificateAuthority string
  1171. project string
  1172. location string
  1173. caPool string
  1174. caPoolTier pb.CaPool_Tier
  1175. }
  1176. type args struct {
  1177. req *apiv1.CreateCertificateAuthorityRequest
  1178. }
  1179. tests := []struct {
  1180. name string
  1181. fields fields
  1182. args args
  1183. want *apiv1.CreateCertificateAuthorityResponse
  1184. wantErr bool
  1185. }{
  1186. {"ok root", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_ENTERPRISE}, args{&apiv1.CreateCertificateAuthorityRequest{
  1187. Type: apiv1.RootCA,
  1188. Template: mustParseCertificate(t, testRootCertificate),
  1189. Lifetime: 24 * time.Hour,
  1190. }}, &apiv1.CreateCertificateAuthorityResponse{
  1191. Name: testAuthorityName,
  1192. Certificate: rootCrt,
  1193. }, false},
  1194. {"ok intermediate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1195. Type: apiv1.IntermediateCA,
  1196. Template: mustParseCertificate(t, testIntermediateCertificate),
  1197. Lifetime: 24 * time.Hour,
  1198. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1199. Name: testAuthorityName,
  1200. Certificate: rootCrt,
  1201. },
  1202. }}, &apiv1.CreateCertificateAuthorityResponse{
  1203. Name: testAuthorityName,
  1204. Certificate: intCrt,
  1205. CertificateChain: []*x509.Certificate{rootCrt},
  1206. }, false},
  1207. {"ok intermediate local signer", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_ENTERPRISE}, args{&apiv1.CreateCertificateAuthorityRequest{
  1208. Type: apiv1.IntermediateCA,
  1209. Template: mustParseCertificate(t, testIntermediateCertificate),
  1210. Lifetime: 24 * time.Hour,
  1211. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1212. Certificate: rootCrt,
  1213. Signer: mustParseECKey(t, testRootKey),
  1214. },
  1215. }}, &apiv1.CreateCertificateAuthorityResponse{
  1216. Name: testAuthorityName,
  1217. Certificate: intCrt,
  1218. CertificateChain: []*x509.Certificate{rootCrt},
  1219. }, false},
  1220. {"ok create key", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1221. Type: apiv1.RootCA,
  1222. Template: mustParseCertificate(t, testRootCertificate),
  1223. Lifetime: 24 * time.Hour,
  1224. CreateKey: &kmsapi.CreateKeyRequest{
  1225. SignatureAlgorithm: kmsapi.ECDSAWithSHA256,
  1226. },
  1227. }}, &apiv1.CreateCertificateAuthorityResponse{
  1228. Name: testAuthorityName,
  1229. Certificate: rootCrt,
  1230. }, false},
  1231. {"fail project", fields{m, "", "", testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1232. Type: apiv1.RootCA,
  1233. Template: mustParseCertificate(t, testRootCertificate),
  1234. Lifetime: 24 * time.Hour,
  1235. }}, nil, true},
  1236. {"fail location", fields{m, "", testProject, "", testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1237. Type: apiv1.RootCA,
  1238. Template: mustParseCertificate(t, testRootCertificate),
  1239. Lifetime: 24 * time.Hour,
  1240. }}, nil, true},
  1241. {"fail caPool", fields{m, "", testProject, testLocation, "", pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1242. Type: apiv1.RootCA,
  1243. Template: mustParseCertificate(t, testRootCertificate),
  1244. Lifetime: 24 * time.Hour,
  1245. }}, nil, true},
  1246. {"fail template", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1247. Type: apiv1.RootCA,
  1248. Lifetime: 24 * time.Hour,
  1249. }}, nil, true},
  1250. {"fail lifetime", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1251. Type: apiv1.RootCA,
  1252. Template: mustParseCertificate(t, testRootCertificate),
  1253. }}, nil, true},
  1254. {"fail parent", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1255. Type: apiv1.IntermediateCA,
  1256. Template: mustParseCertificate(t, testRootCertificate),
  1257. Lifetime: 24 * time.Hour,
  1258. }}, nil, true},
  1259. {"fail parent name", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1260. Type: apiv1.IntermediateCA,
  1261. Template: mustParseCertificate(t, testRootCertificate),
  1262. Lifetime: 24 * time.Hour,
  1263. Parent: &apiv1.CreateCertificateAuthorityResponse{},
  1264. }}, nil, true},
  1265. {"fail type", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1266. Type: 0,
  1267. Template: mustParseCertificate(t, testRootCertificate),
  1268. Lifetime: 24 * time.Hour,
  1269. }}, nil, true},
  1270. {"fail create key", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1271. Type: apiv1.RootCA,
  1272. Template: mustParseCertificate(t, testRootCertificate),
  1273. Lifetime: 24 * time.Hour,
  1274. CreateKey: &kmsapi.CreateKeyRequest{
  1275. SignatureAlgorithm: kmsapi.PureEd25519,
  1276. },
  1277. }}, nil, true},
  1278. {"fail GetCaPool", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1279. Type: apiv1.RootCA,
  1280. Template: mustParseCertificate(t, testRootCertificate),
  1281. Lifetime: 24 * time.Hour,
  1282. }}, nil, true},
  1283. {"fail CreateCaPool", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1284. Type: apiv1.RootCA,
  1285. Template: mustParseCertificate(t, testRootCertificate),
  1286. Lifetime: 24 * time.Hour,
  1287. }}, nil, true},
  1288. {"fail CreateCaPool.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1289. Type: apiv1.RootCA,
  1290. Template: mustParseCertificate(t, testRootCertificate),
  1291. Lifetime: 24 * time.Hour,
  1292. }}, nil, true},
  1293. {"fail CreateCertificateAuthority", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1294. Type: apiv1.RootCA,
  1295. Template: mustParseCertificate(t, testRootCertificate),
  1296. Lifetime: 24 * time.Hour,
  1297. }}, nil, true},
  1298. {"fail CreateCertificateAuthority.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1299. Type: apiv1.RootCA,
  1300. Template: mustParseCertificate(t, testRootCertificate),
  1301. Lifetime: 24 * time.Hour,
  1302. }}, nil, true},
  1303. {"fail EnableCertificateAuthority", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1304. Type: apiv1.RootCA,
  1305. Template: mustParseCertificate(t, testRootCertificate),
  1306. Lifetime: 24 * time.Hour,
  1307. }}, nil, true},
  1308. {"fail EnableCertificateAuthority.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1309. Type: apiv1.RootCA,
  1310. Template: mustParseCertificate(t, testRootCertificate),
  1311. Lifetime: 24 * time.Hour,
  1312. }}, nil, true},
  1313. {"fail EnableCertificateAuthority intermediate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1314. Type: apiv1.IntermediateCA,
  1315. Template: mustParseCertificate(t, testIntermediateCertificate),
  1316. Lifetime: 24 * time.Hour,
  1317. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1318. Name: testAuthorityName,
  1319. Certificate: rootCrt,
  1320. },
  1321. }}, nil, true},
  1322. {"fail EnableCertificateAuthority.Wait intermediate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1323. Type: apiv1.IntermediateCA,
  1324. Template: mustParseCertificate(t, testIntermediateCertificate),
  1325. Lifetime: 24 * time.Hour,
  1326. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1327. Name: testAuthorityName,
  1328. Certificate: rootCrt,
  1329. },
  1330. }}, nil, true},
  1331. {"fail FetchCertificateAuthorityCsr", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1332. Type: apiv1.IntermediateCA,
  1333. Template: mustParseCertificate(t, testIntermediateCertificate),
  1334. Lifetime: 24 * time.Hour,
  1335. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1336. Name: testAuthorityName,
  1337. Certificate: rootCrt,
  1338. },
  1339. }}, nil, true},
  1340. {"fail CreateCertificate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1341. Type: apiv1.IntermediateCA,
  1342. Template: mustParseCertificate(t, testIntermediateCertificate),
  1343. Lifetime: 24 * time.Hour,
  1344. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1345. Name: testAuthorityName,
  1346. Certificate: rootCrt,
  1347. },
  1348. }}, nil, true},
  1349. {"fail ActivateCertificateAuthority", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1350. Type: apiv1.IntermediateCA,
  1351. Template: mustParseCertificate(t, testIntermediateCertificate),
  1352. Lifetime: 24 * time.Hour,
  1353. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1354. Name: testAuthorityName,
  1355. Certificate: rootCrt,
  1356. },
  1357. }}, nil, true},
  1358. {"fail ActivateCertificateAuthority.Wait", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1359. Type: apiv1.IntermediateCA,
  1360. Template: mustParseCertificate(t, testIntermediateCertificate),
  1361. Lifetime: 24 * time.Hour,
  1362. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1363. Name: testAuthorityName,
  1364. Certificate: rootCrt,
  1365. },
  1366. }}, nil, true},
  1367. {"fail x509util.CreateCertificate", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1368. Type: apiv1.IntermediateCA,
  1369. Template: mustParseCertificate(t, testIntermediateCertificate),
  1370. Lifetime: 24 * time.Hour,
  1371. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1372. Certificate: rootCrt,
  1373. Signer: createBadSigner(t),
  1374. },
  1375. }}, nil, true},
  1376. {"fail parseCertificateRequest", fields{m, "", testProject, testLocation, testCaPool, pb.CaPool_DEVOPS}, args{&apiv1.CreateCertificateAuthorityRequest{
  1377. Type: apiv1.IntermediateCA,
  1378. Template: mustParseCertificate(t, testIntermediateCertificate),
  1379. Lifetime: 24 * time.Hour,
  1380. Parent: &apiv1.CreateCertificateAuthorityResponse{
  1381. Certificate: rootCrt,
  1382. Signer: createBadSigner(t),
  1383. },
  1384. }}, nil, true},
  1385. }
  1386. for _, tt := range tests {
  1387. t.Run(tt.name, func(t *testing.T) {
  1388. c := &CloudCAS{
  1389. client: tt.fields.client,
  1390. certificateAuthority: tt.fields.certificateAuthority,
  1391. project: tt.fields.project,
  1392. location: tt.fields.location,
  1393. caPool: tt.fields.caPool,
  1394. caPoolTier: tt.fields.caPoolTier,
  1395. }
  1396. got, err := c.CreateCertificateAuthority(tt.args.req)
  1397. if (err != nil) != tt.wantErr {
  1398. t.Errorf("CloudCAS.CreateCertificateAuthority() error = %+v, wantErr %v", err, tt.wantErr)
  1399. return
  1400. }
  1401. if !reflect.DeepEqual(got, tt.want) {
  1402. t.Errorf("CloudCAS.CreateCertificateAuthority() = %v, want %v", got, tt.want)
  1403. }
  1404. })
  1405. }
  1406. }
  1407. func Test_normalizeCertificateAuthorityName(t *testing.T) {
  1408. type args struct {
  1409. name string
  1410. }
  1411. tests := []struct {
  1412. name string
  1413. args args
  1414. want string
  1415. }{
  1416. {"ok", args{"Test-CA-Name_1234"}, "Test-CA-Name_1234"},
  1417. {"change", args{"💥 CA"}, "--CA"},
  1418. }
  1419. for _, tt := range tests {
  1420. t.Run(tt.name, func(t *testing.T) {
  1421. if got := normalizeCertificateAuthorityName(tt.args.name); got != tt.want {
  1422. t.Errorf("normalizeCertificateAuthorityName() = %v, want %v", got, tt.want)
  1423. }
  1424. })
  1425. }
  1426. }