-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGetNonDomainDriveACLs.py
More file actions
executable file
·55 lines (49 loc) · 2.68 KB
/
GetNonDomainDriveACLs.py
File metadata and controls
executable file
·55 lines (49 loc) · 2.68 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
#!/usr/bin/env python
"""
# Purpose: For a Google Drive User(s), delete all drive file ACls for files shared outside of a list of specified domains
# Note: This script can use basic GAM: https://github.com/jay0lee/GAM or advanced GAM: https://github.com/taers232c/GAMADV-X
# Usage:
# 1: Get ACLS for all files, if you don't want all users, replace all users with your user selection in the command below
# $ Example, Basic GAM: gam all users print filelist id title permissions > filelistperms.csv
# $ Example, advanced GAM: gam config auto_batch_min 1 redirect csv ./filelistperms.csv multiprocess all users print filelist id title permissions
# 2: From that list of ACLs, output a CSV file with headers "Owner,driveFileId,driveFileTitle,permissionId,role,type,emailAddress"
# that lists the driveFileIds and permissionIds for all ACls except those from the specified domains.
# (n.b., role, type, emailAddress and title are not used in the next step, they are included for documentation purposes)
# $ python GetNonDomainDriveACLs.py filelistperms.csv deleteperms.csv
# 3: Inspect deleteperms.csv, verify that it makes sense and then proceed
# 4: Delete the ACLS
# $ gam csv deleteperms.csv gam user "~Owner" delete drivefileacl "~driveFileId" "~permissionId"
"""
import csv
import re
import sys
email_n_address = re.compile(r"permissions.(\d+).emailAddress")
# Substitute your domain(s) in the list below, e.g., domainList = ['domain.com',] domainList = ['domain1.com', 'domain2.com',]
domainList = []
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[2], 'wb')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['Owner', 'driveFileId', 'driveFileTitle', 'permissionId', 'role', 'type', 'emailAddress'], lineterminator='\n')
outputCSV.writeheader()
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'rb')
else:
inputFile = sys.stdin
for row in csv.DictReader(inputFile):
for k, v in row.iteritems():
mg = email_n_address.match(k)
if mg and v:
perm_group = mg.group(1)
if row['permissions.{0}.domain'.format(perm_group)] not in domainList:
outputCSV.writerow({'Owner': row['Owner'],
'driveFileId': row['id'],
'driveFileTitle': row['title'],
'permissionId': 'id:{0}'.format(row['permissions.{0}.id'.format(perm_group)]),
'role': row['permissions.{0}.role'.format(perm_group)],
'type': row['permissions.{0}.type'.format(perm_group)],
'emailAddress': v})
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()