Skip to content
Open
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
2 changes: 2 additions & 0 deletions gremlin-dotnet/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ services:
- SOCKET_SERVER_VERSION=${GREMLIN_SERVER}
ports:
- "45943:45943"
- "45944:45944"

gremlin-dotnet-integration-tests:
container_name: gremlin-dotnet-integration-tests
Expand All @@ -67,6 +68,7 @@ services:
- GREMLIN_SERVER_PORT=45940
- GREMLIN_SECURE_SERVER_PORT=45941
- GREMLIN_SOCKET_SERVER_URL=http://gremlin-socket-server-test-dotnet:45943/gremlin
- GREMLIN_SOCKET_SERVER_PROXY_URL=http://gremlin-socket-server-test-dotnet:45944
- VERTEX_LABEL=dotnet-example
working_dir: /gremlin-dotnet
command: >
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#region License

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#endregion

using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Gremlin.Net.Driver;
using Xunit;

namespace Gremlin.Net.IntegrationTest.Driver
{
public class ProxyBehaviorIntegrationTests
{
private static readonly string Host = GetHost();
private static readonly string ProxyUrl = GetProxyUrl();

private static string GetHost()
{
var url = Environment.GetEnvironmentVariable("GREMLIN_SOCKET_SERVER_URL");
if (string.IsNullOrEmpty(url)) return "localhost";
try
{
return new Uri(url).Host;
}
catch
{
return "localhost";
}
}

private static string GetProxyUrl()
{
var url = Environment.GetEnvironmentVariable("GREMLIN_SOCKET_SERVER_PROXY_URL");
return string.IsNullOrEmpty(url) ? "http://localhost:45944" : url;
}

private static async Task ResetProxyAsync(HttpClient httpClient)
{
using var response = await httpClient.PostAsync($"{ProxyUrl}/__reset", null);
response.EnsureSuccessStatusCode();
}

private static async Task<string[]> GetRecordedAsync(HttpClient httpClient)
{
var json = await httpClient.GetStringAsync($"{ProxyUrl}/__recorded");
return JsonSerializer.Deserialize<string[]>(json) ?? Array.Empty<string>();
}

[Fact]
public async Task ShouldRouteSocketServerTrafficThroughProxy()
{
using var httpClient = new HttpClient();
await ResetProxyAsync(httpClient);

var server = new GremlinServer(Host, SocketServerConstants.Port);
using (var client = new GremlinClient(server,
connectionSettings: new ConnectionSettings { Proxy = new WebProxy(ProxyUrl) }))
{
var resultSet = await client.SubmitAsync<dynamic>(SocketServerConstants.GremlinSingleVertex);
var results = await resultSet.ToListAsync();
Assert.Single(results);
}

var recorded = await GetRecordedAsync(httpClient);
Assert.Contains(recorded, t => t.EndsWith(":45943"));
}

[Fact]
public async Task ShouldNotRecordWhenNoProxy()
{
using var httpClient = new HttpClient();
await ResetProxyAsync(httpClient);

var server = new GremlinServer(Host, SocketServerConstants.Port);
using (var client = new GremlinClient(server))
{
var resultSet = await client.SubmitAsync<dynamic>(SocketServerConstants.GremlinSingleVertex);
var results = await resultSet.ToListAsync();
Assert.Single(results);
}

var recorded = await GetRecordedAsync(httpClient);
Assert.DoesNotContain(recorded, t => t.EndsWith(":45943"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.proxy.HttpProxyHandler;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.tinkerpop.gremlin.driver.exception.ConnectionException;
import org.apache.tinkerpop.gremlin.driver.handler.GremlinResponseHandler;
Expand Down Expand Up @@ -147,9 +150,10 @@ protected void initChannel(final SocketChannel socketChannel) {
// TLS handshake.
final ProxyOptions proxy = cluster.getProxy();
if (proxy != null) {
final SocketAddress proxyAddress = resolveProxyAddress(proxy.getAddress());
final HttpProxyHandler proxyHandler = proxy.hasCredentials()
? new HttpProxyHandler(proxy.getAddress(), proxy.getUsername(), proxy.getPassword())
: new HttpProxyHandler(proxy.getAddress());
? new HttpProxyHandler(proxyAddress, proxy.getUsername(), proxy.getPassword())
: new HttpProxyHandler(proxyAddress);
pipeline.addLast(PIPELINE_PROXY_HANDLER, proxyHandler);
}

Expand Down Expand Up @@ -189,6 +193,21 @@ public void connected() {
}
}
}

/**
* Resolves an unresolved proxy {@link SocketAddress} so that Netty's {@code HttpProxyHandler} can connect to
* it. A resolved address (or a non-{@link InetSocketAddress}) is returned unchanged.
*/
private static SocketAddress resolveProxyAddress(final SocketAddress address) {
if (address instanceof InetSocketAddress) {
final InetSocketAddress inet = (InetSocketAddress) address;
if (inet.isUnresolved()) {
return new InetSocketAddress(inet.getHostString(), inet.getPort());
}
}
return address;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tinkerpop.gremlin.driver;

import org.apache.tinkerpop.gremlin.socket.server.RecordingProxyServer;
import org.apache.tinkerpop.gremlin.socket.server.SimpleTestServer;
import org.apache.tinkerpop.gremlin.socket.server.SocketServerConstants;
import org.apache.tinkerpop.gremlin.socket.server.TestHttpServerInitializer;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV4;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Proves that {@link Cluster.Builder#proxy(ProxyOptions)} routes driver traffic through an HTTP proxy.
* <p>
* A {@link SimpleTestServer} (the real Gremlin socket server) runs on {@link SocketServerConstants#PORT}
* and an in-process {@link RecordingProxyServer} runs on {@link SocketServerConstants#PROXY_PORT}. The
* proxy records the {@code host:port} target of every tunnel it establishes and exposes a small control
* API ({@code GET /__recorded}, {@code POST /__reset}) that the tests query with the JDK11
* {@link java.net.http.HttpClient}.
*/
public class ProxyBehaviorIntegrateTest {

private static final int PORT = SocketServerConstants.PORT;
private static final int PROXY_PORT = SocketServerConstants.PROXY_PORT;

private static SimpleTestServer server;
private static RecordingProxyServer proxyServer;
private static HttpClient httpClient;

@BeforeClass
public static void setUp() throws InterruptedException {
server = new SimpleTestServer(PORT);
server.start(new TestHttpServerInitializer());

proxyServer = new RecordingProxyServer(PROXY_PORT);
proxyServer.start();

httpClient = HttpClient.newHttpClient();
}

@AfterClass
public static void tearDown() {
if (server != null) server.stop();
if (proxyServer != null) proxyServer.stop();
}

private static Cluster.Builder buildCluster() {
return Cluster.build("localhost")
.validationRequest(SocketServerConstants.GREMLIN_SINGLE_VERTEX)
.port(PORT)
.serializer(new GraphBinaryMessageSerializerV4());
}

private static String getRecorded() throws Exception {
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PROXY_PORT + "/__recorded"))
.GET()
.build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
}

private static void resetRecorded() throws Exception {
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PROXY_PORT + "/__reset"))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

@Test
public void shouldRouteTrafficThroughProxy() throws Exception {
resetRecorded();

final Cluster cluster = buildCluster()
.proxy(ProxyOptions.create("localhost", PROXY_PORT))
.create();
try {
final Client client = cluster.connect();
final List<Result> results = client.submit(SocketServerConstants.GREMLIN_SINGLE_VERTEX).all().get();
assertEquals(1, results.size());
assertThat(results.get(0).getObject(), instanceOf(Vertex.class));

// the driver issues an HTTP CONNECT, so the proxy should have recorded the tunnel to the
// socket server. Match on the ":45943" substring to stay tolerant of the host form.
final String recorded = getRecorded();
assertTrue("Expected the proxy to have recorded the tunnel to :" + PORT + " but was: " + recorded,
recorded.contains(":" + PORT));
} finally {
cluster.close();
}
}

@Test
public void shouldNotRecordWithoutProxy() throws Exception {
resetRecorded();

final Cluster cluster = buildCluster().create();
try {
final Client client = cluster.connect();
final List<Result> results = client.submit(SocketServerConstants.GREMLIN_SINGLE_VERTEX).all().get();
assertEquals(1, results.size());
assertThat(results.get(0).getObject(), instanceOf(Vertex.class));

// without a proxy configured the driver connects directly, so nothing should be recorded.
final String recorded = getRecorded();
assertFalse("Expected no tunnel to :" + PORT + " to be recorded but was: " + recorded,
recorded.contains(":" + PORT));
} finally {
cluster.close();
}
}
}
2 changes: 2 additions & 0 deletions gremlin-go/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ services:
- SOCKET_SERVER_VERSION=${GREMLIN_SERVER}
ports:
- "45943:45943"
- "45944:45944"

gremlin-go-integration-tests:
container_name: gremlin-go-integration-tests
Expand All @@ -65,6 +66,7 @@ services:
- GREMLIN_SERVER_URL=http://gremlin-server-test:45940/gremlin
- GREMLIN_SERVER_BASIC_AUTH_URL=https://gremlin-server-test:45941/gremlin
- GREMLIN_SOCKET_SERVER_URL=http://gremlin-socket-server-test-go:45943/gremlin
- GREMLIN_SOCKET_SERVER_PROXY_URL=http://gremlin-socket-server-test-go:45944
- RUN_INTEGRATION_TESTS=true
- RUN_INTEGRATION_WITH_ALIAS_TESTS=true
- RUN_BASIC_AUTH_INTEGRATION_TESTS=true
Expand Down
Loading
Loading