Search in sources :

Example 91 with Secret

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

the class GetSecret method getSecret.

// Get an existing secret.
public static void getSecret(String projectId, String secretId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Create the secret.
        Secret secret = client.getSecret(secretName);
        // Get the replication policy.
        String replication = "";
        if (secret.getReplication().getAutomatic() != null) {
            replication = "AUTOMATIC";
        } else if (secret.getReplication().getUserManaged() != null) {
            replication = "MANAGED";
        } else {
            throw new IllegalStateException("Unknown replication type");
        }
        System.out.printf("Secret %s, replication %s\n", secret.getName(), replication);
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Secret(com.google.cloud.secretmanager.v1.Secret) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 92 with Secret

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

the class GetSecretVersion method getSecretVersion.

// Get an existing secret version.
public static void getSecretVersion(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.getSecretVersion(secretVersionName);
        System.out.printf("Secret version %s, state %s\n", version.getName(), version.getState());
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 93 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project spring-cloud-config by spring-cloud.

the class GoogleSecretManagerEnvironmentRepository method getSecrets.

/**
 * @param application the application name
 * @param profile the profile name
 * @return the properties to add into the environment
 */
private Map<?, ?> getSecrets(String application, String profile) {
    Map<String, String> result = new HashMap<>();
    String prefix = configProvider.getValue(HttpHeaderGoogleConfigProvider.PREFIX_HEADER, false);
    for (Secret secret : accessStrategy.getSecrets()) {
        if (secret.getLabelsOrDefault(applicationLabel, "application").equalsIgnoreCase(application) && secret.getLabelsOrDefault(profileLabel, "profile").equalsIgnoreCase(profile)) {
            result.put(accessStrategy.getSecretName(secret), accessStrategy.getSecretValue(secret, new GoogleSecretComparatorByVersion()));
        } else if (StringUtils.isNotBlank(prefix) && accessStrategy.getSecretName(secret).startsWith(prefix)) {
            result.put(StringUtils.removeStart(accessStrategy.getSecretName(secret), prefix), accessStrategy.getSecretValue(secret, new GoogleSecretComparatorByVersion()));
        }
    }
    return result;
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) GoogleSecretComparatorByVersion(org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretComparatorByVersion) HashMap(java.util.HashMap)

Example 94 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project micronaut-gcp by micronaut-projects.

the class GoogleClientExample method onStartup.

@EventListener
public void onStartup(StartupEvent event) {
    AccessSecretVersionResponse response = client.accessSecretVersion(AccessSecretVersionRequest.newBuilder().setName(SecretVersionName.of("my-cloud-project", "secretName", "latest").toString()).build());
    String secret = response.getPayload().getData().toStringUtf8();
}
Also used : AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse) EventListener(io.micronaut.runtime.event.annotation.EventListener)

Example 95 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project spring-cloud-gcp by GoogleCloudPlatform.

the class SecretManagerPropertyUtilsTests method testShortProperty_projectSecretIdVersion.

@Test
void testShortProperty_projectSecretIdVersion() {
    String property = "sm://my-project/the-secret/2";
    SecretVersionName secretIdentifier = SecretManagerPropertyUtils.getSecretVersionName(property, DEFAULT_PROJECT_ID_PROVIDER);
    assertThat(secretIdentifier.getProject()).isEqualTo("my-project");
    assertThat(secretIdentifier.getSecret()).isEqualTo("the-secret");
    assertThat(secretIdentifier.getSecretVersion()).isEqualTo("2");
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) Test(org.junit.jupiter.api.Test)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)52 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)45 SecretName (com.google.cloud.secretmanager.v1.SecretName)44 Test (org.junit.Test)42 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)22 Secret (com.google.cloud.secretmanager.v1.Secret)21 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)16 ByteString (com.google.protobuf.ByteString)13 Test (org.junit.jupiter.api.Test)13 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)10 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)7 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)6 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)6 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)4 DeleteSecretRequest (com.google.cloud.secretmanager.v1.DeleteSecretRequest)4 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)4 Binding (com.google.iam.v1.Binding)4 Policy (com.google.iam.v1.Policy)4 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)3 Secret (com.google.cloud.secretmanager.v1beta1.Secret)3