use of com.google.cloud.secretmanager.v1.SecretName in project java-docs-samples by GoogleCloudPlatform.
the class DeleteSecretWithEtag method deleteSecret.
// Delete an existing secret with the given name and etag.
public static void deleteSecret(String projectId, String secretId, String etag) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Build the secret name.
SecretName secretName = SecretName.of(projectId, secretId);
// Construct the request.
DeleteSecretRequest request = DeleteSecretRequest.newBuilder().setName(secretName.toString()).setEtag(etag).build();
// Delete the secret.
client.deleteSecret(request);
System.out.printf("Deleted secret %s\n", secretId);
}
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-docs-samples by GoogleCloudPlatform.
the class IamGrantAccess method iamGrantAccess.
// Grant a member access to a particular secret.
public static 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.SecretName in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method testAddSecretVersion.
@Test
public void testAddSecretVersion() throws IOException {
SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName());
AddSecretVersion.addSecretVersion(name.getProject(), name.getSecret());
assertThat(stdOut.toString()).contains("Added secret version");
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-docs-samples by GoogleCloudPlatform.
the class IamRevokeAccess method iamRevokeAccess.
// Revoke a member access to a particular secret.
public static 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.SecretName in project spring-cloud-gcp by GoogleCloudPlatform.
the class SecretManagerTemplate method createNewSecretVersion.
/**
* Create a new version of the secret with the specified payload under a {@link Secret}. Will also
* create the parent secret if it does not already exist.
*/
private void createNewSecretVersion(String secretId, ByteString payload, String projectId) {
if (!secretExists(secretId, projectId)) {
createSecretInternal(secretId, projectId);
}
SecretName name = SecretName.of(projectId, secretId);
AddSecretVersionRequest payloadRequest = AddSecretVersionRequest.newBuilder().setParent(name.toString()).setPayload(SecretPayload.newBuilder().setData(payload)).build();
secretManagerServiceClient.addSecretVersion(payloadRequest);
}
Aggregations