|
| 1 | +// Copyright 2025 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 | +// http://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 | +/** |
| 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( |
| 27 | + projectId, |
| 28 | + datasetId, |
| 29 | + resourceName, |
| 30 | + roleToRemove = null, |
| 31 | + principalToRemove = null |
| 32 | +) { |
| 33 | + // [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 | + |
| 47 | + // (Optional) Role to remove from the table or view. |
| 48 | + // roleToRemove = "roles/bigquery.dataViewer" |
| 49 | + |
| 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 |
| 55 | + |
| 56 | + // Instantiate a client. |
| 57 | + const client = new BigQuery(); |
| 58 | + |
| 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 |
| 77 | + |
| 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 | + } |
| 83 | + |
| 84 | + if (principalToRemove) { |
| 85 | + // The `bindings` array is immutable. Create a copy for modifications. |
| 86 | + const bindings = [...policy.bindings]; |
| 87 | + |
| 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); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Filter out bindings with empty members. |
| 96 | + policy.bindings = bindings.filter( |
| 97 | + binding => binding.members && binding.members.length > 0 |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + // Set the IAM access policy with updated bindings. |
| 103 | + await table.setIamPolicy(policy); |
| 104 | + |
| 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; |
| 115 | + } |
| 116 | + // [END bigquery_revoke_access_to_table_or_view] |
| 117 | +} |
| 118 | + |
| 119 | +module.exports = {revokeAccessToTableOrView}; |
0 commit comments