-
Notifications
You must be signed in to change notification settings - Fork 322
adding code for bigquery policy tag extractor #398
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
Open
karcot1
wants to merge
6
commits into
GoogleCloudPlatform:master
Choose a base branch
from
karcot1:policy_tag_extractor
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb7bc79
adding code for bigquery policy tag extractor
c7ef2ef
Moving files to the scripts directory
18b490b
Apply suggestions from code review
karcot1 e83fbd0
Apply suggestions from code review
karcot1 bc3f6c7
making anti pattern opitmization script generic for any input table (…
franklinWhaite 10954ef
Documenting limitation of only simple column types to README
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,23 @@ | ||
# BigQuery Policy Tag Extractor | ||
|
||
## Introduction | ||
This repo contains a bash script for extracting BigQuery policy tag information from a given dataset. The script will review all the tables in a dataset, and output the table name, column, name, and policy tag ID in a CSV format. | ||
|
||
## Instructions for use | ||
The simplest way to execute this script is to run it directly in Cloud Shell, but if needed it can be executed as part of a larger CI/CD pipeline or process. | ||
|
||
Before using, make sure to update the bash script with the dataset that needs to be reviewed. | ||
|
||
To exceute in Cloud Shell: | ||
1. Start a new session in the GCP project where your BigQuery data resides | ||
2. Open Cloud Shell | ||
3. Upload policy_tag_export.sh to the Cloud Shell environment | ||
4. Execute the script by running "bash policy_tag_export.sh" | ||
5. List the resources in Cloud Shell (ls) and verify that a file called "policy_tags.csv" was created | ||
6. Download the file | ||
karcot1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
## Considerations | ||
* Ensure either you or the service account executing the bash script has the bigquery.metadataViewer role to access the required level of information. | ||
* The extractor can identify specific policy tags on columns, but is limited to the information available to the bq command line tool. In it's current state, this is the full policy tag identifier: | ||
|
||
projects/<PROJECT_ID>/locations/<LOCATION>/taxonomies/<TAXONOMY_ID>/policyTags/<TAG_ID> |
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,20 @@ | ||
#!/bin/bash | ||
karcot1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
DATASET="" | ||
karcot1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
#write all tables in a dataset to a reference TXT file | ||
bq ls --max_results=10000 ${DATASET} | awk '{ print $1 }' | sed '1,2d' > table_list.txt | ||
karcot1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
#loop through each table and export policy tags (if any) to a CSV | ||
echo "Writing to CSV..." | ||
while IFS= read -r TABLE; do | ||
TAG_COUNT="`bq show --schema ${DATASET}.${TABLE} | grep "policyTags" | wc -l`" | ||
|
||
if [ "${TAG_COUNT}" -ge 1 ] | ||
then | ||
COLUMN=`bq show --format=prettyjson ${DATASET}.${TABLE} | jq '.schema.fields[] | select(.policyTags | length>=1)' | jq '.name'` | ||
TAG_ID=`bq show --format=prettyjson ${DATASET}.${TABLE} | jq '.schema.fields[] | select(.policyTags | length>=1)' | jq '.policyTags.names[]'` | ||
karcot1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
echo ${TABLE},${COLUMN},${TAG_ID} | tr -d '"' | ||
fi | ||
done < table_list.txt >> policy_tags.csv | ||
echo "Done." | ||
karcot1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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.