Search in sources :

Example 41 with ACL

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);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ACL(org.apache.zookeeper.data.ACL) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 42 with ACL

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);
    }
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ACL(org.apache.zookeeper.data.ACL) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 43 with ACL

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
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) NoAuthException(org.apache.zookeeper.KeeperException.NoAuthException) ACL(org.apache.zookeeper.data.ACL) Test(org.testng.annotations.Test)

Example 44 with ACL

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;
}
Also used : ACL(org.apache.zookeeper.data.ACL) List(java.util.List) OperationTrace(org.apache.curator.drivers.OperationTrace)

Example 45 with ACL

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);
    }
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ACL(org.apache.zookeeper.data.ACL) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) Id(org.apache.zookeeper.data.Id) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.testng.annotations.Test)

Aggregations

ACL (org.apache.zookeeper.data.ACL)214 Id (org.apache.zookeeper.data.Id)83 ArrayList (java.util.ArrayList)58 Test (org.junit.Test)58 Stat (org.apache.zookeeper.data.Stat)53 KeeperException (org.apache.zookeeper.KeeperException)35 Test (org.testng.annotations.Test)32 CuratorFramework (org.apache.curator.framework.CuratorFramework)19 Test (org.junit.jupiter.api.Test)18 Configuration (org.apache.hadoop.conf.Configuration)17 ZooKeeper (org.apache.zookeeper.ZooKeeper)16 ACLProvider (org.apache.curator.framework.api.ACLProvider)15 List (java.util.List)11 IOException (java.io.IOException)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6 CreateMode (org.apache.zookeeper.CreateMode)6