Search in sources :

Example 6 with Secret

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

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

use of com.google.cloud.secretmanager.v1.Secret in project spring-cloud-config by spring-cloud.

the class GoogleSecretManagerV1AccessStrategy method getSecrets.

@Override
public List<Secret> getSecrets() {
    // Build the parent name.
    ProjectName project = ProjectName.of(getProjectId());
    // Create the request.
    ListSecretsRequest listSecretRequest = ListSecretsRequest.newBuilder().setParent(project.toString()).build();
    // Get all secrets.
    SecretManagerServiceClient.ListSecretsPagedResponse pagedListSecretResponse = client.listSecrets(listSecretRequest);
    List<Secret> result = new ArrayList<Secret>();
    pagedListSecretResponse.iterateAll().forEach(result::add);
    // List all secrets.
    return result;
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) ListSecretsRequest(com.google.cloud.secretmanager.v1.ListSecretsRequest) ArrayList(java.util.ArrayList) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 9 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.

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 10 with Secret

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

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)49 SecretName (com.google.cloud.secretmanager.v1.SecretName)43 Test (org.junit.Test)42 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)37 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)20 Secret (com.google.cloud.secretmanager.v1.Secret)19 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)14 ByteString (com.google.protobuf.ByteString)10 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)7 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)6 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)6 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)5 Test (org.junit.jupiter.api.Test)5 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)4 DeleteSecretRequest (com.google.cloud.secretmanager.v1.DeleteSecretRequest)4 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)4 Binding (com.google.iam.v1.Binding)4 Policy (com.google.iam.v1.Policy)4 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)3 Secret (com.google.cloud.secretmanager.v1beta1.Secret)3