swagle commented on a change in pull request #2638: URL: https://github.com/apache/ozone/pull/2638#discussion_r711391417
########## File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/api/filters/ReconAdminFilter.java ########## @@ -0,0 +1,115 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.ozone.recon.api.filters; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.recon.ReconConfigKeys; +import org.apache.hadoop.ozone.OzoneConfigKeys; +import org.apache.hadoop.security.UserGroupInformation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.security.Principal; +import java.util.Collection; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ADMINISTRATORS_WILDCARD; + +/** + * Filter that can be applied to paths to only allow access by configured + * admins. + */ +@Singleton +public class ReconAdminFilter implements Filter { + + private static final Logger LOG = + LoggerFactory.getLogger(ReconAdminFilter.class); + + private final OzoneConfiguration conf; + + @Inject + ReconAdminFilter(OzoneConfiguration conf) { + this.conf = conf; + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { } + + @Override + public void doFilter(ServletRequest servletRequest, + ServletResponse servletResponse, FilterChain filterChain) + throws IOException, ServletException { + + HttpServletRequest httpServletRequest = + (HttpServletRequest) servletRequest; + HttpServletResponse httpServletResponse = + (HttpServletResponse) servletResponse; + + final Principal userPrincipal = + httpServletRequest.getUserPrincipal(); + boolean isAdmin = false; + String userName = null; + + if (userPrincipal != null) { + UserGroupInformation ugi = + UserGroupInformation.createRemoteUser(userPrincipal.getName()); + userName = ugi.getUserName(); + + if (hasPermission(ugi)) { + isAdmin = true; + filterChain.doFilter(httpServletRequest, httpServletResponse); + } + } + + if (isAdmin) { + if (LOG.isDebugEnabled()) { + LOG.debug("Allowing request from admin user {} to {}", + userName, httpServletRequest.getRequestURL()); + } + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("Rejecting request from non admin user {} to {}", Review comment: This is an audit event, we don't have recon audit log but the log level should be info/warn IMO. -- 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]
