Skip to content

Commit

Permalink
Merge pull request sermant-io#1449 from daizhenyu/develop-mysql-write…
Browse files Browse the repository at this point in the history
…-prohibition

mysql数据库禁写插件测试问题修复
  • Loading branch information
Sherlockhan authored Mar 1, 2024
2 parents a322a95 + c8d5efe commit 340cb3b
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public ClassMatcher getClassMatcher() {

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[] {
MariadbV3EnhancementHelper.getSendQueryInterceptDeclarer()
return new InterceptDeclarer[]{
MariadbV3EnhancementHelper.getSendQueryInterceptDeclarer(),
MariadbV3EnhancementHelper.getExecutePipelineInterceptDeclarer()
};
}
}
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);
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionManager;
import com.huaweicloud.sermant.database.constant.DatabaseType;
import com.huaweicloud.sermant.database.controller.DatabaseController;
import com.huaweicloud.sermant.database.entity.DatabaseInfo;
import com.huaweicloud.sermant.database.handler.DatabaseHandler;
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor;
import com.huaweicloud.sermant.database.utils.SqlParserUtils;

import org.mariadb.jdbc.HostAddress;
import org.mariadb.jdbc.client.impl.StandardClient;
import org.mariadb.jdbc.message.ClientMessage;

/**
Expand All @@ -35,7 +30,7 @@
* @author daizhenyu
* @since 2024-01-30
**/
public class SendQueryInterceptor extends AbstractDatabaseInterceptor {
public class SendQueryInterceptor extends AbstractMariadbV3Interceptor {
/**
* 无参构造方法
*/
Expand All @@ -61,17 +56,4 @@ protected ExecuteContext doBefore(ExecuteContext context) {
}
return context;
}

@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());
}
HostAddress hostAddress = client.getHostAddress();
info.setHostAddress(hostAddress.host);
info.setPort(hostAddress.port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;
import com.huaweicloud.sermant.database.handler.DatabaseHandler;
import com.huaweicloud.sermant.mariadbv3.interceptors.ExecutePipelineInterceptor;
import com.huaweicloud.sermant.mariadbv3.interceptors.SendQueryInterceptor;

/**
* mariadb3.x拦截点辅助类
* mariadb3.x declarer helper
*
* @author daizhenyu
* @since 2024-01-30
Expand All @@ -35,49 +36,76 @@ public class MariadbV3EnhancementHelper {

private static final String SEND_QUERY_METHOD_NAME = "sendQuery";

private static final String EXECUTE_PIPELINE_METHOD_NAME = "executePipeline";

private MariadbV3EnhancementHelper() {
}

/**
* 获取ReplayClient类的ClassMatcher
* Get ClassMatcher of ReplayClient
*
* @return ClassMatcher 类匹配器
* @return ClassMatcher ClassMatcher
*/
public static ClassMatcher getReplayClientClassMatcher() {
return ClassMatcher.nameEquals(REPLAY_CLIENT_CLASS);
}

/**
* 获取StandardClient类的ClassMatcher
* Get ClassMatcher of StandardClient
*
* @return ClassMatcher 类匹配器
* @return ClassMatcher ClassMatcher
*/
public static ClassMatcher getStandardClientClassMatcher() {
return ClassMatcher.nameEquals(STANDARD_CLIENT_CLASS);
}

/**
* 获取sendQuery方法无参拦截器
* Get No-argument Interceptor of sendQuery Method
*
* @return InterceptDeclarer sendQuery方法无参拦截器
* @return InterceptDeclarer No-argument Interceptor of sendQuery Method
*/
public static InterceptDeclarer getSendQueryInterceptDeclarer() {
return InterceptDeclarer.build(getSendQueryMethodMatcher(),
new SendQueryInterceptor());
}

/**
* 获取sendQuery方法有参拦截器
* Get Parametric Interceptor of sendQuery Method
*
* @param handler 数据库自定义处理器
* @return InterceptDeclarer sendQuery方法有参拦截器
* @param handler write operation handler
* @return InterceptDeclarer Parametric Interceptor of sendQuery Method
*/
public static InterceptDeclarer getSendQueryInterceptDeclarer(DatabaseHandler handler) {
return InterceptDeclarer.build(getSendQueryMethodMatcher(),
new SendQueryInterceptor(handler));
}

/**
* Get No-argument Interceptor of executePipeline Method
*
* @return InterceptDeclarer No-argument Interceptor of executePipeline Method
*/
public static InterceptDeclarer getExecutePipelineInterceptDeclarer() {
return InterceptDeclarer.build(getExecutePipelineMethodMatcher(),
new ExecutePipelineInterceptor());
}

/**
* Get Parametric Interceptor of executePipeline Method
*
* @param handler write operation handler
* @return InterceptDeclarer Parametric Interceptor of executePipeline Method
*/
public static InterceptDeclarer getExecutePipelineInterceptDeclarer(DatabaseHandler handler) {
return InterceptDeclarer.build(getExecutePipelineMethodMatcher(),
new ExecutePipelineInterceptor(handler));
}

private static MethodMatcher getSendQueryMethodMatcher() {
return MethodMatcher.nameEquals(SEND_QUERY_METHOD_NAME);
}

private static MethodMatcher getExecutePipelineMethodMatcher() {
return MethodMatcher.nameEquals(EXECUTE_PIPELINE_METHOD_NAME);
}
}

0 comments on commit 340cb3b

Please sign in to comment.