Search in sources :

Example 56 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project java-secretmanager by googleapis.

the class DisableSecretVersion method disableSecretVersion.

// Disable an existing secret version.
public void disableSecretVersion(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);
        // Create the secret.
        SecretVersion version = client.disableSecretVersion(secretVersionName);
        System.out.printf("Disabled 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)

Example 57 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project wrongsecrets by commjoen.

the class Challenge11 method getGCPChallenge11Value.

private String getGCPChallenge11Value() {
    if (isGCP()) {
        log.info("Getting credentials from GCP");
        // Based on https://cloud.google.com/secret-manager/docs/reference/libraries
        try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
            log.info("Fetching secret form Google Secret Manager...");
            SecretVersionName secretVersionName = SecretVersionName.of(projectId, "wrongsecret-3", "latest");
            AccessSecretVersionResponse response = client.accessSecretVersion(secretVersionName);
            return response.getPayload().getData().toStringUtf8();
        } catch (ApiException e) {
            log.error("Exception getting secret: ", e);
        } catch (IOException e) {
            log.error("Could not get the web identity token, due to ", e);
        }
    } else {
        log.info("Skipping credentials from GCP");
    }
    return gcpDefaultValue;
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) IOException(java.io.IOException) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse) ApiException(com.google.api.gax.rpc.ApiException)

Example 58 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project quarkus-google-cloud-services by quarkiverse.

the class SecretManagerConfigSource method getValue.

@Override
public String getValue(String propertyName) {
    SecretVersionName secretVersionName = SecretManagerConfigUtils.getSecretVersionName(propertyName, defaultProject);
    if (secretVersionName == null) {
        // The propertyName is not in the form "${sm//...}" so return null.
        return null;
    }
    AccessSecretVersionResponse response = clientProvider.get().accessSecretVersion(secretVersionName);
    return response.getPayload().getData().toStringUtf8();
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)

Example 59 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project quarkus-google-cloud-services by quarkiverse.

the class SecretManagerConfigUtilsTest method testLongProperty_projectSecretVersion.

@Test
public void testLongProperty_projectSecretVersion() {
    String property = "sm//projects/my-project/secrets/the-secret/versions/3";
    SecretVersionName secretIdentifier = SecretManagerConfigUtils.getSecretVersionName(property, DEFAULT_PROJECT);
    assertThat(secretIdentifier.getProject()).isEqualTo("my-project");
    assertThat(secretIdentifier.getSecret()).isEqualTo("the-secret");
    assertThat(secretIdentifier.getSecretVersion()).isEqualTo("3");
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) Test(org.junit.jupiter.api.Test)

Example 60 with SecretVersionName

use of com.google.cloud.secretmanager.v1.SecretVersionName in project quarkus-google-cloud-services by quarkiverse.

the class SecretManagerConfigUtilsTest method testShortProperty_secretId.

@Test
public void testShortProperty_secretId() {
    String property = "sm//the-secret";
    SecretVersionName secretIdentifier = SecretManagerConfigUtils.getSecretVersionName(property, DEFAULT_PROJECT);
    assertThat(secretIdentifier.getProject()).isEqualTo("defaultProject");
    assertThat(secretIdentifier.getSecret()).isEqualTo("the-secret");
    assertThat(secretIdentifier.getSecretVersion()).isEqualTo("latest");
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) Test(org.junit.jupiter.api.Test)

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