terraform icon indicating copy to clipboard operation
terraform copied to clipboard

backend/s3: `assume_role_with_web_identity` ignores related environment variables

Open jar-b opened this issue 2 years ago • 13 comments

Terraform Version

v1.6.0-beta1

Terraform Configuration Files

terraform {
  backend "s3" {
    bucket = "jb-test"
    key    = "path/to/statefiles"
    region = "us-east-1"
  }
}

Debug Output

https://github.com/hashicorp/terraform/pull/31276#issuecomment-1703530574

Expected Behavior

The assume_role_with_web_identity block should respect the corresponding AWS environment variables.

Actual Behavior

Role assumption flow fails.

Steps to Reproduce

  1. Set the AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE environment variables.
  2. terraform init
  3. Observe failure

Additional Context

Originally reported in this comment: https://github.com/hashicorp/terraform/pull/31276#issuecomment-1703530574

References

Relates #31276 Relates #33730

jar-b avatar Sep 05 '23 17:09 jar-b

@manobi this should actually work, since the AWS SDK for Go handles the environment variables. I've created a branch that (re-)enables API request logging for the authentication flow at https://github.com/hashicorp/terraform/tree/s3/log-base.

Can you try again using that branch and with the environment variable TF_LOG set to DEBUG and share the log, please?

Can you also please share your backend configuration, please?

gdavison avatar Sep 06 '23 20:09 gdavison

I would love to help with this test, but I only have this trust relationship for my CI/CD runners.

I can't try it until it's published as a docker image on docker hub or something like it.

manobi avatar Sep 07 '23 18:09 manobi

The logging update should be released in v1.6.0-beta2

gdavison avatar Sep 07 '23 18:09 gdavison

Have it already being released to public docker registries?

manobi avatar Sep 08 '23 22:09 manobi

No, v1.6.0-beta2 has not yet been built and released at all, thus it is not in Dockerhub. See also: https://hub.docker.com/r/hashicorp/terraform/tags

crw avatar Sep 11 '23 17:09 crw

Hi @manobi, v1.6.0-beta3 is now on Dockerhub

gdavison avatar Sep 22 '23 19:09 gdavison

Hi @manobi. Now that v1.6 has been released, are you still seeing this problem? If so, can you please share your backend configuration and a debug log generated by setting the environment variable TF_LOG to DEBUG

gdavison avatar Oct 16 '23 17:10 gdavison

I'm seeing a similar issue with v1.6.5, however the steps to reproduce are a bit different (set AWS_WEB_IDENTITY_TOKEN_FILE env var, set role_arn in backend config).

Initializing the backend...
╷
│ Error: Missing Required Value
│ 
│   on main.tf line 9, in terraform:
│    9:   backend "s3" {
│ 
│ Exactly one of web_identity_token, web_identity_token_file must be set.
╵

Perhaps I'm barking up the wrong tree and this is a SDK issue, similar to: https://github.com/hashicorp/terraform-provider-aws/issues/27019.

marshallford avatar Dec 06 '23 01:12 marshallford

@crw @justinretzolk since you both responded to my issue yesterday in a related way, I wanted to bring this to your attention as soon as possible given that 1.10 now enforces the split of assume role and assume web identity, essentially assume with web identity is broken for 1.10 if you were previously using environment variables.

Exactly one of web_identity_token, web_identity_token_file must be set.

I can confirm 100% that AWS_WEB_IDENTITY_TOKEN_FILE is set and this is an AWS SDK supported environment variable, so not only has this issue existed in Sept 2023, now that 1.10 forces use of the two assume role blocks, it's now broken hard in 1.10+.

│   on backend.tf line 8, in terraform:
│    8: 		assume_role_with_web_identity = {
│    9: 			role_arn = "arn:aws:iam::XXXXXXXXXXXXX:role/tfstate-management-role"
│   10: 			external_id = "LEARNED THIS IS JUST IGNORED"
│   11: 			session_name = "tfstate-UUID"
│   12: 			policy = "CRAFTED_POLICY"
│   13: 		}
│
│ Exactly one of web_identity_token, web_identity_token_file must be set.

AWS_WEB_IDENTITY_TOKEN_FILE is set by the environment and should 100% be used as a value to web_identity_token_file this use to work without the assume_role_with_web_identity block AND it's even stated in the docs to be supported, but confirmed it is NOT.

ekristen avatar Feb 12 '25 14:02 ekristen

@crw can we get eyes on this, we do a lot of automated deployments and use automatic env vars to set roles and can't use this functionality due to this bug. I believe it's as simple as removing https://github.com/hashicorp/terraform/blob/main/internal/backend/remote-state/s3/backend.go#L522C1-L528C4 as the SDK should pick up the env var automatically if they aren't set.

ekristen avatar Apr 02 '25 16:04 ekristen

I'll re-raise it with the AWS Provider team here at HashiCorp.

crw avatar Apr 02 '25 17:04 crw

Any news on this issue?

trc-ikeskin avatar Sep 17 '25 13:09 trc-ikeskin

Can the documentation be updated to clarify this doesn't work? It would save people from wasting hours on configuration that can't succeed.

vegardx avatar Nov 21 '25 08:11 vegardx