Skip to content

Commit

Permalink
Adds snippet for restoring a crypto key version (#895)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
WalterHub authored and lesv committed Oct 26, 2017
1 parent 6f25300 commit 02130ca
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions kms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Google [Cloud Key Management Service](https://cloud.google.com/kms/) is a
cloud-hosted key management service that lets you manage encryption for your
cloud services the same way you do on-premise. You can generate, use, rotate and
destroy AES256 encryption keys. These sample Java applications demonstrate
destroy AES-256 encryption keys. These sample Java applications demonstrate
how to access the KMS API using the Google Java API Client Libraries.

## Quickstart
Expand All @@ -17,7 +17,7 @@ Build your project with:
You can run the quickstart with:

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

and can see the available snippet commands with:

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

java -cp target/kms-samples-1.0.0-jar-with-dependencies.jar \
com.example.Snippets createKeyRing -p <your-project-id> myFirstKeyRing
com.example.Snippets createKeyRing -p [your-project-id] [your-location] myFirstKeyRing
7 changes: 7 additions & 0 deletions kms/src/main/java/com/example/SnippetCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public void run() throws IOException {
}
}

public static class RestoreCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
public void run() throws IOException {
Snippets.restoreCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version);
}
}

public static class SetPrimaryVersionCommand extends KeyVersionArgs implements Command {

public void run() throws IOException {
Expand Down Expand Up @@ -206,6 +212,7 @@ public void run() throws IOException {
@SubCommand(name = "listCryptoKeyVersions", impl = ListCryptoKeyVersionsCommand.class),
@SubCommand(name = "disableCryptoKeyVersion", impl = DisableCryptoKeyVersionCommand.class),
@SubCommand(name = "destroyCryptoKeyVersion", impl = DestroyCryptoKeyVersionCommand.class),
@SubCommand(name = "restoreCryptoKeyVersion", impl = RestoreCryptoKeyVersionCommand.class),
@SubCommand(name = "getKeyRingPolicy", impl = GetKeyRingPolicyCommand.class),
@SubCommand(name = "getCryptoKeyPolicy", impl = GetCryptoKeyPolicyCommand.class),
@SubCommand(name = "setPrimaryVersion", impl = SetPrimaryVersionCommand.class),
Expand Down
29 changes: 29 additions & 0 deletions kms/src/main/java/com/example/Snippets.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.api.services.cloudkms.v1.model.CryptoKey;
import com.google.api.services.cloudkms.v1.model.CryptoKeyVersion;
import com.google.api.services.cloudkms.v1.model.DestroyCryptoKeyVersionRequest;
import com.google.api.services.cloudkms.v1.model.RestoreCryptoKeyVersionRequest;
import com.google.api.services.cloudkms.v1.model.KeyRing;
import com.google.api.services.cloudkms.v1.model.ListCryptoKeyVersionsResponse;
import com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse;
Expand Down Expand Up @@ -206,6 +207,34 @@ public static CryptoKeyVersion destroyCryptoKeyVersion(
}
// [END kms_destroy_cryptokey_version]

// [START kms_restore_cryptokey_version]

/**
* Restores the given version of a crypto key that is currently scheduled for destruction.
*/
public static CryptoKeyVersion restoreCryptoKeyVersion(
String projectId, String locationId, String keyRingId, String cryptoKeyId, String version)
throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();

// The resource name of the cryptoKey version
String cryptoKeyVersion = String.format(
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s",
projectId, locationId, keyRingId, cryptoKeyId, version);

RestoreCryptoKeyVersionRequest restoreRequest = new RestoreCryptoKeyVersionRequest();

CryptoKeyVersion restored = kms.projects().locations().keyRings().cryptoKeys()
.cryptoKeyVersions()
.restore(cryptoKeyVersion, restoreRequest)
.execute();

System.out.println(restored);
return restored;
}
// [END kms_restore_cryptokey_version]

// [START kms_get_cryptokey_policy]

/**
Expand Down
27 changes: 27 additions & 0 deletions kms/src/test/java/com/example/SnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ public void destroyCryptoKeyVersion_destroys() throws Exception {
KEY_RING_ID, CRYPTO_KEY_ID, version));
}


@Test
public void restoreCryptoKeyVersion_restores() throws Exception {
Snippets.createCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);

Matcher matcher = Pattern.compile(".*cryptoKeyVersions/(\\d+)\",\"state\":\"ENABLED\".*",
Pattern.DOTALL | Pattern.MULTILINE).matcher(bout.toString().trim());
assertTrue(matcher.matches());

String version = matcher.group(1);

// Only key versions schedule for destruction are restorable, so schedule this key
// version for destruction.
Snippets.destroyCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version);

assertThat(bout.toString()).containsMatch(String.format(
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"DESTROY_SCHEDULED\"",
KEY_RING_ID, CRYPTO_KEY_ID, version));

// Now restore the key version.
Snippets.restoreCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version);

assertThat(bout.toString()).containsMatch(String.format(
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"DISABLED\"",
KEY_RING_ID, CRYPTO_KEY_ID, version));
}

@Test
public void setPrimaryVersion_createKeyAndSetPrimaryVersion() throws Exception {
// We can't test that setPrimaryVersion actually took effect via a list call because of
Expand Down

0 comments on commit 02130ca

Please sign in to comment.