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

New Data Source: aws_kms_key #2224

Merged
merged 19 commits into from
Feb 26, 2018
Merged

New Data Source: aws_kms_key #2224

merged 19 commits into from
Feb 26, 2018

Conversation

trung
Copy link
Contributor

@trung trung commented Nov 9, 2017

To fix #2009

@trung trung changed the title data/aws_kms_key: Implement DescribeKey API d/aws_kms_key: Implement DescribeKey API Nov 9, 2017
@radeksimko radeksimko added the new-data-source Introduces a new data source. label Nov 9, 2017
@trung
Copy link
Contributor Author

trung commented Nov 10, 2017

❯ make testacc TESTARGS="-run=TestAccDataSourceAwsKmsKey"
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run=TestAccDataSourceAwsKmsKey -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccDataSourceAwsKmsKey
--- PASS: TestAccDataSourceAwsKmsKey (48.90s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	48.937s

@radeksimko radeksimko added the size/L Managed by automation to categorize the size of a PR. label Nov 15, 2017
@bflad
Copy link
Contributor

bflad commented Dec 5, 2017

While I certainly see the value here, it seems it would also be fairly simple to provide a target_key_arn on the existing aws_kms_alias data source by substituting the target key ID into the alias ARN. We can get away with this since KMS aliases and keys are bound to the same partition, account, and region.

See also CreateAlias documentation:

The alias and the CMK it is mapped to must be in the same AWS account and the same region. You cannot perform this operation on an alias in a different AWS account.

@radeksimko radeksimko added the service/kms Issues and PRs that pertain to the kms service. label Jan 17, 2018
@radeksimko radeksimko changed the title d/aws_kms_key: Implement DescribeKey API New Data Source: aws_kms_key Jan 17, 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.

@trung awesome work here! This is looking pretty good, just a few minor items to take care of then I believe this is good to go. Please let us know if you don't have time to finish this up. 😄

For what its worth given the ability for this to accept alias inputs as well, we might consider this data source for deprecating or aliasing the aws_kms_alias data source to this one in the future.


func dataSourceAwsKmsKeyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kmsconn
keyId, keyIdOk := d.GetOk("key_id")
Copy link
Contributor

Choose a reason for hiding this comment

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

Required: true is set, so this can simply be keyId := d.Get("key_id")

}
var grantTokens []*string
if v, ok := d.GetOk("grant_tokens"); ok {
for _, token := range v.([]interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you can simplify this to just grantTokens = aws.StringSlice(v.([]string))

"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsKmsKey(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: can you append _basic to this function name please? Just a consistent convention we try to use everywhere

resource.TestStep{
Config: testAccDataSourceAwsKmsKeyConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsKmsKeyCheck("data.aws_kms_key.arbitrary"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Can you add simple state checks for other attributes we're expecting to set? This helps with future maintainability. 👍 e.g. (not exhaustive below)

resource.TestMatchResourceAttr("data.aws_kms_key.arbitrary", "arn", regexp.MustCompile("^arn:[^:]+:kms:[^:]+:[^:]+:key/.+")),
resource.TestCheckResourceAttr("data.aws_kms_key.arbitrary", "enabled", "true"),
resource.TestCheckResourceAttrSet("data.aws_kms_key.arbitrary", "valid_to"),


func validateKmsKey(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
arnPrefixPattern := `arn:[\w-]+:([a-zA-Z0-9\-])+:([a-z]{2}-(gov-)?[a-z]+-\d{1})?:(\d{12})?:`
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we switch this to not assume anything about the format of AWS partitions, regions, and account IDs in the future please? arn:[^:]+:kms:[^:]+:[^:]+:

Get information on a AWS Key Management Service (KMS) Key
---

# aws\_kms\_key
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Backslashes are no longer required in the documentation headers

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

trung commented Feb 16, 2018

@bflad thanks for the review
I'm on Lunar New Year celebration now so i won't be able to make changes in the next couple of days. If this is not hurry, i can certainly work on it sometime next week

@bflad
Copy link
Contributor

bflad commented Feb 16, 2018

@trung No worries at all, please enjoy your holiday! 🎉

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Feb 26, 2018
@trung
Copy link
Contributor Author

trung commented Feb 26, 2018

@bflad I've made some changes per your review

❯ make testacc TESTARGS="-run=TestAccDataSourceAwsKmsKey_basic"
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run=TestAccDataSourceAwsKmsKey_basic -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccDataSourceAwsKmsKey_basic
--- PASS: TestAccDataSourceAwsKmsKey_basic (41.88s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	41.922s

@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Feb 26, 2018
@bflad bflad added this to the v1.11.0 milestone Feb 26, 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.

This looks great! Thanks for your work here! 🚀

 make testacc TEST=./aws TESTARGS='-run=TestAccDataSourceAwsKmsKey'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccDataSourceAwsKmsKey -timeout 120m
=== RUN   TestAccDataSourceAwsKmsKey_basic
--- PASS: TestAccDataSourceAwsKmsKey_basic (175.97s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	176.013s

@bflad bflad merged commit ac6ef7b into hashicorp:master Feb 26, 2018
bflad added a commit that referenced this pull request Feb 26, 2018
@trung trung deleted the f-kms-key branch February 26, 2018 12:32
@bflad
Copy link
Contributor

bflad commented Mar 9, 2018

This has been released in version 1.11.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
new-data-source Introduces a new data source. service/kms Issues and PRs that pertain to the kms 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.

Support ARNs for existing KMS keys
3 participants