-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathaudit_log.py
More file actions
56 lines (47 loc) · 1.86 KB
/
audit_log.py
File metadata and controls
56 lines (47 loc) · 1.86 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
56
import asyncio
import json
from cloudify_cli.logger import get_global_json_output
def stream_logs(creator_name,
execution_id,
since,
timeout,
logger,
client):
loop = asyncio.get_event_loop()
loop.run_until_complete(_stream_logs(creator_name,
execution_id,
since,
timeout,
logger,
client))
loop.close()
async def _stream_logs(creator_name,
execution_id,
since,
timeout,
logger,
client):
logger.info('Streaming audit log entries...')
response = await client.auditlog.stream(timeout=timeout,
creator_name=creator_name,
execution_id=execution_id,
since=since)
async for data in response.content:
for audit_log in _streamed_audit_log(data):
if get_global_json_output():
print(audit_log)
else:
print(_format_audit_log(audit_log))
def _streamed_audit_log(data):
line = data.strip().decode(errors='ignore')
if line:
yield json.loads(line)
def _format_audit_log(data):
result = f"[{data['created_at']}]"
if 'creator_name' in data and data['creator_name']:
result = f"{result} user {data['creator_name']}"
if 'execution_id' in data and data['execution_id']:
result = f"{result} execution {data['execution_id']}"
result = f"{result} {data['operation'].upper()}D"
result = f"{result} {data['ref_table']} {data['ref_id']}"
return result