Skip to content

Commit b7bc10c

Browse files
authored
fix: Default username in RoleSessionName (#20188)
In case user does not have entry in `/etc/passwd` the `os.userInfo()` call will throw `SystemError` exception as documented: https://nodejs.org/docs/latest-v16.x/api/os.html#osuserinfooptions Fixes #19401 issue. It can be tested inside Docker for ad-hoc 1234 user ID: ```sh docker run -u 1234 -e CDK_HOME=/tmp npm run cdk diff ``` The `CDK_HOME=/tmp` is a workaround for #7937 issue, where CDK complains that it can't write cached info in user homedir, because it does not exists. Once #7937 will be fixed then #19401 will most likely hit users. However above workaround is a viable option. Hence those two issues are related, but not duplicated. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) Yes, followed the guide. ### Adding new Unconventional Dependencies: * [x] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) No new dependencies. ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? No, it's a bugfix, not a feature. *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6f4aba8 commit b7bc10c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

packages/aws-cdk/lib/api/aws-auth/sdk-provider.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,11 @@ function readIfPossible(filename: string): string | undefined {
459459
* @see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html#API_AssumeRole_RequestParameters
460460
*/
461461
function safeUsername() {
462-
return os.userInfo().username.replace(/[^\w+=,.@-]/g, '@');
462+
try {
463+
return os.userInfo().username.replace(/[^\w+=,.@-]/g, '@');
464+
} catch (e) {
465+
return 'noname';
466+
}
463467
}
464468

465469
/**

packages/aws-cdk/test/api/sdk-provider.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,34 @@ describe('with intercepted network calls', () => {
341341
});
342342
});
343343

344+
test('assuming a role does not fail when OS username cannot be read', async () => {
345+
// GIVEN
346+
prepareCreds({
347+
fakeSts,
348+
config: {
349+
default: { aws_access_key_id: 'foo', $account: '11111' },
350+
},
351+
});
352+
353+
await withMocked(os, 'userInfo', async (userInfo) => {
354+
userInfo.mockImplementation(() => {
355+
// SystemError thrown as documented: https://nodejs.org/docs/latest-v16.x/api/os.html#osuserinfooptions
356+
throw new Error('SystemError on Linux: uv_os_get_passwd returned ENOENT. See #19401 issue.');
357+
});
358+
359+
// WHEN
360+
const provider = await providerFromProfile(undefined);
361+
362+
const sdk = (await provider.forEnvironment(env(uniq('88888')), Mode.ForReading, { assumeRoleArn: 'arn:aws:role' })).sdk as SDK;
363+
await sdk.currentAccount();
364+
365+
// THEN
366+
expect(fakeSts.assumedRoles[0]).toEqual(expect.objectContaining({
367+
roleSessionName: 'aws-cdk-noname',
368+
}));
369+
});
370+
});
371+
344372
test('even if current credentials are for the wrong account, we will still use them to AssumeRole', async () => {
345373
// GIVEN
346374
prepareCreds({

packages/cdk-assets/lib/aws.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ export class DefaultAwsClient implements IAws {
150150
* @see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html#API_AssumeRole_RequestParameters
151151
*/
152152
function safeUsername() {
153-
return os.userInfo().username.replace(/[^\w+=,.@-]/g, '@');
153+
try {
154+
return os.userInfo().username.replace(/[^\w+=,.@-]/g, '@');
155+
} catch (e) {
156+
return 'noname';
157+
}
154158
}
155159

0 commit comments

Comments
 (0)