Skip to content

Commit

Permalink
rework remote console
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentyn Kahamlyk authored and Valentyn Kahamlyk committed May 22, 2024
1 parent 94fdda3 commit f3679f1
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 68 deletions.
8 changes: 5 additions & 3 deletions gremlin-console/conf/remote-graph.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
# under the License.

##############################################################
# This configuration is meant for use with withRemote().
# This configuration is meant for use with with() or connect().
#
# g = traversal('conf/remote-graph.properties')
# g = traversal().with('conf/remote-graph.properties')
# g = connect('conf/remote-graph.properties')
#
# todo: revisit compatible configs
# This file will work with:
# - gremlin-server.yaml
# - gremlin-server-classic.yaml
Expand All @@ -29,5 +31,5 @@
##############################################################

gremlin.remote.remoteConnectionClass=org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
gremlin.remote.driver.clusterFile=conf/remote-objects.yaml
gremlin.remote.driver.clusterFile=conf/remote.yaml
gremlin.remote.driver.sourceName=g
11 changes: 6 additions & 5 deletions gremlin-console/conf/remote-secure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@

##############################################################
# This configuration is meant to have Gremlin Server return
# text serialized objects. The server will toString()
# results giving a view into how scripts are executing.
# serialized objects.
#
# This file will work with:
# - gremlin-server-secure.yaml
##############################################################

hosts: [localhost]
port: 8182
username: stephen
password: password
auth: {
type: basic,
username: stephen,
password: password }
connectionPool: {
enableSsl: true,
sslEnabledProtocols: [TLSv1.2] }
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1, config: { serializeResultToString: true }}
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV4}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# "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
# 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
Expand All @@ -15,10 +15,17 @@
# specific language governing permissions and limitations
# under the License.

gremlin.graph=org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph
##############################################################
# This configuration is meant to have Gremlin Server with
# sigv4 authentication.
##############################################################

gremlin.neo4j.directory=/tmp/neo4j
gremlin.neo4j.conf.dbms.auto_index.nodes.enabled=true
#gremlin.neo4j.conf.dbms.auto_index.nodes.keys=
gremlin.neo4j.conf.dbms.auto_index.relationships.enabled=true
#gremlin.neo4j.conf.dbms.auto_index.relationships.keys=
hosts: [localhost]
port: 8182
auth: {
type: sigv4,
region: us-west1 }
connectionPool: {
enableSsl: true,
sslEnabledProtocols: [TLSv1.2] }
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV4}
7 changes: 2 additions & 5 deletions gremlin-console/conf/remote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@

##############################################################
# This configuration is meant to have Gremlin Server return
# text serialized objects. The server will toString()
# results giving a view into how scripts are executing.
# serialized objects.
#
# This file will work with:
# - gremlin-server.yaml
# - gremlin-server-classic.yaml
# - gremlin-server-modern.yaml
# - gremlin-server-modern-py.yaml
# - gremlin-server-modern-readonly.yaml
# - gremlin-server-spark.yaml
##############################################################

hosts: [localhost]
port: 8182
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1, config: { serializeResultToString: true }}
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV4}
6 changes: 6 additions & 0 deletions gremlin-console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ limitations under the License.
<artifactId>tinkergraph-gremlin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,13 @@
*/
package org.apache.tinkerpop.gremlin.console.jsr223;

import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.auth.Auth;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;

public class ConnectionHelper {
private static final String DEFAULT_G = "g";
private static final int DEFAULT_PORT = 8182;

private ConnectionHelper() {
}

public static Object connect(final String host) {
return connect(host, DEFAULT_G);
}

public static Object connect(final String host, final String g) {
return connect(host, DEFAULT_PORT, g);
}

public static Object connect(final String host, final Auth auth) {
return connect(host, DEFAULT_PORT, DEFAULT_G, auth);
}

public static Object connect(final String host, final int port, final String g) {
return traversal().with(DriverRemoteConnection.using(Cluster.build(host).port(port).create(), g));
}

public static Object connect(final String host, final int port, final String g, final Auth auth) {
return traversal().with(DriverRemoteConnection.using(Cluster.build(host).port(port).auth(auth).create(), g));
public static Object connect(final String fileName) throws Exception {
return traversal().with(fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ scriptEngines: {
org.apache.tinkerpop.gremlin.tinkergraph.jsr223.TinkerGraphGremlinPlugin: {},
org.apache.tinkerpop.gremlin.jsr223.ImportGremlinPlugin: {classImports: [java.lang.Math], methodImports: [java.lang.Math#*]}}}}
serializers:
- { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1, config: { serializeResultToString: true }} # application/vnd.gremlin-v3.0+gryo-stringd
- { className: org.apache.tinkerpop.gremlin.util.ser.GraphSONMessageSerializerV1, config: { ioRegistries: [org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerIoRegistryV1] }} # application/vnd.gremlin-v1.0+json
- { className: org.apache.tinkerpop.gremlin.util.ser.GraphSONMessageSerializerV3, config: { ioRegistries: [org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerIoRegistryV3] }} # application/json,application/vnd.gremlin-v3.0+json
- { className: org.apache.tinkerpop.gremlin.util.ser.GraphSONMessageSerializerV2, config: { ioRegistries: [org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerIoRegistryV2] }} # application/vnd.gremlin-v2.0+json
- { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV4} # application/vnd.graphbinary-v4.0
metrics: {
consoleReporter: {enabled: true, interval: 180000},
csvReporter: {enabled: true, interval: 180000, fileName: /tmp/gremlin-server-metrics.csv},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
# under the License.

##############################################################
# This configuration is meant to have Gremlin Server return
# GraphBinary serialized objects. The TinkerGraph IoRegistry is
# assigned as this is the configuration defined in the
# pre-packaged Gremlin Server configuration files. The
# client configuration for serializers should match server.
# This configuration is meant for use with with() or connect().
#
# g = traversal().with('conf/remote-graph.properties')
# g = connect('conf/remote-graph.properties')
#
# todo: revisit compatible configs
# This file will work with:
# - gremlin-server.yaml
# - gremlin-server-classic.yaml
# - gremlin-server-modern.yaml
# - gremlin-server-modern-py.yaml
# - gremlin-server-modern-readonly.yaml
# - gremlin-server-neo4j.yaml
# - gremlin-server-spark.yaml
# - gremlin-server-secure.yaml
##############################################################

hosts: [localhost]
port: 8182
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1 }
gremlin.remote.remoteConnectionClass=org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
gremlin.remote.driver.clusterFile=conf/remote.yaml
gremlin.remote.driver.sourceName=g
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@

hosts: [localhost]
port: 45940
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1, config: { serializeResultToString: true }}
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV4}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ private static Builder getBuilderFromSettings(final Settings settings) {
.enableUserAgentOnConnect(settings.enableUserAgentOnConnect)
.validationRequest(settings.connectionPool.validationRequest);

if (settings.auth.type != null) {
builder.auth(Auth.from(settings.auth));
}

// the first address was added above in the constructor, so skip it if there are more
if (addresses.size() > 1)
addresses.stream().skip(1).forEach(builder::addContactPoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
*
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
final class Settings {
public final class Settings {

/**
* The port of the Gremlin Server to connect to which defaults to {@code 8192}. The same port will be applied for
* The port of the Gremlin Server to connect to which defaults to {@code 8182}. The same port will be applied for
* all {@link #hosts}.
*/
public int port = 8182;
Expand All @@ -70,6 +70,11 @@ final class Settings {
*/
public ConnectionPoolSettings connectionPool = new ConnectionPoolSettings();

/**
* Settings for authentication.
*/
public AuthSettings auth = new AuthSettings();

/**
* The size of the thread pool defaulted to the number of available processors.
*/
Expand Down Expand Up @@ -207,6 +212,22 @@ public static Settings from(final Configuration conf) {
settings.connectionPool = cpSettings;
}

final Configuration authConf = conf.subset("auth");
if (IteratorUtils.count(authConf.getKeys()) > 0) {
final AuthSettings authSettings = new AuthSettings();

if (authConf.containsKey("type"))
authSettings.type = authConf.getString("type");

if (authConf.containsKey("username"))
authSettings.username = authConf.getString("username");

if (authConf.containsKey("password"))
authSettings.password = authConf.getString("password");

settings.auth = authSettings;
}

return settings;
}

Expand Down Expand Up @@ -345,4 +366,24 @@ public MessageSerializerV4<?> create() throws Exception {
return serializer;
}
}

public static class AuthSettings {
/**
* Type of Auth to submit on requests that require authentication.
*/
public String type = null;
/**
* The username to submit on requests that require authentication.
*/
public String username = null;
/**
* The password to submit on requests that require authentication.
*/
public String password = null;

/**
* The region setting for sigv4 authentication.
*/
public String region = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@

import com.amazonaws.auth.AWSCredentialsProvider;
import org.apache.tinkerpop.gremlin.driver.RequestInterceptor;
import org.apache.tinkerpop.gremlin.driver.Settings;

public interface Auth extends RequestInterceptor {
String AUTH_BASIC = "basic";
String AUTH_SIGV4 = "sigv4";

static Auth basic(final String username, final String password) {
return new Basic(username, password);
}
Expand All @@ -38,7 +42,17 @@ static Auth sigv4(final String regionName, final AWSCredentialsProvider awsCrede
return new Sigv4(regionName, awsCredentialsProvider, serviceName);
}

public class AuthenticationException extends RuntimeException {
static Auth from(Settings.AuthSettings settings) {
if (settings.type.equals(AUTH_BASIC)) {
return basic(settings.username, settings.password);
}
if (settings.type.equals(AUTH_SIGV4)) {
return sigv4(settings.region);
}
throw new IllegalArgumentException("Unknown auth type: " + settings.type);
}

class AuthenticationException extends RuntimeException {
public AuthenticationException(Exception cause) {
super(cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public void shouldCreateFromConfiguration() {
conf.setProperty("port", 8000);
conf.setProperty("nioPoolSize", 16);
conf.setProperty("workerPoolSize", 32);
conf.setProperty("username", "user1");
conf.setProperty("password", "password1");
conf.setProperty("jaasEntry", "JaasIt");
conf.setProperty("protocol", "protocol0");
conf.setProperty("auth.type", "basic");
conf.setProperty("auth.username", "user1");
conf.setProperty("auth.password", "password1");
conf.setProperty("hosts", Arrays.asList("255.0.0.1", "255.0.0.2", "255.0.0.3"));
conf.setProperty("serializer.className", "my.serializers.MySerializer");
conf.setProperty("serializer.config.any", "thing");
Expand Down Expand Up @@ -76,6 +75,9 @@ public void shouldCreateFromConfiguration() {
assertEquals(8000, settings.port);
assertEquals(16, settings.nioPoolSize);
assertEquals(32, settings.workerPoolSize);
assertEquals("basic", settings.auth.type);
assertEquals("user1", settings.auth.username);
assertEquals("password1", settings.auth.password);
assertEquals(Arrays.asList("255.0.0.1", "255.0.0.2", "255.0.0.3"), settings.hosts);
assertEquals("my.serializers.MySerializer", settings.serializer.className);
assertEquals("thing", settings.serializer.config.get("any"));
Expand Down

0 comments on commit f3679f1

Please sign in to comment.