Search in sources :

Example 31 with SecretManagerServiceClient

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

the class ListSecrets method listSecrets.

// List all secrets for a project
public void listSecrets(String projectId) 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.
        ProjectName projectName = ProjectName.of(projectId);
        // Get all secrets.
        ListSecretsPagedResponse pagedResponse = client.listSecrets(projectName);
        // List all secrets.
        pagedResponse.iterateAll().forEach(secret -> {
            System.out.printf("Secret %s\n", secret.getName());
        });
    }
}
Also used : ProjectName(com.google.cloud.secretmanager.v1.ProjectName) ListSecretsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 32 with SecretManagerServiceClient

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

Example 33 with SecretManagerServiceClient

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

Example 34 with SecretManagerServiceClient

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

Example 35 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project nomulus by google.

the class SecretManagerModule method provideSecretManagerClient.

@Provides
@Singleton
static SecretManagerClient provideSecretManagerClient(SecretManagerServiceSettings serviceSettings, @Config("projectId") String project, Retrier retrier) {
    try {
        SecretManagerServiceClient stub = SecretManagerServiceClient.create(serviceSettings);
        Runtime.getRuntime().addShutdownHook(new Thread(stub::close));
        return new SecretManagerClientImpl(project, stub, retrier);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) IOException(java.io.IOException) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)56 SecretName (com.google.cloud.secretmanager.v1.SecretName)24 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)21 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)16 Secret (com.google.cloud.secretmanager.v1.Secret)14 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)13 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)10 ByteString (com.google.protobuf.ByteString)9 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)7 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)5 Test (org.junit.Test)5 DeleteSecretRequest (com.google.cloud.secretmanager.v1.DeleteSecretRequest)4 Binding (com.google.iam.v1.Binding)4 Policy (com.google.iam.v1.Policy)4 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)3 FieldMask (com.google.protobuf.FieldMask)3 Singleton (javax.inject.Singleton)3 AfterClass (org.junit.AfterClass)3 GoogleSecretManagerV1AccessStrategy (org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretManagerV1AccessStrategy)3 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)2