|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.example; |
| 18 | + |
| 19 | +import com.google.cloud.ServiceOptions; |
| 20 | +import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse; |
| 21 | +import com.google.cloud.secretmanager.v1.ProjectName; |
| 22 | +import com.google.cloud.secretmanager.v1.Replication; |
| 23 | +import com.google.cloud.secretmanager.v1.Secret; |
| 24 | +import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; |
| 25 | +import com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse; |
| 26 | +import com.google.cloud.secretmanager.v1.SecretName; |
| 27 | +import com.google.cloud.secretmanager.v1.SecretPayload; |
| 28 | +import com.google.cloud.secretmanager.v1.SecretVersion; |
| 29 | +import com.google.protobuf.ByteString; |
| 30 | +import java.io.IOException; |
| 31 | + |
| 32 | +/** |
| 33 | + * Sample application demonstrating Native Image compatibility with Google Cloud Secret Manager |
| 34 | + * APIs. |
| 35 | + */ |
| 36 | +public class NativeImageSecretManagerSample { |
| 37 | + |
| 38 | + public static void main(String[] args) throws IOException { |
| 39 | + String secretId = "native-secretmanager-test-secret"; |
| 40 | + String projectId = ServiceOptions.getDefaultProjectId(); |
| 41 | + |
| 42 | + try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { |
| 43 | + if (!hasSecret(client, projectId, secretId)) { |
| 44 | + createSecret(client, projectId, secretId); |
| 45 | + } else { |
| 46 | + System.out.println("Project already has secret: " + secretId); |
| 47 | + } |
| 48 | + |
| 49 | + SecretVersion version = addSecretVersion(client, projectId, secretId); |
| 50 | + printSecretVersion(client, version); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + static void createSecret(SecretManagerServiceClient client, String projectId, String secretId) { |
| 55 | + Secret secret = |
| 56 | + Secret.newBuilder() |
| 57 | + .setReplication( |
| 58 | + Replication.newBuilder() |
| 59 | + .setAutomatic(Replication.Automatic.newBuilder().build()) |
| 60 | + .build()) |
| 61 | + .build(); |
| 62 | + ProjectName projectName = ProjectName.of(projectId); |
| 63 | + Secret createdSecret = client.createSecret(projectName, secretId, secret); |
| 64 | + System.out.println("Created secret: " + createdSecret.getName()); |
| 65 | + } |
| 66 | + |
| 67 | + static boolean hasSecret(SecretManagerServiceClient client, String projectId, String secretId) { |
| 68 | + |
| 69 | + ProjectName projectName = ProjectName.of(projectId); |
| 70 | + ListSecretsPagedResponse pagedResponse = client.listSecrets(projectName); |
| 71 | + |
| 72 | + for (Secret secret : pagedResponse.iterateAll()) { |
| 73 | + String otherSecretId = extractSecretId(secret); |
| 74 | + if (secretId.equals(otherSecretId)) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + static SecretVersion addSecretVersion( |
| 83 | + SecretManagerServiceClient client, String projectId, String secretId) { |
| 84 | + |
| 85 | + SecretName secretName = SecretName.of(projectId, secretId); |
| 86 | + SecretPayload payload = |
| 87 | + SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("Hello World")).build(); |
| 88 | + |
| 89 | + SecretVersion version = client.addSecretVersion(secretName, payload); |
| 90 | + System.out.println("Added Secret Version: " + version.getName()); |
| 91 | + return version; |
| 92 | + } |
| 93 | + |
| 94 | + static void printSecretVersion(SecretManagerServiceClient client, SecretVersion version) { |
| 95 | + AccessSecretVersionResponse response = client.accessSecretVersion(version.getName()); |
| 96 | + String payload = response.getPayload().getData().toStringUtf8(); |
| 97 | + System.out.println("Reading secret value: " + payload); |
| 98 | + System.out.println("(Note: Don't print secret values in prod!)"); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns the secret ID from the fully-qualified secret name which has the format: |
| 103 | + * projects/YOUR_PROJECT_ID/secrets/YOUR_SECRET_ID. |
| 104 | + * |
| 105 | + * @param secret {@link Secret} to extract id from. |
| 106 | + * @return a {@link String} representing the secret id |
| 107 | + */ |
| 108 | + private static String extractSecretId(Secret secret) { |
| 109 | + String[] secretNameTokens = secret.getName().split("/"); |
| 110 | + return secretNameTokens[secretNameTokens.length - 1]; |
| 111 | + } |
| 112 | +} |
0 commit comments