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

chore(release): 2.167.1 #32140

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(cli): failure to get credentials when session token is not set (#…
…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.

### Checklist
- [ ] 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*
  • Loading branch information
otaviomacedo authored and mrgrain committed Nov 14, 2024
commit 425efbc3625d4512b83225dcb1d1c155d13b4b9e
7 changes: 6 additions & 1 deletion packages/aws-cdk/lib/api/aws-auth/awscli-compatible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,16 @@ function caBundlePathFromEnvironment(): string | undefined {
function shouldPrioritizeEnv() {
const id = process.env.AWS_ACCESS_KEY_ID || process.env.AMAZON_ACCESS_KEY_ID;
const key = process.env.AWS_SECRET_ACCESS_KEY || process.env.AMAZON_SECRET_ACCESS_KEY;
process.env.AWS_SESSION_TOKEN = process.env.AWS_SESSION_TOKEN || process.env.AMAZON_SESSION_TOKEN;

if (!!id && !!key) {
process.env.AWS_ACCESS_KEY_ID = id;
process.env.AWS_SECRET_ACCESS_KEY = key;

const sessionToken = process.env.AWS_SESSION_TOKEN ?? process.env.AMAZON_SESSION_TOKEN;
if (sessionToken) {
process.env.AWS_SESSION_TOKEN = sessionToken;
}

return true;
}

Expand Down
45 changes: 45 additions & 0 deletions packages/aws-cdk/test/api/aws-auth/awscli-compatible.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { AwsCliCompatible } from '../../../lib/api/aws-auth/awscli-compatible';

describe('Session token', () => {
beforeEach(() => {
process.env.AWS_ACCESS_KEY_ID = 'foo';
process.env.AWS_SECRET_ACCESS_KEY = 'bar';
});

test('does not mess up with session token env variables if they are undefined', async () => {
// Making sure these variables are not defined
delete process.env.AWS_SESSION_TOKEN;
delete process.env.AMAZON_SESSION_TOKEN;

await AwsCliCompatible.credentialChainBuilder();

expect(process.env.AWS_SESSION_TOKEN).toBeUndefined();
});

test('preserves AWS_SESSION_TOKEN if it is defined', async () => {
process.env.AWS_SESSION_TOKEN = 'aaa';
delete process.env.AMAZON_SESSION_TOKEN;

await AwsCliCompatible.credentialChainBuilder();

expect(process.env.AWS_SESSION_TOKEN).toEqual('aaa');
});

test('assigns AWS_SESSION_TOKEN if it is not defined but AMAZON_SESSION_TOKEN is', async () => {
delete process.env.AWS_SESSION_TOKEN;
process.env.AMAZON_SESSION_TOKEN = 'aaa';

await AwsCliCompatible.credentialChainBuilder();

expect(process.env.AWS_SESSION_TOKEN).toEqual('aaa');
});

test('preserves AWS_SESSION_TOKEN if both are defined', async () => {
process.env.AWS_SESSION_TOKEN = 'aaa';
process.env.AMAZON_SESSION_TOKEN = 'bbb';

await AwsCliCompatible.credentialChainBuilder();

expect(process.env.AWS_SESSION_TOKEN).toEqual('aaa');
});
});