Created
November 15, 2019 00:03
-
-
Save e0x70i/5ba0b15dd27adfb0dee6a11c7d49bca0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import json | |
import argparse | |
def csvdict_from_json(o365_report): | |
rows = [] | |
for entry in o365_report: | |
for applied_policy in entry["appliedConditionalAccessPolicies"]: | |
row = { | |
'policyName' : applied_policy['displayName'], | |
'enforcedGrantControls': ', '.join(applied_policy['enforcedGrantControls']), | |
'status': applied_policy['result'], | |
'userId': entry['userId'], | |
'userPrincipalName': entry['userPrincipalName'], | |
'clientAppUsed': entry['clientAppUsed'], | |
'ipAddress': entry['ipAddress'], | |
'resourceDisplayName': entry['resourceDisplayName'], | |
'city': entry['location']['city'], | |
'state': entry['location']['state'], | |
'latitude': entry['location']['geoCoordinates']['latitude'], | |
'longitude': entry['location']['geoCoordinates']['longitude'], | |
} | |
for key,value in entry['deviceDetail'].items(): | |
row[key] = value | |
rows.append(row) | |
return rows | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Create a CSV of Base Conditional Access Policy Enforcement from O365 JSON Export') | |
parser.add_argument('-json', metavar='json', type=str, help='exported sign-ins json file', required=True) | |
parser.add_argument('-o', metavar='output', type=str, help='output csv file', required=True) | |
args = parser.parse_args() | |
with open(args.json) as json_file: | |
o365_report = json.load(json_file) | |
rows = csvdict_from_json(o365_report) | |
with open(args.o, 'w') as out: | |
w = csv.DictWriter(out,delimiter=',', lineterminator='\n', fieldnames=rows[0].keys()) | |
w.writeheader() | |
w.writerows(rows) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment