From 19e8ef4dfa2e4486b800be9db822059622b26673 Mon Sep 17 00:00:00 2001 From: Tsuyoshi Yoshizawa Date: Tue, 17 Oct 2017 20:25:01 +0900 Subject: [PATCH] Use awsProfile in s3region and s3credentials (#57) Use awsProfile in s3region and s3credentials --- Readme.md | 15 ++++++++------- src/main/scala/SBTS3Resolver.scala | 25 +++++++++++++++++-------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Readme.md b/Readme.md index 8a0e3d8..4dc9eab 100644 --- a/Readme.md +++ b/Readme.md @@ -46,7 +46,7 @@ addSbtPlugin("ohnosequences" % "sbt-s3-resolver" % "") | Key | Type | Default | |:-----------------|:---------------------------:|:--------------------------------| | `s3credentials` | [`AWSCredentialsProvider`] | see [below](#credentials) | -| `awsProfile` | `String` | `"default"` | +| `awsProfile` | `Option[String]` | `None` | | `s3region` | [`Region`] | `DefaultAwsRegionProviderChain` | | `s3acl` | [`CannedAccessControlList`] | `PublicRead` | | `s3storageClass` | [`StorageClass`] | `Standard` | @@ -115,12 +115,13 @@ i.e. without using this plugin. Or if you're using it anyway, you can write: The **default credentials** chain in this plugin is ```scala -awsProfile := "default" +awsProfile := Some("default") s3credentials := - new ProfileCredentialsProvider(awsProfile.value) | - new EnvironmentVariableCredentialsProvider() | - new InstanceProfileCredentialsProvider() + (awsProfile.value match { + case Some(profile) => new ProfileCredentialsProvider(profile) + case _ => new DefaultAWSCredentialsProviderChain() + }) ``` * [`new ProfileCredentialsProvider(...)`](http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/ProfileCredentialsProvider.html) which loads credentials for an AWS profile config file @@ -132,14 +133,14 @@ You can find other types of credentials providers in the [AWS Java SDK docs][`AW If you have [different profiles](http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html#credentials-file-format) in your `~/.aws/credentials` file, you can choose the one you need by setting ```scala -awsProfile := "bob" +awsProfile := Some("bob") ``` Or if you would like to use profile credentials and have your env vars override if they exist. This is handy if you have both a local dev environment as well as a CI environment where you need to use env vars. ```scala s3credentials := - new ProfileCredentialsProvider(awsProfile.value) | + new ProfileCredentialsProvider(awsProfile.value.orNull) | new EnvironmentVariableCredentialsProvider() ``` diff --git a/src/main/scala/SBTS3Resolver.scala b/src/main/scala/SBTS3Resolver.scala index 6f0116e..d0601e6 100644 --- a/src/main/scala/SBTS3Resolver.scala +++ b/src/main/scala/SBTS3Resolver.scala @@ -3,8 +3,10 @@ package ohnosequences.sbt import sbt._ import Keys._ import java.util.Optional -import com.amazonaws.auth._, profile._ -import com.amazonaws.regions.{ Region, Regions, RegionUtils, AwsRegionProvider } + +import com.amazonaws.auth._ +import com.amazonaws.auth.profile.ProfileCredentialsProvider +import com.amazonaws.regions._ import com.amazonaws.services.s3.AmazonS3 @@ -21,6 +23,9 @@ object SbtS3Resolver extends AutoPlugin { @deprecated("s3acl is now an Option. Please define it either Some(...) or None if you wish to inherit the bucket default.", "0.18.0") implicit def acl2Option(acl: S3ACL): Option[S3ACL] = Some(acl) + @deprecated("awsProfile is now an Option. Please define it either Some(...).", "0.19.0") + implicit def awsProfile2Option(profile: String): Option[String] = Some(profile) + case class S3Resolver( credentialsProvider: AWSCredentialsProvider, overwrite: Boolean, @@ -85,7 +90,7 @@ object SbtS3Resolver extends AutoPlugin { implicit def fromProviderToAWSRegion(provider: AwsRegionProvider): Region = regionFromString(provider.getRegion()) // Adding setting keys - lazy val awsProfile = settingKey[String]("AWS credentials profile") + lazy val awsProfile = settingKey[Option[String]]("AWS credentials profile") lazy val s3credentials = settingKey[AWSCredentialsProvider]("AWS credentials provider to access S3") lazy val s3region = settingKey[Region]("AWS Region for your S3 resolvers") lazy val s3overwrite = settingKey[Boolean]("Controls whether publishing resolver can overwrite artifacts") @@ -129,12 +134,16 @@ object SbtS3Resolver extends AutoPlugin { // Default settings override def projectSettings: Seq[Setting[_]] = Seq( - awsProfile := "default", + awsProfile := None, s3credentials := - new ProfileCredentialsProvider(awsProfile.value) | - new EnvironmentVariableCredentialsProvider() | - InstanceProfileCredentialsProvider.getInstance(), - s3region := new com.amazonaws.regions.DefaultAwsRegionProviderChain(), + (awsProfile.value match { + case Some(profile) => new ProfileCredentialsProvider(profile) + case _ => new DefaultAWSCredentialsProviderChain() + }), + s3region := (awsProfile.value match { + case Some(profile) => new AwsProfileRegionProvider(profile) + case _ => new DefaultAwsRegionProviderChain() + }), s3overwrite := isSnapshot.value, s3sse := false, s3acl := Some(com.amazonaws.services.s3.model.CannedAccessControlList.PublicRead),