|
@@ -30,8 +30,10 @@ import org.apache.zookeeper.WatchedEvent;
|
|
|
import org.apache.zookeeper.Watcher.Event.KeeperState;
|
|
|
import org.apache.zookeeper.ZooDefs.Perms;
|
|
|
import org.apache.zookeeper.ZooKeeper;
|
|
|
+import org.apache.zookeeper.client.ZKClientConfig;
|
|
|
import org.apache.zookeeper.data.ACL;
|
|
|
import org.apache.zookeeper.data.Id;
|
|
|
+import org.apache.zookeeper.server.ZooKeeperServer;
|
|
|
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
|
|
|
import org.junit.jupiter.api.AfterAll;
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
@@ -42,12 +44,14 @@ public class SaslSuperUserTest extends ClientBase {
|
|
|
private static Id otherSaslUser = new Id("sasl", "joe");
|
|
|
private static Id otherDigestUser;
|
|
|
private static String oldAuthProvider;
|
|
|
+ private static String oldClientConfigSection;
|
|
|
private static String oldLoginConfig;
|
|
|
private static String oldSuperUser;
|
|
|
|
|
|
@BeforeAll
|
|
|
public static void setupStatic() throws Exception {
|
|
|
oldAuthProvider = System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
|
|
|
+ oldClientConfigSection = System.getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY);
|
|
|
|
|
|
File tmpDir = createTmpDir();
|
|
|
File saslConfFile = new File(tmpDir, "jaas.conf");
|
|
@@ -56,42 +60,40 @@ public class SaslSuperUserTest extends ClientBase {
|
|
|
fwriter.write(""
|
|
|
+ "Server {\n"
|
|
|
+ " org.apache.zookeeper.server.auth.DigestLoginModule required\n"
|
|
|
- + " user_super_duper=\"test\";\n"
|
|
|
+ + " user_super_duper=\"test\"\n"
|
|
|
+ + " user_other_super=\"test\";\n"
|
|
|
+ "};\n"
|
|
|
+ "Client {\n"
|
|
|
+ " org.apache.zookeeper.server.auth.DigestLoginModule required\n"
|
|
|
+ " username=\"super_duper\"\n"
|
|
|
+ " password=\"test\";\n"
|
|
|
+ "};"
|
|
|
+ + "OtherClient {\n"
|
|
|
+ + " org.apache.zookeeper.server.auth.DigestLoginModule required\n"
|
|
|
+ + " username=\"other_super\"\n"
|
|
|
+ + " password=\"test\";\n"
|
|
|
+ + "};"
|
|
|
+ "\n");
|
|
|
fwriter.close();
|
|
|
oldLoginConfig = System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath());
|
|
|
- oldSuperUser = System.setProperty("zookeeper.superUser", "super_duper");
|
|
|
+ oldSuperUser = System.setProperty(ZooKeeperServer.SASL_SUPER_USER, "super_duper");
|
|
|
otherDigestUser = new Id("digest", DigestAuthenticationProvider.generateDigest("jack:jack"));
|
|
|
}
|
|
|
|
|
|
@AfterAll
|
|
|
public static void cleanupStatic() {
|
|
|
- if (oldAuthProvider != null) {
|
|
|
- System.setProperty("zookeeper.authProvider.1", oldAuthProvider);
|
|
|
- } else {
|
|
|
- System.clearProperty("zookeeper.authProvider.1");
|
|
|
- }
|
|
|
- oldAuthProvider = null;
|
|
|
-
|
|
|
- if (oldLoginConfig != null) {
|
|
|
- System.setProperty("java.security.auth.login.config", oldLoginConfig);
|
|
|
- } else {
|
|
|
- System.clearProperty("java.security.auth.login.config");
|
|
|
- }
|
|
|
- oldLoginConfig = null;
|
|
|
+ restoreProperty("zookeeper.authProvider.1", oldAuthProvider);
|
|
|
+ restoreProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, oldClientConfigSection);
|
|
|
+ restoreProperty("java.security.auth.login.config", oldLoginConfig);
|
|
|
+ restoreProperty(ZooKeeperServer.SASL_SUPER_USER, oldSuperUser);
|
|
|
+ }
|
|
|
|
|
|
- if (oldSuperUser != null) {
|
|
|
- System.setProperty("zookeeper.superUser", oldSuperUser);
|
|
|
+ private static void restoreProperty(String property, String oldValue) {
|
|
|
+ if (oldValue != null) {
|
|
|
+ System.setProperty(property, oldValue);
|
|
|
} else {
|
|
|
- System.clearProperty("zookeeper.superUser");
|
|
|
+ System.clearProperty(property);
|
|
|
}
|
|
|
- oldSuperUser = null;
|
|
|
}
|
|
|
|
|
|
private AtomicInteger authFailed = new AtomicInteger(0);
|
|
@@ -115,8 +117,7 @@ public class SaslSuperUserTest extends ClientBase {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void testSuperIsSuper() throws Exception {
|
|
|
+ private void connectAndPerformSuperOps() throws Exception {
|
|
|
ZooKeeper zk = createClient();
|
|
|
try {
|
|
|
zk.create("/digest_read", null, Arrays.asList(new ACL(Perms.READ, otherDigestUser)), CreateMode.PERSISTENT);
|
|
@@ -127,11 +128,35 @@ public class SaslSuperUserTest extends ClientBase {
|
|
|
zk.delete("/digest_read", -1);
|
|
|
zk.delete("/sasl_read/sub", -1);
|
|
|
zk.delete("/sasl_read", -1);
|
|
|
- //If the test failes it will most likely fail with a NoAuth exception before it ever gets to this assertion
|
|
|
- assertEquals(authFailed.get(), 0);
|
|
|
} finally {
|
|
|
zk.close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testSuperIsSuper() throws Exception {
|
|
|
+ connectAndPerformSuperOps();
|
|
|
+ //If the test fails it will most likely fail with a NoAuth exception before it ever gets to this assertion
|
|
|
+ assertEquals(authFailed.get(), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testOtherSuperIsSuper() throws Exception {
|
|
|
+ String prevSection = System.setProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, "OtherClient");
|
|
|
+
|
|
|
+ // KLUDGE: We do this quite late, as the server has been
|
|
|
+ // started at this point--but the implementation currently
|
|
|
+ // looks at the properties each time a SASL negotiation completes.
|
|
|
+ String superUser1Prop = ZooKeeperServer.SASL_SUPER_USER + ".1";
|
|
|
+ String prevSuperUser1 = System.setProperty(superUser1Prop, "other_super");
|
|
|
+
|
|
|
+ try {
|
|
|
+ connectAndPerformSuperOps();
|
|
|
+ //If the test fails it will most likely fail with a NoAuth exception before it ever gets to this assertion
|
|
|
+ assertEquals(authFailed.get(), 0);
|
|
|
+ } finally {
|
|
|
+ restoreProperty(superUser1Prop, prevSuperUser1);
|
|
|
+ restoreProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, prevSection);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|