fix getting logs from cloudflare

This commit is contained in:
Jakub Sokołowski 2018-11-19 15:04:08 +01:00
parent b1443b1dce
commit b3560bd042
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020

View File

@ -3,38 +3,34 @@ import os
import json
import CloudFlare
#email = 'jakub@status.im'
#token = os.environ['CF_TOKEN']
#
#cf = CloudFlare.CloudFlare(email, token)
#
email = 'jakub@status.im'
token = os.environ['CF_TOKEN']
cf = CloudFlare.CloudFlare(email, token)
#zones = cf.zones.get(params = {'per_page':100})
#zone_id = zones[0]['id']
#zone_name = zones[0]['id']
#print('Zones:', [z['name'] for z in zones])
#
# BROKEN: https://github.com/cloudflare/python-cloudflare/issues/50
## https://api.cloudflare.com/#audit-logs-list-user-audit-logs
#user_id = cf.user.get()['id']
#logs = cf._base.call_with_auth('GET',
#logs = cf.user.audit_logs.get('status.im',
# params={
# 'actor.email': 'jakub@status.im',
# 'since': '2018-06-01',
# 'per_page': 500,
# 'order': 'when',
# 'direction': 'desc',
# }
#)
with open('/tmp/audit_logs.json') as f:
logs = json.load(f)['result']
print('Found:', len(logs))
for l in logs:
if l['action']['type'] in ['logout', 'login']:
logs = cf.organizations.audit_logs.get('113ef908d19933ef327f079a3def53fc',
params={
'since': '2018-01-01',
'per_page': 5000,
'order': 'when',
'direction': 'asc',
'zone.name': 'status.im'
}
)
for log in logs:
if log['action']['type'] not in ['add', 'delete']:
continue
if l['metadata'].get('zone_name') != 'status.im':
continue
if l['resource'].get('type') != 'DNS_record':
continue
print(l['when'], l['action']['info'])
print('{:30} {:20} {:12} {:>10} {:>7} {:30} {}'.format(
log['when'],
log['actor'].get('email', log['metadata'].get('acted_on_behalf_of')),
log['metadata'].get('zone_name'),
log['action'].get('type'),
log['metadata'].get('type') or '',
log['metadata'].get('name') or '',
log['metadata'].get('content') or ''
))