You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2021/09/13 02:13:25 UTC

[incubator-shenyu] branch master updated: [ISSUE #1979] Refactor integration test for sign plugin (#2058)

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/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 8a7a7b9  [ISSUE #1979] Refactor integration test for sign plugin (#2058)
8a7a7b9 is described below

commit 8a7a7b9a1217e8b72303fee89e2ed8d0913cd98d
Author: Kunshuai Zhu <jo...@gmail.com>
AuthorDate: Mon Sep 13 10:13:18 2021 +0800

    [ISSUE #1979] Refactor integration test for sign plugin (#2058)
    
    * Refactor integration test for sign plugin
    
    * Remove redundant lines
---
 .../common/AbstractPluginDataInit.java             | 37 ++++++++++
 .../shenyu-integrated-test-http/docker-compose.yml |  6 +-
 .../src/main/resources/admin-config/schema.sql     | 36 ---------
 .../test/http/combination/SignPluginTest.java      | 85 +++++++++++++++++++---
 .../shenyu/web/controller/AppAuthController.java   | 83 +++++++++++++++++++++
 5 files changed, 196 insertions(+), 51 deletions(-)

diff --git a/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/AbstractPluginDataInit.java b/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/AbstractPluginDataInit.java
index 7edc01e..7892220 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/AbstractPluginDataInit.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/AbstractPluginDataInit.java
@@ -17,6 +17,9 @@
 
 package org.apache.shenyu.integratedtest.common;
 
+import org.apache.shenyu.common.dto.AuthParamData;
+import org.apache.shenyu.common.dto.AuthPathData;
+import org.apache.shenyu.common.dto.AppAuthData;
 import org.apache.shenyu.common.dto.ConditionData;
 import org.apache.shenyu.common.dto.PluginData;
 import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
@@ -100,4 +103,38 @@ public class AbstractPluginDataInit extends AbstractTest {
     public static String cleanPluginData(final String pluginName) throws IOException {
         return HttpHelper.INSTANCE.getFromGateway("/shenyu/cleanPlugin?name=" + pluginName, String.class);
     }
+
+    /**
+     * Init auth data.
+     *
+     * @param appKey the appKey
+     * @param appSecret the appSecret
+     * @param paramDataList the paramDataList
+     * @param pathDataList the pathDataList
+     * @return the response string
+     * @throws IOException the io exception
+     */
+    public static String initAuthData(final String appKey, final String appSecret,
+                                      final List<AuthParamData> paramDataList,
+                                      final List<AuthPathData> pathDataList) throws IOException {
+        AppAuthData appAuthData = new AppAuthData();
+        appAuthData.setEnabled(true);
+        appAuthData.setOpen(true);
+        appAuthData.setAppKey(appKey);
+        appAuthData.setAppSecret(appSecret);
+        appAuthData.setParamDataList(paramDataList);
+        appAuthData.setPathDataList(pathDataList);
+        return HttpHelper.INSTANCE.postGateway("/shenyu/auth/saveOrUpdate", appAuthData, String.class);
+    }
+
+    /**
+     * Clean auth data.
+     *
+     * @param appKey the appKey
+     * @return the response string
+     * @throws IOException the io exception
+     */
+    public static String cleanAuthData(final String appKey) throws IOException {
+        return HttpHelper.INSTANCE.getFromGateway("/shenyu/auth/delete?appKey=" + appKey, String.class);
+    }
 }
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml b/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml
index 81c2e6a..39b5f32 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/docker-compose.yml
@@ -59,10 +59,6 @@ services:
     restart: always
     networks:
       - shenyu
-    volumes:
-      - type: bind
-        source: ./src/main/resources/admin-config/
-        target: /opt/sql/
     depends_on:
       shenyu-redis:
         condition: service_healthy
@@ -70,7 +66,7 @@ services:
       - 9095:9095
     environment:
       - SPRING_PROFILES_ACTIVE=h2
-      - "shenyu.database.init_script=sql-script/h2/schema.sql;file:/opt/sql/schema.sql"
+      - "shenyu.database.init_script=sql-script/h2/schema.sql"
     healthcheck:
       test: ["CMD-SHELL", "wget -q -O - http://shenyu-admin:9095/actuator/health | grep UP || exit 1"]
       timeout: 2s
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/src/main/resources/admin-config/schema.sql b/shenyu-integrated-test/shenyu-integrated-test-http/src/main/resources/admin-config/schema.sql
deleted file mode 100644
index b4f7b61..0000000
--- a/shenyu-integrated-test/shenyu-integrated-test-http/src/main/resources/admin-config/schema.sql
+++ /dev/null
@@ -1,36 +0,0 @@
--- noinspection SqlNoDataSourceInspectionForFile
-
--- 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.
-/** prepare rate limit **/
--- DELETE FROM plugin WHERE id = '4';
--- INSERT IGNORE INTO plugin (id, name, config, role, enabled, date_created, date_updated) VALUES ('4', 'rate_limiter', '{"mode":"standalone","master":"mymaster","url":"shenyu-redis:6379","password":"abc"}', 1, 1, '2018-06-23 10:26:37', '2021-06-12 13:10:09');
---
--- INSERT IGNORE INTO selector (id, plugin_id, name, match_mode, type, sort, handle, enabled, loged, continued, date_created, date_updated) VALUES ('1403700859048841216', '4', 'http-ratelimit-slidingwindow', 0, 1, 1, null, 1, 1, 1, '2021-06-12 13:08:59', '2021-06-12 13:08:59');
---
--- INSERT IGNORE INTO rule (id, selector_id, match_mode, name, enabled, loged, sort, handle, date_created, date_updated) VALUES ('1403701003857186816', '1403700859048841216', 0, 'http-ratelimit-slidingwindow', 1, 1, 1, '{"algorithmName":"slidingWindow","replenishRate":"1","burstCapacity":"1"}', '2021-06-12 13:09:33', '2021-06-12 13:09:33');
--- INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, param_name, param_value, date_created, date_updated) VALUES ('1403701003865575424', '1403701003857186816', 'uri', '=', '/', '/http/test/path/123', '2021-06-12 13:09:33', '2021-06-12 13:09:33');
--- INSERT IGNORE INTO selector_condition (id, selector_id, param_type, operator, param_name, param_value, date_created, date_updated) VALUES ('1403700859057229824', '1403700859048841216', 'uri', '=', '/', '/http/test/path/123', '2021-06-12 13:08:59', '2021-06-12 13:08:59');
-
-/** prepare sign **/
-UPDATE plugin SET enabled = 1 WHERE id = '1';
-INSERT IGNORE INTO selector (id, plugin_id, name, match_mode, type, sort, handle, enabled, loged, continued,date_created, date_updated) VALUES ('1412234272995270656', '1', 'http-sign', 0, 1, 1, null, 1, 1, 1,'2021-07-06 13:09:3','2021-07-06 13:09:33');
-INSERT IGNORE INTO rule (id, selector_id, match_mode, name, enabled, loged, sort,date_created, date_updated) VALUES ('1412234425227534336', '1412234272995270656', 0, 'http-sign', 1, 1, 1,'2021-07-06 13:09:3','2021-07-06 13:09:33');
-INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, param_name, param_value,date_created, date_updated) VALUES ('1412234425256894464', '1412234425227534336', 'uri', '=', '/', '/http/test/path/456','2021-07-06 13:09:3','2021-07-06 13:09:33');
-INSERT IGNORE INTO selector_condition (id, selector_id, param_type, operator, param_name, param_value,date_created, date_updated) VALUES ('1412234273020436480', '1412234272995270656', 'uri', '=', '/', '/http/test/path/456','2021-07-06 13:09:3','2021-07-06 13:09:33');
-INSERT IGNORE INTO app_auth(id,app_key,app_secret,open,enabled,date_created, date_updated) VALUES ('1412239238157819904','108C27175A2C43C1BC29B1E483D57E3D','061521A73DD94A3FA873C25D050685BB',1,1,'2021-07-06 13:09:3','2021-07-06 13:09:33');
-INSERT IGNORE INTO auth_param(id,auth_id,app_name,date_created, date_updated) VALUES ('1412246190984257536','1412239238157819904','http-sign','2021-07-06 13:09:3','2021-07-06 13:09:33');
-INSERT IGNORE INTO auth_path(id,auth_id,app_name,path,enabled,date_created, date_updated) VALUES ('1412255231079411712','1412239238157819904','http-sign','/http/test/path/456',1,'2021-07-06 13:09:3','2021-07-06 13:09:33');
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/SignPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/SignPluginTest.java
index 7974ae7..c5f27ef 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/SignPluginTest.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/SignPluginTest.java
@@ -19,56 +19,82 @@ package org.apache.shenyu.integrated.test.http.combination;
 
 import com.google.common.collect.Maps;
 import com.google.gson.reflect.TypeToken;
+import org.apache.shenyu.common.dto.AuthParamData;
+import org.apache.shenyu.common.dto.AuthPathData;
+import org.apache.shenyu.common.dto.ConditionData;
+import org.apache.shenyu.common.enums.OperatorEnum;
+import org.apache.shenyu.common.enums.ParamTypeEnum;
+import org.apache.shenyu.common.enums.PluginEnum;
 import org.apache.shenyu.common.utils.SignUtils;
-import org.apache.shenyu.integratedtest.common.AbstractTest;
+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.web.controller.PluginController.RuleLocalData;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.IOException;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 
+import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
 
-public class SignPluginTest extends AbstractTest {
+public final class SignPluginTest extends AbstractPluginDataInit {
+
+    private static final String APP_KEY = "108C27175A2C43C1BC29B1E483D57E3D";
+
+    private static final String APP_SECRET = "061521A73DD94A3FA873C25D050685BB";
+
+    @BeforeClass
+    public static void setup() throws IOException {
+        String authResult = initAuthData(APP_KEY, APP_SECRET, buildAuthParamDataList(), buildAuthPathDataList());
+        assertThat(authResult, is("success"));
+        String pluginResult = initPlugin(PluginEnum.SIGN.getName(), null);
+        assertThat(pluginResult, is("success"));
+        String selectorAndRulesResult = initSelectorAndRules(PluginEnum.SIGN.getName(), "", buildSelectorConditionList(), buildRuleLocalDataList());
+        assertThat(selectorAndRulesResult, is("success"));
+    }
 
     @Test
     public void testSign() throws Exception {
-        final String appKey = "108C27175A2C43C1BC29B1E483D57E3D";
-        final String appSecret = "061521A73DD94A3FA873C25D050685BB";
         final String path = "/http/test/path/456";
         final String testUrlPath = "/http/test/path/456?name=Lee";
         final String version = "1.0.0";
         String now = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+0")).toEpochMilli());
-        Map<String, Object> normalHeaders = buildHeadersMap(now, path, appKey, appSecret, version);
+        Map<String, Object> normalHeaders = buildHeadersMap(now, path, APP_KEY, APP_SECRET, version);
         UserDTO normalRespFuture = HttpHelper.INSTANCE.getFromGateway(testUrlPath, normalHeaders,
                 UserDTO.class);
         assertEquals("hello world", normalRespFuture.getUserName());
 
-        Map<String, Object> errorPathHeaders = buildHeadersMap(now, "errorPath", appKey, appSecret, version);
+        Map<String, Object> errorPathHeaders = buildHeadersMap(now, "errorPath", APP_KEY, APP_SECRET, version);
         AdminResponse<Object> rejectedErrorPathRespFuture = HttpHelper.INSTANCE.getFromGateway(testUrlPath,
                 errorPathHeaders,
                 new TypeToken<AdminResponse<Object>>() {
                 }.getType());
         assertEquals("signature value is error!", rejectedErrorPathRespFuture.getMessage());
 
-        Map<String, Object> errorAppKeyHeaders = buildHeadersMap(now, path, "ERRORKEY", appSecret, version);
+        Map<String, Object> errorAppKeyHeaders = buildHeadersMap(now, path, "ERRORKEY", APP_SECRET, version);
         AdminResponse<Object> rejectedErrorAKRespFuture = HttpHelper.INSTANCE.getFromGateway(testUrlPath,
                 errorAppKeyHeaders,
                 new TypeToken<AdminResponse<Object>>() {
                 }.getType());
         assertEquals("sign appKey does not exist.", rejectedErrorAKRespFuture.getMessage());
 
-        Map<String, Object> errorAppSecretHeaders = buildHeadersMap(now, path, appKey, "ERRORSECRET", version);
+        Map<String, Object> errorAppSecretHeaders = buildHeadersMap(now, path, APP_KEY, "ERRORSECRET", version);
         AdminResponse<Object> rejectedErrorSKRespFuture = HttpHelper.INSTANCE.getFromGateway(testUrlPath,
                 errorAppSecretHeaders,
                 new TypeToken<AdminResponse<Object>>() {
                 }.getType());
         assertEquals("signature value is error!", rejectedErrorSKRespFuture.getMessage());
 
-        Map<String, Object> errorVersionHeaders = buildHeadersMap(now, path, appKey, appSecret, "1.0.2");
+        Map<String, Object> errorVersionHeaders = buildHeadersMap(now, path, APP_KEY, APP_SECRET, "1.0.2");
         AdminResponse<Object> rejectedErrorVersionRespFuture = HttpHelper.INSTANCE.getFromGateway(testUrlPath,
                 errorVersionHeaders,
                 new TypeToken<AdminResponse<Object>>() {
@@ -76,7 +102,7 @@ public class SignPluginTest extends AbstractTest {
         assertEquals("signature value is error!", rejectedErrorVersionRespFuture.getMessage());
 
         String errorTime = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+0")).toEpochMilli() - 360000);
-        Map<String, Object> errorTimestampHeaders = buildHeadersMap(errorTime, path, appKey, appSecret, version);
+        Map<String, Object> errorTimestampHeaders = buildHeadersMap(errorTime, path, APP_KEY, APP_SECRET, version);
         AdminResponse<Object> rejectedErrorTimestampRespFuture = HttpHelper.INSTANCE.getFromGateway(testUrlPath,
                 errorTimestampHeaders,
                 new TypeToken<AdminResponse<Object>>() {
@@ -99,4 +125,43 @@ public class SignPluginTest extends AbstractTest {
         headers.put("version", version);
         return headers;
     }
+
+    private static List<AuthParamData> buildAuthParamDataList() {
+        AuthParamData authParamData = new AuthParamData();
+        authParamData.setAppName("http-sign");
+        authParamData.setAppParam("appParam");
+        return Collections.singletonList(authParamData);
+    }
+
+    private static List<AuthPathData> buildAuthPathDataList() {
+        AuthPathData authPathData = new AuthPathData();
+        authPathData.setAppName("http-sign");
+        authPathData.setPath("/http/test/path/456");
+        authPathData.setEnabled(true);
+        return Collections.singletonList(authPathData);
+    }
+
+    private static List<ConditionData> buildSelectorConditionList() {
+        ConditionData conditionData = new ConditionData();
+        conditionData.setParamType(ParamTypeEnum.URI.getName());
+        conditionData.setOperator(OperatorEnum.EQ.getAlias());
+        conditionData.setParamValue("/http/test/path/456");
+        return Collections.singletonList(conditionData);
+    }
+
+    private static List<RuleLocalData> buildRuleLocalDataList() {
+        final RuleLocalData ruleLocalData = new RuleLocalData();
+        ConditionData conditionData = new ConditionData();
+        conditionData.setParamType(ParamTypeEnum.URI.getName());
+        conditionData.setOperator(OperatorEnum.EQ.getAlias());
+        conditionData.setParamValue("/http/test/path/456");
+        ruleLocalData.setConditionDataList(Collections.singletonList(conditionData));
+        return Collections.singletonList(ruleLocalData);
+    }
+
+    @AfterClass
+    public static void clean() throws IOException {
+        cleanPluginData(PluginEnum.SIGN.getName());
+        cleanAuthData(APP_KEY);
+    }
 }
diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/controller/AppAuthController.java b/shenyu-web/src/main/java/org/apache/shenyu/web/controller/AppAuthController.java
new file mode 100644
index 0000000..84a1dad
--- /dev/null
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/controller/AppAuthController.java
@@ -0,0 +1,83 @@
+/*
+ * 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.web.controller;
+
+import org.apache.shenyu.common.dto.AppAuthData;
+import org.apache.shenyu.sync.data.api.AuthDataSubscriber;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.RequestBody;
+import reactor.core.publisher.Mono;
+
+/**
+ * The type AppAuth controller.
+ */
+@RestController
+@RequestMapping("/shenyu")
+public class AppAuthController {
+
+    /**
+     * logger.
+     */
+    private static final Logger LOG = LoggerFactory.getLogger(PluginController.class);
+
+    private static final String SUCCESS = "success";
+
+    private final AuthDataSubscriber subscriber;
+
+    /**
+     * Instantiates a new AppAuth controller.
+     *
+     * @param subscriber the subscriber
+     */
+    public AppAuthController(final AuthDataSubscriber subscriber) {
+        this.subscriber = subscriber;
+    }
+
+    /**
+     * Clean AppAuth data by appKey.
+     *
+     * @param appKey the appKey
+     * @return the mono
+     */
+    @GetMapping("/auth/delete")
+    public Mono<String> clean(final String appKey) {
+        LOG.info("delete apache shenyu local AppAuth data");
+        AppAuthData appAuthData = new AppAuthData();
+        appAuthData.setAppKey(appKey);
+        subscriber.unSubscribe(appAuthData);
+        return Mono.just(SUCCESS);
+    }
+
+    /**
+     * Save or update app auth data.
+     *
+     * @param appAuthData the app auth data
+     * @return the mono
+     */
+    @PostMapping("/auth/saveOrUpdate")
+    public Mono<String> saveOrUpdate(@RequestBody final AppAuthData appAuthData) {
+        LOG.info("saveOrUpdate apache shenyu local app auth");
+        subscriber.onSubscribe(appAuthData);
+        return Mono.just(SUCCESS);
+    }
+}