coheigea commented on a change in pull request #681: URL: https://github.com/apache/cxf/pull/681#discussion_r447572519
########## File path: rt/features/logging/src/main/java/org/apache/cxf/ext/logging/MaskSensitiveHelper.java ########## @@ -0,0 +1,74 @@ +/** + * 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.cxf.ext.logging; + +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.cxf.message.Message; + +import static org.apache.commons.lang3.ObjectUtils.isEmpty; + +public class MaskSensitiveHelper { + private static final String ELEMENT_NAME_TEMPLATE = "-ELEMENT_NAME-"; + private static final String MATCH_PATTERN_XML = "<-ELEMENT_NAME->(.*?)</-ELEMENT_NAME->"; + private static final String MATCH_PATTERN_JSON = "\"-ELEMENT_NAME-\"[ \\t]*:[ \\t]*\"(.*?)\""; + private static final String REPLACEMENT_PATTERN_XML = "<-ELEMENT_NAME->XXX</-ELEMENT_NAME->"; + private static final String REPLACEMENT_PATTERN_JSON = "\"-ELEMENT_NAME-\": \"XXX\""; + + private static final String XML_CONTENT = "xml"; + private static final String HTML_CONTENT = "html"; + private static final String JSON_CONTENT = "json"; + + final List<String> sensitiveElementNames; + + public MaskSensitiveHelper(final List<String> sensitiveElementNames) { + this.sensitiveElementNames = sensitiveElementNames; + } + + public String maskSensitiveElements( + final Message message, + final String originalLogString) { + if (isEmpty(sensitiveElementNames)) { + return originalLogString; + } + String contentType = (String) message.get(Message.CONTENT_TYPE); + if (StringUtils.containsIgnoreCase(contentType, XML_CONTENT) + || StringUtils.containsIgnoreCase(contentType, HTML_CONTENT)) { + return applyExpression(originalLogString, MATCH_PATTERN_XML, REPLACEMENT_PATTERN_XML); + } else if (StringUtils.containsIgnoreCase(contentType, JSON_CONTENT)) { + return applyExpression(originalLogString, MATCH_PATTERN_JSON, REPLACEMENT_PATTERN_JSON); + } else { + return originalLogString; + } + } + + private String applyExpression( + final String originalLogString, + final String matchPatternTemplate, + final String replacementTemplate) { + String resultString = originalLogString; + for (final String elementName : sensitiveElementNames) { + final String matchPattern = matchPatternTemplate.replaceAll(ELEMENT_NAME_TEMPLATE, elementName); + final String replacement = replacementTemplate.replaceAll(ELEMENT_NAME_TEMPLATE, elementName); Review comment: Could we find a way to do this matching in the constructor so it doesn't have to be done every time the expression is evaluated? ########## File path: rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java ########## @@ -102,8 +110,12 @@ public void createExchangeId(Message message) { } } - protected String transform(final String originalLogString) { + protected String transform(final Message message, final String originalLogString) { return originalLogString; } + protected String maskSensitiveElements(final Message message, String originalLogString) { + return (new MaskSensitiveHelper(sensitiveElementNames)) Review comment: MaskSensitiveHelper is thread-safe, so it would be more performant only to create it once when sensitiveElementNames are set? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org