Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5925b0f
feat(bigquery): initial project setup
hivanalejandro Feb 13, 2025
e24c2e8
feat(bigquery): basic structure
hivanalejandro Feb 13, 2025
84ec0f6
feat(bigquery): Add table and view access policy viewer
hivanalejandro Feb 13, 2025
21f786e
feat(bigquery): Add viewDatasetAccessPolicy tests
hivanalejandro Feb 13, 2025
5d408c7
feat(bigquery): Add viewTableOrViewAccessPolicy tests
hivanalejandro Feb 14, 2025
62e343a
fix(bigquery): Fix linting errors
hivanalejandro Feb 14, 2025
c87d659
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Feb 14, 2025
58ee72a
feat(bigquery): Add revokeTableOrViewAccess feawture and tests
hivanalejandro Feb 17, 2025
62ebc2c
feat(bigquery): Update app.js file to add revokeTableOrViewAccess & f…
hivanalejandro Feb 17, 2025
9d0ce88
fix(bigquery):Fix headers for revokeTableOrViewAccess.js & revokeTabl…
hivanalejandro Feb 17, 2025
627536a
feat(bigquery): Add bigquery_revoke_access_to_table_or_view tag
hivanalejandro Feb 17, 2025
70cfb83
fix(bigquery): Update if/else to if/else if
hivanalejandro Feb 18, 2025
ff35988
feat(bigquery): Add grantAccessToDataset sample and tests
hivanalejandro Feb 20, 2025
b8ad0f4
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Feb 20, 2025
7b38e5a
fix(bigquery): Update lint error
hivanalejandro Feb 20, 2025
5cfa8dc
feat(bigquery): Add grantAccessToTableOrView sample
hivanalejandro Feb 20, 2025
ccf30d6
feat(bigquery): Add grantAccessToTableOrView test
hivanalejandro Feb 20, 2025
eb32aa8
feat(bigquery): Update app.js file with new samples
hivanalejandro Feb 20, 2025
0a5f5dd
feat(bigquery): Add revokeDatasetAccess sample
hivanalejandro Feb 20, 2025
6c40940
feat(bigquery): Add revokeDatasetAccess tests
hivanalejandro Feb 20, 2025
b600729
feat(bigquery): Update app.js file with new sample
hivanalejandro Feb 20, 2025
c7a1821
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Feb 20, 2025
db956a7
chore(bigquery): Update project structure
hivanalejandro Feb 20, 2025
b013b3c
fix(bigquery): Update samples and tests related to Dataset
hivanalejandro Feb 27, 2025
3f3d68a
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Feb 27, 2025
12bc84b
fix(bigquery): Update format issues
hivanalejandro Feb 27, 2025
b9e4bb1
fix(bigquery): Update samples and tests related to Table or View
hivanalejandro Mar 3, 2025
e6e886f
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Mar 3, 2025
ec3c990
fix(bigquery): Update samples and tests according to PR comments
hivanalejandro Mar 3, 2025
232b73b
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Mar 4, 2025
7c45228
fix(bigquery): Update samples and tests format issues
hivanalejandro Mar 5, 2025
f6a685d
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Mar 5, 2025
dbb3a4d
fix(bigquery): Update samples and tests according to PR comments
hivanalejandro Mar 5, 2025
5c04884
fix(bigquery): Standardized punctuation and style in all documents
hivanalejandro Mar 5, 2025
eed264a
fix(bigquery): Add punctuation to test and standardize format in all …
hivanalejandro Mar 6, 2025
20609bf
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Mar 6, 2025
f721a80
Merge branch 'main' into hivanalejandro/bigquery/create-sample/view-d…
hivanalejandro Mar 7, 2025
158f0b0
fix(bigquery): Update samples and tests according to PR comments
hivanalejandro Mar 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions bigquery/cloud-client/app.js
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',
resourceName: 'my_table',
});

await revokeTableOrViewAccess({
projectId,
datasetId: 'my_new_dataset',
resourceName: 'my_table',
memberToRevoke: 'group:[email protected]',
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};
26 changes: 26 additions & 0 deletions bigquery/cloud-client/package.json
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.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've seen, the author should be 'Google LLC', as in:

// Copyright 2020 Google LLC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was checking the package.json files of other samples and all of them have Google Inc. in the author part.

Copy link
Contributor

@eapl-gemugami eapl-gemugami Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These reference package.json files using Google Inc. might be outdated, see renderer/package.json#L6 from 2020 or retail/package.json#L4) from 2021

Also check b/169619920

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, you could ask your NodeJS lead for advice.

"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"
}
}
105 changes: 105 additions & 0 deletions bigquery/cloud-client/src/revokeTableOrViewAccess.js
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]')
* @param {string} [params.roleToRevoke='roles/bigquery.dataViewer'] - Optional. Specific role to revoke
* @returns {Promise<void>}
*/
async function revokeTableOrViewAccess({
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:',
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};
57 changes: 57 additions & 0 deletions bigquery/cloud-client/src/viewDatasetAccessPolicy.js
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
* @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);

// Get properties for an AccessEntry
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,
};
73 changes: 73 additions & 0 deletions bigquery/cloud-client/src/viewTableOrViewAccessPolicy.js
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
* @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,
};
Loading
Loading