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/20 22:35:40 UTC

[17/29] incubator-tinkerpop git commit: Better handling of SaslException in the remote acceptor.

Better handling of SaslException in the remote acceptor.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/c1728b81
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/c1728b81
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/c1728b81

Branch: refs/heads/master
Commit: c1728b813c52ecf52fd2baaf54d00619a262bf81
Parents: a2a2ff1
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 17 15:41:27 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 17 15:41:27 2015 -0400

----------------------------------------------------------------------
 .../console/groovy/plugin/DriverRemoteAcceptor.java   | 13 +++++++++++--
 .../org/apache/tinkerpop/gremlin/driver/Handler.java  | 14 +++++---------
 2 files changed, 16 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c1728b81/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java
----------------------------------------------------------------------
diff --git a/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java b/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java
index 0d662a1..55f77cc 100644
--- a/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java
+++ b/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java
@@ -28,6 +28,7 @@ import org.apache.tinkerpop.gremlin.groovy.plugin.RemoteException;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.codehaus.groovy.tools.shell.Groovysh;
 
+import javax.security.sasl.SaslException;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.Arrays;
@@ -35,6 +36,7 @@ import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.stream.Stream;
 
 /**
  * @author Stephen Mallette (http://stephen.genoprime.com)
@@ -102,6 +104,8 @@ public class DriverRemoteAcceptor implements RemoteAcceptor {
             final List<Result> resultSet = send(line);
             this.shell.getInterp().getContext().setProperty(RESULT, resultSet);
             return resultSet.stream().map(result -> result.getObject()).iterator();
+        } catch (SaslException sasl) {
+            throw new RemoteException("Security error - check username/password and related settings", sasl);
         } catch (Exception ex) {
             final Optional<ResponseException> inner = findResponseException(ex);
             if (inner.isPresent()) {
@@ -127,12 +131,17 @@ public class DriverRemoteAcceptor implements RemoteAcceptor {
         if (this.currentCluster != null) this.currentCluster.close();
     }
 
-    private List<Result> send(final String gremlin) {
+    private List<Result> send(final String gremlin) throws SaslException {
         try {
             return this.currentClient.submit(gremlin).all().get(this.timeout, TimeUnit.MILLISECONDS);
-        } catch (TimeoutException ignored) {
+        } catch(TimeoutException ignored) {
             throw new IllegalStateException("Request timed out while processing - increase the timeout with the :remote command");
         } catch (Exception e) {
+            // handle security error as-is and unwrapped
+            final Optional<Throwable> throwable  = Stream.of(ExceptionUtils.getThrowables(e)).filter(t -> t instanceof SaslException).findFirst();
+            if (throwable.isPresent())
+                throw (SaslException) throwable.get();
+
             throw new IllegalStateException(e.getMessage(), e);
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c1728b81/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Handler.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Handler.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Handler.java
index bf4578e..6658087 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Handler.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Handler.java
@@ -80,9 +80,9 @@ final class Handler {
             // We are only interested in AUTHENTICATE responses here. Everything else can
             // get passed down the pipeline
             if (response.getStatus().getCode() == ResponseStatusCode.AUTHENTICATE) {
-                Attribute<SaslClient> saslClient = channelHandlerContext.attr(saslClientKey);
-                Attribute<Subject> subject = channelHandlerContext.attr(subjectKey);
-                byte[] saslResponse = null;
+                final Attribute<SaslClient> saslClient = channelHandlerContext.attr(saslClientKey);
+                final Attribute<Subject> subject = channelHandlerContext.attr(subjectKey);
+                byte[] saslResponse;
                 // First time through we don't have a sasl client
                 if (saslClient.get() == null) {
                     subject.set(login());
@@ -97,7 +97,7 @@ final class Handler {
             }
         }
 
-        public void handle(Callback[] callbacks) {
+        public void handle(final Callback[] callbacks) {
             for (Callback callback : callbacks) {
                 if (callback instanceof NameCallback) {
                     if (authProps.get(AuthProperties.Property.USERNAME) != null) {
@@ -121,11 +121,7 @@ final class Handler {
             } else {
                 // If we have a subject then run this as a privileged action using the subject
                 try {
-                    return Subject.doAs(subject.get(), new PrivilegedExceptionAction<byte[]>() {
-                        public byte[] run() throws SaslException {
-                            return saslClient.get().evaluateChallenge(challenge);
-                        }
-                    });
+                    return Subject.doAs(subject.get(), (PrivilegedExceptionAction<byte[]>) () -> saslClient.get().evaluateChallenge(challenge));
                 } catch (PrivilegedActionException e) {
                     throw (SaslException)e.getException();
                 }