GeezerVison is showing github.com — Adds snippet for restoring a crypto key version (#895) · bartprokop/java-docs-samples@02130ca
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: kms/README.md +3-3Lines changed: 3 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | Google [Cloud Key Management Service](https://cloud.google.com/kms/) is a | 4 | 4 | cloud-hosted key management service that lets you manage encryption for your | 5 | 5 | 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 | 7 | 7 | how to access the KMS API using the Google Java API Client Libraries. | 8 | 8 |
| 9 | 9 | ## Quickstart | @@ -17,7 +17,7 @@ Build your project with: | 17 | 17 | You can run the quickstart with: | 18 | 18 |
| 19 | 19 | 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] | 21 | 21 |
| 22 | 22 | and can see the available snippet commands with: | 23 | 23 |
| @@ -27,4 +27,4 @@ and can see the available snippet commands with: | 27 | 27 | For example: | 28 | 28 |
| 29 | 29 | 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 |
Collapse file Copy file name to clipboardExpand all lines: kms/src/main/java/com/example/SnippetCommands.java +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,12 @@ public void run() throws IOException { | 109 | 109 | } | 110 | 110 | } | 111 | 111 |
| | 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 | + | 112 | 118 | public static class SetPrimaryVersionCommand extends KeyVersionArgs implements Command { | 113 | 119 |
| 114 | 120 | public void run() throws IOException { | @@ -206,6 +212,7 @@ public void run() throws IOException { | 206 | 212 | @SubCommand(name = "listCryptoKeyVersions", impl = ListCryptoKeyVersionsCommand.class), | 207 | 213 | @SubCommand(name = "disableCryptoKeyVersion", impl = DisableCryptoKeyVersionCommand.class), | 208 | 214 | @SubCommand(name = "destroyCryptoKeyVersion", impl = DestroyCryptoKeyVersionCommand.class), | | 215 | + @SubCommand(name = "restoreCryptoKeyVersion", impl = RestoreCryptoKeyVersionCommand.class), | 209 | 216 | @SubCommand(name = "getKeyRingPolicy", impl = GetKeyRingPolicyCommand.class), | 210 | 217 | @SubCommand(name = "getCryptoKeyPolicy", impl = GetCryptoKeyPolicyCommand.class), | 211 | 218 | @SubCommand(name = "setPrimaryVersion", impl = SetPrimaryVersionCommand.class), | |
Collapse file Copy file name to clipboardExpand all lines: kms/src/main/java/com/example/Snippets.java +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | import com.google.api.services.cloudkms.v1.model.CryptoKey; | 27 | 27 | import com.google.api.services.cloudkms.v1.model.CryptoKeyVersion; | 28 | 28 | import com.google.api.services.cloudkms.v1.model.DestroyCryptoKeyVersionRequest; | | 29 | +import com.google.api.services.cloudkms.v1.model.RestoreCryptoKeyVersionRequest; | 29 | 30 | import com.google.api.services.cloudkms.v1.model.KeyRing; | 30 | 31 | import com.google.api.services.cloudkms.v1.model.ListCryptoKeyVersionsResponse; | 31 | 32 | import com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse; | @@ -206,6 +207,34 @@ public static CryptoKeyVersion destroyCryptoKeyVersion( | 206 | 207 | } | 207 | 208 | // [END kms_destroy_cryptokey_version] | 208 | 209 |
| | 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 | + | 209 | 238 | // [START kms_get_cryptokey_policy] | 210 | 239 |
| 211 | 240 | /** | |
Collapse file Copy file name to clipboardExpand all lines: kms/src/test/java/com/example/SnippetsIT.java +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -204,6 +204,33 @@ public void destroyCryptoKeyVersion_destroys() throws Exception { | 204 | 204 | KEY_RING_ID, CRYPTO_KEY_ID, version)); | 205 | 205 | } | 206 | 206 |
| | 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 | + | 207 | 234 | @Test | 208 | 235 | public void setPrimaryVersion_createKeyAndSetPrimaryVersion() throws Exception { | 209 | 236 | // We can't test that setPrimaryVersion actually took effect via a list call because of | |
|