use of org.apache.zookeeper.data.ACL in project xian by happyyangyuan.
the class TestFramework method testCreateACLSingleAuth.
@Test
public void testCreateACLSingleAuth() throws Exception {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework client = builder.connectString(server.getConnectString()).authorization("digest", "me1:pass1".getBytes()).retryPolicy(new RetryOneTime(1)).build();
client.start();
try {
ACL acl = new ACL(ZooDefs.Perms.WRITE, ZooDefs.Ids.AUTH_IDS);
List<ACL> aclList = Lists.newArrayList(acl);
client.create().withACL(aclList).forPath("/test", "test".getBytes());
client.close();
// Try setting data with me1:pass1
client = builder.connectString(server.getConnectString()).authorization("digest", "me1:pass1".getBytes()).retryPolicy(new RetryOneTime(1)).build();
client.start();
try {
client.setData().forPath("/test", "test".getBytes());
} catch (KeeperException.NoAuthException e) {
Assert.fail("Auth failed");
}
client.close();
// Try setting data with something:else
client = builder.connectString(server.getConnectString()).authorization("digest", "something:else".getBytes()).retryPolicy(new RetryOneTime(1)).build();
client.start();
try {
client.setData().forPath("/test", "test".getBytes());
Assert.fail("Should have failed with auth exception");
} catch (KeeperException.NoAuthException e) {
// expected
}
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.zookeeper.data.ACL in project xian by happyyangyuan.
the class TestFrameworkBackground method testErrorListener.
@Test
public void testErrorListener() throws Exception {
ACLProvider badAclProvider = new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
throw new UnsupportedOperationException();
}
@Override
public List<ACL> getAclForPath(String path) {
throw new UnsupportedOperationException();
}
};
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).aclProvider(badAclProvider).build();
try {
client.start();
final CountDownLatch errorLatch = new CountDownLatch(1);
UnhandledErrorListener listener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof UnsupportedOperationException) {
errorLatch.countDown();
}
}
};
client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
Assert.assertTrue(new Timing().awaitLatch(errorLatch));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.zookeeper.data.ACL in project xian by happyyangyuan.
the class TestNamespaceFacade method testACL.
/**
* Test that ACLs work on a NamespaceFacade. See CURATOR-132
* @throws Exception
*/
@Test
public void testACL() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client.start();
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
client.create().creatingParentsIfNeeded().forPath("/parent/child", "A string".getBytes());
CuratorFramework client2 = client.usingNamespace("parent");
Assert.assertNotNull(client2.getData().forPath("/child"));
client.setACL().withACL(Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, ZooDefs.Ids.ANYONE_ID_UNSAFE))).forPath("/parent/child");
// a slash
try {
List<ACL> acls = client2.getACL().forPath("/child");
Assert.assertNotNull(acls);
Assert.assertEquals(acls.size(), 1);
Assert.assertEquals(acls.get(0).getId(), ZooDefs.Ids.ANYONE_ID_UNSAFE);
Assert.assertEquals(acls.get(0).getPerms(), ZooDefs.Perms.WRITE);
client2.setACL().withACL(Collections.singletonList(new ACL(ZooDefs.Perms.DELETE, ZooDefs.Ids.ANYONE_ID_UNSAFE))).forPath("/child");
Assert.fail("Expected auth exception was not thrown");
} catch (NoAuthException e) {
// Expected
}
}
use of org.apache.zookeeper.data.ACL in project xian by happyyangyuan.
the class GetACLBuilderImpl method pathInForeground.
private List<ACL> pathInForeground(final String path) throws Exception {
OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("GetACLBuilderImpl-Foreground");
List<ACL> result = RetryLoop.callWithRetry(client.getZookeeperClient(), new Callable<List<ACL>>() {
@Override
public List<ACL> call() throws Exception {
return client.getZooKeeper().getACL(path, responseStat);
}
});
trace.setPath(path).setStat(responseStat).commit();
return result;
}
use of org.apache.zookeeper.data.ACL in project xian by happyyangyuan.
the class TestLeaderAcls method testAclErrorWithLeader.
@Test(description = "Validation test for CURATOR-365")
public void testAclErrorWithLeader() throws Exception {
ACLProvider provider = new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.OPEN_ACL_UNSAFE;
}
@Override
public List<ACL> getAclForPath(String path) {
if (path.equals("/base")) {
try {
String testDigest = DigestAuthenticationProvider.generateDigest("test:test");
return Collections.singletonList(new ACL(ZooDefs.Perms.ALL, new Id("digest", testDigest)));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return getDefaultAcl();
}
};
RetryPolicy retryPolicy = new ExponentialBackoffRetry(timing.milliseconds(), 3);
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(retryPolicy).aclProvider(provider).authorization("digest", "test:test".getBytes());
CuratorFramework client = builder.build();
LeaderLatch latch = null;
try {
client.start();
latch = new LeaderLatch(client, "/base");
latch.start();
Assert.assertTrue(latch.await(timing.forWaiting().seconds(), TimeUnit.SECONDS));
latch.close();
latch = null;
CuratorFramework noAuthClient = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy);
try {
noAuthClient.start();
final CountDownLatch noAuthLatch = new CountDownLatch(1);
UnhandledErrorListener listener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof KeeperException.NoAuthException) {
noAuthLatch.countDown();
}
}
};
noAuthClient.getUnhandledErrorListenable().addListener(listener);
// use a path below "base" as noAuthClient is not authorized to create nodes in "/base"
// but also making sure that the code goes through the backgroundCreateParentsThenNode() codepath
latch = new LeaderLatch(noAuthClient, "/base/second");
latch.start();
Assert.assertTrue(timing.awaitLatch(noAuthLatch));
} finally {
CloseableUtils.closeQuietly(noAuthClient);
}
} finally {
CloseableUtils.closeQuietly(latch);
CloseableUtils.closeQuietly(client);
}
}
Aggregations