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

Assuming two roles in the same region in the same account fails #236

Closed
dbogen opened this issue Jul 13, 2021 · 7 comments
Closed

Assuming two roles in the same region in the same account fails #236

dbogen opened this issue Jul 13, 2021 · 7 comments
Labels
bug Something isn't working effort/small This issue will take less than a day of effort to fix p2

Comments

@dbogen
Copy link

dbogen commented Jul 13, 2021

If I use this action to assume a role in a given region in a given account, perform some actions, and then try to assume a different role in the same region and the same account, it looks like the action defaults to using the first role to assume the second role.

In this case, I'm trying to use the first role (source role) to copy from a source S3 bucket to a local store, assuming the second role (target role), and using that second role to copy to a target S3 bucket.

My action looks generally like this:

---
name: Deploy

on:
  workflow_dispatch:

env:
  MY_AWS_REGION: us-west-2
  MY_TARGET_S3_BUCKET: foo-target
  MY_AWS_ACCOUNT_ID: ${{ secrets.MY_AWS_ACCOUNT_ID }}
  MY_SRC_S3_BUCKET: foo-src

jobs:
  build_and_deploy:
    runs-on: [self-hosted, linux, small]
    name: Deploy to S3
    timeout-minutes: 10
    steps:
      - name: Get AWS credentials for source S3 bucket 
        id: get-src-creds
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: ${{ env.MY_AWS_REGION }}
          role-to-assume: arn:aws:iam::${{ env.MY_AWS_ACCOUNT_ID }}:role/foo-src
          role-duration-seconds: 1800

      - name: Copy from source S3 bucket
        id: copy-from-s3
        shell: bash
        run: |
          aws s3 sync --only-show-errors --no-progress --delete  "s3://${MY_SRC_S3_BUCKET}" ${GITHUB_WORKSPACE}

      - name: Get AWS credentials for target S3 bucket
        id: get-target-creds
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: ${{ env.MY_AWS_REGION }}
          role-to-assume: arn:aws:iam::${{ env.MY_AWS_ACCOUNT_ID }}:role/foo-target
          role-duration-seconds: 1800

      - name: Copy to target S3 bucket
        id: copy-to-s3
        shell: bash
        run: |
          aws s3 sync --only-show-errors --no-progress --delete  ${GITHUB_WORKSPACE} "s3://${MY_TARGET_S3_BUCKET}" 

When running that action, the source role assumption succeeds and I'm able to copy files from the source S3 bucket.

The target role assumption fails with the following error:

Error: User: arn:aws:sts::***:assumed-role/foo-src/GitHubActions is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::***:role/foo-target

That error is true; the source role is not authorized to assume the target role.

My assumption was that the action would use the self-hosted runner's instance profile and associated role to assume the second role (which is how it assumed the first role), rather than using the first role to assume the second role.

Is there a workaround to assume a second role in the same region and account where a first role was assumed? Should I instead be passing some sort of flag to tell the action unset all the environment variables it previously set before it tries to assume a new role?

@allisaurus
Copy link
Contributor

Hi @dbogen , thanks for reporting this!

So I think I may know what's going on - this action does reset the session token to account for multiple runs, but the check only triggers when you're providing an AWS_ACCESS_KEY and AWS_SECRET_KEY, vs. relying on an instance role.

I'm still undecided whether this action should always reset these before running - it may sometimes be desired to assume a role with temp creds - but in the meantime, can you try explicitly resetting these vars and then checking the caller identity in your workflow to see if that will get you back to a state which is using the instance role? I.e, add these 2 steps after your Copy from source S3 bucket step:

- name: reset creds
  shell: bash
  run: |
       echo "AWS_ACCESS_KEY_ID=" >> $GITHUB_ENV
       echo "AWS_SECRET_ACCESS_KEY=" >> $GITHUB_ENV
       echo "AWS_SESSION_TOKEN=" >> $GITHUB_ENV

 # should output the ARN of your instance role, if it's now being used
 - name: identity check
   shell: bash
   run: aws sts get-caller-identity

@dbogen
Copy link
Author

dbogen commented Jul 19, 2021

Thanks for the response, @allisaurus .

Resetting those credentials in a separate step is the workaround I ended up using so, yes, that works.

However, it would be great to have a flag (something like, reset-session-token or use-instance-role) to signal to the action that using whatever credentials currently exist in environment variables is not desired when assuming the next role.

@allisaurus allisaurus added feature-request A feature should be added or improved. and removed investigating labels Jul 19, 2021
@dcharles-godaddy
Copy link

Similar problem here. In our case, we use the role associated with the EC2 instance is the only one permitted to assume the other roles that we need. But, after assuming the first role, there doesn't seem to be any way to drop the credentials and go back to the the role associated with the EC2 instance.

@github-actions
Copy link

github-actions bot commented Oct 1, 2022

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 2 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 2 days unless further comments are made. labels Oct 1, 2022
@github-actions github-actions bot closed this as completed Oct 3, 2022
@peterwoodworth peterwoodworth reopened this Oct 4, 2022
@peterwoodworth peterwoodworth added p2 effort/small This issue will take less than a day of effort to fix in-progress This issue is being actively worked on and removed closed-for-staleness labels Oct 4, 2022
@allisaurus allisaurus removed their assignment Dec 22, 2022
@scub
Copy link

scub commented Mar 9, 2023

I'm still undecided whether this action should always reset these before running - it may sometimes be desired to assume a role with temp creds

I think providing a flag to clear out these environment variables is probably a reasonable compromise to both allow the intent of the described behavior while preserving the role chaining which currently happens.

@peterwoodworth peterwoodworth added bug Something isn't working p1 and removed in-progress This issue is being actively worked on feature-request A feature should be added or improved. p2 labels Mar 22, 2023
@peterwoodworth peterwoodworth removed the p1 label May 11, 2023
@peterwoodworth
Copy link
Contributor

A combination of role-chaining or the newly introduced unset-current-credentials in v3 may help in some cases where this action runs multiple times

@github-actions
Copy link

** Note **
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working effort/small This issue will take less than a day of effort to fix p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants