Search in sources :

Example 21 with Secret

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

the class UpdateSecret method updateSecret.

// Update an existing secret.
public static void updateSecret(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()) {
        // Build the name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Build the updated secret.
        Secret secret = Secret.newBuilder().setName(secretName.toString()).putLabels("secretmanager", "rocks").build();
        // Build the field mask.
        FieldMask fieldMask = FieldMaskUtil.fromString("labels");
        // Update the secret.
        Secret updatedSecret = client.updateSecret(secret, fieldMask);
        System.out.printf("Updated secret %s\n", updatedSecret.getName());
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Secret(com.google.cloud.secretmanager.v1.Secret) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) FieldMask(com.google.protobuf.FieldMask)

Example 22 with Secret

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

the class UpdateSecretWithEtag method updateSecret.

// Update an existing secret.
public static void updateSecret(String projectId, String secretId, String etag) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Build the updated secret.
        Secret secret = Secret.newBuilder().setName(secretName.toString()).setEtag(etag).putLabels("secretmanager", "rocks").build();
        // Build the field mask.
        FieldMask fieldMask = FieldMaskUtil.fromString("labels");
        // Update the secret.
        Secret updatedSecret = client.updateSecret(secret, fieldMask);
        System.out.printf("Updated secret %s\n", updatedSecret.getName());
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Secret(com.google.cloud.secretmanager.v1.Secret) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) FieldMask(com.google.protobuf.FieldMask)

Example 23 with Secret

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

the class CreateSecret method createSecret.

// Add a new version to the existing secret.
public static void createSecret(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()) {
        // Build the parent name from the project.
        ProjectName projectName = ProjectName.of(projectId);
        // Build the secret to create.
        Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
        // Create the secret.
        Secret createdSecret = client.createSecret(projectName, secretId, secret);
        System.out.printf("Created secret %s\n", createdSecret.getName());
    }
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 24 with Secret

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

the class GetSecret method getSecret.

// Get an existing secret.
public static void getSecret(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()) {
        // Build the name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Create the secret.
        Secret secret = client.getSecret(secretName);
        // Get the replication policy.
        String replication = "";
        if (secret.getReplication().getAutomatic() != null) {
            replication = "AUTOMATIC";
        } else if (secret.getReplication().getUserManaged() != null) {
            replication = "MANAGED";
        } else {
            throw new IllegalStateException("Unknown replication type");
        }
        System.out.printf("Secret %s, replication %s\n", secret.getName(), replication);
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Secret(com.google.cloud.secretmanager.v1.Secret) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 25 with Secret

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

the class GoogleSecretManagerEnvironmentRepository method getSecrets.

/**
 * @param application the application name
 * @param profile the profile name
 * @return the properties to add into the environment
 */
private Map<?, ?> getSecrets(String application, String profile) {
    Map<String, String> result = new HashMap<>();
    String prefix = configProvider.getValue(HttpHeaderGoogleConfigProvider.PREFIX_HEADER, false);
    for (Secret secret : accessStrategy.getSecrets()) {
        if (secret.getLabelsOrDefault(applicationLabel, "application").equalsIgnoreCase(application) && secret.getLabelsOrDefault(profileLabel, "profile").equalsIgnoreCase(profile)) {
            result.put(accessStrategy.getSecretName(secret), accessStrategy.getSecretValue(secret, new GoogleSecretComparatorByVersion()));
        } else if (StringUtils.isNotBlank(prefix) && accessStrategy.getSecretName(secret).startsWith(prefix)) {
            result.put(StringUtils.removeStart(accessStrategy.getSecretName(secret), prefix), accessStrategy.getSecretValue(secret, new GoogleSecretComparatorByVersion()));
        }
    }
    return result;
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) GoogleSecretComparatorByVersion(org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretComparatorByVersion) HashMap(java.util.HashMap)

Aggregations

Secret (com.google.cloud.secretmanager.v1.Secret)21 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)11 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)11 Test (org.junit.Test)6 SecretName (com.google.cloud.secretmanager.v1.SecretName)5 ByteString (com.google.protobuf.ByteString)5 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)4 Secret (com.google.cloud.secretmanager.v1beta1.Secret)3 AbstractMessage (com.google.protobuf.AbstractMessage)3 FieldMask (com.google.protobuf.FieldMask)3 Secret (com.marcnuri.yakc.model.io.k8s.api.core.v1.Secret)3 Test (org.junit.jupiter.api.Test)3 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)2 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)2 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)2 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)2 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)2 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient.ListSecretsPagedResponse)2 Configuration (com.marcnuri.yakc.config.Configuration)2 Node (com.marcnuri.yakc.model.io.k8s.api.core.v1.Node)2