-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_audit_report.py
363 lines (265 loc) · 12.2 KB
/
google_audit_report.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""Online Scout Manager Interface.
Usage:
google_audit_report.py [-d | --debug] [-n | --no_email] [--email=<email>] [-w | --web] <apiid> <token> <section>
google_audit_report.py (-h | --help)
google_audit_report.py --version
Options:
-d,--debug Turn on debug output.
-n,--no_email Do not send email.
-w,--web Serve report on local web server.
--email=<email> Send to only this email address.
-h,--help Show this screen.
--version Show version.
"""
import os
import csv
import string
import subprocess
import logging
import dateutil.parser
import datetime
import functools
from docopt import docopt
from weekly_report import Reporter
import osm
from group import Group
from update import MAPPING
log = logging.getLogger(__name__)
GAMX = os.environ.get('GAMX_HOME', '/home/rjt/bin/gamx/gam')
GAM = os.environ.get('GAM_HOME', '/home/rjt/bin/gam/gam')
NOW = datetime.datetime.now(datetime.timezone.utc)
DEF_CACHE = "osm.cache"
DEF_CREDS = "osm.creds"
def fetch_osm_adults(osm, auth, sections):
group = Group(osm, auth, MAPPING.keys(), term=None)
contacts = []
def get(field, member_, section):
return member_["{}.{}".format(section, field)]
for section_ in sections:
for member in group.section_all_members(section_):
f1 = functools.partial(get, member_=member, section='contact_primary_member')
first = string.capwords(member['first_name'].strip())
last = string.capwords(member['last_name'].strip())
full_name = f'{first} {last}'
def is_valid_email(func, tag):
email_ = func(tag).strip()
return (len(email_) != 0
and (not email_.startswith('x ') and
func("{}_leaders".format(tag)) == "yes"))
# Add member data
email = f1('email1').strip()
if group.is_leader(member) and not is_valid_email(f1, 'email1'):
log.warning(f'{full_name} is a leader but does not have a member email address.')
if group.is_leader(member) and is_valid_email(f1, 'email1'):
key = '{}-{}-{}'.format(first.lower(), last.lower(), email.lower())
if key not in contacts:
contacts.append(dict(first=first, last=last, email=email.lower()))
return contacts
def fetch_user_report():
out = subprocess.run(args=[GAMX, 'print', 'users', 'allfields'], stdout=subprocess.PIPE, check=True,
universal_newlines=True)
return list(csv.DictReader(out.stdout.splitlines()))
def fetch_email_forwarding():
out = subprocess.run(args=[GAMX, 'all', 'users', 'print', 'forward'], stdout=subprocess.PIPE, check=True,
universal_newlines=True)
return list(csv.DictReader(out.stdout.splitlines()))
def fetch_email_forwarding_address_status():
out = subprocess.run(args=[GAMX, 'all', 'users', 'print', 'forwardingaddresses'], stdout=subprocess.PIPE, check=True,
universal_newlines=True)
return list(csv.DictReader(out.stdout.splitlines()))
def fetch_group_members():
out = subprocess.run(args=[GAMX, 'print', 'group-members'], stdout=subprocess.PIPE, check=True,
universal_newlines=True)
return list(csv.DictReader(out.stdout.splitlines()))
def fetch_admin_team_drives():
out = subprocess.run(args=[GAM, 'user', 'admin', 'print', 'teamdrives'], stdout=subprocess.PIPE, check=True,
universal_newlines=True)
return list(csv.DictReader(out.stdout.splitlines()))
def fetch_team_drive_acl(drive_id):
out = subprocess.run(args=[GAM, 'user', 'admin', 'show', 'drivefileacl', drive_id], stdout=subprocess.PIPE, check=True,
universal_newlines=True)
ret = []
current = dict()
for line in out.stdout.splitlines():
if line == '':
ret.append(current)
current = dict()
continue
try:
k, v = line.split(':')
current[k.strip()] = v.strip()
except ValueError:
continue
return ret
def intro(r):
r.title("Google Audit Report")
r.p("This is an automatic report ... ")
def _main(osm, auth, sections, no_email, email, http):
osm_adults = fetch_osm_adults(osm, auth, sections)
users = fetch_user_report()
osm_users_first_last = set([(_['first'], _['last']) for _ in osm_adults])
g_users_first_last = set([(_['name.givenName'], _['name.familyName']) for _ in users])
missing_in_g = osm_users_first_last - g_users_first_last
missing_in_g_with_email_addresses = [_ for _ in osm_adults if (_['first'], _['last']) in missing_in_g]
# remove duplicates
missing_in_g_with_email_addresses = [dict(t) for t in set([tuple(d.items()) for d in
missing_in_g_with_email_addresses])]
extras_in_g = g_users_first_last - osm_users_first_last
r = Reporter()
admin_team_drives = fetch_admin_team_drives()
forwarding = fetch_email_forwarding()
forwardingaddresses = fetch_email_forwarding_address_status()
group_members = fetch_group_members()
permission_group_members = [member for member in group_members
if member['group'].startswith('7th-lichfield-')]
role_group_members = [member for member in group_members
if not member['group'].startswith('7th-lichfield-')]
r.sub_title("All users")
r.p('This is a list of all of the user accounts in the domain. It should only list real people.')
r.t_start(["Name", "Email"])
for user in users:
r.t_row([user['name.fullName'], user['primaryEmail']])
r.t_end()
r.p("")
r.sub_title("Users that are forwarding their email")
r.p('This is a list of people that have forwarded their @7thlichfield.org.uk email address to '
'another account somewhere. These people will get their email but will not be able to access '
'team drives, unless they login to their @7thlichfield.org.uk account.')
r.t_start(["User", "Forwarding Address"])
forwarding_users = [user for user in forwarding if user['forwardEnabled'] == 'True']
for user in forwarding_users:
r.t_row([user['User'], user['forwardTo']])
r.t_end()
r.p("")
r.sub_title("Users that do not have forwarding and have not logged in for a month")
r.p('These are people that have not logged in for more than 30 days but do not have an active '
'email forwarder set up. These people will not be getting any email that is sent to their '
'@7thlichfield.org.uk account at all.')
r.p('If the Status == pending (and there is a Forwarding Address listed) it means that they have '
'been sent email requesting them to authorise forwarding but they have not responded to it.')
r.t_start(["User", "Email", "Last Login", "Forwarding Address", "Status"])
forwarding_users_email_addresses = [user['User'] for user in forwarding_users]
last_login_users = [user for user in users if
(user['lastLoginTime'] == 'Never' or
(NOW - dateutil.parser.parse(user['lastLoginTime'])).days > 30) and
user['primaryEmail'] not in forwarding_users_email_addresses]
forwardingaddresses_by_email = dict([(_['User'], _) for _ in forwardingaddresses])
for user in last_login_users:
forwarding_email = (forwardingaddresses_by_email[user['primaryEmail']]['forwardingEmail']
if forwardingaddresses_by_email.get(user['primaryEmail']) else "Not set")
verification_status = (forwardingaddresses_by_email[user['primaryEmail']]['verificationStatus']
if forwardingaddresses_by_email.get(user['primaryEmail']) else "Not set")
r.t_row([user['name.fullName'],
user['primaryEmail'],
user['lastLoginTime'],
forwarding_email,
verification_status])
r.t_end()
r.p("")
r.sub_title("Adults that are in OSM but do not have Google accounts")
r.p('This is a list of people that are listed as adults in OSN but do not appear to have '
'@7thlichfield.org.uk accounts.')
r.t_start(["First", "Last", "OSM Member Email"])
for user in missing_in_g_with_email_addresses:
r.t_row([user['first'], user['last'], user['email']])
r.t_end()
r.p("")
r.sub_title("Users with @7thlichfield.org.uk accounts that are not in OSM")
r.p('This is a list of people that have @7thlichfield.org.uk but do not appear to be in OSM.')
r.t_start(["First", "Last"])
for user in extras_in_g:
r.t_row([user[0], user[1]])
r.t_end()
r.p("")
r.sub_title("Groups used to apply permissions to Team Drives")
r.p('These groups are used to control who can access the Team Drives.')
r.t_start(["Group", "Member", "Role", "Status"])
last = ""
for member in permission_group_members:
r.t_row([member['group'] if member['group'] != last else "",
member['email'],
member['status'],
member['role']])
last = member['group']
r.t_end()
r.p("")
r.sub_title("Groups used as collaborative inboxes for roles")
r.p('These groups are the addresses of the inboxes used for roles within the group.')
r.t_start(["Group", "Member", "Role", "Status"])
last = ""
for member in role_group_members:
r.t_row([member['group'] if member['group'] != last else "",
member['email'],
member['status'],
member['role']])
last = member['group']
r.t_end()
r.p("")
r.sub_title("Team Drive access permissions")
r.p('This list shows the permissions set for the top level of the Team Drives. All files within the '
'Team Drive will have at least these permissions.')
r.t_start(["Team Drive", "Name", "Email", "Role", "Team Drive Role", "Deleted"])
role_map = {'organizer': 'full',
'writer': 'edit',
'reader': 'view',
'commenter': 'comment'}
for drive in admin_team_drives:
last = ""
for perm in fetch_team_drive_acl(drive['id']):
r.t_row([drive['name'] if drive['name'] != last else "",
perm['displayName'],
perm['emailAddress'],
perm['role'],
role_map.get(perm['role'], ''),
perm['deleted']])
last = drive['name']
r.t_end()
r.p("")
if no_email:
print(r.report())
elif email:
print("Sending to {}".format(email))
r.send([email, ],
'Google Audit Report')
if http:
print("Serving on http://localhost:8000/")
serve(r)
def serve(report):
import http.server
# noinspection PyClassHasNoInit
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(report.report(), 'UTF-8'))
server_address = ('', 8000)
httpd = http.server.HTTPServer(server_address, Handler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
if __name__ == '__main__':
args = docopt(__doc__, version='OSM 2.0')
if args['--debug']:
level = logging.DEBUG
else:
level = logging.WARN
logging.basicConfig(level=level)
log.debug("Debug On\n")
sections = None
if args['<section>']:
section = args['<section>']
if section == 'Group':
sections = Group.YP_SECTIONS + [Group.ADULT_SECTION, ]
else:
assert section in list(Group.SECTIONIDS.keys()) + ['Group'] + list(Group.SECTIONS_BY_TYPE.keys()), \
"section must be in {!r}.".format(list(Group.SECTIONIDS.keys()) + ['Group'])
sections = Group.SECTIONS_BY_TYPE[section] if section in Group.SECTIONS_BY_TYPE.keys() else [section, ]
auth = osm.Authorisor(args['<apiid>'], args['<token>'])
auth.load_from_file(open(DEF_CREDS, 'r'))
_main(osm, auth, sections,
args['--no_email'],
args['--email'],
args['--web'])