Search in sources :

Example 1 with AccessSecretVersionResponse

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

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

Example 3 with AccessSecretVersionResponse

use of com.google.cloud.secretmanager.v1.AccessSecretVersionResponse in project micronaut-gcp by micronaut-projects.

the class LocalFileResourceLoader method main.

public static void main(String[] args) {
    YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    try {
        String contents = LocalFileResourceLoader.loadSecret("first-gcp-project", "application");
        AccessSecretVersionResponse response = AccessSecretVersionResponse.newBuilder().setName("foo").setPayload(SecretPayload.newBuilder().setData(ByteString.copyFrom(contents.getBytes())).build()).build();
        loader.read("foo", contents.getBytes());
        loader.read("foo", response.getPayload().getData().toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : YamlPropertySourceLoader(io.micronaut.context.env.yaml.YamlPropertySourceLoader) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)

Example 4 with AccessSecretVersionResponse

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

the class NativeImageSecretManagerSample method printSecretVersion.

static void printSecretVersion(SecretManagerServiceClient client, SecretVersion version) {
    AccessSecretVersionResponse response = client.accessSecretVersion(version.getName());
    String payload = response.getPayload().getData().toStringUtf8();
    System.out.println("Reading secret value: " + payload);
    System.out.println("(Note: Don't print secret values in prod!)");
}
Also used : ByteString(com.google.protobuf.ByteString) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)

Example 5 with AccessSecretVersionResponse

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

Aggregations

AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)9 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)5 ByteString (com.google.protobuf.ByteString)5 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)4 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)3 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)3 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1.AccessSecretVersionRequest)2 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)2 Secret (com.google.cloud.secretmanager.v1.Secret)2 Test (org.junit.Test)2 ListSecretVersionsRequest (com.google.cloud.secretmanager.v1.ListSecretVersionsRequest)1 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1beta1.AccessSecretVersionRequest)1 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1beta1.AccessSecretVersionResponse)1 YamlPropertySourceLoader (io.micronaut.context.env.yaml.YamlPropertySourceLoader)1 EventListener (io.micronaut.runtime.event.annotation.EventListener)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1 GoogleConfigProvider (org.springframework.cloud.config.server.environment.secretmanager.GoogleConfigProvider)1 GoogleSecretComparatorByVersion (org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretComparatorByVersion)1