Skip to content

Commit

Permalink
Add test demo for database write prohibition plugin
Browse files Browse the repository at this point in the history
Signed-off-by: hanbingleixue <[email protected]>
  • Loading branch information
hanbingleixue committed Mar 19, 2024
1 parent de55650 commit a2f7818
Show file tree
Hide file tree
Showing 19 changed files with 1,631 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,83 @@ public class DatabaseConstant {
*/
public static final String OPERATION_SUCCEED_CODE = "101";

/**
* successful status code
*/
public static final String SUCCESS_CODE = "0";

/**
* failure status code
*/
public static final String FAILURE_CODE = "1";

/**
* 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() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<curator.version>4.3.0</curator.version>
<httpclient4x.version>4.5.13</httpclient4x.version>
<commons-log.version>1.2</commons-log.version>
<fastjson.version>1.2.83</fastjson.version>
</properties>

<dependencies>
Expand All @@ -37,6 +38,12 @@
<version>${httpclient4x.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class DatabaseConstant {
*/
public static final String OPERATION_SUCCEED_CODE = "101";

/**
* successful status code
*/
public static final String SUCCESS_CODE = "0";

/**
* database table data count
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

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/postgresql/batch/createSequenceByBatch?";

private static final String DELETE_SEQUENCE_URL = "http://127.0.0.1:8081/postgresql/batch/deleteSequenceByBatch?";

private static final String CREATE_TABLE_URL = "http://127.0.0.1:8081/postgresql/batch/createTableByBatch?";

private static final String DELETE_TABLE_URL = "http://127.0.0.1:8081/postgresql/batch/deleteTableByBatch?";

private static final String INSERT_URL = "http://127.0.0.1:8081/postgresql/batch/saveDataByBatch?";

private static final String SELECT_URL = "http://127.0.0.1:8081/postgresql/batch/getDataByBatch?";

private static final String UPDATE_URL = "http://127.0.0.1:8081/postgresql/batch/updateDataByBatch?";

private static final String DELETE_URL = "http://127.0.0.1:8081/postgresql/batch/deleteDataByBatch?";

private static final String CREATE_INDEX_URL = "http://127.0.0.1:8081/postgresql/batch/createIndexByBatch?";

private static final String DELETE_INDEX_URL = "http://127.0.0.1:8081/postgresql/batch/deleteIndexByBatch?";

private static final String ALTER_TABLE_URL = "http://127.0.0.1:8081/postgresql/batch/alterTable?";

/**
* test postgresql database write prohibition function
*/
@Test
@EnabledIfSystemProperty(named = "database.prohibition.integration.test.type", matches = "postgresql")
public void testPostgresql() throws Exception {
DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG);
Thread.sleep(2000);
// test create sequence
testHttpRequest(CREATE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.SUCCESS_CODE);
// test create table
testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_seq&tableName=students",
DatabaseConstant.SUCCESS_CODE);
// test insert data
Result result = testHttpRequest(INSERT_URL + "tableName=students&columnNames=name,"
+ "age&columnValues='lili',11", DatabaseConstant.SUCCESS_CODE);
int id = (int) result.getData();
// test select data
testHttpRequest(SELECT_URL + "tableName=students&columnName=id&columnValue=100",
DatabaseConstant.SUCCESS_CODE);
// test update data
testHttpRequest(UPDATE_URL + "tableName=students&columnNames=name,id&columnValues="
+ "'lili'," + id, DatabaseConstant.SUCCESS_CODE);
// test create index
testHttpRequest(CREATE_INDEX_URL + "tableName=students&columnName=age&indexName=idx_age",
DatabaseConstant.SUCCESS_CODE);
// test alter table
testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=class&columnType=varchar(255)",
DatabaseConstant.SUCCESS_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_SUCCEED_CODE);
// test create table
testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_new_seq&tableName=students_new",
DatabaseConstant.OPERATION_SUCCEED_CODE);
// test insert data
testHttpRequest(INSERT_URL + "columnNames=name,age&columnValues='line',11",
DatabaseConstant.OPERATION_SUCCEED_CODE);
// test select data
testHttpRequest(SELECT_URL + "tableName=students&columnName=id&columnValue=" + id,
DatabaseConstant.SUCCESS_CODE);
// test update data
testHttpRequest(UPDATE_URL + "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 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(CREATE_TABLE_URL + "tableName=students",
DatabaseConstant.OPERATION_SUCCEED_CODE);
// test drop sequence
testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq",
DatabaseConstant.OPERATION_SUCCEED_CODE);
// test alter table
testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=grade&columnType=varchar(255)",
DatabaseConstant.OPERATION_SUCCEED_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.SUCCESS_CODE);
// test drop index
testHttpRequest(DELETE_INDEX_URL + "indexName=idx_age", DatabaseConstant.SUCCESS_CODE);
// test drop table
testHttpRequest(DELETE_TABLE_URL + "tableName=students", DatabaseConstant.SUCCESS_CODE);
// test drop sequence
testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.SUCCESS_CODE);
}
}
Loading

0 comments on commit a2f7818

Please sign in to comment.