Search in sources :

Example 36 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project java-docs-samples by GoogleCloudPlatform.

the class SnippetsIT method testAccessSecretVersion.

@Test
public void testAccessSecretVersion() throws IOException {
    SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION.getName());
    AccessSecretVersion.accessSecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion());
    assertThat(stdOut.toString()).contains("my super secret data");
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) Test(org.junit.Test)

Example 37 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project java-docs-samples by GoogleCloudPlatform.

the class SnippetsIT method testDisableSecretVersion.

@Test
public void testDisableSecretVersion() throws IOException {
    SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION_TO_DISABLE.getName());
    DisableSecretVersion.disableSecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion());
    assertThat(stdOut.toString()).contains("Disabled secret version");
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) Test(org.junit.Test)

Example 38 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project java-docs-samples by GoogleCloudPlatform.

the class SnippetsIT method testDestroySecretVersion.

@Test
public void testDestroySecretVersion() throws IOException {
    SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION_TO_DESTROY.getName());
    DestroySecretVersion.destroySecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion());
    assertThat(stdOut.toString()).contains("Destroyed secret version");
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) Test(org.junit.Test)

Example 39 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project java-docs-samples by GoogleCloudPlatform.

the class AccessSecretVersion method accessSecretVersion.

// Access the payload for the given secret version if one exists. The version
// can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
public static void accessSecretVersion(String projectId, String secretId, String versionId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
        // Access the secret version.
        AccessSecretVersionResponse response = client.accessSecretVersion(secretVersionName);
        // Verify checksum. The used library is available in Java 9+.
        // If using Java 8, you may use the following:
        // https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/files/Crc32c
        byte[] data = response.getPayload().getData().toByteArray();
        Checksum checksum = new CRC32C();
        checksum.update(data, 0, data.length);
        if (response.getPayload().getDataCrc32C() != checksum.getValue()) {
            System.out.printf("Data corruption detected.");
            return;
        }
        // Print the secret payload.
        // 
        // WARNING: Do not print the secret in a production environment - this
        // snippet is showing how to access the secret material.
        String payload = response.getPayload().getData().toStringUtf8();
        System.out.printf("Plaintext: %s\n", payload);
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) Checksum(java.util.zip.Checksum) CRC32C(java.util.zip.CRC32C) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)

Example 40 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project java-docs-samples by GoogleCloudPlatform.

the class DestroySecretVersion method destroySecretVersion.

// Destroy an existing secret version.
public static void destroySecretVersion(String projectId, String secretId, String versionId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name from the version.
        SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
        // Destroy the secret version.
        SecretVersion version = client.destroySecretVersion(secretVersionName);
        System.out.printf("Destroyed secret version %s\n", version.getName());
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Aggregations

SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)63 Test (org.junit.Test)19 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)15 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)13 Test (org.junit.jupiter.api.Test)12 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)7 ByteString (com.google.protobuf.ByteString)3 ApiException (com.google.api.gax.rpc.ApiException)1 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1.AccessSecretVersionRequest)1 DestroySecretVersionRequest (com.google.cloud.secretmanager.v1.DestroySecretVersionRequest)1 DisableSecretVersionRequest (com.google.cloud.secretmanager.v1.DisableSecretVersionRequest)1 EnableSecretVersionRequest (com.google.cloud.secretmanager.v1.EnableSecretVersionRequest)1 SecretName (com.google.cloud.secretmanager.v1.SecretName)1 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)1 IOException (java.io.IOException)1 CRC32C (java.util.zip.CRC32C)1 Checksum (java.util.zip.Checksum)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1