Browse Source

ZOOKEEPER-1550. ZooKeeperSaslClient does not finish anonymous login on OpenJDK (Eugene Koontz via mahadev)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1391526 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 13 years ago
parent
commit
8c6155afb6

+ 3 - 0
CHANGES.txt

@@ -255,6 +255,9 @@ BUGFIXES:
   ZOOKEEPER-1496. Ephemeral node not getting cleared even after client has
   ZOOKEEPER-1496. Ephemeral node not getting cleared even after client has
   exited. (Rakesh R via mahadev)
   exited. (Rakesh R via mahadev)
 
 
+  ZOOKEEPER-1550. ZooKeeperSaslClient does not finish anonymous login on
+  OpenJDK (Eugene Koontz via mahadev)
+
 IMPROVEMENTS:
 IMPROVEMENTS:
 
 
   ZOOKEEPER-1170. Fix compiler (eclipse) warnings: unused imports,
   ZOOKEEPER-1170. Fix compiler (eclipse) warnings: unused imports,

+ 4 - 0
src/java/main/org/apache/zookeeper/Login.java

@@ -270,6 +270,10 @@ public class Login {
         return subject;
         return subject;
     }
     }
 
 
+    public String getLoginContextName() {
+        return loginContextName;
+    }
+
     private synchronized LoginContext login(final String loginContextName) throws LoginException {
     private synchronized LoginContext login(final String loginContextName) throws LoginException {
         if (loginContextName == null) {
         if (loginContextName == null) {
             throw new LoginException("loginContext name (JAAS file section header) was null. " +
             throw new LoginException("loginContext name (JAAS file section header) was null. " +

+ 5 - 0
src/java/main/org/apache/zookeeper/ZooKeeper.java

@@ -23,6 +23,7 @@ import org.apache.zookeeper.OpResult.ErrorResult;
 import org.apache.zookeeper.client.ConnectStringParser;
 import org.apache.zookeeper.client.ConnectStringParser;
 import org.apache.zookeeper.client.HostProvider;
 import org.apache.zookeeper.client.HostProvider;
 import org.apache.zookeeper.client.StaticHostProvider;
 import org.apache.zookeeper.client.StaticHostProvider;
+import org.apache.zookeeper.client.ZooKeeperSaslClient;
 import org.apache.zookeeper.common.PathUtils;
 import org.apache.zookeeper.common.PathUtils;
 import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.Stat;
 import org.apache.zookeeper.data.Stat;
@@ -94,6 +95,10 @@ public class ZooKeeper {
         Environment.logEnv("Client environment:", LOG);
         Environment.logEnv("Client environment:", LOG);
     }
     }
 
 
+    public ZooKeeperSaslClient getSaslClient() {
+        return cnxn.zooKeeperSaslClient;
+    }
+
     private final ZKWatchManager watchManager = new ZKWatchManager();
     private final ZKWatchManager watchManager = new ZKWatchManager();
 
 
     List<String> getDataWatches() {
     List<String> getDataWatches() {

+ 11 - 7
src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java

@@ -76,10 +76,10 @@ public class ZooKeeperSaslClient {
         return saslState;
         return saslState;
     }
     }
 
 
-    private String loginContext;
-
     public String getLoginContext() {
     public String getLoginContext() {
-      return loginContext;
+        if (login != null)
+            return login.getLoginContextName();
+        return null;
     }
     }
 
 
     public ZooKeeperSaslClient(final String serverPrincipal)
     public ZooKeeperSaslClient(final String serverPrincipal)
@@ -193,7 +193,6 @@ public class ZooKeeperSaslClient {
                 if (LOG.isDebugEnabled()) {
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("JAAS loginContext is: " + loginContext);
                     LOG.debug("JAAS loginContext is: " + loginContext);
                 }
                 }
-                this.loginContext = loginContext;
                 // note that the login object is static: it's shared amongst all zookeeper-related connections.
                 // note that the login object is static: it's shared amongst all zookeeper-related connections.
                 // createSaslClient() must be declared synchronized so that login is initialized only once.
                 // createSaslClient() must be declared synchronized so that login is initialized only once.
                 login = new Login(loginContext, new ClientCallbackHandler(null));
                 login = new Login(loginContext, new ClientCallbackHandler(null));
@@ -485,9 +484,14 @@ public class ZooKeeperSaslClient {
         // variable or method in this class to determine whether the client is
         // variable or method in this class to determine whether the client is
         // configured to use SASL. (see also ZOOKEEPER-1455).
         // configured to use SASL. (see also ZOOKEEPER-1455).
         try {
         try {
-            if ((System.getProperty(Environment.JAAS_CONF_KEY) != null) ||
-                (javax.security.auth.login.Configuration.getConfiguration() != null)) {
-                // Client is configured to use SASL.
+  	    if ((System.getProperty(Environment.JAAS_CONF_KEY) != null) ||
+              ((javax.security.auth.login.Configuration.getConfiguration() != null) &&
+                  (javax.security.auth.login.Configuration.getConfiguration().
+                       getAppConfigurationEntry(System.
+                       getProperty(ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY,"Client")) 
+                           != null))) {
+                // Client is configured to use a valid login Configuration, so
+                // authentication is either in progress, successful, or failed.
 
 
                 // 1. Authentication hasn't finished yet: we must wait for it to do so.
                 // 1. Authentication hasn't finished yet: we must wait for it to do so.
                 if ((isComplete() == false) &&
                 if ((isComplete() == false) &&

+ 23 - 17
src/java/test/org/apache/zookeeper/test/SaslAuthDesignatedClientTest.java

@@ -21,13 +21,10 @@ package org.apache.zookeeper.test;
 import java.io.File;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.IOException;
-import java.util.concurrent.atomic.AtomicInteger;
 
 
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.ZooKeeper;
-import org.apache.zookeeper.Watcher.Event.KeeperState;
 import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.client.ZooKeeperSaslClient;
 import org.apache.zookeeper.client.ZooKeeperSaslClient;
 import org.junit.Assert;
 import org.junit.Assert;
@@ -68,20 +65,6 @@ public class SaslAuthDesignatedClientTest extends ClientBase {
         }
         }
     }
     }
 
 
-    private AtomicInteger authFailed = new AtomicInteger(0);
-
-    private class MyWatcher extends CountdownWatcher {
-        @Override
-        public synchronized void process(WatchedEvent event) {
-            if (event.getState() == KeeperState.AuthFailed) {
-                authFailed.incrementAndGet();
-            }
-            else {
-                super.process(event);
-            }
-        }
-    }
-
     @Test
     @Test
     public void testAuth() throws Exception {
     public void testAuth() throws Exception {
         ZooKeeper zk = createClient();
         ZooKeeper zk = createClient();
@@ -95,4 +78,27 @@ public class SaslAuthDesignatedClientTest extends ClientBase {
             zk.close();
             zk.close();
         }
         }
     }
     }
+
+    @Test
+    public void testSaslConfig() throws Exception {
+        ZooKeeper zk = createClient();
+        try {
+            zk.getChildren("/", false);
+            Assert.assertFalse(zk.getSaslClient().
+                clientTunneledAuthenticationInProgress());
+            Assert.assertEquals(zk.getSaslClient().getSaslState(),
+                ZooKeeperSaslClient.SaslState.COMPLETE);
+            Assert.assertNotNull(
+                javax.security.auth.login.Configuration.getConfiguration().
+                    getAppConfigurationEntry("MyZookeeperClient"));
+            Assert.assertSame(zk.getSaslClient().getLoginContext(),
+                "MyZookeeperClient");
+        } catch (KeeperException e) {
+            Assert.fail("test failed :" + e);
+        } finally {
+            zk.close();
+        }
+    }
+
+
 }
 }