Skip to content

Commit 44704f0

Browse files
committed
addressed PR comments
1 parent 46ab65a commit 44704f0

File tree

11 files changed

+47
-62
lines changed

11 files changed

+47
-62
lines changed

kms/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<parent>
1313
<groupId>com.google.cloud.samples</groupId>
1414
<artifactId>shared-configuration</artifactId>
15-
<version>1.0.9</version>
15+
<version>1.0.10</version>
1616
</parent>
1717

1818
<properties>

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google Inc.
2+
* Copyright 2018 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,27 +53,20 @@ public static CryptoKey createAsymmetricKey(String projectId, String locationId,
5353
String cryptoKeyId)
5454
throws IOException {
5555

56-
// Create the Cloud KMS client.
5756
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
58-
// The resource name of the location associated with the KeyRing.
5957
String parent = KeyRingName.format(projectId, locationId, keyRingId);
6058

61-
// Choose a purpose (ASYMMETRIC_DECRYPT or ASYMMETRIC_SIGN).
6259
CryptoKeyPurpose purpose = CryptoKeyPurpose.ASYMMETRIC_DECRYPT;
63-
64-
// Choose an algorithm.
6560
CryptoKeyVersionAlgorithm algorithm = CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256;
61+
6662
CryptoKeyVersionTemplate version = CryptoKeyVersionTemplate.newBuilder()
6763
.setAlgorithm(algorithm)
6864
.build();
69-
70-
// Build the key template
7165
CryptoKey cryptoKey = CryptoKey.newBuilder()
7266
.setPurpose(purpose)
7367
.setVersionTemplate(version)
7468
.build();
7569

76-
// Create the CryptoKey for your project.
7770
CryptoKey createdKey = client.createCryptoKey(parent, cryptoKeyId, cryptoKey);
7871

7972
return createdKey;
@@ -156,7 +149,9 @@ public static byte[] encryptRSA(String keyName, byte[] plaintext)
156149
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey);
157150
PublicKey rsaKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
158151

159-
// Encrypt the plaintext
152+
// Encrypt plaintext for the 'RSA_DECRYPT_OAEP_2048_SHA256' key.
153+
// For other key algorithms:
154+
// https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html
160155
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
161156
OAEPParameterSpec oaepParams = new OAEPParameterSpec(
162157
"SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
@@ -217,7 +212,9 @@ public static boolean verifySignatureRSA(String keyName, byte[] message, byte[]
217212
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey);
218213
PublicKey rsaKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
219214

220-
// Verify the signature
215+
// Verify the 'RSA_SIGN_PKCS1_2048_SHA256' signature.
216+
// For other key algorithms:
217+
// http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Signature
221218
Signature rsaVerify = Signature.getInstance("SHA256withRSA");
222219
rsaVerify.initVerify(rsaKey);
223220
rsaVerify.update(message);
@@ -249,7 +246,9 @@ public static boolean verifySignatureEC(String keyName, byte[] message, byte[] s
249246
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey);
250247
PublicKey ecKey = KeyFactory.getInstance("EC").generatePublic(keySpec);
251248

252-
// Verify the signature
249+
// Verify the 'EC_SIGN_P256_SHA256' signature
250+
// For other key algorithms:
251+
// http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Signature
253252
Signature ecVerify = Signature.getInstance("SHA256withECDSA");
254253
ecVerify.initVerify(ecKey);
255254
ecVerify.update(message);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google Inc.
2+
* Copyright 2018 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ public static byte[] encrypt(
3838
throws IOException {
3939

4040
// Create the KeyManagementServiceClient using try-with-resources to manage client cleanup.
41-
try (KeyManagementServiceClient client = Snippets.createClient()) {
41+
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
4242

4343
// The resource name of the cryptoKey
4444
String resourceName = CryptoKeyName.format(projectId, locationId, keyRingId, cryptoKeyId);
@@ -62,7 +62,7 @@ public static byte[] decrypt(
6262
throws IOException {
6363

6464
// Create the KeyManagementServiceClient using try-with-resources to manage client cleanup.
65-
try (KeyManagementServiceClient client = Snippets.createClient()) {
65+
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
6666

6767
// The resource name of the cryptoKey
6868
String resourceName = CryptoKeyName.format(projectId, locationId, keyRingId, cryptoKeyId);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google Inc.
2+
* Copyright 2017 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google Inc.
2+
* Copyright 2018 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,8 +38,6 @@ public static void main(String... args) throws Exception {
3838
String location = args[1];
3939

4040
// Create the KeyManagementServiceClient using try-with-resources to manage client cleanup.
41-
// GAP: missing ability to set application name
42-
// see https://github.com/googleapis/gax-java/issues/614
4341
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
4442

4543
// The resource name of the location to search

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google Inc.
2+
* Copyright 2017 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google Inc.
2+
* Copyright 2017 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,18 +43,6 @@
4343

4444
public class Snippets {
4545

46-
/**
47-
* Creates a KeyManagementServiceClient using Application Default Credentials.
48-
*
49-
* @return an authorized KeyManagementServiceClient.
50-
* @throws IOException if there's an error getting the default credentials.
51-
*/
52-
public static KeyManagementServiceClient createClient() throws IOException {
53-
// GAP: missing ability to set application name
54-
// see https://github.com/googleapis/gax-java/issues/614
55-
return KeyManagementServiceClient.create();
56-
}
57-
5846
// [START kms_create_keyring]
5947

6048
/**
@@ -411,16 +399,16 @@ public static Policy removeMemberFromCryptoKeyPolicy(
411399

412400
// Create a bindings list that filters out the provided member
413401
List<Binding> newBindings = new ArrayList<>();
414-
for (Binding b : iamPolicy.getBindingsList()) {
415-
if (!b.getRole().equals(role)) {
416-
newBindings.add(b);
402+
for (Binding binding : iamPolicy.getBindingsList()) {
403+
if (!binding.getRole().equals(role)) {
404+
newBindings.add(binding);
417405
continue;
418406
}
419407

420-
Binding.Builder builder = Binding.newBuilder().setRole(b.getRole());
421-
for (String m : b.getMembersList()) {
422-
if (!member.equals(m)) {
423-
builder.addMembers(m);
408+
Binding.Builder builder = Binding.newBuilder().setRole(binding.getRole());
409+
for (String bindingMember : binding.getMembersList()) {
410+
if (!member.equals(bindingMember)) {
411+
builder.addMembers(bindingMember);
424412
}
425413
}
426414
newBindings.add(builder.build());
@@ -458,14 +446,14 @@ public static Policy removeMemberFromKeyRingPolicy(
458446

459447
// Create a bindings list that filters out the provided member
460448
List<Binding> newBindings = new ArrayList<>();
461-
for (Binding b : iamPolicy.getBindingsList()) {
462-
if (!b.getRole().equals(role)) {
463-
newBindings.add(b);
449+
for (Binding binding : iamPolicy.getBindingsList()) {
450+
if (!binding.getRole().equals(role)) {
451+
newBindings.add(binding);
464452
} else {
465-
Binding.Builder builder = Binding.newBuilder().setRole(b.getRole());
466-
for (String m : b.getMembersList()) {
467-
if (!member.equals(m)) {
468-
builder.addMembers(m);
453+
Binding.Builder builder = Binding.newBuilder().setRole(binding.getRole());
454+
for (String bindingMember : binding.getMembersList()) {
455+
if (!member.equals(bindingMember)) {
456+
builder.addMembers(bindingMember);
469457
}
470458
}
471459
newBindings.add(builder.build());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google Inc.
2+
* Copyright 2018 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,7 +89,7 @@ private static boolean createKeyHelper(
8989

9090
@BeforeClass
9191
public static void setUpClass() throws Exception {
92-
// TODO: set application name
92+
// TODO: set application name. https://github.com/googleapis/gax-java/issues/614
9393
client = KeyManagementServiceClient.create();
9494

9595
try {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google Inc.
2+
* Copyright 2018 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,11 +67,11 @@ public static void tearDownClass() throws Exception {
6767
List<CryptoKeyVersion> versions =
6868
Snippets.listCryptoKeyVersions(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);
6969

70-
for (CryptoKeyVersion v : versions) {
71-
if (!v.getState().equals(CryptoKeyVersion.CryptoKeyVersionState.DESTROY_SCHEDULED)) {
70+
for (CryptoKeyVersion version : versions) {
71+
if (!version.getState().equals(CryptoKeyVersion.CryptoKeyVersionState.DESTROY_SCHEDULED)) {
7272
Snippets.destroyCryptoKeyVersion(
7373
PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID,
74-
SnippetsIT.parseVersionId(v.getName()));
74+
SnippetsIT.parseVersionId(version.getName()));
7575
}
7676
}
7777
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google Inc.
2+
* Copyright 2017 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google Inc.
2+
* Copyright 2017 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,10 +73,10 @@ public static void tearDownClass() throws Exception {
7373
List<CryptoKeyVersion> versions =
7474
Snippets.listCryptoKeyVersions(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);
7575

76-
for (CryptoKeyVersion v : versions) {
77-
if (!v.getState().equals(CryptoKeyVersion.CryptoKeyVersionState.DESTROY_SCHEDULED)) {
76+
for (CryptoKeyVersion version : versions) {
77+
if (!version.getState().equals(CryptoKeyVersion.CryptoKeyVersionState.DESTROY_SCHEDULED)) {
7878
Snippets.destroyCryptoKeyVersion(
79-
PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, parseVersionId(v.getName()));
79+
PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, parseVersionId(version.getName()));
8080
}
8181
}
8282
}
@@ -101,11 +101,11 @@ public void listCryptoKeyVersions_retrievesVersions() throws Exception {
101101
List<CryptoKeyVersion> versions =
102102
Snippets.listCryptoKeyVersions(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);
103103

104-
for (CryptoKeyVersion v : versions) {
105-
assertThat(v.getName()).contains(String.format(
104+
for (CryptoKeyVersion version : versions) {
105+
assertThat(version.getName()).contains(String.format(
106106
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/", KEY_RING_ID, CRYPTO_KEY_ID));
107107

108-
if (v.getState().equals(CryptoKeyVersion.CryptoKeyVersionState.ENABLED)) {
108+
if (version.getState().equals(CryptoKeyVersion.CryptoKeyVersionState.ENABLED)) {
109109
return;
110110
}
111111
}

0 commit comments

Comments
 (0)