Skip to content

Commit 102a2b3

Browse files
authored
Merge pull request #4 from Liftric/feat/allow_manual_addition_of_aws_credentials
feat(CodeArtifact): add option to provide aws credentials directly
2 parents 6a1463e + 07ee94d commit 102a2b3

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ codeArtifactRepository {
3838
profile.set("customer1")
3939
// reuses properties of the default extension if not explicitly specified
4040
}
41+
additional("customer2") {
42+
// if a profile is not available you can also provide the credentials directly
43+
accessKeyId = System.getenv("CUSTOMER2_AWS_ACCESS_KEY_ID")
44+
secretAccessKey = System.getenv("AWS_SECRET_ACCESS_KEY")
45+
// reuses properties of the default extension if not explicitly specified
46+
}
4147
}
4248

4349
dependencyResolutionManagement {

src/main/kotlin/com/liftric/code/artifact/repository/CodeArtifact.kt

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.liftric.code.artifact.repository
22

33
import org.gradle.api.provider.Property
4+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
45
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider
6+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
57
import software.amazon.awssdk.regions.Region
68
import software.amazon.awssdk.services.codeartifact.CodeartifactClient
79
import software.amazon.awssdk.services.codeartifact.model.GetAuthorizationTokenResponse
@@ -15,15 +17,28 @@ abstract class CodeArtifact {
1517
abstract val region: Property<Region>
1618
abstract val profile: Property<String>
1719
abstract val tokenExpiresIn: Property<Long>
20+
abstract val accessKeyId: Property<String>
21+
abstract val secretAccessKey: Property<String>
1822

1923
private val stsClient by lazy {
2024
StsClient.builder().apply {
2125
region.orNull?.let {
2226
region(it)
2327
}
24-
profile.orNull?.let {
28+
if (accessKeyId.orNull != null && secretAccessKey.orNull != null) {
2529
credentialsProvider {
26-
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
30+
StaticCredentialsProvider.create(
31+
AwsBasicCredentials.create(
32+
accessKeyId.get(),
33+
secretAccessKey.get(),
34+
)
35+
).resolveCredentials()
36+
}
37+
} else {
38+
profile.orNull?.let {
39+
credentialsProvider {
40+
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
41+
}
2742
}
2843
}
2944
}.build()
@@ -34,9 +49,20 @@ abstract class CodeArtifact {
3449
region.orNull?.let {
3550
region(it)
3651
}
37-
profile.orNull?.let {
52+
if (accessKeyId.orNull != null && secretAccessKey.orNull != null) {
3853
credentialsProvider {
39-
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
54+
StaticCredentialsProvider.create(
55+
AwsBasicCredentials.create(
56+
accessKeyId.get(),
57+
secretAccessKey.get(),
58+
)
59+
).resolveCredentials()
60+
}
61+
} else {
62+
profile.orNull?.let {
63+
credentialsProvider {
64+
ProfileCredentialsProvider.create(profile.get()).resolveCredentials()
65+
}
4066
}
4167
}
4268
}.build()

src/main/kotlin/com/liftric/code/artifact/repository/CodeArtifactRepositoryExtension.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ abstract class CodeArtifactRepositoryExtension(private val extensionContainer: E
1313
block.invoke(this)
1414
region.convention(this@CodeArtifactRepositoryExtension.region)
1515
profile.convention(this@CodeArtifactRepositoryExtension.profile)
16+
accessKeyId.convention(this@CodeArtifactRepositoryExtension.accessKeyId)
17+
secretAccessKey.convention(this@CodeArtifactRepositoryExtension.secretAccessKey)
1618
tokenExpiresIn.convention(this@CodeArtifactRepositoryExtension.tokenExpiresIn)
1719
}
1820
}

0 commit comments

Comments
 (0)