This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 5ad00ee859 fix(logging): isolate console desensitization state (#7075)
5ad00ee859 is described below
commit 5ad00ee85971c4e0f88ac7c5df5d83377f557603
Author: Liming Deng <[email protected]>
AuthorDate: Wed Sep 30 09:56:53 2026 +0800
fix(logging): isolate console desensitization state (#7075)
* fix(logging): isolate console desensitization state
* fix(logging): isolate console masking snapshots
---
.../logging/console/LoggingConsolePlugin.java | 72 +++++++++--------
.../console/entity/LoggingConsoleRuleHandle.java | 92 ++++++++++++++++++++++
.../handler/LoggingConsolePluginDataHandler.java | 6 +-
.../logging/console/LoggingConsolePluginTest.java | 55 +++++++++++++
.../LoggingConsolePluginDataHandlerTest.java | 86 ++++++++++++++++++++
5 files changed, 277 insertions(+), 34 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java
index 1f41bceaac..10ba1e9474 100644
---
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java
@@ -18,7 +18,6 @@
package org.apache.shenyu.plugin.logging.console;
import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.RuleData;
@@ -29,7 +28,7 @@ import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
import org.apache.shenyu.plugin.base.utils.MediaTypeUtils;
import org.apache.shenyu.plugin.logging.common.constant.GenericLoggingConstant;
-import org.apache.shenyu.plugin.logging.common.entity.CommonLoggingRuleHandle;
+import
org.apache.shenyu.plugin.logging.console.entity.LoggingConsoleRuleHandle;
import
org.apache.shenyu.plugin.logging.console.handler.LoggingConsolePluginDataHandler;
import
org.apache.shenyu.plugin.logging.desensitize.api.enums.DataDesensitizeEnum;
import org.apache.shenyu.plugin.logging.desensitize.api.matcher.KeyWordMatch;
@@ -64,7 +63,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
-import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.GZIPInputStream;
@@ -76,36 +74,35 @@ import java.util.zip.GZIPInputStream;
public class LoggingConsolePlugin extends AbstractShenyuPlugin {
private static final Logger LOG =
LoggerFactory.getLogger(LoggingConsolePlugin.class);
-
- private static String dataDesensitizeAlg =
DataDesensitizeEnum.CHARACTER_REPLACE.getDataDesensitizeAlg();
+
+ private static final KeyWordMatch EMPTY_KEY_WORD_MATCH = new
KeyWordMatch(Collections.emptySet());
@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final
ShenyuPluginChain chain,
final SelectorData selector, final RuleData
rule) {
- CommonLoggingRuleHandle commonLoggingRuleHandle =
LoggingConsolePluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
- Set<String> keywordSets = Sets.newHashSet();
+ String cacheKey = CacheKeyUtils.INST.getKey(rule);
+ LoggingConsoleRuleHandle ruleHandle =
LoggingConsolePluginDataHandler.CACHED_HANDLE.get().obtainHandle(cacheKey);
boolean desensitized = Boolean.FALSE;
- KeyWordMatch keyWordMatch = new KeyWordMatch(Collections.emptySet());
- if (Objects.nonNull(commonLoggingRuleHandle)) {
- String keywords = commonLoggingRuleHandle.getKeyword();
- desensitized = StringUtils.isNotBlank(keywords) &&
commonLoggingRuleHandle.getMaskStatus();
- if (desensitized) {
- Collections.addAll(keywordSets, keywords.split(";"));
- dataDesensitizeAlg =
Optional.ofNullable(commonLoggingRuleHandle.getMaskType()).orElse(DataDesensitizeEnum.MD5_ENCRYPT.getDataDesensitizeAlg());
- keyWordMatch = new KeyWordMatch(keywordSets);
- LOG.info("current plugin:{}, keyword:{},
dataDesensitizedAlg:{}", this.named(), keywords, dataDesensitizeAlg);
- }
+ KeyWordMatch keyWordMatch = EMPTY_KEY_WORD_MATCH;
+ String dataDesensitizeAlg =
DataDesensitizeEnum.CHARACTER_REPLACE.getDataDesensitizeAlg();
+ if (Objects.nonNull(ruleHandle) && ruleHandle.isDesensitized()) {
+ desensitized = true;
+ dataDesensitizeAlg = ruleHandle.getDataDesensitizeAlg();
+ keyWordMatch = ruleHandle.getKeyWordMatch();
+ LOG.info("current plugin:{}, keyword:{}, dataDesensitizedAlg:{}",
this.named(), ruleHandle.getKeyword(), dataDesensitizeAlg);
}
ServerHttpRequest request = exchange.getRequest();
//"Print Request Info: "
StringBuilder requestInfo = new
StringBuilder().append(System.lineSeparator());
- requestInfo.append(getRequestUri(request, desensitized, keyWordMatch))
- .append(getRequestMethod(request, desensitized,
keyWordMatch)).append(System.lineSeparator())
- .append(getRequestHeaders(request, desensitized,
keyWordMatch)).append(System.lineSeparator())
- .append(getQueryParams(request, desensitized,
keyWordMatch)).append(System.lineSeparator());
- final LoggingServerHttpResponse loggingServerHttpResponse = new
LoggingServerHttpResponse(exchange.getResponse(), requestInfo, desensitized,
keyWordMatch);
+ requestInfo.append(getRequestUri(request, desensitized, keyWordMatch,
dataDesensitizeAlg))
+ .append(getRequestMethod(request, desensitized, keyWordMatch,
dataDesensitizeAlg)).append(System.lineSeparator())
+ .append(getRequestHeaders(request, desensitized, keyWordMatch,
dataDesensitizeAlg)).append(System.lineSeparator())
+ .append(getQueryParams(request, desensitized, keyWordMatch,
dataDesensitizeAlg)).append(System.lineSeparator());
+ final LoggingServerHttpResponse loggingServerHttpResponse = new
LoggingServerHttpResponse(exchange.getResponse(), requestInfo,
+ desensitized, keyWordMatch, dataDesensitizeAlg);
try {
- return chain.execute(exchange.mutate().request(new
LoggingServerHttpRequest(request, requestInfo, desensitized, keyWordMatch))
+ return chain.execute(exchange.mutate().request(new
LoggingServerHttpRequest(request, requestInfo,
+ desensitized, keyWordMatch, dataDesensitizeAlg))
.response(loggingServerHttpResponse).build())
.doOnError(loggingServerHttpResponse::logError);
} catch (Exception e) {
@@ -124,7 +121,8 @@ public class LoggingConsolePlugin extends
AbstractShenyuPlugin {
return PluginEnum.LOGGING_CONSOLE.getName();
}
- private String getRequestMethod(final ServerHttpRequest request, final
Boolean desensitized, final KeyWordMatch keyWordMatch) {
+ private String getRequestMethod(final ServerHttpRequest request, final
Boolean desensitized,
+ final KeyWordMatch keyWordMatch, final
String dataDesensitizeAlg) {
// desensitize request method
String requestMethod = "";
if (Objects.nonNull(request.getMethod())) {
@@ -134,14 +132,16 @@ public class LoggingConsolePlugin extends
AbstractShenyuPlugin {
return "Request Method: " + requestMethod + System.lineSeparator();
}
- private String getRequestUri(final ServerHttpRequest request, final
Boolean desensitized, final KeyWordMatch keyWordMatch) {
+ private String getRequestUri(final ServerHttpRequest request, final
Boolean desensitized,
+ final KeyWordMatch keyWordMatch, final String
dataDesensitizeAlg) {
// desensitize request uri
String requestUri =
DataDesensitizeUtils.desensitizeSingleKeyword(desensitized,
GenericLoggingConstant.REQUEST_URI,
request.getURI().toString(), keyWordMatch, dataDesensitizeAlg);
return "Request Uri: " + requestUri + System.lineSeparator();
}
- private String getQueryParams(final ServerHttpRequest request, final
Boolean desensitized, final KeyWordMatch keyWordMatch) {
+ private String getQueryParams(final ServerHttpRequest request, final
Boolean desensitized,
+ final KeyWordMatch keyWordMatch, final
String dataDesensitizeAlg) {
MultiValueMap<String, String> params = request.getQueryParams();
StringBuilder logInfo = new StringBuilder();
if (!params.isEmpty()) {
@@ -158,12 +158,13 @@ public class LoggingConsolePlugin extends
AbstractShenyuPlugin {
return logInfo.toString();
}
- private String getRequestHeaders(final ServerHttpRequest request, final
Boolean desensitized, final KeyWordMatch keyWordMatch) {
+ private String getRequestHeaders(final ServerHttpRequest request, final
Boolean desensitized,
+ final KeyWordMatch keyWordMatch, final
String dataDesensitizeAlg) {
HttpHeaders headers = request.getHeaders();
final StringBuilder logInfo = new StringBuilder();
if (!headers.isEmpty()) {
logInfo.append("[Request Headers
Start]").append(System.lineSeparator());
- logInfo.append(getHeaders(headers, desensitized, keyWordMatch));
+ logInfo.append(getHeaders(headers, desensitized, keyWordMatch,
dataDesensitizeAlg));
logInfo.append("[Request Headers
End]").append(System.lineSeparator());
}
return logInfo.toString();
@@ -173,7 +174,8 @@ public class LoggingConsolePlugin extends
AbstractShenyuPlugin {
LOG.info(info);
}
- private String getHeaders(final HttpHeaders headers, final Boolean
desensitized, final KeyWordMatch keyWordMatch) {
+ private String getHeaders(final HttpHeaders headers, final Boolean
desensitized,
+ final KeyWordMatch keyWordMatch, final String
dataDesensitizeAlg) {
StringBuilder logInfo = new StringBuilder();
Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();
entrySet.forEach(entry -> {
@@ -194,13 +196,16 @@ public class LoggingConsolePlugin extends
AbstractShenyuPlugin {
private final Boolean desensitized;
private final KeyWordMatch keyWordMatch;
+
+ private final String dataDesensitizeAlg;
LoggingServerHttpRequest(final ServerHttpRequest delegate, final
StringBuilder logInfo,
- final Boolean desensitized, final
KeyWordMatch keyWordMatch) {
+ final Boolean desensitized, final
KeyWordMatch keyWordMatch, final String dataDesensitizeAlg) {
super(delegate);
this.logInfo = logInfo;
this.desensitized = desensitized;
this.keyWordMatch = keyWordMatch;
+ this.dataDesensitizeAlg = dataDesensitizeAlg;
}
@Override
@@ -235,14 +240,17 @@ public class LoggingConsolePlugin extends
AbstractShenyuPlugin {
private final Boolean desensitized;
private final KeyWordMatch keyWordMatch;
+
+ private final String dataDesensitizeAlg;
LoggingServerHttpResponse(final ServerHttpResponse delegate, final
StringBuilder logInfo,
- final Boolean desensitized, final
KeyWordMatch keyWordMatch) {
+ final Boolean desensitized, final
KeyWordMatch keyWordMatch, final String dataDesensitizeAlg) {
super(delegate);
this.logInfo = logInfo;
this.serverHttpResponse = delegate;
this.desensitized = desensitized;
this.keyWordMatch = keyWordMatch;
+ this.dataDesensitizeAlg = dataDesensitizeAlg;
this.logInfo.append(System.lineSeparator());
}
@@ -333,7 +341,7 @@ public class LoggingConsolePlugin extends
AbstractShenyuPlugin {
private String getResponseHeaders() {
return System.lineSeparator() + "[Response Headers Start]" +
System.lineSeparator()
- +
LoggingConsolePlugin.this.getHeaders(serverHttpResponse.getHeaders(),
desensitized, keyWordMatch)
+ +
LoggingConsolePlugin.this.getHeaders(serverHttpResponse.getHeaders(),
desensitized, keyWordMatch, dataDesensitizeAlg)
+ "[Response Headers End]" + System.lineSeparator();
}
}
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/entity/LoggingConsoleRuleHandle.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/entity/LoggingConsoleRuleHandle.java
new file mode 100644
index 0000000000..0483c7d246
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/entity/LoggingConsoleRuleHandle.java
@@ -0,0 +1,92 @@
+/*
+ * 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.shenyu.plugin.logging.console.entity;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.plugin.logging.common.entity.CommonLoggingRuleHandle;
+import
org.apache.shenyu.plugin.logging.desensitize.api.enums.DataDesensitizeEnum;
+import org.apache.shenyu.plugin.logging.desensitize.api.matcher.KeyWordMatch;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Objects;
+
+/**
+ * Immutable logging console rule configuration.
+ */
+public final class LoggingConsoleRuleHandle {
+
+ private final boolean desensitized;
+
+ private final String keyword;
+
+ private final String dataDesensitizeAlg;
+
+ private final KeyWordMatch keyWordMatch;
+
+ /**
+ * Create a logging console rule configuration.
+ *
+ * @param ruleHandle common logging rule configuration
+ */
+ public LoggingConsoleRuleHandle(final CommonLoggingRuleHandle ruleHandle) {
+ this.keyword = ruleHandle.getKeyword();
+ this.desensitized = StringUtils.isNotBlank(keyword) &&
Boolean.TRUE.equals(ruleHandle.getMaskStatus());
+ this.dataDesensitizeAlg = Objects.nonNull(ruleHandle.getMaskType())
+ ? ruleHandle.getMaskType() :
DataDesensitizeEnum.MD5_ENCRYPT.getDataDesensitizeAlg();
+ this.keyWordMatch = new KeyWordMatch(desensitized
+ ? new HashSet<>(Arrays.asList(keyword.split(";"))) :
Collections.emptySet());
+ }
+
+ /**
+ * Whether desensitization is enabled.
+ *
+ * @return true when enabled
+ */
+ public boolean isDesensitized() {
+ return desensitized;
+ }
+
+ /**
+ * Get configured keywords.
+ *
+ * @return configured keywords
+ */
+ public String getKeyword() {
+ return keyword;
+ }
+
+ /**
+ * Get the desensitization algorithm.
+ *
+ * @return desensitization algorithm
+ */
+ public String getDataDesensitizeAlg() {
+ return dataDesensitizeAlg;
+ }
+
+ /**
+ * Get the compiled keyword matcher.
+ *
+ * @return compiled keyword matcher
+ */
+ public KeyWordMatch getKeyWordMatch() {
+ return keyWordMatch;
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/handler/LoggingConsolePluginDataHandler.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/handler/LoggingConsolePluginDataHandler.java
index 001a521ea7..7bd28169db 100644
---
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/handler/LoggingConsolePluginDataHandler.java
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/handler/LoggingConsolePluginDataHandler.java
@@ -25,6 +25,7 @@ import
org.apache.shenyu.plugin.base.handler.PluginDataHandler;
import org.apache.shenyu.plugin.base.utils.BeanHolder;
import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
import org.apache.shenyu.plugin.logging.common.entity.CommonLoggingRuleHandle;
+import
org.apache.shenyu.plugin.logging.console.entity.LoggingConsoleRuleHandle;
import java.util.Optional;
import java.util.function.Supplier;
@@ -34,13 +35,13 @@ import java.util.function.Supplier;
*/
public class LoggingConsolePluginDataHandler implements PluginDataHandler {
- public static final Supplier<CommonHandleCache<String,
CommonLoggingRuleHandle>> CACHED_HANDLE = new
BeanHolder<>(CommonHandleCache::new);
+ public static final Supplier<CommonHandleCache<String,
LoggingConsoleRuleHandle>> CACHED_HANDLE = new
BeanHolder<>(CommonHandleCache::new);
@Override
public void handlerRule(final RuleData ruleData) {
Optional.ofNullable(ruleData.getHandle()).ifPresent(s -> {
CommonLoggingRuleHandle commonLoggingRuleHandle =
GsonUtils.getInstance().fromJson(s, CommonLoggingRuleHandle.class);
-
CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData),
commonLoggingRuleHandle);
+
CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), new
LoggingConsoleRuleHandle(commonLoggingRuleHandle));
});
}
@@ -53,4 +54,5 @@ public class LoggingConsolePluginDataHandler implements
PluginDataHandler {
public String pluginNamed() {
return PluginEnum.LOGGING_CONSOLE.getName();
}
+
}
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/test/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePluginTest.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/test/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePluginTest.java
index f864733708..3b24654022 100644
---
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/test/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePluginTest.java
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/test/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePluginTest.java
@@ -21,20 +21,30 @@ import org.apache.shenyu.common.dto.RuleData;
import org.apache.shenyu.common.dto.SelectorData;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import
org.apache.shenyu.plugin.logging.desensitize.api.enums.DataDesensitizeEnum;
+import
org.apache.shenyu.plugin.logging.desensitize.api.factory.DataDesensitizeFactory;
+import org.apache.shenyu.plugin.logging.desensitize.api.matcher.KeyWordMatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.http.MediaType;
+import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
+import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
@@ -85,6 +95,51 @@ public final class LoggingConsolePluginTest {
StepVerifier.create(result).expectSubscription().verifyComplete();
}
+ @Test
+ public void testDesensitizationAlgorithmIsIsolatedAcrossDecorators() {
+ final String body = "{\"password\":\"secret\"}";
+ final KeyWordMatch matcher = new
KeyWordMatch(Collections.singleton("password"));
+ final String md5Algorithm =
DataDesensitizeEnum.MD5_ENCRYPT.getDataDesensitizeAlg();
+ final String replaceAlgorithm =
DataDesensitizeEnum.CHARACTER_REPLACE.getDataDesensitizeAlg();
+ final String md5Value =
DataDesensitizeFactory.selectDesensitize("secret", md5Algorithm);
+ final String replaceValue =
DataDesensitizeFactory.selectDesensitize("secret", replaceAlgorithm);
+ final StringBuilder md5RequestLog = new StringBuilder();
+ final StringBuilder replaceRequestLog = new StringBuilder();
+ final LoggingConsolePlugin.LoggingServerHttpRequest md5Request = new
LoggingConsolePlugin.LoggingServerHttpRequest(
+
MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body),
+ md5RequestLog, true, matcher, md5Algorithm);
+ final LoggingConsolePlugin.LoggingServerHttpRequest replaceRequest =
new LoggingConsolePlugin.LoggingServerHttpRequest(
+
MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body),
+ replaceRequestLog, true, matcher, replaceAlgorithm);
+
+ Flux.merge(md5Request.getBody(), replaceRequest.getBody()).blockLast();
+
+ assertTrue(md5RequestLog.toString().contains(md5Value));
+ assertFalse(md5RequestLog.toString().contains(replaceValue));
+ assertTrue(replaceRequestLog.toString().contains(replaceValue));
+ assertFalse(replaceRequestLog.toString().contains(md5Value));
+
+ final StringBuilder md5ResponseLog = new StringBuilder();
+ final StringBuilder replaceResponseLog = new StringBuilder();
+ final MockServerHttpResponse md5Response = new
MockServerHttpResponse();
+ final MockServerHttpResponse replaceResponse = new
MockServerHttpResponse();
+ md5Response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
+
replaceResponse.getHeaders().setContentType(MediaType.APPLICATION_JSON);
+ final LoggingConsolePlugin.LoggingServerHttpResponse md5Decorator =
loggingConsolePlugin.new LoggingServerHttpResponse(
+ md5Response, md5ResponseLog, true, matcher, md5Algorithm);
+ final LoggingConsolePlugin.LoggingServerHttpResponse replaceDecorator
= loggingConsolePlugin.new LoggingServerHttpResponse(
+ replaceResponse, replaceResponseLog, true, matcher,
replaceAlgorithm);
+ final DataBuffer md5Body =
md5Response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
+ final DataBuffer replaceBody =
replaceResponse.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
+
+ Mono.when(md5Decorator.writeWith(Mono.just(md5Body)),
replaceDecorator.writeWith(Mono.just(replaceBody))).block();
+
+ assertTrue(md5ResponseLog.toString().contains(md5Value));
+ assertFalse(md5ResponseLog.toString().contains(replaceValue));
+ assertTrue(replaceResponseLog.toString().contains(replaceValue));
+ assertFalse(replaceResponseLog.toString().contains(md5Value));
+ }
+
@Test
public void testGetOrder() {
assertEquals(loggingConsolePlugin.getOrder(),
PluginEnum.LOGGING_CONSOLE.getCode());
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/test/java/org/apache/shenyu/plugin/logging/console/handler/LoggingConsolePluginDataHandlerTest.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/test/java/org/apache/shenyu/plugin/logging/console/handler/LoggingConsolePluginDataHandlerTest.java
new file mode 100644
index 0000000000..67dc9f9710
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/test/java/org/apache/shenyu/plugin/logging/console/handler/LoggingConsolePluginDataHandlerTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.shenyu.plugin.logging.console.handler;
+
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
+import
org.apache.shenyu.plugin.logging.console.entity.LoggingConsoleRuleHandle;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test cases for {@link LoggingConsolePluginDataHandler}.
+ */
+public final class LoggingConsolePluginDataHandlerTest {
+
+ private final LoggingConsolePluginDataHandler handler = new
LoggingConsolePluginDataHandler();
+
+ private final RuleData ruleData = new RuleData();
+
+ @BeforeEach
+ public void setUp() {
+ ruleData.setSelectorId("selector");
+ ruleData.setId("rule");
+
ruleData.setHandle("{\"keyword\":\"requestBody;responseBody\",\"maskStatus\":true}");
+ }
+
+ @AfterEach
+ public void tearDown() {
+ handler.removeRule(ruleData);
+ }
+
+ @Test
+ public void testCachesKeyWordMatchWithRule() {
+ handler.handlerRule(ruleData);
+ String cacheKey = CacheKeyUtils.INST.getKey(ruleData);
+ LoggingConsoleRuleHandle ruleHandle =
LoggingConsolePluginDataHandler.CACHED_HANDLE.get().obtainHandle(cacheKey);
+
+ assertNotNull(ruleHandle);
+ assertSame(ruleHandle,
LoggingConsolePluginDataHandler.CACHED_HANDLE.get().obtainHandle(cacheKey));
+ assertTrue(ruleHandle.getKeyWordMatch().matches("requestBody"));
+ }
+
+ @Test
+ public void testRemovesKeyWordMatchWithRule() {
+ handler.handlerRule(ruleData);
+ String cacheKey = CacheKeyUtils.INST.getKey(ruleData);
+
+ handler.removeRule(ruleData);
+
+
assertNull(LoggingConsolePluginDataHandler.CACHED_HANDLE.get().obtainHandle(cacheKey));
+ }
+
+ @Test
+ public void testDisabledMaskDoesNotCompileKeywords() {
+ ruleData.setHandle("{\"keyword\":\"[\",\"maskStatus\":false}");
+
+ assertDoesNotThrow(() -> handler.handlerRule(ruleData));
+ LoggingConsoleRuleHandle ruleHandle =
LoggingConsolePluginDataHandler.CACHED_HANDLE.get()
+ .obtainHandle(CacheKeyUtils.INST.getKey(ruleData));
+ assertNotNull(ruleHandle);
+ assertFalse(ruleHandle.isDesensitized());
+ }
+}