-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1462 from hanbingleixue/database-ut
Add a demo module for database write prohibition plugin integration t…
- Loading branch information
Showing
13 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...tegration-tests/database-write-prohibition-test/database-write-prohibition-common/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>database-write-prohibition-test</artifactId> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>database-write-prohibition-common</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
</project> |
43 changes: 43 additions & 0 deletions
43
...n/java/com/huaweicloud/sermant/database/prohibition/common/constant/DatabaseConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.prohibition.common.constant; | ||
|
||
/** | ||
* common constant | ||
* | ||
* @author daizhenyu | ||
* @since 2024-03-11 | ||
**/ | ||
public class DatabaseConstant { | ||
/** | ||
* database write prohibition sqlexception message prefix | ||
*/ | ||
public static final String SQL_EXCEPTION_MESSAGE_PREFIX = "Database prohibit to write"; | ||
|
||
/** | ||
* succeed to prohibit database status code | ||
*/ | ||
public static final int SUCCEED_PROHIBITION_CODE = 100; | ||
|
||
/** | ||
* failed to prohibit database status code | ||
*/ | ||
public static final int FAILED_PROHIBITION_CODE = 101; | ||
|
||
private DatabaseConstant() { | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>database-write-prohibition-test</artifactId> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>database-write-prohibition-integration-test</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<curator.version>4.3.0</curator.version> | ||
<httpclient4x.version>4.5.13</httpclient4x.version> | ||
<commons-log.version>1.2</commons-log.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<artifactId>database-write-prohibition-common</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.curator</groupId> | ||
<artifactId>curator-recipes</artifactId> | ||
<version>${curator.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>${httpclient4x.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<version>${commons-log.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>${project.artifactId}</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
55 changes: 55 additions & 0 deletions
55
...va/com/huaweicloud/sermant/database/prohibition/integration/utils/DynamicConfigUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.prohibition.integration.utils; | ||
|
||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
import org.apache.zookeeper.CreateMode; | ||
import org.apache.zookeeper.data.Stat; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Distribute tools for dynamic configuration | ||
* | ||
* @author daizhenyu | ||
* @since 2024-03-12 | ||
**/ | ||
public class DynamicConfigUtils { | ||
public static final String ZOOKEEPER_NODE_PATH = "/app=default&environment=/sermant.database.write.globalConfig"; | ||
|
||
private DynamicConfigUtils() { | ||
} | ||
|
||
public static void updateConfig(String config) throws Exception { | ||
updateConfig(ZOOKEEPER_NODE_PATH, config); | ||
} | ||
|
||
public static void updateConfig(String path, String config) throws Exception { | ||
CuratorFramework curator = CuratorFrameworkFactory.newClient("127.0.0.1:2181", | ||
new ExponentialBackoffRetry(1000, 3)); | ||
curator.start(); | ||
Stat stat = curator.checkExists().forPath(path); | ||
if (stat == null) { | ||
curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, | ||
config.getBytes(StandardCharsets.UTF_8)); | ||
} else { | ||
curator.setData().forPath(path, config.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...java/com/huaweicloud/sermant/database/prohibition/integration/utils/HttpRequestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.prohibition.integration.utils; | ||
|
||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.util.EntityUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* HTTP Request Tool Class | ||
* | ||
* @author daizhenyu | ||
* @since 2024-03-12 | ||
**/ | ||
public class HttpRequestUtils { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestUtils.class); | ||
|
||
private static final int SUCCESS_CODE = 200; | ||
|
||
private HttpRequestUtils() { | ||
} | ||
|
||
/** | ||
* send get request | ||
* | ||
* @param url hTTP request URL | ||
* @return response Body | ||
*/ | ||
public static String doGet(String url) { | ||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||
RequestConfig requestConfig = RequestConfig.custom() | ||
.build(); | ||
HttpGet httpGet = new HttpGet(url); | ||
httpGet.setConfig(requestConfig); | ||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) { | ||
if (response.getStatusLine().getStatusCode() == SUCCESS_CODE) { | ||
return EntityUtils.toString(response.getEntity()); | ||
} | ||
LOGGER.info("Request error, the message is: {}", EntityUtils.toString(response.getEntity())); | ||
return ""; | ||
} | ||
} catch (IOException e) { | ||
LOGGER.info("Request exception, the message is: {}", e.getMessage()); | ||
return ""; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
sermant-integration-tests/database-write-prohibition-test/opengauss-demo/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>database-write-prohibition-test</artifactId> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>opengauss-demo</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<mybatis.version>3.5.3</mybatis.version> | ||
<dynamic.version>3.5.2</dynamic.version> | ||
<opengauss.version>3.0.0</opengauss.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.opengauss</groupId> | ||
<artifactId>opengauss-jdbc</artifactId> | ||
<version>${opengauss.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-boot-starter</artifactId> | ||
<version>${mybatis.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> | ||
<version>${dynamic.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>${project.artifactId}</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
38 changes: 38 additions & 0 deletions
38
...ain/java/com/huaweicloud/sermant/database/prohibition/opengauss/OpengaussApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.prohibition.opengauss; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Springboot startup class | ||
* | ||
* @author zhp | ||
* @since 2024-02-18 | ||
**/ | ||
@SpringBootApplication | ||
public class OpengaussApplication { | ||
/** | ||
* startup method | ||
* | ||
* @param args process startup input parameter | ||
*/ | ||
public static void main(String[] args) { | ||
SpringApplication.run(OpengaussApplication.class, args); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...n-tests/database-write-prohibition-test/opengauss-demo/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
server: | ||
port: 8081 | ||
spring: | ||
datasource: | ||
driver-class-name: org.opengauss.Driver | ||
url: jdbc:opengauss://127.0.0.1:5432/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true | ||
username: | ||
password: | ||
application: | ||
name: opengauss-demo |
Oops, something went wrong.