Skip to content

Commit cad876a

Browse files
Add Secret Manager IAM samples (#2039)
* Add Secret Manager IAM samples * Use a better variable name for binding * Use service account * Add service account prefix Co-authored-by: Averi Kitsch <[email protected]>
1 parent 4fff89e commit cad876a

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_iam_grant_access]
20+
import com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient;
21+
import com.google.cloud.secretmanager.v1beta1.SecretName;
22+
import com.google.iam.v1.Binding;
23+
import com.google.iam.v1.GetIamPolicyRequest;
24+
import com.google.iam.v1.Policy;
25+
import com.google.iam.v1.SetIamPolicyRequest;
26+
import java.io.IOException;
27+
28+
public class IamGrantAccess {
29+
30+
public void iamGrantAccess() 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+
String member = "user:[email protected]";
35+
iamGrantAccess(projectId, secretId, member);
36+
}
37+
38+
// Grant a member access to a particular secret.
39+
public void iamGrantAccess(String projectId, String secretId, String member) 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+
// Build the name from the version.
45+
SecretName name = SecretName.of(projectId, secretId);
46+
47+
// Create a request to get the current IAM policy.
48+
GetIamPolicyRequest getRequest =
49+
GetIamPolicyRequest.newBuilder().setResource(name.toString()).build();
50+
51+
// Request the current IAM policy.
52+
Policy currentPolicy = client.getIamPolicy(getRequest);
53+
54+
// Build the new binding.
55+
Binding binding =
56+
Binding.newBuilder()
57+
.setRole("roles/secretmanager.secretAccessor")
58+
.addMembers(member)
59+
.build();
60+
61+
// Create a new IAM policy from the current policy, adding the binding.
62+
Policy newPolicy = Policy.newBuilder().mergeFrom(currentPolicy).addBindings(binding).build();
63+
64+
// Create a request to update the IAM policy.
65+
SetIamPolicyRequest setRequest =
66+
SetIamPolicyRequest.newBuilder()
67+
.setResource(name.toString())
68+
.setPolicy(newPolicy)
69+
.build();
70+
71+
// Save the updated IAM policy.
72+
client.setIamPolicy(setRequest);
73+
74+
System.out.printf("Updated IAM policy for %s\n", secretId);
75+
}
76+
}
77+
}
78+
// [END secretmanager_iam_grant_access]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_iam_revoke_access]
20+
import com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient;
21+
import com.google.cloud.secretmanager.v1beta1.SecretName;
22+
import com.google.iam.v1.Binding;
23+
import com.google.iam.v1.GetIamPolicyRequest;
24+
import com.google.iam.v1.Policy;
25+
import com.google.iam.v1.SetIamPolicyRequest;
26+
import java.io.IOException;
27+
28+
public class IamRevokeAccess {
29+
30+
public void iamRevokeAccess() 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+
String member = "user:[email protected]";
35+
iamRevokeAccess(projectId, secretId, member);
36+
}
37+
38+
// Revoke a member access to a particular secret.
39+
public void iamRevokeAccess(String projectId, String secretId, String member) 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+
// Build the name from the version.
45+
SecretName name = SecretName.of(projectId, secretId);
46+
47+
// Create a request to get the current IAM policy.
48+
GetIamPolicyRequest getRequest =
49+
GetIamPolicyRequest.newBuilder().setResource(name.toString()).build();
50+
51+
// Request the current IAM policy.
52+
Policy policy = client.getIamPolicy(getRequest);
53+
54+
// Search through bindings and remove matches.
55+
String roleToFind = "roles/secretmanager.secretAccessor";
56+
for (Binding binding : policy.getBindingsList()) {
57+
if (binding.getRole() == roleToFind && binding.getMembersList().contains(member)) {
58+
binding.getMembersList().remove(member);
59+
}
60+
}
61+
62+
// Create a request to update the IAM policy.
63+
SetIamPolicyRequest setRequest =
64+
SetIamPolicyRequest.newBuilder().setResource(name.toString()).setPolicy(policy).build();
65+
66+
// Save the updated IAM policy.
67+
client.setIamPolicy(setRequest);
68+
69+
System.out.printf("Updated IAM policy for %s\n", secretId);
70+
}
71+
}
72+
}
73+
// [END secretmanager_iam_revoke_access]

secretmanager/src/test/java/com/example/SnippetsIT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
/** Integration (system) tests for {@link Snippets}. */
4848
@RunWith(JUnit4.class)
4949
public class SnippetsIT {
50+
private static final String IAM_USER =
51+
"serviceAccount:[email protected]";
5052
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
5153

5254
private static Secret TEST_SECRET;
@@ -242,6 +244,22 @@ public void testGetSecret() throws IOException {
242244
assertThat(stdOut.toString()).contains("replication AUTOMATIC");
243245
}
244246

247+
@Test
248+
public void testIamGrantAccess() throws IOException {
249+
SecretName name = SecretName.parse(TEST_SECRET.getName());
250+
new IamGrantAccess().iamGrantAccess(name.getProject(), name.getSecret(), IAM_USER);
251+
252+
assertThat(stdOut.toString()).contains("Updated IAM policy");
253+
}
254+
255+
@Test
256+
public void testIamRevokeAccess() throws IOException {
257+
SecretName name = SecretName.parse(TEST_SECRET.getName());
258+
new IamRevokeAccess().iamRevokeAccess(name.getProject(), name.getSecret(), IAM_USER);
259+
260+
assertThat(stdOut.toString()).contains("Updated IAM policy");
261+
}
262+
245263
@Test
246264
public void testListSecretVersions() throws IOException {
247265
SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName());

0 commit comments

Comments
 (0)