-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowAccess.py
More file actions
executable file
·64 lines (50 loc) · 1.92 KB
/
Copy pathRowAccess.py
File metadata and controls
executable file
·64 lines (50 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/python
import datetime
import json
from google.cloud import bigquery
from re import search
'''*********
Apply row level security based on access list input
*********'''
#Setup variables
T=[]
date=datetime.datetime.now()
monthValue=int(date.strftime("%m"))
monthValue-=1
metricsDate="{}-{}-{}".format(date.strftime("%Y"),monthValue,"01")
#Change GCPDate for testing
GCPDate="{}-{}-{}".format(date.strftime("%Y"),date.strftime("%m"),"01")
project_id="<projectid>"
dataset="<dataset>"
tableAccessList="<tablewithaccesslist>"
dataSource=bigquery.Client(project=project_id)
print("Executing for date :{}".format(metricsDate))
metaQuery='Select * from `{}.{}.{}`'.format(project_id,dataset,tableAccessList)
job=dataSource.query(metaQuery)
result=job.result()
for row in result:
project=row[1]
dataSet=row[2]
table=row[3]
policyName=row[4]
groupList=None if row[5] is None else row[5].split(",")
usersList=None if row[7] is None else row[7].split(",")
access=row[8]
tempList=[]
print("Extracting access list:-project:{} dataSet:{} table:{} groups:{} users:{} access:{}"
.format(project,dataSet,table,groupList,usersList,access))
#Reading metadata
sqlQuery = "CREATE OR REPLACE ROW ACCESS POLICY {} ON `{}.{}.{}` GRANT TO".format(policyName,project,dataSet,table)
usersText=""
if(groupList != None and len(groupList)>=1):
for groupName in groupList:
usersText+=""""group:{}",""".format(groupName)
if(usersList != None and len(usersList)>=1):
sqlQuery = "CREATE OR REPLACE ROW ACCESS POLICY {} ON `{}.{}.{}` GRANT TO".format(policyName,project,dataSet,table)
for usersName in usersList:
usersText+=""""user:{}",""".format(usersName)
usersText=usersText[:-1]
sqlQuery+="""({}) FILTER USING({})""".format(usersText,access)
print(sqlQuery)
job=dataSource.query(sqlQuery)
job.result()