-
Notifications
You must be signed in to change notification settings - Fork 18
TS-46892 Extended system proxy support #935
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
Open
Stef2k16
wants to merge
4
commits into
master
Choose a base branch
from
ts/46892_system_proxies
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
teamscale-client/src/test/kotlin/com/teamscale/client/SystemProxyTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package com.teamscale.client | ||
|
|
||
| import com.github.markusbernhardt.proxy.selector.pac.PacProxySelector | ||
| import com.github.markusbernhardt.proxy.selector.pac.UrlPacScriptSource | ||
| import com.teamscale.client.TeamscaleServiceGenerator.buildUserAgent | ||
| import com.teamscale.client.TeamscaleServiceGenerator.createService | ||
| import okhttp3.HttpUrl.Companion.toHttpUrl | ||
| import okhttp3.mockwebserver.Dispatcher | ||
| import okhttp3.mockwebserver.MockResponse | ||
| import okhttp3.mockwebserver.MockWebServer | ||
| import okhttp3.mockwebserver.RecordedRequest | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.AfterEach | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import java.io.IOException | ||
| import java.net.Proxy | ||
| import java.net.ProxySelector | ||
| import java.net.SocketAddress | ||
| import java.net.URI | ||
|
|
||
| /** | ||
| * Tests that the profiler resolves the operating system's proxy configuration, including Proxy-Auto-Config (PAC) files. | ||
| * | ||
| * These tests install a direct (no-proxy) [ProxySelector] as the JVM default so that a request is only ever routed | ||
| * through [proxyServer] if the profiler installs the detected system proxy selector on its OkHttp client. | ||
| */ | ||
| internal class SystemProxyTest { | ||
|
|
||
| /** The proxy that the system configuration / PAC file points at and that requests should be routed through. */ | ||
| private lateinit var proxyServer: MockWebServer | ||
|
|
||
| private val originalDefaultSelector: ProxySelector = ProxySelector.getDefault() | ||
|
|
||
| private val originalSystemProxySelectorSupplier = HttpUtils.systemProxySelectorSupplier | ||
|
|
||
| /** A [ProxySelector] that never returns a proxy, i.e., every connection stays direct. */ | ||
| private val directSelector = object : ProxySelector() { | ||
| override fun select(uri: URI?) = listOf(Proxy.NO_PROXY) | ||
| override fun connectFailed(uri: URI?, sa: SocketAddress?, ioe: IOException?) { | ||
| // Nothing to do | ||
| } | ||
| } | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| proxyServer = MockWebServer() | ||
| proxyServer.start() | ||
| // Make the JVM default a direct connection, so requests only reach proxyServer if the profiler installs the | ||
| // system proxy selector itself. | ||
| ProxySelector.setDefault(directSelector) | ||
| } | ||
|
|
||
| @AfterEach | ||
| fun tearDown() { | ||
| ProxySelector.setDefault(originalDefaultSelector) | ||
| HttpUtils.systemProxySelectorSupplier = originalSystemProxySelectorSupplier | ||
| System.clearProperty("java.net.useSystemProxies") | ||
| System.clearProperty("http.proxyHost") | ||
| System.clearProperty("http.proxyPort") | ||
| proxyServer.shutdown() | ||
| proxyServer.close() | ||
| } | ||
|
|
||
| /** | ||
| * When `-Djava.net.useSystemProxies=true` is set and no explicit `teamscale.*` proxy is configured, the request is | ||
| * routed through the OS-configured proxy (here provided via the standard `http.proxyHost` JVM properties, which | ||
| * proxy-vole reads). The direct default selector guarantees the proxy is only hit because the profiler installed the | ||
| * detected selector. | ||
| */ | ||
| @Test | ||
| fun `system proxy is used when useSystemProxies is enabled`() { | ||
| System.setProperty("java.net.useSystemProxies", "true") | ||
| System.setProperty("http.proxyHost", proxyServer.hostName) | ||
| System.setProperty("http.proxyPort", proxyServer.port.toString()) | ||
|
|
||
| makeRequestThroughProfiler() | ||
|
|
||
| assertThat(proxyServer.requestCount).isEqualTo(1) | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that a PAC file is evaluated and that the resulting proxy is used by our OkHttp client. The PAC-aware | ||
| * selector is what the system detection resolves to, and the profiler installs it on its client; OkHttp then | ||
| * evaluates the PAC script and routes through the returned proxy. | ||
| */ | ||
| @Test | ||
| fun `proxy from a PAC file is evaluated and used`() { | ||
| val pacServer = MockWebServer() | ||
| val pacScript = | ||
| """function FindProxyForURL(url, host) { return "PROXY ${proxyServer.hostName}:${proxyServer.port}"; }""" | ||
| pacServer.dispatcher = object : Dispatcher() { | ||
| override fun dispatch(request: RecordedRequest) = | ||
| MockResponse() | ||
| .setHeader("Content-Type", "application/x-ns-proxy-autoconfig") | ||
| .setBody(pacScript) | ||
| } | ||
| pacServer.start() | ||
|
|
||
| try { | ||
| System.setProperty("java.net.useSystemProxies", "true") | ||
| // Simulate the OS proxy detection resolving to a PAC-based selector. | ||
| HttpUtils.systemProxySelectorSupplier = { | ||
| PacProxySelector(UrlPacScriptSource(pacServer.url("/proxy.pac").toString())) | ||
| } | ||
|
|
||
| makeRequestThroughProfiler() | ||
|
|
||
| assertThat(proxyServer.requestCount).isEqualTo(1) | ||
| } finally { | ||
| pacServer.shutdown() | ||
| pacServer.close() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Without the opt-in flag, the system proxy is never consulted and the connection stays direct, even when the OS | ||
| * would report a proxy. | ||
| */ | ||
| @Test | ||
| fun `no proxy is used without opt-in`() { | ||
| HttpUtils.systemProxySelectorSupplier = { directSelectorReturning(proxyServer) } | ||
|
|
||
| makeRequestThroughProfiler() | ||
|
|
||
| assertThat(proxyServer.requestCount).isZero() | ||
| } | ||
|
|
||
| /** A [ProxySelector] that routes every request through the given server. */ | ||
| private fun directSelectorReturning(server: MockWebServer) = object : ProxySelector() { | ||
| override fun select(uri: URI?) = | ||
| listOf(Proxy(Proxy.Type.HTTP, java.net.InetSocketAddress(server.hostName, server.port))) | ||
|
|
||
| override fun connectFailed(uri: URI?, sa: SocketAddress?, ioe: IOException?) { | ||
| // Nothing to do | ||
| } | ||
| } | ||
|
|
||
| /** Sends one request to a (non-existent) Teamscale server through a client built the same way the profiler builds it. */ | ||
| private fun makeRequestThroughProfiler() { | ||
| val service = createService<ITeamscaleService>( | ||
| "http://teamscale.example.com".toHttpUrl(), | ||
| "someUser", "someAccessToken", | ||
| userAgent = buildUserAgent("Test Tool", "1.0.0") | ||
| ) | ||
| proxyServer.enqueue(MockResponse().setResponseCode(200)) | ||
| runCatching { | ||
| service.sendHeartbeat("", ProfilerInfo(ProcessInformation("", "", 0), null)).execute() | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically unrelated to the bug, but I decided to fix it in here. Both the constants here and the template the constants are used in included the dot so that the dot was included twice in the final properties.