-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat(bigquery): WIP add samples for access policies #3975
base: main
Are you sure you want to change the base?
Changes from 12 commits
5925b0f
e24c2e8
84ec0f6
21f786e
5d408c7
62e343a
c87d659
58ee72a
62ebc2c
9d0ce88
627536a
70cfb83
ff35988
b8ad0f4
7b38e5a
5cfa8dc
ccf30d6
eb32aa8
0a5f5dd
6c40940
b600729
c7a1821
db956a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 {viewDatasetAccessPolicy} = require('./src/viewDatasetAccessPolicy'); | ||
const { | ||
viewTableOrViewAccessPolicy, | ||
} = require('./src/viewTableOrViewAccessPolicy'); | ||
const {revokeTableOrViewAccess} = require('./src/revokeTableOrViewAccess'); | ||
|
||
async function main() { | ||
try { | ||
const projectId = process.env.GOOGLE_CLOUD_PROJECT; | ||
|
||
// Example usage of dataset access policy viewer | ||
await viewDatasetAccessPolicy(); | ||
|
||
// Example usage of table/view access policy viewer | ||
await viewTableOrViewAccessPolicy({ | ||
projectId, | ||
datasetId: 'my_new_dataset', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per my review for Go 5144#discussion_r1956626017, I'd suggest to use a different dataset and table name, and to check the best practices on creating new test resources on NodeJS. For example I'm using these dataset and table names on Python: |
||
resourceName: 'my_table', | ||
}); | ||
|
||
await revokeTableOrViewAccess({ | ||
projectId, | ||
datasetId: 'my_new_dataset', | ||
resourceName: 'my_table', | ||
memberToRevoke: 'group:[email protected]', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in the chat,
I'd suggest using the same for all languages. |
||
roleToRevoke: 'roles/bigquery.dataViewer', | ||
}); | ||
} catch (error) { | ||
console.error('Error:', error); | ||
process.exitCode = 1; | ||
} | ||
} | ||
|
||
// Run the samples if this file is run directly | ||
if (require.main === module) { | ||
main(); | ||
} | ||
|
||
module.export = {viewDatasetAccessPolicy, viewTableOrViewAccessPolicy}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "bigquery-cloud-client", | ||
"description": "Big Query Cloud Client Node.js for Google App", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"author": "Google Inc.", | ||
"engines": { | ||
"node": "20.x" | ||
}, | ||
"scripts": { | ||
"deploy": "gcloud app deploy", | ||
"start": "node app.js", | ||
"unit-test": "c8 mocha -p -j 2 test/ --timeout=10000 --exit", | ||
"test": "npm run unit-test" | ||
}, | ||
"dependencies": { | ||
"@google-cloud/bigquery": "7.9.2" | ||
}, | ||
"devDependencies": { | ||
"c8": "^10.0.0", | ||
"chai": "^4.5.0", | ||
"mocha": "^10.0.0", | ||
"sinon": "^18.0.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,105 @@ | ||||||||||||||||||||||||||||||||||||
// 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. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const {BigQuery} = require('@google-cloud/bigquery'); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// [START bigquery_revoke_access_to_table_or_view] | ||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* Revokes access to a BigQuery table or view | ||||||||||||||||||||||||||||||||||||
* @param {Object} params - The parameters object | ||||||||||||||||||||||||||||||||||||
* @param {string} params.projectId - The ID of the Google Cloud project | ||||||||||||||||||||||||||||||||||||
* @param {string} params.datasetId - The ID of the dataset containing the table/view | ||||||||||||||||||||||||||||||||||||
* @param {string} params.resourceId - The ID of the table or view | ||||||||||||||||||||||||||||||||||||
* @param {string} [params.memberToRevoke] - Optional. Specific member to revoke access from (e.g., 'group:[email protected]') | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it could be better: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it looks like In that case it could be:
|
||||||||||||||||||||||||||||||||||||
* @param {string} [params.roleToRevoke='roles/bigquery.dataViewer'] - Optional. Specific role to revoke | ||||||||||||||||||||||||||||||||||||
* @returns {Promise<void>} | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
async function revokeTableOrViewAccess({ | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per 'Format overview' in the 'Node.js Sample Policies' Check this sample as a reference: nodejs-docs-samples/compute/createInstance.js Lines 36 to 52 in 6398a9a
|
||||||||||||||||||||||||||||||||||||
projectId, | ||||||||||||||||||||||||||||||||||||
datasetId, | ||||||||||||||||||||||||||||||||||||
resourceId, | ||||||||||||||||||||||||||||||||||||
memberToRevoke, | ||||||||||||||||||||||||||||||||||||
roleToRevoke = 'roles/bigquery.dataViewer', | ||||||||||||||||||||||||||||||||||||
}) { | ||||||||||||||||||||||||||||||||||||
// Validate required parameters | ||||||||||||||||||||||||||||||||||||
if (!projectId || !datasetId || !resourceId) { | ||||||||||||||||||||||||||||||||||||
throw new Error( | ||||||||||||||||||||||||||||||||||||
'projectId, datasetId and resourceID are required parameters' | ||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||
// Create BigQuery client | ||||||||||||||||||||||||||||||||||||
const bigquery = new BigQuery({ | ||||||||||||||||||||||||||||||||||||
projectId: projectId, | ||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Get reference to the table or view | ||||||||||||||||||||||||||||||||||||
const dataset = bigquery.dataset(datasetId); | ||||||||||||||||||||||||||||||||||||
const table = dataset.table(resourceId); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Get current IAM policy | ||||||||||||||||||||||||||||||||||||
const [policy] = await table.iam.getPolicy(); | ||||||||||||||||||||||||||||||||||||
console.log( | ||||||||||||||||||||||||||||||||||||
'Current IAM Policy:', | ||||||||||||||||||||||||||||||||||||
JSON.stringify(policy.bindings, null, 2) | ||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Filter bindings based on parameters | ||||||||||||||||||||||||||||||||||||
let newBindings = policy.bindings; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (memberToRevoke && roleToRevoke) { | ||||||||||||||||||||||||||||||||||||
// Remove specific member from specific role | ||||||||||||||||||||||||||||||||||||
newBindings = policy.bindings | ||||||||||||||||||||||||||||||||||||
.map(binding => ({ | ||||||||||||||||||||||||||||||||||||
...binding, | ||||||||||||||||||||||||||||||||||||
members: | ||||||||||||||||||||||||||||||||||||
binding.role === roleToRevoke | ||||||||||||||||||||||||||||||||||||
? binding.members.filter(member => member !== memberToRevoke) | ||||||||||||||||||||||||||||||||||||
: binding.members, | ||||||||||||||||||||||||||||||||||||
})) | ||||||||||||||||||||||||||||||||||||
.filter(binding => binding.members.length > 0); | ||||||||||||||||||||||||||||||||||||
} else if (!memberToRevoke && roleToRevoke) { | ||||||||||||||||||||||||||||||||||||
// Remove all bindings for the specified role | ||||||||||||||||||||||||||||||||||||
newBindings = policy.bindings.filter( | ||||||||||||||||||||||||||||||||||||
binding => binding.role !== roleToRevoke | ||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||
// Keep the current binding as is | ||||||||||||||||||||||||||||||||||||
newBindings = policy.bindings; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Create new policy with updated bindings | ||||||||||||||||||||||||||||||||||||
const newPolicy = { | ||||||||||||||||||||||||||||||||||||
bindings: newBindings, | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Set the new IAM policy | ||||||||||||||||||||||||||||||||||||
await table.iam.setPolicy(newPolicy); | ||||||||||||||||||||||||||||||||||||
console.log(`Access revoked successfully for ${resourceId}`); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Verify the changes | ||||||||||||||||||||||||||||||||||||
const [updatedPolicy] = await table.iam.getPolicy(); | ||||||||||||||||||||||||||||||||||||
console.log( | ||||||||||||||||||||||||||||||||||||
'Updated IAM Policy:', | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps leave a space after the colon to make it cleaner to read? |
||||||||||||||||||||||||||||||||||||
JSON.stringify(updatedPolicy.bindings, null, 2) | ||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||
console.error('Error revoking access:', error); | ||||||||||||||||||||||||||||||||||||
throw error; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// [END bigquery_revoke_access_to_table_or_view] | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
module.exports = {revokeTableOrViewAccess}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||
// 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 {BigQuery} = require('@google-cloud/bigquery'); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// [START bigquery_view_dataset_access_policy] | ||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* View access policies for a BigQuery dataset | ||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||
* @param {object} [overrideValues] Optional parameters to override defaults | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. David Cavazos advised in this Python PR Review against Overriding parameters as it may be confusing. Having these things commented out as TODOs and passing them as arguments to the function. For an example see: nodejs-docs-samples/compute/createInstance.js Lines 35 to 52 in 102eaed
|
||||||||||||||||||||||||||||||||||||||
* @param {string} [overrideValues.datasetId] Dataset ID to view access policies | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
async function viewDatasetAccessPolicy(overrideValues = {}) { | ||||||||||||||||||||||||||||||||||||||
// Instantiate BigQuery client | ||||||||||||||||||||||||||||||||||||||
const bigquery = new BigQuery(); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Dataset from which to get the access policy | ||||||||||||||||||||||||||||||||||||||
const datasetId = overrideValues.datasetId || 'my_new_dataset'; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||
// Prepares a reference to the dataset | ||||||||||||||||||||||||||||||||||||||
const dataset = bigquery.dataset(datasetId); | ||||||||||||||||||||||||||||||||||||||
const [metadata] = await dataset.getMetadata(); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Shows the Access policy as a list of access entries | ||||||||||||||||||||||||||||||||||||||
console.log('Access entries:', metadata.access); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe leave a space after the colon to make it easier to read? |
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Get properties for an AccessEntry | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per David's review and further discussion, on Python we switched from showing the first element to the whole list (for each): |
||||||||||||||||||||||||||||||||||||||
if (metadata.access && metadata.access.length > 0) { | ||||||||||||||||||||||||||||||||||||||
console.log(`Details for Access entry 0 in dataset '${datasetId}':`); | ||||||||||||||||||||||||||||||||||||||
console.log(`Role: ${metadata.access[0].role || 'N/A'}`); | ||||||||||||||||||||||||||||||||||||||
console.log(`SpecialGroup: ${metadata.access[0].specialGroup || 'N/A'}`); | ||||||||||||||||||||||||||||||||||||||
console.log(`UserByEmail: ${metadata.access[0].userByEmail || 'N/A'}`); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||
console.error(`Error viewing dataset access policy: ${error.message}`); | ||||||||||||||||||||||||||||||||||||||
throw error; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
// [END bigquery_view_dataset_access_policy] | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
module.exports = { | ||||||||||||||||||||||||||||||||||||||
viewDatasetAccessPolicy, | ||||||||||||||||||||||||||||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 {BigQuery} = require('@google-cloud/bigquery'); | ||
|
||
// [START bigquery_view_table_or_view_access_policy] | ||
/** | ||
* View access policies for a BigQuery table or view | ||
* | ||
* @param {object} [overrideValues] Optional parameters to override defaults | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Overriding parameters as it may be confusing. Having these things commented out as TODOs and passing them as arguments to the function." See my previous comment in |
||
* @param {string} [overrideValues.projectId] Google Cloud project ID | ||
* @param {string} [overrideValues.datasetId] Dataset ID where the table or view is located | ||
* @param {string} [overrideValues.resourceName] Table or view name to get the access policy | ||
* @throws {Error} If required parameters are missing or if there's an API error | ||
*/ | ||
async function viewTableOrViewAccessPolicy(overrideValues = {}) { | ||
// Initialize default values | ||
const projectId = | ||
overrideValues.projectId || process.env.GOOGLE_CLOUD_PROJECT; | ||
const datasetId = overrideValues.datasetId || 'my_new_dataset'; | ||
const resourceName = overrideValues.resourceName || 'my_table'; | ||
|
||
if (!projectId) { | ||
throw new Error( | ||
'Project ID is required. Set it in overrideValues or GOOGLE_CLOUD_PROJECT environment variable.' | ||
); | ||
} | ||
|
||
try { | ||
// Instantiate BigQuery client | ||
const bigquery = new BigQuery({projectId}); | ||
|
||
// Get the IAM access policy for the table or view | ||
const [policy] = await bigquery | ||
.dataset(datasetId) | ||
.table(resourceName) | ||
.getIamPolicy(); | ||
|
||
// Show policy details | ||
console.log(`Access Policy details for table or view '${resourceName}':`); | ||
console.log(`Bindings: ${JSON.stringify(policy.bindings, null, 2)}`); | ||
console.log(`etag: ${policy.etag}`); | ||
console.log(`version: ${policy.version}`); | ||
|
||
return policy; | ||
} catch (error) { | ||
console.error(`Error viewing table/view access policy: ${error.message}`); | ||
throw error; | ||
} | ||
} | ||
// [END bigquery_view_table_or_view_access_policy] | ||
|
||
// If this file is run directly, execute the function with default values | ||
if (require.main === module) { | ||
viewTableOrViewAccessPolicy().catch(console.error); | ||
} | ||
|
||
module.exports = { | ||
viewTableOrViewAccessPolicy, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Python I found that the previous developer was using the client project, instead of this environment variable, as we do for other samples.
As the tests are still passing, I left the
client.project
instead.conftest.py#L38