cloudcas_test.go 58 KB

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