-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'databricks/main' into sslsocket
- Loading branch information
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
46 changes: 46 additions & 0 deletions
46
databricks-sdk-java/src/main/java/com/databricks/sdk/core/utils/CustomRoutePlanner.java
This file contains 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,46 @@ | ||
package com.databricks.sdk.core.utils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import org.apache.http.HttpException; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.conn.routing.HttpRoute; | ||
import org.apache.http.conn.routing.HttpRoutePlanner; | ||
import org.apache.http.impl.conn.DefaultProxyRoutePlanner; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
/** | ||
* Custom route planner that routes requests via a proxy, except for hosts that match a list of | ||
* non-proxy hosts. | ||
*/ | ||
public class CustomRoutePlanner implements HttpRoutePlanner { | ||
private final DefaultProxyRoutePlanner defaultRoutePlanner; | ||
private final List<Pattern> nonProxyHostRegex; | ||
|
||
public CustomRoutePlanner(HttpHost proxy, String nonProxyHosts) { | ||
this.defaultRoutePlanner = new DefaultProxyRoutePlanner(proxy); | ||
if (nonProxyHosts == null || nonProxyHosts.isEmpty()) { | ||
this.nonProxyHostRegex = new ArrayList<>(); | ||
} else { | ||
this.nonProxyHostRegex = | ||
Arrays.stream(nonProxyHosts.split("\\|")) | ||
.map(host -> host.replace(".", "\\.").replace("*", ".*")) | ||
.map(Pattern::compile) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
@Override | ||
public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) | ||
throws HttpException { | ||
String targetHostName = target.getHostName(); | ||
if (nonProxyHostRegex.stream().anyMatch(pattern -> pattern.matcher(targetHostName).matches())) { | ||
return new HttpRoute(target); // Direct route, no proxy | ||
} | ||
return defaultRoutePlanner.determineRoute(target, request, context); // Route via proxy | ||
} | ||
} |
This file contains 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
90 changes: 90 additions & 0 deletions
90
databricks-sdk-java/src/test/java/com/databricks/sdk/core/utils/CustomRoutePlannerTest.java
This file contains 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,90 @@ | ||
package com.databricks.sdk.core.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.util.Arrays; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.conn.routing.HttpRoute; | ||
import org.apache.http.conn.routing.HttpRoutePlanner; | ||
import org.apache.http.message.BasicHttpRequest; | ||
import org.apache.http.protocol.BasicHttpContext; | ||
import org.apache.http.protocol.HttpContext; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CustomRoutePlannerTest { | ||
|
||
private static HttpHost proxy; | ||
private static HttpRoutePlanner customRoutePlanner; | ||
private static HttpContext context; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
proxy = new HttpHost("proxy.example.com", 8080); | ||
String nonProxyHosts = | ||
String.join("|", Arrays.asList("example.com", "localhost", "*.mydomain.com")); | ||
customRoutePlanner = new CustomRoutePlanner(proxy, nonProxyHosts); | ||
context = new BasicHttpContext(); | ||
} | ||
|
||
@Test | ||
public void testDirectRouteForExactNonProxyHost() throws Exception { | ||
// Test a host that should bypass the proxy | ||
HttpHost target = new HttpHost("example.com", 80); | ||
HttpRoute route = | ||
customRoutePlanner.determineRoute(target, new BasicHttpRequest("GET", "/"), context); | ||
|
||
// Assert that the route is direct (no proxy) | ||
assertEquals(target, route.getTargetHost()); | ||
assertNull(route.getProxyHost()); | ||
} | ||
|
||
@Test | ||
public void testDirectRouteForWildcardNonProxyHost() throws Exception { | ||
// Test a host that matches the wildcard pattern (*.mydomain.com) | ||
HttpHost target = new HttpHost("api.mydomain.com", 80); | ||
HttpRoute route = | ||
customRoutePlanner.determineRoute(target, new BasicHttpRequest("GET", "/"), context); | ||
|
||
// Assert that the route is direct (no proxy) | ||
assertEquals(target, route.getTargetHost()); | ||
assertNull(route.getProxyHost()); | ||
} | ||
|
||
@Test | ||
public void testDirectRouteForLocalhost() throws Exception { | ||
// Test a localhost, which should bypass the proxy | ||
HttpHost target = new HttpHost("localhost", 80); | ||
HttpRoute route = | ||
customRoutePlanner.determineRoute(target, new BasicHttpRequest("GET", "/"), context); | ||
|
||
// Assert that the route is direct (no proxy) | ||
assertEquals(target, route.getTargetHost()); | ||
assertNull(route.getProxyHost()); | ||
} | ||
|
||
@Test | ||
public void testProxyRouteForNonMatchingHost() throws Exception { | ||
// Test a host that does not match the non-proxy patterns | ||
HttpHost target = new HttpHost("otherdomain.com", 80); | ||
HttpRoute route = | ||
customRoutePlanner.determineRoute(target, new BasicHttpRequest("GET", "/"), context); | ||
|
||
// Assert that the route goes through the proxy | ||
assertEquals(target, route.getTargetHost()); | ||
assertEquals(proxy, route.getProxyHost()); | ||
} | ||
|
||
@Test | ||
public void testProxyRouteForPartialWildcardMatch() throws Exception { | ||
// Test a host that does not fully match the wildcard pattern (*.mydomain.com) | ||
HttpHost target = new HttpHost("mydomain.org", 80); | ||
HttpRoute route = | ||
customRoutePlanner.determineRoute(target, new BasicHttpRequest("GET", "/"), context); | ||
|
||
// Assert that the route goes through the proxy | ||
assertEquals(target, route.getTargetHost()); | ||
assertEquals(proxy, route.getProxyHost()); | ||
} | ||
} |