use of com.google.cloud.secretmanager.v1.AccessSecretVersionResponse in project spring-cloud-config by spring-cloud.
the class GoogleSecretManagerV1AccessStrategy method getSecretValue.
@Override
public String getSecretValue(Secret secret, Comparator<SecretVersion> comparator) {
String result = null;
List<SecretVersion> versions = getSecretVersions(secret);
SecretVersion winner = null;
for (SecretVersion secretVersion : versions) {
if ((secretVersion.getState().getNumber() == SecretVersion.State.ENABLED_VALUE) && comparator.compare(secretVersion, winner) > 0) {
winner = secretVersion;
}
}
if (winner != null) {
SecretVersionName name = SecretVersionName.parse(winner.getName());
// Access the secret version.
AccessSecretVersionRequest request = AccessSecretVersionRequest.newBuilder().setName(name.toString()).build();
AccessSecretVersionResponse response = client.accessSecretVersion(request);
result = response.getPayload().getData().toStringUtf8();
}
return result;
}
use of com.google.cloud.secretmanager.v1.AccessSecretVersionResponse in project java-docs-samples by GoogleCloudPlatform.
the class Quickstart method quickstart.
public void quickstart(String projectId, String secretId) throws Exception {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Build the parent name from the project.
ProjectName projectName = ProjectName.of(projectId);
// Create the parent secret.
Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
Secret createdSecret = client.createSecret(projectName, secretId, secret);
// Add a secret version.
SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("hello world!")).build();
SecretVersion addedVersion = client.addSecretVersion(createdSecret.getName(), payload);
// Access the secret version.
AccessSecretVersionResponse response = client.accessSecretVersion(addedVersion.getName());
// 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 data = response.getPayload().getData().toStringUtf8();
System.out.printf("Plaintext: %s\n", data);
}
}
use of com.google.cloud.secretmanager.v1.AccessSecretVersionResponse in project micronaut-gcp by micronaut-projects.
the class LocalFileResourceLoader method main.
public static void main(String[] args) {
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
try {
String contents = LocalFileResourceLoader.loadSecret("first-gcp-project", "application");
AccessSecretVersionResponse response = AccessSecretVersionResponse.newBuilder().setName("foo").setPayload(SecretPayload.newBuilder().setData(ByteString.copyFrom(contents.getBytes())).build()).build();
loader.read("foo", contents.getBytes());
loader.read("foo", response.getPayload().getData().toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.google.cloud.secretmanager.v1.AccessSecretVersionResponse in project java-secretmanager by googleapis.
the class NativeImageSecretManagerSample method printSecretVersion.
static void printSecretVersion(SecretManagerServiceClient client, SecretVersion version) {
AccessSecretVersionResponse response = client.accessSecretVersion(version.getName());
String payload = response.getPayload().getData().toStringUtf8();
System.out.println("Reading secret value: " + payload);
System.out.println("(Note: Don't print secret values in prod!)");
}
use of com.google.cloud.secretmanager.v1.AccessSecretVersionResponse in project java-secretmanager by googleapis.
the class Quickstart method quickstart.
public void quickstart(String projectId, String secretId) throws Exception {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Build the parent name from the project.
ProjectName projectName = ProjectName.of(projectId);
// Create the parent secret.
Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
Secret createdSecret = client.createSecret(projectName, secretId, secret);
// Add a secret version.
SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("hello world!")).build();
SecretVersion addedVersion = client.addSecretVersion(createdSecret.getName(), payload);
// Access the secret version.
AccessSecretVersionResponse response = client.accessSecretVersion(addedVersion.getName());
// 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 data = response.getPayload().getData().toStringUtf8();
System.out.printf("Plaintext: %s\n", data);
}
}
Aggregations