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);
}
}
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());
}
}
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;
}
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();
}
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");
}
Aggregations