Skip to content

Commit

Permalink
Merge pull request #1462 from hanbingleixue/database-ut
Browse files Browse the repository at this point in the history
Add a demo module for database write prohibition plugin integration t…
  • Loading branch information
Sherlockhan authored Mar 13, 2024
2 parents d270273 + 71fcea7 commit 69bf48f
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 0 deletions.
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>
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() {
}
}
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>
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));
}
}
}
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 "";
}
}
}
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>
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);
}
}
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
Loading

0 comments on commit 69bf48f

Please sign in to comment.