Skip to content

Commit 0b04cfd

Browse files
authored
Add secretmanager samples (#1948)
Add initial java samples for Secret Manager (https://cloud.google.com/secret-manager)
1 parent f1f3abe commit 0b04cfd

18 files changed

+1248
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ appengine-generated/
1515
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1616
hs_err_pid*
1717

18+
# jenv
19+
.java-version
20+
1821
# maven
1922
target/
2023
pom.xml.tag

secretmanager/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Google Secret Manager
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=secretmanager/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
Google [Secret Manager](https://cloud.google.com/secret-manager/) provides a
7+
secure and convenient tool for storing API keys, passwords, certificates and
8+
other sensitive data. These sample Java applications demonstrate how to access
9+
the Secret Manager API using the Google Java API Client Libraries.
10+
11+
## Prerequisites
12+
13+
### Enable the API
14+
15+
You must [enable the Secret Manager API](https://console.cloud.google.com/flows/enableapi?apiid=secretmanager.googleapis.com) for your project in order to use these samples
16+
17+
### Set Environment Variables
18+
19+
You must set your project ID in order to run the tests
20+
21+
```text
22+
$ export GOOGLE_CLOUD_PROJECT=<your-project-id-here>
23+
```
24+
25+
### Grant Permissions
26+
27+
You must ensure that the [user account or service account](https://cloud.google.com/iam/docs/service-accounts#differences_between_a_service_account_and_a_user_account) you used to authorize your gcloud session has the proper permissions to edit Secret Manager resources for your project. In the Cloud Console under IAM, add the following roles to the project whose service account you're using to test:
28+
29+
* Secret Manager Admin (`roles/secretmanager.admin`)
30+
* Secret Manager Secret Accessor (`roles/secretmanager.secretAccessor`)
31+
32+
More information can be found in the [Secret Manager Docs](https://cloud.google.com/secret-manager/docs/access-control)

secretmanager/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!--
2+
Copyright 2020 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+
http://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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<groupId>com.google.cloud.secretmanager.samples</groupId>
21+
<artifactId>secretmanager-samples</artifactId>
22+
<packaging>jar</packaging>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not affect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.0.11</version>
32+
</parent>
33+
34+
<properties>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
<maven.compiler.target>1.8</maven.compiler.target>
37+
<maven.compiler.source>1.8</maven.compiler.source>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>com.google.cloud</groupId>
43+
<artifactId>google-cloud-secretmanager</artifactId>
44+
<version>0.2.0</version>
45+
</dependency>
46+
47+
<!-- test dependencies -->
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>4.13</version>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.truth</groupId>
56+
<artifactId>truth</artifactId>
57+
<version>1.0</version>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2020 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+
* http://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.example;
18+
19+
// [START secretmanager_access_secret_version]
20+
import com.google.cloud.secretmanager.v1beta1.AccessSecretVersionRequest;
21+
import com.google.cloud.secretmanager.v1beta1.AccessSecretVersionResponse;
22+
import com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient;
23+
import com.google.cloud.secretmanager.v1beta1.SecretVersionName;
24+
import java.io.IOException;
25+
26+
public class AccessSecretVersion {
27+
28+
public void accessSecretVersion() throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "your-project-id";
31+
String secretId = "your-secret-id";
32+
String versionId = "your-version-id";
33+
accessSecretVersion(projectId, secretId, versionId);
34+
}
35+
36+
// Access the payload for the given secret version if one exists. The version
37+
// can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
38+
public void accessSecretVersion(String projectId, String secretId, String versionId)
39+
throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
44+
SecretVersionName name = SecretVersionName.of(projectId, secretId, versionId);
45+
46+
// Access the secret version.
47+
AccessSecretVersionRequest request =
48+
AccessSecretVersionRequest.newBuilder().setName(name.toString()).build();
49+
AccessSecretVersionResponse response = client.accessSecretVersion(request);
50+
51+
// Print the secret payload.
52+
//
53+
// WARNING: Do not print the secret in a production environment - this
54+
// snippet is showing how to access the secret material.
55+
String payload = response.getPayload().getData().toStringUtf8();
56+
System.out.printf("Plaintext: %s\n", payload);
57+
}
58+
}
59+
}
60+
// [END secretmanager_access_secret_version]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2020 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+
* http://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.example;
18+
19+
// [START secretmanager_add_secret_version]
20+
import com.google.cloud.secretmanager.v1beta1.AddSecretVersionRequest;
21+
import com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1beta1.SecretName;
23+
import com.google.cloud.secretmanager.v1beta1.SecretPayload;
24+
import com.google.cloud.secretmanager.v1beta1.SecretVersion;
25+
import com.google.protobuf.ByteString;
26+
import java.io.IOException;
27+
28+
public class AddSecretVersion {
29+
30+
public void addSecretVersion() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "your-project-id";
33+
String secretId = "your-secret-id";
34+
addSecretVersion(projectId, secretId);
35+
}
36+
37+
// Add a new version to the existing secret.
38+
public void addSecretVersion(String projectId, String secretId) throws IOException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
43+
SecretName name = SecretName.of(projectId, secretId);
44+
45+
// Create the secret payload.
46+
SecretPayload payload =
47+
SecretPayload.newBuilder()
48+
.setData(ByteString.copyFromUtf8("my super secret data"))
49+
.build();
50+
51+
// Create the request.
52+
AddSecretVersionRequest request =
53+
AddSecretVersionRequest.newBuilder()
54+
.setParent(name.toString())
55+
.setPayload(payload)
56+
.build();
57+
58+
// Add the secret version.
59+
SecretVersion version = client.addSecretVersion(request);
60+
System.out.printf("Added secret version %s\n", version.getName());
61+
}
62+
}
63+
}
64+
// [END secretmanager_add_secret_version]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2020 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+
* http://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.example;
18+
19+
// [START secretmanager_create_secret]
20+
import com.google.cloud.secretmanager.v1beta1.CreateSecretRequest;
21+
import com.google.cloud.secretmanager.v1beta1.ProjectName;
22+
import com.google.cloud.secretmanager.v1beta1.Replication;
23+
import com.google.cloud.secretmanager.v1beta1.Secret;
24+
import com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient;
25+
import java.io.IOException;
26+
27+
public class CreateSecret {
28+
29+
public void createSecret() throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "your-project-id";
32+
String secretId = "your-secret-id";
33+
createSecret(projectId, secretId);
34+
}
35+
36+
// Add a new version to the existing secret.
37+
public void createSecret(String projectId, String secretId) throws IOException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
42+
// Build the parent name from the project.
43+
ProjectName parent = ProjectName.of(projectId);
44+
45+
// Build the secret to create.
46+
Secret secret =
47+
Secret.newBuilder()
48+
.setReplication(
49+
Replication.newBuilder()
50+
.setAutomatic(Replication.Automatic.newBuilder().build())
51+
.build())
52+
.build();
53+
54+
// Create the request.
55+
CreateSecretRequest request =
56+
CreateSecretRequest.newBuilder()
57+
.setParent(parent.toString())
58+
.setSecretId(secretId)
59+
.setSecret(secret)
60+
.build();
61+
62+
// Create the secret.
63+
Secret createdSecret = client.createSecret(request);
64+
System.out.printf("Created secret %s\n", createdSecret.getName());
65+
}
66+
}
67+
}
68+
// [END secretmanager_create_secret]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2020 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+
* http://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.example;
18+
19+
// [START secretmanager_delete_secret]
20+
import com.google.cloud.secretmanager.v1beta1.DeleteSecretRequest;
21+
import com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1beta1.SecretName;
23+
import java.io.IOException;
24+
25+
public class DeleteSecret {
26+
27+
public void deleteSecret() throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "your-project-id";
30+
String secretId = "your-secret-id";
31+
deleteSecret(projectId, secretId);
32+
}
33+
34+
// Delete an existing secret with the given name.
35+
public void deleteSecret(String projectId, String secretId) throws IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
40+
// Build the secret name.
41+
SecretName name = SecretName.of(projectId, secretId);
42+
43+
// Create the request.
44+
DeleteSecretRequest request =
45+
DeleteSecretRequest.newBuilder().setName(name.toString()).build();
46+
47+
// Create the secret.
48+
client.deleteSecret(request);
49+
System.out.printf("Deleted secret %s\n", secretId);
50+
}
51+
}
52+
}
53+
// [END secretmanager_delete_secret]

0 commit comments

Comments
 (0)