You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/07/27 20:00:25 UTC
[5/9] incubator-tinkerpop git commit: The "secure" config now include
CompileStaticCompilerCustomizerProvider
The "secure" config now include CompileStaticCompilerCustomizerProvider
This meant that there had to be changes to the init script itself which wasn't type safe. It also meant providing a new way to promote bindings as unresolved variables are not allowed under CompileStatic and TypeChecked. Kept the old method of doing promotion but added the ability for the init script to pass back a Map of bindings to become global bindings. That seems to work well. Will deprecate the old method in 3.1.x.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/8b48e417
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/8b48e417
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/8b48e417
Branch: refs/heads/master
Commit: 8b48e41789e8ffdbd2da9f9f0cc50077b0dbc353
Parents: f0ddc36
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 27 12:24:04 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 27 12:24:04 2015 -0400
----------------------------------------------------------------------
CHANGELOG.asciidoc | 1 +
.../customizer/SimpleSandboxExtension.groovy | 3 +-
.../gremlin/groovy/engine/GremlinExecutor.java | 29 ++++++++----
.../groovy/engine/GremlinExecutorInit.groovy | 3 +-
gremlin-server/conf/gremlin-server-secure.yaml | 12 +++--
.../scripts/empty-sample-secure.groovy | 48 ++++++++++++++++++++
.../server/util/ServerGremlinExecutor.java | 2 +
7 files changed, 81 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 6f1e236..84f408f 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -25,6 +25,7 @@ image::http://www.tinkerpop.com/docs/current/images/gremlin-hindu.png[width=225]
TinkerPop 3.0.1 (NOT OFFICIALLY RELEASED YET)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+* Gremlin Server initialization scripts can now return a `Map` of values that will become global bindings for the server.
* Introduced the `--dryRun` option to the document generation process which ignores actual script execution in the Gremlin Console.
* Fixed bug in `EventStrategy` around property changed events when calling `property` without cardinality or meta-property values.
* Improved support for the `Accept` header for REST-based requests in Gremlin Server.
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
index 111e363..f316dc6 100644
--- a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
@@ -33,7 +33,8 @@ class SimpleSandboxExtension extends SandboxExtension {
graphIsAlwaysGraphInstance = false
methodFilter = (BiPredicate<String, MethodNode>) { descriptor, method ->
- return method.declaringClass.name != 'java.lang.System'
+ // declaring class is null when it refers to a script eval (it seems)
+ return null == method.declaringClass || method.declaringClass.name != 'java.lang.System'
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
index 4d9df08..bd549ea 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
@@ -232,7 +232,7 @@ public class GremlinExecutor implements AutoCloseable {
logger.debug("Preparing to evaluate script - {} - in thread [{}]", script, Thread.currentThread().getName());
final Bindings bindings = new SimpleBindings();
- bindings.putAll(this.globalBindings);
+ bindings.putAll(globalBindings);
bindings.putAll(boundVars);
beforeEval.accept(bindings);
@@ -417,19 +417,28 @@ public class GremlinExecutor implements AutoCloseable {
}).filter(p -> p.getValue1().isPresent()).map(p -> Pair.with(p.getValue0(), p.getValue1().get())).forEachOrdered(p -> {
try {
final Bindings bindings = new SimpleBindings();
- bindings.putAll(this.globalBindings);
+ bindings.putAll(globalBindings);
// evaluate init scripts with hard reference so as to ensure it doesn't get garbage collected
bindings.put(GremlinGroovyScriptEngine.KEY_REFERENCE_TYPE, GremlinGroovyScriptEngine.REFERENCE_TYPE_HARD);
- se.eval(p.getValue1(), bindings, language);
-
- // re-assign graph bindings back to global bindings and grab TraversalSource creations.
- // prevent assignment of non-graph implementations just in case someone tries to overwrite
- // them in the init
- bindings.entrySet().stream()
- .filter(promoteBinding)
- .forEach(kv -> this.globalBindings.put(kv.getKey(), kv.getValue()));
+ // todo: drop the "old method" in 3.1.0
+ // the returned object should be a Map of initialized global bindings - this is in contrast
+ // to the older method of scanning bindings for certain types/keys to promote. the reason
+ // for this change has to do with CompileStatic/TypeChecked "secure" configurations that
+ // require the defined variables to be typed. Under that scenario they are not "unresolved"
+ // and thus don't get set to bindings automatically by the InvokerHelper.
+ final Object initializedBindings = se.eval(p.getValue1(), bindings, language);
+ if (initializedBindings != null && initializedBindings instanceof Map) {
+ globalBindings.putAll((Map) initializedBindings);
+ } else {
+ // re-assign graph bindings back to global bindings and grab TraversalSource creations.
+ // prevent assignment of non-graph implementations just in case someone tries to overwrite
+ // them in the init
+ bindings.entrySet().stream()
+ .filter(promoteBinding)
+ .forEach(kv -> globalBindings.put(kv.getKey(), kv.getValue()));
+ }
logger.info("Initialized {} ScriptEngine with {}", language, p.getValue0());
} catch (ScriptException sx) {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
index 513ccd4..db046b0 100644
--- a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
+++ b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
@@ -19,4 +19,5 @@
def add(x, y) { x + y }
someSet = ["test"] as Set
-someMap = [name:"stephen"]
\ No newline at end of file
+someMap = [name:"stephen"]
+""
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-server/conf/gremlin-server-secure.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/conf/gremlin-server-secure.yaml b/gremlin-server/conf/gremlin-server-secure.yaml
index 530b6cb..7fce9f7 100644
--- a/gremlin-server/conf/gremlin-server-secure.yaml
+++ b/gremlin-server/conf/gremlin-server-secure.yaml
@@ -20,7 +20,7 @@
# secure configuration of Gremlin Server. Keep in mind that
# this configuration uses a generated self-signed certificate
# for SSL and a not so secure TinkerGraph for the credential
-# store - not suitable for production
+# store - not suitable for production.
##############################################################
host: localhost
@@ -38,10 +38,12 @@ scriptEngines: {
gremlin-groovy: {
imports: [java.lang.Math],
staticImports: [java.lang.Math.PI],
- scripts: [scripts/empty-sample.groovy]},
- nashorn: {
- imports: [java.lang.Math],
- staticImports: [java.lang.Math.PI]}}
+ scripts: [scripts/empty-sample-secure.groovy],
+ config: {
+ compilerCustomizerProviders: {
+ "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.ThreadInterruptCustomizerProvider":[],
+ "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TimedInterruptCustomizerProvider":[10000],
+ "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider":["org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SimpleSandboxExtension"]}}}}
serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0 } # application/vnd.gremlin-v1.0+gryo
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }} # application/vnd.gremlin-v1.0+gryo-stringd
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-server/scripts/empty-sample-secure.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/empty-sample-secure.groovy b/gremlin-server/scripts/empty-sample-secure.groovy
new file mode 100644
index 0000000..614e81f
--- /dev/null
+++ b/gremlin-server/scripts/empty-sample-secure.groovy
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+/*******************************************************************
+ * This script is meant to be executed with the
+ * conf/gremlin-server-secure.yaml configuration file as an example
+ * of how an init script running with CompileStaticCustomizerProvider
+ * or TypeCheckedCustomizerProvider must be written to properly
+ * execute.
+ *******************************************************************/
+
+// An example of an initialization script that can be configured to run in Gremlin Server.
+// Functions defined here will go into global cache and will not be removed from there
+// unless there is a reset of the ScriptEngine.
+def addItUp(int x, int y) { x + y }
+
+// an init script that returns a Map allows explicit setting of global bindings.
+def globals = [:]
+
+// defines a sample LifeCycleHook that prints some output to the Gremlin Server console.
+// note that the name of the key in the "global" map is unimportant.
+globals << [hook : [
+ onStartUp: { LifeCycleHook.Context ctx ->
+ ctx.logger.info("Executed once at startup of Gremlin Server.")
+ },
+ onShutDown: { LifeCycleHook.Context ctx ->
+ ctx.logger.info("Executed once at shutdown of Gremlin Server.")
+ }
+] as LifeCycleHook]
+
+// define the default TraversalSource to bind queries to - this one will be named "g".
+globals << [g : graph.traversal()]
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
index 714db3e..02a763a 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
@@ -95,6 +95,7 @@ public class ServerGremlinExecutor<T extends ScheduledExecutorService> {
logger.info("Initialized Gremlin thread pool. Threads in pool named with pattern gremlin-*");
+ // todo: deprecate promoteBindings in 3.1.0
final GremlinExecutor.Builder gremlinExecutorBuilder = GremlinExecutor.build()
.scriptEvaluationTimeout(settings.scriptEvaluationTimeout)
.afterFailure((b, e) -> graphManager.rollbackAll())
@@ -111,6 +112,7 @@ public class ServerGremlinExecutor<T extends ScheduledExecutorService> {
settings.scriptEngines.forEach((k, v) -> {
// make sure that server related classes are available at init
v.imports.add(LifeCycleHook.class.getCanonicalName());
+ v.imports.add(LifeCycleHook.Context.class.getCanonicalName());
gremlinExecutorBuilder.addEngineSettings(k, v.imports, v.staticImports, v.scripts, v.config);
});