How to get policy tag if bigquery attribute from information schema

Viewed 745

We have used policy tag in bigquery for column level security. ( https://cloud.google.com/bigquery/docs/best-practices-policy-tags) . Now we want to check the list of tables and attributes which have the policy tag. Is there any way in bigquery to get it using INFORMATION_SCHEMA? Or any other approach pragmatically to get the attribute tagged with some policy tag?

1 Answers

You can try this solution, which will give you list of tables and columns where PolicyTag is used:

Write Table-List into a File:

DATASET="dataset-name"
bq ls --max_results=10000 ${DATASET} | awk '{ print $1 }' | sed '1,2d' > table_list.txt

Shell Script:

#!/bin/bash

DATASET="dataset-name"

echo "------------------------------"
echo "TableName      ColumnName"
echo "------------------------------"
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'`"
        echo "${TABLE}        ${COLUMN}"
    fi
done < table_list.txt
Related