cloudkms_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package cloudkms
  2. import (
  3. "context"
  4. "crypto"
  5. "fmt"
  6. "io/ioutil"
  7. "reflect"
  8. "testing"
  9. gax "github.com/googleapis/gax-go/v2"
  10. "github.com/smallstep/certificates/kms/apiv1"
  11. "go.step.sm/crypto/pemutil"
  12. "google.golang.org/api/option"
  13. kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
  14. "google.golang.org/grpc/codes"
  15. "google.golang.org/grpc/status"
  16. )
  17. func TestParent(t *testing.T) {
  18. type args struct {
  19. name string
  20. }
  21. tests := []struct {
  22. name string
  23. args args
  24. want string
  25. want1 string
  26. }{
  27. {"zero", args{"child"}, "", "child"},
  28. {"one", args{"parent/child"}, "", "child"},
  29. {"two", args{"grandparent/parent/child"}, "grandparent", "child"},
  30. {"three", args{"great-grandparent/grandparent/parent/child"}, "great-grandparent/grandparent", "child"},
  31. {"empty", args{""}, "", ""},
  32. {"root", args{"/"}, "", ""},
  33. {"child", args{"/child"}, "", "child"},
  34. {"parent", args{"parent/"}, "", ""},
  35. }
  36. for _, tt := range tests {
  37. t.Run(tt.name, func(t *testing.T) {
  38. got, got1 := Parent(tt.args.name)
  39. if got != tt.want {
  40. t.Errorf("Parent() got = %v, want %v", got, tt.want)
  41. }
  42. if got1 != tt.want1 {
  43. t.Errorf("Parent() got1 = %v, want %v", got1, tt.want1)
  44. }
  45. })
  46. }
  47. }
  48. func TestNew(t *testing.T) {
  49. tmp := newKeyManagementClient
  50. t.Cleanup(func() {
  51. newKeyManagementClient = tmp
  52. })
  53. newKeyManagementClient = func(ctx context.Context, opts ...option.ClientOption) (KeyManagementClient, error) {
  54. if len(opts) > 0 {
  55. return nil, fmt.Errorf("test error")
  56. }
  57. return &MockClient{}, nil
  58. }
  59. type args struct {
  60. ctx context.Context
  61. opts apiv1.Options
  62. }
  63. tests := []struct {
  64. name string
  65. args args
  66. want *CloudKMS
  67. wantErr bool
  68. }{
  69. {"ok", args{context.Background(), apiv1.Options{}}, &CloudKMS{client: &MockClient{}}, false},
  70. {"ok with uri", args{context.Background(), apiv1.Options{URI: "cloudkms:"}}, &CloudKMS{client: &MockClient{}}, false},
  71. {"fail credentials", args{context.Background(), apiv1.Options{CredentialsFile: "testdata/missing"}}, nil, true},
  72. {"fail with uri", args{context.Background(), apiv1.Options{URI: "cloudkms:credentials-file=testdata/missing"}}, nil, true},
  73. {"fail schema", args{context.Background(), apiv1.Options{URI: "pkcs11:"}}, nil, true},
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. got, err := New(tt.args.ctx, tt.args.opts)
  78. if (err != nil) != tt.wantErr {
  79. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  80. return
  81. }
  82. if !reflect.DeepEqual(got, tt.want) {
  83. t.Errorf("New() = %v, want %v", got, tt.want)
  84. }
  85. })
  86. }
  87. }
  88. func TestNew_real(t *testing.T) {
  89. type args struct {
  90. ctx context.Context
  91. opts apiv1.Options
  92. }
  93. tests := []struct {
  94. name string
  95. args args
  96. want *CloudKMS
  97. wantErr bool
  98. }{
  99. {"fail credentials", args{context.Background(), apiv1.Options{CredentialsFile: "testdata/missing"}}, nil, true},
  100. {"fail with uri", args{context.Background(), apiv1.Options{URI: "cloudkms:credentials-file=testdata/missing"}}, nil, true},
  101. }
  102. for _, tt := range tests {
  103. t.Run(tt.name, func(t *testing.T) {
  104. got, err := New(tt.args.ctx, tt.args.opts)
  105. if (err != nil) != tt.wantErr {
  106. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  107. return
  108. }
  109. if !reflect.DeepEqual(got, tt.want) {
  110. t.Errorf("New() = %v, want %v", got, tt.want)
  111. }
  112. })
  113. }
  114. }
  115. func TestNewCloudKMS(t *testing.T) {
  116. type args struct {
  117. client KeyManagementClient
  118. }
  119. tests := []struct {
  120. name string
  121. args args
  122. want *CloudKMS
  123. }{
  124. {"ok", args{&MockClient{}}, &CloudKMS{&MockClient{}}},
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. if got := NewCloudKMS(tt.args.client); !reflect.DeepEqual(got, tt.want) {
  129. t.Errorf("NewCloudKMS() = %v, want %v", got, tt.want)
  130. }
  131. })
  132. }
  133. }
  134. func TestCloudKMS_Close(t *testing.T) {
  135. type fields struct {
  136. client KeyManagementClient
  137. }
  138. tests := []struct {
  139. name string
  140. fields fields
  141. wantErr bool
  142. }{
  143. {"ok", fields{&MockClient{close: func() error { return nil }}}, false},
  144. {"fail", fields{&MockClient{close: func() error { return fmt.Errorf("an error") }}}, true},
  145. }
  146. for _, tt := range tests {
  147. t.Run(tt.name, func(t *testing.T) {
  148. k := &CloudKMS{
  149. client: tt.fields.client,
  150. }
  151. if err := k.Close(); (err != nil) != tt.wantErr {
  152. t.Errorf("CloudKMS.Close() error = %v, wantErr %v", err, tt.wantErr)
  153. }
  154. })
  155. }
  156. }
  157. func TestCloudKMS_CreateSigner(t *testing.T) {
  158. keyName := "projects/p/locations/l/keyRings/k/cryptoKeys/c/cryptoKeyVersions/1"
  159. type fields struct {
  160. client KeyManagementClient
  161. }
  162. type args struct {
  163. req *apiv1.CreateSignerRequest
  164. }
  165. tests := []struct {
  166. name string
  167. fields fields
  168. args args
  169. want crypto.Signer
  170. wantErr bool
  171. }{
  172. {"ok", fields{&MockClient{}}, args{&apiv1.CreateSignerRequest{SigningKey: keyName}}, &Signer{client: &MockClient{}, signingKey: keyName}, false},
  173. {"fail", fields{&MockClient{}}, args{&apiv1.CreateSignerRequest{SigningKey: ""}}, nil, true},
  174. }
  175. for _, tt := range tests {
  176. t.Run(tt.name, func(t *testing.T) {
  177. k := &CloudKMS{
  178. client: tt.fields.client,
  179. }
  180. got, err := k.CreateSigner(tt.args.req)
  181. if (err != nil) != tt.wantErr {
  182. t.Errorf("CloudKMS.CreateSigner() error = %v, wantErr %v", err, tt.wantErr)
  183. return
  184. }
  185. if !reflect.DeepEqual(got, tt.want) {
  186. t.Errorf("CloudKMS.CreateSigner() = %v, want %v", got, tt.want)
  187. }
  188. })
  189. }
  190. }
  191. func TestCloudKMS_CreateKey(t *testing.T) {
  192. keyName := "projects/p/locations/l/keyRings/k/cryptoKeys/c"
  193. testError := fmt.Errorf("an error")
  194. alreadyExists := status.Error(codes.AlreadyExists, "already exists")
  195. pemBytes, err := ioutil.ReadFile("testdata/pub.pem")
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. pk, err := pemutil.ParseKey(pemBytes)
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. var retries int
  204. type fields struct {
  205. client KeyManagementClient
  206. }
  207. type args struct {
  208. req *apiv1.CreateKeyRequest
  209. }
  210. tests := []struct {
  211. name string
  212. fields fields
  213. args args
  214. want *apiv1.CreateKeyResponse
  215. wantErr bool
  216. }{
  217. {"ok", fields{
  218. &MockClient{
  219. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  220. return &kmspb.KeyRing{}, nil
  221. },
  222. createCryptoKey: func(_ context.Context, _ *kmspb.CreateCryptoKeyRequest, _ ...gax.CallOption) (*kmspb.CryptoKey, error) {
  223. return &kmspb.CryptoKey{Name: keyName}, nil
  224. },
  225. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  226. return &kmspb.PublicKey{Pem: string(pemBytes)}, nil
  227. },
  228. }},
  229. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.HSM, SignatureAlgorithm: apiv1.ECDSAWithSHA256}},
  230. &apiv1.CreateKeyResponse{Name: keyName + "/cryptoKeyVersions/1", PublicKey: pk, CreateSignerRequest: apiv1.CreateSignerRequest{SigningKey: keyName + "/cryptoKeyVersions/1"}}, false},
  231. {"ok new key ring", fields{
  232. &MockClient{
  233. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  234. return nil, testError
  235. },
  236. createKeyRing: func(_ context.Context, _ *kmspb.CreateKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  237. return nil, alreadyExists
  238. },
  239. createCryptoKey: func(_ context.Context, _ *kmspb.CreateCryptoKeyRequest, _ ...gax.CallOption) (*kmspb.CryptoKey, error) {
  240. return &kmspb.CryptoKey{Name: keyName}, nil
  241. },
  242. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  243. return &kmspb.PublicKey{Pem: string(pemBytes)}, nil
  244. },
  245. }},
  246. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.Software, SignatureAlgorithm: apiv1.SHA256WithRSA, Bits: 3072}},
  247. &apiv1.CreateKeyResponse{Name: keyName + "/cryptoKeyVersions/1", PublicKey: pk, CreateSignerRequest: apiv1.CreateSignerRequest{SigningKey: keyName + "/cryptoKeyVersions/1"}}, false},
  248. {"ok new key version", fields{
  249. &MockClient{
  250. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  251. return &kmspb.KeyRing{}, nil
  252. },
  253. createCryptoKey: func(_ context.Context, _ *kmspb.CreateCryptoKeyRequest, _ ...gax.CallOption) (*kmspb.CryptoKey, error) {
  254. return nil, alreadyExists
  255. },
  256. createCryptoKeyVersion: func(_ context.Context, _ *kmspb.CreateCryptoKeyVersionRequest, _ ...gax.CallOption) (*kmspb.CryptoKeyVersion, error) {
  257. return &kmspb.CryptoKeyVersion{Name: keyName + "/cryptoKeyVersions/2"}, nil
  258. },
  259. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  260. return &kmspb.PublicKey{Pem: string(pemBytes)}, nil
  261. },
  262. }},
  263. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.HSM, SignatureAlgorithm: apiv1.ECDSAWithSHA256}},
  264. &apiv1.CreateKeyResponse{Name: keyName + "/cryptoKeyVersions/2", PublicKey: pk, CreateSignerRequest: apiv1.CreateSignerRequest{SigningKey: keyName + "/cryptoKeyVersions/2"}}, false},
  265. {"ok with retries", fields{
  266. &MockClient{
  267. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  268. return &kmspb.KeyRing{}, nil
  269. },
  270. createCryptoKey: func(_ context.Context, _ *kmspb.CreateCryptoKeyRequest, _ ...gax.CallOption) (*kmspb.CryptoKey, error) {
  271. return &kmspb.CryptoKey{Name: keyName}, nil
  272. },
  273. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  274. if retries != 2 {
  275. retries++
  276. return nil, status.Error(codes.FailedPrecondition, "key is not enabled, current state is: PENDING_GENERATION")
  277. }
  278. return &kmspb.PublicKey{Pem: string(pemBytes)}, nil
  279. },
  280. }},
  281. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.HSM, SignatureAlgorithm: apiv1.ECDSAWithSHA256}},
  282. &apiv1.CreateKeyResponse{Name: keyName + "/cryptoKeyVersions/1", PublicKey: pk, CreateSignerRequest: apiv1.CreateSignerRequest{SigningKey: keyName + "/cryptoKeyVersions/1"}}, false},
  283. {"fail name", fields{&MockClient{}}, args{&apiv1.CreateKeyRequest{}}, nil, true},
  284. {"fail protection level", fields{&MockClient{}}, args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.ProtectionLevel(100)}}, nil, true},
  285. {"fail signature algorithm", fields{&MockClient{}}, args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.Software, SignatureAlgorithm: apiv1.SignatureAlgorithm(100)}}, nil, true},
  286. {"fail number of bits", fields{&MockClient{}}, args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.Software, SignatureAlgorithm: apiv1.SHA256WithRSA, Bits: 1024}},
  287. nil, true},
  288. {"fail create key ring", fields{
  289. &MockClient{
  290. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  291. return nil, testError
  292. },
  293. createKeyRing: func(_ context.Context, _ *kmspb.CreateKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  294. return nil, testError
  295. },
  296. }},
  297. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.HSM, SignatureAlgorithm: apiv1.ECDSAWithSHA256}},
  298. nil, true},
  299. {"fail create key", fields{
  300. &MockClient{
  301. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  302. return &kmspb.KeyRing{}, nil
  303. },
  304. createCryptoKey: func(_ context.Context, _ *kmspb.CreateCryptoKeyRequest, _ ...gax.CallOption) (*kmspb.CryptoKey, error) {
  305. return nil, testError
  306. },
  307. }},
  308. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.HSM, SignatureAlgorithm: apiv1.ECDSAWithSHA256}},
  309. nil, true},
  310. {"fail create key version", fields{
  311. &MockClient{
  312. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  313. return &kmspb.KeyRing{}, nil
  314. },
  315. createCryptoKey: func(_ context.Context, _ *kmspb.CreateCryptoKeyRequest, _ ...gax.CallOption) (*kmspb.CryptoKey, error) {
  316. return nil, alreadyExists
  317. },
  318. createCryptoKeyVersion: func(_ context.Context, _ *kmspb.CreateCryptoKeyVersionRequest, _ ...gax.CallOption) (*kmspb.CryptoKeyVersion, error) {
  319. return nil, testError
  320. },
  321. }},
  322. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.HSM, SignatureAlgorithm: apiv1.ECDSAWithSHA256}},
  323. nil, true},
  324. {"fail get public key", fields{
  325. &MockClient{
  326. getKeyRing: func(_ context.Context, _ *kmspb.GetKeyRingRequest, _ ...gax.CallOption) (*kmspb.KeyRing, error) {
  327. return &kmspb.KeyRing{}, nil
  328. },
  329. createCryptoKey: func(_ context.Context, _ *kmspb.CreateCryptoKeyRequest, _ ...gax.CallOption) (*kmspb.CryptoKey, error) {
  330. return &kmspb.CryptoKey{Name: keyName}, nil
  331. },
  332. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  333. return nil, testError
  334. },
  335. }},
  336. args{&apiv1.CreateKeyRequest{Name: keyName, ProtectionLevel: apiv1.HSM, SignatureAlgorithm: apiv1.ECDSAWithSHA256}},
  337. nil, true},
  338. }
  339. for _, tt := range tests {
  340. t.Run(tt.name, func(t *testing.T) {
  341. k := &CloudKMS{
  342. client: tt.fields.client,
  343. }
  344. got, err := k.CreateKey(tt.args.req)
  345. if (err != nil) != tt.wantErr {
  346. t.Errorf("CloudKMS.CreateKey() error = %v, wantErr %v", err, tt.wantErr)
  347. return
  348. }
  349. if !reflect.DeepEqual(got, tt.want) {
  350. t.Errorf("CloudKMS.CreateKey() = %v, want %v", got, tt.want)
  351. }
  352. })
  353. }
  354. }
  355. func TestCloudKMS_GetPublicKey(t *testing.T) {
  356. keyName := "projects/p/locations/l/keyRings/k/cryptoKeys/c/cryptoKeyVersions/1"
  357. testError := fmt.Errorf("an error")
  358. pemBytes, err := ioutil.ReadFile("testdata/pub.pem")
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. pk, err := pemutil.ParseKey(pemBytes)
  363. if err != nil {
  364. t.Fatal(err)
  365. }
  366. var retries int
  367. type fields struct {
  368. client KeyManagementClient
  369. }
  370. type args struct {
  371. req *apiv1.GetPublicKeyRequest
  372. }
  373. tests := []struct {
  374. name string
  375. fields fields
  376. args args
  377. want crypto.PublicKey
  378. wantErr bool
  379. }{
  380. {"ok", fields{
  381. &MockClient{
  382. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  383. return &kmspb.PublicKey{Pem: string(pemBytes)}, nil
  384. },
  385. }},
  386. args{&apiv1.GetPublicKeyRequest{Name: keyName}}, pk, false},
  387. {"ok with retries", fields{
  388. &MockClient{
  389. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  390. if retries != 2 {
  391. retries++
  392. return nil, status.Error(codes.FailedPrecondition, "key is not enabled, current state is: PENDING_GENERATION")
  393. }
  394. return &kmspb.PublicKey{Pem: string(pemBytes)}, nil
  395. },
  396. }},
  397. args{&apiv1.GetPublicKeyRequest{Name: keyName}}, pk, false},
  398. {"fail name", fields{&MockClient{}}, args{&apiv1.GetPublicKeyRequest{}}, nil, true},
  399. {"fail get public key", fields{
  400. &MockClient{
  401. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  402. return nil, testError
  403. },
  404. }},
  405. args{&apiv1.GetPublicKeyRequest{Name: keyName}}, nil, true},
  406. {"fail parse pem", fields{
  407. &MockClient{
  408. getPublicKey: func(_ context.Context, _ *kmspb.GetPublicKeyRequest, _ ...gax.CallOption) (*kmspb.PublicKey, error) {
  409. return &kmspb.PublicKey{Pem: string("bad pem")}, nil
  410. },
  411. }},
  412. args{&apiv1.GetPublicKeyRequest{Name: keyName}}, nil, true},
  413. }
  414. for _, tt := range tests {
  415. t.Run(tt.name, func(t *testing.T) {
  416. k := &CloudKMS{
  417. client: tt.fields.client,
  418. }
  419. got, err := k.GetPublicKey(tt.args.req)
  420. if (err != nil) != tt.wantErr {
  421. t.Errorf("CloudKMS.GetPublicKey() error = %v, wantErr %v", err, tt.wantErr)
  422. return
  423. }
  424. if !reflect.DeepEqual(got, tt.want) {
  425. t.Errorf("CloudKMS.GetPublicKey() = %v, want %v", got, tt.want)
  426. }
  427. })
  428. }
  429. }