From 71fcea73d79d7a47aae4cae1fc7453e721ee5112 Mon Sep 17 00:00:00 2001 From: hanbingleixue Date: Tue, 12 Mar 2024 16:07:29 +0800 Subject: [PATCH] Add a demo module for database write prohibition plugin integration testing Signed-off-by: hanbingleixue --- .../database-write-prohibition-common/pom.xml | 19 +++++ .../common/constant/DatabaseConstant.java | 43 +++++++++++ .../pom.xml | 77 +++++++++++++++++++ .../integration/utils/DynamicConfigUtils.java | 55 +++++++++++++ .../integration/utils/HttpRequestUtils.java | 68 ++++++++++++++++ .../opengauss-demo/pom.xml | 54 +++++++++++++ .../opengauss/OpengaussApplication.java | 38 +++++++++ .../src/main/resources/application.yml | 10 +++ .../database-write-prohibition-test/pom.xml | 51 ++++++++++++ .../postgresql-demo/pom.xml | 59 ++++++++++++++ .../postgresql/PostgresqlApplication.java | 38 +++++++++ .../src/main/resources/application.yml | 10 +++ sermant-integration-tests/pom.xml | 1 + 13 files changed, 523 insertions(+) create mode 100644 sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/pom.xml create mode 100644 sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/src/main/java/com/huaweicloud/sermant/database/prohibition/common/constant/DatabaseConstant.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml create mode 100644 sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/DynamicConfigUtils.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/HttpRequestUtils.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/opengauss-demo/pom.xml create mode 100644 sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/opengauss/OpengaussApplication.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml create mode 100644 sermant-integration-tests/database-write-prohibition-test/pom.xml create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-demo/pom.xml create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/postgresql/PostgresqlApplication.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/resources/application.yml diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/pom.xml b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/pom.xml new file mode 100644 index 0000000000..4f21fe51fa --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/pom.xml @@ -0,0 +1,19 @@ + + + + database-write-prohibition-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + database-write-prohibition-common + + + 8 + 8 + + + \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/src/main/java/com/huaweicloud/sermant/database/prohibition/common/constant/DatabaseConstant.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/src/main/java/com/huaweicloud/sermant/database/prohibition/common/constant/DatabaseConstant.java new file mode 100644 index 0000000000..9f685cc326 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-common/src/main/java/com/huaweicloud/sermant/database/prohibition/common/constant/DatabaseConstant.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.database.prohibition.common.constant; + +/** + * common constant + * + * @author daizhenyu + * @since 2024-03-11 + **/ +public class DatabaseConstant { + /** + * database write prohibition sqlexception message prefix + */ + public static final String SQL_EXCEPTION_MESSAGE_PREFIX = "Database prohibit to write"; + + /** + * succeed to prohibit database status code + */ + public static final int SUCCEED_PROHIBITION_CODE = 100; + + /** + * failed to prohibit database status code + */ + public static final int FAILED_PROHIBITION_CODE = 101; + + private DatabaseConstant() { + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml new file mode 100644 index 0000000000..6bddaac98c --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml @@ -0,0 +1,77 @@ + + + + database-write-prohibition-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + database-write-prohibition-integration-test + + + 8 + 8 + 4.3.0 + 4.5.13 + 1.2 + + + + + com.huaweicloud.sermant + database-write-prohibition-common + + + org.junit.jupiter + junit-jupiter + test + + + org.apache.curator + curator-recipes + ${curator.version} + test + + + org.apache.httpcomponents + httpclient + ${httpclient4x.version} + test + + + ch.qos.logback + logback-core + test + + + ch.qos.logback + logback-classic + test + + + org.slf4j + slf4j-api + test + + + commons-logging + commons-logging + ${commons-log.version} + test + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/DynamicConfigUtils.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/DynamicConfigUtils.java new file mode 100644 index 0000000000..914b9ff5f1 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/DynamicConfigUtils.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.database.prohibition.integration.utils; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.data.Stat; + +import java.nio.charset.StandardCharsets; + +/** + * Distribute tools for dynamic configuration + * + * @author daizhenyu + * @since 2024-03-12 + **/ +public class DynamicConfigUtils { + public static final String ZOOKEEPER_NODE_PATH = "/app=default&environment=/sermant.database.write.globalConfig"; + + private DynamicConfigUtils() { + } + + public static void updateConfig(String config) throws Exception { + updateConfig(ZOOKEEPER_NODE_PATH, config); + } + + public static void updateConfig(String path, String config) throws Exception { + CuratorFramework curator = CuratorFrameworkFactory.newClient("127.0.0.1:2181", + new ExponentialBackoffRetry(1000, 3)); + curator.start(); + Stat stat = curator.checkExists().forPath(path); + if (stat == null) { + curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, + config.getBytes(StandardCharsets.UTF_8)); + } else { + curator.setData().forPath(path, config.getBytes(StandardCharsets.UTF_8)); + } + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/HttpRequestUtils.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/HttpRequestUtils.java new file mode 100644 index 0000000000..2ce1d9712d --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/utils/HttpRequestUtils.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.database.prohibition.integration.utils; + +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** + * HTTP Request Tool Class + * + * @author daizhenyu + * @since 2024-03-12 + **/ +public class HttpRequestUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestUtils.class); + + private static final int SUCCESS_CODE = 200; + + private HttpRequestUtils() { + } + + /** + * send get request + * + * @param url hTTP request URL + * @return response Body + */ + public static String doGet(String url) { + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + RequestConfig requestConfig = RequestConfig.custom() + .build(); + HttpGet httpGet = new HttpGet(url); + httpGet.setConfig(requestConfig); + try (CloseableHttpResponse response = httpClient.execute(httpGet)) { + if (response.getStatusLine().getStatusCode() == SUCCESS_CODE) { + return EntityUtils.toString(response.getEntity()); + } + LOGGER.info("Request error, the message is: {}", EntityUtils.toString(response.getEntity())); + return ""; + } + } catch (IOException e) { + LOGGER.info("Request exception, the message is: {}", e.getMessage()); + return ""; + } + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/pom.xml b/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/pom.xml new file mode 100644 index 0000000000..df5f967cc0 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/pom.xml @@ -0,0 +1,54 @@ + + + + database-write-prohibition-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + opengauss-demo + + + 8 + 8 + 3.5.3 + 3.5.2 + 3.0.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.opengauss + opengauss-jdbc + ${opengauss.version} + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis.version} + + + com.baomidou + dynamic-datasource-spring-boot-starter + ${dynamic.version} + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/opengauss/OpengaussApplication.java b/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/opengauss/OpengaussApplication.java new file mode 100644 index 0000000000..f10eb080ba --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/opengauss/OpengaussApplication.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.database.prohibition.opengauss; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Springboot startup class + * + * @author zhp + * @since 2024-02-18 + **/ +@SpringBootApplication +public class OpengaussApplication { + /** + * startup method + * + * @param args process startup input parameter + */ + public static void main(String[] args) { + SpringApplication.run(OpengaussApplication.class, args); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml b/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml new file mode 100644 index 0000000000..3eb635c584 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml @@ -0,0 +1,10 @@ +server: + port: 8081 +spring: + datasource: + driver-class-name: org.opengauss.Driver + url: jdbc:opengauss://127.0.0.1:5432/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true + username: + password: + application: + name: opengauss-demo \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/pom.xml b/sermant-integration-tests/database-write-prohibition-test/pom.xml new file mode 100644 index 0000000000..196179acd8 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/pom.xml @@ -0,0 +1,51 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.7.15 + + + 4.0.0 + 1.0.0 + + com.huaweicloud.sermant + database-write-prohibition-test + pom + + database-write-prohibition-common + database-write-prohibition-integration-test + + + + 8 + 8 + + + + + + com.huaweicloud.sermant + database-write-prohibition-common + ${project.version} + + + + + + + opengauss-test + + opengauss-demo + + + + postgresql-test + + postgresql-demo + + + + \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/pom.xml b/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/pom.xml new file mode 100644 index 0000000000..22349682fd --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/pom.xml @@ -0,0 +1,59 @@ + + + + database-write-prohibition-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + postgresql-demo + + + 8 + 8 + 9.4.1210 + 3.5.3 + 3.5.2 + 3.1.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.postgresql + postgresql + ${postgresql.version} + + + com.huaweicloud.sermant + database-write-prohibition-common + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis.version} + + + com.baomidou + dynamic-datasource-spring-boot-starter + ${dynamic.version} + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/postgresql/PostgresqlApplication.java b/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/postgresql/PostgresqlApplication.java new file mode 100644 index 0000000000..5e0c8e7de9 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/postgresql/PostgresqlApplication.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed 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 com.huaweicloud.sermant.database.prohibition.postgresql; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Springboot startup class + * + * @author zhp + * @since 2024-02-18 + **/ +@SpringBootApplication +public class PostgresqlApplication { + /** + * startup method + * + * @param args process startup input parameter + */ + public static void main(String[] args) { + SpringApplication.run(PostgresqlApplication.class, args); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/resources/application.yml b/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/resources/application.yml new file mode 100644 index 0000000000..7348d3c7b4 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/resources/application.yml @@ -0,0 +1,10 @@ +server: + port: 8081 +spring: + datasource: + driver-class-name: org.postgresql.Driver + url: jdbc:postgresql://127.0.0.1:5432/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true + username: + password: + application: + name: postgresql-demo \ No newline at end of file diff --git a/sermant-integration-tests/pom.xml b/sermant-integration-tests/pom.xml index 186538431c..5ce51bd100 100644 --- a/sermant-integration-tests/pom.xml +++ b/sermant-integration-tests/pom.xml @@ -34,5 +34,6 @@ spring-test tag-transmission-test mq-consume-prohibition-test + database-write-prohibition-test