Skip to content

Commit 873db3e

Browse files
author
Jerjou Cheng
committed
Add tests for kms samples.
1 parent 44623a2 commit 873db3e

File tree

8 files changed

+402
-166
lines changed

8 files changed

+402
-166
lines changed

kms/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
<groupId>com.google.apis</groupId>
1818
<artifactId>google-api-services-cloudkms</artifactId>
1919
<version>v1beta1-rev51-1.18.0-rc</version>
20+
<exclusions>
21+
<exclusion> <!-- exclude an old version of Guava -->
22+
<groupId>com.google.guava</groupId>
23+
<artifactId>guava-jdk5</artifactId>
24+
</exclusion>
25+
</exclusions>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.google.guava</groupId>
29+
<artifactId>guava</artifactId>
30+
<version>20.0</version>
2031
</dependency>
2132
<dependency>
2233
<groupId>com.google.api-client</groupId>

kms/src/main/java/com/example/CryptFile.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,31 @@ public static CloudKMS createAuthorizedClient() throws IOException {
6060
}
6161

6262
/**
63-
* Encrypts the given bytes, using the specified crypto key.
63+
* Encrypts the given bytes, using the primary version of the specified crypto key.
64+
*
65+
* The primary version can be updated via the <a
66+
* href="https://g.co/cloud/kms/docs/reference/rest/v1beta1/projects.locations.keyRings.cryptoKeys/updatePrimaryVersion">updatePrimaryVersion</a>
67+
* method.
6468
*/
6569
public static byte[] encrypt(String projectId, String ringId, String keyId, byte[] plaintext)
6670
throws IOException {
71+
return encrypt(projectId, ringId, keyId, null, plaintext);
72+
}
73+
74+
/**
75+
* Encrypts the given bytes, using the specified crypto key version.
76+
*/
77+
public static byte[] encrypt(
78+
String projectId, String ringId, String keyId, String version, byte[] plaintext)
79+
throws IOException {
6780
String location = "global";
6881
// The resource name of the cryptoKey
6982
String cryptoKeyName = String.format(
7083
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
7184
projectId, location, ringId, keyId);
85+
if (null != version) {
86+
cryptoKeyName += "/cryptoKeyVersions/" + version;
87+
}
7288
// Create the Cloud KMS client.
7389
CloudKMS kms = createAuthorizedClient();
7490

kms/src/main/java/com/example/Quickstart.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

kms/src/main/java/com/example/SnippetCommands.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public void run() throws IOException {
6868
}
6969
}
7070

71+
public static class CreateCryptoKeyVersionCommand extends KeyArgs implements Command {
72+
public void run() throws IOException {
73+
Snippets.createCryptoKeyVersion(projectId, ringId, keyId);
74+
}
75+
}
76+
77+
public static class ListKeyRingsCommand extends ProjectIdArgs implements Command {
78+
public void run() throws IOException {
79+
Snippets.listKeyRings(projectId);
80+
}
81+
}
82+
7183
public static class ListCryptoKeysCommand extends KeyRingArgs implements Command {
7284
public void run() throws IOException {
7385
Snippets.listCryptoKeys(projectId, ringId);
@@ -173,6 +185,8 @@ public void run() throws IOException {
173185
@SubCommands({
174186
@SubCommand(name = "createKeyRing", impl = CreateKeyRingCommand.class),
175187
@SubCommand(name = "createCryptoKey", impl = CreateCryptoKeyCommand.class),
188+
@SubCommand(name = "createCryptoKeyVersion", impl = CreateCryptoKeyVersionCommand.class),
189+
@SubCommand(name = "listKeyRings", impl = ListKeyRingsCommand.class),
176190
@SubCommand(name = "listCryptoKeys", impl = ListCryptoKeysCommand.class),
177191
@SubCommand(name = "listCryptoKeyVersions", impl = ListCryptoKeyVersionsCommand.class),
178192
@SubCommand(name = "disableCryptoKeyVersion", impl = DisableCryptoKeyVersionCommand.class),

kms/src/main/java/com/example/Snippets.java

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.api.services.cloudkms.v1beta1.model.KeyRing;
2929
import com.google.api.services.cloudkms.v1beta1.model.ListCryptoKeyVersionsResponse;
3030
import com.google.api.services.cloudkms.v1beta1.model.ListCryptoKeysResponse;
31+
import com.google.api.services.cloudkms.v1beta1.model.ListKeyRingsResponse;
3132
import com.google.api.services.cloudkms.v1beta1.model.Policy;
3233
import com.google.api.services.cloudkms.v1beta1.model.SetIamPolicyRequest;
3334

@@ -114,6 +115,30 @@ public static CryptoKey createCryptoKey(String projectId, String ringId, String
114115
return createdKey;
115116
}
116117

118+
/**
119+
* Creates a new crypto key version for the given id.
120+
*/
121+
public static void createCryptoKeyVersion(
122+
String projectId, String ringId, String keyId) throws IOException {
123+
String location = "global";
124+
// Create the Cloud KMS client.
125+
CloudKMS kms = createAuthorizedClient();
126+
127+
// The resource name of the cryptoKey
128+
String cryptoKeys = String.format(
129+
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
130+
projectId, location, ringId, keyId);
131+
132+
CryptoKeyVersion version = new CryptoKeyVersion();
133+
134+
CryptoKeyVersion newVersion = kms.projects().locations().keyRings().cryptoKeys()
135+
.cryptoKeyVersions()
136+
.create(cryptoKeys, version)
137+
.execute();
138+
139+
System.out.println(newVersion);
140+
}
141+
117142
/**
118143
* Disables the given version of the crypto key.
119144
*/
@@ -263,11 +288,12 @@ public static Policy addMemberToCryptoKeyPolicy(
263288
iamPolicy.setBindings(bindings);
264289

265290
// Set the new IAM Policy.
266-
Policy newIamPolicy = kms.projects().locations().keyRings().cryptoKeys()
291+
Policy newIamPolicy = kms.projects().locations().keyRings()
292+
.cryptoKeys()
267293
.setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy))
268294
.execute();
269295

270-
System.out.println(newIamPolicy);
296+
System.out.println("Response: " + newIamPolicy);
271297
return newIamPolicy;
272298
}
273299

@@ -320,11 +346,12 @@ public static Policy addMemberToKeyRingPolicy(
320346
iamPolicy.setBindings(bindings);
321347

322348
// Set the new IAM Policy.
323-
Policy newIamPolicy = kms.projects().locations().keyRings()
349+
Policy newIamPolicy = kms.projects().locations()
350+
.keyRings()
324351
.setIamPolicy(keyring, new SetIamPolicyRequest().setPolicy(iamPolicy))
325352
.execute();
326353

327-
System.out.println(newIamPolicy);
354+
System.out.println("Response: " + newIamPolicy);
328355
return newIamPolicy;
329356
}
330357

@@ -346,21 +373,21 @@ public static Policy removeMemberFromCryptoKeyPolicy(
346373
// Get the current IAM policy and add the new account to it.
347374
Policy iamPolicy = getCryptoKeyPolicy(projectId, ringId, keyId);
348375

349-
List<Binding> bindings = iamPolicy.getBindings();
350376
// Filter out the given member
351-
for (Binding b : bindings) {
377+
for (Binding b : iamPolicy.getBindings()) {
352378
if (role.equals(b.getRole()) && b.getMembers().contains(member)) {
353-
b.getMembers().remove(member);
379+
b.getMembers().removeAll(Collections.singletonList(member));
354380
break;
355381
}
356382
}
357383

358384
// Set the new IAM Policy.
359-
Policy newIamPolicy = kms.projects().locations().keyRings().cryptoKeys()
385+
Policy newIamPolicy = kms.projects().locations().keyRings()
386+
.cryptoKeys()
360387
.setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy))
361388
.execute();
362389

363-
System.out.println(newIamPolicy);
390+
System.out.println("Response: " + newIamPolicy);
364391
return newIamPolicy;
365392
}
366393

@@ -382,24 +409,54 @@ public static Policy removeMemberFromKeyRingPolicy(
382409
// Get the current IAM policy and add the new account to it.
383410
Policy iamPolicy = getKeyRingPolicy(projectId, ringId);
384411

385-
List<Binding> bindings = iamPolicy.getBindings();
386412
// Filter out the given member
387-
for (Binding b : bindings) {
413+
for (Binding b : iamPolicy.getBindings()) {
388414
if (role.equals(b.getRole()) && b.getMembers().contains(member)) {
389415
b.getMembers().remove(member);
390416
break;
391417
}
392418
}
393419

394420
// Set the new IAM Policy.
395-
Policy newIamPolicy = kms.projects().locations().keyRings().cryptoKeys()
421+
Policy newIamPolicy = kms.projects().locations()
422+
.keyRings()
396423
.setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy))
397424
.execute();
398425

399-
System.out.println(newIamPolicy);
426+
System.out.println("Response: " + newIamPolicy);
400427
return newIamPolicy;
401428
}
402429

430+
/**
431+
* Prints all the keyrings in the given project.
432+
*/
433+
public static void listKeyRings(String projectId) throws IOException {
434+
String location = "global";
435+
// Create the Cloud KMS client.
436+
CloudKMS kms = createAuthorizedClient();
437+
438+
// The resource name of the cryptoKey
439+
String keyRingPath = String.format(
440+
"projects/%s/locations/%s",
441+
projectId, location);
442+
443+
// Make the RPC call
444+
ListKeyRingsResponse response = kms.projects().locations()
445+
.keyRings()
446+
.list(keyRingPath)
447+
.execute();
448+
449+
// Print the returned key rings
450+
if (null != response.getKeyRings()) {
451+
System.out.println("Key Rings: ");
452+
for (KeyRing keyRing : response.getKeyRings()) {
453+
System.out.println(keyRing.getName());
454+
}
455+
} else {
456+
System.out.println("No keyrings defined.");
457+
}
458+
}
459+
403460
/**
404461
* Prints all the keys in the given key ring.
405462
*/

kms/src/test/java/com/example/QuickstartIT.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)