-
Notifications
You must be signed in to change notification settings - Fork 305
Retrieve all audit events across an OCI Tenancy #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed17a05
Retrieve all audit events across an OCI Tenancy
saurabhbangad 1b1f861
Changes based on review and file name.
saurabhbangad 0cab943
Changes based on 2nd review on 20180215
saurabhbangad 0e3e1e0
list_of_audit_events relies on extend
saurabhbangad 6521506
Updates to comments
saurabhbangad c5fc951
Change of order for import modules
saurabhbangad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #!/usr/bin/env python | ||
| # coding: utf-8 | ||
| # Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. | ||
|
|
||
| # This script retrieves all audit logs across an OCI Tenancy. | ||
| # for a timespan defined by start_time and end_time. | ||
| # This sample script retrieves Audit events for last 5 days. | ||
| # This script will work at a tenancy level only. | ||
|
|
||
| import datetime | ||
| import oci | ||
|
|
||
|
|
||
| def get_regions(identity): | ||
| ''' | ||
| To retrieve the list of all available regions. | ||
| ''' | ||
| list_of_regions = [] | ||
| list_regions_response = identity.list_regions() | ||
| for r in list_regions_response.data: | ||
| list_of_regions.append(r.name) | ||
| return list_of_regions | ||
|
|
||
|
|
||
| def get_compartments(identity, tenancy_id): | ||
| ''' | ||
| Retrieve the list of compartments under the tenancy. | ||
| ''' | ||
| compartment_ocids = [] | ||
| # Store tenancy id as the first compartment | ||
| compartment_ocids.append(tenancy_id) | ||
| list_compartments_response = oci.pagination.list_call_get_all_results( | ||
| identity.list_compartments, | ||
| compartment_id=tenancy_id).data | ||
| for c in list_compartments_response: | ||
| compartment_ocids.append(c.id) | ||
| return compartment_ocids | ||
|
|
||
|
|
||
| def get_audit_events(audit, compartment_ocids, start_time, end_time): | ||
| ''' | ||
| Get events iteratively for each compartment defined in 'compartments_ocids' | ||
| for the region defined in 'audit'. | ||
| This method eagerly loads all audit records in the time range and it does | ||
| have performance implications of lot of audit records. | ||
| Ideally, the generator method in oci.pagination should be used to lazily | ||
| load results. | ||
| ''' | ||
| list_of_audit_events = [] | ||
| for c in compartment_ocids: | ||
| list_events_response = oci.pagination.list_call_get_all_results( | ||
| audit.list_events, | ||
| compartment_id=c, | ||
| start_time=start_time, | ||
| end_time=end_time).data | ||
|
|
||
| # Results for a compartment 'c' for a region defined | ||
| # in 'audit' object. | ||
| list_of_audit_events.extend(list_events_response) | ||
| return list_of_audit_events | ||
|
|
||
|
|
||
| # Setting configuration | ||
| # Default path for configuration file is "~/.oci/config" | ||
| config = oci.config.from_file() | ||
| tenancy_id = config["tenancy"] | ||
|
|
||
| # Initiate the client with the locally available config. | ||
| identity = oci.identity.IdentityClient(config) | ||
|
|
||
| # Timespan defined by variables start_time and end_time(today). | ||
| # ListEvents expects timestamps into RFC3339 format. | ||
| # For the purposes of sample script, logs of last 5 days. | ||
| end_time = datetime.datetime.utcnow() | ||
| start_time = end_time + datetime.timedelta(days=-5) | ||
|
|
||
| # This array will be used to store the list of available regions. | ||
| regions = get_regions(identity) | ||
|
|
||
| # This array will be used to store the list of compartments in the tenancy. | ||
| compartments = get_compartments(identity, tenancy_id) | ||
|
|
||
| audit = oci.audit.audit_client.AuditClient(config) | ||
|
|
||
| # For each region get the logs for each compartment. | ||
| for r in regions: | ||
| # Intialize with a region value. | ||
| audit.base_client.set_region(r) | ||
| # To separate results by region use print here. | ||
| audit_events = get_audit_events( | ||
| audit, | ||
| compartments, | ||
| start_time, | ||
| end_time) | ||
|
|
||
| # Results for a region 'r' for each compartment. | ||
| if audit_events: | ||
| print audit_events | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is just for demo/example then this is probably OK. In real life, this would probably be very time-consuming because a customer has to sit there until everything is done (as
get_audit_eventsis eagerly loading all results) for each region.If a customer really did want to slurp all the audit entries, there'd be better ways to do that. But as an example of how to use the API, probably fine