|
14 | 14 |
|
15 | 15 | 'use strict'; |
16 | 16 |
|
17 | | -/** |
18 | | - * Revokes access to a BigQuery table or view. |
19 | | - * @param {string} projectId The ID of the Google Cloud project. |
20 | | - * @param {string} datasetId The ID of the dataset containing the table/view. |
21 | | - * @param {string} resourceName The ID of the table or view. |
22 | | - * @param {string} [roleToRemove=null] Optional. Specific role to revoke. |
23 | | - * @param {string} [principalToRemove=null] Optional. Specific principal to revoke access from. |
24 | | - * @returns {Promise<Array>} The updated IAM policy. |
25 | | - */ |
26 | | -async function revokeAccessToTableOrView( |
| 17 | +async function main( |
27 | 18 | projectId, |
28 | 19 | datasetId, |
29 | | - resourceName, |
| 20 | + tableId, |
30 | 21 | roleToRemove = null, |
31 | 22 | principalToRemove = null |
32 | 23 | ) { |
33 | 24 | // [START bigquery_revoke_access_to_table_or_view] |
34 | | - const {BigQuery} = require('@google-cloud/bigquery'); |
35 | | - |
36 | | - // TODO (developer): Update and un-comment below lines. |
37 | | - |
38 | | - // Google Cloud Platform project. |
39 | | - // projectId = "my_project_id" |
40 | | - |
41 | | - // Dataset where the table or view is. |
42 | | - // datasetId = "my_dataset_id" |
43 | | - |
44 | | - // Table or view name to get the access policy. |
45 | | - // resourceName = "my_table_id" |
46 | 25 |
|
47 | | - // (Optional) Role to remove from the table or view. |
48 | | - // roleToRemove = "roles/bigquery.dataViewer" |
| 26 | + /** |
| 27 | + * TODO(developer): Update and un-comment below lines |
| 28 | + */ |
| 29 | + // const projectId = "YOUR_PROJECT_ID" |
| 30 | + // const datasetId = "YOUR_DATASET_ID" |
| 31 | + // const tableId = "YOUR_TABLE_ID" |
| 32 | + // const roleToRemove = "YOUR_ROLE" |
| 33 | + // const principalToRemove = "YOUR_PRINCIPAL_ID" |
49 | 34 |
|
50 | | - // (Optional) Principal to remove from the table or view. |
51 | | - // principalToRemove = "user:[email protected]" |
52 | | - |
53 | | - // Find more information about roles and principals (refered as members) here: |
54 | | - // https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Binding |
| 35 | + const {BigQuery} = require('@google-cloud/bigquery'); |
55 | 36 |
|
56 | 37 | // Instantiate a client. |
57 | 38 | const client = new BigQuery(); |
58 | 39 |
|
59 | | - // Get a reference to the dataset by datasetId. |
60 | | - const dataset = client.dataset(datasetId); |
61 | | - // Get a reference to the table by tableName. |
62 | | - const table = dataset.table(resourceName); |
63 | | - |
64 | | - // Get the IAM access policy for the table or view. |
65 | | - const [policy] = await table.getIamPolicy(); |
66 | | - |
67 | | - // Initialize bindings array. |
68 | | - if (!policy.bindings) { |
69 | | - policy.bindings = []; |
70 | | - } |
71 | | - |
72 | | - // To revoke access to a table or view, |
73 | | - // remove bindings from the Table or View policy. |
74 | | - // |
75 | | - // Find more details about Policy objects here: |
76 | | - // https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Policy |
| 40 | + async function revokeAccessToTableOrView() { |
| 41 | + const dataset = client.dataset(datasetId); |
| 42 | + const table = dataset.table(tableId); |
77 | 43 |
|
78 | | - if (roleToRemove) { |
79 | | - // Filter out all bindings with the `roleToRemove` |
80 | | - // and assign a new array back to the policy bindings. |
81 | | - policy.bindings = policy.bindings.filter(b => b.role !== roleToRemove); |
82 | | - } |
| 44 | + // Get the IAM access policy for the table or view. |
| 45 | + const [policy] = await table.getIamPolicy(); |
83 | 46 |
|
84 | | - if (principalToRemove) { |
85 | | - // The `bindings` array is immutable. Create a copy for modifications. |
86 | | - const bindings = [...policy.bindings]; |
| 47 | + // Initialize bindings array. |
| 48 | + if (!policy.bindings) { |
| 49 | + policy.bindings = []; |
| 50 | + } |
87 | 51 |
|
88 | | - // Filter out the principal from each binding. |
89 | | - for (const binding of bindings) { |
90 | | - if (binding.members) { |
91 | | - binding.members = binding.members.filter(m => m !== principalToRemove); |
| 52 | + // To revoke access to a table or view, |
| 53 | + // remove bindings from the Table or View policy. |
| 54 | + // |
| 55 | + // Find more details about Policy objects here: |
| 56 | + // https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Policy |
| 57 | + |
| 58 | + if (principalToRemove) { |
| 59 | + // Create a copy of bindings for modifications. |
| 60 | + const bindings = [...policy.bindings]; |
| 61 | + |
| 62 | + // Filter out the principal from each binding. |
| 63 | + for (const binding of bindings) { |
| 64 | + if (binding.members) { |
| 65 | + binding.members = binding.members.filter( |
| 66 | + m => m !== principalToRemove |
| 67 | + ); |
| 68 | + } |
92 | 69 | } |
| 70 | + |
| 71 | + // Filter out bindings with empty members. |
| 72 | + policy.bindings = bindings.filter( |
| 73 | + binding => binding.members && binding.members.length > 0 |
| 74 | + ); |
93 | 75 | } |
94 | 76 |
|
95 | | - // Filter out bindings with empty members. |
96 | | - policy.bindings = bindings.filter( |
97 | | - binding => binding.members && binding.members.length > 0 |
98 | | - ); |
99 | | - } |
| 77 | + if (roleToRemove) { |
| 78 | + // Filter out all bindings with the roleToRemove |
| 79 | + // and assign a new list back to the policy bindings. |
| 80 | + policy.bindings = policy.bindings.filter(b => b.role !== roleToRemove); |
| 81 | + } |
100 | 82 |
|
101 | | - try { |
102 | 83 | // Set the IAM access policy with updated bindings. |
103 | 84 | await table.setIamPolicy(policy); |
104 | 85 |
|
105 | | - // Get the policy again to confirm it's set correctly. |
106 | | - const [verifiedPolicy] = await table.getIamPolicy(); |
107 | | - |
108 | | - // Return the updated policy bindings. |
109 | | - return verifiedPolicy && verifiedPolicy.bindings |
110 | | - ? verifiedPolicy.bindings |
111 | | - : []; |
112 | | - } catch (error) { |
113 | | - console.error('Error settings IAM policy:', error); |
114 | | - throw error; |
| 86 | + // Create a descriptive message based on what was actually removed |
| 87 | + if (roleToRemove && principalToRemove) { |
| 88 | + console.log( |
| 89 | + `Role '${roleToRemove}' revoked for principal '${principalToRemove}' on resource '${datasetId}.${tableId}'.` |
| 90 | + ); |
| 91 | + } else if (roleToRemove) { |
| 92 | + console.log( |
| 93 | + `Role '${roleToRemove}' revoked for all principals on resource '${datasetId}.${tableId}'.` |
| 94 | + ); |
| 95 | + } else if (principalToRemove) { |
| 96 | + console.log( |
| 97 | + `Access revoked for principal '${principalToRemove}' on resource '${datasetId}.${tableId}'.` |
| 98 | + ); |
| 99 | + } else { |
| 100 | + console.log( |
| 101 | + `No changes made to access policy for '${datasetId}.${tableId}'.` |
| 102 | + ); |
| 103 | + } |
115 | 104 | } |
116 | 105 | // [END bigquery_revoke_access_to_table_or_view] |
| 106 | + await revokeAccessToTableOrView(); |
117 | 107 | } |
118 | 108 |
|
119 | | -module.exports = {revokeAccessToTableOrView}; |
| 109 | +exports.revokeAccessToTableOrView = main; |
0 commit comments