This is an automated email from the ASF dual-hosted git repository.

edcoleman pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new a3cab15546 Use Collections.unmodifiableList for static ACL 
declarations (#3195)
a3cab15546 is described below

commit a3cab15546d79c79ccca0ecf68574d091b6e6b09
Author: EdColeman <[email protected]>
AuthorDate: Fri Mar 3 13:52:51 2023 -0500

    Use Collections.unmodifiableList for static ACL declarations (#3195)
    
    Spotbugs flags leaking a mutable collection used when creating the 
ZooKeeper ACLs  - currently ZooKeeper code cannot use Immutable collections 
(List.of()) so this uses Collections.unmodifiableList instead.
    
    * Use Collections.unmodifiableList for static ACL declarations
    * Update 
core/src/test/java/org/apache/accumulo/core/fate/zookeeper/ZooUtilTest.java
---
 .../accumulo/core/fate/zookeeper/ZooUtil.java      | 18 +++---
 .../accumulo/core/fate/zookeeper/ZooUtilTest.java  | 69 ++++++++++++++++++++++
 2 files changed, 80 insertions(+), 7 deletions(-)

diff --git 
a/core/src/main/java/org/apache/accumulo/core/fate/zookeeper/ZooUtil.java 
b/core/src/main/java/org/apache/accumulo/core/fate/zookeeper/ZooUtil.java
index c968c33066..47d906fedb 100644
--- a/core/src/main/java/org/apache/accumulo/core/fate/zookeeper/ZooUtil.java
+++ b/core/src/main/java/org/apache/accumulo/core/fate/zookeeper/ZooUtil.java
@@ -27,6 +27,7 @@ import java.time.OffsetDateTime;
 import java.time.ZoneOffset;
 import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.accumulo.core.Constants;
@@ -94,15 +95,18 @@ public class ZooUtil {
     }
   }
 
-  public static final List<ACL> PRIVATE;
-  public static final List<ACL> PUBLIC;
+  // Need to use Collections.unmodifiableList() instead of List.of() or 
List.copyOf(), because
+  // ImmutableCollections.contains() doesn't handle nulls properly 
(JDK-8265905) and ZooKeeper (as
+  // of 3.8.1) calls acl.contains((Object) null) which throws a NPE when 
passed an immutable
+  // collection
+  public static final List<ACL> PRIVATE =
+      Collections.unmodifiableList(new ArrayList<>(Ids.CREATOR_ALL_ACL));
 
+  public static final List<ACL> PUBLIC;
   static {
-    PRIVATE = new ArrayList<>();
-    PRIVATE.addAll(Ids.CREATOR_ALL_ACL);
-    PUBLIC = new ArrayList<>();
-    PUBLIC.addAll(PRIVATE);
-    PUBLIC.add(new ACL(Perms.READ, Ids.ANYONE_ID_UNSAFE));
+    var publicTmp = new ArrayList<>(PRIVATE);
+    publicTmp.add(new ACL(Perms.READ, Ids.ANYONE_ID_UNSAFE));
+    PUBLIC = Collections.unmodifiableList(publicTmp);
   }
 
   public static String getRoot(final InstanceId instanceId) {
diff --git 
a/core/src/test/java/org/apache/accumulo/core/fate/zookeeper/ZooUtilTest.java 
b/core/src/test/java/org/apache/accumulo/core/fate/zookeeper/ZooUtilTest.java
new file mode 100644
index 0000000000..4f9315b9f5
--- /dev/null
+++ 
b/core/src/test/java/org/apache/accumulo/core/fate/zookeeper/ZooUtilTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class ZooUtilTest {
+  Logger log = LoggerFactory.getLogger(ZooUtilTest.class);
+
+  @Test
+  void checkUnmodifiable() throws Exception {
+    assertTrue(validateACL(ZooUtil.PRIVATE));
+    assertTrue(validateACL(ZooUtil.PUBLIC));
+  }
+
+  @Test
+  public void checkImmutableAcl() throws Exception {
+
+    final List<ACL> mutable = new ArrayList<>(ZooDefs.Ids.CREATOR_ALL_ACL);
+    assertTrue(validateACL(mutable));
+
+    // Replicates the acl check in ZooKeeper.java to show ZooKeeper will not 
accept an
+    // ImmutableCollection for the ACL list. ZooKeeper (as of 3.8.1) calls
+    // acl.contains((Object) null) which throws a NPE when passed an immutable 
collectionCallers
+    // because the way ImmutableCollections.contains() handles nulls 
(JDK-8265905)
+    try {
+      final List<ACL> immutable = List.copyOf(ZooDefs.Ids.CREATOR_ALL_ACL);
+      assertThrows(NullPointerException.class, () -> validateACL(immutable));
+    } catch (Exception ex) {
+      log.warn("validateAcls failed with exception", ex);
+    }
+  }
+
+  // Copied from ZooKeeper 3.8.1 for stand-alone testing here
+  // 
https://github.com/apache/zookeeper/blob/2e9c3f3ceda90aeb9380acc87b253bf7661b7794/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java#L3075/
+  private boolean validateACL(List<ACL> acl) throws 
KeeperException.InvalidACLException {
+    if (acl == null || acl.isEmpty() || acl.contains((Object) null)) {
+      throw new KeeperException.InvalidACLException();
+    }
+    return true;
+  }
+}

Reply via email to