Skip to content
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

adding code for bigquery policy tag extractor #398

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
23 changes: 23 additions & 0 deletions scripts/policy_tag_extractor/README.md
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.
karcot1 marked this conversation as resolved.
Show resolved Hide resolved

## 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.
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>
20 changes: 20 additions & 0 deletions scripts/policy_tag_extractor/policy_tag_export.sh
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.
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.
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.
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.
Show resolved Hide resolved