dsmiley commented on code in PR #5112: URL: https://github.com/apache/solr/pull/5112#discussion_r4354183654
########## solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java: ########## @@ -0,0 +1,231 @@ +/* + * 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.servlet; + +import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST; +import static org.apache.solr.common.SolrException.ErrorCode.SERVER_ERROR; +import static org.apache.solr.common.SolrException.ErrorCode.SERVICE_UNAVAILABLE; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.UnavailableException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import org.apache.solr.client.solrj.jetty.HttpJettySolrClient; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.common.params.ModifiableSolrParams; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.core.CoreContainer; +import org.eclipse.jetty.client.HttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * If the request carries the {@link #TARGET_NODE_HEADER} header naming a different live node, + * proxies the request there verbatim and returns; otherwise passes the request through. Unlike + * {@link HttpSolrCall}'s collection/core-driven proxying, this works for any endpoint, including + * node-level ones (e.g. {@code /solr/admin/**}, {@code /api/node/**}), so a request can be pinned + * to a specific node even when reached through a load balancer that hides which node answers. + */ +public class NodeTargetProxyFilter extends CoreContainerAwareHttpFilter { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + /** + * Header naming a live node (as it appears in ZK live_nodes) to proxy this request to. Two + * shorthand forms are also accepted, both resolved relative to the current node: a value of all + * digits replaces the trailing {@code -<digits>} ordinal in the current node's hostname (e.g. + * {@code 8} turns {@code myhost-3.mydomain.com:3000} into {@code myhost-8.mydomain.com:3000}); + * otherwise, a value with no '.' is treated as a bare peer hostname, with the current node's + * domain suffix and port appended. + */ + public static final String TARGET_NODE_HEADER = "X-Solr-targetNode"; + + @Override + protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) + throws IOException, ServletException { + + String targetNode = req.getHeader(TARGET_NODE_HEADER); + + // also accept it as a query param on GET requests, for convenience when a header can't + // easily be set (e.g. a browser address bar) + String queryString = req.getQueryString(); + if ("GET".equals(req.getMethod()) + && queryString != null + && queryString.contains(TARGET_NODE_HEADER)) { // cheap; avoid double-parse + SolrParams params = SolrRequestParsers.parseQueryString(queryString); + if (targetNode != null && params.get(TARGET_NODE_HEADER) != null) { + res.sendError(400, TARGET_NODE_HEADER + " must EITHER be a header or url param"); + return; + } + targetNode = params.get(TARGET_NODE_HEADER); + if (targetNode != null) { + queryString = filterQueryString(params); + } + } + Review Comment: there is non-trivial complexity to support the query string option, particularly for removal. It's a sunk costs as this point -- it works, tested, etc. The motivation is to make this easy for a user with their browser. ########## solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java: ########## Review Comment: technically, this aspect is a small bug fix to possible encoding issue in the path. That's very unlikely to happen in practice IMO. ########## solr/core/src/java/org/apache/solr/servlet/NodeTargetProxyFilter.java: ########## @@ -0,0 +1,231 @@ +/* + * 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.servlet; + +import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST; +import static org.apache.solr.common.SolrException.ErrorCode.SERVER_ERROR; +import static org.apache.solr.common.SolrException.ErrorCode.SERVICE_UNAVAILABLE; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.UnavailableException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import org.apache.solr.client.solrj.jetty.HttpJettySolrClient; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.common.params.ModifiableSolrParams; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.core.CoreContainer; +import org.eclipse.jetty.client.HttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * If the request carries the {@link #TARGET_NODE_HEADER} header naming a different live node, + * proxies the request there verbatim and returns; otherwise passes the request through. Unlike + * {@link HttpSolrCall}'s collection/core-driven proxying, this works for any endpoint, including + * node-level ones (e.g. {@code /solr/admin/**}, {@code /api/node/**}), so a request can be pinned + * to a specific node even when reached through a load balancer that hides which node answers. + */ +public class NodeTargetProxyFilter extends CoreContainerAwareHttpFilter { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + /** + * Header naming a live node (as it appears in ZK live_nodes) to proxy this request to. Two + * shorthand forms are also accepted, both resolved relative to the current node: a value of all + * digits replaces the trailing {@code -<digits>} ordinal in the current node's hostname (e.g. + * {@code 8} turns {@code myhost-3.mydomain.com:3000} into {@code myhost-8.mydomain.com:3000}); + * otherwise, a value with no '.' is treated as a bare peer hostname, with the current node's + * domain suffix and port appended. + */ + public static final String TARGET_NODE_HEADER = "X-Solr-targetNode"; + + @Override + protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) + throws IOException, ServletException { + + String targetNode = req.getHeader(TARGET_NODE_HEADER); + + // also accept it as a query param on GET requests, for convenience when a header can't + // easily be set (e.g. a browser address bar) + String queryString = req.getQueryString(); + if ("GET".equals(req.getMethod()) + && queryString != null + && queryString.contains(TARGET_NODE_HEADER)) { // cheap; avoid double-parse + SolrParams params = SolrRequestParsers.parseQueryString(queryString); + if (targetNode != null && params.get(TARGET_NODE_HEADER) != null) { + res.sendError(400, TARGET_NODE_HEADER + " must EITHER be a header or url param"); + return; + } + targetNode = params.get(TARGET_NODE_HEADER); + if (targetNode != null) { + queryString = filterQueryString(params); + } + } + + // wrap the request to filter out the header & param. + // NOTE: we assume downstream processing only accesses such data via what we override !! Review Comment: an alternative would be to enhance HttpSolrProxy to directly do this. The header part would trivially be one line in hop-by-hop headers with a comment justification. I don't love the coupling but it's in the same package. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
