dsmiley commented on code in PR #4120:
URL: https://github.com/apache/solr/pull/4120#discussion_r2784705500


##########
solr/core/src/java/org/apache/solr/servlet/AuthenticationFilter.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.SERVER_ERROR;
+import static org.apache.solr.security.AuditEvent.EventType.ANONYMOUS;
+import static org.apache.solr.security.AuditEvent.EventType.AUTHENTICATED;
+import static org.apache.solr.security.AuditEvent.EventType.REJECTED;
+
+import io.opentelemetry.api.trace.Span;
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.FilterConfig;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.security.AuthenticationPlugin;
+import org.apache.solr.security.PKIAuthenticationPlugin;
+import org.apache.solr.security.PublicKeyHandler;
+import org.apache.solr.util.tracing.TraceUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AuthenticationFilter extends CoreContainerAwareHttpFilter {
+
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public static final String PLUGIN_DID_NOT_SET_ERROR_STATUS =
+      "{} threw SolrAuthenticationException without setting request status >= 
400";
+
+  @Override
+  public void init(FilterConfig config) throws ServletException {
+    super.init(config);
+  }
+
+  @Override
+  protected void doFilter(HttpServletRequest req, HttpServletResponse res, 
FilterChain chain)
+      throws IOException, ServletException {
+
+    CoreContainer cc = getCores();
+    AuthenticationPlugin authenticationPlugin = cc.getAuthenticationPlugin();
+    String requestPath = ServletUtils.getPathAfterContext(req);
+
+    /////////////////////////////////////////////////////////
+    //////// Check cases where auth is not required. ////////
+    /////////////////////////////////////////////////////////
+
+    // Is authentication configured?
+    if (authenticationPlugin == null) {
+      cc.audit(ANONYMOUS, req);
+      chain.doFilter(req, res);
+      return;
+    }
+
+    // /admin/info/key must be always open. see SOLR-9188
+    if (PublicKeyHandler.PATH.equals(requestPath)) {
+      log.debug("Pass through PKI authentication endpoint");
+      chain.doFilter(req, res);
+      return;
+    }
+
+    if (isAdminUI(requestPath)) {

Review Comment:
   FYI this will soon be unnecessary with the conversion of SolrDispatchFilter 
to a servlet, as we'll simply configure our filters to be assigned to 
SolrServlet, thus skipping a default servlet for static resources.



##########
solr/core/src/java/org/apache/solr/servlet/AuthenticationFilter.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.SERVER_ERROR;
+import static org.apache.solr.security.AuditEvent.EventType.ANONYMOUS;
+import static org.apache.solr.security.AuditEvent.EventType.AUTHENTICATED;
+import static org.apache.solr.security.AuditEvent.EventType.REJECTED;
+
+import io.opentelemetry.api.trace.Span;
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.FilterConfig;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.security.AuthenticationPlugin;
+import org.apache.solr.security.PKIAuthenticationPlugin;
+import org.apache.solr.security.PublicKeyHandler;
+import org.apache.solr.util.tracing.TraceUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AuthenticationFilter extends CoreContainerAwareHttpFilter {
+
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public static final String PLUGIN_DID_NOT_SET_ERROR_STATUS =
+      "{} threw SolrAuthenticationException without setting request status >= 
400";
+
+  @Override
+  public void init(FilterConfig config) throws ServletException {
+    super.init(config);
+  }
+
+  @Override
+  protected void doFilter(HttpServletRequest req, HttpServletResponse res, 
FilterChain chain)
+      throws IOException, ServletException {
+
+    CoreContainer cc = getCores();
+    AuthenticationPlugin authenticationPlugin = cc.getAuthenticationPlugin();
+    String requestPath = ServletUtils.getPathAfterContext(req);
+
+    /////////////////////////////////////////////////////////
+    //////// Check cases where auth is not required. ////////
+    /////////////////////////////////////////////////////////

Review Comment:
   Can we please not have text banners in code



##########
solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java:
##########


Review Comment:
   Love the reduction in lines of code!



##########
solr/core/src/java/org/apache/solr/servlet/AuthenticationFilter.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.SERVER_ERROR;
+import static org.apache.solr.security.AuditEvent.EventType.ANONYMOUS;
+import static org.apache.solr.security.AuditEvent.EventType.AUTHENTICATED;
+import static org.apache.solr.security.AuditEvent.EventType.REJECTED;
+
+import io.opentelemetry.api.trace.Span;
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.FilterConfig;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.security.AuthenticationPlugin;
+import org.apache.solr.security.PKIAuthenticationPlugin;
+import org.apache.solr.security.PublicKeyHandler;
+import org.apache.solr.util.tracing.TraceUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AuthenticationFilter extends CoreContainerAwareHttpFilter {
+
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public static final String PLUGIN_DID_NOT_SET_ERROR_STATUS =
+      "{} threw SolrAuthenticationException without setting request status >= 
400";
+
+  @Override
+  public void init(FilterConfig config) throws ServletException {
+    super.init(config);
+  }
+
+  @Override
+  protected void doFilter(HttpServletRequest req, HttpServletResponse res, 
FilterChain chain)
+      throws IOException, ServletException {
+
+    CoreContainer cc = getCores();
+    AuthenticationPlugin authenticationPlugin = cc.getAuthenticationPlugin();
+    String requestPath = ServletUtils.getPathAfterContext(req);
+
+    /////////////////////////////////////////////////////////
+    //////// Check cases where auth is not required. ////////
+    /////////////////////////////////////////////////////////
+
+    // Is authentication configured?
+    if (authenticationPlugin == null) {
+      cc.audit(ANONYMOUS, req);
+      chain.doFilter(req, res);
+      return;
+    }
+
+    // /admin/info/key must be always open. see SOLR-9188
+    if (PublicKeyHandler.PATH.equals(requestPath)) {
+      log.debug("Pass through PKI authentication endpoint");
+      chain.doFilter(req, res);
+      return;
+    }
+
+    if (isAdminUI(requestPath)) {
+      log.debug("Pass through Admin UI entry point");
+      chain.doFilter(req, res);
+      return;
+    }
+
+    /////////////////////////////////////////////////////////////////
+    //////// if we make it here, authentication is required. ////////
+    /////////////////////////////////////////////////////////////////
+
+    // internode (internal) requests have their own PKI auth plugin
+    if (isInternodePKI(req, cc)) {
+      authenticationPlugin = cc.getPkiAuthenticationSecurityBuilder();
+    }
+
+    boolean authSuccess = false;
+    try {
+      authSuccess =
+          authenticate(req, res, new AuditSuccessChainWrapper(cc, chain), 
authenticationPlugin);
+    } finally {
+      if (!authSuccess) {
+        cc.audit(REJECTED, req);
+        res.flushBuffer();
+        if (res.getStatus() < 400) {
+          log.error(PLUGIN_DID_NOT_SET_ERROR_STATUS, 
authenticationPlugin.getClass());
+          res.sendError(401, "Authentication Plugin rejected credentials.");
+        }
+      }
+    }
+  }
+
+  /**
+   * The plugin called by this method will call doFilter(req, res) for us 
(which is why we pass in
+   * the filter chain object as a parameter).
+   */
+  private boolean authenticate(
+      HttpServletRequest req,
+      HttpServletResponse res,
+      FilterChain chain,
+      AuthenticationPlugin authenticationPlugin) {
+    try {
+
+      // It is imperative that authentication plugins obey the contract in the 
javadoc for
+      // org.apache.solr.security.AuthenticationPlugin.doAuthenticate, and 
either return
+      // false or throw an exception if they cannot validate the user's 
credentials.
+      // Any plugin that doesn't do this is broken and should be fixed.
+
+      logAuthAttempt(req);
+      return authenticationPlugin.authenticate(req, res, chain);
+    } catch (Exception e) {
+      log.info("Error authenticating", e);
+      throw new SolrException(SERVER_ERROR, "Error during request 
authentication, ", e);
+    }
+  }
+
+  private static boolean isAdminUI(String requestPath) {
+    return "/solr/".equals(requestPath) || "/".equals(requestPath);
+  }
+
+  private boolean isInternodePKI(HttpServletRequest req, CoreContainer cores) {
+    String header = req.getHeader(PKIAuthenticationPlugin.HEADER);
+    String headerV2 = req.getHeader(PKIAuthenticationPlugin.HEADER_V2);
+    return (header != null || headerV2 != null)
+        && cores.getPkiAuthenticationSecurityBuilder() != null;
+  }
+
+  private void logAuthAttempt(HttpServletRequest req) {
+    // moved this to a method because spotless formatting is so horrible, and 
makes the log
+    // message look like a big deal... but it's just taking up space
+    if (log.isDebugEnabled()) {
+      log.debug(
+          "Request to authenticate: {}, domain: {}, port: {}",
+          req,
+          req.getLocalName(),
+          req.getLocalPort());
+    }
+  }
+
+
+
+  private record AuditSuccessChainWrapper(CoreContainer cc, FilterChain chain)

Review Comment:
   records are cool but this doesn't seem like an appropriate use since 
conceptually this is a "FilterChain" not some pojo-ish thing.  I'm assuming 
equals & hashCode aren't needed.



##########
solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java:
##########
@@ -125,7 +118,7 @@ public void doFilter(HttpServletRequest request, 
HttpServletResponse response, F
       throws IOException, ServletException {
     // internal version of doFilter that tracks if we are in a retry
 
-    dispatch(chain, closeShield(request), closeShield(response), false);
+    dispatch(chain, request, response, false);

Review Comment:
   I think the closeShield calls should be *here*.



##########
solr/core/src/java/org/apache/solr/servlet/AuthenticationFilter.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.SERVER_ERROR;
+import static org.apache.solr.security.AuditEvent.EventType.ANONYMOUS;
+import static org.apache.solr.security.AuditEvent.EventType.AUTHENTICATED;
+import static org.apache.solr.security.AuditEvent.EventType.REJECTED;
+
+import io.opentelemetry.api.trace.Span;
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.FilterConfig;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.security.AuthenticationPlugin;
+import org.apache.solr.security.PKIAuthenticationPlugin;
+import org.apache.solr.security.PublicKeyHandler;
+import org.apache.solr.util.tracing.TraceUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AuthenticationFilter extends CoreContainerAwareHttpFilter {

Review Comment:
   again, pleaese say some basic things in javadoc



##########
solr/test-framework/src/java/org/apache/solr/embedded/JettySolrRunner.java:
##########
@@ -423,10 +425,14 @@ public void contextInitialized(ServletContextEvent event) 
{
       rateLimitFilter = 
root.getServletHandler().newFilterHolder(Source.EMBEDDED);
       rateLimitFilter.setHeldClass(RateLimitFilter.class);
 
-      // Ratelimit Requests
+      // Trace Requests

Review Comment:
   IMO these comments don't add anything to the obviousness of the names of 
these held classes & vars.



##########
solr/core/src/java/org/apache/solr/core/CoreContainer.java:
##########
@@ -2510,4 +2512,29 @@ public void addKeyVal(Object map, Object key, Object 
val) throws IOException {
           }
         });
   }
+
+  /**
+   * Check if audit logging is enabled and should happen for given event type
+   *
+   * @param eventType the audit event
+   */
+  public boolean shouldAudit(AuditEvent.EventType eventType) {

Review Comment:
   I don't see callers to what you added.  We should be conservative in adding 
yet more methods to CoreContainer.



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

Reply via email to