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

❗Notice: CDK CLI: "The security token included in the request is invalid" when using user credentials since 2.167.0 #32120

Closed
AdamPD opened this issue Nov 14, 2024 · 26 comments · Fixed by #32134
Labels
bug This issue is a bug. management/tracking Issues that track a subject or multiple issues p0 package/tools Related to AWS CDK Tools or CLI sdk-v3-upgrade Tag issues that are associated to SDK V3 upgrade. Not limited to CR usage of SDK only.

Comments

@AdamPD
Copy link

AdamPD commented Nov 14, 2024

Please add your +1 👍 to let us know you have encountered this

Status: RESOLVED

Overview:

In version 2.167.0 CLI commands, including deployments, fail with authentication errors.

This main cause is with authentication configurations that do not use AWS_SESSION_TOKEN, like IAM User credentials.
A second cause is related to the location of the region configuration, see #32130

Complete Error Message:

The security token included in the request is invalid

Workaround:

Revert to 2.166.0

Solution:

Upgrade to 2.167.1

Related Issues:

#32130


Original issue

Since version 2.167.0, deployments fail due to the inability to get the AWS account ID. The following error occurs in the verbose output of cdk synthesize/deploy:

[01:50:17] Resolving default credentials
[01:50:17] Looking up default account ID from STS
[01:50:18] Unable to determine the default AWS account (InvalidClientTokenId): The security token included in the request is invalid

This is despite having correct AWS environment variables set (AWS_DEFAULT_REGION, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID) and aws sts get-caller-identity works correctly.

Rolling back to 2.166.0 with:

npm install -g [email protected]

resolves the issue and deployments resume as per normal.

Regression Issue

Confirmed Regression.

Last Known Working CDK Version

No response

Expected Behavior

CDK should function correctly and retrieve the account ID via the AWS credentials.

Current Behavior

CDK throws an error stating that it cannot retrieve the account ID due to security token issues.

Reproduction Steps

Upgrade to 2.167.0, use AWS environment variable credentials but don't specify the account ID, and run cdk synthesize.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.167.0 (build 677e108)

Framework Version

No response

Node.js Version

v22.6.0

OS

Ubuntu 22.04.4 LTS

Language

.NET

Language Version

No response

Other information

No response

@AdamPD AdamPD added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 14, 2024
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Nov 14, 2024
@Neuroforge
Copy link

Neuroforge commented Nov 14, 2024

I got the same issue in Github Actions....

          name: Bootstrap Backend with CDK (Production)
                env:
                  AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_PROD}}
                  AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_PROD}}
                  CDK_DEPLOY_ACCOUNT: ${{ secrets.CDK_DEPLOY_ACCOUNT_PROD}}
                  CDK_DEPLOY_REGION: ${{ secrets.AWS_REGION_PROD}}
                  AWS_REGION: ${{ secrets.AWS_REGION_PROD}}
                working-directory: backend
                run: npx cdk bootstrap aws://$CDK_DEPLOY_ACCOUNT/$CDK_DEPLOY_REGION`

Errors with...

❌ Environment aws:/// failed bootstrapping: Error: Need to perform AWS calls for account ***, but no credentials have been configured

@AdamPD's solution worked for me but isn't suitable longterm.

Is this related?

#21562

@Eonfuzz
Copy link

Eonfuzz commented Nov 14, 2024

It's affecting our Azure cdk deploy pipelines too.

@RWS-RHC
Copy link

RWS-RHC commented Nov 14, 2024

Also* affecting us on Bitbucket Pipelines.

@christiandunn
Copy link

christiandunn commented Nov 14, 2024

I have noticed this issue on our CI/CD pipelines as well. AWS credential configuration that has worked before has stopped working. Confirmed it was on version 2.167.0.

@IgorPietraszko
Copy link

Same here. Downgraded to 2.166.0 and trying out.

@minimum-hsu
Copy link

In CDK 2.167.0, got an error meesage

botocore.exceptions.ClientError: An error occurred (UnrecognizedClientException) when calling the DescribeRepositories operation: The security token included in the request is invalid

After downgrading, the cdk diff and deploy jobs are functioning correctly.

@christiandunn
Copy link

Downgrading to the previous version also fixed the problem for me.

@raducostea
Copy link

Same for me, i have aws environment set up correctly. Downgrading to 2.166.0 fixed the issue.

Need to perform AWS calls for account ***, but no credentials have been configured

@otaviomacedo
Copy link
Contributor

@AdamPD (or anyone else in this thread) can you please paste the output of cdk deploy -vvv?

@otaviomacedo otaviomacedo added p0 and removed needs-triage This issue or PR still needs to be triaged. labels Nov 14, 2024
@scarytom
Copy link
Contributor

@otaviomacedo here is the -vvv output from a cdk diff, which works fine on 2.166.0

[09:08:41] [trace] SdkProvider#resolveEnvironment()
[09:08:41] [trace] SdkProvider#baseCredentialsPartition()
[09:08:41] [trace]   SdkProvider#resolveEnvironment()
[09:08:41] [trace]   SdkProvider#obtainBaseCredentials()
[09:08:41] [trace]     SdkProvider#defaultAccount()
[09:08:41] [trace]     SdkProvider#defaultCredentials()
[09:08:41] [trace]   SDK#currentAccount()
[09:08:41] Retrieved account ID YYYYYYYYYYYY from disk cache
[09:08:41] [trace] SdkProvider#forEnvironment()
[09:08:41] [trace]   SdkProvider#resolveEnvironment()
[09:08:41] [trace]   SdkProvider#obtainBaseCredentials()
[09:08:41] [trace]     SdkProvider#defaultAccount()
[09:08:41] [trace]     SdkProvider#defaultCredentials()
[09:08:41] [trace]   SdkProvider#withAssumedRole()
[09:08:41] Assuming role 'arn:aws:iam::XXXXXXXXXXXX:role/cdk-sbsl-infra-lookup-role-XXXXXXXXXXXX-us-east-1'.
[09:08:42] Assuming role failed: The security token included in the request is invalid
Could not assume role in target account using current credentials (which are for account YYYYYYYYYYYY) The security token included in the request is invalid . Please make sure that this role exists in the account. If it doesn't exist, (re)-bootstrap the environment with the right '--trust', using the latest version of the CDK CLI.
[09:08:42] [trace] SdkProvider#resolveEnvironment()
[09:08:42] [trace] SdkProvider#baseCredentialsPartition()
[09:08:42] [trace]   SdkProvider#resolveEnvironment()
[09:08:42] [trace]   SdkProvider#obtainBaseCredentials()
[09:08:42] [trace]     SdkProvider#defaultAccount()
[09:08:42] [trace]     SdkProvider#defaultCredentials()
[09:08:42] [trace]   SDK#currentAccount()
[09:08:42] Retrieved account ID YYYYYYYYYYYY from disk cache
[09:08:42] [trace] SdkProvider#forEnvironment()
[09:08:42] [trace]   SdkProvider#resolveEnvironment()
[09:08:42] [trace]   SdkProvider#obtainBaseCredentials()
[09:08:42] [trace]     SdkProvider#defaultAccount()
[09:08:42] [trace]     SdkProvider#defaultCredentials()
[09:08:42] [trace]   SdkProvider#withAssumedRole()
[09:08:42] Assuming role 'arn:aws:iam::XXXXXXXXXXXX:role/cdk-sbsl-infra-deploy-role-XXXXXXXXXXXX-us-east-1'.
[09:08:43] Assuming role failed: The security token included in the request is invalid
[09:08:43] Reading cached notices from /home/tom/.cdk/cache/notices.json
Could not assume role in target account using current credentials (which are for account YYYYYYYYYYYY) The security token included in the request is invalid . Please make sure that this role exists in the account. If it doesn't exist, (re)-bootstrap the environment with the right '--trust', using the latest version of the CDK CLI.
[09:08:43] Error: Could not assume role in target account using current credentials (which are for account YYYYYYYYYYYY) The security token included in the request is invalid . Please make sure that this role exists in the account. If it doesn't exist, (re)-bootstrap the environment with the right '--trust', using the latest version of the CDK CLI.
    at SdkProvider.withAssumedRole (/usr/lib/node_modules/aws-cdk/lib/index.js:593:5643098)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SdkProvider.forEnvironment (/usr/lib/node_modules/aws-cdk/lib/index.js:593:5639480)
    at async EnvironmentAccess.cachedSdkForEnvironment (/usr/lib/node_modules/aws-cdk/lib/index.js:647:7329)
    at async EnvironmentAccess.prepareSdk (/usr/lib/node_modules/aws-cdk/lib/index.js:647:6285)
    at async Deployments.readCurrentTemplateWithNestedStacks (/usr/lib/node_modules/aws-cdk/lib/index.js:647:8697)
    at async CdkToolkit.diff (/usr/lib/node_modules/aws-cdk/lib/index.js:647:201596)
    at async exec3 (/usr/lib/node_modules/aws-cdk/lib/index.js:650:18965)

@scarytom
Copy link
Contributor

Presume this is caused by #31702, but just a hunch

@otaviomacedo
Copy link
Contributor

otaviomacedo commented Nov 14, 2024

@scarytom Yes, it was. In your case, the CLI is getting credentials for one account, but trying to call another. Is there anything you can share, like your ~/.aws/config file (with confidential data redacted, of course)?

@otbe
Copy link

otbe commented Nov 14, 2024

For us the issue is in the region, e.g.

deploy -vvv

10:38:42] Looking up AWS region in the EC2 Instance Metadata Service (IMDS).
[10:38:43] Unable to retrieve AWS region from IMDS: Error: Error fetching metadata token: TimeoutError: Connection timed out after 1000 ms
[10:38:43] Unable to determine AWS region from environment or AWS configuration (profile: "default"), defaulting to 'us-east-1'

Not quite sure how it worked in older aws-cdk versions but the region is specified in .aws/credentials similar to this

[default]
aws_access_key_id=
aws_secret_access_key=
aws_session_token=
aws_security_token=
region=eu-central-1

That means our stacks are deployed in the wrong region with 2.167.0 🫠
Reverting back to 2.166.0 solves the issue.

@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 14, 2024

@otbe:

  1. Do you also have a ~/.aws/config file?
  2. Were you running this code on an EC2 instance?

@AdamPD
Copy link
Author

AdamPD commented Nov 14, 2024

To be clear on my side, I am running this within AppVeyor on a clean build each time, and we do not have any ~/.aws directory with any configuration - our configuration is 100% through environment variables.

@mrgrain mrgrain added the management/tracking Issues that track a subject or multiple issues label Nov 14, 2024
@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 14, 2024

Can confirm that if you do not have a ~/.aws directory on your machine and configure using AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN, it fails on my machine as well.

Nevermind I typo'ed the region ☹️. Actually works fine.

@scarytom
Copy link
Contributor

@otaviomacedo no, we don't us an ~/.aws/config file. Our CI system just sets the AWS credentials via AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env vars, and our CDK Environment then picks one of our other AWS accounts that has been cdk-bootstrapped to allow access via RBAC.

This setup has been working fine for years, and now fails with 2.167.0

@otbe
Copy link

otbe commented Nov 14, 2024

@rix0rrr

 1. Do you also have a `~/.aws/config` file?

2. Were you running this code on an EC2 instance?
  1. no ~/.aws/config
  2. Yes it was executed on EC2 (however in a docker container)

GeoWill added a commit to DemocracyClub/UK-Polling-Stations that referenced this issue Nov 14, 2024
@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 14, 2024

There are 2 distinct problems being discussed in this thread. While they are both introduced by switching from SDKv2 to SDKv3 we should make sure to distinguish them.

  • The security token included in the request is invalid: seems to happen in some circumstances on CI machines, where configuration happens entirely through environment variables.
  • Unable to determine AWS region from environment or AWS configuration (profile: "default"), defaulting to 'us-east-1': the region that is present in a credentials file is not picked up.

This issue was originally created for the first issue by @AdamPD. @otbe, I will make a new issue for your error so we can discuss that there.

Edit: new issue here: #32130

@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 14, 2024

On the The security token included in the request is invalid error: it occurs to me that the users reporting this seem NOT to be using session credentials, but IAM User credentials instead (access key pair without a session token).

Maybe that's a lead...

@rix0rrr rix0rrr changed the title CDK CLI: 2.167.0 breaks deployment CDK CLI: "The security token included in the request is invalid" since 2.167.0 Nov 14, 2024
@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 14, 2024

Woohoo, this reproduces! IAM User credentials seem broken:

$ ls ~/.aws
ls: /Users/huijbers/.aws: No such file or directory

$ env | grep AWS_
AWS_ACCESS_KEY_ID=*****
AWS_DEFAULT_REGION=us-east-2
AWS_SECRET_ACCESS_KEY=*******

$ npx cdk deploy -vvv
...
[11:33:04] Unable to determine the default AWS account (InvalidClientTokenId): The security token included in the request is invalid
...
Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment

@mrgrain
Copy link
Contributor

mrgrain commented Nov 14, 2024

We have deprecated [email protected] on npmjs.com while we continue our investigation.

Please note The aws-cdk-lib and other @aws-cdk/* packages are not effected by the deprecation.

GeoWill added a commit to DemocracyClub/UK-Polling-Stations that referenced this issue Nov 14, 2024
@gsingh1
Copy link

gsingh1 commented Nov 14, 2024

I can confirm the same issue happening on GHE instances too. NPM package deprecation is working as expected and our builds are clearing on 2.166.0. Thanks.

@rix0rrr rix0rrr changed the title CDK CLI: "The security token included in the request is invalid" since 2.167.0 CDK CLI: "The security token included in the request is invalid" when using user credentials since 2.167.0 Nov 14, 2024
@mrgrain mrgrain pinned this issue Nov 14, 2024
@mrgrain mrgrain changed the title CDK CLI: "The security token included in the request is invalid" when using user credentials since 2.167.0 ‼️ Notice: CDK CLI: "The security token included in the request is invalid" when using user credentials since 2.167.0 Nov 14, 2024
@mergify mergify bot closed this as completed in #32134 Nov 14, 2024
@mergify mergify bot closed this as completed in 9ef4e72 Nov 14, 2024
@mrgrain mrgrain reopened this Nov 14, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2024
@aws aws deleted a comment from github-actions bot Nov 14, 2024
@aws aws deleted a comment from github-actions bot Nov 14, 2024
iliapolo pushed a commit that referenced this issue Nov 14, 2024
…32134)

In Node.js, if you assign `undefined` to an environment variable, that variable ends up having the string `"undefined"`.

If we are using IAM user credentials, `AWS_SESSION_TOKEN` should not be set, but because we were not handling this edge case, it was getting assigned an invalid value:

```
Welcome to Node.js v22.9.0.
Type ".help" for more information.
> process.env.AWS_SESSION_TOKEN || process.env.AMAZON_SESSION_TOKEN
undefined
> process.env.AWS_SESSION_TOKEN = process.env.AWS_SESSION_TOKEN || process.env.AMAZON_SESSION_TOKEN
undefined
> process.env.AWS_SESSION_TOKEN
'undefined'
```

Closes #32120.

- [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@ashishdhingra
Copy link
Contributor

ashishdhingra commented Nov 14, 2024

@github-actions github-actions bot added the potential-regression Marking this issue as a potential regression to be checked by team member label Nov 14, 2024
@mergify mergify bot closed this as completed in 425efbc Nov 15, 2024
@mrgrain mrgrain removed the potential-regression Marking this issue as a potential regression to be checked by team member label Nov 15, 2024
@mrgrain mrgrain changed the title ‼️ Notice: CDK CLI: "The security token included in the request is invalid" when using user credentials since 2.167.0 ❗Notice: CDK CLI: "The security token included in the request is invalid" when using user credentials since 2.167.0 Nov 15, 2024
@github-actions github-actions bot added the potential-regression Marking this issue as a potential regression to be checked by team member label Nov 15, 2024
@mrgrain
Copy link
Contributor

mrgrain commented Nov 16, 2024

Hi all, we have released v2.167.1 with fixes for the reported auth issues.

Please try again and report back. Reports that everything is working are equally useful as failures.

@mrgrain mrgrain added the sdk-v3-upgrade Tag issues that are associated to SDK V3 upgrade. Not limited to CR usage of SDK only. label Nov 17, 2024
@github-actions github-actions bot removed the potential-regression Marking this issue as a potential regression to be checked by team member label Nov 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. management/tracking Issues that track a subject or multiple issues p0 package/tools Related to AWS CDK Tools or CLI sdk-v3-upgrade Tag issues that are associated to SDK V3 upgrade. Not limited to CR usage of SDK only.
Projects
None yet
Development

Successfully merging a pull request may close this issue.