Skip to content

Commit 836b4aa

Browse files
authored
Merge branch 'master' into speech-gcs-examples
2 parents 9fb65f5 + 879aa0d commit 836b4aa

File tree

6 files changed

+188
-6
lines changed

6 files changed

+188
-6
lines changed

kms/pom.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262

6363
<properties>
6464
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
65+
<maven.compiler.source>1.7</maven.compiler.source>
66+
<maven.compiler.target>1.7</maven.compiler.target>
6567
</properties>
6668

6769
<build>
@@ -71,10 +73,6 @@
7173
<groupId>org.apache.maven.plugins</groupId>
7274
<artifactId>maven-compiler-plugin</artifactId>
7375
<version>3.2</version>
74-
<configuration>
75-
<source>5</source>
76-
<target>5</target>
77-
</configuration>
7876
</plugin>
7977
<plugin>
8078
<artifactId>maven-assembly-plugin</artifactId>

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

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static byte[] encrypt(String projectId, String ringId, String keyId, byte
7171
return encrypt(projectId, ringId, keyId, null, plaintext);
7272
}
7373

74+
// [START kms_encrypt]
7475
/**
7576
* Encrypts the given bytes, using the specified crypto key version.
7677
*/
@@ -95,7 +96,9 @@ public static byte[] encrypt(
9596

9697
return response.decodeCiphertext();
9798
}
99+
// [END kms_encrypt]
98100

101+
// [START kms_decrypt]
99102
/**
100103
* Decrypts the given encrypted bytes, using the specified crypto key.
101104
*/
@@ -117,6 +120,7 @@ public static byte[] decrypt(String projectId, String ringId, String keyId, byte
117120

118121
return response.decodePlaintext();
119122
}
123+
// [END kms_decrypt]
120124

121125
public static void main(String[] args) throws IOException {
122126
CryptFileCommands commands = new CryptFileCommands();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ static class Args {
4545
String ringId;
4646
@Argument(metaVar = "keyId", required = true, index = 1, usage = "The key id")
4747
String keyId;
48-
@Argument(metaVar = "inFile", required = true, index = 1, usage = "The source file")
48+
@Argument(metaVar = "inFile", required = true, index = 2, usage = "The source file")
4949
String inFile;
50-
@Argument(metaVar = "outFile", required = true, index = 1, usage = "The destination file")
50+
@Argument(metaVar = "outFile", required = true, index = 3, usage = "The destination file")
5151
String outFile;
5252
}
5353

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Copyright 2016, Google, Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package com.example;
15+
16+
// [START kms_quickstart]
17+
// Imports the Google Cloud client library
18+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
19+
import com.google.api.client.http.HttpTransport;
20+
import com.google.api.client.http.javanet.NetHttpTransport;
21+
import com.google.api.client.json.JsonFactory;
22+
import com.google.api.client.json.jackson2.JacksonFactory;
23+
import com.google.api.services.cloudkms.v1beta1.CloudKMS;
24+
import com.google.api.services.cloudkms.v1beta1.CloudKMSScopes;
25+
import com.google.api.services.cloudkms.v1beta1.model.KeyRing;
26+
import com.google.api.services.cloudkms.v1beta1.model.ListKeyRingsResponse;
27+
28+
import java.io.IOException;
29+
30+
public class Quickstart {
31+
/**
32+
* Creates an authorized CloudKMS client service using Application Default Credentials.
33+
*
34+
* @return an authorized CloudKMS client
35+
* @throws IOException if there's an error getting the default credentials.
36+
*/
37+
public static CloudKMS createAuthorizedClient() throws IOException {
38+
// Create the credential
39+
HttpTransport transport = new NetHttpTransport();
40+
JsonFactory jsonFactory = new JacksonFactory();
41+
// Authorize the client using Application Default Credentials
42+
// @see https://g.co/dv/identity/protocols/application-default-credentials
43+
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
44+
45+
// Depending on the environment that provides the default credentials (e.g. Compute Engine, App
46+
// Engine), the credentials may require us to specify the scopes we need explicitly.
47+
// Check for this case, and inject the scope if required.
48+
if (credential.createScopedRequired()) {
49+
credential = credential.createScoped(CloudKMSScopes.all());
50+
}
51+
52+
return new CloudKMS.Builder(transport, jsonFactory, credential)
53+
.setApplicationName("CloudKMS snippets")
54+
.build();
55+
}
56+
57+
public static void main(String... args) throws Exception {
58+
String projectId = args[0];
59+
// The location of the Key Rings
60+
String location = "global";
61+
62+
// Create the Cloud KMS client.
63+
CloudKMS kms = createAuthorizedClient();
64+
65+
// The resource name of the cryptoKey
66+
String keyRingPath = String.format(
67+
"projects/%s/locations/%s",
68+
projectId, location);
69+
70+
// Make the RPC call
71+
ListKeyRingsResponse response = kms.projects().locations()
72+
.keyRings()
73+
.list(keyRingPath)
74+
.execute();
75+
76+
// Print the returned key rings
77+
if (null != response.getKeyRings()) {
78+
System.out.println("Key Rings: ");
79+
for (KeyRing keyRing : response.getKeyRings()) {
80+
System.out.println(keyRing.getName());
81+
}
82+
} else {
83+
System.out.println("No keyrings defined.");
84+
}
85+
}
86+
}
87+
// [END kms_quickstart]

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

+22
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static CloudKMS createAuthorizedClient() throws IOException {
6767
.build();
6868
}
6969

70+
// [START kms_create_keyring]
7071
/**
7172
* Creates a new key ring with the given id.
7273
*/
@@ -86,7 +87,9 @@ public static KeyRing createKeyRing(String projectId, String ringId) throws IOEx
8687
System.out.println(keyring);
8788
return keyring;
8889
}
90+
// [END kms_create_keyring]
8991

92+
// [START kms_create_cryptokey]
9093
/**
9194
* Creates a new crypto key with the given id.
9295
*/
@@ -114,7 +117,9 @@ public static CryptoKey createCryptoKey(String projectId, String ringId, String
114117
System.out.println(createdKey);
115118
return createdKey;
116119
}
120+
// [END kms_create_cryptokey]
117121

122+
// [START kms_create_cryptokey_version]
118123
/**
119124
* Creates a new crypto key version for the given id.
120125
*/
@@ -138,7 +143,9 @@ public static void createCryptoKeyVersion(
138143

139144
System.out.println(newVersion);
140145
}
146+
// [END kms_create_cryptokey_version]
141147

148+
// [START kms_disable_cryptokey_version]
142149
/**
143150
* Disables the given version of the crypto key.
144151
*/
@@ -166,7 +173,9 @@ public static CryptoKeyVersion disableCryptoKeyVersion(
166173
System.out.println(response);
167174
return response;
168175
}
176+
// [END kms_disable_cryptokey_version]
169177

178+
// [START kms_destroy_cryptokey_version]
170179
/**
171180
* Marks the given version of a crypto key to be destroyed at a scheduled future point.
172181
*/
@@ -192,7 +201,9 @@ public static CryptoKeyVersion destroyCryptoKeyVersion(
192201
System.out.println(destroyed);
193202
return destroyed;
194203
}
204+
// [END kms_destroy_cryptokey_version]
195205

206+
// [START kms_get_cryptokey_policy]
196207
/**
197208
* Retrieves the IAM policy for the given crypto key.
198209
*/
@@ -215,7 +226,9 @@ public static Policy getCryptoKeyPolicy(String projectId, String ringId, String
215226
System.out.println(iamPolicy.getBindings());
216227
return iamPolicy;
217228
}
229+
// [END kms_get_cryptokey_policy]
218230

231+
// [START kms_get_keyring_policy]
219232
/**
220233
* Retrieves the IAM policy for the given crypto key.
221234
*/
@@ -237,7 +250,9 @@ public static Policy getKeyRingPolicy(String projectId, String ringId) throws IO
237250
System.out.println(iamPolicy.getBindings());
238251
return iamPolicy;
239252
}
253+
// [END kms_get_keyring_policy]
240254

255+
// [START kms_add_member_to_cryptokey_policy]
241256
/**
242257
* Adds the given member to the given key, with the given role.
243258
*
@@ -296,7 +311,9 @@ public static Policy addMemberToCryptoKeyPolicy(
296311
System.out.println("Response: " + newIamPolicy);
297312
return newIamPolicy;
298313
}
314+
// [END kms_add_member_to_cryptokey_policy]
299315

316+
// [START kms_add_member_to_keyring_policy]
300317
/**
301318
* Adds the given member to the given keyring, with the given role.
302319
*
@@ -354,7 +371,9 @@ public static Policy addMemberToKeyRingPolicy(
354371
System.out.println("Response: " + newIamPolicy);
355372
return newIamPolicy;
356373
}
374+
// [END kms_add_member_to_keyring_policy]
357375

376+
// [START kms_remove_member_from_cryptokey_policy]
358377
/**
359378
* Removes the given member from the given policy.
360379
*/
@@ -395,7 +414,9 @@ public static Policy removeMemberFromCryptoKeyPolicy(
395414
System.out.println("Response: " + newIamPolicy);
396415
return newIamPolicy;
397416
}
417+
// [END kms_remove_member_from_cryptokey_policy]
398418

419+
// [START kms_remove_member_from_keyring_policy]
399420
/**
400421
* Removes the given member from the given policy.
401422
*/
@@ -431,6 +452,7 @@ public static Policy removeMemberFromKeyRingPolicy(
431452
System.out.println("Response: " + newIamPolicy);
432453
return newIamPolicy;
433454
}
455+
// [END kms_remove_member_from_keyring_policy]
434456

435457
/**
436458
* Prints all the keyrings in the given project.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.example;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import org.junit.AfterClass;
20+
import org.junit.Before;
21+
import org.junit.BeforeClass;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
25+
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
29+
/**
30+
* Integration (system) tests for {@link Quickstart}.
31+
*/
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class QuickstartIT {
35+
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@BeforeClass
40+
public static void setUpClass() throws Exception {
41+
SnippetsIT.setUpClass();
42+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
43+
PrintStream out = new PrintStream(bout);
44+
System.setOut(out);
45+
}
46+
47+
/**
48+
* Destroys all the keys created during this test run.
49+
*/
50+
@AfterClass
51+
public static void tearDownClass() throws Exception {
52+
SnippetsIT.tearDownClass();
53+
}
54+
55+
@Before
56+
public void setUp() throws Exception {
57+
bout = new ByteArrayOutputStream();
58+
out = new PrintStream(bout);
59+
System.setOut(out);
60+
61+
Snippets.createCryptoKeyVersion(
62+
SnippetsIT.PROJECT_ID, SnippetsIT.KEY_RING_ID, SnippetsIT.CRYPTO_KEY_ID);
63+
}
64+
65+
@Test
66+
public void listKeyRings_printsKeyRing() throws Exception {
67+
Quickstart.main(SnippetsIT.PROJECT_ID);
68+
69+
assertThat(bout.toString()).contains(String.format("keyRings/%s", SnippetsIT.KEY_RING_ID));
70+
}
71+
}

0 commit comments

Comments
 (0)