ProxyTrustingFilter.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.pentaho.ui.servlet |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2007 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/
package org.pentaho.ui.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
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.HttpSession;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.HttpSessionContextIntegrationFilter;
import org.acegisecurity.context.SecurityContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.core.session.BaseSession;
import org.pentaho.core.session.IPentahoSession;
import org.pentaho.core.system.PentahoSystem;
import com.pentaho.security.SecurityUtils;
import com.pentaho.security.UserDetailsRoleListService;
/**
*
* This servlet is used to filter Servlet requests coming from another server
* for processing and sets authentication for the user passed in by the
* parameter <b>_TRUST_USER_</b>. It then passes the request down the servlet
* chain to be serviced. Only requests coming from a trusted host will be
* authenticated. Implement the filter and setup the trusted hosts by editing
* the <b>web.xml</b> file as follows.
* <p>
*
* <pre>
*
* <filter>
* <filter-name>ProxyTrustingFilter</filter-name>
* <filter-class>com.pentaho.ui.servlet.ProxyTrustingFilter</filter-class>
* <init-param>
* <param-name>TrustedIpAddrs</param-name>
* <param-value>192.168.10.60,192.168.10.61</param-value>
* </init-param>
* </filter>
* </pre>
*
* In the above example, when a request coming from IP addresses 192.168.10.60
* and 192.168.10.61 has the parameter _TRUST_USER_=<i>name</i> set, tha user
* <i>name</i> will be authenticated.
*
* <p>
* NOTES:
* <p>
*
* It is easy to spoof the URL or IP address so this technique should only be
* used if the server running the filter is not accessible to users. For example
* if the BI Platform is hosted in a DMZ.
* <p>
*
* For this class to be useful, both Pentaho servers should be using the same
* database repository.
* <p>
* The sending server should be using the ProxyServlet enabled to generate the
* requests.
* <p>
*
* @see com.pentaho.test.servlet.ProxyServlet
* @author Doug Moran
*
*/
public class ProxyTrustingFilter implements Filter {
FilterConfig filterConfig;
String[] trustedIpAddrs = null;
private static final Log logger = LogFactory.getLog(ProxyTrustingFilter.class);
public Log getLogger() {
return logger;
}
public void init(FilterConfig filterConfiguration) throws ServletException {
this.filterConfig = filterConfiguration;
trustedIpAddrs = null;
String hostStr = filterConfig.getInitParameter("TrustedIpAddrs"); //$NON-NLS-1$
if (hostStr != null) {
StringTokenizer st = new StringTokenizer(hostStr, ","); //$NON-NLS-1$
List addrs = new ArrayList();
while (st.hasMoreTokens()) {
String tok = st.nextToken().trim();
if (tok.length() > 0) {
addrs.add(tok);
// getLogger().info(
// Messages.getString("ProxyTrustingFilter.DEBUG_0001_TRUSTING",
// tok ) ); //$NON-NLS-1$
}
}
if (addrs.size() > 0) { // Guarantee that its null or has at least 1
// element
trustedIpAddrs = (String[]) addrs.toArray(new String[0]);
}
}
}
boolean isTrusted(String addr) {
if (trustedIpAddrs != null) {
for (int i = 0; i < trustedIpAddrs.length; ++i) {
if (trustedIpAddrs[i].equals(addr)) {
return (true);
}
}
}
return (false);
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
// long startTime = System.currentTimeMillis();
if ((trustedIpAddrs != null) && (request instanceof HttpServletRequest)) {
HttpServletRequest req = (HttpServletRequest) request;
String remoteHost = req.getRemoteAddr();
if (isTrusted(remoteHost)) {
String name = request.getParameter("_TRUST_USER_"); //$NON-NLS-1$
if ((name != null) && (name.length() > 0)) {
PentahoSystem.systemEntryPoint();
try {
IPentahoSession userSession = null;
IPentahoSession existingSession = (IPentahoSession) req.getSession().getAttribute(
BaseSession.PENTAHO_SESSION_KEY);
UserDetailsRoleListService userDetailsRoleListService = PentahoSystem.getUserDetailsRoleListService();
if ((existingSession == null) && (userDetailsRoleListService != null)) {
HttpSession httpSession = req.getSession();
userSession = userDetailsRoleListService.getEffectiveUserSession(name);
Authentication auth = (Authentication) userSession.getAttribute(SecurityUtils.SESSION_PRINCIPAL);
httpSession.setAttribute(BaseSession.PENTAHO_SESSION_KEY, userSession);
/**
* definition of anonymous inner class
*/
SecurityContext authWrapper = new SecurityContext() {
private Authentication authentication;
public Authentication getAuthentication() {
return authentication;
};
public void setAuthentication(Authentication authentication) {
this.authentication = authentication;
};
}; // end anonymous inner class
authWrapper.setAuthentication(auth);
httpSession.setAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY,
authWrapper);
}
} finally {
PentahoSystem.systemExitPoint();
}
}
}
}
chain.doFilter(request, response);
// long stopTime = System.currentTimeMillis();
// getLogger().debug( Messages.getString(
// Messages.getString("ProxyTrustingFilter.DEBUG_0004_REQUEST_TIME"),
// String.valueOf( stopTime - startTime ) ) ); //$NON-NLS-1$
}
public void destroy() {
}
/**
* @param args
*/
public static void main(String[] args) {
}
}
The table below shows all metrics for ProxyTrustingFilter.java.




