cloudcas_test.go 61 KB

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