-
Notifications
You must be signed in to change notification settings - Fork 289
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
BigQuery CAI Scripts #394
Open
timothymanuel
wants to merge
6
commits into
master
Choose a base branch
from
cai_scripts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
BigQuery CAI Scripts #394
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b030da0
Create bq_table_all_readers.sql
timothymanuel a6b9814
Create bq_table_all_editors.sql
timothymanuel 0f082a2
Create bq_table_all_readers_schema.json
timothymanuel c9c316b
Create README.md
timothymanuel 40df60b
Update README.md
timothymanuel 5fe176d
Merge branch 'master' into cai_scripts
timothymanuel 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 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,21 @@ | ||
# Cloud Asset Inventory Scripts | ||
|
||
This folder contains scripts that can be run against Cloud Asset Inventory exports to BigQuery. Refer to the [public documentation](https://cloud.google.com/asset-inventory/docs/exporting-to-bigquery) for directions on setting up export to BigQuery. It is recommended to perform this export as part of a schedule. The tables would be partitioned by either `read-time` or `request-time`. In the attached scripts, the tables are assumed to be partitioned by `read-time`. | ||
|
||
The schema for the BigQuery tables generated from this export is given in the section [here](https://cloud.google.com/asset-inventory/docs/exporting-to-bigquery#bigquery-schema). | ||
|
||
Here are the scripts that are provided. | ||
|
||
### [BigQuery Table Readers](./bq_table_all_readers.sql) | ||
|
||
This script will help Data Stewards or Platform Owners determine the which are the IAM Principals (groups, user or service accounts) that can read data from a BigQuery table. | ||
|
||
The access to the principal could be applied at any level of the resource hierarchy - Org, Folder, Project or Dataset. | ||
|
||
|
||
|
||
### [BigQuery Table Editors](./bq_table_all_editors.sql) | ||
|
||
This script will help Data Stewards or Platform Owners determine the which are the IAM Principals (groups, user or service accounts) that can edit/write data to a BigQuery table. | ||
|
||
The access to the principal could be applied at any level of the resource hierarchy - Org, Folder, Project or Dataset. |
This file contains 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,98 @@ | ||
/* | ||
* Copyright 2023 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. | ||
*/ | ||
/* | ||
* This script creates a table that maps a BigQuery Table to the IAM Principals | ||
* that can edit data in the BigQuery table. | ||
* | ||
* The IAM Principal could be assigned a role at the Table level, the Dataset level | ||
* or any level of Project, Folder or Org which are parents of the Dataset. | ||
* | ||
* This script assumes the following: | ||
* 1. The Cloud Asset Inventory has been exported to tables in BigQuery under | ||
* project - <CAI_EXPORT_PROJECT> and dataset - <CAI_EXPORT_DATASET> | ||
* 2. The user has permissions to query <CAI_EXPORT_PROJECT>.<CAI_EXPORT_DATASET> | ||
* 3. All BigQuery permissions is handled via BigQuery predefined roles | ||
* 4. The BigQuery roles for writing data to a Table are | ||
* a. roles/bigquery.dataEditor | ||
* b. roles/bigquery.dataOwner | ||
* c. roles/bigquery.studioAdmin | ||
* d. roles/bigquery.admin | ||
* 5. Replace <RESOURCE_TABLE> with the Resource table name from the CAI Export | ||
* 6. Replace <IAM_POLICY_TABLE> with the IAM Policy table name from the CAI Export | ||
* | ||
* The schema of the table - `bigquery_table_all_editors` is given in ./schema/table_all_readers_schema.json | ||
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. the json file name does not match |
||
*/ | ||
DECLARE read_date STRING DEFAULT "2023-12-01"; | ||
|
||
CREATE SCHEMA IF NOT EXISTS cai_analysis; | ||
|
||
CREATE | ||
OR REPLACE TABLE cai_analysis.bigquery_table_all_editors AS WITH bq_table_with_parents AS ( | ||
SELECT | ||
name as bq_table, | ||
ARRAY_CONCAT( | ||
[name], | ||
[resource.parent], | ||
ARRAY( | ||
SELECT | ||
CONCAT('//cloudresourcemanager.googleapis.com/', a) | ||
FROM | ||
UNNEST(ancestors) as a | ||
) | ||
) as parent_array | ||
FROM | ||
`<CAI_EXPORT_PROJECT>.<CAI_EXPORT_DATASET>.<RESOURCE_TABLE>` | ||
WHERE | ||
TIMESTAMP_TRUNC(readTime, DAY) = TIMESTAMP(read_date) | ||
AND asset_type = 'bigquery.googleapis.com/Table' | ||
), | ||
bq_editors AS ( | ||
SELECT | ||
a.name, | ||
a.asset_type, | ||
b.role, | ||
ARRAY_CONCAT_AGG(b.members) as members, | ||
FROM | ||
`<CAI_EXPORT_PROJECT>.<CAI_EXPORT_DATASET>.<IAM_POLICY_TABLE>` a | ||
INNER JOIN UNNEST(iam_policy.bindings) as b | ||
WHERE | ||
TIMESTAMP_TRUNC(readTime, DAY) = TIMESTAMP(read_date) | ||
AND b.role IN ( | ||
'roles/bigquery.dataEditor', | ||
'roles/bigquery.dataOwner', | ||
'roles/bigquery.studioAdmin', | ||
'roles/bigquery.admin' | ||
) | ||
GROUP BY | ||
1, | ||
2, | ||
3 | ||
) | ||
SELECT | ||
bqt.bq_table as table_name, | ||
ARRAY_AGG( | ||
STRUCT( | ||
bq_editors.name as parent_name, | ||
bq_editors.role, | ||
bq_editors.members | ||
) | ||
) as inherited_editors | ||
FROM | ||
bq_table_with_parents bqt | ||
INNER JOIN UNNEST(parent_array) as parents | ||
INNER JOIN bq_editors ON parents = bq_editors.name | ||
GROUP BY | ||
1; |
This file contains 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,100 @@ | ||
/* | ||
* Copyright 2023 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. | ||
*/ | ||
/* | ||
* This script creates a table that maps a BigQuery Table to the IAM Principals | ||
* that can read data from the BigQuery table. | ||
* | ||
* The IAM Principal could be assigned a role at the Table level, the Dataset level | ||
* or any level of Project, Folder or Org which are parents of the Dataset. | ||
* | ||
* This script assumes the following: | ||
* 1. The Cloud Asset Inventory has been exported to tables in BigQuery under | ||
* project - <CAI_EXPORT_PROJECT> and dataset - <CAI_EXPORT_DATASET> | ||
* 2. The user has permissions to query <CAI_EXPORT_PROJECT>.<CAI_EXPORT_DATASET> | ||
* 3. All BigQuery permissions is handled via BigQuery predefined roles | ||
* 4. The BigQuery roles for reading data from a Table are | ||
* a. roles/bigquery.dataEditor | ||
* b. roles/bigquery.dataViewer | ||
* c. roles/bigquery.dataOwner | ||
* d. roles/bigquery.studioAdmin | ||
* e. roles/bigquery.admin | ||
* 5. Replace <RESOURCE_TABLE> with the Resource table name from the CAI Export | ||
* 6. Replace <IAM_POLICY_TABLE> with the IAM Policy table name from the CAI Export | ||
* | ||
* The schema of the table - `bigquery_table_all_readers` is given in ./schema/table_all_readers_schema.json | ||
*/ | ||
DECLARE read_date STRING DEFAULT "2023-12-01"; | ||
|
||
CREATE SCHEMA IF NOT EXISTS cai_analysis; | ||
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. These two scripts are almost identical, does it make sense to make them DRY? |
||
|
||
CREATE | ||
OR REPLACE TABLE cai_analysis.bigquery_table_all_readers AS WITH bq_table_with_parents AS ( | ||
SELECT | ||
name as bq_table, | ||
ARRAY_CONCAT( | ||
[name], | ||
[resource.parent], | ||
ARRAY( | ||
SELECT | ||
CONCAT('//cloudresourcemanager.googleapis.com/', a) | ||
FROM | ||
UNNEST(ancestors) as a | ||
) | ||
) as parent_array | ||
FROM | ||
`<CAI_EXPORT_PROJECT>.<CAI_EXPORT_DATASET>.<RESOURCE_TABLE>` | ||
WHERE | ||
TIMESTAMP_TRUNC(readTime, DAY) = TIMESTAMP(read_date) | ||
AND asset_type = 'bigquery.googleapis.com/Table' | ||
), | ||
bq_readers AS ( | ||
SELECT | ||
a.name, | ||
a.asset_type, | ||
b.role, | ||
ARRAY_CONCAT_AGG(b.members) as members, | ||
FROM | ||
`<CAI_EXPORT_PROJECT>.<CAI_EXPORT_DATASET>.<IAM_POLICY_TABLE>` a | ||
INNER JOIN UNNEST(iam_policy.bindings) as b | ||
WHERE | ||
TIMESTAMP_TRUNC(readTime, DAY) = TIMESTAMP(read_date) | ||
AND b.role IN ( | ||
'roles/bigquery.dataEditor', | ||
'roles/bigquery.dataViewer', | ||
'roles/bigquery.dataOwner', | ||
'roles/bigquery.studioAdmin', | ||
'roles/bigquery.admin' | ||
) | ||
GROUP BY | ||
1, | ||
2, | ||
3 | ||
) | ||
SELECT | ||
bqt.bq_table as table_name, | ||
ARRAY_AGG( | ||
STRUCT( | ||
bq_readers.name as parent_name, | ||
bq_readers.role, | ||
bq_readers.members | ||
) | ||
) as inherited_readers | ||
FROM | ||
bq_table_with_parents bqt | ||
INNER JOIN UNNEST(parent_array) as parents | ||
INNER JOIN bq_readers ON parents = bq_readers.name | ||
GROUP BY | ||
1; |
33 changes: 33 additions & 0 deletions
33
scripts/cloud_asset_inventory/schema/bq_table_all_readers_schema.json
This file contains 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,33 @@ | ||
[ | ||
{ | ||
"name": "table_name", | ||
"type": "STRING", | ||
"mode": "NULLABLE", | ||
"description": "Name of BigQuery Table" | ||
}, | ||
{ | ||
"name": "inherited_readers", | ||
"type": "RECORD", | ||
"mode": "REPEATED", | ||
"fields": [ | ||
{ | ||
"name": "parent_name", | ||
"type": "STRING", | ||
"mode": "NULLABLE", | ||
"description": "The name of dataset, project, folder or org which is ancestor of the Table" | ||
}, | ||
{ | ||
"name": "role", | ||
"type": "STRING", | ||
"mode": "NULLABLE", | ||
"description": "The BigQuery predefined role applied on the parent_name resource" | ||
}, | ||
{ | ||
"name": "members", | ||
"type": "STRING", | ||
"mode": "REPEATED", | ||
"description": "IAM Principals assigned with the role on the parent_name resource" | ||
} | ||
] | ||
} | ||
] |
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.
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.
typo