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

fix: lazily resolve proxy environment variables #1066

Merged
merged 6 commits into from
Apr 15, 2024
Merged
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
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
Loading