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

Pass a role ARN in runtime and use with service connection #511

Open
kai2nenobu opened this issue Mar 17, 2023 · 1 comment
Open

Pass a role ARN in runtime and use with service connection #511

kai2nenobu opened this issue Mar 17, 2023 · 1 comment

Comments

@kai2nenobu
Copy link

Is your feature request related to a problem? Please describe.

Assume that I have an IAM user for Azure DevOps and the IAM user can switch two roles in a dev account and a production account.
I want to use one service connection for the IAM user and pass a role ARN in build runtime.

However AWSShellScript@1 task (and other tasks) don't accept a role ARN as input paramter. Now, I manually AssumeRole in AWSShellScript@1 task and set variables like AWS.AccessKeyID as below.

jobs:
  - job: job_in_dev
    variables:
      AWS.Region: ap-northeast-1
      roleArn: arn:aws:iam::123456789012:role/azure-devops-DevRole
      sessionName: $(Build.BuildId)
    steps:
      - task: AWSShellScript@1
        displayName: AssumeRole
        inputs:
          awsCredentials: aws-sample-connection
          scriptType: inline
          inlineScript: |
            aws sts assume-role --role-arn $(roleArn) --role-session-name $(sessionName) \
              --query 'Credentials.[AccessKeyId, SecretAccessKey, SessionToken]' --out text \
              | while read -r v1 v2 v3; do
                  echo "##vso[task.setvariable variable=AWS.AccessKeyID;issecret=true]$v1"
                  echo "##vso[task.setvariable variable=AWS.SecretAccessKey;issecret=true]$v2"
                  echo "##vso[task.setvariable variable=AWS.SessionToken;issecret=true]$v3"
              done
      - task: AWSShellScript@1
        displayName: Do the job in dev account
        inputs:
          scriptType: inline
          inlineScript: aws ec2 describe-instances

This method works but the first step is too redundant.

Describe the solution you'd like

I suggest to add a task to AssumeRole (ex: AWSAssumeRole@0). If AWSAssumeRole@0 accept a service connection (awsCredentials) and a role ARN, the above yaml is simplified.

jobs:
  - job: job_in_dev
    variables:
      AWS.Region: ap-northeast-1
    steps:
      - task: AWSAssumeRole@0
        inputs:
          awsCredentials: aws-sample-connection
          assumeRoleArn: arn:aws:iam::123456789012:role/azure-devops-DevRole
          roleSessionName: $(Build.BuildId)
          externalId: xxx
      # subsequent steps can use a shot-live credential genereated by AssumeRole
      - task: AWSShellScript@1
        displayName: Do the job in dev account
        inputs:
          scriptType: inline
          inlineScript: aws ec2 describe-instances

Describe alternatives you've considered

An alternative solution is a method to pass a role ARN as build variable. If AWSShellScript@1 (and other tasks) read a role ARN from build variable like AWS.AssumeRoleArn, the above yaml is simplified.

jobs:
  - job: job_in_dev
    variables:
      AWS.Region: ap-northeast-1
      AWS.AssumeRoleArn: arn:aws:iam::123456789012:role/azure-devops-DevRole
      AWS.RoleSessionName: $(Build.BuildId)
      AWS.ExternalId: xxx
    steps:
      - task: AWSShellScript@1
        displayName: Do the job in dev account
        inputs:
          awsCredentials: aws-sample-connection
          scriptType: inline
          inlineScript: aws ec2 describe-instances

This solution is already suggested by @FaridNeshat-TomTom in #340 (comment).

@drpdishant
Copy link

How to use similar mechanism to push to ECR?

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

No branches or pull requests

2 participants