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

Conversation

kjmkznr
Copy link
Contributor

@kjmkznr kjmkznr commented Nov 12, 2017

Closes #887

Add support event_selector parameter into aws_cloudtrail resource.
This feature supports settings to Amazon S3 object-level API operations logging.

AWS Documentations

Acceptance Test

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSCloudTrail'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSCloudTrail -timeout 120m
=== RUN   TestAccAWSCloudTrailServiceAccount_basic
--- PASS: TestAccAWSCloudTrailServiceAccount_basic (48.50s)
=== RUN   TestAccAWSCloudTrail_importBasic
--- PASS: TestAccAWSCloudTrail_importBasic (103.48s)
=== RUN   TestAccAWSCloudTrail
=== RUN   TestAccAWSCloudTrail/Trail
=== RUN   TestAccAWSCloudTrail/Trail/isMultiRegion
=== RUN   TestAccAWSCloudTrail/Trail/logValidation
=== RUN   TestAccAWSCloudTrail/Trail/kmsKey
=== RUN   TestAccAWSCloudTrail/Trail/tags
=== RUN   TestAccAWSCloudTrail/Trail/basic
=== RUN   TestAccAWSCloudTrail/Trail/enableLogging
=== RUN   TestAccAWSCloudTrail/Trail/eventSelector
=== RUN   TestAccAWSCloudTrail/Trail/cloudwatch
--- PASS: TestAccAWSCloudTrail (1317.96s)
    --- PASS: TestAccAWSCloudTrail/Trail (1317.96s)
        --- PASS: TestAccAWSCloudTrail/Trail/isMultiRegion (212.59s)
        --- PASS: TestAccAWSCloudTrail/Trail/logValidation (142.34s)
        --- PASS: TestAccAWSCloudTrail/Trail/kmsKey (111.42s)
        --- PASS: TestAccAWSCloudTrail/Trail/tags (204.39s)
        --- PASS: TestAccAWSCloudTrail/Trail/basic (169.49s)
        --- PASS: TestAccAWSCloudTrail/Trail/enableLogging (197.31s)
        --- PASS: TestAccAWSCloudTrail/Trail/eventSelector (129.95s)
        --- PASS: TestAccAWSCloudTrail/Trail/cloudwatch (150.49s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       1469.950s
$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSCloudTrail_importBasic'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSCloudTrail_importBasic -timeout 120m
=== RUN   TestAccAWSCloudTrail_importBasic
--- PASS: TestAccAWSCloudTrail_importBasic (94.65s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       94.665s

@radeksimko radeksimko added enhancement Requests to existing resources that expand the functionality or scope. size/L Managed by automation to categorize the size of a PR. labels Nov 13, 2017
@kjmkznr
Copy link
Contributor Author

kjmkznr commented Dec 10, 2017

Link #887

"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"AWS::S3::Object"}, false),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CloudTrail as of recently also supports Lambda, so you might want to add "AWS::Lambda::Function" to the list of allowed types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks comment!
I added Lambda.

@antonbabenko
Copy link
Contributor

What else should be done on this to get it into?

@radeksimko radeksimko added the service/cloudtrail Issues and PRs that pertain to the cloudtrail service. label Jan 12, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kjmkznr thanks so much for contributing this and sorry its taken so long to get a maintainer review. Please take a look at the feedback and let me know if you have any questions or when this can be reviewed again. Overall great job and we'll get this shipped soon! 😄

@@ -72,6 +73,46 @@ func resourceAwsCloudTrail() *schema.Resource {
Optional: true,
ValidateFunc: validateArn,
},
"event_selector": &schema.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: the &schema.Schema for the attributes are unnecessary since Go 1.7 (except for the one with Elem: &schema.Schema below)

"read_write_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"All", "ReadOnly", "WriteOnly"}, false),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: The AWS SDK provides constants for all of these:

  • "All": cloudtrail.ReadWriteTypeAll
  • "ReadOnly": cloudtrail.ReadWriteTypeReadOnly
  • "WriteOnly": cloudtrail.ReadWriteTypeWriteOnly

Schema: map[string]*schema.Schema{
"read_write_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This attribute should be:

Optional: true,
Default: cloudtrail.ReadWriteTypeAll,

To match the CloudTrail API documentation

return err
}

d.Set("event_selector", flattenAwsCloudTrailEventSelector(eventSelectorsOut.EventSelectors))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check and return for errors here since it is not a simple string attribute:

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

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

if d.HasChange("event_selector") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d.HasChange here will return true on resource creation (as resourceAwsCloudTrailCreate calls resourceAwsCloudTrailUpdate), so that means PutEventSelectors is called twice. You can use !d.IsNewResource() && d.HasChange("event_selector") to prevent that 👍

#### Data Resource Arguments
For **data_resource** the following attributes are supported.

* `type` (Required) - The resource type in witch you want to log data events. You can specify only the follwing value: "AWS::S3::Object"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support "AWS::Lambda::Function" in the validation function as well

For **data_resource** the following attributes are supported.

* `type` (Required) - The resource type in witch you want to log data events. You can specify only the follwing value: "AWS::S3::Object"
* `values` (Required) - A list of ARN for the specified S3 buckets and object prefies..
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: prefixes

Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.#", "1"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.#", "1"),
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check the other event_selector attributes as well:

resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.type", "AWS::S3::Object")
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.values.#", "2")
resource.TestMatchResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.values.0", regexp.MustCompile(`^arn:[^:]+:s3:::.+/foobar$`))
resource.TestMatchResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.values.1", regexp.MustCompile(`^arn:[^:]+:s3:::.+/baz$`))
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.include_management_events", "false"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.read_write_type", "ReadOnly"),

},

"data_resource": &schema.Schema{
Type: schema.TypeSet,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be easier to work with as a schema.TypeList (especially for acceptance testing) unless there's a good reason to keep it a set.

resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.#", "2"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.#", "1"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.1.data_resource.#", "2"),
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check the other event_selector attributes as well:

resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.type", "AWS::S3::Object"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.values.#", "2"),
resource.TestMatchResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.values.0", regexp.MustCompile(`^arn:[^:]+:s3:::.+/foobar$`)),
resource.TestMatchResourceAttr("aws_cloudtrail.foobar", "event_selector.0.data_resource.0.values.1", regexp.MustCompile(`^arn:[^:]+:s3:::.+/baz$`)),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.include_management_events", "true"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.0.read_write_type", "ReadOnly"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.1.data_resource.0.type", "AWS::S3::Object")
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.1.data_resource.0.values.#", "1"),
resource.TestMatchResourceAttr("aws_cloudtrail.foobar", "event_selector.1.data_resource.0.values.0", regexp.MustCompile(`^arn:[^:]+:s3:::.+/tf1$`)),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.1.data_resource.1.type", "AWS::S3::Object"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.1.data_resource.1.values.#", "1"),
resource.TestMatchResourceAttr("aws_cloudtrail.foobar", "event_selector.1.data_resource.1.values.0", regexp.MustCompile(`^arn:[^:]+:s3:::.+/tf2$`)),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.1.include_management_events", "false"),
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "event_selector.1.read_write_type", "All"),

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Feb 6, 2018
@kjmkznr
Copy link
Contributor Author

kjmkznr commented Feb 13, 2018

Thanks for reviews!
I will fixes in this week.

@kjmkznr kjmkznr force-pushed the f-aws-cloudtrail-event-selector-param branch from e101c27 to 1ac5f16 Compare February 17, 2018 10:01
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Feb 17, 2018
@kjmkznr
Copy link
Contributor Author

kjmkznr commented Feb 17, 2018

@bflad
Thank you for the reviews.
I fixed all review points.

@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Feb 19, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much @kjmkznr for your work here! LGTM! 🚀

=== RUN   TestAccAWSCloudTrailServiceAccount_basic
--- PASS: TestAccAWSCloudTrailServiceAccount_basic (10.50s)
=== RUN   TestAccAWSCloudTrail_include_global_service_events
--- PASS: TestAccAWSCloudTrail_include_global_service_events (18.55s)
=== RUN   TestAccAWSCloudTrail_importBasic
--- PASS: TestAccAWSCloudTrail_importBasic (19.74s)
=== RUN   TestAccAWSCloudTrail
=== RUN   TestAccAWSCloudTrail/Trail
=== RUN   TestAccAWSCloudTrail/Trail/basic
=== RUN   TestAccAWSCloudTrail/Trail/cloudwatch
=== RUN   TestAccAWSCloudTrail/Trail/enableLogging
=== RUN   TestAccAWSCloudTrail/Trail/tags
=== RUN   TestAccAWSCloudTrail/Trail/eventSelector
=== RUN   TestAccAWSCloudTrail/Trail/isMultiRegion
=== RUN   TestAccAWSCloudTrail/Trail/logValidation
=== RUN   TestAccAWSCloudTrail/Trail/kmsKey
--- PASS: TestAccAWSCloudTrail (257.44s)

@bflad bflad added this to the v1.10.0 milestone Feb 19, 2018
@bflad bflad merged commit 9b6dff6 into hashicorp:master Feb 19, 2018
bflad added a commit that referenced this pull request Feb 19, 2018
@kjmkznr
Copy link
Contributor Author

kjmkznr commented Feb 20, 2018

Thanks!

@kjmkznr kjmkznr deleted the f-aws-cloudtrail-event-selector-param branch February 20, 2018 02:03
@bflad
Copy link
Contributor

bflad commented Feb 27, 2018

This has been released in version 1.10.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 7, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/cloudtrail Issues and PRs that pertain to the cloudtrail service. size/L Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Support CloudTrail EventSelectors
5 participants