You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by br...@apache.org on 2021/04/26 20:56:53 UTC

[solr] branch main updated: SOLR-15340: Fix allowPaths building.

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

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


The following commit(s) were added to refs/heads/main by this push:
     new f206930  SOLR-15340: Fix allowPaths building.
f206930 is described below

commit f20693055744e36abb92f4d54835d18dcec6bd98
Author: Bruno Roustant <br...@gmail.com>
AuthorDate: Mon Apr 26 22:41:52 2021 +0200

    SOLR-15340: Fix allowPaths building.
---
 .../java/org/apache/solr/core/CoreContainer.java   | 26 ++++++---------
 .../src/java/org/apache/solr/core/SolrPaths.java   | 38 ++++++++++++++++++++--
 .../java/org/apache/solr/core/SolrXmlConfig.java   | 10 ++----
 .../apache/solr/cloud/UnloadDistributedZkTest.java |  2 +-
 4 files changed, 50 insertions(+), 26 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java
index 6b00365..a47dc9b 100644
--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java
+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java
@@ -43,7 +43,6 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeoutException;
 import java.util.function.Supplier;
-import java.util.stream.Collectors;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableMap;
@@ -279,9 +278,9 @@ public class CoreContainer {
   private PackageStoreAPI packageStoreAPI;
   private PackageLoader packageLoader;
 
-  private Set<Path> allowPaths;
+  private final Set<Path> allowPaths;
 
-  private AllowListUrlChecker allowListUrlChecker;
+  private final AllowListUrlChecker allowListUrlChecker;
 
   // Bits for the state variable.
   public final static long LOAD_COMPLETE = 0x1L;
@@ -395,18 +394,19 @@ public class CoreContainer {
             cfg.getReplayUpdatesThreads(),
             new SolrNamedThreadFactory("replayUpdatesExecutor")));
 
-    this.allowPaths = new java.util.HashSet<>();
-    addToAllowPath(cfg.getSolrHome());
-    addToAllowPath(cfg.getCoreRootDirectory());
+    SolrPaths.AllowPathBuilder allowPathBuilder = new SolrPaths.AllowPathBuilder();
+    allowPathBuilder.addPath(cfg.getSolrHome());
+    allowPathBuilder.addPath(cfg.getCoreRootDirectory());
     if (cfg.getSolrDataHome() != null) {
-      addToAllowPath(cfg.getSolrDataHome());
+      allowPathBuilder.addPath(cfg.getSolrDataHome());
     }
     if (!cfg.getAllowPaths().isEmpty()) {
-      addAllToAllowPath(cfg.getAllowPaths());
+      cfg.getAllowPaths().forEach(allowPathBuilder::addPath);
       if (log.isInfoEnabled()) {
         log.info("Allowing use of paths: {}", cfg.getAllowPaths());
       }
     }
+    this.allowPaths = allowPathBuilder.build();
 
     this.allowListUrlChecker = AllowListUrlChecker.create(config);
 
@@ -418,14 +418,6 @@ public class CoreContainer {
     }
   }
 
-  private void addToAllowPath(Path path) {
-    this.allowPaths.add(path.normalize());
-  }
-
-  private void addAllToAllowPath(Set<Path> paths) {
-    this.allowPaths.addAll(paths.stream().map( path -> path.normalize()).collect(Collectors.toSet()));
-  }
-
   @SuppressWarnings({"unchecked"})
   private synchronized void initializeAuthorizationPlugin(Map<String, Object> authorizationConf) {
     authorizationConf = Utils.getDeepCopy(authorizationConf, 4);
@@ -619,6 +611,8 @@ public class CoreContainer {
     containerProperties = null;
     replayUpdatesExecutor = null;
     distributedCollectionCommandRunner = Optional.empty();
+    allowPaths = null;
+    allowListUrlChecker = null;
   }
 
   public static CoreContainer createAndLoad(Path solrHome) {
diff --git a/solr/core/src/java/org/apache/solr/core/SolrPaths.java b/solr/core/src/java/org/apache/solr/core/SolrPaths.java
index caa0fb0..6e06e08 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrPaths.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrPaths.java
@@ -22,6 +22,7 @@ import java.lang.invoke.MethodHandles;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.commons.exec.OS;
@@ -36,9 +37,13 @@ public final class SolrPaths {
   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
   /**
-   * Special path set which accepts all paths.
+   * Special path which means to accept all paths.
    */
-  public static final Set<Path> ALL_PATHS = Collections.singleton(Paths.get("_ALL_"));
+  public static final Path ALL_PATH = Paths.get("_ALL_");
+  /**
+   * Special singleton path set containing only {@link #ALL_PATH}.
+   */
+  private static final Set<Path> ALL_PATHS = Collections.singleton(ALL_PATH);
 
   private SolrPaths() {} // don't create this
 
@@ -82,4 +87,33 @@ public final class SolrPaths {
           "Path " + path + " must be relative to SOLR_HOME, SOLR_DATA_HOME coreRootDirectory. Set system property 'solr.allowPaths' to add other allowed paths.");
     }
   }
+
+  /**
+   * Builds a set of allowed {@link Path}.
+   * Detects special paths '*' and '_ALL_' that mean all paths are allowed.
+   */
+  public static class AllowPathBuilder {
+
+    private static final Path WILDCARD_PATH = Paths.get("*");
+
+    private Set<Path> paths;
+
+    public AllowPathBuilder addPath(Path path) {
+      if (paths != ALL_PATHS) {
+        if (path.equals(ALL_PATH) || path.equals(WILDCARD_PATH)) {
+          paths = ALL_PATHS;
+        } else {
+          if (paths == null) {
+            paths = new HashSet<>();
+          }
+          paths.add(path.normalize());
+        }
+      }
+      return this;
+    }
+
+    public Set<Path> build() {
+      return paths == null ? Collections.emptySet() : paths;
+    }
+  }
 }
diff --git a/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java b/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java
index a98565d..43b3c6f 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java
@@ -39,7 +39,6 @@ import java.util.Set;
 import java.util.regex.Pattern;
 
 import com.google.common.base.Strings;
-import com.google.common.collect.Sets;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.solr.client.solrj.impl.HttpClientUtil;
@@ -371,14 +370,11 @@ public class SolrXmlConfig {
     }
     // Parse the list of paths. The special values '*' and '_ALL_' mean all paths.
     String[] pathStrings = COMMA_SEPARATED_PATTERN.split(commaSeparatedString);
-    Set<Path> paths = Sets.newHashSetWithExpectedSize(pathStrings.length);
+    SolrPaths.AllowPathBuilder allowPathBuilder = new SolrPaths.AllowPathBuilder();
     for (String p : pathStrings) {
-      if ("*".equals(p) || "_ALL_".equals(p)) {
-        return SolrPaths.ALL_PATHS;
-      }
-      paths.add(Paths.get(p));
+      allowPathBuilder.addPath(Paths.get(p));
     }
-    return paths;
+    return allowPathBuilder.build();
   }
 
   private static UpdateShardHandlerConfig loadUpdateConfig(NamedList<Object> nl, boolean alwaysDefine) {
diff --git a/solr/core/src/test/org/apache/solr/cloud/UnloadDistributedZkTest.java b/solr/core/src/test/org/apache/solr/cloud/UnloadDistributedZkTest.java
index 722700d..f839168 100644
--- a/solr/core/src/test/org/apache/solr/cloud/UnloadDistributedZkTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/UnloadDistributedZkTest.java
@@ -70,7 +70,7 @@ public class UnloadDistributedZkTest extends BasicDistributedZkTest {
     jettys.forEach(j -> {
       Set<Path> allowPath = j.getCoreContainer().getAllowPaths();
       allowPath.clear();
-      allowPath.addAll(SolrPaths.ALL_PATHS); // Allow non-standard core instance path
+      allowPath.add(SolrPaths.ALL_PATH); // Allow non-standard core instance path
     });
     testCoreUnloadAndLeaders(); // long
     testUnloadLotsOfCores(); // long