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

(api/aws-auth): Error on CDK commands when user does not exist #19401

Closed
adambro opened this issue Mar 15, 2022 · 4 comments · Fixed by #20188
Closed

(api/aws-auth): Error on CDK commands when user does not exist #19401

adambro opened this issue Mar 15, 2022 · 4 comments · Fixed by #20188
Assignees
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. p2 package/tools Related to AWS CDK Tools or CLI

Comments

@adambro
Copy link
Contributor

adambro commented Mar 15, 2022

What is the problem?

In some cases (i.e. Docker container) a system user might not be in /etc/passwd file. It causes cdk diff (for example) to fail.

Reproduction Steps

For any CDK project run cli via docker container that has newly created user. for example:

docker run --rm -v `pwd`:/usr/src/app -w /usr/src/app -u 1001 -e NO_UPDATE_NOTIFIER=1 -e npm_config_cache=/tmp -e CDK_HOME=/tmp -v ~/.aws:/.aws node:14 npm run cdk diff

What did you expect to happen?

By looking at code of safeUsername() function it's just used as part of RoleSessionName name. It could have a nice fallback to i.e. $USER env var or just predefined default value. The function is very simple:

function safeUsername() {
    return os.userInfo().username.replace(/[^\w+=,.@-]/g, '@');
}

What actually happened?

It causes cdk diff (for example) to fail with following stack trace:

Stack EksCdkStack
Reading existing template for stack EksCdkStack.
Retrieved account ID xxx from disk cache
Assuming role 'arn:aws:iam::xxx:role/cdk-hnb659fds-lookup-role-xxx-eu-west-1'.
SystemError [ERR_SYSTEM_ERROR]: A system error occurred: uv_os_get_passwd returned ENOENT (no such file or directory)
    at new SystemError (internal/errors.js:217:5)
    at new NodeError (internal/errors.js:302:7)
    at Object.userInfo (os.js:347:11)
    at safeUsername (/usr/src/app/node_modules/aws-cdk/lib/api/aws-auth/sdk-provider.ts:462:13)
    at SdkProvider.withAssumedRole (/usr/src/app/node_modules/aws-cdk/lib/api/aws-auth/sdk-provider.ts:348:37)
    at SdkProvider.forEnvironment (/usr/src/app/node_modules/aws-cdk/lib/api/aws-auth/sdk-provider.ts:189:28)

CDK CLI Version

2.10.0 (build e5b301f)

Framework Version

No response

Node.js Version

v14.19.0

OS

Ubuntu 20.04

Language

Typescript

Language Version

No response

Other information

I can contribute PR if you wish. Any hints of fallback value are appreciated :)

@adambro adambro added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 15, 2022
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Mar 15, 2022
@adambro adambro changed the title (api/aws-auth): Error on CDK commands when user does exist (api/aws-auth): Error on CDK commands when user does not exist Mar 15, 2022
@rittneje
Copy link

We are seeing the same from cdk synth. And we already have role credentials, so it shouldn't be trying to calculate a role session name.

With regards to the fallback, maybe just use the uid? Might also be helpful to include the hostname too.

@ryparker ryparker added the p2 label Mar 15, 2022
@rix0rrr
Copy link
Contributor

rix0rrr commented Mar 18, 2022

Duplicate of #7937.

@rix0rrr rix0rrr closed this as completed Mar 18, 2022
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@adambro
Copy link
Contributor Author

adambro commented Mar 18, 2022

Hey @rix0rrr the #7937 bug is about problem with cdkHomeDir, which I took care of by setting CDK_HOME=/tmp env var. I bet if people do set CDK_HOME they will encounter the problem with safeUsername() as I described here. So those are related, but not duplicated.

adambro added a commit to adambro/aws-cdk that referenced this issue May 3, 2022
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 aws#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 aws#7937 issue, where CDK complains
that it can't write cached info in user homedir, because it does not
exists.

Once aws#7937 will be fixed then aws#19401 will most likely hit users. However
above workaround is a viable option. Hence those two issues are related,
but not duplicated.
mergify bot pushed a commit that referenced this issue May 27, 2022
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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants