This is an automated email from the ASF dual-hosted git repository.
xiaoyu 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 b40664d4a [type:refactor] added more test and fixed checkParam (#4275)
b40664d4a is described below
commit b40664d4aa0d0b5e389aecf072d3f25144b80024
Author: 愿凌飞 <[email protected]>
AuthorDate: Mon Dec 19 11:10:32 2022 +0800
[type:refactor] added more test and fixed checkParam (#4275)
* [type:refactor] added more test and fixed check checkParam for
CryptorRequestPlugin
* fix code style
* fix
---
.../http/combination/CryptorRequestPluginTest.java | 141 ++++++++++++++++-----
.../cryptor/request/CryptorRequestPlugin.java | 2 +-
.../cryptor/response/CryptorResponsePlugin.java | 6 +-
.../shenyu/plugin/cryptor/utils/CryptorUtil.java | 32 +++++
.../shenyu/plugin/cryptor/utils/JsonUtil.java | 19 ---
5 files changed, 148 insertions(+), 52 deletions(-)
diff --git
a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/CryptorRequestPluginTest.java
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/CryptorRequestPluginTest.java
index 0afb02f60..e0763b4fe 100644
---
a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/CryptorRequestPluginTest.java
+++
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/CryptorRequestPluginTest.java
@@ -17,6 +17,7 @@
package org.apache.shenyu.integrated.test.http.combination;
+import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import org.apache.shenyu.common.dto.ConditionData;
import org.apache.shenyu.common.enums.OperatorEnum;
@@ -24,23 +25,30 @@ import org.apache.shenyu.common.enums.ParamTypeEnum;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.utils.JsonUtils;
import org.apache.shenyu.integratedtest.common.AbstractPluginDataInit;
+import org.apache.shenyu.integratedtest.common.dto.AdminResponse;
import org.apache.shenyu.integratedtest.common.dto.UserDTO;
import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
import org.apache.shenyu.plugin.cryptor.handler.CryptorRuleHandler;
import org.apache.shenyu.plugin.cryptor.strategy.RsaStrategy;
import org.apache.shenyu.web.controller.LocalPluginController.RuleLocalData;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
-import java.util.ArrayList;
+import java.util.Base64;
import java.util.Collections;
import java.util.List;
-import java.util.Base64;
+import java.util.Objects;
-import static org.hamcrest.Matchers.is;
+import static
org.apache.shenyu.plugin.api.result.ShenyuResultEnum.DECRYPTION_ERROR;
+import static
org.apache.shenyu.plugin.api.result.ShenyuResultEnum.ENCRYPTION_ERROR;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
public final class CryptorRequestPluginTest extends AbstractPluginDataInit {
@@ -58,6 +66,8 @@ public final class CryptorRequestPluginTest extends
AbstractPluginDataInit {
private static final RsaStrategy RSA_STRATEGY = new RsaStrategy();
+ private final UserDTO originalBody = new UserDTO(TEST_USER_ID,
TEST_USER_NAME);
+
@BeforeEach
public void setup() throws IOException {
String pluginResult = initPlugin(PluginEnum.CRYPTOR_REQUEST.getName(),
null);
@@ -65,37 +75,108 @@ public final class CryptorRequestPluginTest extends
AbstractPluginDataInit {
}
@Test
- public void testDecryptRequest() throws Exception {
- String selectorAndRulesResult =
initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(),
+ @DisplayName("decrypt")
+ public void testDecrypt() throws Exception {
+ initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(),
"", buildSelectorConditionList(),
buildRuleLocalDataList("data", "decrypt"));
- assertThat(selectorAndRulesResult, is("success"));
-
- JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("userId", TEST_USER_ID);
- jsonObject.addProperty("userName", TEST_USER_NAME);
JsonObject request = new JsonObject();
- request.addProperty("data", RSA_STRATEGY.encrypt(RSA_PUBLIC_KEY,
jsonObject.toString()));
+ request.addProperty("data", RSA_STRATEGY.encrypt(RSA_PUBLIC_KEY,
JsonUtils.toJson(originalBody)));
+
UserDTO actualUser = HttpHelper.INSTANCE.postGateway(TEST_PATH,
request, UserDTO.class);
- assertThat(actualUser.getUserId(), is(TEST_USER_ID));
- assertThat(actualUser.getUserName(), is(TEST_USER_NAME));
+ assertThat(actualUser.getUserId(), is(originalBody.getUserId()));
+ assertThat(actualUser.getUserName(), is(originalBody.getUserName()));
+
}
@Test
+ @DisplayName("encrypt")
public void testEncryptRequest() throws Exception {
String selectorAndRulesResult =
initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(),
"", buildSelectorConditionList(),
buildRuleLocalDataList("userId", "encrypt"));
assertThat(selectorAndRulesResult, is("success"));
- JsonObject request = new JsonObject();
- request.addProperty("userId", TEST_USER_ID);
- request.addProperty("userName", TEST_USER_NAME);
- UserDTO actualUser = HttpHelper.INSTANCE.postGateway(TEST_PATH,
request, UserDTO.class);
+ UserDTO actualUser = HttpHelper.INSTANCE.postGateway(TEST_PATH,
originalBody, UserDTO.class);
byte[] inputByte =
Base64.getMimeDecoder().decode(actualUser.getUserId());
assertThat(RSA_STRATEGY.decrypt(RSA_PRIVATE_KEY, inputByte),
is(TEST_USER_ID));
assertThat(actualUser.getUserName(), is(TEST_USER_NAME));
}
+ @Test
+ @DisplayName("skip this plugin when rule handle is null")
+ public void testWhenRuleHandleIsNull() throws Exception {
+ initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(),
+ "", buildSelectorConditionList(),
Lists.newArrayList(buildRuleLocalData(null)));
+ UserDTO actualUser = HttpHelper.INSTANCE.postGateway(TEST_PATH,
originalBody, UserDTO.class);
+
+ assertThat(actualUser.getUserId(), is(originalBody.getUserId()));
+ assertThat(actualUser.getUserName(), is(originalBody.getUserName()));
+
+ }
+
+ @Test
+ @DisplayName("return failed message when request doesnt exist filed")
+ public void testWhenRequestBodyIsNull() throws Exception {
+ initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(),
+ "", buildSelectorConditionList(),
buildRuleLocalDataList("data", "decrypt"));
+
+ AdminResponse response = HttpHelper.INSTANCE.postGateway(TEST_PATH,
new JsonObject(), AdminResponse.class);
+
+ assertThat(response.getCode(), is(-114));
+ assertThat(response.getMessage(), is("Please check Cryptor request
plugin's [fieldNames]"));
+ }
+
+ @DisplayName("return failed message when decrypt or encrypt failed")
+ @ParameterizedTest(name = "return failed message when {0} failed")
+ @ValueSource(strings = {"decrypt", "encrypt"})
+ public void testWhenDecryptionOrEncryptionIsFailed(final String way)
throws Exception {
+
+ CryptorRuleHandler handler = buildRuleHandler("ras", way,
"wrong_encrypt_key", "wrong_decrypt_key", "data");
+ RuleLocalData ruleLocalData = buildRuleLocalData(handler);
+
+ initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(), "",
buildSelectorConditionList(), Lists.newArrayList(ruleLocalData));
+
+ JsonObject request = new JsonObject();
+ request.addProperty("data", "random_data");
+
+ AdminResponse response = HttpHelper.INSTANCE.postGateway(TEST_PATH,
request, AdminResponse.class);
+ ShenyuResultEnum resultEnum = "decrypt".equals(way) ? DECRYPTION_ERROR
: ENCRYPTION_ERROR;
+ assertThat(response.getCode(), is(resultEnum.getCode()));
+ assertThat(response.getMessage(), is(resultEnum.getMsg()));
+ }
+
+ @DisplayName("return failed message when key is null")
+ @ParameterizedTest(name = "return failed message when {0}-key is null")
+ @ValueSource(strings = {"decrypt", "encrypt"})
+ public void testWhenKeyIsNull(final String way) throws Exception {
+
+ CryptorRuleHandler handler = buildRuleHandler("ras", way, null, null,
"data");
+ RuleLocalData ruleLocalData = buildRuleLocalData(handler);
+
+ initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(), "",
buildSelectorConditionList(), Lists.newArrayList(ruleLocalData));
+
+ JsonObject request = new JsonObject();
+ AdminResponse response = HttpHelper.INSTANCE.postGateway(TEST_PATH,
request, AdminResponse.class);
+
+ String keyName = "decrypt".equals(way) ? "decryptKey" : "encryptKey";
+ assertThat(response.getMessage(), is(String.format("Please check
Cryptor request plugin's [%s]", keyName)));
+ }
+
+ @DisplayName("return failed message when fieldNames is null")
+ @Test
+ public void testWhenFieldNamesIsNull() throws Exception {
+
+ CryptorRuleHandler handler = buildRuleHandler("ras", "decrypt",
RSA_PUBLIC_KEY, RSA_PRIVATE_KEY, null);
+ RuleLocalData ruleLocalData = buildRuleLocalData(handler);
+
+ initSelectorAndRules(PluginEnum.CRYPTOR_REQUEST.getName(), "",
buildSelectorConditionList(), Lists.newArrayList(ruleLocalData));
+
+ JsonObject request = new JsonObject();
+ AdminResponse response = HttpHelper.INSTANCE.postGateway(TEST_PATH,
request, AdminResponse.class);
+
+ assertThat(response.getMessage(), is(String.format("Please check
Cryptor request plugin's [%s]", "fieldNames")));
+ }
+
private List<ConditionData> buildSelectorConditionList() {
ConditionData conditionData = new ConditionData();
conditionData.setParamType(ParamTypeEnum.URI.getName());
@@ -105,22 +186,14 @@ public final class CryptorRequestPluginTest extends
AbstractPluginDataInit {
}
private List<RuleLocalData> buildRuleLocalDataList(final String
fieldNames, final String way) {
- List<RuleLocalData> ruleLocalDataList = new ArrayList<>();
- ruleLocalDataList.add(buildRuleLocalData(fieldNames, way));
- return ruleLocalDataList;
+ CryptorRuleHandler cryptorRuleHandler = buildRuleHandler("rsa", way,
RSA_PUBLIC_KEY, RSA_PRIVATE_KEY, fieldNames);
+ return Lists.newArrayList(buildRuleLocalData(cryptorRuleHandler));
}
- private RuleLocalData buildRuleLocalData(final String fieldNames, final
String way) {
+ private RuleLocalData buildRuleLocalData(final CryptorRuleHandler
cryptorRuleHandler) {
final RuleLocalData ruleLocalData = new RuleLocalData();
- CryptorRuleHandler cryptorRuleHandler = new CryptorRuleHandler();
- cryptorRuleHandler.setDecryptKey(RSA_PRIVATE_KEY);
- cryptorRuleHandler.setEncryptKey(RSA_PUBLIC_KEY);
- cryptorRuleHandler.setStrategyName("rsa");
- cryptorRuleHandler.setFieldNames(fieldNames);
- cryptorRuleHandler.setWay(way);
-
- ruleLocalData.setRuleHandler(JsonUtils.toJson(cryptorRuleHandler));
+ ruleLocalData.setRuleHandler(Objects.isNull(cryptorRuleHandler) ? null
: JsonUtils.toJson(cryptorRuleHandler));
ConditionData conditionData = new ConditionData();
conditionData.setParamType(ParamTypeEnum.URI.getName());
conditionData.setOperator(OperatorEnum.EQ.getAlias());
@@ -129,6 +202,16 @@ public final class CryptorRequestPluginTest extends
AbstractPluginDataInit {
return ruleLocalData;
}
+ private CryptorRuleHandler buildRuleHandler(final String strategyName,
final String way, final String encryptKey, final String decryptKey, final
String fieldNames) {
+ CryptorRuleHandler cryptorRuleHandler = new CryptorRuleHandler();
+ cryptorRuleHandler.setDecryptKey(decryptKey);
+ cryptorRuleHandler.setEncryptKey(encryptKey);
+ cryptorRuleHandler.setStrategyName(strategyName);
+ cryptorRuleHandler.setFieldNames(fieldNames);
+ cryptorRuleHandler.setWay(way);
+ return cryptorRuleHandler;
+ }
+
@AfterEach
public void clean() throws IOException {
String cleanResult =
cleanPluginData(PluginEnum.CRYPTOR_REQUEST.getName());
diff --git
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPlugin.java
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPlugin.java
index 03dd715ff..b4c5b306e 100644
---
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPlugin.java
+++
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPlugin.java
@@ -69,7 +69,7 @@ public class CryptorRequestPlugin extends
AbstractShenyuPlugin {
return chain.execute(exchange);
}
- Pair<Boolean, String> pair = JsonUtil.checkParam(ruleHandle);
+ Pair<Boolean, String> pair = CryptorUtil.checkParam(ruleHandle);
if (Boolean.TRUE.equals(pair.getLeft())) {
ShenyuResultEnum resultEnum =
ShenyuResultEnum.CRYPTOR_REQUEST_ERROR_CONFIGURATION;
return WebFluxResultUtils.failedResult(resultEnum.getCode(),
diff --git
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePlugin.java
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePlugin.java
index 5d54a591f..e93f92cc9 100644
---
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePlugin.java
+++
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePlugin.java
@@ -28,9 +28,9 @@ import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
import org.apache.shenyu.plugin.cryptor.decorator.CryptorResponseDecorator;
-import org.apache.shenyu.plugin.cryptor.handler.CryptorRuleHandler;
import
org.apache.shenyu.plugin.cryptor.handler.CryptorResponsePluginDataHandler;
-import org.apache.shenyu.plugin.cryptor.utils.JsonUtil;
+import org.apache.shenyu.plugin.cryptor.handler.CryptorRuleHandler;
+import org.apache.shenyu.plugin.cryptor.utils.CryptorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
@@ -52,7 +52,7 @@ public class CryptorResponsePlugin extends
AbstractShenyuPlugin {
LOG.error("Cryptor response rule configuration is null :{}",
rule.getId());
return chain.execute(exchange);
}
- Pair<Boolean, String> pair = JsonUtil.checkParam(ruleHandle);
+ Pair<Boolean, String> pair = CryptorUtil.checkParam(ruleHandle);
if (Boolean.TRUE.equals(pair.getLeft())) {
Object error = ShenyuResultWrap.error(exchange,
ShenyuResultEnum.CRYPTOR_RESPONSE_ERROR_CONFIGURATION.getCode(),
ShenyuResultEnum.CRYPTOR_RESPONSE_ERROR_CONFIGURATION.getMsg() + "[" +
pair.getRight() + "]", null);
diff --git
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/CryptorUtil.java
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/CryptorUtil.java
index b8bddd183..baed78db9 100644
---
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/CryptorUtil.java
+++
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/CryptorUtil.java
@@ -19,9 +19,12 @@ package org.apache.shenyu.plugin.cryptor.utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
+import org.apache.shenyu.plugin.cryptor.handler.CryptorRuleHandler;
import org.apache.shenyu.plugin.cryptor.strategy.CryptorStrategyFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@@ -76,4 +79,33 @@ public final class CryptorUtil {
Arrays.asList(fieldNames.split("\\.")));
return resultJe.toString();
}
+
+ /**
+ * check param.
+ *
+ * @param ruleHandle ruleHandle
+ * @return is null
+ */
+ public static Pair<Boolean, String> checkParam(final CryptorRuleHandler
ruleHandle) {
+
+ if (StringUtils.isEmpty(ruleHandle.getWay())) {
+ return Pair.of(true, "way");
+ }
+
+ if (StringUtils.isEmpty(ruleHandle.getStrategyName())) {
+ return Pair.of(true, "strategyName");
+ }
+
+ if (StringUtils.isEmpty(ruleHandle.getFieldNames())) {
+ return Pair.of(true, "fieldNames");
+ }
+
+ if (ruleHandle.getWay().equals(CryptorStrategyFactory.DECRYPT) &&
StringUtils.isEmpty(ruleHandle.getDecryptKey())) {
+ return Pair.of(true, "decryptKey");
+ }
+ if (ruleHandle.getWay().equals(CryptorStrategyFactory.ENCRYPT) &&
StringUtils.isEmpty(ruleHandle.getEncryptKey())) {
+ return Pair.of(true, "encryptKey");
+ }
+ return Pair.of(false, "");
+ }
}
diff --git
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/JsonUtil.java
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/JsonUtil.java
index d6038659e..d661bff16 100644
---
a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/JsonUtil.java
+++
b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/utils/JsonUtil.java
@@ -20,10 +20,7 @@ package org.apache.shenyu.plugin.cryptor.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.tuple.Pair;
import org.apache.shenyu.common.utils.GsonUtils;
-import org.apache.shenyu.plugin.cryptor.handler.CryptorRuleHandler;
import org.springframework.util.CollectionUtils;
import java.util.List;
@@ -63,22 +60,6 @@ public final class JsonUtil {
return str;
}
- /**
- * check param.
- * @param ruleHandle ruleHandle
- * @return is null
- */
- public static Pair<Boolean, String> checkParam(final CryptorRuleHandler
ruleHandle) {
- String json = GsonUtils.getGson().toJson(ruleHandle);
- Map<String, String> map = GsonUtils.getInstance().toObjectMap(json,
String.class);
- for (Map.Entry<String, String> entry : map.entrySet()) {
- if (StringUtils.isEmpty(map.get(entry.getKey()))) {
- return Pair.of(true, entry.getKey());
- }
- }
- return Pair.of(false, "");
- }
-
/**
* operate json.
* @param jsonElement jsonElement