dlmarion commented on code in PR #3118: URL: https://github.com/apache/accumulo/pull/3118#discussion_r1053628614
########## core/src/main/java/org/apache/accumulo/core/fate/zookeeper/ZooAclUtil.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 + * + * https://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.accumulo.core.fate.zookeeper; + +import static java.util.Objects.requireNonNull; + +import java.util.List; + +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZooAclUtil { + + private static final Logger log = LoggerFactory.getLogger(ZooAclUtil.class); + + /** + * Utility class - prevent instantiation. + */ + private ZooAclUtil() {} + + public static String extractAuthName(ACL acl) { + requireNonNull(acl, "provided ACL cannot be null"); + try { + return acl.getId().getId().trim().split(":")[0]; + } catch (Exception ex) { + log.debug("Invalid ACL passed, cannot parse id from '{}'", acl); + return ""; + } + } + + /** + * translate the ZooKeeper ACL perm bits into a string. The output order is cdrwa (when all perms + * are set) + * + * Mirrors `org.apache.zookeeper.ZKUtil.getPermString()` added in more recent ZooKeeper versions. + */ + public static String translateZooPerm(final int perm) { + if (perm == ZooDefs.Perms.ALL) { + return "cdrwa"; + } + StringBuilder sb = new StringBuilder(); + if ((perm & ZooDefs.Perms.CREATE) != 0) { + sb.append("c"); + } + if ((perm & ZooDefs.Perms.DELETE) != 0) { + sb.append("d"); + } + if ((perm & ZooDefs.Perms.READ) != 0) { + sb.append("r"); + } + if ((perm & ZooDefs.Perms.WRITE) != 0) { + sb.append("w"); + } + if ((perm & ZooDefs.Perms.ADMIN) != 0) { + sb.append("a"); + } + if (sb.length() == 0) { + return "invalid"; + } + return sb.toString(); + } + + /** + * Process the ZooKeeper acls and return a structure that shows if the accumulo user has ALL + * (cdrwa) access and if any other user has anything other than read access. + * <ul> + * <li>Accumulo having access is expected - anything else needs checked + * <li>Anyone having more than read access is unexpected and should be checked. + * </ul> + * + * @param acls ZooKeeper ACLs for a node + * @return acl status with accumulo and other accesses. + */ + public static ZkAccumuloAclStatus checkWritableAuth(List<ACL> acls) { + + ZkAccumuloAclStatus result = new ZkAccumuloAclStatus(); + + for (ACL a : acls) { + String name = extractAuthName(a); + // check accumulo has or inherits all access + if ("accumulo".equals(name) || "anyone".equals(name)) { + if (a.getPerms() == ZooDefs.Perms.ALL) { + result.setAccumuloHasFull(); + } + } + // check if not accumulo and any perm bit other than read is set + if (!"accumulo".equals(name) && ((a.getPerms() & ~ZooDefs.Perms.READ) != 0)) { + result.setAnyMayUpdate(); + } + // check if not accumulo and any read perm is set + if (!"accumulo".equals(name) && ((a.getPerms() & ZooDefs.Perms.READ) != 0)) { Review Comment: wouldn't you want to check that "anyone" matches name? There could be another user that has an ACL on this node. ########## core/src/test/java/org/apache/accumulo/core/fate/zookeeper/ZooAclUtilTest.java: ########## @@ -0,0 +1,87 @@ +/* + * 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 + * + * https://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.accumulo.core.fate.zookeeper; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; + +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class ZooAclUtilTest { + + private static final Logger log = LoggerFactory.getLogger(ZooAclUtilTest.class); + + @Test + public void extractAuthNameTest() { + assertThrows(NullPointerException.class, () -> ZooAclUtil.extractAuthName(null)); + assertEquals("", + ZooAclUtil.extractAuthName(new ACL(ZooDefs.Perms.ALL, new Id("digest", null)))); + assertEquals("accumulo", ZooAclUtil + .extractAuthName(new ACL(ZooDefs.Perms.ALL, new Id("digest", "accumulo:abcd123")))); + assertEquals("noauth", + ZooAclUtil.extractAuthName(new ACL(ZooDefs.Perms.ALL, new Id("digest", "noauth")))); + assertEquals("noscheme", + ZooAclUtil.extractAuthName(new ACL(ZooDefs.Perms.ALL, new Id("", "noscheme")))); + assertEquals("", ZooAclUtil.extractAuthName(new ACL(ZooDefs.Perms.ALL, new Id("digest", "")))); + } + + @Test + public void translateZooPermTest() { + assertEquals("invalid", ZooAclUtil.translateZooPerm(0)); + assertEquals("cdrwa", ZooAclUtil.translateZooPerm(ZooDefs.Perms.ALL)); + assertEquals("drw", ZooAclUtil + .translateZooPerm(ZooDefs.Perms.DELETE | ZooDefs.Perms.READ | ZooDefs.Perms.WRITE)); + assertEquals("ca", ZooAclUtil.translateZooPerm(ZooDefs.Perms.CREATE | ZooDefs.Perms.ADMIN)); + } + + @Test + public void checkWritableAuthTest() { + ACL a1 = new ACL(ZooDefs.Perms.ALL, new Id("digest", "accumulo:abcd123")); + ZooAclUtil.ZkAccumuloAclStatus status = ZooAclUtil.checkWritableAuth(List.of(a1)); + assertTrue(status.accumuloHasFull()); + assertFalse(status.anyMayUpdate()); + assertFalse(status.anyCanRead()); + + ACL a2 = new ACL(ZooDefs.Perms.ALL, new Id("digest", "someone:abcd123")); + status = ZooAclUtil.checkWritableAuth(List.of(a2)); + assertFalse(status.accumuloHasFull()); + assertTrue(status.anyMayUpdate()); Review Comment: `anyone` can't update this znode, only `someone` can, right? ########## core/src/main/java/org/apache/accumulo/core/fate/zookeeper/ZooAclUtil.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 + * + * https://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.accumulo.core.fate.zookeeper; + +import static java.util.Objects.requireNonNull; + +import java.util.List; + +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZooAclUtil { + + private static final Logger log = LoggerFactory.getLogger(ZooAclUtil.class); + + /** + * Utility class - prevent instantiation. + */ + private ZooAclUtil() {} + + public static String extractAuthName(ACL acl) { + requireNonNull(acl, "provided ACL cannot be null"); + try { + return acl.getId().getId().trim().split(":")[0]; + } catch (Exception ex) { + log.debug("Invalid ACL passed, cannot parse id from '{}'", acl); + return ""; + } + } + + /** + * translate the ZooKeeper ACL perm bits into a string. The output order is cdrwa (when all perms + * are set) + * + * Mirrors `org.apache.zookeeper.ZKUtil.getPermString()` added in more recent ZooKeeper versions. + */ + public static String translateZooPerm(final int perm) { + if (perm == ZooDefs.Perms.ALL) { + return "cdrwa"; + } + StringBuilder sb = new StringBuilder(); + if ((perm & ZooDefs.Perms.CREATE) != 0) { + sb.append("c"); + } + if ((perm & ZooDefs.Perms.DELETE) != 0) { + sb.append("d"); + } + if ((perm & ZooDefs.Perms.READ) != 0) { + sb.append("r"); + } + if ((perm & ZooDefs.Perms.WRITE) != 0) { + sb.append("w"); + } + if ((perm & ZooDefs.Perms.ADMIN) != 0) { + sb.append("a"); + } + if (sb.length() == 0) { + return "invalid"; + } + return sb.toString(); + } + + /** + * Process the ZooKeeper acls and return a structure that shows if the accumulo user has ALL + * (cdrwa) access and if any other user has anything other than read access. + * <ul> + * <li>Accumulo having access is expected - anything else needs checked + * <li>Anyone having more than read access is unexpected and should be checked. + * </ul> + * + * @param acls ZooKeeper ACLs for a node + * @return acl status with accumulo and other accesses. + */ + public static ZkAccumuloAclStatus checkWritableAuth(List<ACL> acls) { + + ZkAccumuloAclStatus result = new ZkAccumuloAclStatus(); + + for (ACL a : acls) { + String name = extractAuthName(a); + // check accumulo has or inherits all access + if ("accumulo".equals(name) || "anyone".equals(name)) { + if (a.getPerms() == ZooDefs.Perms.ALL) { + result.setAccumuloHasFull(); + } + } + // check if not accumulo and any perm bit other than read is set + if (!"accumulo".equals(name) && ((a.getPerms() & ~ZooDefs.Perms.READ) != 0)) { Review Comment: wouldn't you want to check that "anyone" matches `name`? There could be another user that has an ACL on this node. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
