cloudkms_test.go 13 KB

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