Skip to content

Commit

Permalink
Support StaticCredentials for AWS Clients (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianxu88 authored May 26, 2022
1 parent e64d0bc commit 7f78b9f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
5 changes: 5 additions & 0 deletions orchard-provider-aws/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
com.salesforce.mce.orchard {
io.aws {
logging.uri = ${?ORCHARD_AWS_LOGGING_URI}
static.credentials {
accessKeyId = ${?ORCHARD_AWS_ACCESS_KEY_ID}
secretKey = ${?ORCHARD_AWS_SECRET_KEY}
}
client.region = ${?ORCHARD_AWS_CLIENT_REGION}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,55 @@

package com.salesforce.mce.orchard.io.aws

import software.amazon.awssdk.auth.credentials.{AwsBasicCredentials, StaticCredentialsProvider}
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.ec2.Ec2Client
import software.amazon.awssdk.services.emr.EmrClient
import software.amazon.awssdk.services.ssm.SsmClient

object Client {

def ec2(): Ec2Client = Ec2Client.create()
def emr(): EmrClient = EmrClient.create()
def ssm(): SsmClient = SsmClient.create()
def staticCredentialsOpt: Option[StaticCredentialsProvider] = for {
awsAccessKeyId <- ProviderSettings().awsAccessKeyId
awsSecretKey <- ProviderSettings().awsSecretKey
} yield StaticCredentialsProvider.create(
AwsBasicCredentials.create(awsAccessKeyId, awsSecretKey)
)

def clientRegionOpt: Option[Region] = for {
clientRegion <- ProviderSettings().awsClientRegion
} yield Region.of(clientRegion)

def ec2(): Ec2Client = {

val clientBuilder = clientRegionOpt
.map(Ec2Client.builder().region)
.getOrElse(Ec2Client.builder())

staticCredentialsOpt
.map(clientBuilder.credentialsProvider(_).build())
.getOrElse(clientBuilder.build())
}

def emr(): EmrClient = {

val clientBuilder = clientRegionOpt
.map(EmrClient.builder().region)
.getOrElse(EmrClient.builder())

staticCredentialsOpt
.map(clientBuilder.credentialsProvider(_).build())
.getOrElse(clientBuilder.build())
}

def ssm(): SsmClient = {

val clientBuilder = clientRegionOpt
.map(SsmClient.builder().region)
.getOrElse(SsmClient.builder())

staticCredentialsOpt
.map(clientBuilder.credentialsProvider(_).build())
.getOrElse(clientBuilder.build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@

package com.salesforce.mce.orchard.io.aws

import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import com.typesafe.config.{Config, ConfigFactory}

case class ProviderSettings(config: Config) {

def loggingUri: Option[String] = {
val path = "logging.uri"
if (config.hasPath(path)) Option(config.getString(path))
else None
private def getConfigString(path: String): Option[String] = {
if (config.hasPath(path)) Option(config.getString(path)) else None
}

lazy val loggingUri = getConfigString("aws.logging.uri")
lazy val awsAccessKeyId = getConfigString("aws.static.credentials.accessKeyId")
lazy val awsSecretKey = getConfigString("aws.static.credentials.secretKey")
lazy val awsClientRegion = getConfigString("aws.client.region")
}

object ProviderSettings {

def apply(): ProviderSettings =
ProviderSettings(ConfigFactory.load().getConfig("com.salesforce.mce.orchard.io.aws"))
ProviderSettings(ConfigFactory.load().getConfig("com.salesforce.mce.orchard.io"))

}
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "0.4.0"
ThisBuild / version := "0.5.0"

0 comments on commit 7f78b9f

Please sign in to comment.