Skip to content

Commit

Permalink
MongoDB数据库禁写开发
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <[email protected]>
  • Loading branch information
daizhenyu committed Jan 23, 2024
1 parent 9cc6a63 commit 99151d5
Show file tree
Hide file tree
Showing 17 changed files with 1,027 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 = false;

/**
* 需要禁写的数据库
*/
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.Collections;
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 Collections.EMPTY_SET;
}

/**
* 获取全局配置
*
* @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,46 @@
/*
* 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;

/**
* 数据库控制器
*
* @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,66 @@
/*
* 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.interceptor;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
import com.huaweicloud.sermant.database.handler.DatabaseHandler;

/**
* mongodb抽象interceptor
*
* @author daizhenyu
* @since 2024-01-22
**/
public abstract class AbstractDatabaseInterceptor extends AbstractInterceptor {
protected DatabaseHandler handler;

@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
if (handler != null) {
handler.doBefore(context);
return context;
}
return doBefore(context);
}

@Override
public ExecuteContext after(ExecuteContext context) throws Exception {
if (handler != null) {
handler.doAfter(context);
return context;
}
return context;
}

@Override
public ExecuteContext onThrow(ExecuteContext context) throws Exception {
if (handler != null) {
handler.doOnThrow(context);
return context;
}
return context;
}

/**
* 方法执行前
*
* @param context 上下文
* @return ExecuteContext 上下文
*/
protected abstract ExecuteContext doBefore(ExecuteContext context);
}
Loading

0 comments on commit 99151d5

Please sign in to comment.