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

Support for OIDC #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ open class AwsS3BuildCache : AbstractBuildCache() {
get() = field ?: System.getenv("S3_BUILD_CACHE_SESSION_TOKEN")
var awsProfile: String? = null
get() = field ?: System.getenv("S3_BUILD_CACHE_PROFILE")

// OIDC Configuration
var awsWebIdentityTokenFile: String? = null
get() = field ?: System.getenv("AWS_WEB_IDENTITY_TOKEN_FILE")
var awsRoleARN: String? = null
get() = field ?: System.getenv("AWS_ROLE_ARN")

var lookupDefaultAwsCredentials: Boolean = false
var credentialsProvider: AwsCredentialsProvider? = null
var showStatistics: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ import com.github.burrunan.s3cache.AwsS3BuildCache
import org.gradle.caching.BuildCacheService
import org.gradle.caching.BuildCacheServiceFactory
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.auth.credentials.*
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.S3ClientBuilder
import software.amazon.awssdk.services.s3.S3Configuration
import java.net.URI
import java.nio.file.FileSystems

private val logger = LoggerFactory.getLogger(AwsS3BuildCacheServiceFactory::class.java)

Expand Down Expand Up @@ -106,13 +103,23 @@ class AwsS3BuildCacheServiceFactory : BuildCacheServiceFactory<AwsS3BuildCache>

private fun S3ClientBuilder.addCredentials(config: AwsS3BuildCache) {
val credentials = when {
config.credentialsProvider != null -> config.credentialsProvider
config.awsAccessKeyId.isNullOrBlank() || config.awsSecretKey.isNullOrBlank() -> when {
config.lookupDefaultAwsCredentials -> return
!config.awsProfile.isNullOrBlank() ->
ProfileCredentialsProvider.create(config.awsProfile)
else -> AnonymousCredentialsProvider.create()
}
config.credentialsProvider != null ->
config.credentialsProvider

config.awsWebIdentityTokenFile != null ->
WebIdentityTokenFileCredentialsProvider.builder().apply {
this.roleArn(config.awsRoleARN)
this.webIdentityTokenFile(FileSystems.getDefault().getPath(config.awsWebIdentityTokenFile!!))
}.build()

config.awsAccessKeyId.isNullOrBlank() || config.awsSecretKey.isNullOrBlank() ->
when {
config.lookupDefaultAwsCredentials -> return
!config.awsProfile.isNullOrBlank() ->
ProfileCredentialsProvider.create(config.awsProfile)
else -> AnonymousCredentialsProvider.create()
}

else ->
StaticCredentialsProvider.create(
if (config.sessionToken.isNullOrEmpty()) {
Expand Down
Loading