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

Function main

kms/iamRemoveMember.js:17–82  ·  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_remove_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 iamRemoveMember() {
52 // Get the current IAM policy.
53 const [policy] = await client.getIamPolicy({
54 resource: resourceName,
55 });
56
57 // Build a new list of policy bindings with the user excluded.
58 for (const i in policy.bindings) {
59 const binding = policy.bindings[i];
60 if (binding.role !== 'roles/cloudkms.cryptoKeyEncrypterDecrypter') {
61 continue;
62 }
63
64 const idx = binding.members.indexOf(member);
65 if (idx !== -1) {
66 binding.members.splice(idx, 1);
67 }
68 }
69
70 // Save the updated IAM policy.
71 const [updatedPolicy] = await client.setIamPolicy({
72 resource: resourceName,
73 policy: policy,
74 });

Callers 1

iamRemoveMember.jsFile · 0.70

Calls 1

iamRemoveMemberFunction · 0.85

Tested by

no test coverage detected