-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathget_user_role_changes.py
executable file
·191 lines (162 loc) · 6.86 KB
/
get_user_role_changes.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
#!/usr/bin/env python3
# this script retrieves all user role audit records for a given date range
# the endpoint for audit records is https://api.pagerduty.com/audit/records
import argparse
import csv
import json
import pdpyras
from datetime import datetime, timezone
from dateutil import parser, relativedelta
from tabulate import tabulate
user_roles = {
'owner': 'Owner',
'admin': 'Global Admin',
'user': 'Manager',
'limited_user': 'Responder',
'observer': 'Observer',
'restricted_access': 'Restricted Access',
'read_only_limited_user': 'Limited Stakeholder',
'read_only_user': 'Stakeholder',
'none': 'None'
}
user_role_to_tier = {
user_roles['owner']: 'Full User',
user_roles['admin']: 'Full User',
user_roles['user']: 'Full User',
user_roles['limited_user']: 'Full User',
user_roles['observer']: 'Full User',
user_roles['restricted_access']: 'Full User',
user_roles['read_only_limited_user']: 'Stakeholder',
user_roles['read_only_user']: 'Stakeholder',
user_roles['none']: 'None'
}
actor_types = {
'user_reference': 'User',
'app_reference': 'App',
'api_key_reference': 'API Key',
}
def get_api_path(user_id):
return f'users/{user_id}/audit/records' if user_id else 'audit/records'
def get_api_params(since, until, user_id):
params={'since': since, 'until': until}
if not user_id:
params['root_resource_types[]'] = 'users'
return params
def print_changes(changes, tier_changes):
header = header_row(tier_changes)
print(tabulate(header + changes, tablefmt='grid'))
def write_changes_to_csv(changes, tier_changes, filename):
header = header_row(tier_changes)
with open(filename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=header[0].keys())
writer.writerows(header + changes)
def header_row(tier_changes):
header = {
'date': 'Date',
'id': 'User ID',
'summary': 'User Name',
'before_value': 'Role Before',
'value': 'Role After',
'actor_id': 'Actor ID',
'actor_type': 'Actor Type',
'actor_summary': 'Actor Summary'
}
if tier_changes:
header.update({'before_value': 'Role Tier Before', 'value': 'Role Tier After'})
return [header]
def get_record_actor(record):
actors = record.get('actors', [])
if not len(actors):
return {
'actor_type': '',
'actor_id': '',
'actor_summary': ''
}
actor = actors[0]
return {
'actor_type': actor_types[actor['type']],
'actor_id': actor['id'],
'actor_summary': actor.get('summary', '')
}
def get_role_changes(record, only_updates):
if only_updates and record['action'] != 'update':
return []
# `details` and `fields` can both be null according to the API docs.
field_changes = record.get('details', {}).get('fields', [])
role_changes = filter(lambda fc: fc['name'] == 'role', field_changes)
def format_role_change(role_change):
role_change = {
'id': record['root_resource']['id'],
'summary': record['root_resource'].get('summary', ''),
'value': user_roles[role_change.get('value', 'none')],
'before_value': user_roles[role_change.get('before_value', 'none')],
'date': record['execution_time']
}
role_change.update(get_record_actor(record))
return role_change
return list(map(format_role_change, role_changes))
def get_role_tier_changes(role_changes):
tier_changes = []
for role_change in role_changes:
role_change['value'] = user_role_to_tier[role_change['value']]
role_change['before_value'] = user_role_to_tier[role_change['before_value']]
if role_change['value'] != role_change['before_value']:
tier_changes.append(role_change)
return tier_changes
def chunk_date_range(args):
# Mirror the default of the audit APIs, get records for the last 24 hours
since, until = args.since, args.until
if not since or not until:
now = datetime.now(timezone.utc)
yesterday = now - relativedelta.relativedelta(hours=+24)
yield (datetime.isoformat(yesterday), datetime.isoformat(now))
return
since = parser.isoparse(since)
until = parser.isoparse(until)
if since > until:
raise 'Invalid date range'
# Audit API requests have a date range limit of 31 days, so we
# split the requested date range into 30 day chunks
while True:
next_since = since + relativedelta.relativedelta(days=+30)
if next_since < until:
yield (datetime.isoformat(since), datetime.isoformat(next_since))
since = next_since
else:
yield (datetime.isoformat(since), datetime.isoformat(until))
return
def main(args, session):
user_id, tier_changes = args.user_id, args.tier_changes
try:
role_changes = []
chunked_date_range = chunk_date_range(args)
for since, until in chunked_date_range:
for record in session.iter_cursor(get_api_path(user_id), params=get_api_params(since, until, user_id)):
if args.show_all:
print(json.dumps(record))
record_role_changes = get_role_changes(record, args.only_updates)
role_changes += record_role_changes
changes = get_role_tier_changes(role_changes) if tier_changes else role_changes
if len(changes):
changes = sorted(changes, key=lambda rc: rc['date'])
print_changes(changes, tier_changes)
if args.filename:
write_changes_to_csv(changes, tier_changes, args.filename)
else:
print(f'No {"tier" if tier_changes else "role"} changes found.')
except pdpyras.PDClientError as e:
print('Could not get user role change audit records')
raise e
if __name__ == '__main__':
ap = argparse.ArgumentParser(description='Prints all user role or tier changes between the given dates')
ap.add_argument('-k', '--api-key', required=True, help='REST API key')
ap.add_argument('-s', '--since', required=False, help='Start of date range to search')
ap.add_argument('-u', '--until', required=False, help='End of date range to search')
ap.add_argument('-i', '--user-id', required=False, help='Filter results to a single user ID')
ap.add_argument('-o', '--only-updates', action='store_true', help='Exclude user creates and deletes from role change results')
ap.add_argument('-t', '--tier-changes', action='store_true', help='Get user role tier changes')
ap.add_argument('-a', '--show-all', action='store_true', help='Prints all fetched user records in JSON format')
ap.add_argument('-f', '--filename', required=False, help='Write results to a CSV file')
args = ap.parse_args()
session = pdpyras.APISession(args.api_key)
main(args, session)