From 63a5ac4523379c81d82ffb1b9a8eabd74de6dd56 Mon Sep 17 00:00:00 2001 From: daizhenyu <1449308021@qq.com> Date: Wed, 13 Mar 2024 10:25:05 +0800 Subject: [PATCH] database write prohibition integration: mongodb, mysql Signed-off-by: daizhenyu <1449308021@qq.com> --- .../database-write-prohibition/action.yml | 38 ++ ...ase_write_prohibition_integration_test.yml | 95 +++++ .../common/constant/DatabaseConstant.java | 8 +- .../pom.xml | 15 - .../constant/DatabaseConstant.java | 48 +++ .../mariadb/MariaDbProhibitionTest.java | 191 ++++++++++ .../mongodb/MongoDbProhibitionTest.java | 165 +++++++++ .../mariadb-demo/pom.xml | 46 +++ .../demo/mariadb/MariadbApplication.java | 38 ++ .../controller/MariadbBatchSqlController.java | 232 +++++++++++++ .../MariadbPreparedSqlController.java | 244 +++++++++++++ .../MariadbStaticSqlController.java | 234 +++++++++++++ .../src/main/resources/application.properties | 3 + .../mongodb-demo/pom.xml | 46 +++ .../demo/mongodb/MongoDbApplication.java | 39 +++ .../mongodb/controller/MongoDbController.java | 327 ++++++++++++++++++ .../src/main/resources/application.properties | 2 + .../database-write-prohibition-test/pom.xml | 14 +- 18 files changed, 1765 insertions(+), 20 deletions(-) create mode 100644 .github/actions/common/database-write-prohibition/action.yml create mode 100644 .github/workflows/database_write_prohibition_integration_test.yml create mode 100644 sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/constant/DatabaseConstant.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/mariadb/MariaDbProhibitionTest.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/mongodb/MongoDbProhibitionTest.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/mariadb-demo/pom.xml create mode 100644 sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/MariadbApplication.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbBatchSqlController.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbPreparedSqlController.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbStaticSqlController.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/resources/application.properties create mode 100644 sermant-integration-tests/database-write-prohibition-test/mongodb-demo/pom.xml create mode 100644 sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/MongoDbApplication.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/controller/MongoDbController.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/resources/application.properties diff --git a/.github/actions/common/database-write-prohibition/action.yml b/.github/actions/common/database-write-prohibition/action.yml new file mode 100644 index 0000000000..76d77f54b0 --- /dev/null +++ b/.github/actions/common/database-write-prohibition/action.yml @@ -0,0 +1,38 @@ +name: "Common operations" +description: "do something common for database-write-prohibition plugin test" +runs: + using: "composite" + steps: + - name: Set up JDK ${{ env.javaVersion }} + uses: actions/setup-java@v3 + with: + java-version: ${{ env.javaVersion }} + distribution: 'adopt' + cache: maven + - name: get zookeeper from cache + uses: actions/cache@v3 + with: + path: apache-zookeeper-3.6.3-bin.tar.gz + key: ${{ runner.os }}-apache-zookeeper-3.6.3 + - name: run zookeeper + shell: bash + run: | + tar -zxf apache-zookeeper-3.6.3-bin.tar.gz + bash apache-zookeeper-3.6.3-bin/bin/zkServer.sh start apache-zookeeper-3.6.3-bin/conf/zoo_sample.cfg + - name: cache dependencies + uses: actions/cache@v3 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + - name: cache agent + uses: actions/cache@v3 + with: + path: sermant-agent-*/ + key: ${{ runner.os }}-agent-${{ github.run_id }} + - name: entry + uses: ./.github/actions/common/entry + with: + log-dir: ./logs/database-write-prohibition/common + key: ${{ runner.os }}-agent-${{ github.run_id }} \ No newline at end of file diff --git a/.github/workflows/database_write_prohibition_integration_test.yml b/.github/workflows/database_write_prohibition_integration_test.yml new file mode 100644 index 0000000000..94e9f90829 --- /dev/null +++ b/.github/workflows/database_write_prohibition_integration_test.yml @@ -0,0 +1,95 @@ +name: database-write-prohibition integration test +env: + sermantVersion: 1.0.0 +on: + push: + pull_request: + branches: + - '*' + paths: + - 'sermant-agentcore/**' + - 'sermant-integration-tests/database-write-prohibition-test/**' + - 'sermant-plugins/sermant-database-write-prohibition/**' + - '.github/workflows/database_write_prohibition_integration_test.yml' + - '.github/actions/common/database-write-prohibition/**' + - '.github/actions/scenarios/database-write-prohibition/**' +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-${{ github.head_ref }} + cancel-in-progress: true +jobs: + download-midwares-and-cache: + name: download midwares and cache + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: cache zookeeper + uses: actions/cache@v3 + with: + path: apache-zookeeper-3.6.3-bin.tar.gz + key: ${{ runner.os }}-apache-zookeeper-3.6.3 + restore-keys: | + ${{ runner.os }}-apache-zookeeper-3.6.3 + - name: download zookeeper + run: | + export ROOT_PATH=$(pwd) + bash ./sermant-integration-tests/scripts/tryDownloadMidware.sh zk + build-agent-and-cache: + name: build agent and cache + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 8 + uses: actions/setup-java@v3 + with: + java-version: '8' + distribution: 'adopt' + cache: maven + - name: cache agent + uses: actions/cache@v3 + with: + path: sermant-agent-*/ + key: ${{ runner.os }}-agent-${{ github.run_id }} + - name: package agent + run: | + sed -i '/sermant-backend/d' pom.xml + sed -i '/sermant-integration-tests/d' pom.xml + sed -i '/sermant-injector/d' pom.xml + mvn package -DskipTests -Ptest --file pom.xml + test-for-database-write-prohibition-mongodb: + name: Test for database-write-prohibition mongodb + runs-on: ubuntu-latest + needs: [build-agent-and-cache, download-midwares-and-cache] + strategy: + matrix: + include: + fail-fast: false + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 100 + - name: set java version to environment + run: | + echo "javaVersion=8" >> $GITHUB_ENV + - name: common operations + uses: ./.github/actions/common/database-write-prohibition + - name: database-write-prohibition-mongodb test for mongodbVersion=${{ matrix.mongodbVersion }} + uses: ./.github/actions/scenarios/database-write-prohibition/mongodb + test-for-database-write-prohibition-mysql: + name: Test for database-write-prohibition mysql + runs-on: ubuntu-latest + needs: [build-agent-and-cache, download-midwares-and-cache] + strategy: + matrix: + include: + fail-fast: false + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 100 + - name: set java version to environment + run: | + echo "javaVersion=8" >> $GITHUB_ENV + - name: common operations + uses: ./.github/actions/common/database-write-prohibition + - name: database-write-prohibition-mysql test for mysqlVersion=${{ matrix.mysqlVersion }} + uses: ./.github/actions/scenarios/database-write-prohibition/mysql \ 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 index 9f685cc326..e980acb9d2 100644 --- 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 @@ -29,14 +29,14 @@ public class DatabaseConstant { public static final String SQL_EXCEPTION_MESSAGE_PREFIX = "Database prohibit to write"; /** - * succeed to prohibit database status code + * fail to execute database write operation status code */ - public static final int SUCCEED_PROHIBITION_CODE = 100; + public static final String OPERATION_FAIL_CODE = "100"; /** - * failed to prohibit database status code + * succeed to execute database write operation status code */ - public static final int FAILED_PROHIBITION_CODE = 101; + public static final String OPERATION_SUCCEED_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 index 6bddaac98c..e0b57f5451 100644 --- 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 @@ -20,10 +20,6 @@ - - com.huaweicloud.sermant - database-write-prohibition-common - org.junit.jupiter junit-jupiter @@ -63,15 +59,4 @@ 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/constant/DatabaseConstant.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/constant/DatabaseConstant.java new file mode 100644 index 0000000000..c8d930f5fb --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/constant/DatabaseConstant.java @@ -0,0 +1,48 @@ +/* + * 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.constant; + +/** + * common constant + * + * @author daizhenyu + * @since 2024-03-11 + **/ +public class DatabaseConstant { + /** + * fail to execute database write operation status code + */ + public static final String OPERATION_FAIL_CODE = "100"; + + /** + * succeed to execute database write operation status code + */ + public static final String OPERATION_SUCCEED_CODE = "101"; + + /** + * database table data count + */ + public static final String DATA_COUNT = "1"; + + /** + * line separator + */ + public static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + private DatabaseConstant() { + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/mariadb/MariaDbProhibitionTest.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/mariadb/MariaDbProhibitionTest.java new file mode 100644 index 0000000000..ad69131862 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/mariadb/MariaDbProhibitionTest.java @@ -0,0 +1,191 @@ +/* + * 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.mariadb; + +import com.huaweicloud.sermant.database.prohibition.integration.constant.DatabaseConstant; +import com.huaweicloud.sermant.database.prohibition.integration.utils.DynamicConfigUtils; +import com.huaweicloud.sermant.database.prohibition.integration.utils.HttpRequestUtils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +/** + * mariadb write prohibition integration test + * + * @author daizhenyu + * @since 2024-03-12 + **/ +@EnabledIfSystemProperty(named = "database.write.prohibition.integration.test.type", matches = "MARIADB") +public class MariaDbProhibitionTest { + private static final String MARIADB_CONFIG_ON = + "enableMySqlWriteProhibition: true" + DatabaseConstant.LINE_SEPARATOR + + "mySqlDatabases:" + DatabaseConstant.LINE_SEPARATOR + + " - test"; + + private static final String MARIADB_CONFIG_OFF = + "enableMySqlWriteProhibition: false" + DatabaseConstant.LINE_SEPARATOR + + "mySqlDatabases:" + DatabaseConstant.LINE_SEPARATOR + + " - test"; + + @BeforeAll + public static void before() throws Exception { + DynamicConfigUtils.updateConfig(MARIADB_CONFIG_OFF); + Thread.sleep(3000); + + // prepare test data + HttpRequestUtils.doGet("http://127.0.0.1:9098/static/createTable?table=tableSelect"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/static/insert?table=tableSelect"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/static/createTable?table=tableDrop"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/static/createTable?table=tableData"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/static/insert?table=tableData"); + + DynamicConfigUtils.updateConfig(MARIADB_CONFIG_ON); + Thread.sleep(3000); + } + + /** + * select + */ + @Test + public void testSelect() { + Assertions.assertEquals(DatabaseConstant.DATA_COUNT, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/select?table=tableSelect")); + Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/select?table=tableSelect")); + Assertions.assertEquals(DatabaseConstant.DATA_COUNT, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/select?table=tableSelect")); + } + + /** + * create table + */ + @Test + public void testTable() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/createTable?table=tableCreate")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/createTable?table=tableCreate")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/createTable?table=tableCreate")); + } + + /** + * drop table + */ + @Test + public void testDropCollection() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/dropTable?table=tableDrop")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/dropTable?table=tableDrop")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/dropTable?table=tableDrop")); + } + + /** + * insert + */ + @Test + public void testInsert() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/insert?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/insert?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/insert?table=tableData")); + } + + /** + * update + */ + @Test + public void testUpdate() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/update?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/update?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/update?table=tableData")); + } + + /** + * delete + */ + @Test + public void testDelete() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/delete?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/delete?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/delete?table=tableData")); + } + + /** + * create index + */ + @Test + public void testCreateIndex() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/creatIndex?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/creatIndex?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/creatIndex?table=tableData")); + } + + /** + * delete index + */ + @Test + public void testDeleteIndex() throws Exception { + DynamicConfigUtils.updateConfig(MARIADB_CONFIG_OFF); + Thread.sleep(3000); + + Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/creatIndex?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/creatIndex?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/creatIndex?table=tableData")); + + DynamicConfigUtils.updateConfig(MARIADB_CONFIG_ON); + Thread.sleep(3000); + + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/dropIndex?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/dropIndex?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/dropIndex?table=tableData")); + } + + /** + * alter table + */ + @Test + public void testAlterTable() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/static/alterTable?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/batch/alterTable?table=tableData")); + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/prepared/alterTable?table=tableData")); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/mongodb/MongoDbProhibitionTest.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/mongodb/MongoDbProhibitionTest.java new file mode 100644 index 0000000000..a155a64cd4 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/mongodb/MongoDbProhibitionTest.java @@ -0,0 +1,165 @@ +/* + * 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.mongodb; + +import com.huaweicloud.sermant.database.prohibition.integration.constant.DatabaseConstant; +import com.huaweicloud.sermant.database.prohibition.integration.utils.DynamicConfigUtils; +import com.huaweicloud.sermant.database.prohibition.integration.utils.HttpRequestUtils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +/** + * mongodb write prohibition integration test + * + * @author daizhenyu + * @since 2024-03-12 + **/ +@EnabledIfSystemProperty(named = "database.write.prohibition.integration.test.type", matches = "MONGODB") +public class MongoDbProhibitionTest { + private static final String MONGODB_CONFIG_ON = + "enableMongoDbWriteProhibition: true" + DatabaseConstant.LINE_SEPARATOR + + "mongoDbDatabases:" + DatabaseConstant.LINE_SEPARATOR + + " - test"; + + private static final String MONGODB_CONFIG_OFF = + "enableMongoDbWriteProhibition: false" + DatabaseConstant.LINE_SEPARATOR + + "mongoDbDatabases:" + DatabaseConstant.LINE_SEPARATOR + + " - test"; + + @BeforeAll + public static void before() throws Exception { + DynamicConfigUtils.updateConfig(MONGODB_CONFIG_OFF); + Thread.sleep(3000); + + // prepare test data + HttpRequestUtils.doGet("http://127.0.0.1:9098/createCollection?databaseName=test&collectionName=find"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/insert?databaseName=test&collectionName=find&fieldName" + + "=key1&value=value1"); + HttpRequestUtils + .doGet("http://127.0.0.1:9098/createCollection?databaseName=test&collectionName=drop"); + HttpRequestUtils + .doGet("http://127.0.0.1:9098/createCollection?databaseName=test&collectionName=data"); + HttpRequestUtils + .doGet("http://127.0.0.1:9098/insert?databaseName=test&collectionName=data"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/insert?databaseName=test&collectionName=data&fieldName" + + "=key1&value=value1"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/insert?databaseName=test&collectionName=data&fieldName" + + "=key1&value=value2"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/insert?databaseName=test&collectionName=data&fieldName" + + "=key1&value=value3"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/insert?databaseName=test&collectionName=data&fieldName" + + "=key2&value=value4"); + HttpRequestUtils.doGet("http://127.0.0.1:9098/createIndex?databaseName=test&collectionName=data&fieldName" + + "=key1"); + + DynamicConfigUtils.updateConfig(MONGODB_CONFIG_ON); + Thread.sleep(3000); + } + + /** + * find scene + */ + @Test + public void testFind() { + Assertions.assertEquals(DatabaseConstant.DATA_COUNT, HttpRequestUtils + .doGet("http://127.0.0.1:9098/find?databaseName=test&collectionName=find")); + Assertions.assertEquals(DatabaseConstant.DATA_COUNT, HttpRequestUtils + .doGet("http://127.0.0.1:9098/aggregate?databaseName=test&collectionName=find&fieldName" + + "=key1&value=value1")); + } + + /** + * create collection + */ + @Test + public void testCreateCollection() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/createCollection?databaseName=test&collectionName=create")); + } + + /** + * drop collection + */ + @Test + public void testDropCollection() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils + .doGet("http://127.0.0.1:9098/dropCollection?databaseName=test&collectionName=drop")); + } + + /** + * insert document + */ + @Test + public void testInsert() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, + HttpRequestUtils.doGet("http://127.0.0.1:9098/insert?" + + "databaseName=test&collectionName=data&fieldName=key1&value=value5")); + } + + /** + * update document + */ + @Test + public void testUpdate() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, + HttpRequestUtils.doGet("http://127.0.0.1:9098/update?" + + "databaseName=test&collectionName=data&fieldName=key1&value=value1")); + } + + /** + * replace document + */ + @Test + public void testReplace() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, + HttpRequestUtils.doGet("http://127.0.0.1:9098/replace?" + + "databaseName=test&collectionName=data&fieldName=key1&value=value2")); + } + + /** + * delete document + */ + @Test + public void testDelete() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, + HttpRequestUtils.doGet("http://127.0.0.1:9098/replace?" + + "databaseName=test&collectionName=data&fieldName=key1&value=value3")); + } + + /** + * create index + */ + @Test + public void testCreateIndex() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, + HttpRequestUtils.doGet("http://127.0.0.1:9098/createIndex?" + + "databaseName=test&collectionName=data&fieldName=key2")); + } + + /** + * delete index + */ + @Test + public void testDeleteIndex() { + Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, + HttpRequestUtils.doGet("http://127.0.0.1:9098/deleteIndex?" + + "databaseName=test&collectionName=data&fieldName=key1")); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/pom.xml b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/pom.xml new file mode 100644 index 0000000000..0d5e9e7a3c --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/pom.xml @@ -0,0 +1,46 @@ + + + + database-write-prohibition-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + mariadb-demo + + + 8 + 8 + 2.6.2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.mariadb.jdbc + mariadb-java-client + ${mariadb.version} + + + com.huaweicloud.sermant + database-write-prohibition-common + + + + + ${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/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/MariadbApplication.java b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/MariadbApplication.java new file mode 100644 index 0000000000..42f10e02bd --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/MariadbApplication.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.demo.mariadb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * springboot startup class + * + * @author daizhenyu + * @since 2024-03-11 + **/ +@SpringBootApplication +public class MariadbApplication { + /** + * startup class + * + * @param args process startup input parameters + */ + public static void main(String[] args) { + SpringApplication.run(MariadbApplication.class, args); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbBatchSqlController.java b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbBatchSqlController.java new file mode 100644 index 0000000000..1026d897fd --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbBatchSqlController.java @@ -0,0 +1,232 @@ +/* + * 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.demo.mariadb.controller; + +import com.huaweicloud.sermant.database.prohibition.common.constant.DatabaseConstant; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * operate mysql by batch sql + * + * @author daizhenyu + * @since 2024-03-12 + **/ +@RequestMapping("batch") +@RestController +public class MariadbBatchSqlController { + @Value("${mysql.address}") + private String mysqlAddress; + + /** + * createTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("createTable") + public String createTable(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String createTableQuery = + "CREATE TABLE " + table + " (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255)," + + " age INT)"; + statement.addBatch(createTableQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * dropTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("dropTable") + public String dropTable(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String dropTableQuery = "DROP TABLE IF EXISTS " + table; + statement.addBatch(dropTableQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * createIndex + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("creatIndex") + public String createIndex(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String createIndexQuery = "CREATE INDEX idx_name ON " + table + " (name)"; + statement.addBatch(createIndexQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * dropIndex + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("dropIndex") + public String dropIndex(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String dropIndexQuery = "DROP INDEX idx_name ON " + table; + statement.addBatch(dropIndexQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * alterTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("alterTable") + public String alterTable(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String alterTableQuery = "ALTER TABLE " + table + " ADD COLUMN address VARCHAR(255)"; + statement.addBatch(alterTableQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * insert + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("insert") + public String insert(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String insertQuery = "INSERT INTO " + table + " (name, age) VALUES ('John Doe', 25)"; + statement.addBatch(insertQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * update + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("update") + public String update(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String updateQuery = "UPDATE " + table + " SET age = 26 WHERE id = 1;"; + statement.addBatch(updateQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * delete + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("delete") + public String delete(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String deleteQuery = "DELETE FROM " + table + " WHERE id = 1"; + statement.addBatch(deleteQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * select + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("select") + public String select(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String selectQuery = "SELECT * FROM " + table; + statement.addBatch(selectQuery); + statement.executeBatch(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } +} \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbPreparedSqlController.java b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbPreparedSqlController.java new file mode 100644 index 0000000000..671621baab --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbPreparedSqlController.java @@ -0,0 +1,244 @@ +/* + * 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.demo.mariadb.controller; + +import com.huaweicloud.sermant.database.prohibition.common.constant.DatabaseConstant; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * operate mysql by prepared sql + * + * @author daizhenyu + * @since 2024-03-12 + **/ +@RequestMapping("prepared") +@RestController +public class MariadbPreparedSqlController { + private static final int PARAM_INDEX_FIRST = 1; + + private static final int PARAM_INDEX_SECOND = 2; + + private static final int TABLE_FIELD_AGE = 25; + + @Value("${mysql.address}") + private String mysqlAddress; + + /** + * createTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("createTable") + public String createTable(String table) { + String createTableQuery = + "CREATE TABLE " + table + " (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)"; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(createTableQuery); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * dropTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("dropTable") + public String dropTable(String table) { + String dropTableQuery = "DROP TABLE IF EXISTS " + table; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(dropTableQuery); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * createIndex + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("creatIndex") + public String createIndex(String table) { + String createIndexQuery = "CREATE INDEX idx_name ON " + table + " (name)"; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(createIndexQuery); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * dropIndex + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("dropIndex") + public String dropIndex(String table) { + String dropIndexQuery = "DROP INDEX idx_name ON " + table; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(dropIndexQuery); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * alterTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("alterTable") + public String alterTable(String table) { + String alterTableQuery = "ALTER TABLE " + table + " ADD COLUMN address VARCHAR(255)"; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(alterTableQuery); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * insert + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("insert") + public String insert(String table) { + String insertQuery = "INSERT INTO " + table + " (name, age) VALUES (?, ?)"; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(insertQuery); + statement.setString(PARAM_INDEX_FIRST, "John Doe"); + statement.setInt(PARAM_INDEX_SECOND, TABLE_FIELD_AGE); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * update + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("update") + public String update(String table) { + String updateQuery = "UPDATE " + table + " SET age = ? WHERE id = ?"; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(updateQuery); + statement.setInt(PARAM_INDEX_FIRST, TABLE_FIELD_AGE); + statement.setInt(PARAM_INDEX_SECOND, 1); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * delete + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("delete") + public String delete(String table) { + String deleteQuery = "DELETE FROM " + table + " WHERE id = ?"; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(deleteQuery); + statement.setInt(1, 1); + statement.executeUpdate(); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * select + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("select") + public int select(String table) { + int rowCount = 0; + String selectQuery = "SELECT * FROM " + table; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + PreparedStatement statement = connection.prepareStatement(selectQuery); + ResultSet resultSet = statement.executeQuery(); + rowCount = countRows(resultSet); + } catch (SQLException e) { + // ignore + } + return rowCount; + } + + private int countRows(ResultSet resultSet) throws SQLException { + int rowCount = 0; + if (resultSet != null) { + resultSet.last(); + rowCount = resultSet.getRow(); + resultSet.beforeFirst(); + } + return rowCount; + } +} \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbStaticSqlController.java b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbStaticSqlController.java new file mode 100644 index 0000000000..72f5789e70 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/java/com/huaweicloud/sermant/demo/mariadb/controller/MariadbStaticSqlController.java @@ -0,0 +1,234 @@ +/* + * 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.demo.mariadb.controller; + +import com.huaweicloud.sermant.database.prohibition.common.constant.DatabaseConstant; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * operate mysql by static sql + * + * @author daizhenyu + * @since 2024-03-12 + **/ +@RequestMapping("static") +@RestController +public class MariadbStaticSqlController { + @Value("${mysql.address}") + private String mysqlAddress; + + /** + * createTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("createTable") + public String createTable(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String createTableQuery = + "CREATE TABLE " + table + " (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255)," + + " age INT)"; + statement.execute(createTableQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * dropTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("dropTable") + public String dropTable(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String dropTableQuery = "DROP TABLE IF EXISTS " + table; + statement.executeUpdate(dropTableQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * createIndex + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("creatIndex") + public String createIndex(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String createIndexQuery = "CREATE INDEX idx_name ON " + table + " (name)"; + statement.executeUpdate(createIndexQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * dropIndex + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("dropIndex") + public String dropIndex(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String dropIndexQuery = "DROP INDEX idx_name ON " + table; + statement.executeUpdate(dropIndexQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * alterTable + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("alterTable") + public String alterTable(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String alterTableQuery = "ALTER TABLE " + table + " ADD COLUMN address VARCHAR(255)"; + statement.executeUpdate(alterTableQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * insert + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("insert") + public String insert(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String insertQuery = "INSERT INTO " + table + " (name, age) VALUES ('John Doe', 25)"; + statement.executeUpdate(insertQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * update + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("update") + public String update(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String updateQuery = "UPDATE " + table + " SET age = 26 WHERE id = 1"; + statement.executeUpdate(updateQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * delete + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("delete") + public String delete(String table) { + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String deleteQuery = "DELETE FROM " + table + " WHERE id = 1"; + statement.executeUpdate(deleteQuery); + } catch (SQLException e) { + if (e.getMessage().contains(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + /** + * select + * + * @param table table name + * @return int prohibition status code + */ + @RequestMapping("select") + public int select(String table) { + int rowCount = 0; + try (Connection connection = DriverManager.getConnection(mysqlAddress)) { + Statement statement = connection.createStatement(); + String selectQuery = "SELECT * FROM " + table; + ResultSet resultSet = statement.executeQuery(selectQuery); + rowCount = countRows(resultSet); + } catch (SQLException e) { + // ignore + } + return rowCount; + } + + private int countRows(ResultSet resultSet) throws SQLException { + int rowCount = 0; + if (resultSet != null) { + resultSet.last(); + rowCount = resultSet.getRow(); + resultSet.beforeFirst(); + } + return rowCount; + } +} \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/resources/application.properties b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/resources/application.properties new file mode 100644 index 0000000000..8c890a781a --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mariadb-demo/src/main/resources/application.properties @@ -0,0 +1,3 @@ +mysql.address=jdbc:mariadb://localhost:3306/test?user=root&password=&useUnicode=true&useSSL=false\ + &characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true +server.port=9098 \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/pom.xml b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/pom.xml new file mode 100644 index 0000000000..138119e170 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/pom.xml @@ -0,0 +1,46 @@ + + + + database-write-prohibition-test + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + mongodb-demo + + + 8 + 8 + 3.7.0 + + + + + org.mongodb + mongodb-driver-sync + ${mongodb.version} + + + org.springframework.boot + spring-boot-starter-web + + + com.huaweicloud.sermant + database-write-prohibition-common + + + + + ${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/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/MongoDbApplication.java b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/MongoDbApplication.java new file mode 100644 index 0000000000..0dce242c37 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/MongoDbApplication.java @@ -0,0 +1,39 @@ +/* + * 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.demo.mongodb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; + +/** + * springboot startup class + * + * @author daizhenyu + * @since 2024-03-11 + **/ +@SpringBootApplication(exclude = MongoAutoConfiguration.class) +public class MongoDbApplication { + /** + * startup class + * + * @param args process startup input parameters + */ + public static void main(String[] args) { + SpringApplication.run(MongoDbApplication.class, args); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/controller/MongoDbController.java b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/controller/MongoDbController.java new file mode 100644 index 0000000000..52a6638c33 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/java/com/huaweicloud/sermant/demo/mongodb/controller/MongoDbController.java @@ -0,0 +1,327 @@ +/* + * 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.demo.mongodb.controller; + +import com.huaweicloud.sermant.database.prohibition.common.constant.DatabaseConstant; + +import com.mongodb.client.AggregateIterable; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Aggregates; +import com.mongodb.client.model.Filters; +import com.mongodb.client.model.IndexOptions; +import com.mongodb.client.model.Indexes; +import com.mongodb.client.model.UpdateOptions; + +import org.bson.Document; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.sql.SQLException; +import java.util.Arrays; + +import javax.annotation.PostConstruct; + +/** + * operate mongodb + * + * @author daizhenyu + * @since 2024-03-11 + **/ +@RestController +public class MongoDbController { + @Value("${mongodb.address}") + private String mongoDbAddress; + + private MongoClient mongoClient; + + /** + * mongoClient init + */ + @PostConstruct + public void init() { + mongoClient = MongoClients.create(mongoDbAddress); + } + + /** + * createCollection + * + * @param databaseName database name + * @param collectionName collection name + * @return int prohibition status code + */ + @RequestMapping("createCollection") + public String createCollection(String databaseName, String collectionName) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + createCollection(database, collectionName); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void createCollection(MongoDatabase database, String collectionName) throws SQLException { + database.createCollection(collectionName); + } + + /** + * dropCollection + * + * @param databaseName database name + * @param collectionName collection name + * @return int prohibition status code + */ + @RequestMapping("dropCollection") + public String dropCollection(String databaseName, String collectionName) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + dropCollection(database, collectionName); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void dropCollection(MongoDatabase database, String collectionName) throws SQLException { + MongoCollection collection = database.getCollection(collectionName); + collection.drop(); + } + + /** + * createIndex + * + * @param databaseName database name + * @param collectionName collection name + * @param fieldName field name + * @return int prohibition status code + */ + @RequestMapping("createIndex") + public String createIndex(String databaseName, String collectionName, String fieldName) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + createIndex(database, collectionName, fieldName); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void createIndex(MongoDatabase database, String collectionName, String fieldName) throws SQLException { + MongoCollection collection = database.getCollection(collectionName); + IndexOptions indexOptions = new IndexOptions().unique(true); + collection.createIndex(Indexes.ascending(fieldName), indexOptions); + } + + /** + * deleteIndex + * + * @param databaseName database name + * @param collectionName collection name + * @param fieldName field name + * @return int prohibition status code + */ + @RequestMapping("deleteIndex") + public String deleteIndex(String databaseName, String collectionName, String fieldName) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + deleteIndex(database, collectionName, fieldName); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void deleteIndex(MongoDatabase database, String collectionName, String fieldName) throws SQLException { + MongoCollection collection = database.getCollection(collectionName); + collection.dropIndex(Indexes.ascending(fieldName)); + } + + /** + * aggregateDocument + * + * @param databaseName database name + * @param collectionName collection name + * @param fieldName field name + * @param value field value + * @return int number of documents + */ + @RequestMapping("aggregate") + public int aggregate(String databaseName, String collectionName, String fieldName, String value) { + MongoDatabase database = mongoClient.getDatabase(databaseName); + MongoCollection collection = database.getCollection(collectionName); + AggregateIterable aggregate = collection.aggregate( + Arrays.asList(Aggregates.match(Filters.eq(fieldName, value))) + ); + int count = 0; + for (Document document : aggregate) { + count++; + } + return count; + } + + /** + * findDocument + * + * @param databaseName database name + * @param collectionName collection name + * @return int number of documents + */ + @RequestMapping("find") + public int findDocument(String databaseName, String collectionName) { + int documentCount = 0; + MongoDatabase database = mongoClient.getDatabase(databaseName); + MongoCollection collection = database.getCollection(collectionName); + FindIterable documents = collection.find(); + for (Document document : documents) { + documentCount++; + } + return documentCount; + } + + /** + * insertDocument + * + * @param databaseName database name + * @param collectionName collection name + * @param fieldName field name + * @param value field value + * @return int prohibition status code + */ + @RequestMapping("insert") + public String insertDocument(String databaseName, String collectionName, String fieldName, String value) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + insertDocument(database, collectionName, fieldName, value); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void insertDocument(MongoDatabase database, String collectionName, String fieldName, String value) + throws SQLException { + MongoCollection collection = database.getCollection(collectionName); + Document document = new Document(fieldName, value); + collection.insertOne(document); + } + + /** + * deleteDocument + * + * @param databaseName database name + * @param collectionName collection name + * @param fieldName field name + * @param value field value + * @return int prohibition status code + */ + @RequestMapping("delete") + public String deleteDocument(String databaseName, String collectionName, String fieldName, String value) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + deleteDocument(database, collectionName, fieldName, value); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void deleteDocument(MongoDatabase database, String collectionName, String fieldName, String value) + throws SQLException { + MongoCollection collection = database.getCollection(collectionName); + collection.deleteOne( + Filters.eq(fieldName, value) + ); + } + + /** + * deleteDocument + * + * @param databaseName database name + * @param collectionName collection name + * @param fieldName field name + * @param value field value + * @return int prohibition status code + */ + @RequestMapping("update") + public String updateDocument(String databaseName, String collectionName, String fieldName, String value) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + updateDocument(database, collectionName, fieldName, value); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void updateDocument(MongoDatabase database, String collectionName, String fieldName, String value) + throws SQLException { + MongoCollection collection = database.getCollection(collectionName); + collection.updateOne( + Filters.eq(fieldName, value), + new Document("$set", new Document(fieldName, "newValue")), + new UpdateOptions().upsert(true) + ); + } + + /** + * replaceDocument + * + * @param databaseName database name + * @param collectionName collection name + * @param fieldName field name + * @param value field value + * @return int prohibition status code + */ + @RequestMapping("replace") + public String replaceDocument(String databaseName, String collectionName, String fieldName, String value) { + try { + MongoDatabase database = mongoClient.getDatabase(databaseName); + replaceDocument(database, collectionName, fieldName, value); + } catch (SQLException e) { + if (e.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return DatabaseConstant.OPERATION_FAIL_CODE; + } + } + return DatabaseConstant.OPERATION_SUCCEED_CODE; + } + + private void replaceDocument(MongoDatabase database, String collectionName, String fieldName, String value) + throws SQLException { + MongoCollection collection = database.getCollection(collectionName); + collection.replaceOne(Filters.eq(fieldName, value), + new Document("$set", new Document(fieldName, "newValue"))); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/resources/application.properties b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/resources/application.properties new file mode 100644 index 0000000000..83f171e779 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/mongodb-demo/src/main/resources/application.properties @@ -0,0 +1,2 @@ +mongodb.address=mongodb://localhost:27017 +server.port=9098 \ 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 index 196179acd8..96e6f56976 100644 --- a/sermant-integration-tests/database-write-prohibition-test/pom.xml +++ b/sermant-integration-tests/database-write-prohibition-test/pom.xml @@ -14,9 +14,9 @@ com.huaweicloud.sermant database-write-prohibition-test pom + database-write-prohibition-common - database-write-prohibition-integration-test @@ -47,5 +47,17 @@ postgresql-demo + + mongodb-test + + mongodb-demo + + + + mysql-test + + mariadb-demo + + \ No newline at end of file