Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS profile in remote cache DSL #24

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#### Unreleased
- *TBD*
#### Changed
- Added support for specifying AWS profile in DSL

#### 1.5
- 2022-10-05 - [4 commits](https://github.com/burrunan/gradle-s3-build-cache/compare/v1.4...v1.5)
#### Changed
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The AWS S3 build cache implementation has a few configuration options:
| `awsAccessKeyId` | The AWS access key id | no | `getenv("S3_BUILD_CACHE_ACCESS_KEY_ID")` |
| `awsSecretKey` | The AWS secret key | no | `getenv("S3_BUILD_CACHE_SECRET_KEY")` |
| `sessionToken` | The AWS sessionToken when you use temporal credentials | no | `getenv("S3_BUILD_CACHE_SESSION_TOKEN")` |
| `awsProfile` | The AWS profile to use for authentication | no | `getenv("S3_BUILD_CACHE_PROFILE")` |
| `lookupDefaultAwsCredentials` | Configures if `DefaultAWSCredentialsProviderChain` could be used to lookup credentials | yes | false |
| `showStatistics` | Displays statistics on the remote cache performance | Yes | `true` |
| `showStatisticsWhenImpactExceeds` | Specifies minimum duration to trigger printing the stats, milliseconds | Yes | `100` |
Expand Down Expand Up @@ -135,7 +136,11 @@ environment variables.

If you want to use AWS default credentials [`DefaultAWSCredentialsProviderChain`](http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html),
then configure `lookupDefaultAwsCredentials=true`.
Note: it will still try `S3_BUILD_CACHE_` variables first.

If you want to use a specific [AWS profile](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials-profiles.html),
then configure `awsProfile="<profile_name>"`.

Note: even with these values set, it will still try `S3_BUILD_CACHE_` variables first.

### S3 Bucket Permissions for cache population

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ open class AwsS3BuildCache : AbstractBuildCache() {
var awsAccessKeyId: String? = System.getenv("S3_BUILD_CACHE_ACCESS_KEY_ID")
var awsSecretKey: String? = System.getenv("S3_BUILD_CACHE_SECRET_KEY")
var sessionToken: String? = System.getenv("S3_BUILD_CACHE_SESSION_TOKEN")
var awsProfile: String? = System.getenv("S3_BUILD_CACHE_PROFILE")
var lookupDefaultAwsCredentials: Boolean = false
var showStatistics: Boolean = true
var showStatisticsWhenImpactExceeds: Long = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3Client
Expand Down Expand Up @@ -99,6 +100,8 @@ class AwsS3BuildCacheServiceFactory : BuildCacheServiceFactory<AwsS3BuildCache>
val credentials = when {
config.awsAccessKeyId.isNullOrBlank() || config.awsSecretKey.isNullOrBlank() -> when {
config.lookupDefaultAwsCredentials -> return
!config.awsProfile.isNullOrBlank() ->
ProfileCredentialsProvider.create(config.awsProfile)
else -> AnonymousCredentialsProvider.create()
}
else ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,15 @@ class AwsS3BuildCacheServiceFactoryTest {
val service = subject.createBuildCacheService(conf, buildCacheDescriber)
Assertions.assertNotNull(service)
}

@Test
fun testAWSProfileCredentials() {
val conf = buildCache {
bucket = "my-bucket"
region = "us-west-1"
awsProfile = "any aws profile"
}
val service = subject.createBuildCacheService(conf, buildCacheDescriber)
Assertions.assertNotNull(service)
}
}