cloudcas_test.go 59 KB

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