Skip to content

Commit

Permalink
Add support for AWS profile when resolving region
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Dec 6, 2022
1 parent 54673f1 commit d894770
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
16 changes: 11 additions & 5 deletions modules/nf-commons/src/main/nextflow/Global.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class Global {
if( env==null ) env = SysEnv.get()
if( config==null ) config = this.config

def home = Paths.get(System.properties.get('user.home') as String)
def file = home.resolve('.aws/config')

return getAwsRegion0(env, config, file)
}

static protected String getAwsRegion0(Map env, Map config, Path awsFile) {
// check nxf config file
if( config && config.aws instanceof Map ) {
def region = ((Map)config.aws).region
Expand All @@ -179,14 +186,13 @@ class Global {
return env.AWS_DEFAULT_REGION.toString()
}

def home = Paths.get(System.properties.get('user.home') as String)
def file = home.resolve('.aws/config')
if( !file.exists() ) {
if( !awsFile.exists() ) {
return null
}

def ini = new IniFile(file)
return ini.section('default').region
def profile = getAwsProfile0(env, config)
def ini = new IniFile(awsFile)
return ini.section(profile).region
}

static List<String> getAwsCredentials(Map env) {
Expand Down
48 changes: 48 additions & 0 deletions modules/nf-commons/src/test/nextflow/GlobalTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class GlobalTest extends Specification {
aws_session_token = zzz
'''

expect:
Global.getAwsCredentials0([AWS_PROFILE: 'foo'], null, [file]) == ['xxx','yyy']
Global.getAwsCredentials0([AWS_DEFAULT_PROFILE: 'bar'], null, [file]) == ['xxx','yyy','zzz']

Expand All @@ -93,6 +94,53 @@ class GlobalTest extends Specification {

}

def testGetAwsRegion() {
expect:
Global.getAwsRegion([:], [:]) == null
and:
Global.getAwsRegion([:], [aws:[region:'eu-west-2']]) == 'eu-west-2'
and:
// config has priority
Global.getAwsRegion([AWS_DEFAULT_REGION: 'us-central-1'], [aws:[region:'eu-west-2']]) == 'eu-west-2'

and:
Global.getAwsRegion([AWS_DEFAULT_REGION: 'us-central-1'], [:]) == 'us-central-1'
}

def testGetAwsRegionFromAwsFile() {
given:
def file = Files.createTempFile('test','test')
file.text = '''
[default]
aws_access_key_id = aaa
aws_secret_access_key = bbbb
region = reg-something
[foo]
aws_access_key_id = xxx
aws_secret_access_key = yyy
region = reg-foo
[bar]
aws_access_key_id = xxx
aws_secret_access_key = yyy
aws_session_token = zzz
'''

expect:
Global.getAwsRegion0([AWS_DEFAULT_REGION: 'us-central-1'], [:], file) == 'us-central-1'

and:
Global.getAwsRegion0([:], [:], file) == 'reg-something'

and:
Global.getAwsRegion0([:], [aws:[profile: 'foo']], file) == 'reg-foo'

cleanup:
file?.delete()
}


def testAwsCredentialsWithFileAndProfileInTheConfig() {

given:
Expand Down

0 comments on commit d894770

Please sign in to comment.