|
| 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] |
0 commit comments