From 5d26305312dd0a20e02b3dfd8f2663440d838424 Mon Sep 17 00:00:00 2001 From: daizhenyu <1449308021@qq.com> Date: Mon, 4 Mar 2024 09:54:43 +0800 Subject: [PATCH] Fix mongodb database write prohibition plugin bug Signed-off-by: daizhenyu <1449308021@qq.com> --- report/pom.xml | 17 +- .../AbstractDatabaseInterceptor.java | 15 + .../AbstractMongoDbInterceptor.java | 11 +- .../utils/MongoDbEnhancementHelper.java | 220 ------------ .../mongodb-3.x-plugin/pom.xml | 68 ++++ .../constant/MethodParamTypeName.java | 86 +++++ .../CommandOperationHelperDeclarer.java | 10 +- .../MixedBulkWriteOperationDeclarer.java | 12 +- .../GeneralExecuteInterceptor.java} | 38 +- .../MixedBulkWriteOperationInterceptor.java} | 43 ++- .../utils/MongoDbV3EnhancementHelper.java | 285 +++++++++++++++ ....core.plugin.agent.declarer.PluginDeclarer | 17 + .../interceptors/ConnectionSourceImpl.java | 70 ++++ .../GeneralExecuteInterceptorTest.java} | 37 +- ...xedBulkWriteOperationInterceptorTest.java} | 37 +- .../pom.xml | 2 +- .../constant/MethodParamTypeName.java | 49 +-- .../CommandOperationHelperDeclarer.java | 40 +++ .../MixedBulkWriteOperationDeclarer.java | 42 +++ .../SyncOperationHelperDeclarer.java | 40 +++ .../GeneralExecuteInterceptor.java} | 40 ++- .../MixedBulkWriteOperationInterceptor.java | 30 +- .../utils/MongoDbV4EnhancementHelper.java | 333 ++++++++++++++++++ ....core.plugin.agent.declarer.PluginDeclarer | 5 +- .../interceptors/ConnectionSourceImpl.java | 8 +- .../GeneralExecuteInterceptorTest.java} | 23 +- ...ixedBulkWriteOperationInterceptorTest.java | 19 +- .../interceptors/ExecuteInterceptor.java | 18 +- .../ExecuteServerInterceptor.java | 16 +- .../interceptors/PrepareInterceptor.java | 16 +- .../interceptors/SendQueryInterceptor.java | 16 +- .../pom.xml | 9 +- 32 files changed, 1229 insertions(+), 443 deletions(-) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors => database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor}/AbstractMongoDbInterceptor.java (72%) delete mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/utils/MongoDbEnhancementHelper.java create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/pom.xml create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/constant/MethodParamTypeName.java rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb => mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3}/declarers/CommandOperationHelperDeclarer.java (78%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb => mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3}/declarers/MixedBulkWriteOperationDeclarer.java (76%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptor.java => mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptor.java} (57%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptor.java => mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptor.java} (51%) create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/utils/MongoDbV3EnhancementHelper.java create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/ConnectionSourceImpl.java rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptorTest.java => mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptorTest.java} (71%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptorTest.java => mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptorTest.java} (71%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin => mongodb-4.x-plugin}/pom.xml (97%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb => mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4}/constant/MethodParamTypeName.java (60%) create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/CommandOperationHelperDeclarer.java create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/MixedBulkWriteOperationDeclarer.java create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/SyncOperationHelperDeclarer.java rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptor.java => mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptor.java} (55%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb => mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4}/interceptors/MixedBulkWriteOperationInterceptor.java (66%) create mode 100644 sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/utils/MongoDbV4EnhancementHelper.java rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin => mongodb-4.x-plugin}/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer (73%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb => mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4}/interceptors/ConnectionSourceImpl.java (90%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptorTest.java => mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptorTest.java} (83%) rename sermant-plugins/sermant-database-write-prohibition/{mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb => mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4}/interceptors/MixedBulkWriteOperationInterceptorTest.java (86%) diff --git a/report/pom.xml b/report/pom.xml index a86e92c105..178c1d05ff 100644 --- a/report/pom.xml +++ b/report/pom.xml @@ -48,7 +48,22 @@ com.huaweicloud.sermant - mongodb-3.x-4.x-plugin + mongodb-3.x-plugin + ${project.version} + + + com.huaweicloud.sermant + mongodb-4.x-plugin + ${project.version} + + + com.huaweicloud.sermant + mysql-mariadb-2.x-plugin + ${project.version} + + + com.huaweicloud.sermant + mysql-mariadb-3.x-plugin ${project.version} diff --git a/sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor/AbstractDatabaseInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor/AbstractDatabaseInterceptor.java index 0efab53f42..9a8beabd5f 100644 --- a/sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor/AbstractDatabaseInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor/AbstractDatabaseInterceptor.java @@ -109,4 +109,19 @@ protected void handleWriteOperationIfWriteDisabled(String sql, String databaseNa DatabaseController.disableDatabaseWriteOperation(databaseName, context); } } + + /** + * If the mongodb database has write prohibition enabled and it is a write operation, the write prohibition logic + * will be executed + * + * @param databaseName databaseName + * @param prohibitionDatabases prohibition write database collection + * @param context contextual information + */ + protected void handleWriteOperationIfWriteDisabled(String databaseName, + Set prohibitionDatabases, ExecuteContext context) { + if (prohibitionDatabases.contains(databaseName)) { + DatabaseController.disableDatabaseWriteOperation(databaseName, context); + } + } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/AbstractMongoDbInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor/AbstractMongoDbInterceptor.java similarity index 72% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/AbstractMongoDbInterceptor.java rename to sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor/AbstractMongoDbInterceptor.java index b10a0300b9..3bbfb7ab03 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/AbstractMongoDbInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/database-controller/src/main/java/com/huaweicloud/sermant/database/interceptor/AbstractMongoDbInterceptor.java @@ -14,15 +14,13 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.database.interceptor; 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.interceptor.AbstractDatabaseInterceptor; /** - * mongodb抽象interceptor + * MongoDB Abstract Interceptor * * @author daizhenyu * @since 2024-02-02 @@ -31,9 +29,8 @@ public abstract class AbstractMongoDbInterceptor extends AbstractDatabaseInterce @Override public ExecuteContext doBefore(ExecuteContext context) { String database = getDataBaseInfo(context).getDatabaseName(); - if (DatabaseWriteProhibitionManager.getMongoDbProhibitionDatabases().contains(database)) { - DatabaseController.disableDatabaseWriteOperation(database, context); - } + handleWriteOperationIfWriteDisabled(database, + DatabaseWriteProhibitionManager.getMongoDbProhibitionDatabases(), context); return context; } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/utils/MongoDbEnhancementHelper.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/utils/MongoDbEnhancementHelper.java deleted file mode 100644 index 3ed8c05f46..0000000000 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/utils/MongoDbEnhancementHelper.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * 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.mongodb.utils; - -import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; -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.mongodb.constant.MethodParamTypeName; -import com.huaweicloud.sermant.mongodb.interceptors.ExecuteCommandInterceptor; -import com.huaweicloud.sermant.mongodb.interceptors.ExecuteRetryableCommandInterceptor; -import com.huaweicloud.sermant.mongodb.interceptors.ExecuteWriteCommandInterceptor; -import com.huaweicloud.sermant.mongodb.interceptors.MixedBulkWriteOperationInterceptor; - -/** - * mongodb拦截点辅助类 - * - * @author daizhenyu - * @since 2024-01-16 - **/ -public class MongoDbEnhancementHelper { - private static final String MIXED_BULK_WRITE_CLASS = "com.mongodb.internal.operation.MixedBulkWriteOperation"; - - private static final String COMMAND_OPERATION_CLASS = "com.mongodb.internal.operation.CommandOperationHelper"; - - private static final String EXECUTE_METHOD_NAME = "execute"; - - private static final String EXECUTE_COMMAND_METHOD_NAME = "executeCommand"; - - private static final String EXECUTE_WRITE_COMMAND_METHOD_NAME = "executeWriteCommand"; - - private static final String EXECUTE_RETRYABLE_COMMAND_METHOD_NAME = "executeRetryableCommand"; - - private static final int METHOD_PARAM_COUNT = 9; - - private static final String[] EXECUTE_COMMAND_PARAMS_TYPE = { - MethodParamTypeName.STRING_CLASS_NAME, - MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, - MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, - MethodParamTypeName.DECODER_CLASS_NAME, - MethodParamTypeName.CONNECTION_SOURCE_CLASS_NAME, - MethodParamTypeName.CONNECTION_CLASS_NAME, - MethodParamTypeName.READ_PREFERENCE_CLASS_NAME - }; - - private static final String[] EXECUTE_RETRY_COMMAND_PARAMS_TYPE = { - MethodParamTypeName.WRITE_BINDING_CLASS_NAME, - MethodParamTypeName.STRING_CLASS_NAME, - MethodParamTypeName.READ_PREFERENCE_CLASS_NAME, - MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, - MethodParamTypeName.DECODER_CLASS_NAME, - MethodParamTypeName.COMMAND_CREATOR_CLASS_NAME, - MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME, - MethodParamTypeName.FUNCTION_CLASS_NAME - }; - - private MongoDbEnhancementHelper() { - } - - /** - * 获取CommandOperationHelper类的ClassMatcher - * - * @return ClassMatcher 类匹配器 - */ - public static ClassMatcher getCommandOperationHelperClassMatcher() { - return ClassMatcher.nameEquals(COMMAND_OPERATION_CLASS); - } - - /** - * 获取MixedBulkWriteOperation类的ClassMatcher - * - * @return ClassMatcher 类匹配器 - */ - public static ClassMatcher getMixedBulkWriteOperationClassMatcher() { - return ClassMatcher.nameEquals(MIXED_BULK_WRITE_CLASS); - } - - /** - * 获取MixedBulkWriteOperation写操作无参拦截器数组 - * - * @return InterceptDeclarer[] MixedBulkWriteOperation写操作无参拦截器数组 - */ - public static InterceptDeclarer[] getMixedBulkWriteOperationInterceptDeclarers() { - MixedBulkWriteOperationInterceptor mixedBulkWriteOperationInterceptor = - new MixedBulkWriteOperationInterceptor(); - return new InterceptDeclarer[]{ - InterceptDeclarer.build(getExecuteMethodMatcher(), mixedBulkWriteOperationInterceptor), - }; - } - - /** - * 获取MixedBulkWriteOperation execute方法无参拦截器 - * - * @return InterceptDeclarer MixedBulkWriteOperation execute方法无参拦截器 - */ - public static InterceptDeclarer getExecuteInterceptDeclarer() { - return InterceptDeclarer.build(getExecuteMethodMatcher(), - new MixedBulkWriteOperationInterceptor()); - } - - /** - * 获取MixedBulkWriteOperation execute方法有参拦截器 - * - * @param handler 数据库自定义处理器 - * @return InterceptDeclarer MixedBulkWriteOperation execute方法有参拦截器 - */ - public static InterceptDeclarer getExecuteInterceptDeclarer(DatabaseHandler handler) { - return InterceptDeclarer.build(getExecuteMethodMatcher(), - new MixedBulkWriteOperationInterceptor(handler)); - } - - /** - * 获取CommandOperationHelper写操作无参拦截器数组 - * - * @return InterceptDeclarer[] CommandOperationHelper写操作无参拦截器数组 - */ - public static InterceptDeclarer[] getCommandOperationHelperInterceptDeclarers() { - return new InterceptDeclarer[]{ - InterceptDeclarer.build(getExecuteCommandMethodMatcher(), new ExecuteCommandInterceptor()), - InterceptDeclarer.build(getExecuteWriteCommandMethodMatcher(), new ExecuteWriteCommandInterceptor()), - InterceptDeclarer.build(getExecuteRetryableCommandMethodMatcher(), - new ExecuteRetryableCommandInterceptor()) - }; - } - - /** - * 获取CommandOperationHelper executeCommand方法无参拦截器 - * - * @return InterceptDeclarer CommandOperationHelper executeCommand方法无参拦截器 - */ - public static InterceptDeclarer getExecuteCommandInterceptDeclarer() { - return InterceptDeclarer.build(getExecuteCommandMethodMatcher(), new ExecuteCommandInterceptor()); - } - - /** - * 获取CommandOperationHelper executeCommand方法有参拦截器 - * - * @param handler 数据库自定义处理器 - * @return InterceptDeclarer CommandOperationHelper executeCommand方法有参拦截器 - */ - public static InterceptDeclarer getExecuteCommandInterceptDeclarer(DatabaseHandler handler) { - return InterceptDeclarer.build(getExecuteCommandMethodMatcher(), new ExecuteCommandInterceptor(handler)); - } - - /** - * 获取CommandOperationHelper executeWriteCommand方法无参拦截器 - * - * @return InterceptDeclarer CommandOperationHelper executeWriteCommand方法无参拦截器 - */ - public static InterceptDeclarer getExecuteWriteCommandInterceptDeclarer() { - return InterceptDeclarer.build(getExecuteWriteCommandMethodMatcher(), new ExecuteWriteCommandInterceptor()); - } - - /** - * 获取CommandOperationHelper executeWriteCommand方法有参拦截器 - * - * @param handler 数据库自定义处理器 - * @return InterceptDeclarer CommandOperationHelper executeWriteCommand方法有参拦截器 - */ - public static InterceptDeclarer getExecuteWriteCommandInterceptDeclarer( - DatabaseHandler handler) { - return InterceptDeclarer - .build(getExecuteWriteCommandMethodMatcher(), new ExecuteWriteCommandInterceptor(handler)); - } - - /** - * 获取CommandOperationHelper executeRetryableCommand方法无参拦截器 - * - * @return InterceptDeclarer CommandOperationHelper executeRetryableCommand方法无参拦截器 - */ - public static InterceptDeclarer getExecuteRetryableCommandInterceptDeclarer() { - return InterceptDeclarer - .build(getExecuteRetryableCommandMethodMatcher(), new ExecuteRetryableCommandInterceptor()); - } - - /** - * 获取CommandOperationHelper executeRetryableCommand方法有参拦截器 - * - * @param handler 数据库自定义处理器 - * @return InterceptDeclarer CommandOperationHelper executeRetryableCommand方法有参拦截器 - */ - public static InterceptDeclarer getExecuteRetryableCommandInterceptDeclarer( - DatabaseHandler handler) { - return InterceptDeclarer - .build(getExecuteRetryableCommandMethodMatcher(), new ExecuteRetryableCommandInterceptor(handler)); - } - - private static MethodMatcher getExecuteMethodMatcher() { - return MethodMatcher.nameEquals(EXECUTE_METHOD_NAME); - } - - private static MethodMatcher getExecuteCommandMethodMatcher() { - return MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) - .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE)); - } - - private static MethodMatcher getExecuteWriteCommandMethodMatcher() { - return MethodMatcher.nameEquals(EXECUTE_WRITE_COMMAND_METHOD_NAME) - .and(MethodMatcher.paramCountEquals(METHOD_PARAM_COUNT)); - } - - private static MethodMatcher getExecuteRetryableCommandMethodMatcher() { - return MethodMatcher.nameEquals(EXECUTE_RETRYABLE_COMMAND_METHOD_NAME) - .and(MethodMatcher.paramTypesEqual(EXECUTE_RETRY_COMMAND_PARAMS_TYPE)); - } -} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/pom.xml b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/pom.xml new file mode 100644 index 0000000000..a99c97aec7 --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/pom.xml @@ -0,0 +1,68 @@ + + + + sermant-database-write-prohibition + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + mongodb-3.x-plugin + + + 8 + 8 + false + plugin + 3.7.0 + + + + + com.huaweicloud.sermant + sermant-agentcore-core + provided + + + org.mongodb + mongodb-driver-sync + ${mongodb.version} + provided + + + com.huaweicloud.sermant + database-controller + ${project.version} + + + junit + junit + test + + + org.mockito + mockito-inline + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + org.apache.maven.plugins + maven-shade-plugin + + + + + \ No newline at end of file diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/constant/MethodParamTypeName.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/constant/MethodParamTypeName.java new file mode 100644 index 0000000000..95dddea2fe --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/constant/MethodParamTypeName.java @@ -0,0 +1,86 @@ +/* + * 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.mongodbv3.constant; + +/** + * Fully qualified name constant class of mongodb3.x method parameter type + * + * @author daizhenyu + * @since 2024-01-27 + **/ +public class MethodParamTypeName { + /** + * String Class Fully Qualified Name + */ + public static final String STRING_CLASS_NAME = "java.lang.String"; + + /** + * WriteBinding Class Fully Qualified Name + */ + public static final String WRITE_BINDING_CLASS_NAME = "com.mongodb.binding.WriteBinding"; + + /** + * ReadPreference Class Fully Qualified Name + */ + public static final String READ_PREFERENCE_CLASS_NAME = "com.mongodb.ReadPreference"; + + /** + * FieldNameValidator Class Fully Qualified Name + */ + public static final String FIELD_NAME_VALIDATOR_CLASS_NAME = "org.bson.FieldNameValidator"; + + /** + * Decoder Class Fully Qualified Name + */ + public static final String DECODER_CLASS_NAME = "org.bson.codecs.Decoder"; + + /** + * CommandCreator Class Fully Qualified Name + */ + public static final String COMMAND_CREATOR_CLASS_NAME = + "com.mongodb.operation.CommandOperationHelper$CommandCreator"; + + /** + * CommandWriteTransformer Interface Fully Qualified Name + */ + public static final String COMMAND_WRITE_TRANSFORMER_CLASS_NAME = + "com.mongodb.operation.CommandOperationHelper$CommandWriteTransformer"; + + /** + * CommandTransformer Interface Fully Qualified Name + */ + public static final String COMMAND_TRANSFORMER_CLASS_NAME = + "com.mongodb.operation.CommandOperationHelper$CommandTransformer"; + + /** + * Function Class Fully Qualified Name + */ + public static final String FUNCTION_CLASS_NAME = "com.mongodb.Function"; + + /** + * BsonDocument Class Fully Qualified Name + */ + public static final String BSON_DOCUMENT_CLASS_NAME = "org.bson.BsonDocument"; + + /** + * Connection Class Fully Qualified Name + */ + public static final String CONNECTION_CLASS_NAME = "com.mongodb.connection.Connection"; + + private MethodParamTypeName() { + } +} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/declarers/CommandOperationHelperDeclarer.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/declarers/CommandOperationHelperDeclarer.java similarity index 78% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/declarers/CommandOperationHelperDeclarer.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/declarers/CommandOperationHelperDeclarer.java index 8701d8f9f1..f3d1653f81 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/declarers/CommandOperationHelperDeclarer.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/declarers/CommandOperationHelperDeclarer.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.declarers; +package com.huaweicloud.sermant.mongodbv3.declarers; import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; -import com.huaweicloud.sermant.mongodb.utils.MongoDbEnhancementHelper; +import com.huaweicloud.sermant.mongodbv3.utils.MongoDbV3EnhancementHelper; /** - * mongodb写操作增强声明器 + * CommandOperationHelper Declarer * * @author daizhenyu * @since 2024-01-16 @@ -30,11 +30,11 @@ public class CommandOperationHelperDeclarer extends AbstractPluginDeclarer { @Override public ClassMatcher getClassMatcher() { - return MongoDbEnhancementHelper.getCommandOperationHelperClassMatcher(); + return MongoDbV3EnhancementHelper.getCommandOperationHelperClassMatcher(); } @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return MongoDbEnhancementHelper.getCommandOperationHelperInterceptDeclarers(); + return MongoDbV3EnhancementHelper.getCommandOperationHelperInterceptDeclarers(); } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/declarers/MixedBulkWriteOperationDeclarer.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/declarers/MixedBulkWriteOperationDeclarer.java similarity index 76% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/declarers/MixedBulkWriteOperationDeclarer.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/declarers/MixedBulkWriteOperationDeclarer.java index 1fdb92f596..18efd26be4 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/declarers/MixedBulkWriteOperationDeclarer.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/declarers/MixedBulkWriteOperationDeclarer.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.declarers; +package com.huaweicloud.sermant.mongodbv3.declarers; import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; -import com.huaweicloud.sermant.mongodb.utils.MongoDbEnhancementHelper; +import com.huaweicloud.sermant.mongodbv3.utils.MongoDbV3EnhancementHelper; /** - * MixedBulkWriteOperation类增强声明器 + * MixedBulkWriteOperation Declarer * * @author daizhenyu * @since 2024-01-16 @@ -30,11 +30,13 @@ public class MixedBulkWriteOperationDeclarer extends AbstractPluginDeclarer { @Override public ClassMatcher getClassMatcher() { - return MongoDbEnhancementHelper.getMixedBulkWriteOperationClassMatcher(); + return MongoDbV3EnhancementHelper.getMixedBulkWriteOperationClassMatcher(); } @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return MongoDbEnhancementHelper.getMixedBulkWriteOperationInterceptDeclarers(); + return new InterceptDeclarer[]{ + MongoDbV3EnhancementHelper.getExecuteInterceptDeclarer(), + }; } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptor.java similarity index 57% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptor.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptor.java index c54ac464d6..b9d10ef709 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptor.java @@ -14,38 +14,40 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv3.interceptors; 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.handler.DatabaseHandler; +import com.huaweicloud.sermant.database.interceptor.AbstractMongoDbInterceptor; import com.mongodb.ServerAddress; -import com.mongodb.connection.ConnectionDescription; -import com.mongodb.internal.connection.Connection; +import com.mongodb.binding.ConnectionSource; +import com.mongodb.binding.WriteBinding; +import com.mongodb.connection.ServerDescription; + +import java.util.Optional; /** - * 执行executeCommand方法的拦截器 + * CommandOperationHelper executeCommand、executeRetryableCommand Method Interceptor * * @author daizhenyu * @since 2024-01-18 **/ -public class ExecuteCommandInterceptor extends AbstractMongoDbInterceptor { - private static final int PARAM_INDEX = 5; - +public class GeneralExecuteInterceptor extends AbstractMongoDbInterceptor { /** - * 无参构造方法 + * No-argument constructor */ - public ExecuteCommandInterceptor() { + public GeneralExecuteInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ - public ExecuteCommandInterceptor(DatabaseHandler handler) { + public GeneralExecuteInterceptor(DatabaseHandler handler) { this.handler = handler; } @@ -53,11 +55,13 @@ public ExecuteCommandInterceptor(DatabaseHandler handler) { protected void createAndCacheDatabaseInfo(ExecuteContext context) { DatabaseInfo info = new DatabaseInfo(DatabaseType.MONGODB); context.setLocalFieldValue(DATABASE_INFO, info); - info.setDatabaseName((String) context.getArguments()[0]); - Connection connection = (Connection) context.getArguments()[PARAM_INDEX]; - ConnectionDescription description = connection.getDescription(); - if (description != null) { - ServerAddress serverAddress = description.getServerAddress(); + info.setDatabaseName((String) context.getArguments()[1]); + ServerAddress serverAddress = Optional.ofNullable((WriteBinding) context.getArguments()[0]) + .map(WriteBinding::getWriteConnectionSource) + .map(ConnectionSource::getServerDescription) + .map(ServerDescription::getAddress) + .orElse(null); + if (serverAddress != null) { info.setHostAddress(serverAddress.getHost()); info.setPort(serverAddress.getPort()); } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptor.java similarity index 51% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptor.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptor.java index 26010e94cc..3320187d9c 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptor.java @@ -14,35 +14,41 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv3.interceptors; 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.handler.DatabaseHandler; +import com.huaweicloud.sermant.database.interceptor.AbstractMongoDbInterceptor; import com.mongodb.ServerAddress; -import com.mongodb.internal.binding.WriteBinding; +import com.mongodb.binding.ConnectionSource; +import com.mongodb.binding.WriteBinding; +import com.mongodb.connection.ServerDescription; +import com.mongodb.operation.MixedBulkWriteOperation; + +import java.util.Optional; /** - * 执行重试write操作的拦截器 + * MixedBulkWriteOperation Interceptor * * @author daizhenyu - * @since 2024-01-18 + * @since 2024-01-16 **/ -public class ExecuteRetryableCommandInterceptor extends AbstractMongoDbInterceptor { +public class MixedBulkWriteOperationInterceptor extends AbstractMongoDbInterceptor { /** - * 无参构造方法 + * No-argument constructor */ - public ExecuteRetryableCommandInterceptor() { + public MixedBulkWriteOperationInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ - public ExecuteRetryableCommandInterceptor(DatabaseHandler handler) { + public MixedBulkWriteOperationInterceptor(DatabaseHandler handler) { this.handler = handler; } @@ -50,12 +56,15 @@ public ExecuteRetryableCommandInterceptor(DatabaseHandler handler) { protected void createAndCacheDatabaseInfo(ExecuteContext context) { DatabaseInfo info = new DatabaseInfo(DatabaseType.MONGODB); context.setLocalFieldValue(DATABASE_INFO, info); - ServerAddress serverAddress = ((WriteBinding) context.getArguments()[0]) - .getWriteConnectionSource() - .getServerDescription() - .getAddress(); - info.setDatabaseName((String) context.getArguments()[1]); - info.setHostAddress(serverAddress.getHost()); - info.setPort(serverAddress.getPort()); + info.setDatabaseName(((MixedBulkWriteOperation) context.getObject()).getNamespace().getDatabaseName()); + ServerAddress serverAddress = Optional.ofNullable((WriteBinding) context.getArguments()[0]) + .map(WriteBinding::getWriteConnectionSource) + .map(ConnectionSource::getServerDescription) + .map(ServerDescription::getAddress) + .orElse(null); + if (serverAddress != null) { + info.setHostAddress(serverAddress.getHost()); + info.setPort(serverAddress.getPort()); + } } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/utils/MongoDbV3EnhancementHelper.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/utils/MongoDbV3EnhancementHelper.java new file mode 100644 index 0000000000..01e1cd0218 --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv3/utils/MongoDbV3EnhancementHelper.java @@ -0,0 +1,285 @@ +/* + * 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.mongodbv3.utils; + +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +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.mongodbv3.constant.MethodParamTypeName; +import com.huaweicloud.sermant.mongodbv3.interceptors.GeneralExecuteInterceptor; +import com.huaweicloud.sermant.mongodbv3.interceptors.MixedBulkWriteOperationInterceptor; + +/** + * mongodb declarer helper + * + * @author daizhenyu + * @since 2024-01-16 + **/ +public class MongoDbV3EnhancementHelper { + private static final String MIXED_BULK_WRITE_CLASS = "com.mongodb.operation.MixedBulkWriteOperation"; + + private static final String COMMAND_OPERATION_CLASS = "com.mongodb.operation.CommandOperationHelper"; + + private static final String EXECUTE_METHOD_NAME = "execute"; + + private static final String EXECUTE_COMMAND_METHOD_NAME = "executeCommand"; + + private static final String EXECUTE_WRAPPED_COMMAND_PROTOCOL_METHOD_NAME = "executeWrappedCommandProtocol"; + + private static final String EXECUTE_RETRYABLE_COMMAND_METHOD_NAME = "executeRetryableCommand"; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_FIRST = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_SECOND = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_THIRD = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] PROTOCOL_PARAMS_TYPE_FIRST = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME, + MethodParamTypeName.COMMAND_TRANSFORMER_CLASS_NAME + }; + + private static final String[] PROTOCOL_PARAMS_TYPE_SECOND = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME, + MethodParamTypeName.COMMAND_TRANSFORMER_CLASS_NAME + }; + + private static final String[] PROTOCOL_PARAMS_TYPE_THIRD = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.COMMAND_TRANSFORMER_CLASS_NAME + }; + + private static final String[] COMMON_EXECUTE_PARAMS_TYPE = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME + }; + + private static final String[] EXECUTE_RETRY_COMMAND_PARAMS_TYPE_FIRST = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.READ_PREFERENCE_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.COMMAND_CREATOR_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME, + MethodParamTypeName.FUNCTION_CLASS_NAME + }; + + private static final String[] EXECUTE_RETRY_COMMAND_PARAMS_TYPE_SECOND = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.READ_PREFERENCE_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.COMMAND_CREATOR_CLASS_NAME, + MethodParamTypeName.COMMAND_TRANSFORMER_CLASS_NAME + }; + + private MongoDbV3EnhancementHelper() { + } + + /** + * Get ClassMatcher of CommandOperationHelper Class + * + * @return ClassMatcher ClassMatcher + */ + public static ClassMatcher getCommandOperationHelperClassMatcher() { + return ClassMatcher.nameEquals(COMMAND_OPERATION_CLASS); + } + + /** + * Get ClassMatcher of MixedBulkWriteOperation Class + * + * @return ClassMatcher ClassMatcher + */ + public static ClassMatcher getMixedBulkWriteOperationClassMatcher() { + return ClassMatcher.nameEquals(MIXED_BULK_WRITE_CLASS); + } + + /** + * Get No-argument Interceptor of MixedBulkWriteOperation execute Method + * + * @return InterceptDeclarer No-argument Interceptor of MixedBulkWriteOperation execute Method + */ + public static InterceptDeclarer getExecuteInterceptDeclarer() { + return InterceptDeclarer.build(getExecuteMethodMatcher(), new MixedBulkWriteOperationInterceptor()); + } + + /** + * Get Parametric Interceptor of MixedBulkWriteOperation execute Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of MixedBulkWriteOperation execute Method + */ + public static InterceptDeclarer getExecuteInterceptDeclarer(DatabaseHandler handler) { + return InterceptDeclarer.build(getExecuteMethodMatcher(), + new MixedBulkWriteOperationInterceptor(handler)); + } + + /** + * Get No-argument Interceptor Array of CommandOperationHelper + * + * @return InterceptDeclarer[] No-argument Interceptor Array of CommandOperationHelper + */ + public static InterceptDeclarer[] getCommandOperationHelperInterceptDeclarers() { + GeneralExecuteInterceptor generalExecuteInterceptor = new GeneralExecuteInterceptor(); + return new InterceptDeclarer[]{ + InterceptDeclarer.build(getExecuteWrappedCommandProtocolMethodMatcher(), + generalExecuteInterceptor), + InterceptDeclarer.build(getExecuteCommandMethodMatcher(), + generalExecuteInterceptor), + InterceptDeclarer.build(getExecuteRetryableCommandMethodMatcher(), + generalExecuteInterceptor) + }; + } + + /** + * Get No-argument Interceptor of CommandOperationHelper executeCommand Method + * + * @return InterceptDeclarer No-argument Interceptor of CommandOperationHelper executeCommand Method + */ + public static InterceptDeclarer getExecuteCommandInterceptDeclarer() { + return InterceptDeclarer + .build(getExecuteCommandMethodMatcher(), new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of CommandOperationHelper executeCommand Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of CommandOperationHelper executeCommand Method + */ + public static InterceptDeclarer getExecuteCommandInterceptDeclarer(DatabaseHandler handler) { + return InterceptDeclarer + .build(getExecuteCommandMethodMatcher(), new GeneralExecuteInterceptor(handler)); + } + + /** + * Get No-argument Interceptor of CommandOperationHelper executeWrappedCommandProtocol Method + * + * @return InterceptDeclarer No-argument Interceptor of CommandOperationHelper executeWrappedCommandProtocol Method + */ + public static InterceptDeclarer getExecuteWrappedCommandProtocolInterceptDeclarer() { + return InterceptDeclarer + .build(getExecuteWrappedCommandProtocolMethodMatcher(), new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of CommandOperationHelper executeWrappedCommandProtocol Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of CommandOperationHelper executeWrappedCommandProtocol Method + */ + public static InterceptDeclarer getExecuteWrappedCommandProtocolInterceptDeclarer( + DatabaseHandler handler) { + return InterceptDeclarer + .build(getExecuteWrappedCommandProtocolMethodMatcher(), new GeneralExecuteInterceptor(handler)); + } + + /** + * Get No-argument Interceptor of CommandOperationHelper executeRetryableCommand Method + * + * @return InterceptDeclarer No-argument Interceptor of CommandOperationHelper executeRetryableCommand Method + */ + public static InterceptDeclarer getExecuteRetryableCommandInterceptDeclarer() { + return InterceptDeclarer + .build(getExecuteRetryableCommandMethodMatcher(), new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of CommandOperationHelper executeRetryableCommand Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of CommandOperationHelper executeRetryableCommand Method + */ + public static InterceptDeclarer getExecuteRetryableCommandInterceptDeclarer( + DatabaseHandler handler) { + return InterceptDeclarer + .build(getExecuteRetryableCommandMethodMatcher(), new GeneralExecuteInterceptor(handler)); + } + + private static MethodMatcher getExecuteMethodMatcher() { + return MethodMatcher.nameEquals(EXECUTE_METHOD_NAME); + } + + private static MethodMatcher getExecuteCommandMethodMatcher() { + return (MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_FIRST))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_SECOND))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_THIRD))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(COMMON_EXECUTE_PARAMS_TYPE))); + } + + private static MethodMatcher getExecuteWrappedCommandProtocolMethodMatcher() { + return (MethodMatcher.nameEquals(EXECUTE_WRAPPED_COMMAND_PROTOCOL_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(PROTOCOL_PARAMS_TYPE_FIRST))) + .or(MethodMatcher.nameEquals(EXECUTE_WRAPPED_COMMAND_PROTOCOL_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(PROTOCOL_PARAMS_TYPE_SECOND))) + .or(MethodMatcher.nameEquals(EXECUTE_WRAPPED_COMMAND_PROTOCOL_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(PROTOCOL_PARAMS_TYPE_THIRD))) + .or(MethodMatcher.nameEquals(EXECUTE_WRAPPED_COMMAND_PROTOCOL_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(COMMON_EXECUTE_PARAMS_TYPE))); + } + + private static MethodMatcher getExecuteRetryableCommandMethodMatcher() { + return (MethodMatcher.nameEquals(EXECUTE_RETRYABLE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_RETRY_COMMAND_PARAMS_TYPE_FIRST))) + .or(MethodMatcher.nameEquals(EXECUTE_RETRYABLE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_RETRY_COMMAND_PARAMS_TYPE_SECOND))); + } +} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer new file mode 100644 index 0000000000..b6a8f400fa --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -0,0 +1,17 @@ +# +# 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. +# +com.huaweicloud.sermant.mongodbv3.declarers.MixedBulkWriteOperationDeclarer +com.huaweicloud.sermant.mongodbv3.declarers.CommandOperationHelperDeclarer \ No newline at end of file diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/ConnectionSourceImpl.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/ConnectionSourceImpl.java new file mode 100644 index 0000000000..4868d13c93 --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/ConnectionSourceImpl.java @@ -0,0 +1,70 @@ +/* + * 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.mongodbv3.interceptors; + +import com.mongodb.binding.ConnectionSource; +import com.mongodb.connection.Connection; +import com.mongodb.connection.ServerDescription; +import com.mongodb.session.SessionContext; + +/** + * Implementation class of ConnectionSource interface + * + * @author daizhenyu + * @since 2024-02-05 + **/ +public class ConnectionSourceImpl implements ConnectionSource { + private ServerDescription description; + + /** + * Construction method + * + * @param description Server information + */ + public ConnectionSourceImpl(ServerDescription description) { + this.description = description; + } + @Override + public ServerDescription getServerDescription() { + return description; + } + + @Override + public SessionContext getSessionContext() { + return null; + } + + @Override + public Connection getConnection() { + return null; + } + + @Override + public int getCount() { + return 0; + } + + @Override + public ConnectionSource retain() { + return null; + } + + @Override + public void release() { + + } +} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptorTest.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptorTest.java similarity index 71% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptorTest.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptorTest.java index 0861296572..d813cfc6bb 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteCommandInterceptorTest.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/GeneralExecuteInterceptorTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv3.interceptors; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionConfig; @@ -22,10 +22,10 @@ import com.mongodb.MongoNamespace; import com.mongodb.ServerAddress; -import com.mongodb.connection.ConnectionDescription; -import com.mongodb.internal.connection.Connection; -import com.mongodb.internal.connection.DefaultServerConnection; -import com.mongodb.internal.operation.MixedBulkWriteOperation; +import com.mongodb.binding.SingleServerBinding; +import com.mongodb.binding.WriteBinding; +import com.mongodb.connection.ServerDescription; +import com.mongodb.operation.MixedBulkWriteOperation; import org.junit.AfterClass; import org.junit.Assert; @@ -38,12 +38,12 @@ import java.util.Set; /** - * ExecuteCommand方法拦截器单元测试 + * GeneralExecuteInterceptor UT * * @author daizhenyu * @since 2024-01-23 **/ -public class ExecuteCommandInterceptorTest { +public class GeneralExecuteInterceptorTest { private static DatabaseWriteProhibitionConfig globalConfig = new DatabaseWriteProhibitionConfig(); private static MongoNamespace namespace = new MongoNamespace("database-test", @@ -57,7 +57,7 @@ public class ExecuteCommandInterceptorTest { private static Object[] argument; - private ExecuteCommandInterceptor interceptor = new ExecuteCommandInterceptor(); + private GeneralExecuteInterceptor interceptor = new GeneralExecuteInterceptor(); @BeforeClass public static void setUp() { @@ -65,12 +65,12 @@ public static void setUp() { operationMock = Mockito.mock(MixedBulkWriteOperation.class); methodMock = Mockito.mock(Method.class); Mockito.when(operationMock.getNamespace()).thenReturn(namespace); - Connection connection = Mockito.mock(DefaultServerConnection.class); - ConnectionDescription description = Mockito.mock(ConnectionDescription.class); + WriteBinding binding = Mockito.mock(SingleServerBinding.class); + ServerDescription description = Mockito.mock(ServerDescription.class); ServerAddress serverAddress = new ServerAddress("127.0.0.1", 8080); - Mockito.when(connection.getDescription()).thenReturn(description); - Mockito.when(description.getServerAddress()).thenReturn(serverAddress); - argument = new Object[]{"database-test", null, null, null, null, connection}; + Mockito.when(binding.getWriteConnectionSource()).thenReturn(new ConnectionSourceImpl(description)); + Mockito.when(description.getAddress()).thenReturn(serverAddress); + argument = new Object[]{binding, "database-test"}; } @AfterClass @@ -80,27 +80,30 @@ public static void tearDown() { @Test public void testDoBefore() throws Exception { - // 数据库禁写开关关闭 + // Database write prohibition switch is turned off globalConfig.setEnableMongoDbWriteProhibition(false); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - // 数据库禁写开关关闭,禁写数据库set包含被拦截的数据库 + // Database write prohibition switch is turned off, + // and the write-prohibited database set contains intercepted databases. Set databases = new HashSet<>(); databases.add("database-test"); globalConfig.setMongoDbDatabases(databases); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - //数据库禁写开关打开,禁写数据库集合包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set contains intercepted databases. globalConfig.setEnableMongoDbWriteProhibition(true); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertEquals("Database prohibit to write, database: database-test", context.getThrowableOut().getMessage()); - //数据库禁写开关打开,禁写数据库集合不包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set does not contain intercepted databases. globalConfig.setMongoDbDatabases(new HashSet<>()); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptorTest.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptorTest.java similarity index 71% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptorTest.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptorTest.java index 028c4b5946..105c1ed1fb 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptorTest.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv3/interceptors/MixedBulkWriteOperationInterceptorTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv3.interceptors; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionConfig; @@ -22,10 +22,10 @@ import com.mongodb.MongoNamespace; import com.mongodb.ServerAddress; -import com.mongodb.connection.ConnectionDescription; -import com.mongodb.internal.connection.Connection; -import com.mongodb.internal.connection.DefaultServerConnection; -import com.mongodb.internal.operation.MixedBulkWriteOperation; +import com.mongodb.binding.SingleServerBinding; +import com.mongodb.binding.WriteBinding; +import com.mongodb.connection.ServerDescription; +import com.mongodb.operation.MixedBulkWriteOperation; import org.junit.AfterClass; import org.junit.Assert; @@ -38,12 +38,12 @@ import java.util.Set; /** - * ExecuteCommand方法拦截器单元测试 + * MixedBulkWriteOperationInterceptor UT * * @author daizhenyu * @since 2024-01-23 **/ -public class ExecuteWriteCommandInterceptorTest { +public class MixedBulkWriteOperationInterceptorTest { private static DatabaseWriteProhibitionConfig globalConfig = new DatabaseWriteProhibitionConfig(); private static MongoNamespace namespace = new MongoNamespace("database-test", @@ -57,7 +57,7 @@ public class ExecuteWriteCommandInterceptorTest { private static Object[] argument; - private ExecuteWriteCommandInterceptor interceptor = new ExecuteWriteCommandInterceptor(); + private MixedBulkWriteOperationInterceptor interceptor = new MixedBulkWriteOperationInterceptor(); @BeforeClass public static void setUp() { @@ -65,12 +65,12 @@ public static void setUp() { operationMock = Mockito.mock(MixedBulkWriteOperation.class); methodMock = Mockito.mock(Method.class); Mockito.when(operationMock.getNamespace()).thenReturn(namespace); - Connection connection = Mockito.mock(DefaultServerConnection.class); - ConnectionDescription description = Mockito.mock(ConnectionDescription.class); + WriteBinding binding = Mockito.mock(SingleServerBinding.class); + ServerDescription description = Mockito.mock(ServerDescription.class); ServerAddress serverAddress = new ServerAddress("127.0.0.1", 8080); - Mockito.when(connection.getDescription()).thenReturn(description); - Mockito.when(description.getServerAddress()).thenReturn(serverAddress); - argument = new Object[]{"database-test", null, null, null, connection}; + Mockito.when(binding.getWriteConnectionSource()).thenReturn(new ConnectionSourceImpl(description)); + Mockito.when(description.getAddress()).thenReturn(serverAddress); + argument = new Object[]{binding}; } @AfterClass @@ -80,27 +80,30 @@ public static void tearDown() { @Test public void testDoBefore() throws Exception { - // 数据库禁写开关关闭 + // Database write prohibition switch is turned off globalConfig.setEnableMongoDbWriteProhibition(false); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - // 数据库禁写开关关闭,禁写数据库set包含被拦截的数据库 + // Database write prohibition switch is turned off, + // and the write-prohibited database set contains intercepted databases. Set databases = new HashSet<>(); databases.add("database-test"); globalConfig.setMongoDbDatabases(databases); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - //数据库禁写开关打开,禁写数据库集合包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set contains intercepted databases. globalConfig.setEnableMongoDbWriteProhibition(true); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertEquals("Database prohibit to write, database: database-test", context.getThrowableOut().getMessage()); - //数据库禁写开关打开,禁写数据库集合不包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set does not contain intercepted databases. globalConfig.setMongoDbDatabases(new HashSet<>()); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/pom.xml b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/pom.xml similarity index 97% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/pom.xml rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/pom.xml index ac3cebbc2c..5faa7d0b67 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/pom.xml +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/pom.xml @@ -10,7 +10,7 @@ 4.0.0 - mongodb-3.x-4.x-plugin + mongodb-4.x-plugin 8 diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/constant/MethodParamTypeName.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/constant/MethodParamTypeName.java similarity index 60% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/constant/MethodParamTypeName.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/constant/MethodParamTypeName.java index adc763a841..9612eee9db 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/constant/MethodParamTypeName.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/constant/MethodParamTypeName.java @@ -14,88 +14,67 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.constant; +package com.huaweicloud.sermant.mongodbv4.constant; /** - * 方法参数类型的全限定名常量类 + * Fully qualified name constant class of mongodb4.x method parameter type * * @author daizhenyu * @since 2024-01-27 **/ public class MethodParamTypeName { /** - * String类全限定名 + * String Class Fully Qualified Name */ public static final String STRING_CLASS_NAME = "java.lang.String"; /** - * WriteBinding类全限定名 + * WriteBinding Class Fully Qualified Name */ public static final String WRITE_BINDING_CLASS_NAME = "com.mongodb.internal.binding.WriteBinding"; /** - * ReadPreference类全限定名 + * ReadPreference Class Fully Qualified Name */ public static final String READ_PREFERENCE_CLASS_NAME = "com.mongodb.ReadPreference"; /** - * FieldNameValidator类全限定名 + * FieldNameValidator Class Fully Qualified Name */ public static final String FIELD_NAME_VALIDATOR_CLASS_NAME = "org.bson.FieldNameValidator"; /** - * Decoder类全限定名 + * Decoder Class Fully Qualified Name */ public static final String DECODER_CLASS_NAME = "org.bson.codecs.Decoder"; /** - * CommandCreator类全限定名 + * CommandCreator Interface Fully Qualified Name */ public static final String COMMAND_CREATOR_CLASS_NAME = - "com.mongodb.internal.operation.CommandOperationHelper.CommandCreator"; + "com.mongodb.internal.operation.CommandOperationHelper$CommandCreator"; /** - * CommandWriteTransformer类全限定名 + * CommandWriteTransformer Interface Fully Qualified Name */ public static final String COMMAND_WRITE_TRANSFORMER_CLASS_NAME = - "com.mongodb.internal.operation.CommandOperationHelper.CommandWriteTransformer"; + "com.mongodb.internal.operation.CommandOperationHelper$CommandWriteTransformer"; /** - * Function类全限定名 + * Function Class Fully Qualified Name */ public static final String FUNCTION_CLASS_NAME = "com.mongodb.Function"; /** - * BsonDocument类全限定名 + * BsonDocument Class Fully Qualified Name */ public static final String BSON_DOCUMENT_CLASS_NAME = "org.bson.BsonDocument"; /** - * ConnectionSource类全限定名 - */ - public static final String CONNECTION_SOURCE_CLASS_NAME = "com.mongodb.internal.binding.ConnectionSource"; - - /** - * Connection类全限定名 + * Connection Class Fully Qualified Name */ public static final String CONNECTION_CLASS_NAME = "com.mongodb.internal.connection.Connection"; - /** - * AsyncWriteBinding类全限定名 - */ - public static final String ASYNC_WRITE_BINDING_CLASS_NAME = "com.mongodb.internal.binding.AsyncWriteBinding"; - - /** - * CommandWriteTransformerAsync类全限定名 - */ - public static final String COMMAND_WRITE_TRANSFORMER_ASYNC_CLASS_NAME = - "com.mongodb.internal.operation.CommandOperationHelper.CommandWriteTransformerAsync"; - - /** - * SingleResultCallback类全限定名 - */ - public static final String SINGLE_RESULT_CALLBACK_CLASS_NAME = "com.mongodb.internal.async.SingleResultCallback"; - private MethodParamTypeName() { } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/CommandOperationHelperDeclarer.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/CommandOperationHelperDeclarer.java new file mode 100644 index 0000000000..6f2266697d --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/CommandOperationHelperDeclarer.java @@ -0,0 +1,40 @@ +/* + * 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.mongodbv4.declarers; + +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.mongodbv4.utils.MongoDbV4EnhancementHelper; + +/** + * CommandOperationHelper Declarer + * + * @author daizhenyu + * @since 2024-01-16 + **/ +public class CommandOperationHelperDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return MongoDbV4EnhancementHelper.getCommandOperationHelperClassMatcher(); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return MongoDbV4EnhancementHelper.getCommandOperationHelperInterceptDeclarers(); + } +} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/MixedBulkWriteOperationDeclarer.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/MixedBulkWriteOperationDeclarer.java new file mode 100644 index 0000000000..efce26b1fb --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/MixedBulkWriteOperationDeclarer.java @@ -0,0 +1,42 @@ +/* + * 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.mongodbv4.declarers; + +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.mongodbv4.utils.MongoDbV4EnhancementHelper; + +/** + * MixedBulkWriteOperation Declarer + * + * @author daizhenyu + * @since 2024-01-16 + **/ +public class MixedBulkWriteOperationDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return MongoDbV4EnhancementHelper.getMixedBulkWriteOperationClassMatcher(); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + MongoDbV4EnhancementHelper.getExecuteInterceptDeclarer(), + }; + } +} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/SyncOperationHelperDeclarer.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/SyncOperationHelperDeclarer.java new file mode 100644 index 0000000000..a0da2ddabe --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/declarers/SyncOperationHelperDeclarer.java @@ -0,0 +1,40 @@ +/* + * 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.mongodbv4.declarers; + +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.mongodbv4.utils.MongoDbV4EnhancementHelper; + +/** + * SyncOperationHelper Declarer + * + * @author daizhenyu + * @since 2024-02-22 + **/ +public class SyncOperationHelperDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return MongoDbV4EnhancementHelper.getSyncOperationHelperDeclarerClassMatcher(); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return MongoDbV4EnhancementHelper.getSyncOperationHelperInterceptDeclarers(); + } +} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptor.java similarity index 55% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptor.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptor.java index 850501b363..ed03583392 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptor.java @@ -14,38 +14,40 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv4.interceptors; 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.handler.DatabaseHandler; +import com.huaweicloud.sermant.database.interceptor.AbstractMongoDbInterceptor; import com.mongodb.ServerAddress; -import com.mongodb.connection.ConnectionDescription; -import com.mongodb.internal.connection.Connection; +import com.mongodb.connection.ServerDescription; +import com.mongodb.internal.binding.ConnectionSource; +import com.mongodb.internal.binding.WriteBinding; + +import java.util.Optional; /** - * 执行executeWriteCommand方法的拦截器 + * executeCommand、executeRetryableCommand、executeRetryableWrite Method Interceptor * * @author daizhenyu - * @since 2024-01-18 + * @since 2024-02-23 **/ -public class ExecuteWriteCommandInterceptor extends AbstractMongoDbInterceptor { - private static final int PARAM_INDEX = 4; - +public class GeneralExecuteInterceptor extends AbstractMongoDbInterceptor { /** - * 无参构造方法 + * No-argument constructor */ - public ExecuteWriteCommandInterceptor() { + public GeneralExecuteInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ - public ExecuteWriteCommandInterceptor(DatabaseHandler handler) { + public GeneralExecuteInterceptor(DatabaseHandler handler) { this.handler = handler; } @@ -53,11 +55,13 @@ public ExecuteWriteCommandInterceptor(DatabaseHandler handler) { protected void createAndCacheDatabaseInfo(ExecuteContext context) { DatabaseInfo info = new DatabaseInfo(DatabaseType.MONGODB); context.setLocalFieldValue(DATABASE_INFO, info); - info.setDatabaseName((String) context.getArguments()[0]); - Connection connection = (Connection) context.getArguments()[PARAM_INDEX]; - ConnectionDescription description = connection.getDescription(); - if (description != null) { - ServerAddress serverAddress = description.getServerAddress(); + info.setDatabaseName((String) context.getArguments()[1]); + ServerAddress serverAddress = Optional.ofNullable((WriteBinding) context.getArguments()[0]) + .map(WriteBinding::getWriteConnectionSource) + .map(ConnectionSource::getServerDescription) + .map(ServerDescription::getAddress) + .orElse(null); + if (serverAddress != null) { info.setHostAddress(serverAddress.getHost()); info.setPort(serverAddress.getPort()); } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/MixedBulkWriteOperationInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/interceptors/MixedBulkWriteOperationInterceptor.java similarity index 66% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/MixedBulkWriteOperationInterceptor.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/interceptors/MixedBulkWriteOperationInterceptor.java index 0150549eaa..c9abed562d 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodb/interceptors/MixedBulkWriteOperationInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/interceptors/MixedBulkWriteOperationInterceptor.java @@ -14,34 +14,39 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv4.interceptors; 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.handler.DatabaseHandler; +import com.huaweicloud.sermant.database.interceptor.AbstractMongoDbInterceptor; import com.mongodb.ServerAddress; +import com.mongodb.connection.ServerDescription; +import com.mongodb.internal.binding.ConnectionSource; import com.mongodb.internal.binding.WriteBinding; import com.mongodb.internal.operation.MixedBulkWriteOperation; +import java.util.Optional; + /** - * MixedBulkWriteOperation类拦截声明器 + * MixedBulkWriteOperation Class execute Method Interceptor * * @author daizhenyu * @since 2024-01-16 **/ public class MixedBulkWriteOperationInterceptor extends AbstractMongoDbInterceptor { /** - * 无参构造方法 + * No-argument constructor */ public MixedBulkWriteOperationInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ public MixedBulkWriteOperationInterceptor(DatabaseHandler handler) { this.handler = handler; @@ -51,12 +56,15 @@ public MixedBulkWriteOperationInterceptor(DatabaseHandler handler) { protected void createAndCacheDatabaseInfo(ExecuteContext context) { DatabaseInfo info = new DatabaseInfo(DatabaseType.MONGODB); context.setLocalFieldValue(DATABASE_INFO, info); - ServerAddress serverAddress = ((WriteBinding) context.getArguments()[0]) - .getWriteConnectionSource() - .getServerDescription() - .getAddress(); info.setDatabaseName(((MixedBulkWriteOperation) context.getObject()).getNamespace().getDatabaseName()); - info.setHostAddress(serverAddress.getHost()); - info.setPort(serverAddress.getPort()); + ServerAddress serverAddress = Optional.ofNullable((WriteBinding) context.getArguments()[0]) + .map(WriteBinding::getWriteConnectionSource) + .map(ConnectionSource::getServerDescription) + .map(ServerDescription::getAddress) + .orElse(null); + if (serverAddress != null) { + info.setHostAddress(serverAddress.getHost()); + info.setPort(serverAddress.getPort()); + } } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/utils/MongoDbV4EnhancementHelper.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/utils/MongoDbV4EnhancementHelper.java new file mode 100644 index 0000000000..fbe15dbf85 --- /dev/null +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/java/com/huaweicloud/sermant/mongodbv4/utils/MongoDbV4EnhancementHelper.java @@ -0,0 +1,333 @@ +/* + * 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.mongodbv4.utils; + +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +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.mongodbv4.constant.MethodParamTypeName; +import com.huaweicloud.sermant.mongodbv4.interceptors.GeneralExecuteInterceptor; +import com.huaweicloud.sermant.mongodbv4.interceptors.MixedBulkWriteOperationInterceptor; + +/** + * mongodb declarer helper + * + * @author daizhenyu + * @since 2024-01-16 + **/ +public class MongoDbV4EnhancementHelper { + private static final String MIXED_BULK_WRITE_CLASS = "com.mongodb.internal.operation.MixedBulkWriteOperation"; + + private static final String COMMAND_OPERATION_CLASS = "com.mongodb.internal.operation.CommandOperationHelper"; + + private static final String SYNC_OPERATION_CLASS = "com.mongodb.internal.operation.SyncOperationHelper"; + + private static final String EXECUTE_METHOD_NAME = "execute"; + + private static final String EXECUTE_COMMAND_METHOD_NAME = "executeCommand"; + + private static final String EXECUTE_RETRYABLE_COMMAND_METHOD_NAME = "executeRetryableCommand"; + + private static final String EXECUTE_RETRYABLE_WRITE = "executeRetryableWrite"; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_FIRST = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_SECOND = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_THIRD = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_FOURTH = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME + }; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_FIFTH = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] EXECUTE_COMMAND_PARAMS_TYPE_SIXTH = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.BSON_DOCUMENT_CLASS_NAME, + MethodParamTypeName.CONNECTION_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME + }; + + private static final String[] EXECUTE_RETRY_COMMAND_PARAMS_TYPE = { + MethodParamTypeName.WRITE_BINDING_CLASS_NAME, + MethodParamTypeName.STRING_CLASS_NAME, + MethodParamTypeName.READ_PREFERENCE_CLASS_NAME, + MethodParamTypeName.FIELD_NAME_VALIDATOR_CLASS_NAME, + MethodParamTypeName.DECODER_CLASS_NAME, + MethodParamTypeName.COMMAND_CREATOR_CLASS_NAME, + MethodParamTypeName.COMMAND_WRITE_TRANSFORMER_CLASS_NAME, + MethodParamTypeName.FUNCTION_CLASS_NAME + }; + + private MongoDbV4EnhancementHelper() { + } + + /** + * Get ClassMatcher of CommandOperationHelper Class + * + * @return ClassMatcher ClassMatcher + */ + public static ClassMatcher getCommandOperationHelperClassMatcher() { + return ClassMatcher.nameEquals(COMMAND_OPERATION_CLASS); + } + + /** + * Get ClassMatcher of MixedBulkWriteOperation Class + * + * @return ClassMatcher ClassMatcher + */ + public static ClassMatcher getMixedBulkWriteOperationClassMatcher() { + return ClassMatcher.nameEquals(MIXED_BULK_WRITE_CLASS); + } + + /** + * Get ClassMatcher of SyncOperationHelperDeclarer Class + * + * @return ClassMatcher ClassMatcher + */ + public static ClassMatcher getSyncOperationHelperDeclarerClassMatcher() { + return ClassMatcher.nameEquals(SYNC_OPERATION_CLASS); + } + + /** + * Get No-argument Interceptor of MixedBulkWriteOperation execute Method + * + * @return InterceptDeclarer No-argument Interceptor of MixedBulkWriteOperation execute Method + */ + public static InterceptDeclarer getExecuteInterceptDeclarer() { + return InterceptDeclarer.build(getExecuteMethodMatcher(), + new MixedBulkWriteOperationInterceptor()); + } + + /** + * Get Parametric Interceptor of MixedBulkWriteOperation execute Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of MixedBulkWriteOperation execute Method + */ + public static InterceptDeclarer getExecuteInterceptDeclarer(DatabaseHandler handler) { + return InterceptDeclarer.build(getExecuteMethodMatcher(), + new MixedBulkWriteOperationInterceptor(handler)); + } + + /** + * Get No-argument Interceptor Array of SyncOperationHelper + * + * @return InterceptDeclarer[] No-argument Interceptor Array of SyncOperationHelper + */ + public static InterceptDeclarer[] getSyncOperationHelperInterceptDeclarers() { + GeneralExecuteInterceptor generalExecuteInterceptor = new GeneralExecuteInterceptor(); + return new InterceptDeclarer[]{ + InterceptDeclarer.build(getExecuteCommandMethodMatcher(), generalExecuteInterceptor), + InterceptDeclarer.build(getExecuteRetryableWriteMethodMatcher(), generalExecuteInterceptor) + }; + } + + /** + * Get No-argument Interceptor of SyncOperationHelper executeCommand Method + * + * @return InterceptDeclarer No-argument Interceptor of SyncOperationHelper executeCommand Method + */ + public static InterceptDeclarer getExecuteCommandInterceptDeclarer4Sync() { + return InterceptDeclarer.build(getExecuteCommandMethodMatcher(), + new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of SyncOperationHelper executeCommand Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of SyncOperationHelper executeCommand Method + */ + public static InterceptDeclarer getExecuteCommandInterceptDeclarer4Sync(DatabaseHandler handler) { + return InterceptDeclarer.build(getExecuteCommandMethodMatcher(), + new GeneralExecuteInterceptor(handler)); + } + + /** + * Get No-argument Interceptor of SyncOperationHelper executeRetryableWrite Method + * + * @return InterceptDeclarer No-argument Interceptor of SyncOperationHelper executeRetryableWrite Method + */ + public static InterceptDeclarer getExecuteRetryableWriteInterceptDeclarer4Sync() { + return InterceptDeclarer.build(getExecuteRetryableWriteMethodMatcher(), + new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of SyncOperationHelper executeRetryableWrite Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of SyncOperationHelper executeRetryableWrite Method + */ + public static InterceptDeclarer getExecuteRetryableWriteInterceptDeclarer4Sync(DatabaseHandler handler) { + return InterceptDeclarer.build(getExecuteRetryableWriteMethodMatcher(), + new GeneralExecuteInterceptor(handler)); + } + + /** + * Get No-argument Interceptor Array of CommandOperationHelper + * + * @return InterceptDeclarer[] No-argument Interceptor Array of CommandOperationHelper + */ + public static InterceptDeclarer[] getCommandOperationHelperInterceptDeclarers() { + GeneralExecuteInterceptor generalExecuteInterceptor = new GeneralExecuteInterceptor(); + return new InterceptDeclarer[]{ + InterceptDeclarer.build(getExecuteCommandMethodMatcher4CommandOperation(), + generalExecuteInterceptor), + InterceptDeclarer.build(getExecuteRetryableCommandMethodMatcher4CommandOperation(), + generalExecuteInterceptor), + InterceptDeclarer.build(getExecuteRetryableWriteMethodMatcher(), + generalExecuteInterceptor) + }; + } + + /** + * Get No-argument Interceptor of CommandOperationHelper executeCommand Method + * + * @return InterceptDeclarer No-argument Interceptor of CommandOperationHelper executeCommand Method + */ + public static InterceptDeclarer getExecuteCommandInterceptDeclarer() { + return InterceptDeclarer + .build(getExecuteCommandMethodMatcher4CommandOperation(), new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of CommandOperationHelper executeCommand Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of CommandOperationHelper executeCommand Method + */ + public static InterceptDeclarer getExecuteCommandInterceptDeclarer(DatabaseHandler handler) { + return InterceptDeclarer + .build(getExecuteCommandMethodMatcher4CommandOperation(), new GeneralExecuteInterceptor(handler)); + } + + /** + * Get No-argument Interceptor of CommandOperationHelper executeRetryableCommand Method + * + * @return InterceptDeclarer No-argument Interceptor of CommandOperationHelper executeRetryableCommand Method + */ + public static InterceptDeclarer getExecuteRetryableCommandInterceptDeclarer() { + return InterceptDeclarer + .build(getExecuteRetryableCommandMethodMatcher4CommandOperation(), + new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of CommandOperationHelper executeRetryableCommand Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of CommandOperationHelper executeRetryableCommand Method + */ + public static InterceptDeclarer getExecuteRetryableCommandInterceptDeclarer( + DatabaseHandler handler) { + return InterceptDeclarer + .build(getExecuteRetryableCommandMethodMatcher4CommandOperation(), + new GeneralExecuteInterceptor(handler)); + } + + /** + * Get No-argument Interceptor of CommandOperationHelper executeRetryableWrite Method + * + * @return InterceptDeclarer No-argument Interceptor of CommandOperationHelper executeRetryableWrite Method + */ + public static InterceptDeclarer getExecuteRetryableWriteInterceptDeclarer() { + return InterceptDeclarer + .build(getExecuteRetryableWriteMethodMatcher(), + new GeneralExecuteInterceptor()); + } + + /** + * Get Parametric Interceptor of CommandOperationHelper executeRetryableWrite Method + * + * @param handler write operation handler + * @return InterceptDeclarer Parametric Interceptor of CommandOperationHelper executeRetryableWrite Method + */ + public static InterceptDeclarer getExecuteRetryableWriteInterceptDeclarer( + DatabaseHandler handler) { + return InterceptDeclarer + .build(getExecuteRetryableWriteMethodMatcher(), + new GeneralExecuteInterceptor(handler)); + } + + private static MethodMatcher getExecuteMethodMatcher() { + return MethodMatcher.nameEquals(EXECUTE_METHOD_NAME); + } + + private static MethodMatcher getExecuteCommandMethodMatcher4CommandOperation() { + return (MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_FIRST))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_SECOND))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_THIRD))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_FOURTH))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_FIFTH))) + .or(MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_COMMAND_PARAMS_TYPE_SIXTH))); + } + + private static MethodMatcher getExecuteRetryableCommandMethodMatcher4CommandOperation() { + return MethodMatcher.nameEquals(EXECUTE_RETRYABLE_COMMAND_METHOD_NAME) + .and(MethodMatcher.paramTypesEqual(EXECUTE_RETRY_COMMAND_PARAMS_TYPE)); + } + + private static MethodMatcher getExecuteCommandMethodMatcher() { + return MethodMatcher.nameEquals(EXECUTE_COMMAND_METHOD_NAME); + } + + private static MethodMatcher getExecuteRetryableWriteMethodMatcher() { + return MethodMatcher.nameEquals(EXECUTE_RETRYABLE_WRITE); + } +} diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer similarity index 73% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 4dd727257e..62623484a8 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -13,5 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -com.huaweicloud.sermant.mongodb.declarers.MixedBulkWriteOperationDeclarer -com.huaweicloud.sermant.mongodb.declarers.CommandOperationHelperDeclarer \ No newline at end of file +com.huaweicloud.sermant.mongodbv4.declarers.MixedBulkWriteOperationDeclarer +com.huaweicloud.sermant.mongodbv4.declarers.CommandOperationHelperDeclarer +com.huaweicloud.sermant.mongodbv4.declarers.SyncOperationHelperDeclarer \ No newline at end of file diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ConnectionSourceImpl.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/ConnectionSourceImpl.java similarity index 90% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ConnectionSourceImpl.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/ConnectionSourceImpl.java index e5f26ae6c6..47d6dbc907 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ConnectionSourceImpl.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/ConnectionSourceImpl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv4.interceptors; import com.mongodb.ServerApi; import com.mongodb.connection.ServerDescription; @@ -23,7 +23,7 @@ import com.mongodb.internal.session.SessionContext; /** - * ConnectionSource接口的实现类 + * Implementation class of ConnectionSource interface * * @author daizhenyu * @since 2024-02-05 @@ -32,9 +32,9 @@ public class ConnectionSourceImpl implements ConnectionSource { private ServerDescription description; /** - * 构造方法 + * Construction method * - * @param description 服务端信息 + * @param description Server information */ public ConnectionSourceImpl(ServerDescription description) { this.description = description; diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptorTest.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptorTest.java similarity index 83% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptorTest.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptorTest.java index 305e8f16e4..81be19637d 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteRetryableCommandInterceptorTest.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/GeneralExecuteInterceptorTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv4.interceptors; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionConfig; @@ -22,14 +22,10 @@ import com.mongodb.MongoNamespace; import com.mongodb.ServerAddress; -import com.mongodb.ServerApi; import com.mongodb.connection.ServerDescription; -import com.mongodb.internal.binding.ConnectionSource; import com.mongodb.internal.binding.SingleServerBinding; import com.mongodb.internal.binding.WriteBinding; -import com.mongodb.internal.connection.Connection; import com.mongodb.internal.operation.MixedBulkWriteOperation; -import com.mongodb.internal.session.SessionContext; import org.junit.AfterClass; import org.junit.Assert; @@ -42,12 +38,12 @@ import java.util.Set; /** - * ExecuteCommand方法拦截器单元测试 + * GeneralExecuteInterceptor UT * * @author daizhenyu * @since 2024-01-23 **/ -public class ExecuteRetryableCommandInterceptorTest { +public class GeneralExecuteInterceptorTest { private static DatabaseWriteProhibitionConfig globalConfig = new DatabaseWriteProhibitionConfig(); private static MongoNamespace namespace = new MongoNamespace("database-test", @@ -61,7 +57,7 @@ public class ExecuteRetryableCommandInterceptorTest { private static Object[] argument; - private ExecuteRetryableCommandInterceptor interceptor = new ExecuteRetryableCommandInterceptor(); + private GeneralExecuteInterceptor interceptor = new GeneralExecuteInterceptor(); @BeforeClass public static void setUp() { @@ -84,27 +80,30 @@ public static void tearDown() { @Test public void testDoBefore() throws Exception { - // 数据库禁写开关关闭 + // Database write prohibition switch is turned off globalConfig.setEnableMongoDbWriteProhibition(false); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - // 数据库禁写开关关闭,禁写数据库set包含被拦截的数据库 + // Database write prohibition switch is turned off, + // and the write-prohibited database set contains intercepted databases. Set databases = new HashSet<>(); databases.add("database-test"); globalConfig.setMongoDbDatabases(databases); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - //数据库禁写开关打开,禁写数据库集合包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set contains intercepted databases. globalConfig.setEnableMongoDbWriteProhibition(true); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertEquals("Database prohibit to write, database: database-test", context.getThrowableOut().getMessage()); - //数据库禁写开关打开,禁写数据库集合不包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set does not contain intercepted databases. globalConfig.setMongoDbDatabases(new HashSet<>()); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); diff --git a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/MixedBulkWriteOperationInterceptorTest.java b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/MixedBulkWriteOperationInterceptorTest.java similarity index 86% rename from sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/MixedBulkWriteOperationInterceptorTest.java rename to sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/MixedBulkWriteOperationInterceptorTest.java index ba16b40cd4..2aab4af972 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mongodb-3.x-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodb/interceptors/MixedBulkWriteOperationInterceptorTest.java +++ b/sermant-plugins/sermant-database-write-prohibition/mongodb-4.x-plugin/src/test/java/com/huaweicloud/sermant/mongodbv4/interceptors/MixedBulkWriteOperationInterceptorTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.mongodb.interceptors; +package com.huaweicloud.sermant.mongodbv4.interceptors; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionConfig; @@ -22,14 +22,10 @@ import com.mongodb.MongoNamespace; import com.mongodb.ServerAddress; -import com.mongodb.ServerApi; import com.mongodb.connection.ServerDescription; -import com.mongodb.internal.binding.ConnectionSource; import com.mongodb.internal.binding.SingleServerBinding; import com.mongodb.internal.binding.WriteBinding; -import com.mongodb.internal.connection.Connection; import com.mongodb.internal.operation.MixedBulkWriteOperation; -import com.mongodb.internal.session.SessionContext; import org.junit.AfterClass; import org.junit.Assert; @@ -42,7 +38,7 @@ import java.util.Set; /** - * MixedBulkWriteOperation类写操作拦截器单元测试 + * MixedBulkWriteOperationInterceptor UT * * @author daizhenyu * @since 2024-01-23 @@ -84,27 +80,30 @@ public static void tearDown() { @Test public void testDoBefore() throws Exception { - // 数据库禁写开关关闭 + // Database write prohibition switch is turned off globalConfig.setEnableMongoDbWriteProhibition(false); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - // 数据库禁写开关关闭,禁写数据库set包含被拦截的数据库 + // Database write prohibition switch is turned off, + // and the write-prohibited database set contains intercepted databases. Set databases = new HashSet<>(); databases.add("database-test"); globalConfig.setMongoDbDatabases(databases); interceptor.before(context); Assert.assertNull(context.getThrowableOut()); - //数据库禁写开关打开,禁写数据库集合包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set contains intercepted databases. globalConfig.setEnableMongoDbWriteProhibition(true); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); Assert.assertEquals("Database prohibit to write, database: database-test", context.getThrowableOut().getMessage()); - //数据库禁写开关打开,禁写数据库集合不包含被拦截的数据库 + // Database write prohibition switch is turned on, + // and the write-prohibited database set does not contain intercepted databases. globalConfig.setMongoDbDatabases(new HashSet<>()); context = ExecuteContext.forMemberMethod(operationMock, methodMock, argument, null, null); interceptor.before(context); diff --git a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteInterceptor.java index 653c4261f1..5b8c949050 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteInterceptor.java @@ -18,14 +18,12 @@ 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.internal.util.dao.ClientPrepareResult; /** - * executeQuery、executeBatchClient方法拦截器 + * executeQuery、executeBatchClient Method Interceptor * * @author daizhenyu * @since 2024-01-26 @@ -34,15 +32,15 @@ public class ExecuteInterceptor extends AbstractMariadbV2Interceptor { private static final int PARAM_INDEX = 2; /** - * 无参构造方法 + * No-argument constructor */ public ExecuteInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ public ExecuteInterceptor(DatabaseHandler handler) { this.handler = handler; @@ -52,16 +50,14 @@ public ExecuteInterceptor(DatabaseHandler handler) { protected ExecuteContext doBefore(ExecuteContext context) { String database = getDataBaseInfo(context).getDatabaseName(); Object argument = context.getArguments()[PARAM_INDEX]; - String sql = null; + String sql; if (argument instanceof ClientPrepareResult) { sql = ((ClientPrepareResult) argument).getSql(); } else { sql = (String) argument; } - if (SqlParserUtils.isWriteOperation(sql) - && DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases().contains(database)) { - DatabaseController.disableDatabaseWriteOperation(database, context); - } + handleWriteOperationIfWriteDisabled(sql, database, + DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases(), context); return context; } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteServerInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteServerInterceptor.java index 11b3f167b3..58806b24b3 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteServerInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/ExecuteServerInterceptor.java @@ -18,29 +18,27 @@ 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.internal.util.dao.ServerPrepareResult; /** - * executePreparedQuery、executeBatchServer方法拦截器 + * executePreparedQuery、executeBatchServer Method Interceptor * * @author daizhenyu * @since 2024-01-26 **/ public class ExecuteServerInterceptor extends AbstractMariadbV2Interceptor { /** - * 无参构造方法 + * No-argument constructor */ public ExecuteServerInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ public ExecuteServerInterceptor(DatabaseHandler handler) { this.handler = handler; @@ -50,10 +48,8 @@ public ExecuteServerInterceptor(DatabaseHandler handler) { protected ExecuteContext doBefore(ExecuteContext context) { String database = getDataBaseInfo(context).getDatabaseName(); String sql = ((ServerPrepareResult) context.getArguments()[1]).getSql(); - if (SqlParserUtils.isWriteOperation(sql) - && DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases().contains(database)) { - DatabaseController.disableDatabaseWriteOperation(database, context); - } + handleWriteOperationIfWriteDisabled(sql, database, + DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases(), context); return context; } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/PrepareInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/PrepareInterceptor.java index c852c411d0..5800706707 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/PrepareInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-2.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv2/interceptors/PrepareInterceptor.java @@ -18,27 +18,25 @@ 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; /** - * prepare方法拦截器 + * prepare Method Interceptor * * @author daizhenyu * @since 2024-01-27 **/ public class PrepareInterceptor extends AbstractMariadbV2Interceptor { /** - * 无参构造方法 + * No-argument constructor */ public PrepareInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ public PrepareInterceptor(DatabaseHandler handler) { this.handler = handler; @@ -48,10 +46,8 @@ public PrepareInterceptor(DatabaseHandler handler) { protected ExecuteContext doBefore(ExecuteContext context) { String database = getDataBaseInfo(context).getDatabaseName(); String sql = (String) context.getArguments()[0]; - if (SqlParserUtils.isWriteOperation(sql) - && DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases().contains(database)) { - DatabaseController.disableDatabaseWriteOperation(database, context); - } + handleWriteOperationIfWriteDisabled(sql, database, + DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases(), context); return context; } } diff --git a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv3/interceptors/SendQueryInterceptor.java b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv3/interceptors/SendQueryInterceptor.java index c6ea8c0557..cb5bddc3e2 100644 --- a/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv3/interceptors/SendQueryInterceptor.java +++ b/sermant-plugins/sermant-database-write-prohibition/mysql-mariadb-3.x-plugin/src/main/java/com/huaweicloud/sermant/mariadbv3/interceptors/SendQueryInterceptor.java @@ -18,29 +18,27 @@ 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; /** - * sendQuery方法拦截器 + * sendQuery Method Interceptor * * @author daizhenyu * @since 2024-01-30 **/ public class SendQueryInterceptor extends AbstractMariadbV3Interceptor { /** - * 无参构造方法 + * No-argument constructor */ public SendQueryInterceptor() { } /** - * 有参构造方法 + * Parametric constructor * - * @param handler 写操作处理器 + * @param handler write operation handler */ public SendQueryInterceptor(DatabaseHandler handler) { this.handler = handler; @@ -50,10 +48,8 @@ public SendQueryInterceptor(DatabaseHandler handler) { protected ExecuteContext doBefore(ExecuteContext context) { ClientMessage clientMessage = (ClientMessage) context.getArguments()[0]; String database = getDataBaseInfo(context).getDatabaseName(); - if (SqlParserUtils.isWriteOperation(clientMessage.description()) - && DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases().contains(database)) { - DatabaseController.disableDatabaseWriteOperation(database, context); - } + handleWriteOperationIfWriteDisabled(clientMessage.description(), database, + DatabaseWriteProhibitionManager.getMySqlProhibitionDatabases(), context); return context; } } diff --git a/sermant-plugins/sermant-database-write-prohibition/pom.xml b/sermant-plugins/sermant-database-write-prohibition/pom.xml index b235ba4363..24499e5881 100644 --- a/sermant-plugins/sermant-database-write-prohibition/pom.xml +++ b/sermant-plugins/sermant-database-write-prohibition/pom.xml @@ -28,7 +28,8 @@ database-controller database-config-service - mongodb-3.x-4.x-plugin + mongodb-4.x-plugin + mongodb-3.x-plugin mysql-mariadb-2.x-plugin mysql-mariadb-3.x-plugin opengauss-3.0.x-plugin @@ -42,7 +43,8 @@ database-controller database-config-service - mongodb-3.x-4.x-plugin + mongodb-4.x-plugin + mongodb-3.x-plugin mysql-mariadb-2.x-plugin mysql-mariadb-3.x-plugin opengauss-3.0.x-plugin @@ -56,7 +58,8 @@ database-controller database-config-service - mongodb-3.x-4.x-plugin + mongodb-4.x-plugin + mongodb-3.x-plugin mysql-mariadb-2.x-plugin mysql-mariadb-3.x-plugin opengauss-3.0.x-plugin