cloudcas_test.go 59 KB

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