Skip to content

Commit 966a24f

Browse files
white0utlesv
authored andcommitted
KMS: Make samples consistent with all languages. (#816)
In particular: * Define keyRingId and cryptoKeyId consistently with the API. * User defined locationId everywhere. I opted to keep the file I/O in the Command runner to keep the API clean, especially when being rendered on https://cloud.google.com/kms/docs/encrypt-decrypt. Tracking bug: http://b/64758639
1 parent 513173a commit 966a24f

File tree

7 files changed

+164
-183
lines changed

7 files changed

+164
-183
lines changed

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,64 +59,51 @@ public static CloudKMS createAuthorizedClient() throws IOException {
5959
.build();
6060
}
6161

62-
/**
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/v1/projects.locations.keyRings.cryptoKeys/updatePrimaryVersion">updatePrimaryVersion</a>
67-
* method.
68-
*/
69-
public static byte[] encrypt(String projectId, String ringId, String keyId, byte[] plaintext)
70-
throws IOException {
71-
return encrypt(projectId, ringId, keyId, null, plaintext);
72-
}
73-
7462
// [START kms_encrypt]
63+
7564
/**
76-
* Encrypts the given bytes, using the specified crypto key version.
65+
* Encrypts the given plaintext using the specified crypto key.
7766
*/
7867
public static byte[] encrypt(
79-
String projectId, String ringId, String keyId, String version, byte[] plaintext)
68+
String projectId, String locationId, String keyRingId, String cryptoKeyId, byte[] plaintext)
8069
throws IOException {
81-
String location = "global";
8270
// The resource name of the cryptoKey
83-
String cryptoKeyName = String.format(
71+
String resourceName = String.format(
8472
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
85-
projectId, location, ringId, keyId);
86-
if (null != version) {
87-
cryptoKeyName += "/cryptoKeyVersions/" + version;
88-
}
73+
projectId, locationId, keyRingId, cryptoKeyId);
74+
8975
// Create the Cloud KMS client.
9076
CloudKMS kms = createAuthorizedClient();
9177

9278
EncryptRequest request = new EncryptRequest().encodePlaintext(plaintext);
9379
EncryptResponse response = kms.projects().locations().keyRings().cryptoKeys()
94-
.encrypt(cryptoKeyName, request)
95-
.execute();
80+
.encrypt(resourceName, request)
81+
.execute();
9682

9783
return response.decodeCiphertext();
9884
}
9985
// [END kms_encrypt]
10086

10187
// [START kms_decrypt]
88+
10289
/**
103-
* Decrypts the given encrypted bytes, using the specified crypto key.
90+
* Decrypts the provided ciphertext with the specified crypto key.
10491
*/
105-
public static byte[] decrypt(String projectId, String ringId, String keyId, byte[] encrypted)
92+
public static byte[] decrypt(String projectId, String locationId, String keyRingId,
93+
String cryptoKeyId, byte[] ciphertext)
10694
throws IOException {
107-
String location = "global";
10895
// Create the Cloud KMS client.
10996
CloudKMS kms = createAuthorizedClient();
11097

11198
// The resource name of the cryptoKey
11299
String cryptoKeyName = String.format(
113100
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
114-
projectId, location, ringId, keyId);
101+
projectId, locationId, keyRingId, cryptoKeyId);
115102

116-
DecryptRequest request = new DecryptRequest().encodeCiphertext(encrypted);
103+
DecryptRequest request = new DecryptRequest().encodeCiphertext(ciphertext);
117104
DecryptResponse response = kms.projects().locations().keyRings().cryptoKeys()
118-
.decrypt(cryptoKeyName, request)
119-
.execute();
105+
.decrypt(cryptoKeyName, request)
106+
.execute();
120107

121108
return response.decodePlaintext();
122109
}

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
import org.kohsuke.args4j.spi.SubCommandHandler;
2121
import org.kohsuke.args4j.spi.SubCommands;
2222

23-
import java.io.FileOutputStream;
24-
import java.io.IOException;
2523
import java.nio.file.Files;
2624
import java.nio.file.Paths;
2725

26+
import java.io.FileOutputStream;
27+
import java.io.IOException;
28+
2829
/**
2930
* Defines the different sub-commands and their parameters, for command-line invocation.
3031
*/
@@ -33,50 +34,44 @@ class CryptFileCommands {
3334
* An interface for a command-line sub-command.
3435
*/
3536
interface Command {
36-
public void run() throws IOException;
37+
void run() throws IOException;
3738
}
3839

3940
// Most of the commands take some subset of the same arguments, so specify groups of arguments
4041
// as classes for greater code reuse.
4142
static class Args {
4243
@Option(name = "--project-id", aliases = "-p", required = true, usage = "Your GCP project ID")
4344
String projectId;
44-
@Argument(metaVar = "ringId", required = true, index = 0, usage = "The ring id")
45-
String ringId;
46-
@Argument(metaVar = "keyId", required = true, index = 1, usage = "The key id")
47-
String keyId;
48-
@Argument(metaVar = "inFile", required = true, index = 2, usage = "The source file")
45+
@Argument(metaVar = "locationId", required = true, index = 0, usage = "The key location")
46+
String locationId;
47+
@Argument(metaVar = "keyRingId", required = true, index = 1, usage = "The key ring id")
48+
String keyRingId;
49+
@Argument(metaVar = "cryptoKeyId", required = true, index = 2, usage = "The crypto key id")
50+
String cryptoKeyId;
51+
@Argument(metaVar = "inFile", required = true, index = 3, usage = "The source file")
4952
String inFile;
50-
@Argument(metaVar = "outFile", required = true, index = 3, usage = "The destination file")
53+
@Argument(metaVar = "outFile", required = true, index = 4, usage = "The destination file")
5154
String outFile;
5255
}
5356

5457
public static class EncryptCommand extends Args implements Command {
5558
public void run() throws IOException {
5659
byte[] encrypted = CryptFile.encrypt(
57-
projectId, ringId, keyId,
58-
Files.readAllBytes(Paths.get(inFile)));
60+
projectId, locationId, keyRingId, cryptoKeyId, Files.readAllBytes(Paths.get(inFile)));
5961

60-
FileOutputStream stream = new FileOutputStream(outFile);
61-
try {
62+
try (FileOutputStream stream = new FileOutputStream(outFile)) {
6263
stream.write(encrypted);
63-
} finally {
64-
stream.close();
6564
}
6665
}
6766
}
6867

6968
public static class DecryptCommand extends Args implements Command {
7069
public void run() throws IOException {
7170
byte[] decrypted = CryptFile.decrypt(
72-
projectId, ringId, keyId,
73-
Files.readAllBytes(Paths.get(inFile)));
71+
projectId, locationId, keyRingId, cryptoKeyId, Files.readAllBytes(Paths.get(inFile)));
7472

75-
FileOutputStream stream = new FileOutputStream(outFile);
76-
try {
73+
try (FileOutputStream stream = new FileOutputStream(outFile)) {
7774
stream.write(decrypted);
78-
} finally {
79-
stream.close();
8075
}
8176
}
8277
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static CloudKMS createAuthorizedClient() throws IOException {
5757
public static void main(String... args) throws Exception {
5858
String projectId = args[0];
5959
// The location of the Key Rings
60-
String location = "global";
60+
String location = args[1];
6161

6262
// Create the Cloud KMS client.
6363
CloudKMS kms = createAuthorizedClient();
@@ -80,7 +80,7 @@ public static void main(String... args) throws Exception {
8080
System.out.println(keyRing.getName());
8181
}
8282
} else {
83-
System.out.println("No keyrings defined.");
83+
System.out.println("No key rings defined.");
8484
}
8585
}
8686
}

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SnippetCommands {
3030
* An interface for a command-line sub-command.
3131
*/
3232
interface Command {
33-
public void run() throws IOException;
33+
void run() throws IOException;
3434
}
3535

3636
// Most of the commands take some subset of the same arguments, so specify groups of arguments
@@ -40,79 +40,84 @@ static class ProjectIdArgs {
4040
String projectId;
4141
}
4242

43-
static class KeyRingArgs extends ProjectIdArgs {
44-
@Argument(metaVar = "ringId", required = true, index = 0, usage = "The ring id")
45-
String ringId;
43+
static class LocationIdArgs extends ProjectIdArgs {
44+
@Argument(metaVar = "locationId", required = true, index = 0, usage = "The location id")
45+
String locationId;
46+
}
47+
48+
static class KeyRingArgs extends LocationIdArgs {
49+
@Argument(metaVar = "keyRingId", required = true, index = 1, usage = "The key ring id")
50+
String keyRingId;
4651
}
4752

4853
static class KeyArgs extends KeyRingArgs {
49-
@Argument(metaVar = "keyId", required = true, index = 1, usage = "The key id")
50-
String keyId;
54+
@Argument(metaVar = "cryptoKeyId", required = true, index = 2, usage = "The crypto key id")
55+
String cryptoKeyId;
5156
}
5257

5358
static class KeyVersionArgs extends KeyArgs {
54-
@Argument(metaVar = "version", required = true, index = 2, usage = "The key version")
59+
@Argument(metaVar = "version", required = true, index = 3, usage = "The key version")
5560
String version;
5661
}
5762

5863

5964
public static class CreateKeyRingCommand extends KeyRingArgs implements Command {
6065
public void run() throws IOException {
61-
Snippets.createKeyRing(projectId, ringId);
66+
Snippets.createKeyRing(projectId, locationId, keyRingId);
6267
}
6368
}
6469

6570
public static class CreateCryptoKeyCommand extends KeyArgs implements Command {
6671
public void run() throws IOException {
67-
Snippets.createCryptoKey(projectId, ringId, keyId);
72+
Snippets.createCryptoKey(projectId, locationId, keyRingId, cryptoKeyId);
6873
}
6974
}
7075

7176
public static class CreateCryptoKeyVersionCommand extends KeyArgs implements Command {
7277
public void run() throws IOException {
73-
Snippets.createCryptoKeyVersion(projectId, ringId, keyId);
78+
Snippets.createCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId);
7479
}
7580
}
7681

77-
public static class ListKeyRingsCommand extends ProjectIdArgs implements Command {
82+
public static class ListKeyRingsCommand extends LocationIdArgs implements Command {
7883
public void run() throws IOException {
79-
Snippets.listKeyRings(projectId);
84+
Snippets.listKeyRings(projectId, locationId);
8085
}
8186
}
8287

8388
public static class ListCryptoKeysCommand extends KeyRingArgs implements Command {
8489
public void run() throws IOException {
85-
Snippets.listCryptoKeys(projectId, ringId);
90+
Snippets.listCryptoKeys(projectId, locationId, keyRingId);
8691
}
8792
}
8893

8994
public static class ListCryptoKeyVersionsCommand extends KeyArgs implements Command {
9095
public void run() throws IOException {
91-
Snippets.listCryptoKeyVersions(projectId, ringId, keyId);
96+
Snippets.listCryptoKeyVersions(projectId, locationId, keyRingId, cryptoKeyId);
9297
}
9398
}
9499

95100
public static class DisableCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
96101
public void run() throws IOException {
97-
Snippets.disableCryptoKeyVersion(projectId, ringId, keyId, version);
102+
Snippets.disableCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version);
98103
}
99104
}
100105

101106
public static class DestroyCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
102107
public void run() throws IOException {
103-
Snippets.destroyCryptoKeyVersion(projectId, ringId, keyId, version);
108+
Snippets.destroyCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version);
104109
}
105110
}
106111

107112
public static class GetKeyRingPolicyCommand extends KeyRingArgs implements Command {
108113
public void run() throws IOException {
109-
Snippets.getKeyRingPolicy(projectId, ringId);
114+
Snippets.getKeyRingPolicy(projectId, locationId, keyRingId);
110115
}
111116
}
112117

113118
public static class GetCryptoKeyPolicyCommand extends KeyArgs implements Command {
114119
public void run() throws IOException {
115-
Snippets.getCryptoKeyPolicy(projectId, ringId, keyId);
120+
Snippets.getCryptoKeyPolicy(projectId, locationId, keyRingId, cryptoKeyId);
116121
}
117122
}
118123

@@ -128,7 +133,7 @@ public static class AddMemberToKeyRingPolicyCommand extends KeyRingArgs implemen
128133
String role;
129134

130135
public void run() throws IOException {
131-
Snippets.addMemberToKeyRingPolicy(projectId, ringId, member, role);
136+
Snippets.addMemberToKeyRingPolicy(projectId, locationId, keyRingId, member, role);
132137
}
133138
}
134139

@@ -144,7 +149,7 @@ public static class AddMemberToCryptoKeyPolicyCommand extends KeyArgs implements
144149
String role;
145150

146151
public void run() throws IOException {
147-
Snippets.addMemberToCryptoKeyPolicy(projectId, ringId, keyId, member, role);
152+
Snippets.addMemberToCryptoKeyPolicy(projectId, locationId, keyRingId, cryptoKeyId, member, role);
148153
}
149154
}
150155

@@ -160,7 +165,7 @@ public static class RemoveMemberFromKeyRingPolicyCommand extends KeyRingArgs imp
160165
String role;
161166

162167
public void run() throws IOException {
163-
Snippets.removeMemberFromKeyRingPolicy(projectId, ringId, member, role);
168+
Snippets.removeMemberFromKeyRingPolicy(projectId, locationId, keyRingId, member, role);
164169
}
165170
}
166171

@@ -176,7 +181,7 @@ public static class RemoveMemberFromCryptoKeyPolicyCommand extends KeyArgs imple
176181
String role;
177182

178183
public void run() throws IOException {
179-
Snippets.removeMemberFromCryptoKeyPolicy(projectId, ringId, keyId, member, role);
184+
Snippets.removeMemberFromCryptoKeyPolicy(projectId, locationId, keyRingId, cryptoKeyId, member, role);
180185
}
181186
}
182187

0 commit comments

Comments
 (0)