Skip to content

Commit

Permalink
Merge pull request #4 from Liftric/feat/allow_manual_addition_of_aws_…
Browse files Browse the repository at this point in the history
…credentials

feat(CodeArtifact): add option to provide aws credentials directly
  • Loading branch information
Ingwersaft committed Apr 9, 2024
2 parents 6a1463e + 07ee94d commit 102a2b3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ codeArtifactRepository {
profile.set("customer1")
// reuses properties of the default extension if not explicitly specified
}
additional("customer2") {
// if a profile is not available you can also provide the credentials directly
accessKeyId = System.getenv("CUSTOMER2_AWS_ACCESS_KEY_ID")
secretAccessKey = System.getenv("AWS_SECRET_ACCESS_KEY")
// reuses properties of the default extension if not explicitly specified
}
}

dependencyResolutionManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.liftric.code.artifact.repository

import org.gradle.api.provider.Property
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
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.codeartifact.CodeartifactClient
import software.amazon.awssdk.services.codeartifact.model.GetAuthorizationTokenResponse
Expand All @@ -15,15 +17,28 @@ abstract class CodeArtifact {
abstract val region: Property<Region>
abstract val profile: Property<String>
abstract val tokenExpiresIn: Property<Long>
abstract val accessKeyId: Property<String>
abstract val secretAccessKey: Property<String>

private val stsClient by lazy {
StsClient.builder().apply {
region.orNull?.let {
region(it)
}
profile.orNull?.let {
if (accessKeyId.orNull != null && secretAccessKey.orNull != null) {
credentialsProvider {
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
StaticCredentialsProvider.create(
AwsBasicCredentials.create(
accessKeyId.get(),
secretAccessKey.get(),
)
).resolveCredentials()
}
} else {
profile.orNull?.let {
credentialsProvider {
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
}
}
}
}.build()
Expand All @@ -34,9 +49,20 @@ abstract class CodeArtifact {
region.orNull?.let {
region(it)
}
profile.orNull?.let {
if (accessKeyId.orNull != null && secretAccessKey.orNull != null) {
credentialsProvider {
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
StaticCredentialsProvider.create(
AwsBasicCredentials.create(
accessKeyId.get(),
secretAccessKey.get(),
)
).resolveCredentials()
}
} else {
profile.orNull?.let {
credentialsProvider {
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
}
}
}
}.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ abstract class CodeArtifactRepositoryExtension(private val extensionContainer: E
block.invoke(this)
region.convention(this@CodeArtifactRepositoryExtension.region)
profile.convention(this@CodeArtifactRepositoryExtension.profile)
accessKeyId.convention(this@CodeArtifactRepositoryExtension.accessKeyId)
secretAccessKey.convention(this@CodeArtifactRepositoryExtension.secretAccessKey)
tokenExpiresIn.convention(this@CodeArtifactRepositoryExtension.tokenExpiresIn)
}
}
Expand Down

0 comments on commit 102a2b3

Please sign in to comment.