Skip to content
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

r/cloudtrail: Add support CloudTrail EventSelector #2258

Merged
merged 14 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions aws/resource_aws_cloudtrail.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/service/cloudtrail"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsCloudTrail() *schema.Resource {
Expand Down Expand Up @@ -72,6 +73,51 @@ func resourceAwsCloudTrail() *schema.Resource {
Optional: true,
ValidateFunc: validateArn,
},
"event_selector": {
Type: schema.TypeList,
Optional: true,
MaxItems: 5,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"read_write_type": {
Type: schema.TypeString,
Optional: true,
Default: cloudtrail.ReadWriteTypeAll,
ValidateFunc: validation.StringInSlice([]string{
cloudtrail.ReadWriteTypeAll,
cloudtrail.ReadWriteTypeReadOnly,
cloudtrail.ReadWriteTypeWriteOnly,
}, false),
},

"include_management_events": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"data_resource": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"AWS::S3::Object", "AWS::Lambda::Function"}, false),
},
"values": {
Type: schema.TypeList,
Required: true,
MaxItems: 250,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
},
},
"home_region": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -150,6 +196,13 @@ func resourceAwsCloudTrailCreate(d *schema.ResourceData, meta interface{}) error
}
}

// Event Selectors
if _, ok := d.GetOk("event_selector"); ok {
if err := cloudTrailSetEventSelectors(conn, d); err != nil {
return err
}
}

return resourceAwsCloudTrailUpdate(d, meta)
}

Expand Down Expand Up @@ -227,6 +280,18 @@ func resourceAwsCloudTrailRead(d *schema.ResourceData, meta interface{}) error {
}
d.Set("enable_logging", logstatus)

// Get EventSelectors
eventSelectorsOut, err := conn.GetEventSelectors(&cloudtrail.GetEventSelectorsInput{
TrailName: aws.String(d.Id()),
})
if err != nil {
return err
}

if err := d.Set("event_selector", flattenAwsCloudTrailEventSelector(eventSelectorsOut.EventSelectors)); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -300,6 +365,13 @@ func resourceAwsCloudTrailUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if !d.IsNewResource() && d.HasChange("event_selector") {
log.Printf("[DEBUG] Updating event selector on CloudTrail: %s", input)
if err := cloudTrailSetEventSelectors(conn, d); err != nil {
return err
}
}

log.Printf("[DEBUG] CloudTrail updated: %s", t)

return resourceAwsCloudTrailRead(d, meta)
Expand Down Expand Up @@ -357,3 +429,98 @@ func cloudTrailSetLogging(conn *cloudtrail.CloudTrail, enabled bool, id string)

return nil
}

func cloudTrailSetEventSelectors(conn *cloudtrail.CloudTrail, d *schema.ResourceData) error {
input := &cloudtrail.PutEventSelectorsInput{
TrailName: aws.String(d.Id()),
}

eventSelectors := expandAwsCloudTrailEventSelector(d.Get("event_selector").([]interface{}))
input.EventSelectors = eventSelectors

if err := input.Validate(); err != nil {
return fmt.Errorf("Error validate CloudTrail (%s): %s", d.Id(), err)
}

_, err := conn.PutEventSelectors(input)
if err != nil {
return fmt.Errorf("Error set event selector on CloudTrail (%s): %s", d.Id(), err)
}

return nil
}

func expandAwsCloudTrailEventSelector(configured []interface{}) []*cloudtrail.EventSelector {
eventSelectors := make([]*cloudtrail.EventSelector, 0, len(configured))

for _, raw := range configured {
data := raw.(map[string]interface{})
dataResources := expandAwsCloudTrailEventSelectorDataResource(data["data_resource"].([]interface{}))

es := &cloudtrail.EventSelector{
IncludeManagementEvents: aws.Bool(data["include_management_events"].(bool)),
ReadWriteType: aws.String(data["read_write_type"].(string)),
DataResources: dataResources,
}
eventSelectors = append(eventSelectors, es)
}

return eventSelectors
}

func expandAwsCloudTrailEventSelectorDataResource(configured []interface{}) []*cloudtrail.DataResource {
dataResources := make([]*cloudtrail.DataResource, 0, len(configured))

for _, raw := range configured {
data := raw.(map[string]interface{})

values := make([]*string, len(data["values"].([]interface{})))
for i, vv := range data["values"].([]interface{}) {
str := vv.(string)
values[i] = aws.String(str)
}

dataResource := &cloudtrail.DataResource{
Type: aws.String(data["type"].(string)),
Values: values,
}

dataResources = append(dataResources, dataResource)
}

return dataResources
}

func flattenAwsCloudTrailEventSelector(configured []*cloudtrail.EventSelector) []map[string]interface{} {
eventSelectors := make([]map[string]interface{}, 0, len(configured))

// Prevent default configurations shows differences
if len(configured) == 1 && len(configured[0].DataResources) == 0 {
return eventSelectors
}

for _, raw := range configured {
item := make(map[string]interface{})
item["read_write_type"] = *raw.ReadWriteType
item["include_management_events"] = *raw.IncludeManagementEvents
item["data_resource"] = flattenAwsCloudTrailEventSelectorDataResource(raw.DataResources)

eventSelectors = append(eventSelectors, item)
}

return eventSelectors
}

func flattenAwsCloudTrailEventSelectorDataResource(configured []*cloudtrail.DataResource) []map[string]interface{} {
dataResources := make([]map[string]interface{}, 0, len(configured))

for _, raw := range configured {
item := make(map[string]interface{})
item["type"] = *raw.Type
item["values"] = flattenStringList(raw.Values)

dataResources = append(dataResources, item)
}

return dataResources
}
Loading