Skip to content

Commit 4a15284

Browse files
authored
feat: add IAM samples (#34)
1 parent 1d97e26 commit 4a15284

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

secret-manager/iamGrantAccess.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(
18+
name = 'projects/my-project/secrets/my-secret',
19+
member = 'user:[email protected]'
20+
) {
21+
// [START secretmanager_iam_grant_access]
22+
/**
23+
* TODO(developer): Uncomment these variables before running the sample.
24+
*/
25+
// const name = 'projects/my-project/secrets/my-secret';
26+
// const member = 'user:[email protected]';
27+
//
28+
// NOTE: Each member must be prefixed with its type. See the IAM documentation
29+
// for more information: https://cloud.google.com/iam/docs/overview.
30+
31+
// Imports the Secret Manager library
32+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
33+
34+
// Instantiates a client
35+
const client = new SecretManagerServiceClient();
36+
37+
async function grantAccess() {
38+
// Get the current IAM policy.
39+
const [policy] = await client.getIamPolicy({
40+
resource: name,
41+
});
42+
43+
// Add the user with accessor permissions to the bindings list.
44+
policy.bindings.push({
45+
role: 'roles/secretmanager.secretAccessor',
46+
members: [member],
47+
});
48+
49+
// Save the updated IAM policy.
50+
await client.setIamPolicy({
51+
resource: name,
52+
policy: policy,
53+
});
54+
55+
console.log(`Updated IAM policy for ${name}`);
56+
}
57+
58+
grantAccess();
59+
// [END secretmanager_iam_grant_access]
60+
}
61+
62+
const args = process.argv.slice(2);
63+
main(...args).catch(console.error);

secret-manager/iamRevokeAccess.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(
18+
name = 'projects/my-project/secrets/my-secret',
19+
member = 'user:[email protected]'
20+
) {
21+
// [START secretmanager_iam_revoke_access]
22+
/**
23+
* TODO(developer): Uncomment these variables before running the sample.
24+
*/
25+
// const name = 'projects/my-project/secrets/my-secret';
26+
// const member = 'user:[email protected]';
27+
//
28+
// NOTE: Each member must be prefixed with its type. See the IAM documentation
29+
// for more information: https://cloud.google.com/iam/docs/overview.
30+
31+
// Imports the Secret Manager library
32+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
33+
34+
// Instantiates a client
35+
const client = new SecretManagerServiceClient();
36+
37+
async function grantAccess() {
38+
// Get the current IAM policy.
39+
const [policy] = await client.getIamPolicy({
40+
resource: name,
41+
});
42+
43+
// Build a new list of policy bindings with the user excluded.
44+
for (const i in policy.bindings) {
45+
const binding = policy.bindings[i];
46+
if (binding.role !== 'roles/secretmanager.secretAccessor') {
47+
continue;
48+
}
49+
50+
const idx = binding.members.indexOf(member);
51+
if (idx !== -1) {
52+
binding.members.splice(idx, 1);
53+
}
54+
}
55+
56+
// Save the updated IAM policy.
57+
await client.setIamPolicy({
58+
resource: name,
59+
policy: policy,
60+
});
61+
62+
console.log(`Updated IAM policy for ${name}`);
63+
}
64+
65+
grantAccess();
66+
// [END secretmanager_iam_revoke_access]
67+
}
68+
69+
const args = process.argv.slice(2);
70+
main(...args).catch(console.error);

secret-manager/test/secretmanager.test.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const client = new SecretManagerServiceClient();
2323

2424
const projectId = process.env.GCLOUD_PROJECT;
2525
const secretId = uuidv4();
26-
const payload = `my super secret data`;
26+
const payload = 'my super secret data';
27+
const iamUser = 'user:[email protected]';
2728

2829
let secret;
2930
let version;
@@ -136,6 +137,18 @@ describe(`Secret Manager samples`, () => {
136137
});
137138

138139
it(`gets secret versions`, async () => {
140+
const output = execSync(`node iamGrantAccess.js ${secret.name} ${iamUser}`);
141+
assert.match(output, new RegExp(`Updated IAM policy`));
142+
});
143+
144+
it(`revokes access permissions`, async () => {
145+
const output = execSync(
146+
`node iamRevokeAccess.js ${secret.name} ${iamUser}`
147+
);
148+
assert.match(output, new RegExp(`Updated IAM policy`));
149+
});
150+
151+
it(`grants access permissions`, async () => {
139152
const output = execSync(`node getSecretVersion.js ${version.name}`);
140153
assert.match(output, new RegExp(`Found secret ${version.name}`));
141154
});

0 commit comments

Comments
 (0)