Search in sources :

Example 1 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.SecretVersion in project spring-cloud-config by spring-cloud.

the class GoogleSecretManagerV1AccessStrategy method getSecretValue.

@Override
public String getSecretValue(Secret secret, Comparator<SecretVersion> comparator) {
    String result = null;
    List<SecretVersion> versions = getSecretVersions(secret);
    SecretVersion winner = null;
    for (SecretVersion secretVersion : versions) {
        if ((secretVersion.getState().getNumber() == SecretVersion.State.ENABLED_VALUE) && comparator.compare(secretVersion, winner) > 0) {
            winner = secretVersion;
        }
    }
    if (winner != null) {
        SecretVersionName name = SecretVersionName.parse(winner.getName());
        // Access the secret version.
        AccessSecretVersionRequest request = AccessSecretVersionRequest.newBuilder().setName(name.toString()).build();
        AccessSecretVersionResponse response = client.accessSecretVersion(request);
        result = response.getPayload().getData().toStringUtf8();
    }
    return result;
}
Also used : AccessSecretVersionRequest(com.google.cloud.secretmanager.v1.AccessSecretVersionRequest) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)

Example 2 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.SecretVersion in project spring-cloud-config by spring-cloud.

the class GoogleSecretManagerV1AccessStrategy method getSecretVersions.

private List<SecretVersion> getSecretVersions(Secret secret) {
    SecretName parent = SecretName.parse(secret.getName());
    // Create the request.
    ListSecretVersionsRequest listVersionRequest = ListSecretVersionsRequest.newBuilder().setParent(parent.toString()).build();
    // Get all versions.
    SecretManagerServiceClient.ListSecretVersionsPagedResponse pagedListVersionResponse = client.listSecretVersions(listVersionRequest);
    List<SecretVersion> result = new ArrayList<SecretVersion>();
    pagedListVersionResponse.iterateAll().forEach(result::add);
    return result;
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) ArrayList(java.util.ArrayList) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) ListSecretVersionsRequest(com.google.cloud.secretmanager.v1.ListSecretVersionsRequest)

Example 3 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.SecretVersion in project java-docs-samples by GoogleCloudPlatform.

the class AddSecretVersion method addSecretVersion.

// Add a new version to the existing secret.
public static 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 4 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.SecretVersion in project java-docs-samples by GoogleCloudPlatform.

the class DisableSecretVersion method disableSecretVersion.

// Disable an existing secret version.
public static void disableSecretVersion(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);
        // Disable the secret version.
        SecretVersion version = client.disableSecretVersion(secretVersionName);
        System.out.printf("Disabled 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 5 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.SecretVersion in project java-docs-samples by GoogleCloudPlatform.

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)

Aggregations

SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)22 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)19 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)13 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)7 Test (org.junit.Test)7 SecretName (com.google.cloud.secretmanager.v1.SecretName)5 ByteString (com.google.protobuf.ByteString)5 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)4 SecretVersion (com.google.cloud.secretmanager.v1beta1.SecretVersion)4 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1.AccessSecretVersionRequest)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.v1beta1.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 AddSecretVersionRequest (com.google.cloud.secretmanager.v1beta1.AddSecretVersionRequest)1