use of com.google.cloud.secretmanager.v1.Secret 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);
}
}
use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.
the class QuickstartIT method afterAll.
@AfterClass
public static void afterAll() throws Exception {
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Delete the secret created by quickstart
SecretName name = SecretName.of(PROJECT_ID, SECRET_ID);
DeleteSecretRequest deleteRequest = DeleteSecretRequest.newBuilder().setName(name.toString()).build();
client.deleteSecret(deleteRequest);
}
}
use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.
the class GetSecretVersion method getSecretVersion.
// Get an existing secret version.
public 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 java-secretmanager by googleapis.
the class IamRevokeAccess method iamRevokeAccess.
// Revoke a member access to a particular secret.
public void iamRevokeAccess(String projectId, String secretId, String member) 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.
SecretName secretName = SecretName.of(projectId, secretId);
// Request the current IAM policy.
Policy policy = client.getIamPolicy(GetIamPolicyRequest.newBuilder().setResource(secretName.toString()).build());
// Search through bindings and remove matches.
String roleToFind = "roles/secretmanager.secretAccessor";
for (Binding binding : policy.getBindingsList()) {
if (binding.getRole() == roleToFind && binding.getMembersList().contains(member)) {
binding.getMembersList().remove(member);
}
}
// Save the updated IAM policy.
client.setIamPolicy(SetIamPolicyRequest.newBuilder().setResource(secretName.toString()).setPolicy(policy).build());
System.out.printf("Updated IAM policy for %s\n", secretId);
}
}
use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.
the class NativeImageSecretManagerSampleIT method afterAll.
@AfterClass
public static void afterAll() throws Exception {
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Delete the secret created by quickstart
SecretName name = SecretName.of(PROJECT_ID, NATIVE_TEST_SECRET_ID);
DeleteSecretRequest deleteRequest = DeleteSecretRequest.newBuilder().setName(name.toString()).build();
client.deleteSecret(deleteRequest);
}
}
Aggregations