cloudcas_test.go 58 KB

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