forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippetsIT.java
More file actions
815 lines (704 loc) · 30.4 KB
/
SnippetsIT.java
File metadata and controls
815 lines (704 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kms;
import static com.google.common.truth.Truth.assertThat;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.kms.v1.CreateCryptoKeyRequest;
import com.google.cloud.kms.v1.CryptoKey;
import com.google.cloud.kms.v1.CryptoKey.CryptoKeyPurpose;
import com.google.cloud.kms.v1.CryptoKeyName;
import com.google.cloud.kms.v1.CryptoKeyVersion;
import com.google.cloud.kms.v1.CryptoKeyVersion.CryptoKeyVersionAlgorithm;
import com.google.cloud.kms.v1.CryptoKeyVersion.CryptoKeyVersionState;
import com.google.cloud.kms.v1.CryptoKeyVersionName;
import com.google.cloud.kms.v1.CryptoKeyVersionTemplate;
import com.google.cloud.kms.v1.Digest;
import com.google.cloud.kms.v1.EncryptResponse;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import com.google.cloud.kms.v1.KeyRing;
import com.google.cloud.kms.v1.KeyRingName;
import com.google.cloud.kms.v1.ListCryptoKeyVersionsRequest;
import com.google.cloud.kms.v1.LocationName;
import com.google.cloud.kms.v1.MacSignResponse;
import com.google.cloud.kms.v1.ProtectionLevel;
import com.google.cloud.kms.v1.PublicKey;
import com.google.common.base.Strings;
import com.google.protobuf.ByteString;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.FieldMaskUtil;
import com.nimbusds.jose.JOSEException;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class SnippetsIT {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String LOCATION_ID = "us-east1";
private static String KEY_RING_ID;
private static String ASYMMETRIC_DECRYPT_KEY_ID;
private static String ASYMMETRIC_SIGN_EC_KEY_ID;
private static String ASYMMETRIC_SIGN_RSA_KEY_ID;
private static String HSM_KEY_ID;
private static String MAC_KEY_ID;
private static String SYMMETRIC_KEY_ID;
@BeforeClass
public static void beforeAll() throws IOException {
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
KEY_RING_ID = "kms-test-keyring-java";
try {
createKeyRing(KEY_RING_ID);
} catch (Exception e) {
// Ignore.
}
ASYMMETRIC_DECRYPT_KEY_ID = "kms-test-asymmetric-decrypt-v3";
createKeyOrIgnore(ASYMMETRIC_DECRYPT_KEY_ID, SnippetsIT::createAsymmetricDecryptKey);
ASYMMETRIC_SIGN_EC_KEY_ID = "kms-test-asymmetric-sign-ec-v3";
createKeyOrIgnore(ASYMMETRIC_SIGN_EC_KEY_ID, SnippetsIT::createAsymmetricSignEcKey);
ASYMMETRIC_SIGN_RSA_KEY_ID = "kms-test-asymmetric-sign-rsa-v3";
createKeyOrIgnore(ASYMMETRIC_SIGN_RSA_KEY_ID, SnippetsIT::createAsymmetricSignRsaKey);
HSM_KEY_ID = "kms-test-hsm-v3";
createKeyOrIgnore(HSM_KEY_ID, SnippetsIT::createHsmKey);
MAC_KEY_ID = "kms-test-mac-v3";
createKeyOrIgnore(MAC_KEY_ID, SnippetsIT::createMacKey);
SYMMETRIC_KEY_ID = "kms-test-symmetric-v3";
createKeyOrIgnore(SYMMETRIC_KEY_ID, SnippetsIT::createSymmetricKey);
}
private ByteArrayOutputStream stdOut;
private ByteArrayOutputStream stdErr;
@Before
public void beforeEach() {
stdOut = new ByteArrayOutputStream();
stdErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
System.setErr(new PrintStream(stdErr));
}
@After
public void afterEach() {
stdOut = null;
stdErr = null;
System.setOut(null);
System.setErr(null);
}
@AfterClass
public static void afterAll() throws IOException {
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
// Iterate over each key ring's key's crypto key versions and destroy.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
for (CryptoKey key : client.listCryptoKeys(getKeyRingName()).iterateAll()) {
if (key.hasRotationPeriod() || key.hasNextRotationTime()) {
CryptoKey keyWithoutRotation = CryptoKey.newBuilder().setName(key.getName()).build();
FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");
client.updateCryptoKey(keyWithoutRotation, fieldMask);
}
/*
ListCryptoKeyVersionsRequest listVersionsRequest =
ListCryptoKeyVersionsRequest.newBuilder()
.setParent(key.getName())
.setFilter("state != DESTROYED AND state != DESTROY_SCHEDULED")
.build();
for (CryptoKeyVersion version :
client.listCryptoKeyVersions(listVersionsRequest).iterateAll()) {
client.destroyCryptoKeyVersion(version.getName());
}
*/
}
}
}
private static LocationName getLocationName() {
return LocationName.of(PROJECT_ID, LOCATION_ID);
}
private static KeyRingName getKeyRingName() {
return KeyRingName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID);
}
private static String getRandomId() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
private static KeyRing createKeyRing(String keyRingId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
KeyRingName keyRingName = KeyRingName.of(PROJECT_ID, LOCATION_ID, keyRingId);
try {
return client.getKeyRing(keyRingName);
} catch (com.google.api.gax.rpc.NotFoundException e) {
// KeyRing doesn't exist, create it.
KeyRing keyRing = KeyRing.newBuilder().build();
return client.createKeyRing(getLocationName(), keyRingId, keyRing);
}
}
}
interface KeyCreator {
void create(String id) throws IOException;
}
private static void createKeyOrIgnore(String id, KeyCreator creator) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, id);
try {
client.getCryptoKey(keyName);
return; // Exists
} catch (com.google.api.gax.rpc.NotFoundException e) {
// Doesn't exist, try creating.
}
}
try {
creator.create(id);
} catch (com.google.api.gax.rpc.AlreadyExistsException e) {
// Ignore
}
}
private static CryptoKey createAsymmetricDecryptKey(String keyId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKey key =
CryptoKey.newBuilder()
.setPurpose(CryptoKeyPurpose.ASYMMETRIC_DECRYPT)
.setVersionTemplate(
CryptoKeyVersionTemplate.newBuilder()
.setAlgorithm(CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256)
.build())
.putLabels("foo", "bar")
.putLabels("zip", "zap")
.build();
CryptoKey createdKey = client.createCryptoKey(getKeyRingName(), keyId, key);
return createdKey;
}
}
private static CryptoKey createAsymmetricSignEcKey(String keyId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKey key =
CryptoKey.newBuilder()
.setPurpose(CryptoKeyPurpose.ASYMMETRIC_SIGN)
.setVersionTemplate(
CryptoKeyVersionTemplate.newBuilder()
.setAlgorithm(CryptoKeyVersionAlgorithm.EC_SIGN_P256_SHA256)
.build())
.putLabels("foo", "bar")
.putLabels("zip", "zap")
.build();
CryptoKey createdKey = client.createCryptoKey(getKeyRingName(), keyId, key);
return createdKey;
}
}
private static CryptoKey createAsymmetricSignRsaKey(String keyId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKey key =
CryptoKey.newBuilder()
.setPurpose(CryptoKeyPurpose.ASYMMETRIC_SIGN)
.setVersionTemplate(
CryptoKeyVersionTemplate.newBuilder()
.setAlgorithm(CryptoKeyVersionAlgorithm.RSA_SIGN_PSS_2048_SHA256)
.build())
.putLabels("foo", "bar")
.putLabels("zip", "zap")
.build();
CryptoKey createdKey = client.createCryptoKey(getKeyRingName(), keyId, key);
return createdKey;
}
}
private static CryptoKey createHsmKey(String keyId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKey key =
CryptoKey.newBuilder()
.setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT)
.setVersionTemplate(
CryptoKeyVersionTemplate.newBuilder()
.setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)
.setProtectionLevel(ProtectionLevel.HSM)
.build())
.putLabels("foo", "bar")
.putLabels("zip", "zap")
.build();
CryptoKey createdKey = client.createCryptoKey(getKeyRingName(), keyId, key);
return createdKey;
}
}
private static CryptoKey createMacKey(String keyId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKey key =
CryptoKey.newBuilder()
.setPurpose(CryptoKeyPurpose.MAC)
.setVersionTemplate(
CryptoKeyVersionTemplate.newBuilder()
.setAlgorithm(CryptoKeyVersionAlgorithm.HMAC_SHA256)
.setProtectionLevel(ProtectionLevel.HSM)
.build())
.putLabels("foo", "bar")
.putLabels("zip", "zap")
.build();
CryptoKey createdKey = client.createCryptoKey(getKeyRingName(), keyId, key);
return createdKey;
}
}
private static CryptoKey createSymmetricKey(String keyId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKey key =
CryptoKey.newBuilder()
.setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT)
.setVersionTemplate(
CryptoKeyVersionTemplate.newBuilder()
.setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)
.build())
.putLabels("foo", "bar")
.putLabels("zip", "zap")
.build();
CryptoKey createdKey = client.createCryptoKey(getKeyRingName(), keyId, key);
return createdKey;
}
}
private static CryptoKey createSymmetricKeyWithNoInitialVersion(String keyId) throws IOException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKey key =
CryptoKey.newBuilder()
.setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT)
.setVersionTemplate(
CryptoKeyVersionTemplate.newBuilder()
.setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)
.build())
.putLabels("foo", "bar")
.putLabels("zip", "zap")
.build();
CreateCryptoKeyRequest request =
CreateCryptoKeyRequest.newBuilder()
.setParent(getKeyRingName().toString())
.setCryptoKeyId(keyId)
.setCryptoKey(key)
.setSkipInitialVersionCreation(true)
.build();
return client.createCryptoKey(request);
}
}
private static CryptoKeyVersion createKeyVersion(String keyId)
throws IOException, InterruptedException, TimeoutException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, keyId);
CryptoKeyVersion keyVersion = CryptoKeyVersion.newBuilder().build();
CryptoKeyVersion createdVersion = client.createCryptoKeyVersion(keyName, keyVersion);
for (int i = 1; i <= 5; i++) {
CryptoKeyVersion gotVersion = client.getCryptoKeyVersion(createdVersion.getName());
if (gotVersion.getState() == CryptoKeyVersionState.ENABLED) {
return gotVersion;
}
Thread.sleep(500 * i);
}
throw new TimeoutException("key version not ready in timeout");
}
}
private static byte[] convertPemToDer(String pem) {
BufferedReader bufferedReader = new BufferedReader(new StringReader(pem));
String encoded =
bufferedReader
.lines()
.filter(line -> !line.startsWith("-----BEGIN") && !line.startsWith("-----END"))
.collect(Collectors.joining());
return Base64.getDecoder().decode(encoded);
}
@Test
public void testQuickstart() throws IOException {
new Quickstart().quickstart(PROJECT_ID, LOCATION_ID);
assertThat(stdOut.toString()).contains("key rings");
}
@Test
public void testCreateKeyAsymmetricDecrypt() throws IOException {
new CreateKeyAsymmetricDecrypt()
.createKeyAsymmetricDecrypt(PROJECT_ID, LOCATION_ID, KEY_RING_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created asymmetric key");
}
@Test
public void testCreateKeyAsymmetricSign() throws IOException {
new CreateKeyAsymmetricSign()
.createKeyAsymmetricSign(PROJECT_ID, LOCATION_ID, KEY_RING_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created asymmetric key");
}
@Test
public void testCreateKeyHsm() throws IOException {
new CreateKeyHsm().createKeyHsm(PROJECT_ID, LOCATION_ID, KEY_RING_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created hsm key");
}
@Test
public void testCreateKeyLabels() throws IOException {
new CreateKeyLabels().createKeyLabels(PROJECT_ID, LOCATION_ID, KEY_RING_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created key with labels");
}
@Test
public void testCreateKeyMac() throws IOException {
new CreateKeyMac().createKeyMac(PROJECT_ID, LOCATION_ID, KEY_RING_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created mac key");
}
@Test
public void testCreateKeyRing() throws IOException {
new CreateKeyRing().createKeyRing(PROJECT_ID, LOCATION_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created key ring");
}
@Test
public void testCreateKeyRotationSchedule() throws IOException {
new CreateKeyRotationSchedule()
.createKeyRotationSchedule(PROJECT_ID, LOCATION_ID, KEY_RING_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created key with rotation");
}
@Test
public void testCreateKeySymmetricEncryptDecrypt() throws IOException {
new CreateKeySymmetricEncryptDecrypt()
.createKeySymmetricEncryptDecrypt(PROJECT_ID, LOCATION_ID, KEY_RING_ID, getRandomId());
assertThat(stdOut.toString()).contains("Created symmetric key");
}
@Test
public void testCreateKeyVersion() throws IOException {
new CreateKeyVersion().createKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
assertThat(stdOut.toString()).contains("Created key version");
}
@Test
public void testDecryptAsymmetric() throws IOException, GeneralSecurityException {
String plaintext = "my message";
byte[] ciphertext;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyVersionName keyVersionName =
CryptoKeyVersionName.of(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, "1");
PublicKey publicKey = client.getPublicKey(keyVersionName);
byte[] derKey = convertPemToDer(publicKey.getPem());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey);
java.security.PublicKey rsaKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
OAEPParameterSpec oaepParams =
new OAEPParameterSpec(
"SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.ENCRYPT_MODE, rsaKey, oaepParams);
ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
}
new DecryptAsymmetric()
.decryptAsymmetric(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, "1", ciphertext);
assertThat(stdOut.toString()).contains("my message");
}
@Test
public void testDecryptSymmetric() throws IOException {
String plaintext = "my plaintext";
byte[] ciphertext;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName =
CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
EncryptResponse result = client.encrypt(keyName, ByteString.copyFromUtf8(plaintext));
ciphertext = result.getCiphertext().toByteArray();
}
new DecryptSymmetric()
.decryptSymmetric(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, ciphertext);
assertThat(stdOut.toString()).contains(plaintext);
}
@Test
public void testDestroyRestoreKeyVersion()
throws IOException, InterruptedException, TimeoutException {
CryptoKeyVersion keyVersion = createKeyVersion(ASYMMETRIC_DECRYPT_KEY_ID);
String name = keyVersion.getName();
String keyVersionId = name.substring(name.lastIndexOf('/') + 1);
new DestroyKeyVersion()
.destroyKeyVersion(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Destroyed key version");
new RestoreKeyVersion()
.restoreKeyVersion(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Restored key version");
}
@Test
public void testDeleteKey() throws IOException {
String deleteKeyId = getRandomId();
createSymmetricKeyWithNoInitialVersion(deleteKeyId);
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, deleteKeyId);
// Delete the key.
new DeleteKey().deleteKey(PROJECT_ID, LOCATION_ID, KEY_RING_ID, deleteKeyId);
String output = stdOut.toString() + stdErr.toString();
assertThat(output).contains("Deleted key");
}
}
@Test
public void testGetRetiredResource()
throws IOException, InterruptedException, ExecutionException {
String deleteKeyId = getRandomId();
createSymmetricKeyWithNoInitialVersion(deleteKeyId);
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName =
CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, deleteKeyId);
client.deleteCryptoKeyAsync(keyName).get();
}
// List retired resources to find the one we just deleted.
String retiredResourceName = null;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
for (com.google.cloud.kms.v1.RetiredResource resource :
client.listRetiredResources(getLocationName()).iterateAll()) {
if (resource.toString().contains(deleteKeyId)) {
retiredResourceName = resource.getName();
break;
}
}
}
// If not found, asserting null will fail.
assertThat(retiredResourceName).isNotNull();
// The name of the retired resource is required for Get.
String retiredResourceId =
retiredResourceName.substring(retiredResourceName.lastIndexOf('/') + 1);
new GetRetiredResource().getRetiredResource(PROJECT_ID, LOCATION_ID, retiredResourceId);
assertThat(stdOut.toString()).contains("Retired resource");
}
@Test
public void testListRetiredResources()
throws IOException, InterruptedException, ExecutionException {
String deleteKeyId = getRandomId();
createSymmetricKeyWithNoInitialVersion(deleteKeyId);
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName =
CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, deleteKeyId);
client.deleteCryptoKeyAsync(keyName).get();
}
new ListRetiredResources().listRetiredResources(PROJECT_ID, LOCATION_ID);
// Since we ran DeleteKey above, there should be at least one retired resource.
assertThat(stdOut.toString()).contains("Retired resource");
}
@Test
public void testDisableEnableKeyVersion() throws Exception {
CryptoKeyVersion keyVersion = createKeyVersion(ASYMMETRIC_DECRYPT_KEY_ID);
String name = keyVersion.getName();
String keyVersionId = name.substring(name.lastIndexOf('/') + 1);
new DisableKeyVersion()
.disableKeyVersion(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Disabled key version");
new EnableKeyVersion()
.enableKeyVersion(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Enabled key version");
}
@Test
public void testEncryptAsymmetric() throws IOException, GeneralSecurityException {
new EncryptAsymmetric()
.encryptAsymmetric(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, "1", "my message");
assertThat(stdOut.toString()).contains("Ciphertext");
}
@Test
public void testEncryptSymmetric() throws IOException {
new EncryptSymmetric()
.encryptSymmetric(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, "my message");
assertThat(stdOut.toString()).contains("Ciphertext");
}
@Test
public void testGenerateRandomBytes() throws IOException {
new GenerateRandomBytes().generateRandomBytes(PROJECT_ID, LOCATION_ID, 256);
assertThat(stdOut.toString()).contains("Random bytes");
}
@Test
public void testGetKeyVersionAttestation() throws IOException {
new GetKeyVersionAttestation()
.getKeyVersionAttestation(PROJECT_ID, LOCATION_ID, KEY_RING_ID, HSM_KEY_ID, "1");
assertThat(stdOut.toString()).contains("CAVIUM");
}
@Test
public void testGetKeyLabels() throws IOException {
String keyId = getRandomId();
createSymmetricKey(keyId);
new GetKeyLabels().getKeyLabels(PROJECT_ID, LOCATION_ID, KEY_RING_ID, keyId);
assertThat(stdOut.toString()).contains("foo=bar");
}
@Test
public void testGetPublicKey() throws IOException, GeneralSecurityException {
new GetPublicKey()
.getPublicKey(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, "1");
assertThat(stdOut.toString()).contains("Public key");
}
@Test
public void testConvertPublicKeyToJwk()
throws IOException, GeneralSecurityException, JOSEException {
new ConvertPublicKeyToJwk()
.convertPublicKey(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, "1");
assertThat(stdOut.toString()).contains("kty");
}
@Test
public void testIamAddMember() throws IOException {
new IamAddMember()
.iamAddMember(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, "group:test@google.com");
assertThat(stdOut.toString()).contains("Updated IAM policy");
}
@Test
public void testIamGetPolicy() throws IOException {
new IamGetPolicy().iamGetPolicy(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
assertThat(stdOut.toString()).contains("IAM policy");
}
@Test
public void testIamRemoveMember() throws IOException {
new IamRemoveMember()
.iamRemoveMember(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, "group:test@google.com");
assertThat(stdOut.toString()).contains("Updated IAM policy");
}
@Test
public void testSignAsymmetric() throws IOException, GeneralSecurityException {
new SignAsymmetric()
.signAsymmetric(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_RSA_KEY_ID, "1", "my message");
assertThat(stdOut.toString()).contains("Signature");
}
@Test
public void testsignMac() throws IOException, GeneralSecurityException {
new SignMac().signMac(PROJECT_ID, LOCATION_ID, KEY_RING_ID, MAC_KEY_ID, "1", "my message");
assertThat(stdOut.toString()).contains("Signature");
}
@Test
public void testUpdateKeyAddRotation() throws IOException {
new UpdateKeyAddRotation()
.updateKeyAddRotation(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
assertThat(stdOut.toString()).contains("Updated key");
}
@Test
public void testUpdateKeyRemoveLabels() throws IOException {
String keyId = getRandomId();
createSymmetricKeyWithNoInitialVersion(keyId);
new UpdateKeyRemoveLabels()
.updateKeyRemoveLabels(PROJECT_ID, LOCATION_ID, KEY_RING_ID, keyId);
assertThat(stdOut.toString()).contains("Updated key");
}
@Test
public void testUpdateKeyRemoveRotation() throws IOException {
new UpdateKeyRemoveRotation()
.updateKeyRemoveRotation(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
assertThat(stdOut.toString()).contains("Updated key");
}
@Test
public void testUpdateKeySetPrimary() throws IOException {
new UpdateKeySetPrimary()
.updateKeySetPrimary(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, "1");
assertThat(stdOut.toString()).contains("Updated key");
}
@Test
public void testUpdateKeyUpdateLabels() throws IOException {
String keyId = getRandomId();
createSymmetricKeyWithNoInitialVersion(keyId);
new UpdateKeyUpdateLabels()
.updateKeyUpdateLabels(PROJECT_ID, LOCATION_ID, KEY_RING_ID, keyId);
assertThat(stdOut.toString()).contains("Updated key");
}
@Test
public void testVerifyAsymmetricEc() throws IOException, GeneralSecurityException {
String message = "my message";
byte[] signature;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyVersionName versionName =
CryptoKeyVersionName.of(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_EC_KEY_ID, "1");
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(message.getBytes(StandardCharsets.UTF_8));
Digest digest = Digest.newBuilder().setSha256(ByteString.copyFrom(hash)).build();
signature = client.asymmetricSign(versionName, digest).getSignature().toByteArray();
}
new VerifyAsymmetricEc()
.verifyAsymmetricEc(
PROJECT_ID,
LOCATION_ID,
KEY_RING_ID,
ASYMMETRIC_SIGN_EC_KEY_ID,
"1",
message,
signature);
assertThat(stdOut.toString()).contains("Signature");
}
@Test
public void testVerifyAsymmetricRsa() throws IOException, GeneralSecurityException {
String message = "my message";
byte[] signature;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyVersionName versionName =
CryptoKeyVersionName.of(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_RSA_KEY_ID, "1");
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(message.getBytes(StandardCharsets.UTF_8));
Digest digest = Digest.newBuilder().setSha256(ByteString.copyFrom(hash)).build();
signature = client.asymmetricSign(versionName, digest).getSignature().toByteArray();
}
new VerifyAsymmetricRsa()
.verifyAsymmetricRsa(
PROJECT_ID,
LOCATION_ID,
KEY_RING_ID,
ASYMMETRIC_SIGN_RSA_KEY_ID,
"1",
message,
signature);
assertThat(stdOut.toString()).contains("Signature");
}
@Test
public void verifyMac() throws IOException, GeneralSecurityException {
String data = "my data";
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyVersionName versionName =
CryptoKeyVersionName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, MAC_KEY_ID, "1");
MacSignResponse response = client.macSign(versionName, ByteString.copyFromUtf8(data));
new VerifyMac()
.verifyMac(
PROJECT_ID,
LOCATION_ID,
KEY_RING_ID,
MAC_KEY_ID,
"1",
data,
response.getMac().toByteArray());
assertThat(stdOut.toString()).contains("Success: true");
}
}
@Test
public void testKeyImportEndToEnd()
throws IOException, GeneralSecurityException, InterruptedException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
final String importedKeyId = getRandomId();
final String importJobId = getRandomId();
new CreateKeyForImport()
.createKeyForImport(PROJECT_ID, LOCATION_ID, KEY_RING_ID, importedKeyId);
new CreateImportJob().createImportJob(PROJECT_ID, LOCATION_ID, KEY_RING_ID, importJobId);
while (!stdOut.toString().contains("ACTIVE")) {
Thread.sleep(1000);
new CheckStateImportJob()
.checkStateImportJob(PROJECT_ID, LOCATION_ID, KEY_RING_ID, importJobId);
}
new ImportManuallyWrappedKey()
.importManuallyWrappedKey(
PROJECT_ID, LOCATION_ID, KEY_RING_ID, importedKeyId, importJobId);
new CheckStateImportedKey()
.checkStateImportedKey(PROJECT_ID, LOCATION_ID, KEY_RING_ID, importedKeyId, "1");
}
}
}