Skip to content

Commit d53b2d0

Browse files
author
Walter Poupore
committed
Adds snippet for restoring a key version
1 parent 373de60 commit d53b2d0

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 29 additions & 0 deletions
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_destroy_cryptokey_version]
237+
209238
// [START kms_get_cryptokey_policy]
210239

211240
/**

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,24 @@ 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\":\"DESTROY_SCHEDULED\".*",
213+
Pattern.DOTALL | Pattern.MULTILINE).matcher(bout.toString().trim());
214+
assertTrue(matcher.matches());
215+
216+
String version = matcher.group(1);
217+
218+
Snippets.restoreCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version);
219+
220+
assertThat(bout.toString()).containsMatch(String.format(
221+
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"DISABLED\"",
222+
KEY_RING_ID, CRYPTO_KEY_ID, version));
223+
}
224+
207225
@Test
208226
public void setPrimaryVersion_createKeyAndSetPrimaryVersion() throws Exception {
209227
// We can't test that setPrimaryVersion actually took effect via a list call because of

0 commit comments

Comments
 (0)