Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 591d604

Browse files
authored
Merge pull request #1 from kdallmeyer-sr/assume_role_option_cleanup
Refactor function and clean up
2 parents c65e770 + 1669352 commit 591d604

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/main/scala/com/audienceproject/spark/dynamodb/connector/DynamoConnector.scala

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,46 @@ private[dynamodb] trait DynamoConnector {
3636
new DynamoDB(client)
3737
}
3838

39+
/**
40+
* Get credentials from a passed in arn or from profile or return the default credential provider
41+
* */
42+
private def getCredentials(chosenRegion: String, assumedArn: Option[String]) = {
43+
assumedArn.map(arn => {
44+
val stsClient = AWSSecurityTokenServiceClientBuilder
45+
.standard()
46+
.withCredentials(new DefaultAWSCredentialsProviderChain)
47+
.withRegion(chosenRegion)
48+
.build()
49+
val assumeRoleResult = stsClient.assumeRole(
50+
new AssumeRoleRequest()
51+
.withRoleSessionName("DynamoDBAssumed")
52+
.withRoleArn(arn)
53+
)
54+
val stsCredentials = assumeRoleResult.getCredentials
55+
val assumeCreds = new BasicSessionCredentials(
56+
stsCredentials.getAccessKeyId,
57+
stsCredentials.getSecretAccessKey,
58+
stsCredentials.getSessionToken
59+
)
60+
new AWSStaticCredentialsProvider(assumeCreds)
61+
}).orElse(Option(System.getProperty("aws.profile")).map(new ProfileCredentialsProvider(_)))
62+
.getOrElse(new DefaultAWSCredentialsProviderChain)
63+
}
64+
3965
def getDynamoDBClient(region: Option[String] = None, assumedArn: Option[String] = None): AmazonDynamoDB = {
4066
val chosenRegion = region.getOrElse(sys.env.getOrElse("aws.dynamodb.region", "us-east-1"))
67+
val credentials = getCredentials(chosenRegion, assumedArn)
68+
4169
Option(System.getProperty("aws.dynamodb.endpoint")).map(endpoint => {
42-
val credentials = Option(System.getProperty("aws.profile"))
43-
.map(new ProfileCredentialsProvider(_))
44-
.getOrElse(new DefaultAWSCredentialsProviderChain)
4570
AmazonDynamoDBClientBuilder.standard()
4671
.withCredentials(credentials)
4772
.withEndpointConfiguration(new EndpointConfiguration(endpoint, chosenRegion))
4873
.build()
4974
}).getOrElse(
50-
assumedArn.map(arn => {
51-
val stsClient = AWSSecurityTokenServiceClientBuilder
52-
.standard()
53-
.withCredentials(new DefaultAWSCredentialsProviderChain)
54-
.withRegion(chosenRegion)
55-
.build()
56-
val assumeRoleResult = stsClient.assumeRole(
57-
new AssumeRoleRequest()
58-
.withRoleSessionName("DynamoDBAssumed")
59-
.withRoleArn(arn)
60-
)
61-
val stsCredentials = assumeRoleResult.getCredentials
62-
val assumeCreds = new BasicSessionCredentials(
63-
stsCredentials.getAccessKeyId,
64-
stsCredentials.getSecretAccessKey,
65-
stsCredentials.getSessionToken
66-
)
67-
AmazonDynamoDBClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(assumeCreds)).build()
68-
}).getOrElse(AmazonDynamoDBClientBuilder.standard().withRegion(chosenRegion).build())
75+
AmazonDynamoDBClientBuilder.standard()
76+
.withCredentials(credentials)
77+
.withRegion(chosenRegion)
78+
.build()
6979
)
7080
}
7181

0 commit comments

Comments
 (0)