use of com.google.cloud.secretmanager.v1.SecretName 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.SecretName 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);
}
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-secretmanager by googleapis.
the class AddSecretVersion method addSecretVersion.
// Add a new version to the existing secret.
public void addSecretVersion(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()) {
SecretName secretName = SecretName.of(projectId, secretId);
// Create the secret payload.
SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("my super secret data")).build();
// Add the secret version.
SecretVersion version = client.addSecretVersion(secretName, payload);
System.out.printf("Added secret version %s\n", version.getName());
}
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-secretmanager by googleapis.
the class UpdateSecret method updateSecret.
// Update an existing secret.
public void updateSecret(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);
// Build the updated secret.
Secret secret = Secret.newBuilder().setName(secretName.toString()).putLabels("secretmanager", "rocks").build();
// Build the field mask.
FieldMask fieldMask = FieldMaskUtil.fromString("labels");
// Create the secret.
Secret updatedSecret = client.updateSecret(secret, fieldMask);
System.out.printf("Updated secret %s\n", updatedSecret.getName());
}
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-secretmanager by googleapis.
the class SnippetsIT method testGetSecret.
@Test
public void testGetSecret() throws IOException {
SecretName name = SecretName.parse(TEST_SECRET.getName());
new GetSecret().getSecret(name.getProject(), name.getSecret());
assertThat(stdOut.toString()).contains("Secret");
assertThat(stdOut.toString()).contains("replication AUTOMATIC");
}
Aggregations