MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

kms/iamAddMember.js:17–75  ·  view source on GitHub ↗
(
  projectId = 'my-project',
  locationId = 'us-east1',
  keyRingId = 'my-key-ring',
  keyId = 'my-key',
  member = 'user:foo@example.com'
)

Source from the content-addressed store, hash-verified

15'use strict';
16
17async function main(
18 projectId = 'my-project',
19 locationId = 'us-east1',
20 keyRingId = 'my-key-ring',
21 keyId = 'my-key',
22 member = 'user:foo@example.com'
23) {
24 // [START kms_iam_add_member]
25 //
26 // TODO(developer): Uncomment these variables before running the sample.
27 //
28 // const projectId = 'my-project';
29 // const locationId = 'us-east1';
30 // const keyRingId = 'my-key-ring';
31 // const keyId = 'my-key';
32 // const member = 'user:foo@example.com';
33
34 // Imports the Cloud KMS library
35 const {KeyManagementServiceClient} = require('@google-cloud/kms');
36
37 // Instantiates a client
38 const client = new KeyManagementServiceClient();
39
40 // Build the resource name
41 const resourceName = client.cryptoKeyPath(
42 projectId,
43 locationId,
44 keyRingId,
45 keyId
46 );
47
48 // The resource name could also be a key ring.
49 // const resourceName = client.keyRingPath(projectId, locationId, keyRingId);
50
51 async function iamAddMember() {
52 // Get the current IAM policy.
53 const [policy] = await client.getIamPolicy({
54 resource: resourceName,
55 });
56
57 // Add the member to the policy.
58 policy.bindings.push({
59 role: 'roles/cloudkms.cryptoKeyEncrypterDecrypter',
60 members: [member],
61 });
62
63 // Save the updated policy.
64 const [updatedPolicy] = await client.setIamPolicy({
65 resource: resourceName,
66 policy: policy,
67 });
68
69 console.log('Updated policy');
70 return updatedPolicy;
71 }
72
73 return iamAddMember();
74 // [END kms_iam_add_member]

Callers 1

iamAddMember.jsFile · 0.70

Calls 1

iamAddMemberFunction · 0.85

Tested by

no test coverage detected