You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2020/07/03 22:50:43 UTC

[GitHub] [cxf] ashakirin commented on a change in pull request #681: CXF-8099: mask sensitive logging elements

ashakirin commented on a change in pull request #681:
URL: https://github.com/apache/cxf/pull/681#discussion_r449715765



##########
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:
       I made some measurements year ago - there are no different in modern java with or without pre-compiled patterns - it is automatically optimized. However I find code with replaceAll more readable as with pattern. Can change it, but win is minimal




----------------------------------------------------------------
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:
users@infra.apache.org