Skip to content

Commit 02130ca

Browse files
WalterHublesv
authored andcommitted
Adds snippet for restoring a crypto key version (#895)
* Updates READ with minor corrections * Updates READ with minor corrections * Adds snippet for restoring a key version * Fixes region tag in comment. * Updated test to schedule key version destruction prior to restore attempt
1 parent 6f25300 commit 02130ca

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

kms/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Google [Cloud Key Management Service](https://cloud.google.com/kms/) is a
44
cloud-hosted key management service that lets you manage encryption for your
55
cloud services the same way you do on-premise. You can generate, use, rotate and
6-
destroy AES256 encryption keys. These sample Java applications demonstrate
6+
destroy AES-256 encryption keys. These sample Java applications demonstrate
77
how to access the KMS API using the Google Java API Client Libraries.
88

99
## Quickstart
@@ -17,7 +17,7 @@ Build your project with:
1717
You can run the quickstart with:
1818

1919
java -cp target/kms-samples-1.0.0-jar-with-dependencies.jar \
20-
com.example.Quickstart [your-project-id]
20+
com.example.Quickstart [your-project-id] [your-location]
2121

2222
and can see the available snippet commands with:
2323

@@ -27,4 +27,4 @@ and can see the available snippet commands with:
2727
For example:
2828

2929
java -cp target/kms-samples-1.0.0-jar-with-dependencies.jar \
30-
com.example.Snippets createKeyRing -p <your-project-id> myFirstKeyRing
30+
com.example.Snippets createKeyRing -p [your-project-id] [your-location] myFirstKeyRing

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

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ public void run() throws IOException {
109109
}
110110
}
111111

112+
public static class RestoreCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
113+
public void run() throws IOException {
114+
Snippets.restoreCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version);
115+
}
116+
}
117+
112118
public static class SetPrimaryVersionCommand extends KeyVersionArgs implements Command {
113119

114120
public void run() throws IOException {
@@ -206,6 +212,7 @@ public void run() throws IOException {
206212
@SubCommand(name = "listCryptoKeyVersions", impl = ListCryptoKeyVersionsCommand.class),
207213
@SubCommand(name = "disableCryptoKeyVersion", impl = DisableCryptoKeyVersionCommand.class),
208214
@SubCommand(name = "destroyCryptoKeyVersion", impl = DestroyCryptoKeyVersionCommand.class),
215+
@SubCommand(name = "restoreCryptoKeyVersion", impl = RestoreCryptoKeyVersionCommand.class),
209216
@SubCommand(name = "getKeyRingPolicy", impl = GetKeyRingPolicyCommand.class),
210217
@SubCommand(name = "getCryptoKeyPolicy", impl = GetCryptoKeyPolicyCommand.class),
211218
@SubCommand(name = "setPrimaryVersion", impl = SetPrimaryVersionCommand.class),

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

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.api.services.cloudkms.v1.model.CryptoKey;
2727
import com.google.api.services.cloudkms.v1.model.CryptoKeyVersion;
2828
import com.google.api.services.cloudkms.v1.model.DestroyCryptoKeyVersionRequest;
29+
import com.google.api.services.cloudkms.v1.model.RestoreCryptoKeyVersionRequest;
2930
import com.google.api.services.cloudkms.v1.model.KeyRing;
3031
import com.google.api.services.cloudkms.v1.model.ListCryptoKeyVersionsResponse;
3132
import com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse;
@@ -206,6 +207,34 @@ public static CryptoKeyVersion destroyCryptoKeyVersion(
206207
}
207208
// [END kms_destroy_cryptokey_version]
208209

210+
// [START kms_restore_cryptokey_version]
211+
212+
/**
213+
* Restores the given version of a crypto key that is currently scheduled for destruction.
214+
*/
215+
public static CryptoKeyVersion restoreCryptoKeyVersion(
216+
String projectId, String locationId, String keyRingId, String cryptoKeyId, String version)
217+
throws IOException {
218+
// Create the Cloud KMS client.
219+
CloudKMS kms = createAuthorizedClient();
220+
221+
// The resource name of the cryptoKey version
222+
String cryptoKeyVersion = String.format(
223+
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s",
224+
projectId, locationId, keyRingId, cryptoKeyId, version);
225+
226+
RestoreCryptoKeyVersionRequest restoreRequest = new RestoreCryptoKeyVersionRequest();
227+
228+
CryptoKeyVersion restored = kms.projects().locations().keyRings().cryptoKeys()
229+
.cryptoKeyVersions()
230+
.restore(cryptoKeyVersion, restoreRequest)
231+
.execute();
232+
233+
System.out.println(restored);
234+
return restored;
235+
}
236+
// [END kms_restore_cryptokey_version]
237+
209238
// [START kms_get_cryptokey_policy]
210239

211240
/**

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

+27
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,33 @@ public void destroyCryptoKeyVersion_destroys() throws Exception {
204204
KEY_RING_ID, CRYPTO_KEY_ID, version));
205205
}
206206

207+
208+
@Test
209+
public void restoreCryptoKeyVersion_restores() throws Exception {
210+
Snippets.createCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);
211+
212+
Matcher matcher = Pattern.compile(".*cryptoKeyVersions/(\\d+)\",\"state\":\"ENABLED\".*",
213+
Pattern.DOTALL | Pattern.MULTILINE).matcher(bout.toString().trim());
214+
assertTrue(matcher.matches());
215+
216+
String version = matcher.group(1);
217+
218+
// Only key versions schedule for destruction are restorable, so schedule this key
219+
// version for destruction.
220+
Snippets.destroyCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version);
221+
222+
assertThat(bout.toString()).containsMatch(String.format(
223+
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"DESTROY_SCHEDULED\"",
224+
KEY_RING_ID, CRYPTO_KEY_ID, version));
225+
226+
// Now restore the key version.
227+
Snippets.restoreCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version);
228+
229+
assertThat(bout.toString()).containsMatch(String.format(
230+
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"DISABLED\"",
231+
KEY_RING_ID, CRYPTO_KEY_ID, version));
232+
}
233+
207234
@Test
208235
public void setPrimaryVersion_createKeyAndSetPrimaryVersion() throws Exception {
209236
// We can't test that setPrimaryVersion actually took effect via a list call because of

0 commit comments

Comments
 (0)