Skip to content

Commit 514dfe5

Browse files
ccushingkohashim
authored andcommitted
Add audit events data source and runable audit example
1 parent af2f866 commit 514dfe5

File tree

9 files changed

+551
-0
lines changed

9 files changed

+551
-0
lines changed

docs/Table of Contents.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This lists all of the available OCI resources and/or data sources.
44

55
* **Audit**
6+
* [Audit Events](audit/audit_events.md)
67
* [Configurations](audit/configurations.md)
78
* **Core**
89
* [Boot Volume Attachments](core/boot_volume_attachments.md)

docs/audit/audit_events.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
# oci_audit_events
3+
4+
## AuditEvent DataSource
5+
6+
Gets a list of audit_events.
7+
8+
### List Operation
9+
Returns all audit events for the specified compartment that were processed within the specified time range.
10+
The following arguments are supported:
11+
12+
* `compartment_id` - (Required) The OCID of the compartment.
13+
* `end_time` - (Required) Returns events that were processed before this end date and time, expressed in [RFC 3339](https://tools.ietf.org/html/rfc3339) timestamp format. For example, a start value of `2017-01-01T00:00:00Z` and an end value of `2017-01-02T00:00:00Z` will retrieve a list of all events processed on January 1, 2017. Similarly, a start value of `2017-01-01T00:00:00Z` and an end value of `2017-02-01T00:00:00Z` will result in a list of all events processed between January 1, 2017 and January 31, 2017. You can specify a value with granularity to the minute. Seconds (and milliseconds, if included) must be set to `0`.
14+
* `start_time` - (Required) Returns events that were processed at or after this start date and time, expressed in [RFC 3339](https://tools.ietf.org/html/rfc3339) timestamp format. For example, a start value of `2017-01-15T11:30:00Z` will retrieve a list of all events processed since 30 minutes after the 11th hour of January 15, 2017, in Coordinated Universal Time (UTC). You can specify a value with granularity to the minute. Seconds (and milliseconds, if included) must be set to `0`.
15+
* `limit` - (Optional) The number of pages of events to request from the service. Default to 1. Large `start_time` and `end_time` ranges or very active tenancies may result in very large data sets that could cause performance issues running Terraform commands. This default value mitigates that risk by requiring intentionally setting a higher tolerance for slow running Terarform commands with potentially large statefiles.
16+
17+
18+
The following attributes are exported:
19+
20+
* `audit_events` - The list of audit_events.
21+
22+
### Example Usage
23+
24+
```hcl
25+
data "oci_audit_events" "test_audit_events" {
26+
#Required
27+
compartment_id = "${var.compartment_id}"
28+
end_time = "${var.audit_event_end_time}"
29+
start_time = "${var.audit_event_start_time}"
30+
}
31+
```
32+
### AuditEvent Reference
33+
34+
The following attributes are exported:
35+
36+
* `compartment_id` - The OCID of the compartment.
37+
* `credential_id` - The credential ID of the user. This value is extracted from the HTTP 'Authorization' request header. It consists of the tenantId, userId, and user fingerprint, all delimited by a slash (/).
38+
* `event_id` - The GUID of the event.
39+
* `event_name` - The name of the event. Example: `LaunchInstance`
40+
* `event_source` - The source of the event.
41+
* `event_time` - The time the event occurred, expressed in [RFC 3339](https://tools.ietf.org/html/rfc3339) timestamp format.
42+
* `event_type` - The type of the event.
43+
* `principal_id` - The OCID of the user whose action triggered the event.
44+
* `request_action` - The HTTP method of the request.
45+
* `request_agent` - The user agent of the client that made the request.
46+
* `request_headers` - The HTTP header fields and values in the request.
47+
* `request_id` - The opc-request-id of the request.
48+
* `request_origin` - The IP address of the source of the request.
49+
* `request_parameters` - The query parameter fields and values for the request.
50+
* `request_resource` - The resource targeted by the request.
51+
* `response_headers` - The headers of the response.
52+
* `response_payload` - Metadata of interest from the response payload. For example, the OCID of a resource.
53+
* `response_status` - The status code of the response.
54+
* `response_time` - The time of the response to the audited request, expressed in [RFC 3339](https://tools.ietf.org/html/rfc3339) timestamp format.
55+
* `tenant_id` - The OCID of the tenant.

docs/examples/audit/main.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/*
3+
* This example shows how to use the audit_configuration Resource to set the event retention period and list events with
4+
* the audit_events Data Source.
5+
*/
6+
7+
// These variables would commonly be defined as environment variables or sourced in a .env file
8+
variable "tenancy_ocid" {}
9+
variable "user_ocid" {}
10+
variable "fingerprint" {}
11+
variable "private_key_path" {}
12+
variable "compartment_ocid" {}
13+
variable "region" {}
14+
15+
16+
provider "oci" {
17+
region = "${var.region}"
18+
tenancy_ocid = "${var.tenancy_ocid}"
19+
user_ocid = "${var.user_ocid}"
20+
fingerprint = "${var.fingerprint}"
21+
private_key_path = "${var.private_key_path}"
22+
}
23+
24+
25+
resource "oci_audit_configuration" "audit_configuration" {
26+
compartment_id = "${var.tenancy_ocid}"
27+
retention_period_days = "99"
28+
}
29+
30+
data "oci_audit_configuration" "audit_configuration" {
31+
compartment_id = "${var.tenancy_ocid}"
32+
}
33+
34+
output "retention_period_days" {
35+
value = "${data.oci_audit_configuration.audit_configuration.retention_period_days}"
36+
}
37+
38+
39+
data "oci_audit_events" "audit_events" {
40+
compartment_id = "${var.compartment_ocid}"
41+
# NOTE: These dates should be updated to applicable ranges of events within your tenancy.
42+
# CAUTION: Specifying wide date ranges may pull excessively large sets of event data from the Audit service.
43+
start_time = "${timeadd(timestamp(), "-1m")}"
44+
end_time = "${timestamp()}"
45+
}
46+
47+
output "audit_events" {
48+
value = "${data.oci_audit_events.audit_events.audit_events}"
49+
}

provider/audit_event_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform/helper/resource"
10+
"github.com/hashicorp/terraform/terraform"
11+
)
12+
13+
const (
14+
AuditEventResourceConfig = AuditEventResourceDependencies + `
15+
16+
`
17+
AuditEventResourceDependencies = ""
18+
)
19+
20+
func TestAuditEventResource_basic(t *testing.T) {
21+
provider := testAccProvider
22+
config := testProviderConfig()
23+
24+
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
25+
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
26+
27+
datasourceName := "data.oci_audit_events.test_audit_events"
28+
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: map[string]terraform.ResourceProvider{
32+
"oci": provider,
33+
},
34+
Steps: []resource.TestStep{
35+
// verify datasource
36+
{
37+
Config: config + `
38+
data "oci_audit_events" "test_audit_events" {
39+
#Required
40+
compartment_id = "${var.compartment_id}"
41+
end_time = "${timestamp()}"
42+
start_time = "${timeadd(timestamp(), "-1m")}"
43+
limit = "1"
44+
}
45+
` + compartmentIdVariableStr + AuditEventResourceConfig,
46+
Check: resource.ComposeAggregateTestCheckFunc(
47+
resource.TestCheckResourceAttr(datasourceName, "compartment_id", compartmentId),
48+
resource.TestCheckResourceAttrSet(datasourceName, "end_time"),
49+
resource.TestCheckResourceAttrSet(datasourceName, "start_time"),
50+
51+
resource.TestCheckResourceAttrSet(datasourceName, "audit_events.#"),
52+
),
53+
},
54+
},
55+
})
56+
}

0 commit comments

Comments
 (0)