From 712b84b0b27e0bf6875b7f209d812937a36ed24c Mon Sep 17 00:00:00 2001 From: hanbingleixue Date: Fri, 22 Mar 2024 16:04:30 +0800 Subject: [PATCH] Add test demo for database write prohibition plugin Signed-off-by: hanbingleixue --- .../opengauss/action.yml | 74 ++ .../postgresql/action.yml | 66 ++ ...ase_write_prohibition_integration_test.yml | 90 ++- .../common/constant/DatabaseConstant.java | 73 +- .../pom.xml | 7 + .../AbstractDatabaseProhibitionTest.java | 62 ++ .../constant/DatabaseConstant.java | 4 +- .../integration/entity/Result.java | 78 +++ .../BatchSqlExecuteTest.java | 135 ++++ .../PreparedSqlExecuteTest.java | 129 ++++ .../StaticSqlExecuteTest.java | 135 ++++ .../opengauss-demo/pom.xml | 54 -- .../src/main/resources/application.yml | 10 - .../database-write-prohibition-test/pom.xml | 10 +- .../postgresql/PostgresqlApplication.java | 38 - .../pom.xml | 33 +- .../DatabaseWriteProhibitionApplication.java} | 6 +- .../DatabaseWriteProhibitionController.java | 654 ++++++++++++++++++ .../database/prohibition/entity/Result.java | 78 +++ .../database/prohibition/entity/Students.java | 64 ++ .../handler/GlobalExceptionHandler.java | 54 ++ .../prohibition/mapper/DatabaseMapper.java | 92 +++ .../prohibition/mapper/StudentsMapper.java | 34 + .../src/main/resources/application.yml | 2 +- 24 files changed, 1851 insertions(+), 131 deletions(-) create mode 100644 .github/actions/scenarios/database-write-prohibition/opengauss/action.yml create mode 100644 .github/actions/scenarios/database-write-prohibition/postgresql/action.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/AbstractDatabaseProhibitionTest.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/entity/Result.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/postgresql_opengauss/BatchSqlExecuteTest.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/postgresql_opengauss/PreparedSqlExecuteTest.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/postgresql_opengauss/StaticSqlExecuteTest.java delete mode 100644 sermant-integration-tests/database-write-prohibition-test/opengauss-demo/pom.xml delete mode 100644 sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml delete mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/postgresql/PostgresqlApplication.java rename sermant-integration-tests/database-write-prohibition-test/{postgresql-demo => postgresql-opengauss-demo}/pom.xml (64%) rename sermant-integration-tests/database-write-prohibition-test/{opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/opengauss/OpengaussApplication.java => postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/DatabaseWriteProhibitionApplication.java} (84%) create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/controller/DatabaseWriteProhibitionController.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Result.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Students.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/handler/GlobalExceptionHandler.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/DatabaseMapper.java create mode 100644 sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/StudentsMapper.java rename sermant-integration-tests/database-write-prohibition-test/{postgresql-demo => postgresql-opengauss-demo}/src/main/resources/application.yml (88%) diff --git a/.github/actions/scenarios/database-write-prohibition/opengauss/action.yml b/.github/actions/scenarios/database-write-prohibition/opengauss/action.yml new file mode 100644 index 0000000000..babb8847e5 --- /dev/null +++ b/.github/actions/scenarios/database-write-prohibition/opengauss/action.yml @@ -0,0 +1,74 @@ +name: "Opengauss Database Write Prohibition Plugin Test" +description: "Auto test for opengauss database write prohibition" +runs: + using: composite + steps: + - name: entry + uses: ./.github/actions/common/entry + with: + log-dir: ./logs/database-write-prohibition/opengauss + - name: install opengauss + shell: bash + run: | + sudo apt-get update + sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - + sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" + sudo apt-get update + sudo apt-get install -y docker-ce docker-ce-cli containerd.io + docker run --name opengauss -d --privileged=true -e GS_PASSWORD=${{ env.datasourcePassword }} -p 5432:5432 enmotech/opengauss:3.0.0 + sleep 20s + - name: package demos + shell: bash + run: | + mvn package -Ddatabase.version=${{ matrix.opengaussVersion }} -Ddatabase.groupId=org.opengauss -Ddatabase.artifactId=opengauss-jdbc \ + -DskipTests -Ppostgresql-opengauss-test --file sermant-integration-tests/database-write-prohibition-test/pom.xml + - name: start opengauss(org.opengauss) demo + if: matrix.opengaussVersion == '3.0.0' || matrix.opengaussVersion == '3.0.5-og' || matrix.opengaussVersion == '3.1.0-og' || matrix.opengaussVersion =='3.1.1-og' + shell: bash + env: + SPRING_DATASOURCE_DRIVER_CLASS_NAME: org.opengauss.Driver + SPRING_DATASOURCE_URL: jdbc:opengauss://127.0.0.1:5432/postgres?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true + SPRING_DATASOURCE_USERNAME: gaussdb + SPRING_DATASOURCE_PASSWORD: ${{ env.datasourcePassword }} + run: | + nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar -jar \ + -Dsermant_log_dir=${{ env.logDir }}/opengauss \ + sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/target/postgresql-opengauss-demo.jar > ${{ env.logDir }}/opengauss.log 2>&1 & + - name: start opengauss(org.postgresql) demo + if: matrix.opengaussVersion == '3.1.0' || matrix.opengaussVersion =='3.1.1' || matrix.opengaussVersion == '3.0.5' + shell: bash + env: + SPRING_DATASOURCE_DRIVER_CLASS_NAME: org.postgresql.Driver + SPRING_DATASOURCE_URL: jdbc:postgresql://127.0.0.1:5432/postgres?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true + SPRING_DATASOURCE_USERNAME: gaussdb + SPRING_DATASOURCE_PASSWORD: ${{ env.datasourcePassword }} + run: | + nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar -jar \ + -Dsermant_log_dir=${{ env.logDir }}/opengauss \ + sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/target/postgresql-opengauss-demo.jar > ${{ env.logDir }}/opengauss.log 2>&1 & + - name: waiting for services start + shell: bash + run: | + ps -ef | grep java + bash ./sermant-integration-tests/scripts/checkService.sh http://127.0.0.1:8081/checkStatus 120 + - name: test opengauss + shell: bash + run: | + mvn test -Ddatabase.prohibition.integration.test.type=POSTGRESQL_OPENGAUSS --file \ + sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml + - name: exit + if: always() + uses: ./.github/actions/common/exit + with: + processor-keyword: postgresql + - name: if failure then upload error log + uses: actions/upload-artifact@v3 + if: ${{ failure() || cancelled() }} + with: + name: (${{ github.job }})-database-write-prohibition-opengauss-${{ matrix.opengaussVersion }}-logs + path: | + ./*.log + ./logs/** + if-no-files-found: warn + retention-days: 2 \ No newline at end of file diff --git a/.github/actions/scenarios/database-write-prohibition/postgresql/action.yml b/.github/actions/scenarios/database-write-prohibition/postgresql/action.yml new file mode 100644 index 0000000000..b65d8d874e --- /dev/null +++ b/.github/actions/scenarios/database-write-prohibition/postgresql/action.yml @@ -0,0 +1,66 @@ +name: "Postgresql Database Write Prohibition Plugin Test" +description: "Auto test for postgresql database write prohibition" +runs: + using: composite + steps: + - name: entry + uses: ./.github/actions/common/entry + with: + log-dir: ./logs/database-write-prohibition/postgresql + - name: install postgresql + shell: bash + run: | + curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg + echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list + sudo apt update + sudo apt install postgresql-14 -y + sudo service postgresql start + sudo service postgresql status + sudo -u postgres psql -c "CREATE DATABASE test;" + sudo chmod -R 777 /etc/postgresql/14/main/ + sudo sed -i 's/scram-sha-256/trust/g' /etc/postgresql/14/main/pg_hba.conf + sudo sed -i 's/peer/trust/g' /etc/postgresql/14/main/pg_hba.conf + sudo echo "host all all all md5" >> /etc/postgresql/14/main/pg_hba.conf + sudo sed -i 's/#password_encryption = scram-sha-256/password_encryption = md5/g' /etc/postgresql/14/main/postgresql.conf + sudo -u postgres psql -c "SELECT pg_reload_conf();set password_encryption = 'md5';show password_encryption;ALTER USER postgres WITH PASSWORD '${{ env.datasourcePassword }}';" + - name: package demos + shell: bash + run: | + mvn package -Ddatabase.version=${{ matrix.postgresqlVersion }} -Ddatabase.groupId=org.postgresql -Ddatabase.artifactId=postgresql \ + -DskipTests -Ppostgresql-opengauss-test --file sermant-integration-tests/database-write-prohibition-test/pom.xml + - name: start postgresql demo + shell: bash + env: + SPRING_DATASOURCE_DRIVER_CLASS_NAME: org.postgresql.Driver + SPRING_DATASOURCE_URL: jdbc:postgresql://127.0.0.1:5432/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true + SPRING_DATASOURCE_USERNAME: postgres + SPRING_DATASOURCE_PASSWORD: ${{ env.datasourcePassword }} + run: | + nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar -jar \ + -Dsermant_log_dir=${{ env.logDir }}/postgresql \ + sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/target/postgresql-opengauss-demo.jar > ${{ env.logDir }}/postgresql.log 2>&1 & + - name: waiting for services start + shell: bash + run: | + ps -ef | grep java + bash ./sermant-integration-tests/scripts/checkService.sh http://127.0.0.1:8081/checkStatus 120 + - name: test postgresql + shell: bash + run: | + mvn test -Ddatabase.prohibition.integration.test.type=POSTGRESQL_OPENGAUSS --file \ + sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml + - name: exit + if: always() + uses: ./.github/actions/common/exit + with: + processor-keyword: postgresql + - name: if failure then upload error log + uses: actions/upload-artifact@v3 + if: ${{ failure() || cancelled() }} + with: + name: (${{ github.job }})-database-write-prohibition-postgresql-${{ matrix.postgresqlVersion }}-logs + path: | + ./*.log + ./logs/** + if-no-files-found: warn + retention-days: 2 \ 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 index 0aa89eee13..17ef0e4f6d 100644 --- a/.github/workflows/database_write_prohibition_integration_test.yml +++ b/.github/workflows/database_write_prohibition_integration_test.yml @@ -118,4 +118,92 @@ jobs: - name: common operations uses: ./.github/actions/common/database-write-prohibition - name: database-write-prohibition-mysql test for mysqlClientVersion=${{ matrix.mysqlClientVersion }} - uses: ./.github/actions/scenarios/database-write-prohibition/mysql \ No newline at end of file + uses: ./.github/actions/scenarios/database-write-prohibition/mysql + test-for-postgresql: + name: Test for postgresql + runs-on: ubuntu-latest + needs: [build-agent-and-cache, download-midwares-and-cache] + strategy: + matrix: + include: + - postgresqlVersion: "9.4-1200-jdbc4" + - postgresqlVersion: "9.4-1203-jdbc4" + - postgresqlVersion: "9.4-1206-jdbc4" + - postgresqlVersion: "9.4-1200-jdbc41" + - postgresqlVersion: "9.4-1203-jdbc41" + - postgresqlVersion: "9.4-1206-jdbc41" + - postgresqlVersion: "9.4.1207" + - postgresqlVersion: "9.4.1210" + - postgresqlVersion: "9.4.1212" + - postgresqlVersion: "42.0.0" + - postgresqlVersion: "42.1.0" + - postgresqlVersion: "42.1.2" + - postgresqlVersion: "42.1.4" + - postgresqlVersion: "42.2.0" + - postgresqlVersion: "42.2.3" + - postgresqlVersion: "42.2.6" + - postgresqlVersion: "42.2.9" + - postgresqlVersion: "42.2.12" + - postgresqlVersion: "42.2.15" + - postgresqlVersion: "42.2.18" + - postgresqlVersion: "42.2.21" + - postgresqlVersion: "42.2.24" + - postgresqlVersion: "42.2.27" + - postgresqlVersion: "42.2.29" + - postgresqlVersion: "42.3.0" + - postgresqlVersion: "42.3.3" + - postgresqlVersion: "42.3.6" + - postgresqlVersion: "42.3.8" + - postgresqlVersion: "42.3.10" + - postgresqlVersion: "42.4.0" + - postgresqlVersion: "42.4.3" + - postgresqlVersion: "42.4.5" + - postgresqlVersion: "42.5.0" + - postgresqlVersion: "42.5.3" + - postgresqlVersion: "42.5.6" + - postgresqlVersion: "42.6.0" + - postgresqlVersion: "42.6.2" + - postgresqlVersion: "42.7.0" + - postgresqlVersion: "42.7.2" + fail-fast: false + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 100 + - name: set java version to environment + run: | + echo "javaVersion=8" >> $GITHUB_ENV + datasourcePassword=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 10)A1a#$ + echo "datasourcePassword=$datasourcePassword" >> $GITHUB_ENV + - name: common operations + uses: ./.github/actions/common/database-write-prohibition + - name: database-write-prohibition test for postgresqlVersion=${{ matrix.postgresqlVersion }} + uses: ./.github/actions/scenarios/database-write-prohibition/postgresql/ + test-for-opengauss: + name: Test for opengauss + runs-on: ubuntu-latest + needs: [build-agent-and-cache, download-midwares-and-cache] + strategy: + matrix: + include: + - opengaussVersion: "3.0.0" + - opengaussVersion: "3.0.5" + - opengaussVersion: "3.0.5-og" + - opengaussVersion: "3.1.0" + - opengaussVersion: "3.1.0-og" + - opengaussVersion: "3.1.1" + - opengaussVersion: "3.1.1-og" + fail-fast: false + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 100 + - name: set java version to environment + run: | + echo "javaVersion=8" >> $GITHUB_ENV + datasourcePassword=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 10)A1a#$ + echo "datasourcePassword=$datasourcePassword" >> $GITHUB_ENV + - name: common operations + uses: ./.github/actions/common/database-write-prohibition + - name: database-write-prohibition test for opengaussVersion=${{ matrix.opengaussVersion }} + uses: ./.github/actions/scenarios/database-write-prohibition/opengauss/ \ 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 e980acb9d2..cf89e36959 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,15 +29,82 @@ public class DatabaseConstant { public static final String SQL_EXCEPTION_MESSAGE_PREFIX = "Database prohibit to write"; /** - * fail to execute database write operation status code + * Status code for execute database operation failure */ public static final String OPERATION_FAIL_CODE = "100"; /** - * succeed to execute database write operation status code + * Status code for successful execute database operation */ public static final String OPERATION_SUCCEED_CODE = "101"; + /** + * select sql + */ + public static final String SELECT_SQL = "SELECT NAME,AGE FROM %s WHERE %s = %s"; + + /** + * delete sql + */ + public static final String DELETE_SQL = "DELETE FROM %s WHERE %s = %s"; + + /** + * insert sql + */ + public static final String INSERT_SQL = "INSERT INTO %s (%s) values (%s) RETURNING id"; + + /** + * insert sql no return value + */ + public static final String INSERT_SQL_NO_RETURN = "INSERT INTO %s (%s) values (%s)"; + + /** + * update sql + */ + public static final String UPDATE_SQL = "UPDATE %s SET %s = %s WHERE %s = %s"; + + /** + * create table sql + */ + public static final String CREATE_TABLE_SQL = "CREATE TABLE %s (id int4 NOT NULL DEFAULT " + + "nextval('%s'::regclass), name varchar(255) ,age int4)"; + + /** + * delete table sql + */ + public static final String DELETE_TABLE_SQL = "DROP TABLE %s"; + + /** + * create index sql + */ + public static final String CREATE_INDEX_SQL = "CREATE INDEX %s ON %s (%s)"; + + /** + * delete index sql + */ + public static final String DELETE_INDEX_SQL = "DROP INDEX %s"; + + /** + * create sequence sql + */ + public static final String CREATE_SEQUENCE_SQL = "CREATE SEQUENCE %s INCREMENT 1 MINVALUE 1 MAXVALUE " + + "9223372036854775807 START 1 CACHE 1"; + + /** + * delete sequence sql + */ + public static final String DELETE_SEQUENCE_SQL = "DROP SEQUENCE %s"; + + /** + * alter table sql + */ + public static final String ALTER_TABLE_SQL = "ALTER TABLE %s ADD COLUMN %s %s"; + + /** + * Request parameter separator + */ + public static final String PARAM_SEPARATOR = ","; + private DatabaseConstant() { } -} +} \ No newline at end of file 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 e0b57f5451..01d72d81f4 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 @@ -17,6 +17,7 @@ 4.3.0 4.5.13 1.2 + 1.2.83 @@ -37,6 +38,12 @@ ${httpclient4x.version} test + + com.alibaba + fastjson + ${fastjson.version} + test + ch.qos.logback logback-core diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/AbstractDatabaseProhibitionTest.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/AbstractDatabaseProhibitionTest.java new file mode 100644 index 0000000000..de35f385ca --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/AbstractDatabaseProhibitionTest.java @@ -0,0 +1,62 @@ +/* + * 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; + +import com.huaweicloud.sermant.database.prohibition.integration.entity.Result; +import com.huaweicloud.sermant.database.prohibition.integration.utils.HttpRequestUtils; + +import com.alibaba.fastjson.JSONObject; + +import org.junit.jupiter.api.Assertions; + +/** + * Database write prohibition test + * + * @author zhp + * @since 2024-03-13 + */ +public abstract class AbstractDatabaseProhibitionTest { + /** + * enable database write prohibition configuration + */ + public static final String ENABLE_DATABASE_WRITE_PROHIBITION_CONFIG = "enablePostgreSqlWriteProhibition: true\n" + + "enableOpenGaussWriteProhibition: true\n" + + "postgreSqlDatabases:\n" + + " - test\n" + + "openGaussDatabases:\n" + + " - postgres"; + + /** + * disable database write prohibition configuration + */ + public static final String DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG = ""; + + /** + * test interface results + * + * @param httpRequestUrl Request Address + * @param statusCode 预期结果编码 + * @return 结果 + */ + public static Result testHttpRequest(String httpRequestUrl, String statusCode) { + String response = HttpRequestUtils.doGet(httpRequestUrl); + Result result = JSONObject.parseObject(response, Result.class); + Assertions.assertEquals(result.getCode(), statusCode); + return result; + } +} 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 index c8d930f5fb..56c3f8d0e6 100644 --- 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 @@ -24,12 +24,12 @@ **/ public class DatabaseConstant { /** - * fail to execute database write operation status code + * Status code for execute database operation failure */ public static final String OPERATION_FAIL_CODE = "100"; /** - * succeed to execute database write operation status code + * Status code for successful execute database operation */ public static final String OPERATION_SUCCEED_CODE = "101"; diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/entity/Result.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/entity/Result.java new file mode 100644 index 0000000000..e2df698665 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/entity/Result.java @@ -0,0 +1,78 @@ +/* + * 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.entity; + +/** + * Result Information + * + * @author zhp + * @since 2024-03-13 + */ +public class Result { + /** + * result code + */ + private String code; + + /** + * result message + */ + private String message; + + /** + * result data + */ + private Object data; + + /** + * Constructor + * + * @param code result code + * @param message result message + * @param data result data + */ + public Result(String code, String message, Object data) { + this.code = code; + this.message = message; + this.data = data; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/BatchSqlExecuteTest.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/BatchSqlExecuteTest.java new file mode 100644 index 0000000000..763d8c0d70 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/BatchSqlExecuteTest.java @@ -0,0 +1,135 @@ +/* + * 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.postgresql_opengauss; + +import com.huaweicloud.sermant.database.prohibition.integration.constant.DatabaseConstant; +import com.huaweicloud.sermant.database.prohibition.integration.entity.Result; +import com.huaweicloud.sermant.database.prohibition.integration.AbstractDatabaseProhibitionTest; +import com.huaweicloud.sermant.database.prohibition.integration.utils.DynamicConfigUtils; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +/** + * Database write prohibition test + * + * @author zhp + * @since 2024-03-13 + */ +public class BatchSqlExecuteTest extends AbstractDatabaseProhibitionTest { + private static final String CREATE_SEQUENCE_URL = "http://127.0.0.1:8081/batch/createSequenceByBatch?"; + + private static final String DELETE_SEQUENCE_URL = "http://127.0.0.1:8081/batch/deleteSequenceByBatch?"; + + private static final String CREATE_TABLE_URL = "http://127.0.0.1:8081/batch/createTableByBatch?"; + + private static final String DELETE_TABLE_URL = "http://127.0.0.1:8081/batch/deleteTableByBatch?"; + + private static final String INSERT_URL = "http://127.0.0.1:8081/batch/saveDataByBatch?"; + + private static final String SELECT_URL = "http://127.0.0.1:8081/batch/getDataByBatch?"; + + private static final String UPDATE_URL = "http://127.0.0.1:8081/batch/updateDataByBatch?"; + + private static final String DELETE_URL = "http://127.0.0.1:8081/batch/deleteDataByBatch?"; + + private static final String CREATE_INDEX_URL = "http://127.0.0.1:8081/batch/createIndexByBatch?"; + + private static final String DELETE_INDEX_URL = "http://127.0.0.1:8081/batch/deleteIndexByBatch?"; + + private static final String ALTER_TABLE_URL = "http://127.0.0.1:8081/batch/alterTable?"; + + /** + * test postgresql and opengauss database write prohibition function + */ + @Test + @EnabledIfSystemProperty(named = "database.prohibition.integration.test.type", matches = "POSTGRESQL_OPENGAUSS") + public void testPostgresqlAndOpenGauss() throws Exception { + DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test create sequence + testHttpRequest(CREATE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test create table + testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_seq&tableName=students", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test insert data + Result result = testHttpRequest(INSERT_URL + "tableName=students&columnNames=name," + + "age&columnValues='lili',11", DatabaseConstant.OPERATION_SUCCEED_CODE); + int id = (int) result.getData(); + // test select data + testHttpRequest(SELECT_URL + "tableName=students&columnName=id&columnValue=100", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test update data + testHttpRequest(UPDATE_URL + "tableName=students&columnNames=name,id&columnValues=" + + "'lili'," + id, DatabaseConstant.OPERATION_SUCCEED_CODE); + // test create index + testHttpRequest(CREATE_INDEX_URL + "tableName=students&columnName=age&indexName=idx_age", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test alter table + testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=class&columnType=varchar(255)", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // enable database write prohibition + DynamicConfigUtils.updateConfig(ENABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test create sequence + testHttpRequest(CREATE_SEQUENCE_URL + "sequenceName=students_new_seq", + DatabaseConstant.OPERATION_FAIL_CODE); + // test create table + testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_new_seq&tableName=students_new", + DatabaseConstant.OPERATION_FAIL_CODE); + // test insert data + testHttpRequest(INSERT_URL + "columnNames=name,age&columnValues='line',11", + DatabaseConstant.OPERATION_FAIL_CODE); + // test select data + testHttpRequest(SELECT_URL + "tableName=students&columnName=id&columnValue=" + id, + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test update data + testHttpRequest(UPDATE_URL + "columnNames=name,id&columnValues='lili'," + id, + DatabaseConstant.OPERATION_FAIL_CODE); + // test create index + testHttpRequest(CREATE_INDEX_URL + "tableName=students&columnName=age&indexName=idx_age", + DatabaseConstant.OPERATION_FAIL_CODE); + // test delete data + testHttpRequest(DELETE_URL + "tableName=students&columnNames=id&columnValues=" + id, + DatabaseConstant.OPERATION_FAIL_CODE); + // test drop index + testHttpRequest(DELETE_INDEX_URL + "indexName=idx_age", + DatabaseConstant.OPERATION_FAIL_CODE); + // test drop table + testHttpRequest(CREATE_TABLE_URL + "tableName=students", + DatabaseConstant.OPERATION_FAIL_CODE); + // test drop sequence + testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq", + DatabaseConstant.OPERATION_FAIL_CODE); + // test alter table + testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=grade&columnType=varchar(255)", + DatabaseConstant.OPERATION_FAIL_CODE); + // disable database write prohibition + DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test delete data + testHttpRequest(DELETE_URL + "tableName=students&columnNames=id&columnValues=" + id, + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop index + testHttpRequest(DELETE_INDEX_URL + "indexName=idx_age", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop table + testHttpRequest(DELETE_TABLE_URL + "tableName=students", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop sequence + testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.OPERATION_SUCCEED_CODE); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/PreparedSqlExecuteTest.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/PreparedSqlExecuteTest.java new file mode 100644 index 0000000000..3a98e2879b --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/PreparedSqlExecuteTest.java @@ -0,0 +1,129 @@ +/* + * 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.postgresql_opengauss; + +import com.huaweicloud.sermant.database.prohibition.integration.constant.DatabaseConstant; +import com.huaweicloud.sermant.database.prohibition.integration.entity.Result; +import com.huaweicloud.sermant.database.prohibition.integration.AbstractDatabaseProhibitionTest; +import com.huaweicloud.sermant.database.prohibition.integration.utils.DynamicConfigUtils; + +import com.alibaba.fastjson.JSONObject; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +/** + * Database write prohibition test + * + * @author zhp + * @since 2024-03-13 + */ +public class PreparedSqlExecuteTest extends AbstractDatabaseProhibitionTest { + private static final String CREATE_SEQUENCE_URL = "http://127.0.0.1:8081/createSequence?"; + + private static final String DELETE_SEQUENCE_URL = "http://127.0.0.1:8081/deleteSequence?"; + + private static final String CREATE_TABLE_URL = "http://127.0.0.1:8081/createTable?"; + + private static final String DELETE_TABLE_URL = "http://127.0.0.1:8081/deleteTable?"; + + private static final String INSERT_URL = "http://127.0.0.1:8081/saveStudents?"; + + private static final String SELECT_URL = "http://127.0.0.1:8081/getById?"; + + private static final String UPDATE_URL = "http://127.0.0.1:8081/updateStudents?"; + + private static final String DELETE_URL = "http://127.0.0.1:8081/deleteStudents?"; + + private static final String CREATE_INDEX_URL = "http://127.0.0.1:8081/createIndex?"; + + private static final String DELETE_INDEX_URL = "http://127.0.0.1:8081/deleteIndex?"; + + private static final String ALTER_TABLE_URL = "http://127.0.0.1:8081/alterTable?"; + + /** + * test postgresql and opengauss database write prohibition function + */ + @Test + @EnabledIfSystemProperty(named = "database.prohibition.integration.test.type", matches = "POSTGRESQL_OPENGAUSS") + public void testPostgresqlAndOpenGauss() throws Exception { + DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test create sequence + testHttpRequest(CREATE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test create table + testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_seq&tableName=students", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test insert data + Result result = testHttpRequest(INSERT_URL + "name=line&age=11", DatabaseConstant.OPERATION_SUCCEED_CODE); + JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(result.getData())); + int id = jsonObject.getIntValue("id"); + // test select data + testHttpRequest(SELECT_URL + "id=" + id, DatabaseConstant.OPERATION_SUCCEED_CODE); + // test update data + testHttpRequest(UPDATE_URL + "name=line&age=33&id=" + id, DatabaseConstant.OPERATION_SUCCEED_CODE); + // test create index + testHttpRequest(CREATE_INDEX_URL + "tableName=students&columnName=age&indexName=idx_age", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test alter table + testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=class&columnType=varchar(255)", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // enable database write prohibition + DynamicConfigUtils.updateConfig(ENABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test create sequence + testHttpRequest(CREATE_SEQUENCE_URL + "sequenceName=students_new_seq", + DatabaseConstant.OPERATION_FAIL_CODE); + // test create table + testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_new_seq&tableName=students_new", + DatabaseConstant.OPERATION_FAIL_CODE); + // test insert data + testHttpRequest(INSERT_URL + "name=line&age=33", DatabaseConstant.OPERATION_FAIL_CODE); + // test select data + testHttpRequest(SELECT_URL + "id=" + id, DatabaseConstant.OPERATION_SUCCEED_CODE); + // test update data + testHttpRequest(UPDATE_URL + "name=line&age=33&id=" + id, DatabaseConstant.OPERATION_FAIL_CODE); + // test create index + testHttpRequest(CREATE_INDEX_URL + "tableName=students&columnName=age&indexName=idx_age", + DatabaseConstant.OPERATION_FAIL_CODE); + // test delete data + testHttpRequest(DELETE_URL + "id=" + id, DatabaseConstant.OPERATION_FAIL_CODE); + // test drop index + testHttpRequest(DELETE_INDEX_URL + "indexName=idx_age", DatabaseConstant.OPERATION_FAIL_CODE); + // test drop table + testHttpRequest(CREATE_TABLE_URL + "tableName=students", + DatabaseConstant.OPERATION_FAIL_CODE); + // test drop sequence + testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq", + DatabaseConstant.OPERATION_FAIL_CODE); + // test alter table + testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=grade&columnType=varchar(255)", + DatabaseConstant.OPERATION_FAIL_CODE); + // disable database write prohibition + DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test delete data + testHttpRequest(DELETE_URL + "id=" + id, DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop index + testHttpRequest(DELETE_INDEX_URL + "indexName=idx_age", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop table + testHttpRequest(DELETE_TABLE_URL + "tableName=students", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop sequence + testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.OPERATION_SUCCEED_CODE); + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/StaticSqlExecuteTest.java b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/StaticSqlExecuteTest.java new file mode 100644 index 0000000000..e7e224caa2 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/src/test/java/com/huaweicloud/sermant/database/prohibition/integration/postgresql_opengauss/StaticSqlExecuteTest.java @@ -0,0 +1,135 @@ +/* + * 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.postgresql_opengauss; + +import com.huaweicloud.sermant.database.prohibition.integration.constant.DatabaseConstant; +import com.huaweicloud.sermant.database.prohibition.integration.entity.Result; +import com.huaweicloud.sermant.database.prohibition.integration.AbstractDatabaseProhibitionTest; +import com.huaweicloud.sermant.database.prohibition.integration.utils.DynamicConfigUtils; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +/** + * Database write prohibition test + * + * @author zhp + * @since 2024-03-13 + */ +public class StaticSqlExecuteTest extends AbstractDatabaseProhibitionTest { + private static final String CREATE_SEQUENCE_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/createSequenceByStaticSql?"; + + private static final String DELETE_SEQUENCE_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/deleteSequenceByStaticSql?"; + + private static final String CREATE_TABLE_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/createTableByStaticSql?"; + + private static final String DELETE_TABLE_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/deleteTableByStaticSql?"; + + private static final String INSERT_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/saveDataByStaticSql?"; + + private static final String SELECT_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/getDataByStaticSql?"; + + private static final String UPDATE_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/updateDataByStaticSql?"; + + private static final String DELETE_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/deleteDataByStaticSql?"; + + private static final String CREATE_INDEX_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/createIndexByStaticSql?"; + + private static final String DELETE_INDEX_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/deleteIndexByStaticSql?"; + + private static final String ALTER_TABLE_BY_STATIC_SQL_URL = "http://127.0.0.1:8081/static/alterTable?"; + + /** + * test postgresql and opengauss database write prohibition function + */ + @Test + @EnabledIfSystemProperty(named = "database.prohibition.integration.test.type", matches = "POSTGRESQL_OPENGAUSS") + public void testPostgresqlAndOpenGauss() throws Exception { + DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test create sequence + testHttpRequest(CREATE_SEQUENCE_BY_STATIC_SQL_URL + "sequenceName=students_seq", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test create table + testHttpRequest(CREATE_TABLE_BY_STATIC_SQL_URL + "sequenceName=students_seq&tableName=students", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test insert data + Result result = testHttpRequest(INSERT_BY_STATIC_SQL_URL + "tableName=students&columnNames=name," + + "age&columnValues='lili',11", DatabaseConstant.OPERATION_SUCCEED_CODE); + int id = (int) result.getData(); + // test select data + testHttpRequest(SELECT_BY_STATIC_SQL_URL + "tableName=students&columnName=id&columnValue=" + id, + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test update data + testHttpRequest(UPDATE_BY_STATIC_SQL_URL + "tableName=students&columnNames=name,id&columnValues=" + + "'lili'," + id, DatabaseConstant.OPERATION_SUCCEED_CODE); + // test create index + testHttpRequest(CREATE_INDEX_BY_STATIC_SQL_URL + "tableName=students&columnName=age&indexName=idx_age", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test alter table + testHttpRequest(ALTER_TABLE_BY_STATIC_SQL_URL + "tableName=students&columnName=class&columnType=varchar(255)", + DatabaseConstant.OPERATION_SUCCEED_CODE); + // enable database write prohibition + DynamicConfigUtils.updateConfig(ENABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test create sequence + testHttpRequest(CREATE_SEQUENCE_BY_STATIC_SQL_URL + "sequenceName=students_new_seq", + DatabaseConstant.OPERATION_FAIL_CODE); + // test create table + testHttpRequest(CREATE_TABLE_BY_STATIC_SQL_URL + "sequenceName=students_new_seq&tableName=students_new", + DatabaseConstant.OPERATION_FAIL_CODE); + // test insert data + testHttpRequest(INSERT_BY_STATIC_SQL_URL + "columnNames=name,age&columnValues='line',11", + DatabaseConstant.OPERATION_FAIL_CODE); + // test select data + testHttpRequest(SELECT_BY_STATIC_SQL_URL + "tableName=students&columnName=id&columnValue=" + id, + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test update data + testHttpRequest(UPDATE_BY_STATIC_SQL_URL + "columnNames=name,id&columnValues='lili'," + id, + DatabaseConstant.OPERATION_FAIL_CODE); + // test create index + testHttpRequest(CREATE_INDEX_BY_STATIC_SQL_URL + "tableName=students&columnName=age&indexName=idx_age", + DatabaseConstant.OPERATION_FAIL_CODE); + // test delete data + testHttpRequest(DELETE_BY_STATIC_SQL_URL + "tableName=students&columnNames=id&columnValues=" + id, + DatabaseConstant.OPERATION_FAIL_CODE); + // test drop index + testHttpRequest(DELETE_INDEX_BY_STATIC_SQL_URL + "indexName=idx_age", + DatabaseConstant.OPERATION_FAIL_CODE); + // test drop table + testHttpRequest(CREATE_TABLE_BY_STATIC_SQL_URL + "tableName=students", + DatabaseConstant.OPERATION_FAIL_CODE); + // test drop sequence + testHttpRequest(DELETE_SEQUENCE_BY_STATIC_SQL_URL + "sequenceName=students_seq", + DatabaseConstant.OPERATION_FAIL_CODE); + // test alter table + testHttpRequest(ALTER_TABLE_BY_STATIC_SQL_URL + "tableName=students&columnName=grade&columnType=varchar(255)", + DatabaseConstant.OPERATION_FAIL_CODE); + // disable database write prohibition + DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); + Thread.sleep(2000); + // test delete data + testHttpRequest(DELETE_BY_STATIC_SQL_URL + "tableName=students&columnNames=id&columnValues=" + id, + DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop index + testHttpRequest(DELETE_INDEX_BY_STATIC_SQL_URL + "indexName=idx_age", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop table + testHttpRequest(DELETE_TABLE_BY_STATIC_SQL_URL + "tableName=students", DatabaseConstant.OPERATION_SUCCEED_CODE); + // test drop sequence + testHttpRequest(DELETE_SEQUENCE_BY_STATIC_SQL_URL + "sequenceName=students_seq", DatabaseConstant.OPERATION_SUCCEED_CODE); + } +} 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 deleted file mode 100644 index df5f967cc0..0000000000 --- a/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/pom.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - 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/resources/application.yml b/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml deleted file mode 100644 index 3eb635c584..0000000000 --- a/sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml +++ /dev/null @@ -1,10 +0,0 @@ -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 index 96e6f56976..f0531a0b6c 100644 --- a/sermant-integration-tests/database-write-prohibition-test/pom.xml +++ b/sermant-integration-tests/database-write-prohibition-test/pom.xml @@ -36,15 +36,9 @@ - opengauss-test + postgresql-opengauss-test - opengauss-demo - - - - postgresql-test - - postgresql-demo + postgresql-opengauss-demo 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 deleted file mode 100644 index 5e0c8e7de9..0000000000 --- a/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/postgresql/PostgresqlApplication.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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/pom.xml b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/pom.xml similarity index 64% rename from sermant-integration-tests/database-write-prohibition-test/postgresql-demo/pom.xml rename to sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/pom.xml index 22349682fd..a05e6c50c3 100644 --- a/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/pom.xml +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/pom.xml @@ -9,15 +9,17 @@ 4.0.0 - postgresql-demo + postgresql-opengauss-demo 8 8 - 9.4.1210 3.5.3 3.5.2 - 3.1.0 + 1.0.0-beta.2 + org.postgresql + postgresql + 9.4.1211 @@ -26,9 +28,23 @@ spring-boot-starter-web - org.postgresql - postgresql - ${postgresql.version} + com.ongres.scram + client + ${scram.version} + + + com.ongres.scram + common + ${scram.version} + + + org.springframework.boot + spring-boot-starter-logging + + + ${database.groupId} + ${database.artifactId} + ${database.version} com.huaweicloud.sermant @@ -39,11 +55,6 @@ mybatis-plus-boot-starter ${mybatis.version} - - com.baomidou - dynamic-datasource-spring-boot-starter - ${dynamic.version} - 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/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/DatabaseWriteProhibitionApplication.java similarity index 84% rename from sermant-integration-tests/database-write-prohibition-test/opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/opengauss/OpengaussApplication.java rename to sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/DatabaseWriteProhibitionApplication.java index f10eb080ba..08dd377fa6 100644 --- 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/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/DatabaseWriteProhibitionApplication.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.database.prohibition.opengauss; +package com.huaweicloud.sermant.database.prohibition; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -26,13 +26,13 @@ * @since 2024-02-18 **/ @SpringBootApplication -public class OpengaussApplication { +public class DatabaseWriteProhibitionApplication { /** * startup method * * @param args process startup input parameter */ public static void main(String[] args) { - SpringApplication.run(OpengaussApplication.class, args); + SpringApplication.run(DatabaseWriteProhibitionApplication.class, args); } } diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/controller/DatabaseWriteProhibitionController.java b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/controller/DatabaseWriteProhibitionController.java new file mode 100644 index 0000000000..d2aa11a6ab --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/controller/DatabaseWriteProhibitionController.java @@ -0,0 +1,654 @@ +/* + * 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.controller; + +import com.huaweicloud.sermant.database.prohibition.common.constant.DatabaseConstant; +import com.huaweicloud.sermant.database.prohibition.entity.Result; +import com.huaweicloud.sermant.database.prohibition.entity.Students; +import com.huaweicloud.sermant.database.prohibition.mapper.DatabaseMapper; +import com.huaweicloud.sermant.database.prohibition.mapper.StudentsMapper; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +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; +import java.util.Collections; +import java.util.Locale; + +import javax.annotation.Resource; +import javax.websocket.server.PathParam; + +/** + * Database write prohibition test interface + * + * @author zhp + * @since 2024-01-13 + */ +@RestController +public class DatabaseWriteProhibitionController implements InitializingBean { + @Resource + private StudentsMapper studentsMapper; + + @Resource + private DatabaseMapper databaseMapper; + + @Value(value = "${spring.datasource.url}") + private String url; + + @Value(value = "${spring.datasource.username}") + private String username; + + @Value(value = "${spring.datasource.password}") + private String password; + + private Connection connection; + + /** + * query data based on id + * + * @param id table primary key + * @return Students + */ + @GetMapping("/getById") + public Result getById(@PathParam("id") int id) { + Students students = studentsMapper.selectById(id); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, students); + } + + /** + * save student information + * + * @param name student Name + * @param age student age + * @return number of successful insertions + */ + @GetMapping("/saveStudents") + public Result saveStudents(@PathParam("name") String name, @PathParam("age") int age) { + Students students = new Students(); + students.setAge(age); + students.setName(name); + studentsMapper.insert(students); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, students); + } + + /** + * update student information based on id + * + * @param name student Name + * @param id table primary key + * @param age student age + * @return number of successful updates + */ + @GetMapping("/updateStudents") + public Result updateStudents(@PathParam("name") String name, @PathParam("age") int age, @PathParam("id") int id) { + Students students = new Students(); + students.setAge(age); + students.setName(name); + students.setId(id); + studentsMapper.updateById(students); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, students); + } + + /** + * update student information based on id + * + * @param id table primary key + * @return number of successful deletions + */ + @GetMapping("/deleteStudents") + public Result updateStudents(@PathParam("id") int id) { + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, studentsMapper.deleteById(id)); + } + + /** + * create table + * + * @param tableName table name + * @param sequenceName sequence name + * @return create table results + */ + @GetMapping("/createTable") + public Result createTable(@PathParam("tableName") String tableName, + @PathParam("sequenceName") String sequenceName) { + databaseMapper.createTable(tableName, sequenceName); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * delete table + * + * @param tableName table name + * @return delete table results + */ + @GetMapping("/deleteTable") + public Result deleteTable(@PathParam("tableName") String tableName) { + databaseMapper.deleteTable(tableName); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * create index + * + * @param indexName index name + * @param tableName table name + * @param columnName column name + * @return create index result + */ + @GetMapping("/createIndex") + public Result createIndex(@PathParam("tableName") String tableName, @PathParam("indexName") String indexName, + @PathParam("columnName") String columnName) { + databaseMapper.createIndex(indexName, tableName, columnName); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * delete index + * + * @param indexName index name + * @return delete index result + */ + @GetMapping("/deleteIndex") + public Result deleteIndex(@PathParam("indexName") String indexName) { + databaseMapper.deleteIndex(indexName); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * create sequence + * + * @param sequenceName sequence name + * @return create sequence result + */ + @GetMapping("/createSequence") + public Result createSequence(@PathParam("sequenceName") String sequenceName) { + databaseMapper.createSequence(sequenceName); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * delete sequence + * + * @param sequenceName sequence name + * @return create sequence result + */ + @GetMapping("/deleteSequence") + public Result deleteSequence(@PathParam("sequenceName") String sequenceName) { + databaseMapper.deleteSequence(sequenceName); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * create sequence + * + * @param tableName table name + * @param columnName column name + * @param columnType column type + * @return alter table result + */ + @GetMapping("/alterTable") + public Result alterTable(@PathParam("tableName") String tableName, @PathParam("columnName") String columnName, + @PathParam("columnType") String columnType) { + databaseMapper.addColumn(tableName, columnName, columnType); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * query data + * + * @param columnValue column value + * @param tableName table name + * @param columnName column name + * @return Students + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/getDataByStaticSql") + public Result getDataByStaticSql(@PathParam("tableName") String tableName, + @PathParam("columnName") String columnName, @PathParam("columnValue") String columnValue) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.SELECT_SQL, tableName, columnName, columnValue); + try (Statement statement = connection.createStatement()) { + statement.execute(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + } + + /** + * update data + * + * @param tableName table name + * @param columnNames column names + * @param columnValues column values + * @return updated result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/saveDataByStaticSql") + public Result saveDataByStaticSql(@PathParam("tableName") String tableName, + @PathParam("columnNames") String columnNames, @PathParam("columnValues") String columnValues) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.INSERT_SQL, tableName, columnNames, columnValues); + try (Statement statement = connection.createStatement()) { + ResultSet resultSet = statement.executeQuery(sql); + if (resultSet.next()) { + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, resultSet.getInt("id")); + } + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + } + + /** + * update data + * + * @param tableName table name + * @param columnNames column names + * @param columnValues column values + * @return updated result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/updateDataByStaticSql") + public Result updateDataByStaticSql(@PathParam("tableName") String tableName, + @PathParam("columnNames") String columnNames, @PathParam("columnValues") String columnValues) + throws SQLException { + String[] columnNameArray = columnNames.split(DatabaseConstant.PARAM_SEPARATOR); + String[] columnValueArray = columnValues.split(DatabaseConstant.PARAM_SEPARATOR); + if (columnNameArray.length <= 1 || columnValueArray.length <= 1) { + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, "parameter format not correct", null); + } + String sql = String.format(Locale.ROOT, DatabaseConstant.UPDATE_SQL, tableName, columnNameArray[0], + columnValueArray[0], columnNameArray[1], columnValueArray[1]); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete data + * + * @param tableName table name + * @param columnNames column names + * @param columnValues column values + * @return updated result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/deleteDataByStaticSql") + public Result deleteDataByStaticSql(@PathParam("tableName") String tableName, + @PathParam("columnNames") String columnNames, @PathParam("columnValues") String columnValues) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_SQL, tableName, columnNames, columnValues); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * create table + * + * @param tableName table name + * @param sequenceName sequence name + * @return create table result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/createTableByStaticSql") + public Result createTableByStaticSql(@PathParam("tableName") String tableName, + @PathParam("sequenceName") String sequenceName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.CREATE_TABLE_SQL, tableName, sequenceName); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete table + * + * @param tableName table name + * @return delete table result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/deleteTableByStaticSql") + public Result deleteTableByStaticSql(@PathParam("tableName") String tableName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_TABLE_SQL, tableName); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * create index + * + * @param indexName index name + * @param tableName table name + * @param columnName column name + * @return create index result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/createIndexByStaticSql") + public Result createIndexByStaticSql(@PathParam("tableName") String tableName, + @PathParam("indexName") String indexName, @PathParam("columnName") String columnName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.CREATE_INDEX_SQL, indexName, tableName, columnName); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete index + * + * @param indexName index name + * @return delete index result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/deleteIndexByStaticSql") + public Result deleteIndexByStaticSql(@PathParam("indexName") String indexName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_INDEX_SQL, indexName); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * create sequence + * + * @param sequenceName sequence name + * @return create sequence result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/createSequenceByStaticSql") + public Result createSequenceByStaticSql(@PathParam("sequenceName") String sequenceName) throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.CREATE_SEQUENCE_SQL, sequenceName); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete sequence + * + * @param sequenceName sequence name + * @return create sequence result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/deleteSequenceByStaticSql") + public Result deleteSequenceByStaticSql(@PathParam("sequenceName") String sequenceName) throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_SEQUENCE_SQL, sequenceName); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * alter table + * + * @param tableName table name + * @param columnName column name + * @param columnType column type + * @return alter table result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/static/alterTable") + public Result alterTableByStaticSql(@PathParam("tableName") String tableName, + @PathParam("columnName") String columnName, @PathParam("columnType") String columnType) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.ALTER_TABLE_SQL, tableName, columnName, columnType); + try (Statement statement = connection.createStatement()) { + int rowsAffected = statement.executeUpdate(sql); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * query data + * + * @return Students + */ + @GetMapping("/batch/getDataByBatch") + public Result getDataByBatch() { + studentsMapper.selectBatchIds(Collections.singletonList(1)); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } + + /** + * update data + * + * @param tableName table name + * @param columnNames column names + * @param columnValues column values + * @return updated result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/saveDataByBatch") + public Result saveDataByBatch(@PathParam("tableName") String tableName, + @PathParam("columnNames") String columnNames, @PathParam("columnValues") String columnValues) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.INSERT_SQL_NO_RETURN, tableName, columnNames, + columnValues); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] result = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, result[0]); + } + } + + /** + * update data + * + * @param tableName table name + * @param columnNames column names + * @param columnValues column values + * @return updated result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/updateDataByBatch") + public Result updateDataByBatch(@PathParam("tableName") String tableName, + @PathParam("columnNames") String columnNames, @PathParam("columnValues") String columnValues) + throws SQLException { + String[] columnNameArray = columnNames.split(DatabaseConstant.PARAM_SEPARATOR); + String[] columnValueArray = columnValues.split(DatabaseConstant.PARAM_SEPARATOR); + String sql = String.format(Locale.ROOT, DatabaseConstant.UPDATE_SQL, tableName, columnNameArray[0], + columnValueArray[0], columnNameArray[1], columnValueArray[1]); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete data + * + * @param tableName table name + * @param columnNames column names + * @param columnValues column values + * @return updated result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/deleteDataByBatch") + public Result deleteDataByBatch(@PathParam("tableName") String tableName, + @PathParam("columnNames") String columnNames, @PathParam("columnValues") String columnValues) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_SQL, tableName, columnNames, columnValues); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * create table + * + * @param tableName table name + * @param sequenceName sequence name + * @return create table result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/createTableByBatch") + public Result createTableByBatch(@PathParam("tableName") String tableName, + @PathParam("sequenceName") String sequenceName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.CREATE_TABLE_SQL, tableName, sequenceName); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete table + * + * @param tableName table name + * @return delete table result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/deleteTableByBatch") + public Result deleteTableByBatch(@PathParam("tableName") String tableName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_TABLE_SQL, tableName); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * create index + * + * @param indexName index name + * @param tableName table name + * @param columnName column name + * @return create index result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/createIndexByBatch") + public Result createIndexByBatch(@PathParam("tableName") String tableName, + @PathParam("indexName") String indexName, @PathParam("columnName") String columnName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.CREATE_INDEX_SQL, indexName, tableName, columnName); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete index + * + * @param indexName index name + * @return delete index result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/deleteIndexByBatch") + public Result deleteIndexByBatch(@PathParam("indexName") String indexName) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_INDEX_SQL, indexName); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * create sequence + * + * @param sequenceName sequence name + * @return create sequence result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/createSequenceByBatch") + public Result createSequenceByBatch(@PathParam("sequenceName") String sequenceName) throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.CREATE_SEQUENCE_SQL, sequenceName); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * delete sequence + * + * @param sequenceName sequence name + * @return create sequence result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/deleteSequenceByBatch") + public Result deleteSequenceByBatch(@PathParam("sequenceName") String sequenceName) throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.DELETE_SEQUENCE_SQL, sequenceName); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + /** + * alter table + * + * @param tableName table name + * @param columnName column name + * @param columnType column type + * @return alter table result + * @throws SQLException create connection or execute sql exception + */ + @GetMapping("/batch/alterTable") + public Result alterTableByBatch(@PathParam("tableName") String tableName, + @PathParam("columnName") String columnName, @PathParam("columnType") String columnType) + throws SQLException { + String sql = String.format(Locale.ROOT, DatabaseConstant.ALTER_TABLE_SQL, tableName, columnName, columnType); + try (Statement statement = connection.createStatement()) { + statement.addBatch(sql); + int[] rowsAffected = statement.executeBatch(); + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, rowsAffected); + } + } + + @Override + public void afterPropertiesSet() throws SQLException { + connection = DriverManager.getConnection(url, username, password); + } + + /** + * Used to check if the process starts properly + * + * @return string + */ + @GetMapping("/checkStatus") + public Result checkStatus() { + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, null, null); + } +} \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Result.java b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Result.java new file mode 100644 index 0000000000..6d21e149c4 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Result.java @@ -0,0 +1,78 @@ +/* + * 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.entity; + +/** + * Result Information + * + * @author zhp + * @since 2024-03-13 + */ +public class Result { + /** + * result code + */ + private String code; + + /** + * result message + */ + private String message; + + /** + * result data + */ + private Object data; + + /** + * Constructor + * + * @param code result code + * @param message result message + * @param data result data + */ + public Result(String code, String message, Object data) { + this.code = code; + this.message = message; + this.data = data; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Students.java b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Students.java new file mode 100644 index 0000000000..f081de80b3 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/entity/Students.java @@ -0,0 +1,64 @@ +/* + * 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.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; + +import java.io.Serializable; + +/** + * The entity class corresponding to the student table + * + * @author zhp + * @since 2024-01-13 + */ +@TableName("students") +public class Students implements Serializable { + @TableId(type = IdType.AUTO) + private Integer id; + + private String name; + + private int age; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/handler/GlobalExceptionHandler.java b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000000..26832a0413 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/handler/GlobalExceptionHandler.java @@ -0,0 +1,54 @@ +/* + * 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.handler; + +import com.huaweicloud.sermant.database.prohibition.common.constant.DatabaseConstant; +import com.huaweicloud.sermant.database.prohibition.entity.Result; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.sql.SQLException; + +/** + * global exception handling + * + * @author zhp + * @since 2024-01-13 + */ +@RestControllerAdvice +public class GlobalExceptionHandler { + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + /** + * handler SqlException + * + * @param sqlException sql Exception + * @return result information + */ + @ExceptionHandler(SQLException.class) + public Result handlerSqlException(SQLException sqlException) { + log.warn(sqlException.getMessage(), sqlException); + if (sqlException.getMessage().startsWith(DatabaseConstant.SQL_EXCEPTION_MESSAGE_PREFIX)) { + return new Result(DatabaseConstant.OPERATION_FAIL_CODE, sqlException.getMessage(), null); + } + return new Result(DatabaseConstant.OPERATION_SUCCEED_CODE, sqlException.getMessage(), null); + } +} \ No newline at end of file diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/DatabaseMapper.java b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/DatabaseMapper.java new file mode 100644 index 0000000000..e21f0f66a0 --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/DatabaseMapper.java @@ -0,0 +1,92 @@ +/* + * 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.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Update; + +/** + * Database Mapper + * + * @author zhp + * @since 2024-01-13 + */ +@Mapper +public interface DatabaseMapper { + /** + * create table + * + * @param tableName table name + * @param sequenceName sequence name + */ + @Update("CREATE TABLE ${tableName} (id int4 NOT NULL DEFAULT nextval('${sequenceName}'::regclass),name varchar" + + "(255),age int4)") + void createTable(String tableName, String sequenceName); + + /** + * delete table + * + * @param tableName table name + */ + @Update("DROP TABLE ${tableName}") + void deleteTable(String tableName); + + /** + * create sequence + * + * @param sequenceName sequence Name + */ + @Update("CREATE SEQUENCE ${sequenceName} INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1") + void createSequence(String sequenceName); + + /** + * delete sequence + * + * @param sequenceName sequence Name + */ + @Update("DROP SEQUENCE ${sequenceName}") + void deleteSequence(String sequenceName); + + /** + * create index + * + * @param indexName index name + * @param tableName table name + * @param columnName column name + */ + @Update("CREATE INDEX ${indexName} ON ${tableName} (${columnName})") + void createIndex(String indexName, String tableName, String columnName); + + /** + * delete index + * + * @param indexName index name + */ + @Update("DROP INDEX ${indexName}") + void deleteIndex(String indexName); + + /** + * alter table + * + * @param tableName table name + * @param columnName column name + * @param columnType column type + */ + @Update("ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${columnType}") + void addColumn(String tableName, String columnName, String columnType); +} diff --git a/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/StudentsMapper.java b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/StudentsMapper.java new file mode 100644 index 0000000000..21a1a87c2f --- /dev/null +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/java/com/huaweicloud/sermant/database/prohibition/mapper/StudentsMapper.java @@ -0,0 +1,34 @@ +/* + * 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.mapper; + +import com.huaweicloud.sermant.database.prohibition.entity.Students; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +import org.apache.ibatis.annotations.Mapper; + +/** + * Student table mapper + * + * @author zhp + * @since 2024-01-13 + */ +@Mapper +public interface StudentsMapper extends BaseMapper { +} 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-opengauss-demo/src/main/resources/application.yml similarity index 88% rename from sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/resources/application.yml rename to sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/resources/application.yml index 7348d3c7b4..5823848b81 100644 --- a/sermant-integration-tests/database-write-prohibition-test/postgresql-demo/src/main/resources/application.yml +++ b/sermant-integration-tests/database-write-prohibition-test/postgresql-opengauss-demo/src/main/resources/application.yml @@ -7,4 +7,4 @@ spring: username: password: application: - name: postgresql-demo \ No newline at end of file + name: postgresql-opengauss-demo \ No newline at end of file