Search in sources :

Example 11 with SecretVersion

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

the class NativeImageSecretManagerSample method main.

/**
 * Runs the Secret Manager sample application.
 */
public static void main(String[] args) throws IOException {
    String projectId = ServiceOptions.getDefaultProjectId();
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        if (!hasSecret(client, projectId, NATIVE_TEST_SECRET_ID)) {
            createSecret(client, projectId, NATIVE_TEST_SECRET_ID);
        } else {
            System.out.println("Project already has secret: " + NATIVE_TEST_SECRET_ID);
        }
        SecretVersion version = addSecretVersion(client, projectId, NATIVE_TEST_SECRET_ID);
        printSecretVersion(client, version);
    }
}
Also used : SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) ByteString(com.google.protobuf.ByteString)

Example 12 with SecretVersion

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

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

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

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

the class GetSecretVersion method getSecretVersion.

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

Aggregations

SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)22 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)21 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)13 SecretName (com.google.cloud.secretmanager.v1.SecretName)7 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)7 Test (org.junit.Test)7 ByteString (com.google.protobuf.ByteString)6 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)5 SecretVersion (com.google.cloud.secretmanager.v1beta1.SecretVersion)4 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1.AccessSecretVersionRequest)2 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)2 ListSecretVersionsRequest (com.google.cloud.secretmanager.v1.ListSecretVersionsRequest)2 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)2 Secret (com.google.cloud.secretmanager.v1.Secret)2 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 ArrayList (java.util.ArrayList)2 DestroySecretVersionRequest (com.google.cloud.secretmanager.v1.DestroySecretVersionRequest)1 DisableSecretVersionRequest (com.google.cloud.secretmanager.v1.DisableSecretVersionRequest)1 EnableSecretVersionRequest (com.google.cloud.secretmanager.v1.EnableSecretVersionRequest)1