Changeset 2483

Show
Ignore:
Timestamp:
10/01/08 09:31:49 (3 months ago)
Author:
dsanders
Message:

Modified so that when logging request body we do not log usernames and passwords for authentication requests - or other authentication material values.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/otis/src/org/bandit/otis/server/OtisServlet.java

    r2481 r2483  
    5555 
    5656import org.w3c.dom.Document; 
     57import org.w3c.dom.Element; 
     58import org.w3c.dom.Node; 
     59import org.w3c.dom.NodeList; 
    5760 
    5861import org.eclipse.higgins.idas.registry.IdASRegistry; 
     
    6063import org.apache.commons.logging.Log; 
    6164import org.apache.commons.logging.LogFactory; 
     65 
     66import javax.xml.transform.dom.DOMSource; 
     67import java.io.StringWriter; 
     68import javax.xml.transform.stream.StreamResult; 
     69import javax.xml.transform.TransformerFactory; 
     70import javax.xml.transform.Transformer; 
     71import javax.xml.transform.OutputKeys; 
    6272 
    6373import java.io.ByteArrayOutputStream; 
     
    133143        private Map                                                             m_acceptMediaMap = null; 
    134144        private boolean                                         m_bAllowInsecureChannel = false; 
    135         protected static boolean                                m_bSanitizeStrings = false; 
     145        protected static boolean                        m_bSanitizeStrings = false; 
    136146        public static Map                                               m_attrs = new HashMap(); 
    137147        public static List                                      m_readByDefaultAttrList = null; 
     148        private Transformer                                     m_serializer = null; 
    138149 
    139150        public OtisServlet() 
     
    155166                { 
    156167                } 
     168                try 
     169                { 
     170                        m_serializer = TransformerFactory.newInstance().newTransformer(); 
     171                        m_serializer.setOutputProperty( OutputKeys.ENCODING, "ISO-8859-1"); 
     172                        m_serializer.setOutputProperty( OutputKeys.METHOD, "xml"); 
     173                        m_serializer.setOutputProperty( OutputKeys.INDENT, "yes"); 
     174                        m_serializer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes"); 
     175                } 
     176                catch (Exception e) 
     177                { 
     178                        m_log.debug( "Exception " + e.getClass().getName() + " initializing XML serializer: " + e.getMessage()); 
     179                } 
    157180        } 
    158181         
     
    170193                        responseMap.put( Constants.TEMPLATE_HTTP_RESPONSE_CODE,  Integer.valueOf( iHttpResponseCode));  
    171194                        responseMap.put( Constants.TEMPLATE_HTTP_RESPONSE_STATUS, errMsg); 
    172                 } 
    173                 if (e != null) 
    174                 { 
    175                         responseMap.put( Constants.TEMPLATE_EXCEPTION, e); 
    176                         otisException.setStackTrace(e.getStackTrace()); 
     195                        if (e != null) 
     196                        { 
     197                                responseMap.put( Constants.TEMPLATE_EXCEPTION, e); 
     198                        } 
    177199                } 
    178200                return( otisException); 
     
    186208                Map                     responseMap) throws OtisException 
    187209        { 
    188                 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
    189                 PrintStream ps = new PrintStream(bs); 
    190                 e.printStackTrace(ps); 
    191                 log.debug("Originating exception's stack:\n" + bs.toString()); 
     210                if (e != null) 
     211                { 
     212                        ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
     213                        PrintStream ps = new PrintStream(bs); 
     214                        e.printStackTrace(ps); 
     215                        log.debug("Originating exception's stack:\n" + bs.toString()); 
     216                } 
    192217                throw OtisServlet.getOtisException( log, errMsg, e, iHttpResponseCode, responseMap); 
    193218        } 
     
    11891214        } 
    11901215         
     1216        private String xmlToString( 
     1217                Element domElement) throws OtisException 
     1218        { 
     1219                try 
     1220                { 
     1221                        if (m_serializer != null) 
     1222                        { 
     1223                                StringWriter    strOut = new StringWriter( 1024); 
     1224                                m_serializer.transform( new DOMSource( domElement), new StreamResult( strOut)); 
     1225                                return( strOut.toString()); 
     1226                        } 
     1227                } 
     1228                catch (Throwable e) 
     1229                { 
     1230                } 
     1231                return( null); 
     1232        }        
     1233                 
     1234        private void logSanitizedRequestDoc( 
     1235                Document                requestDoc) 
     1236        { 
     1237                if (m_log.isDebugEnabled()) 
     1238                { 
     1239                        try 
     1240                        { 
     1241                                DocumentBuilderFactory  documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
     1242                                documentBuilderFactory.setNamespaceAware( true); 
     1243                                DocumentBuilder                 documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
     1244                                Document                                                newDoc = documentBuilder.newDocument(); 
     1245                                Node                                                    newNode = newDoc.importNode( requestDoc.getDocumentElement(), true); 
     1246                                 
     1247                                newDoc.appendChild( newNode); 
     1248                                 
     1249                                // Find all AuthMaterialValue elements. 
     1250                                 
     1251                                NodeList        nodes = newDoc.getElementsByTagNameNS( Constants.OTIS_NAMESPACE, Constants.OTIS_AUTH_MATERIAL_VALUE_ELEMENT); 
     1252                                 
     1253                                if (nodes != null && nodes.getLength() > 0) 
     1254                                { 
     1255                                        for (int iLoop = 0; iLoop < nodes.getLength(); iLoop++) 
     1256                                        { 
     1257                                                Node    nd = nodes.item( iLoop); 
     1258                                                Node    childNode = nd.getFirstChild(); 
     1259                                                 
     1260                                                // Remove all child elements 
     1261                                                 
     1262                                                while (childNode != null) 
     1263                                                { 
     1264                                                        nd.removeChild( childNode); 
     1265                                                        childNode = nd.getFirstChild(); 
     1266                                                } 
     1267                                                 
     1268                                                // Add a text element that blanks out the value 
     1269                                                 
     1270                                                nd.appendChild( newDoc.createTextNode( "*****")); 
     1271                                        } 
     1272                                } 
     1273                                 
     1274                                // Serialize the document and log it. 
     1275                                 
     1276                                String  strNewContent = xmlToString( newDoc.getDocumentElement()); 
     1277                                 
     1278                                if (strNewContent != null) 
     1279                                { 
     1280                                        m_log.debug( "REQUEST BODY:"); 
     1281                                        m_log.debug( strNewContent); 
     1282                                } 
     1283                        } 
     1284                        catch (Throwable e) 
     1285                        { 
     1286                        } 
     1287                } 
     1288        } 
     1289         
    11911290        public void doRequest( 
    11921291                HttpServletRequest      request, 
     
    12151314                m_log.debug( strHttpOperation); 
    12161315         
    1217                 m_log.trace("BEGIN REQUEST"); 
    1218                 m_log.trace( request.getMethod() + " " + strRequestURI); 
    1219                 while (enumHeaderNames.hasMoreElements()) 
    1220                 { 
    1221                         String  szHeaderName = (String)enumHeaderNames.nextElement(); 
    1222                         String  szHeader = (String)request.getHeader( szHeaderName); 
    1223                         m_log.trace( szHeaderName + ": " + szHeader); 
    1224                 } 
    1225                 m_log.trace("END REQUEST"); 
     1316                if (m_log.isTraceEnabled()) 
     1317                { 
     1318                        m_log.trace("BEGIN REQUEST"); 
     1319                        m_log.trace( request.getMethod() + " " + strRequestURI); 
     1320                        while (enumHeaderNames.hasMoreElements()) 
     1321                        { 
     1322                                String  szHeaderName = (String)enumHeaderNames.nextElement(); 
     1323                                String  szHeader = (String)request.getHeader( szHeaderName); 
     1324                                m_log.trace( szHeaderName + ": " + szHeader); 
     1325                        } 
     1326                        m_log.trace("END REQUEST"); 
     1327                } 
    12261328 
    12271329                // Validate accept header 
     
    12931395                                        if (buf2 != null) 
    12941396                                        { 
    1295                                                 m_log.debug( "REQUEST BODY: " + (new String( buf2))); 
    12961397                                                bufStream = new ByteArrayInputStream( buf2);  
    12971398                                                requestDoc = documentBuilder.parse( bufStream); 
     1399                                                logSanitizedRequestDoc( requestDoc); 
    12981400                                        } 
    12991401                                }