Search in sources :

Example 41 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class DestroySecretVersion method destroySecretVersion.

// Destroy an existing secret version.
public void destroySecretVersion(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.destroySecretVersion(secretVersionName);
        System.out.printf("Destroyed secret version %s\n", version.getName());
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 42 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class DeleteSecret method deleteSecret.

// Delete an existing secret with the given name.
public void deleteSecret(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 secret name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Create the secret.
        client.deleteSecret(secretName);
        System.out.printf("Deleted secret %s\n", secretId);
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 43 with Secret

use of com.google.cloud.secretmanager.v1.Secret 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());
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 44 with Secret

use of com.google.cloud.secretmanager.v1.Secret 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);
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 45 with Secret

use of com.google.cloud.secretmanager.v1.Secret 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());
        });
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) ListSecretVersionsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)51 SecretName (com.google.cloud.secretmanager.v1.SecretName)44 Test (org.junit.Test)42 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)38 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)22 Secret (com.google.cloud.secretmanager.v1.Secret)21 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)16 ByteString (com.google.protobuf.ByteString)13 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)9 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)7 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)6 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)6 Test (org.junit.jupiter.api.Test)5 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)4 DeleteSecretRequest (com.google.cloud.secretmanager.v1.DeleteSecretRequest)4 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)4 Binding (com.google.iam.v1.Binding)4 Policy (com.google.iam.v1.Policy)4 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)3 Secret (com.google.cloud.secretmanager.v1beta1.Secret)3