cloudkms_test.go 13 KB

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