You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/09/02 10:35:05 UTC

[camel] 01/04: CAMEL-16895: Log masking optimized to detect if input is xml, json or key value and mask accordingly. Added unit test for user reporting issue.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit d577d5832d75337870a2bd1be450c89c81557c2c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Sep 1 09:28:43 2021 +0200

    CAMEL-16895: Log masking optimized to detect if input is xml,json or key value and mask accordingly. Added unit test for user reporting issue.
---
 .../DefaultMaskingFormatterLongJsonTest.java       | 37 ++++++++++
 .../camel/support/processor/long-json-masked.json  | 84 ++++++++++++++++++++++
 .../apache/camel/support/processor/long-json.json  | 84 ++++++++++++++++++++++
 .../support/processor/DefaultMaskingFormatter.java | 23 ++++--
 .../java/org/apache/camel/util/SensitiveUtils.java | 15 +++-
 5 files changed, 233 insertions(+), 10 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/processor/DefaultMaskingFormatterLongJsonTest.java b/core/camel-core/src/test/java/org/apache/camel/support/processor/DefaultMaskingFormatterLongJsonTest.java
new file mode 100644
index 0000000..f537a58
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/support/processor/DefaultMaskingFormatterLongJsonTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.camel.support.processor;
+
+import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class DefaultMaskingFormatterLongJsonTest {
+
+    @Test
+    public void testMaskLongJson() throws Exception {
+        DefaultMaskingFormatter formatter = new DefaultMaskingFormatter();
+
+        String in = IOHelper.loadText(DefaultMaskingFormatterLongJsonTest.class.getResourceAsStream("long-json.json"));
+        String out = formatter.format(in);
+
+        String expected
+                = IOHelper.loadText(DefaultMaskingFormatterLongJsonTest.class.getResourceAsStream("long-json-masked.json"));
+        Assertions.assertEquals(expected, out);
+    }
+
+}
diff --git a/core/camel-core/src/test/resources/org/apache/camel/support/processor/long-json-masked.json b/core/camel-core/src/test/resources/org/apache/camel/support/processor/long-json-masked.json
new file mode 100644
index 0000000..6f5798c
--- /dev/null
+++ b/core/camel-core/src/test/resources/org/apache/camel/support/processor/long-json-masked.json
@@ -0,0 +1,84 @@
+{
+  "qwjabrlanfssaPaymentID": 93974430,
+  "apiPortNumber": 0,
+  "pollerFrequency": 0,
+  "serviceCode": "QWERETEMKLJLANDD",
+  "extraData": {
+    "97280991": {
+      "pushToOriginator": true,
+      "uidID": 41189716,
+      "metaData": [
+        {
+          "Value": 1070,
+          "Name": "Amount"
+        },
+        {
+          "Value": "PHP82TK6MY",
+          "Name": "MpesaQwasdrtjknsNumber"
+        },
+        {
+          "Name": "Balance"
+        },
+        {
+          "Value": 20210825224512,
+          "Name": "TransactionDate"
+        },
+        {
+          "Value": 599726145860,
+          "Name": "PhoneNumber"
+        }
+      ],
+      "pokjytsAmountCharged": 1070,
+      "hardwareRequestId": null,
+      "ProductCode": "UJHANSNLWII",
+      "isTillNumber": true,
+      "uniqueID": "83859103",
+      "vouchercode": " ",
+      "paybillNumber": "431553",
+      "password": "xxxxx"
+    }
+  },
+  "oiusdftrPaymentStatus": 139,
+  "serviceUrl": "",
+  "lkjmnbsdcvfdertCurrencyCode": "YUM",
+  "wrapperUrl": "",
+  "wsdlFile": "",
+  "protocol": "default",
+  "sslEnabled": 0,
+  "numberOfSends": 0,
+  "invoiceNumber": "",
+  "erdfghytIsdgftgDate": "2021-08-25 22:45:20",
+  "lkjditgfJhsgruaCustomerName": "Customer",
+  "apiUserName": "",
+  "ojudhfgKaswsxsUUID": "1102967588",
+  "iuytredfHsdededAccountNumber": "4622785548",
+  "tyghfderrMode": [
+    "TUND_PUDF"
+  ],
+  "zxcvfgtrNsdfdfdMSISDN": "254726173860",
+  "apiFunctionName": "",
+  "kjhxcTransactionID": [
+    "PHP82TK6MY"
+  ],
+  "apiPassword": "xxxxx",
+  "kjhcvbnUUID": "1102967588",
+  "dfcxvbnClientCode": [
+    "SAFKE"
+  ],
+  "sslCertificatePath": "",
+  "isTokenService": 0,
+  "wsazxcvgbgthyujAmount": 1070,
+  "plsjdhfhPusherProcessorClass": "",
+  "requestOriginIDs": [
+    "21"
+  ],
+  "firstSend": "",
+  "maxNumberOfSends": 0,
+  "narration": "The service request is processed successfully.",
+  "hubCreationDate": "2021-08-25 22:45:20",
+  "allowsPolling": 1,
+  "serviceID": 3004,
+  "lastSend": "2021-08-25 19:45:20",
+  "nextSend": "2021-08-25 22:45:20",
+  "autoAcknowledgejfhtysidj": 0
+}
\ No newline at end of file
diff --git a/core/camel-core/src/test/resources/org/apache/camel/support/processor/long-json.json b/core/camel-core/src/test/resources/org/apache/camel/support/processor/long-json.json
new file mode 100644
index 0000000..4612724
--- /dev/null
+++ b/core/camel-core/src/test/resources/org/apache/camel/support/processor/long-json.json
@@ -0,0 +1,84 @@
+{
+  "qwjabrlanfssaPaymentID": 93974430,
+  "apiPortNumber": 0,
+  "pollerFrequency": 0,
+  "serviceCode": "QWERETEMKLJLANDD",
+  "extraData": {
+    "97280991": {
+      "pushToOriginator": true,
+      "uidID": 41189716,
+      "metaData": [
+        {
+          "Value": 1070,
+          "Name": "Amount"
+        },
+        {
+          "Value": "PHP82TK6MY",
+          "Name": "MpesaQwasdrtjknsNumber"
+        },
+        {
+          "Name": "Balance"
+        },
+        {
+          "Value": 20210825224512,
+          "Name": "TransactionDate"
+        },
+        {
+          "Value": 599726145860,
+          "Name": "PhoneNumber"
+        }
+      ],
+      "pokjytsAmountCharged": 1070,
+      "hardwareRequestId": null,
+      "ProductCode": "UJHANSNLWII",
+      "isTillNumber": true,
+      "uniqueID": "83859103",
+      "vouchercode": " ",
+      "paybillNumber": "431553",
+      "password": "secret"
+    }
+  },
+  "oiusdftrPaymentStatus": 139,
+  "serviceUrl": "",
+  "lkjmnbsdcvfdertCurrencyCode": "YUM",
+  "wrapperUrl": "",
+  "wsdlFile": "",
+  "protocol": "default",
+  "sslEnabled": 0,
+  "numberOfSends": 0,
+  "invoiceNumber": "",
+  "erdfghytIsdgftgDate": "2021-08-25 22:45:20",
+  "lkjditgfJhsgruaCustomerName": "Customer",
+  "apiUserName": "",
+  "ojudhfgKaswsxsUUID": "1102967588",
+  "iuytredfHsdededAccountNumber": "4622785548",
+  "tyghfderrMode": [
+    "TUND_PUDF"
+  ],
+  "zxcvfgtrNsdfdfdMSISDN": "254726173860",
+  "apiFunctionName": "",
+  "kjhxcTransactionID": [
+    "PHP82TK6MY"
+  ],
+  "apiPassword": "mysecret",
+  "kjhcvbnUUID": "1102967588",
+  "dfcxvbnClientCode": [
+    "SAFKE"
+  ],
+  "sslCertificatePath": "",
+  "isTokenService": 0,
+  "wsazxcvgbgthyujAmount": 1070,
+  "plsjdhfhPusherProcessorClass": "",
+  "requestOriginIDs": [
+    "21"
+  ],
+  "firstSend": "",
+  "maxNumberOfSends": 0,
+  "narration": "The service request is processed successfully.",
+  "hubCreationDate": "2021-08-25 22:45:20",
+  "allowsPolling": 1,
+  "serviceID": 3004,
+  "lastSend": "2021-08-25 19:45:20",
+  "nextSend": "2021-08-25 22:45:20",
+  "autoAcknowledgejfhtysidj": 0
+}
\ No newline at end of file
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultMaskingFormatter.java b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultMaskingFormatter.java
index 516a7f6..3d84f70 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultMaskingFormatter.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultMaskingFormatter.java
@@ -29,7 +29,8 @@ import org.apache.camel.spi.MaskingFormatter;
  */
 public class DefaultMaskingFormatter implements MaskingFormatter {
 
-    private static final Set<String> DEFAULT_KEYWORDS = new HashSet<>(Arrays.asList("passphrase", "password", "secretKey"));
+    private static final Set<String> DEFAULT_KEYWORDS
+            = new HashSet<>(Arrays.asList("passphrase", "password", "secretKey"));
     private Set<String> keywords;
     private boolean maskKeyValue;
     private boolean maskXmlElement;
@@ -60,16 +61,24 @@ public class DefaultMaskingFormatter implements MaskingFormatter {
             return source;
         }
 
+        // xml,json or key=value pairs is the formats supported
+        boolean xml = maskXmlElement && source.startsWith("<");
+        boolean json = maskJson && !xml && (source.startsWith("{") || source.startsWith("["));
+
         String answer = source;
-        if (maskKeyValue) {
-            answer = keyValueMaskPattern.matcher(answer).replaceAll("$1\"" + maskString + "\"");
-        }
-        if (maskXmlElement) {
+        if (xml) {
             answer = xmlElementMaskPattern.matcher(answer).replaceAll("$1" + maskString + "$3");
-        }
-        if (maskJson) {
+            if (maskKeyValue) {
+                // used for the attributes in the XML tags
+                answer = keyValueMaskPattern.matcher(answer).replaceAll("$1\"" + maskString + "\"");
+            }
+        } else if (json) {
             answer = jsonMaskPattern.matcher(answer).replaceAll("$1\"" + maskString + "\"");
+        } else if (maskKeyValue) {
+            // key=value paris
+            answer = keyValueMaskPattern.matcher(answer).replaceAll("$1\"" + maskString + "\"");
         }
+
         return answer;
     }
 
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/SensitiveUtils.java b/core/camel-util/src/main/java/org/apache/camel/util/SensitiveUtils.java
index f4aebd5..599359c 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/SensitiveUtils.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/SensitiveUtils.java
@@ -14,16 +14,17 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.camel.util;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Locale;
 import java.util.Set;
 
 public final class SensitiveUtils {
-    private static final Set<String> SENSITIVE_KEYS = new HashSet<>(
+
+    private static final Set<String> SENSITIVE_KEYS = Collections.unmodifiableSet(new HashSet<>(
             Arrays.asList(
                     // Generated by camel build tools - do NOT edit this list!
                     // SENSITIVE-KEYS: START
@@ -92,12 +93,19 @@ public final class SensitiveUtils {
                     "verificationcode",
                     "zookeeperpassword"
             // SENSITIVE-KEYS: END
-            ));
+            )));
 
     private SensitiveUtils() {
     }
 
     /**
+     * All the sensitive keys (unmodifiable) in lower-case
+     */
+    public static Set<String> getSensitiveKeys() {
+        return SENSITIVE_KEYS;
+    }
+
+    /**
      * Whether the given configuration property contains a sensitive a sensitive key (such as password, accesstoken,
      * etc.)
      *
@@ -113,4 +121,5 @@ public final class SensitiveUtils {
         text = StringHelper.replaceAll(text, "-", "");
         return SENSITIVE_KEYS.contains(text);
     }
+
 }