sonatype-lift[bot] commented on code in PR #975: URL: https://github.com/apache/solr/pull/975#discussion_r965305689
########## solr/core/src/java/org/apache/solr/jersey/SolrRequestAuthorizer.java: ########## @@ -0,0 +1,132 @@ +/* + * 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.jersey; + +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.util.List; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.ResourceInfo; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.core.CoreContainer; +import org.apache.solr.security.AuthorizationContext; +import org.apache.solr.security.AuthorizationUtils; +import org.apache.solr.security.HttpServletAuthorizationContext; +import org.apache.solr.security.PermissionNameProvider; +import org.apache.solr.servlet.ServletUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A JAX-RS request filter that blocks or allows requests based on the authorization plugin + * configured in security.json. + */ +@Provider +public class SolrRequestAuthorizer implements ContainerRequestFilter { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Context private ResourceInfo resourceInfo; + + public SolrRequestAuthorizer() { + log.info("Creating a new SolrRequestAuthorizer"); + } + + @SuppressWarnings("unchecked") + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + final CoreContainer coreContainer = + (CoreContainer) requestContext.getProperty(RequestContextConstants.CORE_CONTAINER_KEY); + final HttpServletRequest servletRequest = + (HttpServletRequest) + requestContext.getProperty(RequestContextConstants.HTTP_SERVLET_REQ_KEY); + final HttpServletResponse servletResponse = + (HttpServletResponse) + requestContext.getProperty(RequestContextConstants.HTTP_SERVLET_RSP_KEY); + final AuthorizationContext.RequestType requestType = + (AuthorizationContext.RequestType) + requestContext.getProperty(RequestContextConstants.REQUEST_TYPE_KEY); + final List<String> collectionNames = + (List<String>) requestContext.getProperty(RequestContextConstants.COLLECTION_LIST_KEY); + final SolrParams solrParams = + (SolrParams) requestContext.getProperty(RequestContextConstants.SOLR_PARAMS_KEY); + + /* + * HttpSolrCall has more involved logic to check whether a request requires authorization, but most of that + * revolves around checking for (1) static paths (e.g. index.html) or (2) HttpSolrCall 'action's that don't need + * authorization (e.g. request-forwarding) + * + * Since we don't invoke Jersey code in those particular cases we can ignore those checks here. + */ + if (coreContainer.getAuthorizationPlugin() == null) { + return; + } + final AuthorizationContext authzContext = + getAuthzContext(servletRequest, requestType, collectionNames, solrParams, coreContainer); + log.debug("Attempting authz with context {}", authzContext); + AuthorizationUtils.AuthorizationFailure authzFailure = + AuthorizationUtils.authorize(servletRequest, servletResponse, coreContainer, authzContext); + if (authzFailure != null) { + final Response failureResponse = + Response.status(authzFailure.getStatusCode()).entity(authzFailure.getMessage()).build(); + requestContext.abortWith(failureResponse); + } + } + + private AuthorizationContext getAuthzContext( + HttpServletRequest servletRequest, + AuthorizationContext.RequestType reqType, + List<String> collectionNames, + SolrParams solrParams, + CoreContainer cores) { Review Comment: *[UnusedVariable](https://errorprone.info/bugpattern/UnusedVariable):* The parameter 'cores' is never read. --- ```suggestion getAuthzContext(servletRequest, requestType, collectionNames, solrParams); ``` --- <details><summary><b>âšī¸ Learn about @sonatype-lift commands</b></summary> You can reply with the following commands. For example, reply with ***@sonatype-lift ignoreall*** to leave out all findings. | **Command** | **Usage** | | ------------- | ------------- | | `@sonatype-lift ignore` | Leave out the above finding from this PR | | `@sonatype-lift ignoreall` | Leave out all the existing findings from this PR | | `@sonatype-lift exclude <file\|issue\|path\|tool>` | Exclude specified `file\|issue\|path\|tool` from Lift findings by updating your config.toml file | **Note:** When talking to LiftBot, you need to **refresh** the page to see its response. <sub>[Click here](https://github.com/apps/sonatype-lift/installations/new) to add LiftBot to another repo.</sub></details> --- Was this a good recommendation? [ [đ Not relevant](https://www.sonatype.com/lift-comment-rating?comment=328914606&lift_comment_rating=1) ] - [ [đ Won't fix](https://www.sonatype.com/lift-comment-rating?comment=328914606&lift_comment_rating=2) ] - [ [đ Not critical, will fix](https://www.sonatype.com/lift-comment-rating?comment=328914606&lift_comment_rating=3) ] - [ [đ Critical, will fix](https://www.sonatype.com/lift-comment-rating?comment=328914606&lift_comment_rating=4) ] - [ [đ Critical, fixing now](https://www.sonatype.com/lift-comment-rating?comment=328914606&lift_comment_rating=5) ] -- 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: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org