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

Add KMS encryption support to the aws_athena_database resource. #6117

Merged
merged 12 commits into from
Oct 15, 2018

Conversation

joestump
Copy link
Contributor

@joestump joestump commented Oct 10, 2018

What does this add?

Adds an encryption_key attribute to the aws_athena_database resource. This allows TF to create databases that read from encrypted S3 buckets.

Example HCL

resource "aws_kms_key" "test" {
  name = "athenta-test"
}

resource "aws_athena_database" "test" {
  name          = "athena-test"
  bucket        = "${aws_s3_bucket.test.bucket}"
  force_destroy = true

  encryption_key {
    type = "SSE_KMS"
    id   = "${aws_kms_key.test.arn}" 
  }
}

Test output

make testacc TESTARGS='-run=TestAccAWSAthenaDatabase'
TF_ACC=1 go test ./... -v -run=TestAccAWSAthenaDatabase -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccAWSAthenaDatabase_basic
--- PASS: TestAccAWSAthenaDatabase_basic (49.53s)
=== RUN   TestAccAWSAthenaDatabase_encryption
--- PASS: TestAccAWSAthenaDatabase_encryption (79.31s)
=== RUN   TestAccAWSAthenaDatabase_nameStartsWithUnderscore
--- PASS: TestAccAWSAthenaDatabase_nameStartsWithUnderscore (49.75s)
=== RUN   TestAccAWSAthenaDatabase_nameCantHaveUppercase
--- PASS: TestAccAWSAthenaDatabase_nameCantHaveUppercase (0.92s)
=== RUN   TestAccAWSAthenaDatabase_destroyFailsIfTablesExist
--- PASS: TestAccAWSAthenaDatabase_destroyFailsIfTablesExist (61.16s)
=== RUN   TestAccAWSAthenaDatabase_forceDestroyAlwaysSucceeds
--- PASS: TestAccAWSAthenaDatabase_forceDestroyAlwaysSucceeds (72.15s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	314.027s

@ghost ghost added size/M Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. service/athena Issues and PRs that pertain to the athena service. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. labels Oct 10, 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.

Hi @joestump 👋 Thanks for submitting this! Please see the initial feedback below and let us know if you have any questions.

},
}
}

func getResultConfig(d *schema.ResourceData) (*athena.ResultConfiguration, error) {
Copy link
Contributor

@bflad bflad Oct 14, 2018

Choose a reason for hiding this comment

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

Since this project has a very flat package structure, we prefer to include the service name in the function name. We also refer to these generally as "expand" functions and it would be nice to reduce the signature to not use d directly. e.g.

func expandAthenaResultConfiguration(bucket string, encryptionConfigurationList []interface{}) (*athena.ResultConfiguration, error) {

keyType := data["type"].(string)
keyID := data["id"].(string)

if len(keyType) <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the schema already has this set as Required: true this error handling is extraneous.

}

if strings.HasSuffix(keyType, "_KMS") && len(keyID) <= 0 {
return nil, fmt.Errorf("Key type %s requires a valid KMS key ID", keyType)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the API not provide a helpful error message already? Since this error handling occurs during apply time anyways, we should avoid adding logic the API already handles unless necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does; just felt dirty making an AWS call for known errors. If that’s the pattern, I’ll update though.

@@ -38,18 +38,73 @@ func resourceAwsAthenaDatabase() *schema.Resource {
Optional: true,
Default: false,
},
"encryption_key": {
Copy link
Contributor

Choose a reason for hiding this comment

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

The API refers to this as encryption_configuration -- is there any reason to diverge from their naming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd modeled it after the CodePipeline's HCL, which makes a bit more sense, but if the pattern is to copy the API directly to HCL I'll update.

Copy link
Contributor

Choose a reason for hiding this comment

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

It might make a bit more sense today, but not necessarily in the future should the API change. Here are a few reasons we prefer to stick with the API:

  • We have gotten bitten in the past by trying to create abstractions and the API changes in a way that doesn't match those abstractions.
  • Less of a maintenance burden to mentally work with the codebase if there are no abstractions/conversions.
  • It keeps Terraform in line with the AWS API documentation, SDKs, and CloudFormation so operators coming from other documentation or systems can easily reference or convert between them.
  • Different AWS services don't always line up with each other in terms of implementations. While this may look similar to CodePipeline today, Athena or CodePipeline may change tomorrow.

Hope this helps!

MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Copy link
Contributor

Choose a reason for hiding this comment

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

The API refers to this as kms_key -- is there any reason to diverge from their naming?

Type: schema.TypeString,
Optional: true,
},
"type": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Two things:

  • The API refers to this as encryption_option -- is there any reason to diverge from their naming?
  • We should validate this attribute using the SDK provided constants:
ValidateFunc: validation.StringInSlice([]string{
  athena.EncryptionOptionCseKms,
  athena.EncryptionOptionSseKms,
  athena.EncryptionOptionSseS3,
}, false),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call on the validate. I mentioned why I diverged from the API above. I think the HCL I've proposed would look cleaner than this:

encryption_configuration {
  encryption_option = "CSE_KMS"
  kms_key = "${aws_kms_key.foo.id}"
}

It feels verbose to me. I think the CodePipeline API looks/feels more correct.

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Oct 14, 2018
@joestump
Copy link
Contributor Author

@bflad I've addressed most of the comments related to the Golang. Let me know what you think about my response on the HCL. I'm new to the AWS provider and will defer to the prevailing patterns. 👍

Thanks for the review!

@joestump
Copy link
Contributor Author

@bflad I took a stab at adding your notes into the CONTRIBUTING.md. I believe all comments have been addressed now. 👍

@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Oct 15, 2018
@bflad bflad added this to the v1.41.0 milestone Oct 15, 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, @joestump! 🚀

--- PASS: TestAccAWSAthenaDatabase_nameCantHaveUppercase (0.89s)
--- PASS: TestAccAWSAthenaDatabase_nameStartsWithUnderscore (26.71s)
--- PASS: TestAccAWSAthenaDatabase_basic (26.77s)
--- PASS: TestAccAWSAthenaDatabase_forceDestroyAlwaysSucceeds (31.01s)
--- PASS: TestAccAWSAthenaDatabase_destroyFailsIfTablesExist (36.16s)
--- PASS: TestAccAWSAthenaDatabase_encryption (49.10s)

@bflad bflad merged commit f551870 into hashicorp:master Oct 15, 2018
bflad added a commit that referenced this pull request Oct 15, 2018
@joestump joestump deleted the jstump-kms-athena branch October 16, 2018 01:04
@bflad
Copy link
Contributor

bflad commented Oct 18, 2018

This has been released in version 1.41.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 2, 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 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. service/athena Issues and PRs that pertain to the athena service. size/M Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants