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

Adds encryption configuration to ECR repository resource and data source #14520

Merged
merged 2 commits into from
Aug 7, 2020
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
58 changes: 38 additions & 20 deletions aws/data_source_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,25 @@ func dataSourceAwsEcrRepository() *schema.Resource {
Read: dataSourceAwsEcrRepositoryRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"registry_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"repository_url": {
Type: schema.TypeString,
Computed: true,
},
"image_tag_mutability": {
Type: schema.TypeString,
"encryption_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"encryption_type": {
Type: schema.TypeString,
Computed: true,
},
"kms_key": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"image_scanning_configuration": {
Type: schema.TypeList,
Expand All @@ -48,6 +47,23 @@ func dataSourceAwsEcrRepository() *schema.Resource {
},
},
},
"image_tag_mutability": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"registry_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"repository_url": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
Expand Down Expand Up @@ -80,23 +96,25 @@ func dataSourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) er

d.SetId(aws.StringValue(repository.RepositoryName))
d.Set("arn", arn)
d.Set("registry_id", repository.RegistryId)
d.Set("name", repository.RepositoryName)
d.Set("registry_id", repository.RegistryId)
d.Set("repository_url", repository.RepositoryUri)
d.Set("image_tag_mutability", repository.ImageTagMutability)

tags, err := keyvaluetags.EcrListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for ECR Repository (%s): %w", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
return fmt.Errorf("error setting tags for ECR Repository (%s): %w", arn, err)
}

if err := d.Set("image_scanning_configuration", flattenImageScanningConfiguration(repository.ImageScanningConfiguration)); err != nil {
return fmt.Errorf("error setting image_scanning_configuration: %s", err)
return fmt.Errorf("error setting image_scanning_configuration for ECR Repository (%s): %w", arn, err)
}

if err := d.Set("encryption_configuration", flattenEcrRepositoryEncryptionConfiguration(repository.EncryptionConfiguration)); err != nil {
return fmt.Errorf("error setting encryption_configuration for ECR Repository (%s): %w", arn, err)
}

return nil
Expand Down
47 changes: 47 additions & 0 deletions aws/data_source_aws_ecr_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ func TestAccAWSEcrRepositoryDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
resource.TestCheckResourceAttrPair(resourceName, "image_scanning_configuration.#", dataSourceName, "image_scanning_configuration.#"),
resource.TestCheckResourceAttrPair(resourceName, "image_tag_mutability", dataSourceName, "image_tag_mutability"),
resource.TestCheckResourceAttrPair(resourceName, "encryption_configuration.#", dataSourceName, "encryption_configuration.#"),
),
},
},
})
}

func TestAccAWSEcrRepositoryDataSource_encryption(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ecr_repository.test"
dataSourceName := "data.aws_ecr_repository.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsEcrRepositoryDataSourceConfig_encryption(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "registry_id", dataSourceName, "registry_id"),
resource.TestCheckResourceAttrPair(resourceName, "repository_url", dataSourceName, "repository_url"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
resource.TestCheckResourceAttrPair(resourceName, "image_scanning_configuration.#", dataSourceName, "image_scanning_configuration.#"),
resource.TestCheckResourceAttrPair(resourceName, "image_tag_mutability", dataSourceName, "image_tag_mutability"),
resource.TestCheckResourceAttrPair(resourceName, "encryption_configuration.#", dataSourceName, "encryption_configuration.#"),
resource.TestCheckResourceAttrPair(resourceName, "encryption_configuration.0.encryption_type", dataSourceName, "encryption_configuration.0.encryption_type"),
resource.TestCheckResourceAttrPair(resourceName, "encryption_configuration.0.kms_key", dataSourceName, "encryption_configuration.0.kms_key"),
),
},
},
Expand Down Expand Up @@ -69,3 +97,22 @@ data "aws_ecr_repository" "test" {
}
`, rName)
}

func testAccCheckAwsEcrRepositoryDataSourceConfig_encryption(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {}

resource "aws_ecr_repository" "test" {
name = %q

encryption_configuration {
encryption_type = "KMS"
kms_key = aws_kms_key.test.arn
}
}

data "aws_ecr_repository" "test" {
name = aws_ecr_repository.test.name
}
`, rName)
}
105 changes: 81 additions & 24 deletions aws/resource_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,35 @@ func resourceAwsEcrRepository() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"name": {
"arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Computed: true,
},
"image_tag_mutability": {
Type: schema.TypeString,
"encryption_configuration": {
Type: schema.TypeList,
Optional: true,
Default: ecr.ImageTagMutabilityMutable,
ValidateFunc: validation.StringInSlice([]string{
ecr.ImageTagMutabilityMutable,
ecr.ImageTagMutabilityImmutable,
}, false),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"encryption_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
ecr.EncryptionTypeAes256,
ecr.EncryptionTypeKms,
}, false),
Default: ecr.EncryptionTypeAes256,
ForceNew: true,
},
"kms_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
},
DiffSuppressFunc: suppressMissingOptionalConfigurationBlock,
ForceNew: true,
},
"image_scanning_configuration": {
Type: schema.TypeList,
Expand All @@ -56,10 +72,19 @@ func resourceAwsEcrRepository() *schema.Resource {
},
DiffSuppressFunc: suppressMissingOptionalConfigurationBlock,
},
"tags": tagsSchema(),
"arn": {
"image_tag_mutability": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Default: ecr.ImageTagMutabilityMutable,
ValidateFunc: validation.StringInSlice([]string{
ecr.ImageTagMutabilityMutable,
ecr.ImageTagMutabilityImmutable,
}, false),
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"registry_id": {
Type: schema.TypeString,
Expand All @@ -69,6 +94,7 @@ func resourceAwsEcrRepository() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -77,9 +103,10 @@ func resourceAwsEcrRepositoryCreate(d *schema.ResourceData, meta interface{}) er
conn := meta.(*AWSClient).ecrconn

input := ecr.CreateRepositoryInput{
ImageTagMutability: aws.String(d.Get("image_tag_mutability").(string)),
RepositoryName: aws.String(d.Get("name").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().EcrTags(),
ImageTagMutability: aws.String(d.Get("image_tag_mutability").(string)),
RepositoryName: aws.String(d.Get("name").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().EcrTags(),
EncryptionConfiguration: expandEcrRepositoryEncryptionConfiguration(d.Get("encryption_configuration").([]interface{})),
}

imageScanningConfigs := d.Get("image_scanning_configuration").([]interface{})
Expand Down Expand Up @@ -153,18 +180,20 @@ func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) erro
d.Set("repository_url", repository.RepositoryUri)
d.Set("image_tag_mutability", repository.ImageTagMutability)

if err := d.Set("image_scanning_configuration", flattenImageScanningConfiguration(repository.ImageScanningConfiguration)); err != nil {
return fmt.Errorf("error setting image_scanning_configuration: %s", err)
}

tags, err := keyvaluetags.EcrListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for ECR Repository (%s): %s", arn, err)
return fmt.Errorf("error listing tags for ECR Repository (%s): %w", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
return fmt.Errorf("error setting tags for ECR Repository (%s): %w", arn, err)
}

if err := d.Set("image_scanning_configuration", flattenImageScanningConfiguration(repository.ImageScanningConfiguration)); err != nil {
return fmt.Errorf("error setting image_scanning_configuration for ECR Repository (%s): %w", arn, err)
}

if err := d.Set("encryption_configuration", flattenEcrRepositoryEncryptionConfiguration(repository.EncryptionConfiguration)); err != nil {
return fmt.Errorf("error setting encryption_configuration for ECR Repository (%s): %w", arn, err)
}

return nil
Expand All @@ -183,6 +212,34 @@ func flattenImageScanningConfiguration(isc *ecr.ImageScanningConfiguration) []ma
}
}

func expandEcrRepositoryEncryptionConfiguration(data []interface{}) *ecr.EncryptionConfiguration {
if len(data) == 0 || data[0] == nil {
return nil
}

ec := data[0].(map[string]interface{})
config := &ecr.EncryptionConfiguration{
EncryptionType: aws.String(ec["encryption_type"].(string)),
}
if v, ok := ec["kms_key"]; ok {
if s := v.(string); s != "" {
config.KmsKey = aws.String(v.(string))
}
}
return config
}

func flattenEcrRepositoryEncryptionConfiguration(ec *ecr.EncryptionConfiguration) []map[string]interface{} {
config := map[string]interface{}{
"encryption_type": aws.StringValue(ec.EncryptionType),
"kms_key": aws.StringValue(ec.KmsKey),
}

return []map[string]interface{}{
config,
}
}

func resourceAwsEcrRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
arn := d.Get("arn").(string)
conn := meta.(*AWSClient).ecrconn
Expand Down
Loading