-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(bigquery): Add samples for control access 2/3 #4024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dd285e8
feat(bigquery): Add samples for control access 2/3
hivanalejandro e38bb02
Merge branch 'main' into hivanalejandro-features-bigquery-2
hivanalejandro 3d5dde6
Merge branch 'main' into hivanalejandro-features-bigquery-2
hivanalejandro edab26d
chore(bigquery): testing, stylistic update
hivanalejandro cdc663a
fix(bigquery): update principalId content
hivanalejandro dce3f89
Merge branch 'main' into hivanalejandro-features-bigquery-2
telpirion 6114b93
chore(bigquery): simplify logging logic in revokeTableOrViewAccess
hivanalejandro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| async function main(datasetId, entityId) { | ||
| // [START bigquery_revoke_dataset_access] | ||
|
|
||
| /** | ||
| * TODO(developer): Update and un-comment below lines | ||
| */ | ||
|
|
||
| // const datasetId = "my_project_id.my_dataset" | ||
|
|
||
| // ID of the user or group from whom you are revoking access. | ||
| // const entityId = "[email protected]" | ||
|
|
||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| // Instantiate a client. | ||
| const bigquery = new BigQuery(); | ||
|
|
||
| async function revokeDatasetAccess() { | ||
| const [dataset] = await bigquery.dataset(datasetId).get(); | ||
|
|
||
| // To revoke access to a dataset, remove elements from the access list. | ||
| // | ||
| // See the BigQuery client library documentation for more details on access entries: | ||
| // https://cloud.google.com/nodejs/docs/reference/bigquery/latest | ||
|
|
||
| // Filter access entries to exclude entries matching the specified entity_id | ||
| // and assign a new list back to the access list. | ||
| dataset.metadata.access = dataset.metadata.access.filter(entry => { | ||
| return !( | ||
| entry.entity_id === entityId || | ||
| entry.userByEmail === entityId || | ||
| entry.groupByEmail === entityId | ||
| ); | ||
| }); | ||
|
|
||
| // Update will only succeed if the dataset | ||
| // has not been modified externally since retrieval. | ||
| // | ||
| // See the BigQuery client library documentation for more details on metadata updates: | ||
| // https://cloud.google.com/bigquery/docs/updating-datasets | ||
|
|
||
| // Update just the 'access entries' property of the dataset. | ||
| await dataset.setMetadata(dataset.metadata); | ||
|
|
||
| console.log(`Revoked access to '${entityId}' from '${datasetId}'.`); | ||
| } | ||
| // [END bigquery_revoke_dataset_access] | ||
| await revokeDatasetAccess(); | ||
| } | ||
|
|
||
| exports.revokeDatasetAccess = main; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| async function main( | ||
| projectId, | ||
| datasetId, | ||
| tableId, | ||
| roleToRemove = null, | ||
| principalToRemove = null | ||
| ) { | ||
| // [START bigquery_revoke_access_to_table_or_view] | ||
|
|
||
| /** | ||
| * TODO(developer): Update and un-comment below lines | ||
| */ | ||
| // const projectId = "YOUR_PROJECT_ID" | ||
| // const datasetId = "YOUR_DATASET_ID" | ||
| // const tableId = "YOUR_TABLE_ID" | ||
| // const roleToRemove = "YOUR_ROLE" | ||
| // const principalToRemove = "YOUR_PRINCIPAL_ID" | ||
|
|
||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| // Instantiate a client. | ||
| const client = new BigQuery(); | ||
|
|
||
| async function revokeAccessToTableOrView() { | ||
| const dataset = client.dataset(datasetId); | ||
| const table = dataset.table(tableId); | ||
|
|
||
| // Get the IAM access policy for the table or view. | ||
| const [policy] = await table.getIamPolicy(); | ||
|
|
||
| // Initialize bindings array. | ||
| if (!policy.bindings) { | ||
| policy.bindings = []; | ||
| } | ||
|
|
||
| // To revoke access to a table or view, | ||
| // remove bindings from the Table or View policy. | ||
| // | ||
| // Find more details about Policy objects here: | ||
| // https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Policy | ||
|
|
||
| if (principalToRemove) { | ||
| // Create a copy of bindings for modifications. | ||
| const bindings = [...policy.bindings]; | ||
|
|
||
| // Filter out the principal from each binding. | ||
| for (const binding of bindings) { | ||
| if (binding.members) { | ||
| binding.members = binding.members.filter( | ||
| m => m !== principalToRemove | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Filter out bindings with empty members. | ||
| policy.bindings = bindings.filter( | ||
| binding => binding.members && binding.members.length > 0 | ||
| ); | ||
| } | ||
|
|
||
| if (roleToRemove) { | ||
| // Filter out all bindings with the roleToRemove | ||
| // and assign a new list back to the policy bindings. | ||
| policy.bindings = policy.bindings.filter(b => b.role !== roleToRemove); | ||
| } | ||
|
|
||
| // Set the IAM access policy with updated bindings. | ||
| await table.setIamPolicy(policy); | ||
|
|
||
| // Create a descriptive message based on what was actually removed | ||
hivanalejandro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (roleToRemove && principalToRemove) { | ||
hivanalejandro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| console.log( | ||
| `Role '${roleToRemove}' revoked for principal '${principalToRemove}' on resource '${datasetId}.${tableId}'.` | ||
| ); | ||
| } else if (roleToRemove) { | ||
| console.log( | ||
| `Role '${roleToRemove}' revoked for all principals on resource '${datasetId}.${tableId}'.` | ||
| ); | ||
| } else if (principalToRemove) { | ||
| console.log( | ||
| `Access revoked for principal '${principalToRemove}' on resource '${datasetId}.${tableId}'.` | ||
| ); | ||
| } else { | ||
| console.log( | ||
| `No changes made to access policy for '${datasetId}.${tableId}'.` | ||
| ); | ||
| } | ||
| } | ||
| // [END bigquery_revoke_access_to_table_or_view] | ||
| await revokeAccessToTableOrView(); | ||
| } | ||
|
|
||
| exports.revokeAccessToTableOrView = main; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const {beforeEach, afterEach, it, describe} = require('mocha'); | ||
| const assert = require('assert'); | ||
| const sinon = require('sinon'); | ||
|
|
||
| const {setupBeforeAll, cleanupResources} = require('./config'); | ||
| const {grantAccessToDataset} = require('../grantAccessToDataset'); | ||
| const {revokeDatasetAccess} = require('../revokeDatasetAccess'); | ||
|
|
||
| describe('revokeDatasetAccess', () => { | ||
| let datasetId = null; | ||
| let entityId = null; | ||
| const role = 'READER'; | ||
|
|
||
| beforeEach(async () => { | ||
| const response = await setupBeforeAll(); | ||
| datasetId = response.datasetId; | ||
| entityId = response.entityId; | ||
|
|
||
| sinon.stub(console, 'log'); | ||
| sinon.stub(console, 'error'); | ||
| }); | ||
|
|
||
| // Clean up after all tests. | ||
| afterEach(async () => { | ||
| await cleanupResources(datasetId); | ||
| console.log.restore(); | ||
| console.error.restore(); | ||
| }); | ||
|
|
||
| it('should revoke access to a dataset', async () => { | ||
| // Grant access to the dataset. | ||
| await grantAccessToDataset(datasetId, entityId, role); | ||
|
|
||
| // Reset console.log stub to clear the history of calls | ||
| console.log.resetHistory(); | ||
|
|
||
| // Now revoke access. | ||
| await revokeDatasetAccess(datasetId, entityId); | ||
|
|
||
| // Check if the right message was logged. | ||
| assert.strictEqual( | ||
| console.log.calledWith( | ||
| `Revoked access to '${entityId}' from '${datasetId}'.` | ||
| ), | ||
| true | ||
| ); | ||
| }); | ||
| }); |
113 changes: 113 additions & 0 deletions
113
bigquery/cloud-client/test/revokeTableOrViewAccess.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const {describe, it, beforeEach, afterEach} = require('mocha'); | ||
| const assert = require('assert'); | ||
| const sinon = require('sinon'); | ||
|
|
||
| const {revokeAccessToTableOrView} = require('../revokeTableOrViewAccess'); | ||
| const {grantAccessToTableOrView} = require('../grantAccessToTableOrView'); | ||
| const {setupBeforeAll, cleanupResources} = require('./config'); | ||
|
|
||
| describe('revokeTableOrViewAccess', () => { | ||
| let datasetId = null; | ||
| let tableId = null; | ||
| let entityId = null; | ||
| const projectId = process.env.GCLOUD_PROJECT; | ||
| const roleId = 'roles/bigquery.dataViewer'; | ||
|
|
||
| beforeEach(async () => { | ||
| const response = await setupBeforeAll(); | ||
| datasetId = response.datasetId; | ||
| tableId = response.tableId; | ||
| entityId = response.entityId; | ||
|
|
||
| sinon.stub(console, 'log'); | ||
| sinon.stub(console, 'error'); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await cleanupResources(datasetId); | ||
| console.log.restore(); | ||
| console.error.restore(); | ||
| }); | ||
|
|
||
| it('should revoke access to a table for a specific role', async () => { | ||
| const principalId = `group:${entityId}`; | ||
|
|
||
| // Grant access first. | ||
| await grantAccessToTableOrView( | ||
| projectId, | ||
| datasetId, | ||
| tableId, | ||
| principalId, | ||
| roleId | ||
| ); | ||
|
|
||
| // Reset console log history. | ||
| console.log.resetHistory(); | ||
|
|
||
| // Revoke access for the role. | ||
| await revokeAccessToTableOrView( | ||
| projectId, | ||
| datasetId, | ||
| tableId, | ||
| roleId, | ||
| null | ||
| ); | ||
|
|
||
| // Check that the right message was logged. | ||
| assert.strictEqual( | ||
| console.log.calledWith( | ||
| `Role '${roleId}' revoked for all principals on resource '${datasetId}.${tableId}'.` | ||
| ), | ||
| true | ||
| ); | ||
| }); | ||
|
|
||
| it('should revoke access to a table for a specific principal', async () => { | ||
| const principalId = `group:${entityId}`; | ||
|
|
||
| // Grant access first. | ||
| await grantAccessToTableOrView( | ||
| projectId, | ||
| datasetId, | ||
| tableId, | ||
| principalId, | ||
| roleId | ||
| ); | ||
|
|
||
| // Reset console log history. | ||
| console.log.resetHistory(); | ||
|
|
||
| // Revoke access for the principal. | ||
| await revokeAccessToTableOrView( | ||
| projectId, | ||
| datasetId, | ||
| tableId, | ||
| null, | ||
| principalId | ||
| ); | ||
|
|
||
| // Check that the right message was logged. | ||
| assert.strictEqual( | ||
| console.log.calledWith( | ||
| `Access revoked for principal '${principalId}' on resource '${datasetId}.${tableId}'.` | ||
| ), | ||
| true | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.