forked from sermant-io/Sermant
-
Notifications
You must be signed in to change notification settings - Fork 0
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 sermant-io#1449 from daizhenyu/develop-mysql-write…
…-prohibition mysql数据库禁写插件测试问题修复
- Loading branch information
Showing
5 changed files
with
159 additions
and
31 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
53 changes: 53 additions & 0 deletions
53
...ain/java/com/huaweicloud/sermant/mariadbv3/interceptors/AbstractMariadbV3Interceptor.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,53 @@ | ||
/* | ||
* 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.mariadbv3.interceptors; | ||
|
||
import com.huaweicloud.sermant.core.common.LoggerFactory; | ||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.database.constant.DatabaseType; | ||
import com.huaweicloud.sermant.database.entity.DatabaseInfo; | ||
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor; | ||
|
||
import org.mariadb.jdbc.HostAddress; | ||
import org.mariadb.jdbc.client.impl.StandardClient; | ||
|
||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Abstract Interceptor of Mariadb3.X | ||
* | ||
* @author daizhenyu | ||
* @since 2024-01-30 | ||
**/ | ||
public abstract class AbstractMariadbV3Interceptor extends AbstractDatabaseInterceptor { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(); | ||
|
||
@Override | ||
protected void createAndCacheDatabaseInfo(ExecuteContext context) { | ||
DatabaseInfo info = new DatabaseInfo(DatabaseType.MYSQL); | ||
context.setLocalFieldValue(DATABASE_INFO, info); | ||
StandardClient client = (StandardClient) context.getObject(); | ||
if (client.getContext() != null) { | ||
info.setDatabaseName(client.getContext().getDatabase()); | ||
} else { | ||
LOGGER.warning("Failed to obtain database name."); | ||
} | ||
HostAddress hostAddress = client.getHostAddress(); | ||
info.setHostAddress(hostAddress.host); | ||
info.setPort(hostAddress.port); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../main/java/com/huaweicloud/sermant/mariadbv3/interceptors/ExecutePipelineInterceptor.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.mariadbv3.interceptors; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionManager; | ||
import com.huaweicloud.sermant.database.controller.DatabaseController; | ||
import com.huaweicloud.sermant.database.handler.DatabaseHandler; | ||
import com.huaweicloud.sermant.database.utils.SqlParserUtils; | ||
|
||
import org.mariadb.jdbc.message.ClientMessage; | ||
|
||
/** | ||
* executePipeline Method Interceptor | ||
* | ||
* @author daizhenyu | ||
* @since 2024-01-30 | ||
**/ | ||
public class ExecutePipelineInterceptor extends AbstractMariadbV3Interceptor { | ||
/** | ||
* No-argument constructor | ||
*/ | ||
public ExecutePipelineInterceptor() { | ||
} | ||
|
||
/** | ||
* Parametric constructor | ||
* | ||
* @param handler write operation handler | ||
*/ | ||
public ExecutePipelineInterceptor(DatabaseHandler handler) { | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
protected ExecuteContext doBefore(ExecuteContext context) { | ||
String database = getDataBaseInfo(context).getDatabaseName(); | ||
if (!DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases().contains(database)) { | ||
return context; | ||
} | ||
ClientMessage[] clientMessages = (ClientMessage[]) context.getArguments()[0]; | ||
for (ClientMessage clientMessage : clientMessages) { | ||
if (SqlParserUtils.isWriteOperation(clientMessage.description())) { | ||
DatabaseController.disableDatabaseWriteOperation(database, context); | ||
return context; | ||
} | ||
} | ||
return context; | ||
} | ||
} |
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