|
@@ -18,8 +18,12 @@
|
|
|
|
|
|
package org.apache.zookeeper.test;
|
|
package org.apache.zookeeper.test;
|
|
|
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
import java.util.concurrent.CountDownLatch;
|
|
import java.util.concurrent.CountDownLatch;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
import org.apache.zookeeper.CreateMode;
|
|
import org.apache.zookeeper.CreateMode;
|
|
@@ -27,18 +31,32 @@ import org.apache.zookeeper.KeeperException;
|
|
import org.apache.zookeeper.TestableZooKeeper;
|
|
import org.apache.zookeeper.TestableZooKeeper;
|
|
import org.apache.zookeeper.WatchedEvent;
|
|
import org.apache.zookeeper.WatchedEvent;
|
|
import org.apache.zookeeper.Watcher.Event.KeeperState;
|
|
import org.apache.zookeeper.Watcher.Event.KeeperState;
|
|
|
|
+import org.apache.zookeeper.ZooDefs;
|
|
import org.apache.zookeeper.ZooDefs.Ids;
|
|
import org.apache.zookeeper.ZooDefs.Ids;
|
|
import org.apache.zookeeper.ZooKeeper;
|
|
import org.apache.zookeeper.ZooKeeper;
|
|
|
|
+import org.apache.zookeeper.data.ACL;
|
|
|
|
+import org.apache.zookeeper.data.Id;
|
|
|
|
+import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
|
|
|
|
+import org.junit.jupiter.api.AfterAll;
|
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
public class AuthTest extends ClientBase {
|
|
public class AuthTest extends ClientBase {
|
|
|
|
|
|
- static {
|
|
|
|
|
|
+ @BeforeAll
|
|
|
|
+ public static void setup() {
|
|
// password is test
|
|
// password is test
|
|
|
|
+ // the default digestAlg is: SHA1
|
|
System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU=");
|
|
System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU=");
|
|
System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.test.InvalidAuthProvider");
|
|
System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.test.InvalidAuthProvider");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @AfterAll
|
|
|
|
+ public static void teardown() {
|
|
|
|
+ System.clearProperty("zookeeper.DigestAuthenticationProvider.superDigest");
|
|
|
|
+ System.clearProperty(DigestAuthenticationProvider.DIGEST_ALGORITHM_KEY);
|
|
|
|
+ }
|
|
|
|
+
|
|
private final CountDownLatch authFailed = new CountDownLatch(1);
|
|
private final CountDownLatch authFailed = new CountDownLatch(1);
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -160,4 +178,97 @@ public class AuthTest extends ClientBase {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void testOrdinaryACL() throws Exception {
|
|
|
|
+ ZooKeeper zk = createClient();
|
|
|
|
+ try {
|
|
|
|
+ String path = "/path1";
|
|
|
|
+ zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
|
|
|
|
+ zk.addAuthInfo("digest", "username1:password1".getBytes());
|
|
|
|
+ List<ACL> list = new ArrayList<>();
|
|
|
|
+ int perm = ZooDefs.Perms.ALL;
|
|
|
|
+ String userPassword = "username1:password1";
|
|
|
|
+ Id id = new Id("auth", userPassword);
|
|
|
|
+ list.add(new ACL(perm, id));
|
|
|
|
+ zk.setACL(path, list, -1);
|
|
|
|
+ zk.close();
|
|
|
|
+
|
|
|
|
+ zk = createClient();
|
|
|
|
+ zk.addAuthInfo("digest", "super:test".getBytes());
|
|
|
|
+ zk.getData(path, false, null);
|
|
|
|
+ zk.close();
|
|
|
|
+
|
|
|
|
+ zk = createClient();
|
|
|
|
+ try {
|
|
|
|
+ zk.getData(path, false, null);
|
|
|
|
+ fail("should have NoAuthException");
|
|
|
|
+ } catch (KeeperException.NoAuthException e) {
|
|
|
|
+ // expected
|
|
|
|
+ }
|
|
|
|
+ zk.addAuthInfo("digest", "username1:password1".getBytes());
|
|
|
|
+ zk.getData(path, false, null);
|
|
|
|
+ } finally {
|
|
|
|
+ zk.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testGenerateDigest() throws NoSuchAlgorithmException {
|
|
|
|
+ assertEquals("super:D/InIHSb7yEEbrWz8b9l71RjZJU=", DigestAuthenticationProvider.generateDigest("super:test"));
|
|
|
|
+ assertEquals("super:yyuhPKumRtNj4r8GnSbbwuq1vhE=", DigestAuthenticationProvider.generateDigest("super:zookeeper"));
|
|
|
|
+ assertEquals("super:t6lQTvqID/Gl5Or0n4FYE6kKP8w=", DigestAuthenticationProvider.generateDigest(("super:foo")));
|
|
|
|
+ assertEquals("super:hTdNN4QH4isoRvCrQ1Jf7REREQ4=", DigestAuthenticationProvider.generateDigest(("super:bar")));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // This test is used to check the correctness of the algorithm
|
|
|
|
+ // For the same digest algorithm and input, the output of digest hash is the constant.
|
|
|
|
+ @Test
|
|
|
|
+ public void testDigest() throws NoSuchAlgorithmException {
|
|
|
|
+ assertEquals("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", getGeneratedDigestStr(DigestAuthenticationProvider.digest("test")));
|
|
|
|
+ assertEquals("8a0444ded963cf1118dd34aa1acaafec268c654d", getGeneratedDigestStr(DigestAuthenticationProvider.digest("zookeeper")));
|
|
|
|
+ assertEquals("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", getGeneratedDigestStr(DigestAuthenticationProvider.digest(("foo"))));
|
|
|
|
+ assertEquals("62cdb7020ff920e5aa642c3d4066950dd1f01f4d", getGeneratedDigestStr(DigestAuthenticationProvider.digest(("bar"))));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // this method is used to generate the digest String to help us to compare the result generated by some online tool easily
|
|
|
|
+ protected static String getGeneratedDigestStr(byte[] bytes) {
|
|
|
|
+ StringBuilder stringBuilder = new StringBuilder("");
|
|
|
|
+ if (bytes == null || bytes.length <= 0) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ for (int i = 0; i < bytes.length; i++) {
|
|
|
|
+ int v = bytes[i] & 0xFF;
|
|
|
|
+ String hv = Integer.toHexString(v);
|
|
|
|
+ if (hv.length() < 2) {
|
|
|
|
+ stringBuilder.append(0);
|
|
|
|
+ }
|
|
|
|
+ stringBuilder.append(hv);
|
|
|
|
+ }
|
|
|
|
+ return stringBuilder.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public enum DigestAlgEnum {
|
|
|
|
+ SHA_1("SHA1"),
|
|
|
|
+ SHA_256("SHA-256"),
|
|
|
|
+ SHA3_256("SHA3-256");
|
|
|
|
+
|
|
|
|
+ private String name;
|
|
|
|
+
|
|
|
|
+ DigestAlgEnum(String name) {
|
|
|
|
+ this.name = name;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getName() {
|
|
|
|
+ return this.name;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<String> getValues() {
|
|
|
|
+ List<String> digestList = new ArrayList<>();
|
|
|
|
+ for (DigestAlgEnum digest : values()) {
|
|
|
|
+ digestList.add(digest.getName());
|
|
|
|
+ }
|
|
|
|
+ return digestList;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|