Skip to content

Commit 7062f9e

Browse files
saurabhbangadnathan-vu
authored andcommitted
Added example script for retrieving all audit events across an OCI Tenancy (#44)
* Retrieve all audit events across an OCI Tenancy This script retrieves all audit logs across an OCI Tenancy for a defined timespan. This script relies on OCI config as per the format defined at https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm This script will work at a tenancy level only. The config file should have the following contents User OCID RSA private key in PEM format Fingerprint Tenancy OCID * Changes based on review and file name. Accommodated the changes suggested by @nathan-vu The following is a summary of all the changes: -file name from RetrieveAuditEvents.py to retrieve_audit_events.py -instead of hard coding to start_time and end_time, they receive values on execution -oci pagination is being used instead of custom definition -removed the len(range(list)) for -get_regions() and get_compartments() now accept identity as their parameter -region is updated with set_region() -changes to variables name; they use snake_casing -edited comments * Changes based on 2nd review on 20180215 The following were the changes made in the latest version: -start_time and end_time do not do any str operations and rely on datetime.datetime -logs are retrieved only for the timespan of last 5 days instead of a month; this would be a better experience considering the time retrieve results -changes to get_compartments; it will accept tenancy_id as its parameter and it will also be the added to the list even before ListCompartments api call is made -unnecessary initializations were removed -change of comment style for the description of functions -parameter 'r' was removed from get_regions() which was originally put to demonstrate the region being used Changes not made: -No changes were made to the retrieval method i.e. it is still eagerly loading all the results * list_of_audit_events relies on extend * Updates to comments Updated the following sections' comments: -get_audit_events * Change of order for import modules
1 parent 2e4f73f commit 7062f9e

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

examples/retrieve_audit_events.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
4+
5+
# This script retrieves all audit logs across an OCI Tenancy.
6+
# for a timespan defined by start_time and end_time.
7+
# This sample script retrieves Audit events for last 5 days.
8+
# This script will work at a tenancy level only.
9+
10+
import datetime
11+
import oci
12+
13+
14+
def get_regions(identity):
15+
'''
16+
To retrieve the list of all available regions.
17+
'''
18+
list_of_regions = []
19+
list_regions_response = identity.list_regions()
20+
for r in list_regions_response.data:
21+
list_of_regions.append(r.name)
22+
return list_of_regions
23+
24+
25+
def get_compartments(identity, tenancy_id):
26+
'''
27+
Retrieve the list of compartments under the tenancy.
28+
'''
29+
compartment_ocids = []
30+
# Store tenancy id as the first compartment
31+
compartment_ocids.append(tenancy_id)
32+
list_compartments_response = oci.pagination.list_call_get_all_results(
33+
identity.list_compartments,
34+
compartment_id=tenancy_id).data
35+
for c in list_compartments_response:
36+
compartment_ocids.append(c.id)
37+
return compartment_ocids
38+
39+
40+
def get_audit_events(audit, compartment_ocids, start_time, end_time):
41+
'''
42+
Get events iteratively for each compartment defined in 'compartments_ocids'
43+
for the region defined in 'audit'.
44+
This method eagerly loads all audit records in the time range and it does
45+
have performance implications of lot of audit records.
46+
Ideally, the generator method in oci.pagination should be used to lazily
47+
load results.
48+
'''
49+
list_of_audit_events = []
50+
for c in compartment_ocids:
51+
list_events_response = oci.pagination.list_call_get_all_results(
52+
audit.list_events,
53+
compartment_id=c,
54+
start_time=start_time,
55+
end_time=end_time).data
56+
57+
# Results for a compartment 'c' for a region defined
58+
# in 'audit' object.
59+
list_of_audit_events.extend(list_events_response)
60+
return list_of_audit_events
61+
62+
63+
# Setting configuration
64+
# Default path for configuration file is "~/.oci/config"
65+
config = oci.config.from_file()
66+
tenancy_id = config["tenancy"]
67+
68+
# Initiate the client with the locally available config.
69+
identity = oci.identity.IdentityClient(config)
70+
71+
# Timespan defined by variables start_time and end_time(today).
72+
# ListEvents expects timestamps into RFC3339 format.
73+
# For the purposes of sample script, logs of last 5 days.
74+
end_time = datetime.datetime.utcnow()
75+
start_time = end_time + datetime.timedelta(days=-5)
76+
77+
# This array will be used to store the list of available regions.
78+
regions = get_regions(identity)
79+
80+
# This array will be used to store the list of compartments in the tenancy.
81+
compartments = get_compartments(identity, tenancy_id)
82+
83+
audit = oci.audit.audit_client.AuditClient(config)
84+
85+
# For each region get the logs for each compartment.
86+
for r in regions:
87+
# Intialize with a region value.
88+
audit.base_client.set_region(r)
89+
# To separate results by region use print here.
90+
audit_events = get_audit_events(
91+
audit,
92+
compartments,
93+
start_time,
94+
end_time)
95+
96+
# Results for a region 'r' for each compartment.
97+
if audit_events:
98+
print audit_events

0 commit comments

Comments
 (0)