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

for_each rejects secrets as map values #28638

Closed
piotr-jagiello opened this issue May 7, 2021 · 4 comments · Fixed by #28644
Closed

for_each rejects secrets as map values #28638

piotr-jagiello opened this issue May 7, 2021 · 4 comments · Fixed by #28644
Assignees
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code v0.15 Issues (primarily bugs) reported against v0.15 releases

Comments

@piotr-jagiello
Copy link

Propagation of "sensitive" property is sometimes overzealous, so resources can't be constructed with for_each from a map that has sensitive values, but not sensitive keys.

Terraform Version

Terraform v0.15.3
on linux_amd64

Terraform Configuration Files

locals {
    global_secrets = {
        password = sensitive("aaaaaa")
    }
    service_specific_secrets = {
        unrelated_service = {
            unrelated_password = sensitive("bbbbb")
        }
    }

    service_a_secrets = merge(local.global_secrets, lookup(local.service_specific_secrets, "service_a", {}))
}

resource "null_resource" "service_a_secrets" {
    for_each = local.service_a_secrets
    provisioner "local-exec" {
        command = "echo '${each.key}: saving the secret safely'"
    }
}

Debug Output

Crash Output

Expected Behavior

Terraform should create the resource.

Actual Behavior

It throws an error:

│ Error: Invalid for_each argument
│ 
│   on test.tf line 15, in resource "null_resource" "service_a_secrets":
│   15:     for_each = local.service_a_secrets
│     ├────────────────
│     │ local.service_a_secrets has a sensitive value
│ 
│ Sensitive values, or values derived from sensitive values, cannot be used as for_each arguments. If used, the sensitive value could be exposed as a resource instance key.
╵

Steps to Reproduce

  1. terraform init
  2. terraform apply

Additional Context

References

similar to #28426

@piotr-jagiello piotr-jagiello added bug new new issue not yet triaged labels May 7, 2021
@piotr-jagiello
Copy link
Author

@alisdair

@alisdair alisdair added confirmed a Terraform Core team member has reproduced this issue v0.15 Issues (primarily bugs) reported against v0.15 releases and removed new new issue not yet triaged labels May 7, 2021
@alisdair alisdair self-assigned this May 7, 2021
@alisdair
Copy link
Member

alisdair commented May 7, 2021

Copying my note from the linked issue:

In this case the problem is the lookup function, which was fixed in cty to fully support sensitivity marks in zclconf/go-cty#98, but I didn't notice that Terraform has its own implementation of lookup. We need to make the same improvements here.

@alisdair alisdair added the explained a Terraform Core team member has described the root cause of this issue in code label May 7, 2021
@alisdair
Copy link
Member

alisdair commented May 7, 2021

Thanks again for reporting this, @piotr-jagiello! The linked PR #28644 fixes this:

$ tf apply -auto-approve
null_resource.service_a_secrets["password"]: Creating...
null_resource.service_a_secrets["password"]: Provisioning with 'local-exec'...
null_resource.service_a_secrets["password"] (local-exec): Executing: ["/bin/sh" "-c" "echo 'password: saving the secret safely'"]
null_resource.service_a_secrets["password"] (local-exec): password: saving the secret safely
null_resource.service_a_secrets["password"]: Creation complete after 0s [id=4410171153199759786]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

@github-actions
Copy link

github-actions bot commented Jun 7, 2021

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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code v0.15 Issues (primarily bugs) reported against v0.15 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants