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

Adding secret_binary to aws_secretsmanager_secret_version #6070

Merged
merged 4 commits into from
Oct 8, 2018

Conversation

eraac
Copy link
Contributor

@eraac eraac commented Oct 4, 2018

Fixes #4571

Changes proposed in this pull request:

  • Adding data secret_binary for data "aws_secretsmanager_secret_version"
  • Adding input secret_binary for resource "aws_secretsmanager_secret_version"

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccAwsSecretsManagerSecretVersion'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAwsSecretsManagerSecretVersion -timeout 120m
=== RUN   TestAccAwsSecretsManagerSecretVersion_BasicString
--- PASS: TestAccAwsSecretsManagerSecretVersion_BasicString (12.46s)
=== RUN   TestAccAwsSecretsManagerSecretVersion_BasicBinary
--- PASS: TestAccAwsSecretsManagerSecretVersion_BasicBinary (10.40s)
=== RUN   TestAccAwsSecretsManagerSecretVersion_VersionStages
--- PASS: TestAccAwsSecretsManagerSecretVersion_VersionStages (26.18s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	49.086s
...

First time i contribute to a terraform provider, i hope to do well.

I have just few doubt about the code for the resource (for the cast []byte <-> string). For the test i only copy/paste the test for secret_string

@ghost ghost added size/M Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. labels Oct 4, 2018
@bflad bflad added the service/secretsmanager Issues and PRs that pertain to the secretsmanager service. label Oct 4, 2018
@bflad
Copy link
Contributor

bflad commented Oct 4, 2018

Hi @eraac 👋 Thanks for submitting this! When working with binary data in Terraform, it should be base64 encoded and decoded to prevent Terraform state corruption. Are you able to update the handling to include that? Otherwise this is looking pretty good so far. 👍

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

eraac commented Oct 4, 2018

@bflad https://docs.aws.amazon.com/sdk-for-go/api/service/secretsmanager/#GetSecretValueOutput according to documentation is automatically base64 encoded/decoded by the SDK

@eraac
Copy link
Contributor Author

eraac commented Oct 4, 2018

I probably misunderstood, you mean for the terraform himself ?

So user should insert data as base64 and I decode it in the plugin ? What about output, should be decoded or not ?

@bflad
Copy link
Contributor

bflad commented Oct 4, 2018

So user should insert data as base64 and I decode it in the plugin ?

Yes, sorry if I was unclear! Basically, we should require something like "${base64encode(file("path/to/binaryfile"))}" or "${base64gzip(file("path/to/binaryfile"))}" in configurations so that there are no issues with parsing configurations or saving the binary data in the Terraform state as a UTF-8 string.

If the AWS SDK already handles the conversion correct, that's great! We'll just want to make sure the acceptance test is updated to perform the above on some binary content (there are some zip files in aws/test-fixtures that can be used here) and that the documentation notes the required base64 input and likely mentioning the base64encode() function available in Terraform.

What about output, should be decoded or not ?

It should be base64 encoded. If people need to get the actual binary contents, they can use base64decode() function

@eraac
Copy link
Contributor Author

eraac commented Oct 4, 2018

I take a look to other resources using base64 and your response confirm. Thanks for the precision

I push new commit with all the changes needed (code, test and documentation), should be good now :proud:

@eraac eraac changed the title [WIP] Adding secret_binary to aws_secretsmanager_secret_version Adding secret_binary to aws_secretsmanager_secret_version Oct 4, 2018
@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Oct 5, 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.

LGTM, thanks @eraac! 🚀

--- PASS: TestAccAwsSecretsManagerSecretVersion_BasicString (14.55s)
--- PASS: TestAccAwsSecretsManagerSecretVersion_Base64Binary (13.49s)
--- PASS: TestAccAwsSecretsManagerSecretVersion_VersionStages (33.27s)

@@ -42,4 +42,5 @@ data "aws_secretsmanager_secret_version" "by-version-stage" {
* `arn` - The ARN of the secret.
* `id` - The unique identifier of this version of the secret.
* `secret_string` - The decrypted part of the protected secret information that was originally provided as a string.
* `secret_binary` - The decrypted part of the protected secret information that was originally provided as a binary.
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 note this is base64 encoded. I'll make the very minor edit post-merge. 😄

if v, ok := d.GetOk("secret_binary"); ok {
vs := []byte(v.(string))

if !isBase64Encoded(vs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a blocker for merging, but I believe we could enhance this check to actually be done during plan with a ValidateFunc on the secret_binary attribute:

ValidateFunc: func(v interface{}, name string) (warns []string, errs []error) {
	s := v.(string)
	if !isBase64Encoded([]byte(s)) {
		errs = append(errs, fmt.Errorf("%s: must be base64-encoded", name))
	}
	return
},

At some point we can migrate the existing attributes that use this (or a similar) ValidateFunc for ensure Base64 content into its own, easy to use common function like validateBase64String. 👍

@bflad bflad added this to the v1.40.0 milestone Oct 8, 2018
@bflad bflad merged commit c670f65 into hashicorp:master Oct 8, 2018
bflad added a commit that referenced this pull request Oct 8, 2018
@bflad
Copy link
Contributor

bflad commented Oct 10, 2018

This has been released in version 1.40.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/secretsmanager Issues and PRs that pertain to the secretsmanager 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.

add secret_binary attribute to aws_secretsmanager_secret_version data source
2 participants