CURATOR-111 - Modified the authorization handling so that the underlying auth data is stored in a single list. Modified tests to have both a single auth and multiple auth case.
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/58328915 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/58328915 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/58328915 Branch: refs/heads/master Commit: 58328915a6e005bc4171bb866cb2eba4762c3718 Parents: 7fc3e65 Author: Cameron McKenzie <came...@unico.com.au> Authored: Tue Jan 13 16:42:59 2015 +1100 Committer: Cameron McKenzie <came...@unico.com.au> Committed: Tue Jan 13 16:42:59 2015 +1100 ---------------------------------------------------------------------- .../framework/CuratorFrameworkFactory.java | 21 ++---- .../framework/imps/CuratorFrameworkImpl.java | 4 -- .../curator/framework/imps/TestFramework.java | 73 +++++++++++++++----- 3 files changed, 64 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/58328915/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java index ae26f2c..314d669 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java @@ -20,6 +20,8 @@ package org.apache.curator.framework; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + import org.apache.curator.RetryPolicy; import org.apache.curator.ensemble.EnsembleProvider; import org.apache.curator.ensemble.fixed.FixedEnsembleProvider; @@ -34,6 +36,7 @@ import org.apache.curator.utils.DefaultZookeeperFactory; import org.apache.curator.utils.ZookeeperFactory; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; + import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; @@ -107,8 +110,6 @@ public class CuratorFrameworkFactory private RetryPolicy retryPolicy; private ThreadFactory threadFactory = null; private String namespace; - private String authScheme = null; - private byte[] authValue = null; private List<AuthInfo> authInfos = null; private byte[] defaultData = LOCAL_ADDRESS; private CompressionProvider compressionProvider = DEFAULT_COMPRESSION_PROVIDER; @@ -156,6 +157,8 @@ public class CuratorFrameworkFactory /** * Add connection authorization + * + * Subsequent calls to this method overwrite the prior calls. * * @param scheme the scheme * @param auth the auth bytes @@ -163,8 +166,8 @@ public class CuratorFrameworkFactory */ public Builder authorization(String scheme, byte[] auth) { - this.authScheme = scheme; - this.authValue = (auth != null) ? Arrays.copyOf(auth, auth.length) : null; + this.authInfos = Lists.newArrayList(); + this.authInfos.add(new AuthInfo(scheme, (auth != null) ? Arrays.copyOf(auth, auth.length) : null)); return this; } @@ -380,16 +383,6 @@ public class CuratorFrameworkFactory return namespace; } - public String getAuthScheme() - { - return authScheme; - } - - public byte[] getAuthValue() - { - return (authValue != null) ? Arrays.copyOf(authValue, authValue.length) : null; - } - public List<AuthInfo> getAuthInfos() { return authInfos; http://git-wip-us.apache.org/repos/asf/curator/blob/58328915/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java index e8785be..d4d902a 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java @@ -129,10 +129,6 @@ public class CuratorFrameworkImpl implements CuratorFramework private List<AuthInfo> buildAuths(CuratorFrameworkFactory.Builder builder) { ImmutableList.Builder<AuthInfo> builder1 = ImmutableList.builder(); - if ( builder.getAuthScheme() != null ) - { - builder1.add(new AuthInfo(builder.getAuthScheme(), builder.getAuthValue())); - } if ( builder.getAuthInfos() != null ) { builder1.addAll(builder.getAuthInfos()); http://git-wip-us.apache.org/repos/asf/curator/blob/58328915/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java index c98dd0f..6646a57 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java @@ -19,6 +19,7 @@ package org.apache.curator.framework.imps; import com.google.common.collect.Lists; + import org.apache.curator.framework.AuthInfo; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; @@ -42,6 +43,7 @@ import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Stat; import org.testng.Assert; import org.testng.annotations.Test; + import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; @@ -168,18 +170,12 @@ public class TestFramework extends BaseClassForTests } @Test - public void testCreateACL() throws Exception + public void testCreateACLSingleAuth() throws Exception { - // Add a few authInfos - List<AuthInfo> authInfos = new ArrayList<AuthInfo>(); - authInfos.add(new AuthInfo("digest", "me1:pass1".getBytes())); - authInfos.add(new AuthInfo("digest", "me2:pass2".getBytes())); - CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); CuratorFramework client = builder .connectString(server.getConnectString()) - .authorization("digest", "me:pass".getBytes()) - .authorization(authInfos) + .authorization("digest", "me1:pass1".getBytes()) .retryPolicy(new RetryOneTime(1)) .build(); client.start(); @@ -190,10 +186,10 @@ public class TestFramework extends BaseClassForTests client.create().withACL(aclList).forPath("/test", "test".getBytes()); client.close(); - // Try setting data with me:pass + // Try setting data with me1:pass1 client = builder .connectString(server.getConnectString()) - .authorization("digest", "me:pass".getBytes()) + .authorization("digest", "me1:pass1".getBytes()) .retryPolicy(new RetryOneTime(1)) .build(); client.start(); @@ -207,12 +203,57 @@ public class TestFramework extends BaseClassForTests } 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 + { + client.close(); + } + } + + @Test + public void testCreateACLMultipleAuths() throws Exception + { + // Add a few authInfos + List<AuthInfo> authInfos = new ArrayList<AuthInfo>(); + authInfos.add(new AuthInfo("digest", "me1:pass1".getBytes())); + authInfos.add(new AuthInfo("digest", "me2:pass2".getBytes())); + + CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); + CuratorFramework client = builder + .connectString(server.getConnectString()) + .authorization(authInfos) + .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(); + .connectString(server.getConnectString()) + .authorization("digest", "me1:pass1".getBytes()) + .retryPolicy(new RetryOneTime(1)) + .build(); client.start(); try { @@ -224,10 +265,10 @@ public class TestFramework extends BaseClassForTests } client.close(); - // Try setting data with me2:pass2 + // Try setting data with me1:pass1 client = builder .connectString(server.getConnectString()) - .authorization("digest", "me:pass2".getBytes()) + .authorization("digest", "me2:pass2".getBytes()) .retryPolicy(new RetryOneTime(1)) .build(); client.start();