-
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 #1423 from daizhenyu/develop-mysql-write-prohibition
MongoDB数据库禁写开发
- Loading branch information
Showing
17 changed files
with
1,027 additions
and
0 deletions.
There are no files selected for viewing
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
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
27 changes: 27 additions & 0 deletions
27
sermant-plugins/sermant-database-write-prohibition/database-controller/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,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> |
64 changes: 64 additions & 0 deletions
64
...src/main/java/com/huaweicloud/sermant/database/config/DatabaseWriteProhibitionConfig.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,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; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...rc/main/java/com/huaweicloud/sermant/database/config/DatabaseWriteProhibitionManager.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,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(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...troller/src/main/java/com/huaweicloud/sermant/database/controller/DatabaseController.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,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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...se-controller/src/main/java/com/huaweicloud/sermant/database/handler/DatabaseHandler.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,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); | ||
} |
66 changes: 66 additions & 0 deletions
66
...c/main/java/com/huaweicloud/sermant/database/interceptor/AbstractDatabaseInterceptor.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,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); | ||
} |
Oops, something went wrong.