Skip to content

Commit

Permalink
MongoDB数据库禁写开发
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <1449308021@qq.com>
  • Loading branch information
daizhenyu committed Jan 22, 2024
1 parent 9cc6a63 commit 07fac75
Show file tree
Hide file tree
Showing 17 changed files with 923 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ plugins:
- service-removal
- service-visibility
- tag-transmission
- database-write-prohibition
# dynamicPlugins用于配置支持动态安装的插件, agentmain启动时生效, 允许卸载:
# 1. active类型插件将会主动启用
# 2. passive类型插件需通过指令或接口调用启用
Expand Down
3 changes: 3 additions & 0 deletions sermant-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<module>sermant-service-removal</module>
<module>sermant-spring-beans-deal</module>
<module>sermant-tag-transmission</module>
<module>sermant-database-write-prohibition</module>
</modules>
<build>
<pluginManagement>
Expand Down Expand Up @@ -74,6 +75,7 @@
<module>sermant-service-removal</module>
<module>sermant-spring-beans-deal</module>
<module>sermant-tag-transmission</module>
<module>sermant-database-write-prohibition</module>
</modules>
<build>
<pluginManagement>
Expand Down Expand Up @@ -106,6 +108,7 @@
<module>sermant-service-removal</module>
<module>sermant-spring-beans-deal</module>
<module>sermant-tag-transmission</module>
<module>sermant-database-write-prohibition</module>
</modules>
</profile>
</profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>sermant-database-write-prohibition</artifactId>
<groupId>com.huaweicloud.sermant</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>database-controller</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.huaweicloud.sermant</groupId>
<artifactId>sermant-agentcore-core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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.config;

import java.util.HashSet;
import java.util.Set;

/**
* 数据库禁写插件配置类
*
* @author daizhenyu
* @since 2024-01-15
**/
public class DatabaseWriteProhibitionConfig {
/**
* 是否开启禁写
*/
private boolean enableDatabaseWriteProhibition = true;

/**
* 需要禁写的数据库
*/
private Set<String> databases = new HashSet<>();

public boolean isEnableDatabaseWriteProhibition() {
return enableDatabaseWriteProhibition;
}

public void setEnableDatabaseWriteProhibition(boolean enableDatabaseWriteProhibition) {
this.enableDatabaseWriteProhibition = enableDatabaseWriteProhibition;
}

/**
* 获取禁消费的数据库列表
*
* @return 数据库列表
*/
public Set<String> getDatabases() {
return databases;
}

public void setDatabases(Set<String> databases) {
this.databases = databases;
}

@Override
public String toString() {
return "enableDatabaseWriteProhibition=" + enableDatabaseWriteProhibition + ", databases=" + databases;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.config;

import java.util.HashSet;
import java.util.Set;

/**
* 数据库禁写配置管理类
*
* @author daizhenyu
* @since 2024-01-22
*/
public class DatabaseWriteProhibitionManager {
private static DatabaseWriteProhibitionConfig globalConfig = new DatabaseWriteProhibitionConfig();

private static DatabaseWriteProhibitionConfig localConfig = new DatabaseWriteProhibitionConfig();

private DatabaseWriteProhibitionManager() {
}

/**
* 获取要禁止消费的数据库集合
*
* @return 禁止消费的数据库集合
*/
public static Set<String> getProhibitionDatabases() {
if (globalConfig.isEnableDatabaseWriteProhibition()) {
return globalConfig.getDatabases();
}
if (localConfig.isEnableDatabaseWriteProhibition()) {
return localConfig.getDatabases();
}
return new HashSet<>();
}

/**
* 获取全局配置
*
* @return 全局配置
*/
public static DatabaseWriteProhibitionConfig getGlobalConfig() {
return globalConfig;
}

/**
* 获取局部配置
*
* @return 局部配置
*/
public static DatabaseWriteProhibitionConfig getLocalConfig() {
return localConfig;
}

/**
* 更新全局配置
*
* @param config 禁止消费配置
*/
public static void updateGlobalConfig(DatabaseWriteProhibitionConfig config) {
if (config == null) {
globalConfig = new DatabaseWriteProhibitionConfig();
return;
}
globalConfig = config;
}

/**
* 更新局部配置
*
* @param config 禁止消费配置
*/
public static void updateLocalConfig(DatabaseWriteProhibitionConfig config) {
if (config == null) {
localConfig = new DatabaseWriteProhibitionConfig();
return;
}
localConfig = config;
}

/**
* 打印配置信息
*
* @return 配置信息
*/
public static String printConfig() {
return "Global WriteProhibitionConfig: " + globalConfig.toString() + "; Local WriteProhibitionConfig: "
+ localConfig.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.controller;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;

import java.sql.SQLException;
import java.util.Set;

Check failure on line 22 in sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/controller/DatabaseController.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Unused import - java.util.Set. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/controller/DatabaseController.java:22:8: error: Unused import - java.util.Set. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)

/**
* 数据库控制器
*
* @author daizhenyu
* @since 2024-01-15
**/
public class DatabaseController {
private static Object result = new Object();

private DatabaseController() {
}

/**
* 获取需要禁写的数据库清单
*
* @param database 数据库名称
* @param context 拦截点上下文对象
* @return
*/
public static void disableDatabaseWriteOperation(String database, ExecuteContext context) {
context.setThrowableOut(new SQLException("Database prohibit to write, database: " + database));
context.skip(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.database.handler;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;

/**
* 数据库禁写处理接口
*
* @author daizhenyu
* @since 2024-01-15
**/
public interface DatabaseHandler {
/**
* 拦截点前置处理
*
* @param context 上下文信息
*/
void doBefore(ExecuteContext context);

/**
* 拦截点后置处理
*
* @param context 上下文信息
*/
void doAfter(ExecuteContext context);

/**
* 拦截点异常处理
*
* @param context 上下文信息
*/
void doOnThrow(ExecuteContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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>sermant-database-write-prohibition</artifactId>
<groupId>com.huaweicloud.sermant</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mongodb-3.x-4.x-plugin</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<config.skip.flag>false</config.skip.flag>
<package.plugin.type>plugin</package.plugin.type>
<mongodb.version>4.3.4</mongodb.version>
</properties>

<dependencies>
<dependency>
<groupId>com.huaweicloud.sermant</groupId>
<artifactId>sermant-agentcore-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.huaweicloud.sermant</groupId>
<artifactId>database-controller</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 07fac75

Please sign in to comment.