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

Add initial config profile writer sample #557

Closed

Conversation

pecigonzalo
Copy link

@pecigonzalo pecigonzalo commented Oct 27, 2022

Issue #: #112

Description of changes: Add support to store multiple profiles in config


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


I don't know what format this repo uses, VSCode by default formats like this.

Comment on lines +200 to +230
function exportCredentialsToConfig(profile, params) {
const awsConfigFile = "~/.aws/config";
if (fs.existsSync(awsConfigFile)) {
var config = ini.parse(fs.readFileSync(awsConfigFile, "utf-8"));
} else {
var config = {};
}

// Initialize the configuration object
const profileName = `profile ${profile}`;
config[profileName] = {};

// Configure the AWS CLI and AWS SDKs using environment variables and set them as secrets.
// Setting the credentials as secrets masks them in Github Actions logs
const { webIdentityTokenFile, roleArn, regionName } = params;

// web_identity_token_file:
// Specifies a web identity token file location
config[profileName].web_identity_token_file = webIdentityTokenFile;

// role_arn:
// Specifies the role to assume by this profile
config[profileName].role_arn = roleArn;

// region:
// Specifies the region for this profile
config[profileName].region = regionName;

fs.writeFileSync(awsConfigFile, ini.stringify(config));
}

Copy link
Author

Choose a reason for hiding this comment

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

This is the main change

Choose a reason for hiding this comment

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

So you don't need a credentials file? Only a config file will suffice?

Copy link
Author

Choose a reason for hiding this comment

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

The config needs to point to a webIdentityToken file, which is already supported. We just need to point to it from multiple profiles.

@pecigonzalo pecigonzalo mentioned this pull request Oct 27, 2022
@jfagoagas
Copy link

jfagoagas commented Oct 31, 2022

Hi @pecigonzalo, I was just about to create a new PR to support this workflow. I understand you want to include the feature to save the assumed credentials in the ${HOME}/.aws/config, right?

In my use case I need the following step because a tool within the workflow requires to load the config from the ${HOME}/.aws/config so to configure a secure flow I have to do the following:

     - name: Configure AWS Credentials
       uses: aws-actions/configure-aws-credentials@v1
       with:
          aws-region: ${{ env.AWS_REGION }}
          role-to-assume: ${{ env.AWS_ROLE }}
          role-session-name: session-role

     - name: Set AWS Credentials file
       run: |
          [[ ! -d "${HOME}/.aws" ]] && mkdir ${HOME}/.aws
          echo "[profile ${{ env.ENV }}]" >> ${HOME}/.aws/config && \
          echo "aws_access_key_id=${{ env.AWS_ACCESS_KEY_ID }}" >> ${HOME}/.aws/config && \
          echo "aws_secret_access_key=${{ env.AWS_SECRET_ACCESS_KEY }}" >> ${HOME}/.aws/config && \
          echo "aws_session_token=${{ env.AWS_SESSION_TOKEN }}" >> ${HOME}/.aws/config

Thanks!

@bconnorwhite
Copy link

@jfagoagas Being able to set a custom path for the .aws directory would also be great. As an example with this action you need .aws to be in the action workspace, since ~/.aws isn't mounted to the image:
dflook/terraform-github-actions#13 (comment)

@jfagoagas
Copy link

@bconnorwhite totally agree, it would be great!

@pecigonzalo
Copy link
Author

Agree with all comments and features, I opened this to confirm the approach with the contributors in the linked issue. If they are happy with it, I'll gladly support the use cases you highlighted

const nameTruncated = nameWithoutSpecialCharacters.slice(0, MAX_TAG_VALUE_LENGTH)
return nameTruncated
const nameWithoutSpecialCharacters = name.replace(
/[^\p{L}\p{Z}\p{N}_:/=+.-@-]/gu,

Check warning

Code scanning / CodeQL

Overly permissive regular expression range

Suspicious character range that is equivalent to \[.\/0-9:;<=>?@\].
@pecigonzalo
Copy link
Author

Closing in favor of #633

@pecigonzalo pecigonzalo deleted the feature/oidc-profile branch January 24, 2023 23:09
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.

None yet

4 participants