Skip to content

Commit c7ac37b

Browse files
feat: add proxy support
1 parent 42ea05c commit c7ac37b

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,19 @@ limit role permissions to those required for CodeArtifact.
8181

8282
## Other configuration properties
8383

84-
| System Property | Environment Variable | Description |
85-
|--------------------------------------------------------------|--------------------------------------------------------------|---------------------------------------------------------------------------------------------|
86-
| codeartifact.domains | CODEARTIFACT_DOMAINS | Regex of domains to provide authentication for (defaults to all domains) |
84+
| System Property | Environment Variable | Description |
85+
|-------------------------------------------------------------|--------------------------------------------------------------|---------------------------------------------------------------------------------------------|
86+
| codeartifact.domains | CODEARTIFACT_DOMAINS | Regex of domains to provide authentication for (defaults to all domains) |
87+
codeartifact.proxy.enabled | CODEARTIFACT_PROXY_ENABLED | Enable proxying of CodeArtifact URLs (default: true), if proxy base URLs configured (above) |
88+
| codeartifact.<domain>-<domain owner>-<region>.proxy.base-url | CODEARTIFACT_<DOMAIN>_<DOMAIN_OWNER>_<REGION>_PROXY_BASE_URL | Proxy base URL to use |
89+
| codeartifact.<region>.proxy.base-url | CODEARTIFACT_<REGION>_PROXY_BASE_URL | Proxy base URL to use |
90+
| codeartifact.proxy.base-url | CODEARTIFACT_PROXY_BASE_URL | Proxy base URL to use |
91+
92+
## Reverse Proxy Configuration
93+
94+
If you wish to place CodeArtifact behind a reverse proxy (for example, CloudFront or other CDN) you can specify the proxy to use (see table above). The configured CodeArtifact URLs are used to generate a CodeArtifact token, and are then reconfigured to use the proxy URL (continuing to pass along the authentication token). Your configured proxy is responsible for handling authentication/caching as appropriate.
95+
96+
Repositories configured for publishing are not configured to use the proxy.
8797

8898
## Obtaining CodeArtifact tokens for other (non-Maven repository) uses
8999

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description=CodeArtifact settings plugin for Gradle
33
kotlin.code.style=official
44

55
group=io.cloudshiftdev.codeartifact
6-
version=1.0.7
6+
version=1.1.0
77

88
org.gradle.jvmargs=-Dfile.encoding\=UTF-8
99
org.gradle.vfs.watch=true

src/main/kotlin/io/cloudshiftdev/gradle/codeartifact/CodeArtifactPlugin.kt

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.cloudshiftdev.gradle.codeartifact
22

33
import io.cloudshiftdev.gradle.codeartifact.CodeArtifactEndpoint.Companion.toCodeArtifactEndpoint
44
import io.cloudshiftdev.gradle.codeartifact.CodeArtifactEndpoint.Companion.toCodeArtifactEndpointOrNull
5+
import java.net.URI
56
import javax.inject.Inject
67
import kotlin.contracts.ExperimentalContracts
78
import kotlin.contracts.contract
@@ -72,14 +73,17 @@ public abstract class CodeArtifactPlugin @Inject constructor(private val objects
7273

7374
project.plugins.withType<MavenPublishPlugin> {
7475
project.configure<PublishingExtension> {
75-
repositories.all { configureCodeArtifactRepository(this, project.providers) }
76+
repositories.all {
77+
configureCodeArtifactRepository(this, project.providers, useProxy = false)
78+
}
7679
}
7780
}
7881
}
7982

8083
private fun configureCodeArtifactRepository(
8184
repository: ArtifactRepository,
8285
providers: ProviderFactory,
86+
useProxy: Boolean = true,
8387
) {
8488
if (!shouldConfigureCodeArtifactRepository(repository)) return
8589

@@ -88,6 +92,25 @@ public abstract class CodeArtifactPlugin @Inject constructor(private val objects
8892

8993
val tokenProvider = providers.codeArtifactToken(endpoint)
9094
repository.setConfiguredCredentials(createRepoCredentials(tokenProvider))
95+
96+
val proxyEnabled = resolveSystemVar("codeartifact.proxy.enabled")?.toBoolean() ?: true
97+
98+
if (useProxy && proxyEnabled) {
99+
val key1 =
100+
"codeartifact.${endpoint.domain}-${endpoint.domainOwner}-${endpoint.region}.proxy.base-url"
101+
val key2 = "codeartifact.${endpoint.region}.proxy.base-url"
102+
val key3 = "codeartifact.proxy.base-url"
103+
resolveSystemVar(key1, key2, key3)?.let { proxyBaseUrl ->
104+
val proxyUrl = URI("${proxyBaseUrl}/${endpoint.url.path}")
105+
logger.lifecycle(
106+
"Using proxy for CodeArtifact repository: $proxyUrl -> ${endpoint.url}"
107+
)
108+
repository.url = proxyUrl
109+
}
110+
?: run {
111+
logger.info("No proxy configured for CodeArtifact repository: ${endpoint.url}")
112+
}
113+
}
91114
}
92115

93116
@OptIn(ExperimentalContracts::class)

src/main/kotlin/io/cloudshiftdev/gradle/codeartifact/Extensions.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ internal fun URI.queryParameters() =
1414
key to value
1515
} ?: emptyMap()
1616

17-
internal fun resolveSystemVar(key: String): String? =
17+
internal fun resolveSystemVar(vararg keys: String): String? {
18+
return keys.firstNotNullOfOrNull { resolveSystemVarInternal(it) }
19+
}
20+
21+
internal fun resolveSystemVarInternal(key: String): String? =
1822
System.getProperty(key)?.takeIf(String::isNotBlank)
1923
?: System.getenv(key.toScreamingSnakeCase())?.takeIf(String::isNotBlank)
2024

0 commit comments

Comments
 (0)