This is an automated email from the ASF dual-hosted git repository.
symat pushed a commit to branch branch-3.6
in repository https://gitbox.apache.org/repos/asf/zookeeper.git
The following commit(s) were added to refs/heads/branch-3.6 by this push:
new 8bcaf7b ZOOKEEPER-3905: Race condition causes sessions to be created
for clients even though their certificate authentication has failed (3.6)
8bcaf7b is described below
commit 8bcaf7bb3cfa6470e1660e2b36964ae2284197df
Author: Andor Molnar <[email protected]>
AuthorDate: Wed Aug 12 06:23:06 2020 +0000
ZOOKEEPER-3905: Race condition causes sessions to be created for clients
even though their certificate authentication has failed (3.6)
Backport to branch-3.6
Netty channel doesn't get closed if authentication fails after a successful
SSL handshake. We need a custom authentication provider in order to trigger
this, because the default implementation does the same check as for the SSL
handshake. Hence it never fails.
Unit test added to make sure client is not able to connect.
Author: Andor Molnar <[email protected]>
Reviewers: Enrico Olivelli <[email protected]>, Mate Szalay-Beko
<[email protected]>, Norbert Kalmar <[email protected]>
Closes #1427 from anmolnar/ZOOKEEPER-3905_36
---
.../apache/zookeeper/server/NettyServerCnxn.java | 3 ++
.../zookeeper/server/auth/ProviderRegistry.java | 6 ++-
.../zookeeper/server/quorum/QuorumPeerConfig.java | 8 ++--
.../test/AuthFailX509AuthenticationProvider.java | 51 ++++++++++++++++++++++
.../java/org/apache/zookeeper/test/ClientBase.java | 6 ++-
.../org/apache/zookeeper/test/ClientSSLTest.java | 28 ++++++++++++
6 files changed, 96 insertions(+), 6 deletions(-)
diff --git
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxn.java
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxn.java
index 442f39d..f25f9a4 100644
---
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxn.java
+++
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxn.java
@@ -115,6 +115,9 @@ public class NettyServerCnxn extends ServerCnxn {
// if this is not in cnxns then it's already closed
if (!factory.cnxns.remove(this)) {
LOG.debug("cnxns size:{}", factory.cnxns.size());
+ if (channel.isOpen()) {
+ channel.close();
+ }
return;
}
diff --git
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ProviderRegistry.java
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ProviderRegistry.java
index 29997a2..856cf78 100644
---
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ProviderRegistry.java
+++
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ProviderRegistry.java
@@ -29,8 +29,10 @@ public class ProviderRegistry {
private static final Logger LOG =
LoggerFactory.getLogger(ProviderRegistry.class);
+ public static final String AUTHPROVIDER_PROPERTY_PREFIX =
"zookeeper.authProvider.";
+
private static boolean initialized = false;
- private static Map<String, AuthenticationProvider> authenticationProviders
= new HashMap<>();
+ private static final Map<String, AuthenticationProvider>
authenticationProviders = new HashMap<>();
//VisibleForTesting
public static void reset() {
@@ -49,7 +51,7 @@ public class ProviderRegistry {
Enumeration<Object> en = System.getProperties().keys();
while (en.hasMoreElements()) {
String k = (String) en.nextElement();
- if (k.startsWith("zookeeper.authProvider.")) {
+ if (k.startsWith(AUTHPROVIDER_PROPERTY_PREFIX)) {
String className = System.getProperty(k);
try {
Class<?> c =
ZooKeeperServer.class.getClassLoader().loadClass(className);
diff --git
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
index ae73ef3..ef13d74 100644
---
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
+++
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
@@ -46,6 +46,7 @@ import org.apache.zookeeper.common.PathUtils;
import org.apache.zookeeper.common.StringUtils;
import org.apache.zookeeper.metrics.impl.DefaultMetricsProvider;
import org.apache.zookeeper.server.ZooKeeperServer;
+import org.apache.zookeeper.server.auth.ProviderRegistry;
import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType;
import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
import org.apache.zookeeper.server.quorum.auth.QuorumAuth;
@@ -521,11 +522,12 @@ public class QuorumPeerConfig {
*/
public static void configureSSLAuth() throws ConfigException {
try (ClientX509Util clientX509Util = new ClientX509Util()) {
- String sslAuthProp = "zookeeper.authProvider."
+ String sslAuthProp = ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX
+
System.getProperty(clientX509Util.getSslAuthProviderProperty(), "x509");
if (System.getProperty(sslAuthProp) == null) {
- if ("zookeeper.authProvider.x509".equals(sslAuthProp)) {
- System.setProperty("zookeeper.authProvider.x509",
"org.apache.zookeeper.server.auth.X509AuthenticationProvider");
+ if ((ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX +
"x509").equals(sslAuthProp)) {
+
System.setProperty(ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX + "x509",
+
"org.apache.zookeeper.server.auth.X509AuthenticationProvider");
} else {
throw new ConfigException("No auth provider configured for
the SSL authentication scheme '"
+
System.getProperty(clientX509Util.getSslAuthProviderProperty())
diff --git
a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthFailX509AuthenticationProvider.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthFailX509AuthenticationProvider.java
new file mode 100644
index 0000000..32121e6
--- /dev/null
+++
b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthFailX509AuthenticationProvider.java
@@ -0,0 +1,51 @@
+/*
+ * 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.zookeeper.test;
+
+import javax.net.ssl.X509KeyManager;
+import javax.net.ssl.X509TrustManager;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.common.X509Exception;
+import org.apache.zookeeper.server.ServerCnxn;
+import org.apache.zookeeper.server.auth.X509AuthenticationProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AuthFailX509AuthenticationProvider extends
X509AuthenticationProvider {
+ private static final Logger LOG =
LoggerFactory.getLogger(AuthFailX509AuthenticationProvider.class);
+
+ public AuthFailX509AuthenticationProvider() throws X509Exception {
+ super();
+ }
+
+ public AuthFailX509AuthenticationProvider(X509TrustManager trustManager,
X509KeyManager keyManager) {
+ super(trustManager, keyManager);
+ }
+
+ @Override
+ public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[]
authData) {
+ LOG.info("Authentication failed");
+ return KeeperException.Code.AUTHFAILED;
+ }
+
+ @Override
+ public String getScheme() {
+ return "authfail";
+ }
+}
diff --git
a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java
index 4235b6b..bb3b7be 100644
--- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java
@@ -735,10 +735,14 @@ public abstract class ClientBase extends ZKTestCase {
* in cases of network failure
*/
public static ZooKeeper createZKClient(String cxnString, int
sessionTimeout) throws IOException {
+ return createZKClient(cxnString, sessionTimeout, CONNECTION_TIMEOUT);
+ }
+
+ public static ZooKeeper createZKClient(String cxnString, int
sessionTimeout, long connectionTimeout) throws IOException {
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(cxnString, sessionTimeout, watcher);
try {
- watcher.waitForConnected(CONNECTION_TIMEOUT);
+ watcher.waitForConnected(connectionTimeout);
} catch (InterruptedException | TimeoutException e) {
fail("ZooKeeper client can not connect to " + cxnString);
}
diff --git
a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSSLTest.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSSLTest.java
index 1d3233f..12838a5 100644
---
a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSSLTest.java
+++
b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSSLTest.java
@@ -23,6 +23,8 @@
package org.apache.zookeeper.test;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.PortAssignment;
import org.apache.zookeeper.ZooDefs;
@@ -31,15 +33,21 @@ import org.apache.zookeeper.client.ZKClientConfig;
import org.apache.zookeeper.common.ClientX509Util;
import org.apache.zookeeper.server.NettyServerCnxnFactory;
import org.apache.zookeeper.server.ServerCnxnFactory;
+import org.apache.zookeeper.server.auth.ProviderRegistry;
import org.apache.zookeeper.server.quorum.QuorumPeerTestBase;
import org.junit.After;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
public class ClientSSLTest extends QuorumPeerTestBase {
private ClientX509Util clientX509Util;
+ @Rule
+ public ExpectedException exceptionRule = ExpectedException.none();
+
@Before
public void setup() {
System.setProperty(NettyServerCnxnFactory.PORT_UNIFICATION_KEY,
Boolean.TRUE.toString());
@@ -157,4 +165,24 @@ public class ClientSSLTest extends QuorumPeerTestBase {
mt.shutdown();
}
+ @Test
+ public void testSecureStandaloneServerAuthFail() throws IOException {
+ exceptionRule.expect(AssertionError.class);
+ exceptionRule.expectMessage("ZooKeeper client can not connect");
+ try {
+ System.setProperty(ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX +
"authfail",
+
"org.apache.zookeeper.test.AuthFailX509AuthenticationProvider");
+ System.setProperty(clientX509Util.getSslAuthProviderProperty(),
"authfail");
+
+ Integer secureClientPort = PortAssignment.unique();
+ MainThread mt = new MainThread(MainThread.UNSET_MYID, "",
secureClientPort, false);
+ mt.start();
+
+ ClientBase.createZKClient("localhost:" + secureClientPort,
TIMEOUT, 3000);
+ fail("Client should not able to connect to this server, because
auth failed");
+ } finally {
+ System.clearProperty(ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX
+ "authfail");
+ System.clearProperty(clientX509Util.getSslAuthProviderProperty());
+ }
+ }
}