Repository: curator
Updated Branches:
  refs/heads/CURATOR-111 [created] 7fc3e6507


Patch v1: Add authorization(List<AuthInfo)


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/71dc640b
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/71dc640b
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/71dc640b

Branch: refs/heads/CURATOR-111
Commit: 71dc640b663057a6acaae1f9af93aea48fee3de9
Parents: de1d38c
Author: Karthik Kambatla <ka...@cloudera.com>
Authored: Mon Dec 29 14:16:55 2014 -0800
Committer: Karthik Kambatla <ka...@cloudera.com>
Committed: Mon Dec 29 14:16:55 2014 -0800

----------------------------------------------------------------------
 .../org/apache/curator/framework/AuthInfo.java  | 51 ++++++++++++++++++++
 .../framework/CuratorFrameworkFactory.java      | 23 +++++++++
 .../framework/imps/CuratorFrameworkImpl.java    | 40 ++++++---------
 .../curator/framework/imps/TestFramework.java   | 45 +++++++++++++++++
 4 files changed, 133 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/71dc640b/curator-framework/src/main/java/org/apache/curator/framework/AuthInfo.java
----------------------------------------------------------------------
diff --git 
a/curator-framework/src/main/java/org/apache/curator/framework/AuthInfo.java 
b/curator-framework/src/main/java/org/apache/curator/framework/AuthInfo.java
new file mode 100644
index 0000000..2f879c5
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/AuthInfo.java
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.curator.framework;
+
+import java.util.Arrays;
+
+public class AuthInfo
+{
+    final String    scheme;
+    final byte[]    auth;
+
+    public AuthInfo(String scheme, byte[] auth)
+    {
+        this.scheme = scheme;
+        this.auth = auth;
+    }
+
+    public String getScheme() {
+        return scheme;
+    }
+
+    public byte[] getAuth() {
+        return auth;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "AuthInfo{" +
+            "scheme='" + scheme + '\'' +
+            ", auth=" + Arrays.toString(auth) +
+            '}';
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/71dc640b/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 2d21fb7..4d695e4 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
@@ -34,7 +34,9 @@ import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
@@ -106,6 +108,7 @@ public class CuratorFrameworkFactory
         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;
         private ZookeeperFactory    zookeeperFactory = 
DEFAULT_ZOOKEEPER_FACTORY;
@@ -165,6 +168,21 @@ public class CuratorFrameworkFactory
         }
 
         /**
+         * Add connection authorization. The supplied authInfos are appended 
to those added via call to
+         * {@link #authorization(java.lang.String, byte[])} for backward 
compatibility.
+         *
+         * Subsequent calls to this method overwrite the prior calls.
+         *
+         * @param authInfos list of {@link AuthInfo} objects with scheme and 
auth
+         * @return this
+         */
+        public Builder authorization(List<AuthInfo> authInfos) {
+            this.authInfos = new ArrayList<AuthInfo>(authInfos.size());
+            this.authInfos.addAll(authInfos);
+            return this;
+        }
+
+        /**
          * Set the list of servers to connect to. IMPORTANT: use either this 
or {@link #ensembleProvider(EnsembleProvider)}
          * but not both.
          *
@@ -356,6 +374,11 @@ public class CuratorFrameworkFactory
             return (authValue != null) ? Arrays.copyOf(authValue, 
authValue.length) : null;
         }
 
+        public List<AuthInfo> getAuthInfos()
+        {
+            return authInfos;
+        }
+
         public byte[] getDefaultData()
         {
             return defaultData;

http://git-wip-us.apache.org/repos/asf/curator/blob/71dc640b/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 c4b1349..f7b08a6 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
@@ -24,6 +24,7 @@ import org.apache.curator.CuratorConnectionLossException;
 import org.apache.curator.CuratorZookeeperClient;
 import org.apache.curator.RetryLoop;
 import org.apache.curator.TimeTrace;
+import org.apache.curator.framework.AuthInfo;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.framework.api.*;
@@ -43,7 +44,10 @@ import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.Callable;
 import java.util.concurrent.DelayQueue;
@@ -65,7 +69,7 @@ public class CuratorFrameworkImpl implements CuratorFramework
     private final BlockingQueue<OperationAndData<?>>                    
backgroundOperations;
     private final NamespaceImpl                                         
namespace;
     private final ConnectionStateManager                                
connectionStateManager;
-    private final AtomicReference<AuthInfo>                             
authInfo = new AtomicReference<AuthInfo>();
+    private final List<AuthInfo>                                        
authInfos;
     private final byte[]                                                
defaultData;
     private final FailedDeleteManager                                   
failedDeleteManager;
     private final CompressionProvider                                   
compressionProvider;
@@ -86,27 +90,6 @@ public class CuratorFrameworkImpl implements CuratorFramework
 
     private final AtomicReference<CuratorFrameworkState>                    
state;
 
-    private static class AuthInfo
-    {
-        final String    scheme;
-        final byte[]    auth;
-
-        private AuthInfo(String scheme, byte[] auth)
-        {
-            this.scheme = scheme;
-            this.auth = auth;
-        }
-
-        @Override
-        public String toString()
-        {
-            return "AuthInfo{" +
-                "scheme='" + scheme + '\'' +
-                ", auth=" + Arrays.toString(auth) +
-                '}';
-        }
-    }
-
     public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder)
     {
         ZookeeperFactory localZookeeperFactory = 
makeZookeeperFactory(builder.getZookeeperFactory());
@@ -155,9 +138,14 @@ public class CuratorFrameworkImpl implements 
CuratorFramework
         byte[]      builderDefaultData = builder.getDefaultData();
         defaultData = (builderDefaultData != null) ? 
Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0];
 
+        authInfos = new ArrayList<AuthInfo>();
         if ( builder.getAuthScheme() != null )
         {
-            authInfo.set(new AuthInfo(builder.getAuthScheme(), 
builder.getAuthValue()));
+            authInfos.add(new AuthInfo(builder.getAuthScheme(), 
builder.getAuthValue()));
+        }
+        if ( builder.getAuthInfos() != null )
+        {
+            authInfos.addAll(builder.getAuthInfos());
         }
 
         failedDeleteManager = new FailedDeleteManager(this);
@@ -172,10 +160,9 @@ public class CuratorFrameworkImpl implements 
CuratorFramework
             public ZooKeeper newZooKeeper(String connectString, int 
sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception
             {
                 ZooKeeper zooKeeper = 
actualZookeeperFactory.newZooKeeper(connectString, sessionTimeout, watcher, 
canBeReadOnly);
-                AuthInfo auth = authInfo.get();
-                if ( auth != null )
+                for (AuthInfo auth : authInfos)
                 {
-                    zooKeeper.addAuthInfo(auth.scheme, auth.auth);
+                    zooKeeper.addAuthInfo(auth.getScheme(), auth.getAuth());
                 }
 
                 return zooKeeper;
@@ -208,6 +195,7 @@ public class CuratorFrameworkImpl implements 
CuratorFramework
         namespaceFacadeCache = parent.namespaceFacadeCache;
         namespace = new NamespaceImpl(this, null);
         state = parent.state;
+        authInfos = parent.authInfos;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/curator/blob/71dc640b/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 93d346b..0f51b25 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.test.BaseClassForTests;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.curator.framework.CuratorFramework;
@@ -42,6 +43,8 @@ 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;
 import java.util.concurrent.CountDownLatch;
@@ -169,10 +172,16 @@ public class TestFramework extends BaseClassForTests
     @Test
     public void     testCreateACL() 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)
             .retryPolicy(new RetryOneTime(1))
             .build();
         client.start();
@@ -183,6 +192,7 @@ public class TestFramework extends BaseClassForTests
             client.create().withACL(aclList).forPath("/test", 
"test".getBytes());
             client.close();
 
+            // Try setting data with me:pass
             client = builder
                 .connectString(server.getConnectString())
                 .authorization("digest", "me:pass".getBytes())
@@ -199,6 +209,41 @@ public class TestFramework extends BaseClassForTests
             }
             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 me2:pass2
+            client = builder
+                    .connectString(server.getConnectString())
+                    .authorization("digest", "me:pass2".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())

Reply via email to