@Override protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException { MyWatcher watcher = new MyWatcher(); return createClient(watcher, hp); }
@BeforeClass public static void init() { System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); try { File tmpDir = createTmpDir(); File saslConfFile = new File(tmpDir, "jaas.conf"); String jaasContent = getJaasFileContent(); FileWriter fwriter = new FileWriter(saslConfFile); fwriter.write(jaasContent); fwriter.close(); System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); } catch (IOException e) { // could not create tmp directory to hold JAAS conf file : test will // fail now. } }
@Test public void testZKOperationsAfterClientSaslAuthFailure() throws Exception { CountdownWatcher watcher = new CountdownWatcher(); ZooKeeper zk = new ZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); watcher.waitForConnected(CONNECTION_TIMEOUT); try { setSaslFailureFlag(zk); // try node creation for around 15 second, int totalTry = 10; int tryCount = 0; boolean success = false; while (!success && tryCount++ <= totalTry) { try { zk.create("/saslAuthFail", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); success = true; } catch (KeeperException.ConnectionLossException e) { Thread.sleep(1000); // do nothing } } assertTrue("ZNode creation is failing continuously after Sasl auth failure.", success); } finally { zk.close(); } }
@Test public void testAuth() throws Exception { ZooKeeper zk = createClient(); try { zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); Thread.sleep(1000); } finally { zk.close(); } }
@Test public void testValidSaslIds() throws Exception { ZooKeeper zk = createClient(); List<String> validIds = new ArrayList<String>(); validIds.add("user"); validIds.add("service/host.name.com"); validIds.add("user@KERB.REALM"); validIds.add("service/host.name.com@KERB.REALM"); int i = 0; for(String validId: validIds) { List<ACL> aclList = new ArrayList<ACL>(); ACL acl = new ACL(0,new Id("sasl",validId)); aclList.add(acl); zk.create("/valid"+i,null,aclList,CreateMode.PERSISTENT); i++; } }
@Test public void testInvalidSaslIds() throws Exception { ZooKeeper zk = createClient(); List<String> invalidIds = new ArrayList<String>(); invalidIds.add("user@KERB.REALM/server.com"); invalidIds.add("user@KERB.REALM1@KERB.REALM2"); int i = 0; for(String invalidId: invalidIds) { List<ACL> aclList = new ArrayList<ACL>(); try { ACL acl = new ACL(0,new Id("sasl",invalidId)); aclList.add(acl); zk.create("/invalid"+i,null,aclList,CreateMode.PERSISTENT); Assert.fail("SASLAuthenticationProvider.isValid() failed to catch invalid Id."); } catch (KeeperException.InvalidACLException e) { // ok. } finally { i++; } } }