use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project java-secretmanager by googleapis.
the class SnippetsIT method addSecretVersion.
private static SecretVersion addSecretVersion(Secret secret) throws IOException {
SecretName parent = SecretName.parse(secret.getName());
AddSecretVersionRequest request = AddSecretVersionRequest.newBuilder().setParent(parent.toString()).setPayload(SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("my super secret data")).build()).build();
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
return client.addSecretVersion(request);
}
}
use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project java-secretmanager by googleapis.
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 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);
// 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);
}
}
use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient 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());
}
}
use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project java-secretmanager by googleapis.
the class GetSecret method getSecret.
// Get an existing secret.
public 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.SecretManagerServiceClient in project native-image-support-java by GoogleCloudPlatform.
the class SecretManagerSampleApplication method main.
/**
* Runs the Secret Manager sample application.
*/
public static void main(String[] args) throws IOException {
String projectId = ServiceOptions.getDefaultProjectId();
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
if (!hasSecret(client, projectId, GRAALVM_TEST_SECRET_ID)) {
createSecret(client, projectId, GRAALVM_TEST_SECRET_ID);
} else {
System.out.println("Project already has secret: " + GRAALVM_TEST_SECRET_ID);
}
SecretVersion version = addSecretVersion(client, projectId, GRAALVM_TEST_SECRET_ID);
printSecretVersion(client, version);
}
}
Aggregations