Skip to content

Commit

Permalink
fix: lazily resolve proxy environment variables (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauzadis authored Apr 15, 2024
1 parent 5dddc09 commit b211771
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .changes/6ffd0a2b-9801-4814-a6d1-9d84196ff130.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "6ffd0a2b-9801-4814-a6d1-9d84196ff130",
"type": "bugfix",
"description": "Lazily resolve proxy environment variables",
"issues": [
"https://github.com/awslabs/aws-sdk-kotlin/issues/1281"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ class HttpEngineConfigImplTest {
}
}
}

// reproduces https://github.com/awslabs/aws-sdk-kotlin/issues/1281
@Test
fun testCanConfigureProxySelectorWithInvalidEnvVarsPresent() {
try {
System.setProperty("http.proxyHost", "invalid!")
System.setProperty("https.proxyHost", "invalid!")

val builder = HttpEngineConfigImpl.BuilderImpl()
builder.buildHttpEngineConfig()

builder.httpClient {
proxySelector = ProxySelector.NoProxy
}
builder.buildHttpEngineConfig()
} finally {
System.clearProperty("http.proxyHost")
System.clearProperty("https.proxyHost")
}
}
}

private fun config(block: HttpEngineConfig.Builder.() -> Unit): HttpEngineConfig =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ import aws.smithy.kotlin.runtime.util.PropertyProvider
* - `no_proxy`, `NO_PROXY`
*/
internal class EnvironmentProxySelector(provider: PlatformEnvironProvider = PlatformProvider.System) : ProxySelector {
private val httpProxy =
resolveProxyByProperty(provider, Scheme.HTTP) ?: resolveProxyByEnvironment(provider, Scheme.HTTP)
private val httpsProxy =
resolveProxyByProperty(provider, Scheme.HTTPS) ?: resolveProxyByEnvironment(provider, Scheme.HTTPS)
private val noProxyHosts = resolveNoProxyHosts(provider)
private val httpProxy by lazy { resolveProxyByProperty(provider, Scheme.HTTP) ?: resolveProxyByEnvironment(provider, Scheme.HTTP) }
private val httpsProxy by lazy { resolveProxyByProperty(provider, Scheme.HTTPS) ?: resolveProxyByEnvironment(provider, Scheme.HTTPS) }
private val noProxyHosts by lazy { resolveNoProxyHosts(provider) }

override fun select(url: Url): ProxyConfig {
if (httpProxy == null && httpsProxy == null || noProxy(url)) return ProxyConfig.Direct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ class EnvironmentProxySelectorTest {
fun testSelectFailures() {
failCases.forEachIndexed { idx, failCase ->
val testProvider = TestPlatformEnvironmentProvider(failCase.env, failCase.props)
val selector = EnvironmentProxySelector(testProvider)
val exception = assertFailsWith<ClientException>("[idx=$idx] expected ClientException") {
EnvironmentProxySelector(testProvider)
selector.select(Url.parse("http://localhost:8000")) // call `select` because proxy selector resolves env vars lazily
}

val expectedError = (failCase.env + failCase.props).map { (k, v) -> """$k="$v"""" }.joinToString(", ")
Expand Down

0 comments on commit b211771

Please sign in to comment.