Search in sources :

Example 21 with SecretManagerServiceClient

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

the class NativeImageSecretManagerSampleIT method testCreateAndPrintSecret.

@Test
public void testCreateAndPrintSecret() throws IOException {
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        NativeImageSecretManagerSample.createSecret(client, PROJECT_ID, NATIVE_TEST_SECRET_ID);
        SecretVersion version = NativeImageSecretManagerSample.addSecretVersion(client, PROJECT_ID, NATIVE_TEST_SECRET_ID);
        NativeImageSecretManagerSample.printSecretVersion(client, version);
        assertThat(bout.toString()).contains("Reading secret value: Hello World");
    }
}
Also used : SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) Test(org.junit.Test)

Example 22 with SecretManagerServiceClient

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

Example 23 with SecretManagerServiceClient

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

the class CreateSecret method createSecret.

// Add a new version to the existing secret.
public void createSecret(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 from the project.
        ProjectName projectName = ProjectName.of(projectId);
        // Build the secret to create.
        Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
        // Create the secret.
        Secret createdSecret = client.createSecret(projectName, secretId, secret);
        System.out.printf("Created secret %s\n", createdSecret.getName());
    }
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 24 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient 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 25 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient 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)

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