Skip to content

feat(credential-provider-assume-role): add fromTokenFile credential provider #2147

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

Closed
wants to merge 10 commits into from

Conversation

trivikr
Copy link
Member

@trivikr trivikr commented Mar 17, 2021

Issue

Fixes: #1808

Description

Adds fromTokenFile credential provider to read credentials from EKS service account.

  • Reads file location of where the OIDC token is stored from either environment or config file parameters.
  • Reads IAM role wanting to be assumed from either environment or config file paramters.
  • Reads optional role session name to be used to distinguish sessions from either environment or config file paramters.
    If session name is not defined, it comes up with a role session name.
  • Reads OIDC roken from file on disk.
  • Calls sts:AssumeRoleWithWebIdentity to get credentials.
  • Uses credentials of source_profile to assume the role specified if specified.
Environment Variable Config Variable Required Description
AWS_WEB_IDENTITY_TOKEN_FILE web_identity_token_file true File location of where the OIDC token is stored
AWS_IAM_ROLE_ARN role_arn true The IAM role wanting to be assumed
AWS_IAM_ROLE_SESSION_NAME role_session_name false The IAM session name used to distinguish sessions

Testing

A basic example of using fromTokenFile:

import { STSClient, AssumeRoleWithWebIdentityCommand } from "@aws-sdk/client-sts";
import { fromTokenFile } from "@aws-sdk/credential-provider-assume-role";

const stsClient = new STSClient({});

const roleAssumerWithWebIdentity = async (params) => {
  const { Credentials } = await stsClient.send(
    new AssumeRoleWithWebIdentityCommand(params)
  );
  if (!Credentials || !Credentials.AccessKeyId || !Credentials.SecretAccessKey) {
    throw new Error(`Invalid response from STS.assumeRole call with role ${params.RoleArn}`);
  }
  return {
    accessKeyId: Credentials.AccessKeyId,
    secretAccessKey: Credentials.SecretAccessKey,
    sessionToken: Credentials.SessionToken,
    expiration: Credentials.Expiration,
  };
};

const client = new FooClient({
  credentials: fromTokenFile({
    roleAssumerWithWebIdentity
  });
});

Values in environment variables

The values can be defined in environment varaibles as follows:

$ node
> Object.fromEntries(Object.entries(process.env).filter(([key, value]) => key.startsWith("AWS_")));
{
  AWS_WEB_IDENTITY_TOKEN_FILE: '/temp/token',
  AWS_ROLE_ARN: 'arn:aws:iam::123456789012:role/example-role-arn'
}

Values in configuration files

The values can be defined in configuration files as follows:

[sample-profile]
web_identity_token_file = /temp/token
role_session_name = arn:aws:iam::123456789012:role/example-role-arn

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@trivikr trivikr changed the title chore(credential-provider-assume-role): add scaffolding with basic README feat(credential-provider-assume-role): add fromTokeFile credential provider Mar 17, 2021
@trivikr trivikr changed the title feat(credential-provider-assume-role): add fromTokeFile credential provider feat(credential-provider-assume-role): add fromTokenFile credential provider Mar 17, 2021
@trivikr trivikr force-pushed the credential-provider-assume-role branch from f1ec193 to 286099b Compare March 18, 2021 15:59
@codecov-io
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (main@906e4b0). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2147   +/-   ##
=======================================
  Coverage        ?   78.74%           
=======================================
  Files           ?      381           
  Lines           ?    16129           
  Branches        ?     3463           
=======================================
  Hits            ?    12701           
  Misses          ?     3428           
  Partials        ?        0           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 906e4b0...286099b. Read the comment docs.

@trivikr trivikr requested a review from AllanZhengYP March 18, 2021 17:20
[source_profile]: true,
});

return options.roleAssumer(await sourceCreds, { RoleArn, RoleSessionName });
Copy link
Contributor

@AllanZhengYP AllanZhengYP Mar 24, 2021

Choose a reason for hiding this comment

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

If we allow roleAssumer chaining here, should we allow other assume role options? Like MFA serial, and MFA token? I'm comparing to normal INI credential provider

if (mfa_serial) {
if (!options.mfaCodeProvider) {
throw new ProviderError(
`Profile ${profileName} requires multi-factor authentication,` + ` but no MFA code callback was provided.`,
false
);
}
params.SerialNumber = mfa_serial;
params.TokenCode = await options.mfaCodeProvider(mfa_serial);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

If we allow roleAssumer chaining here, should we allow other assume role options?

Short answer: Yes.
Long answer: Not in this PR. A separate PR should be created to introduce reusable fromAssumeRole function.

@trivikr trivikr force-pushed the credential-provider-assume-role branch from bae849a to 37864ed Compare March 24, 2021 17:36
@aws-sdk-js-automation
Copy link

AWS CodeBuild CI Report

  • CodeBuild project: sdk-staging-test
  • Commit ID: 37864ed
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@trivikr
Copy link
Member Author

trivikr commented Mar 24, 2021

Decision after offline discussion with @AllanZhengYP :

The fromProcess and fromSSO credentials providers will be called from fromIni in future PRs.

@trivikr
Copy link
Member Author

trivikr commented Mar 25, 2021

Closing in favor of the following PRs:

@trivikr trivikr closed this Mar 25, 2021
@github-actions
Copy link

github-actions bot commented Apr 8, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 8, 2021
@trivikr trivikr deleted the credential-provider-assume-role branch June 1, 2021 22:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Credentials from EKS service account
5 participants