use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project java-secretmanager by googleapis.
the class EnableSecretVersion method enableSecretVersion.
// Enable an existing secret version.
public void enableSecretVersion(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.enableSecretVersion(secretVersionName);
System.out.printf("Enabled secret version %s\n", version.getName());
}
}
use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient 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.SecretManagerServiceClient in project java-secretmanager by googleapis.
the class IamGrantAccess method iamGrantAccess.
// Grant a member access to a particular secret.
public void iamGrantAccess(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 currentPolicy = client.getIamPolicy(GetIamPolicyRequest.newBuilder().setResource(secretName.toString()).build());
// Build the new binding.
Binding binding = Binding.newBuilder().setRole("roles/secretmanager.secretAccessor").addMembers(member).build();
// Create a new IAM policy from the current policy, adding the binding.
Policy newPolicy = Policy.newBuilder().mergeFrom(currentPolicy).addBindings(binding).build();
// Save the updated IAM policy.
client.setIamPolicy(SetIamPolicyRequest.newBuilder().setResource(secretName.toString()).setPolicy(newPolicy).build());
System.out.printf("Updated IAM policy for %s\n", secretId);
}
}
use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient 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.SecretManagerServiceClient in project java-secretmanager by googleapis.
the class ListSecretVersions method listSecretVersions.
// List all secret versions for a secret.
public void listSecretVersions(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 parent name.
SecretName projectName = SecretName.of(projectId, secretId);
// Get all versions.
ListSecretVersionsPagedResponse pagedResponse = client.listSecretVersions(projectName);
// List all versions and their state.
pagedResponse.iterateAll().forEach(version -> {
System.out.printf("Secret version %s, %s\n", version.getName(), version.getState());
});
}
}
Aggregations