You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by GitBox <gi...@apache.org> on 2021/04/15 15:28:58 UTC

[GitHub] [solr] madrob commented on a change in pull request #47: SOLR-15340: Rename shardsWhitelist and extract AllowListUrlChecker to use it more broadly.

madrob commented on a change in pull request #47:
URL: https://github.com/apache/solr/pull/47#discussion_r614157585



##########
File path: solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java
##########
@@ -38,6 +24,14 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.lang.invoke.MethodHandles;
+import java.util.*;

Review comment:
       Please don't use wildcard imports.

##########
File path: solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java
##########
@@ -74,6 +75,8 @@
 
   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+  private static final Pattern COMMA_SEPARATED_PATTERN = Pattern.compile(",\\s*");

Review comment:
       Should this accept whitespace on either side of the comma? `\\s*,\\s*`

##########
File path: solr/core/src/java/org/apache/solr/security/AllowListUrlChecker.java
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.solr.security;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.cloud.ClusterState;
+import org.apache.solr.core.NodeConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+import java.lang.invoke.MethodHandles;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Validates URLs based on an allow list or a {@link ClusterState} in SolrCloud.
+ */
+public class AllowListUrlChecker {
+
+    private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+    /**
+     * {@link org.apache.solr.core.SolrXmlConfig} property to configure the allowed URLs.
+     */
+    public static final String URL_ALLOW_LIST = "allowUrls";
+
+    /**
+     * System property to disable URL checking and {@link #ALLOW_ALL} instead.
+     */
+    public static final String DISABLE_URL_ALLOW_LIST = "solr.disable." + URL_ALLOW_LIST;
+
+    /**
+     * Clue given in URL-forbidden exceptions messages.
+     */
+    public static final String SET_SOLR_DISABLE_URL_ALLOW_LIST_CLUE = "Set -D" + DISABLE_URL_ALLOW_LIST + "=true to disable URL allow-list checks.";
+
+    /**
+     * Singleton checker which allows all URLs. {@link #isEnabled()} returns false.
+     */
+    public static final AllowListUrlChecker ALLOW_ALL;
+
+    static {
+        try {
+            ALLOW_ALL = new AllowListUrlChecker(Collections.emptyList()) {
+                @Override
+                public void checkAllowList(List<String> urls, ClusterState clusterState) {
+                    // Allow.
+                }
+
+                @Override
+                public boolean isEnabled() {
+                    return false;
+                }
+
+                @Override
+                public String toString() {
+                    return getClass().getSimpleName() + " [allow all]";
+                }
+            };
+        } catch (MalformedURLException e) {
+            // Never thrown.
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
+        }
+    }
+
+    /**
+     * Allow list of hosts. Elements in the list will be host:port (no protocol or context).
+     */
+    private final Set<String> hostAllowList;
+
+    /**
+     * @param urlAllowList List of allowed URLs. URLs must be well-formed, missing protocol is tolerated.
+     *                    An empty list means there is no explicit allow-list of URLs, in this case no
+     *                    URL is allowed unless a {@link ClusterState} is provided in
+     *                    {@link #checkAllowList(List, ClusterState)}.
+     * @throws MalformedURLException If an URL is invalid.
+     */
+    public AllowListUrlChecker(List<String> urlAllowList) throws MalformedURLException {
+        hostAllowList = parseHostPorts(urlAllowList);
+    }
+
+    /**
+     * Creates a URL checker based on the {@link NodeConfig} property to configure the allowed URLs.
+     */
+    public static AllowListUrlChecker create(NodeConfig config) {
+        if (Boolean.getBoolean(DISABLE_URL_ALLOW_LIST)) {
+            return AllowListUrlChecker.ALLOW_ALL;
+        } else if (System.getProperty("solr.disable.shardsWhitelist") != null) {
+            log.warn("Property 'solr.disable.shardsWhitelist' is deprecated, please use '{}' instead.", DISABLE_URL_ALLOW_LIST);
+        }
+        try {
+            return new AllowListUrlChecker(config.getAllowUrls());
+        } catch (MalformedURLException e) {
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
+                    "Invalid URL syntax in '" + URL_ALLOW_LIST + "' configuration: " + config.getAllowUrls(), e);
+        }
+    }
+
+    /**
+     * @see #checkAllowList(List, ClusterState)
+     */
+    public void checkAllowList(List<String> urls) throws MalformedURLException {
+        checkAllowList(urls, null);
+    }
+
+    /**
+     * Checks that the given URLs are present in the configured allow-list or in the provided {@link ClusterState}
+     * (in case of cloud mode).
+     *
+     * @param urls         The list of urls to check.
+     * @param clusterState The up to date {@link ClusterState}, can be null in case of non-cloud mode.
+     * @throws MalformedURLException If an URL is invalid.
+     * @throws SolrException         If an URL is not present in the allow-list or in the provided {@link ClusterState}.
+     */
+    public void checkAllowList(List<String> urls, @Nullable ClusterState clusterState) throws MalformedURLException {
+        Set<String> clusterHostAllowList = clusterState == null ? Collections.emptySet() : clusterState.getHostAllowList();
+        for (String url : urls) {
+            String hostPort = parseHostPort(url);
+            if (!clusterHostAllowList.contains(hostPort) && !hostAllowList.contains(hostPort)) {
+                throw new SolrException(SolrException.ErrorCode.FORBIDDEN, "URL " + url +
+                        " is neither a live node of the cluster nor in the configured '" +
+                        URL_ALLOW_LIST + "' " + hostAllowList);
+            }
+        }
+    }
+
+    /**
+     * Whether this checker has been created with a non-empty allow-list of URLs.
+     */
+    public boolean hasExplicitAllowList() {
+        return !hostAllowList.isEmpty();
+    }
+
+    /**
+     * Whether the URL checking is enabled. Only {@link #ALLOW_ALL} returns false.
+     */
+    public boolean isEnabled() {
+        return true;
+    }
+
+    /**
+     * Only for testing.
+     */
+    @VisibleForTesting
+    public Set<String> getHostAllowList() {
+        return hostAllowList == null ? null : Collections.unmodifiableSet(hostAllowList);
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " [allowList=" + hostAllowList + "]";
+    }
+
+    @VisibleForTesting
+    static Set<String> parseHostPorts(List<String> urls) throws MalformedURLException {
+        if (urls == null || urls.isEmpty()) {
+            return Collections.emptySet();
+        }
+        Set<String> hostPorts = new HashSet<>((int) (urls.size() / 0.7f) + 1);
+        for (String urlString : urls) {
+            hostPorts.add(parseHostPort(urlString));
+        }
+        return hostPorts;
+    }
+
+    private static String parseHostPort(String url) throws MalformedURLException {
+        url = url.trim();
+        URL u;
+        if (!url.startsWith("http://") && !url.startsWith("https://")) {

Review comment:
       Are there circumstances where we would be looking at a different protocol? Since we're using this for more than just shards now, maybe this will be part of BackupRestore and will need to accept `s3://` URLs?

##########
File path: solr/core/src/java/org/apache/solr/security/AllowListUrlChecker.java
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.solr.security;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.cloud.ClusterState;
+import org.apache.solr.core.NodeConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+import java.lang.invoke.MethodHandles;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Validates URLs based on an allow list or a {@link ClusterState} in SolrCloud.
+ */
+public class AllowListUrlChecker {
+
+    private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+    /**
+     * {@link org.apache.solr.core.SolrXmlConfig} property to configure the allowed URLs.
+     */
+    public static final String URL_ALLOW_LIST = "allowUrls";
+
+    /**
+     * System property to disable URL checking and {@link #ALLOW_ALL} instead.
+     */
+    public static final String DISABLE_URL_ALLOW_LIST = "solr.disable." + URL_ALLOW_LIST;
+
+    /**
+     * Clue given in URL-forbidden exceptions messages.
+     */
+    public static final String SET_SOLR_DISABLE_URL_ALLOW_LIST_CLUE = "Set -D" + DISABLE_URL_ALLOW_LIST + "=true to disable URL allow-list checks.";
+
+    /**
+     * Singleton checker which allows all URLs. {@link #isEnabled()} returns false.
+     */
+    public static final AllowListUrlChecker ALLOW_ALL;
+
+    static {
+        try {
+            ALLOW_ALL = new AllowListUrlChecker(Collections.emptyList()) {
+                @Override
+                public void checkAllowList(List<String> urls, ClusterState clusterState) {
+                    // Allow.
+                }
+
+                @Override
+                public boolean isEnabled() {
+                    return false;
+                }
+
+                @Override
+                public String toString() {
+                    return getClass().getSimpleName() + " [allow all]";
+                }
+            };
+        } catch (MalformedURLException e) {
+            // Never thrown.
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
+        }
+    }
+
+    /**
+     * Allow list of hosts. Elements in the list will be host:port (no protocol or context).
+     */
+    private final Set<String> hostAllowList;
+
+    /**
+     * @param urlAllowList List of allowed URLs. URLs must be well-formed, missing protocol is tolerated.
+     *                    An empty list means there is no explicit allow-list of URLs, in this case no
+     *                    URL is allowed unless a {@link ClusterState} is provided in
+     *                    {@link #checkAllowList(List, ClusterState)}.
+     * @throws MalformedURLException If an URL is invalid.
+     */
+    public AllowListUrlChecker(List<String> urlAllowList) throws MalformedURLException {
+        hostAllowList = parseHostPorts(urlAllowList);
+    }
+
+    /**
+     * Creates a URL checker based on the {@link NodeConfig} property to configure the allowed URLs.
+     */
+    public static AllowListUrlChecker create(NodeConfig config) {
+        if (Boolean.getBoolean(DISABLE_URL_ALLOW_LIST)) {
+            return AllowListUrlChecker.ALLOW_ALL;
+        } else if (System.getProperty("solr.disable.shardsWhitelist") != null) {
+            log.warn("Property 'solr.disable.shardsWhitelist' is deprecated, please use '{}' instead.", DISABLE_URL_ALLOW_LIST);
+        }
+        try {
+            return new AllowListUrlChecker(config.getAllowUrls());
+        } catch (MalformedURLException e) {
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
+                    "Invalid URL syntax in '" + URL_ALLOW_LIST + "' configuration: " + config.getAllowUrls(), e);
+        }
+    }
+
+    /**
+     * @see #checkAllowList(List, ClusterState)
+     */
+    public void checkAllowList(List<String> urls) throws MalformedURLException {
+        checkAllowList(urls, null);
+    }
+
+    /**
+     * Checks that the given URLs are present in the configured allow-list or in the provided {@link ClusterState}
+     * (in case of cloud mode).
+     *
+     * @param urls         The list of urls to check.
+     * @param clusterState The up to date {@link ClusterState}, can be null in case of non-cloud mode.
+     * @throws MalformedURLException If an URL is invalid.
+     * @throws SolrException         If an URL is not present in the allow-list or in the provided {@link ClusterState}.
+     */
+    public void checkAllowList(List<String> urls, @Nullable ClusterState clusterState) throws MalformedURLException {
+        Set<String> clusterHostAllowList = clusterState == null ? Collections.emptySet() : clusterState.getHostAllowList();
+        for (String url : urls) {
+            String hostPort = parseHostPort(url);
+            if (!clusterHostAllowList.contains(hostPort) && !hostAllowList.contains(hostPort)) {
+                throw new SolrException(SolrException.ErrorCode.FORBIDDEN, "URL " + url +
+                        " is neither a live node of the cluster nor in the configured '" +
+                        URL_ALLOW_LIST + "' " + hostAllowList);
+            }
+        }
+    }
+
+    /**
+     * Whether this checker has been created with a non-empty allow-list of URLs.
+     */
+    public boolean hasExplicitAllowList() {
+        return !hostAllowList.isEmpty();
+    }
+
+    /**
+     * Whether the URL checking is enabled. Only {@link #ALLOW_ALL} returns false.
+     */
+    public boolean isEnabled() {
+        return true;
+    }
+
+    /**
+     * Only for testing.
+     */
+    @VisibleForTesting
+    public Set<String> getHostAllowList() {
+        return hostAllowList == null ? null : Collections.unmodifiableSet(hostAllowList);
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " [allowList=" + hostAllowList + "]";
+    }
+
+    @VisibleForTesting
+    static Set<String> parseHostPorts(List<String> urls) throws MalformedURLException {
+        if (urls == null || urls.isEmpty()) {
+            return Collections.emptySet();
+        }
+        Set<String> hostPorts = new HashSet<>((int) (urls.size() / 0.7f) + 1);

Review comment:
       This feels like a weird optimization.

##########
File path: solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java
##########
@@ -352,12 +358,19 @@ private static NodeConfig fillSolrSection(NodeConfig.NodeConfigBuilder builder,
     return builder.build();
   }
 
-  private static Set<Path> stringToPaths(String commaSeparatedString) {
+  private static List<String> separateStrings(String commaSeparatedString) {
+    if (Strings.isNullOrEmpty(commaSeparatedString)) {
+      return Collections.emptyList();
+    }
+    return Arrays.asList(COMMA_SEPARATED_PATTERN.split(commaSeparatedString));
+  }
+
+  private static Set<Path> separatePaths(String commaSeparatedString) {
     if (Strings.isNullOrEmpty(commaSeparatedString)) {
       return Collections.emptySet();
     }
     // Parse list of paths. The special value '*' is mapped to _ALL_ to mean all paths
-    return Arrays.stream(commaSeparatedString.split(",\\s?"))
+    return Arrays.stream(COMMA_SEPARATED_PATTERN.split(commaSeparatedString))
         .map(p -> Paths.get("*".equals(p) ? "_ALL_" : p)).collect(Collectors.toSet());

Review comment:
       If `*` (or `_ALL_`) is present, can we drop the rest of the paths?




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org