Search in sources :

Example 61 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project native-image-support-java by GoogleCloudPlatform.

the class SecretManagerSampleApplication 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 62 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project native-image-support-java by GoogleCloudPlatform.

the class SecretManagerSampleApplication method addSecretVersion.

static SecretVersion addSecretVersion(SecretManagerServiceClient client, String projectId, String secretId) {
    SecretName secretName = SecretName.of(projectId, secretId);
    SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("Hello World")).build();
    SecretVersion version = client.addSecretVersion(secretName, payload);
    System.out.println("Added Secret Version: " + version.getName());
    return version;
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretPayload(com.google.cloud.secretmanager.v1.SecretPayload)

Example 63 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project wrongsecrets by commjoen.

the class Challenge11 method getGCPChallenge11Value.

private String getGCPChallenge11Value() {
    if (isGCP()) {
        log.info("Getting credentials from GCP");
        // Based on https://cloud.google.com/secret-manager/docs/reference/libraries
        try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
            log.info("Fetching secret form Google Secret Manager...");
            SecretVersionName secretVersionName = SecretVersionName.of(projectId, "wrongsecret-3", "latest");
            AccessSecretVersionResponse response = client.accessSecretVersion(secretVersionName);
            return response.getPayload().getData().toStringUtf8();
        } catch (ApiException e) {
            log.error("Exception getting secret: ", e);
        } catch (IOException e) {
            log.error("Could not get the web identity token, due to ", e);
        }
    } else {
        log.info("Skipping credentials from GCP");
    }
    return gcpDefaultValue;
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) IOException(java.io.IOException) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse) ApiException(com.google.api.gax.rpc.ApiException)

Example 64 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project quarkus-google-cloud-services by quarkiverse.

the class ApplicationProducerMock method secretManagerServiceClient.

@Produces
@Singleton
@Default
public SecretManagerServiceClient secretManagerServiceClient() {
    SecretManagerServiceClient client = Mockito.mock(SecretManagerServiceClient.class);
    when(client.accessSecretVersion(any(SecretVersionName.class))).thenReturn(AccessSecretVersionResponse.newBuilder().setName("test-secret").setPayload(SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("hello"))).build());
    return client;
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) Produces(javax.enterprise.inject.Produces) Singleton(javax.inject.Singleton) Default(javax.enterprise.inject.Default)

Example 65 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project quarkus-google-cloud-services by quarkiverse.

the class SecretManagerProducer method secretManagerClient.

@Produces
@Singleton
@Default
@Unremovable
public SecretManagerServiceClient secretManagerClient() throws IOException {
    GcpBootstrapConfiguration gcpConfiguration = gcpConfigHolder.getBootstrapConfig();
    SecretManagerServiceSettings.Builder builder = SecretManagerServiceSettings.newBuilder().setCredentialsProvider(() -> googleCredentials);
    builder.setQuotaProjectId(gcpConfiguration.projectId.orElse(null));
    return SecretManagerServiceClient.create(builder.build());
}
Also used : SecretManagerServiceSettings(com.google.cloud.secretmanager.v1.SecretManagerServiceSettings) GcpBootstrapConfiguration(io.quarkiverse.googlecloudservices.common.GcpBootstrapConfiguration) Produces(javax.enterprise.inject.Produces) Singleton(javax.inject.Singleton) Unremovable(io.quarkus.arc.Unremovable) Default(javax.enterprise.inject.Default)

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