2015-08-28 00:12:12 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-15 15:10:36 +01:00
|
|
|
"log"
|
2015-08-28 00:12:12 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudtrail"
|
2016-04-15 17:06:40 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-08-28 00:12:12 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSCloudTrail_basic(t *testing.T) {
|
|
|
|
var trail cloudtrail.Trail
|
2016-04-15 17:06:40 +02:00
|
|
|
cloudTrailRandInt := acctest.RandInt()
|
2015-08-28 00:12:12 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudTrailDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig(cloudTrailRandInt),
|
2015-08-28 00:12:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "include_global_service_events", "true"),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2015-08-28 00:12:12 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfigModified(cloudTrailRandInt),
|
2015-08-28 00:12:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "s3_key_prefix", "/prefix"),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "include_global_service_events", "false"),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2015-08-28 00:12:12 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-21 08:15:29 +01:00
|
|
|
func TestAccAWSCloudTrail_enable_logging(t *testing.T) {
|
|
|
|
var trail cloudtrail.Trail
|
2016-04-15 17:06:40 +02:00
|
|
|
cloudTrailRandInt := acctest.RandInt()
|
2015-11-21 08:15:29 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudTrailDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig(cloudTrailRandInt),
|
2015-11-21 08:15:29 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
2015-11-22 18:47:23 +01:00
|
|
|
// AWS will create the trail with logging turned off.
|
|
|
|
// Test that "enable_logging" default works.
|
|
|
|
testAccCheckCloudTrailLoggingEnabled("aws_cloudtrail.foobar", true, &trail),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2015-11-21 08:15:29 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfigModified(cloudTrailRandInt),
|
2015-11-21 08:15:29 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
2015-11-22 18:47:23 +01:00
|
|
|
testAccCheckCloudTrailLoggingEnabled("aws_cloudtrail.foobar", false, &trail),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2015-11-21 08:15:29 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig(cloudTrailRandInt),
|
2015-11-21 08:15:29 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
2015-11-22 18:47:23 +01:00
|
|
|
testAccCheckCloudTrailLoggingEnabled("aws_cloudtrail.foobar", true, &trail),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2015-11-21 08:15:29 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-01 19:02:41 +01:00
|
|
|
func TestAccAWSCloudTrail_is_multi_region(t *testing.T) {
|
|
|
|
var trail cloudtrail.Trail
|
2016-04-15 17:06:40 +02:00
|
|
|
cloudTrailRandInt := acctest.RandInt()
|
2016-02-01 19:02:41 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudTrailDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig(cloudTrailRandInt),
|
2016-02-01 19:02:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "is_multi_region_trail", "false"),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2016-02-01 19:02:41 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfigMultiRegion(cloudTrailRandInt),
|
2016-02-01 19:02:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "is_multi_region_trail", "true"),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2016-02-01 19:02:41 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig(cloudTrailRandInt),
|
2016-02-01 19:02:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "is_multi_region_trail", "false"),
|
2016-02-08 11:03:21 +01:00
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSCloudTrail_logValidation(t *testing.T) {
|
|
|
|
var trail cloudtrail.Trail
|
2016-04-15 17:06:40 +02:00
|
|
|
cloudTrailRandInt := acctest.RandInt()
|
2016-02-08 11:03:21 +01:00
|
|
|
|
|
|
|
// TODO: Add test for KMS Key ID
|
|
|
|
// once https://github.com/hashicorp/terraform/pull/3928 is merged
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudTrailDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig_logValidation(cloudTrailRandInt),
|
2016-02-08 11:03:21 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "s3_key_prefix", ""),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "include_global_service_events", "true"),
|
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", true, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig_logValidationModified(cloudTrailRandInt),
|
2016-02-08 11:03:21 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "s3_key_prefix", ""),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "include_global_service_events", "true"),
|
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
2016-02-01 19:02:41 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:10:36 +01:00
|
|
|
func TestAccAWSCloudTrail_tags(t *testing.T) {
|
|
|
|
var trail cloudtrail.Trail
|
|
|
|
var trailTags []*cloudtrail.Tag
|
|
|
|
var trailTagsModified []*cloudtrail.Tag
|
2016-04-15 17:06:40 +02:00
|
|
|
cloudTrailRandInt := acctest.RandInt()
|
2016-02-15 15:10:36 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudTrailDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig_tags(cloudTrailRandInt),
|
2016-02-15 15:10:36 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
2016-06-10 17:07:02 +02:00
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "tags.%", "2"),
|
2016-02-15 15:10:36 +01:00
|
|
|
testAccCheckCloudTrailLoadTags(&trail, &trailTags),
|
|
|
|
testAccCheckCloudTrailCheckTags(&trailTags, map[string]string{"Foo": "moo", "Pooh": "hi"}),
|
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig_tagsModified(cloudTrailRandInt),
|
2016-02-15 15:10:36 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
2016-06-10 17:07:02 +02:00
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "tags.%", "3"),
|
2016-02-15 15:10:36 +01:00
|
|
|
testAccCheckCloudTrailLoadTags(&trail, &trailTagsModified),
|
|
|
|
testAccCheckCloudTrailCheckTags(&trailTagsModified, map[string]string{"Foo": "moo", "Moo": "boom", "Pooh": "hi"}),
|
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-04-15 17:06:40 +02:00
|
|
|
Config: testAccAWSCloudTrailConfig_tagsModifiedAgain(cloudTrailRandInt),
|
2016-02-15 15:10:36 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudTrailExists("aws_cloudtrail.foobar", &trail),
|
2016-06-10 17:07:02 +02:00
|
|
|
resource.TestCheckResourceAttr("aws_cloudtrail.foobar", "tags.%", "0"),
|
2016-02-15 15:10:36 +01:00
|
|
|
testAccCheckCloudTrailLoadTags(&trail, &trailTagsModified),
|
|
|
|
testAccCheckCloudTrailCheckTags(&trailTagsModified, map[string]string{}),
|
|
|
|
testAccCheckCloudTrailLogValidationEnabled("aws_cloudtrail.foobar", false, &trail),
|
|
|
|
testAccCheckCloudTrailKmsKeyIdEquals("aws_cloudtrail.foobar", "", &trail),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-28 00:12:12 +02:00
|
|
|
func testAccCheckCloudTrailExists(n string, trail *cloudtrail.Trail) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudtrailconn
|
|
|
|
params := cloudtrail.DescribeTrailsInput{
|
|
|
|
TrailNameList: []*string{aws.String(rs.Primary.ID)},
|
|
|
|
}
|
|
|
|
resp, err := conn.DescribeTrails(¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.TrailList) == 0 {
|
|
|
|
return fmt.Errorf("Trail not found")
|
|
|
|
}
|
|
|
|
*trail = *resp.TrailList[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 08:15:29 +01:00
|
|
|
func testAccCheckCloudTrailLoggingEnabled(n string, desired bool, trail *cloudtrail.Trail) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudtrailconn
|
|
|
|
params := cloudtrail.GetTrailStatusInput{
|
|
|
|
Name: aws.String(rs.Primary.ID),
|
|
|
|
}
|
|
|
|
resp, err := conn.GetTrailStatus(¶ms)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *resp.IsLogging != desired {
|
2015-11-22 20:54:11 +01:00
|
|
|
return fmt.Errorf("Expected logging status %t, given %t", desired, *resp.IsLogging)
|
2015-11-21 08:15:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 11:03:21 +01:00
|
|
|
func testAccCheckCloudTrailLogValidationEnabled(n string, desired bool, trail *cloudtrail.Trail) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if trail.LogFileValidationEnabled == nil {
|
|
|
|
return fmt.Errorf("No LogFileValidationEnabled attribute present in trail: %s", trail)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *trail.LogFileValidationEnabled != desired {
|
|
|
|
return fmt.Errorf("Expected log validation status %t, given %t", desired,
|
2016-02-12 22:52:46 +01:00
|
|
|
*trail.LogFileValidationEnabled)
|
2016-02-08 11:03:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// local state comparison
|
|
|
|
enabled, ok := rs.Primary.Attributes["enable_log_file_validation"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("No enable_log_file_validation attribute defined for %s, expected %t",
|
|
|
|
n, desired)
|
|
|
|
}
|
|
|
|
desiredInString := fmt.Sprintf("%t", desired)
|
|
|
|
if enabled != desiredInString {
|
2016-02-12 22:52:46 +01:00
|
|
|
return fmt.Errorf("Expected log validation status %s, saved %s", desiredInString, enabled)
|
2016-02-08 11:03:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudTrailKmsKeyIdEquals(n string, desired string, trail *cloudtrail.Trail) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if desired != "" && trail.KmsKeyId == nil {
|
|
|
|
return fmt.Errorf("No KmsKeyId attribute present in trail: %s, expected %s",
|
|
|
|
trail, desired)
|
|
|
|
}
|
|
|
|
|
|
|
|
// work around string pointer
|
|
|
|
var kmsKeyIdInString string
|
|
|
|
if trail.KmsKeyId == nil {
|
|
|
|
kmsKeyIdInString = ""
|
|
|
|
} else {
|
|
|
|
kmsKeyIdInString = *trail.KmsKeyId
|
|
|
|
}
|
|
|
|
|
|
|
|
if kmsKeyIdInString != desired {
|
|
|
|
return fmt.Errorf("Expected KMS Key ID %q to equal %q",
|
|
|
|
*trail.KmsKeyId, desired)
|
|
|
|
}
|
|
|
|
|
|
|
|
kmsKeyId, ok := rs.Primary.Attributes["kms_key_id"]
|
|
|
|
if desired != "" && !ok {
|
|
|
|
return fmt.Errorf("No kms_key_id attribute defined for %s", n)
|
|
|
|
}
|
|
|
|
if kmsKeyId != desired {
|
|
|
|
return fmt.Errorf("Expected KMS Key ID %q, saved %q", desired, kmsKeyId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 00:12:12 +02:00
|
|
|
func testAccCheckAWSCloudTrailDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudtrailconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_cloudtrail" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
params := cloudtrail.DescribeTrailsInput{
|
|
|
|
TrailNameList: []*string{aws.String(rs.Primary.ID)},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.DescribeTrails(¶ms)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(resp.TrailList) != 0 &&
|
|
|
|
*resp.TrailList[0].Name == rs.Primary.ID {
|
|
|
|
return fmt.Errorf("CloudTrail still exists: %s", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:10:36 +01:00
|
|
|
func testAccCheckCloudTrailLoadTags(trail *cloudtrail.Trail, tags *[]*cloudtrail.Tag) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudtrailconn
|
|
|
|
input := cloudtrail.ListTagsInput{
|
|
|
|
ResourceIdList: []*string{trail.TrailARN},
|
|
|
|
}
|
|
|
|
out, err := conn.ListTags(&input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Received CloudTrail tags during test: %s", out)
|
|
|
|
if len(out.ResourceTagList) > 0 {
|
|
|
|
*tags = out.ResourceTagList[0].TagsList
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Loading CloudTrail tags into a var: %s", *tags)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 17:06:40 +02:00
|
|
|
func testAccAWSCloudTrailConfig(cloudTrailRandInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-08-28 00:12:12 +02:00
|
|
|
resource "aws_cloudtrail" "foobar" {
|
2016-05-08 20:08:58 +02:00
|
|
|
name = "tf-trail-foobar-%d"
|
2015-08-28 00:12:12 +02:00
|
|
|
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_s3_bucket" "foo" {
|
|
|
|
bucket = "tf-test-trail-%d"
|
|
|
|
force_destroy = true
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailAclCheck",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:GetBucketAcl",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailWrite",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:PutObject",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d/*",
|
|
|
|
"Condition": {
|
|
|
|
"StringEquals": {
|
|
|
|
"s3:x-amz-acl": "bucket-owner-full-control"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
2016-05-08 20:08:58 +02:00
|
|
|
`, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|
2015-08-28 00:12:12 +02:00
|
|
|
|
2016-04-15 17:06:40 +02:00
|
|
|
func testAccAWSCloudTrailConfigModified(cloudTrailRandInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-08-28 00:12:12 +02:00
|
|
|
resource "aws_cloudtrail" "foobar" {
|
2016-05-08 20:08:58 +02:00
|
|
|
name = "tf-trail-foobar-%d"
|
2015-08-28 00:12:12 +02:00
|
|
|
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
|
|
|
s3_key_prefix = "/prefix"
|
|
|
|
include_global_service_events = false
|
2015-11-22 18:47:23 +01:00
|
|
|
enable_logging = false
|
2015-08-28 00:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_s3_bucket" "foo" {
|
|
|
|
bucket = "tf-test-trail-%d"
|
|
|
|
force_destroy = true
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailAclCheck",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:GetBucketAcl",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailWrite",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:PutObject",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d/*",
|
|
|
|
"Condition": {
|
|
|
|
"StringEquals": {
|
|
|
|
"s3:x-amz-acl": "bucket-owner-full-control"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
2016-05-08 20:08:58 +02:00
|
|
|
`, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|
2016-02-01 19:02:41 +01:00
|
|
|
|
2016-04-15 17:06:40 +02:00
|
|
|
func testAccAWSCloudTrailConfigMultiRegion(cloudTrailRandInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-02-01 19:02:41 +01:00
|
|
|
resource "aws_cloudtrail" "foobar" {
|
2016-05-08 20:08:58 +02:00
|
|
|
name = "tf-trail-foobar-%d"
|
2016-02-01 19:02:41 +01:00
|
|
|
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
|
|
|
is_multi_region_trail = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_s3_bucket" "foo" {
|
|
|
|
bucket = "tf-test-trail-%d"
|
|
|
|
force_destroy = true
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailAclCheck",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:GetBucketAcl",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailWrite",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:PutObject",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d/*",
|
|
|
|
"Condition": {
|
|
|
|
"StringEquals": {
|
|
|
|
"s3:x-amz-acl": "bucket-owner-full-control"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
2016-05-08 20:08:58 +02:00
|
|
|
`, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|
2016-02-08 11:03:21 +01:00
|
|
|
|
2016-04-15 17:06:40 +02:00
|
|
|
func testAccAWSCloudTrailConfig_logValidation(cloudTrailRandInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-02-08 11:03:21 +01:00
|
|
|
resource "aws_cloudtrail" "foobar" {
|
2016-05-08 20:08:58 +02:00
|
|
|
name = "tf-acc-trail-log-validation-test-%d"
|
2016-02-08 11:03:21 +01:00
|
|
|
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
|
|
|
is_multi_region_trail = true
|
|
|
|
include_global_service_events = true
|
|
|
|
enable_log_file_validation = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_s3_bucket" "foo" {
|
|
|
|
bucket = "tf-test-trail-%d"
|
|
|
|
force_destroy = true
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailAclCheck",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:GetBucketAcl",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailWrite",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:PutObject",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d/*",
|
|
|
|
"Condition": {
|
|
|
|
"StringEquals": {
|
|
|
|
"s3:x-amz-acl": "bucket-owner-full-control"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
2016-05-08 20:08:58 +02:00
|
|
|
`, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|
2016-02-08 11:03:21 +01:00
|
|
|
|
2016-04-15 17:06:40 +02:00
|
|
|
func testAccAWSCloudTrailConfig_logValidationModified(cloudTrailRandInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-02-08 11:03:21 +01:00
|
|
|
resource "aws_cloudtrail" "foobar" {
|
2016-05-08 20:08:58 +02:00
|
|
|
name = "tf-acc-trail-log-validation-test-%d"
|
2016-02-08 11:03:21 +01:00
|
|
|
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
|
|
|
include_global_service_events = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_s3_bucket" "foo" {
|
|
|
|
bucket = "tf-test-trail-%d"
|
|
|
|
force_destroy = true
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailAclCheck",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:GetBucketAcl",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailWrite",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:PutObject",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d/*",
|
|
|
|
"Condition": {
|
|
|
|
"StringEquals": {
|
|
|
|
"s3:x-amz-acl": "bucket-owner-full-control"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
2016-05-08 20:08:58 +02:00
|
|
|
`, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|
2016-02-15 15:10:36 +01:00
|
|
|
|
|
|
|
var testAccAWSCloudTrailConfig_tags_tpl = `
|
|
|
|
resource "aws_cloudtrail" "foobar" {
|
2016-05-08 20:08:58 +02:00
|
|
|
name = "tf-acc-trail-log-validation-test-%d"
|
2016-02-15 15:10:36 +01:00
|
|
|
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_s3_bucket" "foo" {
|
|
|
|
bucket = "tf-test-trail-%d"
|
|
|
|
force_destroy = true
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailAclCheck",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:GetBucketAcl",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Sid": "AWSCloudTrailWrite",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:PutObject",
|
|
|
|
"Resource": "arn:aws:s3:::tf-test-trail-%d/*",
|
|
|
|
"Condition": {
|
|
|
|
"StringEquals": {
|
|
|
|
"s3:x-amz-acl": "bucket-owner-full-control"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-04-15 17:06:40 +02:00
|
|
|
func testAccAWSCloudTrailConfig_tags(cloudTrailRandInt int) string {
|
2016-05-11 17:18:45 +02:00
|
|
|
tagsString := `tags {
|
2016-02-15 15:10:36 +01:00
|
|
|
Foo = "moo"
|
|
|
|
Pooh = "hi"
|
2016-05-11 17:18:45 +02:00
|
|
|
}`
|
|
|
|
return fmt.Sprintf(testAccAWSCloudTrailConfig_tags_tpl,
|
|
|
|
cloudTrailRandInt,
|
|
|
|
tagsString,
|
|
|
|
cloudTrailRandInt,
|
|
|
|
cloudTrailRandInt,
|
|
|
|
cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAWSCloudTrailConfig_tagsModified(cloudTrailRandInt int) string {
|
2016-05-11 17:18:45 +02:00
|
|
|
tagsString := `tags {
|
2016-02-15 15:10:36 +01:00
|
|
|
Foo = "moo"
|
|
|
|
Pooh = "hi"
|
|
|
|
Moo = "boom"
|
2016-05-11 17:18:45 +02:00
|
|
|
}`
|
|
|
|
return fmt.Sprintf(testAccAWSCloudTrailConfig_tags_tpl,
|
|
|
|
cloudTrailRandInt,
|
|
|
|
tagsString,
|
|
|
|
cloudTrailRandInt,
|
|
|
|
cloudTrailRandInt,
|
|
|
|
cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAWSCloudTrailConfig_tagsModifiedAgain(cloudTrailRandInt int) string {
|
|
|
|
return fmt.Sprintf(testAccAWSCloudTrailConfig_tags_tpl,
|
2016-05-11 17:18:45 +02:00
|
|
|
cloudTrailRandInt, "", cloudTrailRandInt, cloudTrailRandInt, cloudTrailRandInt)
|
2016-04-15 17:06:40 +02:00
|
|
|
}
|