Skip to content

Commit e2070be

Browse files
committed
ZOOKEEPER-4753: zookeeper-server: Improvement: Explicit handling of DIGEST-MD5 vs GSSAPI in quorum auth
Before this, the SASL-based quorum authorizer did not explicitly distinguish between the DIGEST-MD5 and GSSAPI mechanisms: it was simply relying on NameCallback and PasswordCallback for authentication with the former and examining Kerberos principals in AuthorizeCallback for the latter. It turns out that some SASL/DIGEST-MD5 configurations cause authentication and authorization IDs not to match the expected format, and the DIGEST-MD5-based portions of the quorum test suite to fail with obscure errors. (They can be traced to failures to join the quorum, but only by looking into detailed logs.) This patch uses the login module name to determine whether DIGEST-MD5 or GSSAPI is used, and relaxes the authentication ID check for the former. As a cleanup, it keeps the password-based credential map empty when Kerberos principals are expected. It finally adapts a test, and adds a new one, ensuring "weirdly-shaped" credentials only cause authentication failures in the GSSAPI case.
1 parent 6cae2cd commit e2070be

File tree

3 files changed

+107
-21
lines changed

3 files changed

+107
-21
lines changed

Diff for: zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumServerCallbackHandler.java

+32-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.zookeeper.server.quorum.auth;
2020

2121
import java.io.IOException;
22+
import java.util.Collections;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425
import java.util.Set;
@@ -31,6 +32,7 @@
3132
import javax.security.auth.login.Configuration;
3233
import javax.security.sasl.AuthorizeCallback;
3334
import javax.security.sasl.RealmCallback;
35+
import org.apache.zookeeper.server.auth.DigestLoginModule;
3436
import org.slf4j.Logger;
3537
import org.slf4j.LoggerFactory;
3638

@@ -46,7 +48,8 @@ public class SaslQuorumServerCallbackHandler implements CallbackHandler {
4648
private static final Logger LOG = LoggerFactory.getLogger(SaslQuorumServerCallbackHandler.class);
4749

4850
private String userName;
49-
private final Map<String, String> credentials = new HashMap<>();
51+
private final boolean isDigestAuthn;
52+
private final Map<String, String> credentials;
5053
private final Set<String> authzHosts;
5154

5255
public SaslQuorumServerCallbackHandler(
@@ -60,20 +63,35 @@ public SaslQuorumServerCallbackHandler(
6063
LOG.error(errorMessage);
6164
throw new IOException(errorMessage);
6265
}
63-
credentials.clear();
66+
67+
Map<String, String> credentials = new HashMap<>();
68+
boolean isDigestAuthn = true;
69+
6470
for (AppConfigurationEntry entry : configurationEntries) {
65-
Map<String, ?> options = entry.getOptions();
66-
// Populate DIGEST-MD5 user -> password map with JAAS configuration entries from the "QuorumServer" section.
67-
// Usernames are distinguished from other options by prefixing the username with a "user_" prefix.
68-
for (Map.Entry<String, ?> pair : options.entrySet()) {
69-
String key = pair.getKey();
70-
if (key.startsWith(USER_PREFIX)) {
71-
String userName = key.substring(USER_PREFIX.length());
72-
credentials.put(userName, (String) pair.getValue());
71+
if (entry.getLoginModuleName().equals(DigestLoginModule.class.getName())) {
72+
Map<String, ?> options = entry.getOptions();
73+
// Populate DIGEST-MD5 user -> password map with JAAS configuration entries from the "QuorumServer" section.
74+
// Usernames are distinguished from other options by prefixing the username with a "user_" prefix.
75+
for (Map.Entry<String, ?> pair : options.entrySet()) {
76+
String key = pair.getKey();
77+
if (key.startsWith(USER_PREFIX)) {
78+
String userName = key.substring(USER_PREFIX.length());
79+
credentials.put(userName, (String) pair.getValue());
80+
}
7381
}
82+
} else {
83+
isDigestAuthn = false;
7484
}
7585
}
7686

87+
this.isDigestAuthn = isDigestAuthn;
88+
if (isDigestAuthn) {
89+
this.credentials = Collections.unmodifiableMap(credentials);
90+
LOG.warn("Using DIGEST-MD5 for quorum authorization");
91+
} else {
92+
this.credentials = Collections.emptyMap();
93+
}
94+
7795
// authorized host lists
7896
this.authzHosts = authzHosts;
7997
}
@@ -126,13 +144,15 @@ private void handleAuthorizeCallback(AuthorizeCallback ac) {
126144
// 2. Verify whether the connecting host is present in authorized hosts.
127145
// If not exists, then connecting peer is not authorized to join the
128146
// ensemble and will reject it.
129-
if (authzFlag) {
147+
if (!isDigestAuthn && authzFlag) {
130148
String[] components = authorizationID.split("[/@]");
131149
if (components.length == 3) {
132150
authzFlag = authzHosts.contains(components[1]);
151+
} else {
152+
authzFlag = false;
133153
}
134154
if (!authzFlag) {
135-
LOG.error("SASL authorization completed, {} is not authorized to connect", components[1]);
155+
LOG.error("SASL authorization completed, {} is not authorized to connect", authorizationID);
136156
}
137157
}
138158

Diff for: zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosAuthTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class QuorumKerberosAuthTest extends KerberosSecurityTestcase {
5757
+ " debug=false\n"
5858
+ " refreshKrb5Config=true\n"
5959
+ " principal=\""
60-
+ KerberosTestUtils.getServerPrincipal()
60+
+ KerberosTestUtils.replaceHostPattern(KerberosTestUtils.getHostServerPrincipal())
6161
+ "\";\n"
6262
+ "};\n"
6363
+ "QuorumLearner {\n"
@@ -71,7 +71,7 @@ public class QuorumKerberosAuthTest extends KerberosSecurityTestcase {
7171
+ " debug=false\n"
7272
+ " refreshKrb5Config=true\n"
7373
+ " principal=\""
74-
+ KerberosTestUtils.getLearnerPrincipal()
74+
+ KerberosTestUtils.replaceHostPattern(KerberosTestUtils.getHostLearnerPrincipal())
7575
+ "\";\n"
7676
+ "};\n";
7777
setupJaasConfig(jaasEntries);
@@ -81,10 +81,10 @@ public class QuorumKerberosAuthTest extends KerberosSecurityTestcase {
8181
public static void setUp() throws Exception {
8282
// create keytab
8383
keytabFile = new File(KerberosTestUtils.getKeytabFile());
84-
String learnerPrincipal = KerberosTestUtils.getLearnerPrincipal();
85-
String serverPrincipal = KerberosTestUtils.getServerPrincipal();
86-
learnerPrincipal = learnerPrincipal.substring(0, learnerPrincipal.lastIndexOf("@"));
87-
serverPrincipal = serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@"));
84+
String learnerPrincipal = KerberosTestUtils.getHostLearnerPrincipal();
85+
String serverPrincipal = KerberosTestUtils.getHostServerPrincipal();
86+
learnerPrincipal = KerberosTestUtils.replaceHostPattern(learnerPrincipal.substring(0, learnerPrincipal.lastIndexOf("@")));
87+
serverPrincipal = KerberosTestUtils.replaceHostPattern(serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@")));
8888
getKdc().createPrincipal(keytabFile, learnerPrincipal, serverPrincipal);
8989
}
9090

Diff for: zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosHostBasedAuthTest.java

+69-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ public class QuorumKerberosHostBasedAuthTest extends KerberosSecurityTestcase {
4444
private static String hostServerPrincipal = KerberosTestUtils.getHostServerPrincipal();
4545
private static String hostLearnerPrincipal = KerberosTestUtils.getHostLearnerPrincipal();
4646
private static String hostNamedLearnerPrincipal = KerberosTestUtils.getHostNamedLearnerPrincipal("myHost");
47+
private static String hostlessLearnerPrincipal = KerberosTestUtils.getLearnerPrincipal();
4748

4849
static {
49-
setupJaasConfigEntries(hostServerPrincipal, hostLearnerPrincipal, hostNamedLearnerPrincipal);
50+
setupJaasConfigEntries(hostServerPrincipal, hostLearnerPrincipal, hostNamedLearnerPrincipal, hostlessLearnerPrincipal);
5051
}
5152

5253
private static void setupJaasConfigEntries(
5354
String hostServerPrincipal,
5455
String hostLearnerPrincipal,
55-
String hostNamedLearnerPrincipal) {
56+
String hostNamedLearnerPrincipal,
57+
String hostlessLearnerPrincipal) {
5658
String keytabFilePath = FilenameUtils.normalize(KerberosTestUtils.getKeytabFile(), true);
5759

5860
// note: we use "refreshKrb5Config=true" to refresh the kerberos config in the JVM,
@@ -93,6 +95,18 @@ private static void setupJaasConfigEntries(
9395
+ " refreshKrb5Config=true\n"
9496
+ " principal=\"" + hostNamedLearnerPrincipal
9597
+ "\";\n"
98+
+ "};\n"
99+
+ "QuorumLearnerMissingHost {\n"
100+
+ " com.sun.security.auth.module.Krb5LoginModule required\n"
101+
+ " useKeyTab=true\n"
102+
+ " keyTab=\"" + keytabFilePath
103+
+ "\"\n"
104+
+ " storeKey=true\n"
105+
+ " useTicketCache=false\n"
106+
+ " debug=false\n"
107+
+ " refreshKrb5Config=true\n"
108+
+ " principal=\"" + hostlessLearnerPrincipal
109+
+ "\";\n"
96110
+ "};\n";
97111
setupJaasConfig(jaasEntries);
98112
}
@@ -110,7 +124,11 @@ public static void setUp() throws Exception {
110124

111125
// learner with ipaddress in principal
112126
String learnerPrincipal2 = hostNamedLearnerPrincipal.substring(0, hostNamedLearnerPrincipal.lastIndexOf("@"));
113-
getKdc().createPrincipal(keytabFile, learnerPrincipal, learnerPrincipal2, serverPrincipal);
127+
128+
// learner without host in principal
129+
String learnerPrincipal3 = hostlessLearnerPrincipal.substring(0, hostlessLearnerPrincipal.lastIndexOf("@"));
130+
131+
getKdc().createPrincipal(keytabFile, learnerPrincipal, learnerPrincipal2, learnerPrincipal3, serverPrincipal);
114132
}
115133

116134
@AfterEach
@@ -224,4 +242,52 @@ public void testConnectBadServer() throws Exception {
224242
}
225243
}
226244

245+
/**
246+
* Test to verify that the bad server connection to the quorum should be rejected.
247+
*/
248+
@Test
249+
@Timeout(value = 120)
250+
public void testConnectHostlessPrincipalBadServer() throws Exception {
251+
String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@"));
252+
Map<String, String> authConfigs = new HashMap<>();
253+
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
254+
authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
255+
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
256+
authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal);
257+
String connectStr = startQuorum(3, authConfigs, 3);
258+
CountdownWatcher watcher = new CountdownWatcher();
259+
ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
260+
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
261+
for (int i = 0; i < 10; i++) {
262+
zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
263+
}
264+
zk.close();
265+
266+
String quorumCfgSection = mt.get(0).getQuorumCfgSection();
267+
StringBuilder sb = new StringBuilder();
268+
sb.append(quorumCfgSection);
269+
270+
int myid = mt.size() + 1;
271+
final int clientPort = PortAssignment.unique();
272+
String server = String.format("server.%d=localhost:%d:%d:participant", myid, PortAssignment.unique(), PortAssignment.unique());
273+
sb.append(server + "\n");
274+
quorumCfgSection = sb.toString();
275+
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearnerMissingHost");
276+
MainThread badServer = new MainThread(myid, clientPort, quorumCfgSection, authConfigs);
277+
badServer.start();
278+
watcher = new CountdownWatcher();
279+
connectStr = "127.0.0.1:" + clientPort;
280+
zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
281+
try {
282+
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT / 3);
283+
fail("Must throw exception as the principal does not include an authorized host!");
284+
} catch (TimeoutException e) {
285+
// expected
286+
} finally {
287+
zk.close();
288+
badServer.shutdown();
289+
badServer.deleteBaseDir();
290+
}
291+
}
292+
227293
}

0 commit comments

Comments
 (0)