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

feat(cli): MFA support #6510

Merged
merged 2 commits into from Aug 17, 2020
Merged

feat(cli): MFA support #6510

merged 2 commits into from Aug 17, 2020

Conversation

nikolauska
Copy link
Contributor

@nikolauska nikolauska commented Feb 28, 2020

With these changes AWS CDK now supports mfa_serial field so when profile has mfa_serial set the user is asked for MFA token. If user then adds corrects short lived token they will get access to environment.

Example config for assume role with MFA that will be supported after these changes.

[profile mfa]
region=eu-west-1

[profile mfa-role]
source_profile=mfa
role_arn=arn:aws:iam::account:role/role
mfa_serial=arn:aws:iam::account:mfa/user

These changes currently only have one test as I don't have enough knowledge of the code base to write better tests. Current test only checks that user is asked for token by looking at the error message which should result in invalid token.

Fixes: #1248


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@nikolauska nikolauska changed the title feat(cli): Config file and MFA support feat(cli): MFA support Feb 28, 2020
@nikolauska nikolauska marked this pull request as ready for review March 2, 2020 18:42
@nikolauska
Copy link
Contributor Author

Merged to latest master after there were lot of changes to authentication. Changes required are now much smaller than earlier as I only needed to add the tokenCodeFn function.

@nikolauska
Copy link
Contributor Author

Seems like the build failed for 503 service unavailable error which should not be related to these changes

@shivlaks
Copy link
Contributor

Seems like the build failed for 503 service unavailable error which should not be related to these changes

@nikolauska yeah it's unrelated, I'll see if i can trigger a re-run of the build for you

}

if (await fs.pathExists(configFileName())) {
sources.push(() => new AWS.SharedIniFileCredentials({ profile, filename: credentialsFileName() }));
sources.push(() => new AWS.SharedIniFileCredentials({ profile, filename: credentialsFileName(), tokenCodeFn }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this block slipped in accidentally during a refactor of mine.

Mind taking it out? <3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing :)

That line looked weird to me, but as it did not seem to cause issue when I was testing it I decided to keep it.

Copy link
Contributor

@shivlaks shivlaks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Add Tests
  • Update README
  • Add more motivation and approach details into the commit body. (i.e. why is it failing today, what are we missing, what are we adding, etc..)

packages/aws-cdk/lib/api/aws-auth/awscli-compatible.ts Outdated Show resolved Hide resolved
*/
async function tokenCodeFn(serial: string, cb: (err?: Error, token?: string) => void): Promise<void> {
debug('Require MFA token for serial', serial);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the device serial or the serial ARN?

@mergify mergify bot dismissed shivlaks’s stale review April 9, 2020 21:13

Pull request has been modified.

@nikolauska
Copy link
Contributor Author

Squashed all the commits and added more info to commit body.

There is now also one test, but with limited knowledge of bigger code base it was the only one I got working. Also added section to README.md about the MFA support.

@vastamaki
Copy link

@shivlaks Any updates about this?

@shivlaks
Copy link
Contributor

@nikolauska @vastamaki sorry about the delay, didn't realize this PR was updated. I'll take a look this week. stay tuned!

Copy link
Contributor

@shivlaks shivlaks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good to me! had a question about the additional dependency we would be introducing here with inquirer

packages/aws-cdk/package.json Outdated Show resolved Hide resolved
@shivlaks shivlaks added the pr/do-not-merge This PR should not be merged at this time. label May 8, 2020
shivlaks
shivlaks previously approved these changes May 8, 2020
Copy link
Contributor

@shivlaks shivlaks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you also have a conflict to resolve here.

Otherwise I'm okay with it. Added the do-not-merge label so @rix0rrr can also have a look

packages/aws-cdk/README.md Show resolved Hide resolved
await provider.withAssumedRole('arn:aws:iam::account:role/role', undefined, undefined);
} catch (e) {
// Mock token cannot work, but having this error means user was asked for MFA token
expect(e.message).toEqual('The security token included in the request is invalid.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sure hope this test doesn't do any actual calls to STS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it will as it will ask token before calling STS, but I'm not 100% sure about that. If there is better way to test it I will change to use that as I just used that other role test as base and just changed it by adding mfa_serial to it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked into the aws-sdk source code and according to the load function it will ask for token before continuing with assumeRole call. As my original test did return token it made STS call, but it failed on the next step as it was not a valid token. I've changed my test so that it now throws error so it won't call assumeRole.

Link to the code part in the aws-sdk I mentioned:
https://github.com/aws/aws-sdk-js/blob/75690eb8edb4f90dbcac1e8ad03847262f8cddf4/lib/credentials/shared_ini_file_credentials.js#L233

@dnascimento
Copy link

Hi @nikolauska , would you have a chance to review and have it merged? Your contribution will help large orgs to adopt CDK, thanks for making it

Copy link
Contributor

@shivlaks shivlaks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • pending verification that unit tests don't make actual calls to STS

@nikolauska I also believe we can remove the yarn.lock changes in this PR as we are no longer adding a new dependency for prompting users

@mergify mergify bot dismissed shivlaks’s stale review July 16, 2020 09:02

Pull request has been modified.

With this change AWS CDK supports MFA. Specifically it
will support mfa_serial field in the profile config by asking
user for MFA token for the mfa_serial field ARN.

AWS SDK has support for this built in so only change is adding
tokenCodeFn function to sharedIniCredentials options. Callback
sent to that function is used to return token back to SDK.

Inquirer package is used to create interactive prompt for user
to type the MFA token.
@nikolauska
Copy link
Contributor Author

@shivlaks I've now removed the yarn.lock changes and made one change to MFA test to make sure it won't call the assumeRole.

@johan-lindqvist
Copy link

This looks great! Hopefully this gets merged soon, thanks for the great work @nikolauska

@nikolauska
Copy link
Contributor Author

@shivlaks Any news on getting this merged?

Copy link
Contributor

@shivlaks shivlaks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this contribution!

@christophgysin
Copy link
Contributor

@shivlaks Now that inquirer has been replaced with promptly, is there still something that blocks this from being merged? I'm asking because it is still labelled pr/do-not-merge?

@shivlaks
Copy link
Contributor

@shivlaks Now that inquirer has been replaced with promptly, is there still something that blocks this from being merged? I'm asking because it is still labelled pr/do-not-merge?

good catch, my miss on dropping the label

@shivlaks shivlaks removed the pr/do-not-merge This PR should not be merged at this time. label Aug 17, 2020
@mergify
Copy link
Contributor

mergify bot commented Aug 17, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: a4c204e
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Aug 17, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature-request: support mfa_serial (consistency with aws cli)
8 participants