use of org.apache.zookeeper.data.ACL in project cdap by caskdata.
the class SharedResourceCacheTest method testCache.
@Test
public void testCache() throws Exception {
String parentZNode = ZK_NAMESPACE + "/testCache";
List<ACL> acls = Lists.newArrayList(ZooDefs.Ids.OPEN_ACL_UNSAFE);
// create 2 cache instances
ZKClientService zkClient1 = injector1.getInstance(ZKClientService.class);
zkClient1.startAndWait();
SharedResourceCache<String> cache1 = new SharedResourceCache<>(zkClient1, new StringCodec(), parentZNode, acls);
cache1.init();
// add items to one and wait for them to show up in the second
String key1 = "key1";
String value1 = "value1";
cache1.put(key1, value1);
ZKClientService zkClient2 = injector2.getInstance(ZKClientService.class);
zkClient2.startAndWait();
SharedResourceCache<String> cache2 = new SharedResourceCache<>(zkClient2, new StringCodec(), parentZNode, acls);
cache2.init();
waitForEntry(cache2, key1, value1, 10000);
assertEquals(cache1.get(key1), cache2.get(key1));
final String key2 = "key2";
String value2 = "value2";
cache1.put(key2, value2);
waitForEntry(cache2, key2, value2, 10000);
assertEquals(cache1.get(key2), cache2.get(key2));
final String key3 = "key3";
String value3 = "value3";
cache2.put(key3, value3);
waitForEntry(cache1, key3, value3, 10000);
assertEquals(cache2.get(key3), cache1.get(key3));
// replace an existing key
final String value2new = "value2.2";
final SettableFuture<String> value2future = SettableFuture.create();
ResourceListener<String> value2listener = new BaseResourceListener<String>() {
@Override
public void onResourceUpdate(String name, String instance) {
LOG.info("Resource updated: {}={}", name, instance);
if (key2.equals(name) && value2new.equals(instance)) {
value2future.set(instance);
}
}
};
cache2.addListener(value2listener);
cache1.put(key2, value2new);
assertEquals(value2new, value2future.get(10, TimeUnit.SECONDS));
assertEquals(value2new, cache2.get(key2));
cache2.removeListener(value2listener);
// remove items from the second and wait for them to disappear from the first
// Use a latch to make sure both cache see the changes
final CountDownLatch key3RemoveLatch = new CountDownLatch(2);
cache1.addListener(new BaseResourceListener<String>() {
@Override
public void onResourceDelete(String name) {
LOG.info("Resource deleted on cache 1 {}", name);
if (name.equals(key3)) {
key3RemoveLatch.countDown();
}
}
});
final SettableFuture<String> key3RemoveFuture = SettableFuture.create();
ResourceListener<String> key3Listener = new BaseResourceListener<String>() {
@Override
public void onResourceDelete(String name) {
LOG.info("Resource deleted on cache 2 {}", name);
if (name.equals(key3)) {
key3RemoveFuture.set(name);
key3RemoveLatch.countDown();
}
}
};
cache2.addListener(key3Listener);
cache1.remove(key3);
String removedKey = key3RemoveFuture.get();
assertEquals(key3, removedKey);
assertNull(cache2.get(key3));
key3RemoveLatch.await(5, TimeUnit.SECONDS);
// verify that cache contents are equal
assertEquals(cache1, cache2);
}
use of org.apache.zookeeper.data.ACL in project zookeeper by apache.
the class SaslAuthTest method testInvalidSaslIds.
@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++;
}
}
}
use of org.apache.zookeeper.data.ACL in project zookeeper by apache.
the class SaslAuthTest method testValidSaslIds.
@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++;
}
}
use of org.apache.zookeeper.data.ACL in project zookeeper by apache.
the class AclParser method parse.
/**
* parse string into list of ACL
* @param aclString
* @return
*/
public static List<ACL> parse(String aclString) {
List<ACL> acl;
String[] acls = aclString.split(",");
acl = new ArrayList<ACL>();
for (String a : acls) {
int firstColon = a.indexOf(':');
int lastColon = a.lastIndexOf(':');
if (firstColon == -1 || lastColon == -1 || firstColon == lastColon) {
System.err.println(a + " does not have the form scheme:id:perm");
continue;
}
ACL newAcl = new ACL();
newAcl.setId(new Id(a.substring(0, firstColon), a.substring(firstColon + 1, lastColon)));
newAcl.setPerms(getPermFromString(a.substring(lastColon + 1)));
acl.add(newAcl);
}
return acl;
}
use of org.apache.zookeeper.data.ACL in project zookeeper by apache.
the class GetAclCommand method exec.
@Override
public boolean exec() throws CliException {
String path = args[1];
Stat stat = new Stat();
List<ACL> acl;
try {
acl = zk.getACL(path, stat);
} catch (IllegalArgumentException ex) {
throw new MalformedPathException(ex.getMessage());
} catch (KeeperException | InterruptedException ex) {
throw new CliWrapperException(ex);
}
for (ACL a : acl) {
out.println(a.getId() + ": " + getPermString(a.getPerms()));
}
if (cl.hasOption("s")) {
new StatPrinter(out).print(stat);
}
return false;
}
Aggregations