@@ -28,6 +28,7 @@ import (
2828 "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
2929 "github.com/aws/aws-sdk-go/aws/session"
3030 "github.com/aws/aws-sdk-go/service/ecr"
31+ "github.com/aws/aws-sdk-go/service/sts"
3132 "github.com/caarlos0/env"
3233 cicxt "github.com/devtron-labs/ci-runner/executor/context"
3334 bean2 "github.com/devtron-labs/ci-runner/helper/bean"
@@ -210,7 +211,7 @@ const CacheModeMax = "max"
210211const CacheModeMin = "min"
211212
212213type DockerCredentials struct {
213- DockerUsername , DockerPassword , AwsRegion , AccessKey , SecretKey , DockerRegistryURL , DockerRegistryType , CredentialsType string
214+ DockerUsername , DockerPassword , AwsRegion , AccessKey , SecretKey , AssumeRoleArn , DockerRegistryURL , DockerRegistryType , CredentialsType string
214215}
215216
216217type EnvironmentVariables struct {
@@ -257,6 +258,34 @@ func (impl *DockerHelperImpl) DockerLogin(ciContext cicxt.CiContext, dockerCrede
257258 log .Println (err )
258259 return err
259260 }
261+
262+ // If an assume role ARN is provided, use STS to assume the cross-account role
263+ if len (dockerCredentials .AssumeRoleArn ) > 0 {
264+ stsClient := sts .New (sess )
265+ assumeOutput , err := stsClient .AssumeRole (& sts.AssumeRoleInput {
266+ RoleArn : aws .String (dockerCredentials .AssumeRoleArn ),
267+ RoleSessionName : aws .String ("devtron-ecr-cross-account" ),
268+ })
269+ if err != nil {
270+ log .Printf ("error in assuming role %s: %v" , dockerCredentials .AssumeRoleArn , err )
271+ return err
272+ }
273+ assumedCreds := credentials .NewStaticCredentials (
274+ * assumeOutput .Credentials .AccessKeyId ,
275+ * assumeOutput .Credentials .SecretAccessKey ,
276+ * assumeOutput .Credentials .SessionToken ,
277+ )
278+ sess , err = session .NewSession (& aws.Config {
279+ Region : & dockerCredentials .AwsRegion ,
280+ Credentials : assumedCreds ,
281+ })
282+ if err != nil {
283+ log .Println (err )
284+ return err
285+ }
286+ log .Printf ("STS AssumeRole successful for cross-account ECR access, roleArn: %s" , dockerCredentials .AssumeRoleArn )
287+ }
288+
260289 svc := ecr .New (sess )
261290 input := & ecr.GetAuthorizationTokenInput {}
262291 authData , err := svc .GetAuthorizationToken (input )
@@ -293,7 +322,11 @@ func (impl *DockerHelperImpl) DockerLogin(ciContext cicxt.CiContext, dockerCrede
293322 log .Println (err )
294323 return err
295324 }
296- log .Println ("Docker login successful with username " , username , " on docker registry URL " , dockerCredentials .DockerRegistryURL )
325+ if len (dockerCredentials .AssumeRoleArn ) > 0 {
326+ log .Printf ("Docker login successful (cross-account via AssumeRole %s) with username %s on registry %s" , dockerCredentials .AssumeRoleArn , username , dockerCredentials .DockerRegistryURL )
327+ } else {
328+ log .Println ("Docker login successful with username " , username , " on docker registry URL " , dockerCredentials .DockerRegistryURL )
329+ }
297330 return nil
298331 }
299332
@@ -1428,6 +1461,7 @@ func (impl *DockerHelperImpl) GetDockerAuthConfigForPrivateRegistries(workflowRe
14281461 AccessKeyEcr : workflowRequest .AccessKey ,
14291462 SecretAccessKeyEcr : workflowRequest .SecretKey ,
14301463 EcrRegion : workflowRequest .AwsRegion ,
1464+ AssumeRoleArnEcr : workflowRequest .AssumeRoleArn ,
14311465 IsRegistryPrivate : true ,
14321466 }
14331467 }
0 commit comments