forked from sermant-io/Sermant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: daizhenyu <[email protected]>
- Loading branch information
Showing
31 changed files
with
1,501 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...tabase-controller/src/main/java/com/huaweicloud/sermant/database/entity/DatabaseInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.entity; | ||
|
||
/** | ||
* 数据库示例信息 | ||
* | ||
* @author daizhenyu | ||
* @since 2024-02-01 | ||
**/ | ||
public class DatabaseInfo { | ||
private String databaseName; | ||
|
||
private String hostAddress; | ||
|
||
private int port; | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
|
||
public String getDatabaseName() { | ||
return databaseName; | ||
} | ||
|
||
public void setDatabaseName(String databaseName) { | ||
this.databaseName = databaseName; | ||
} | ||
|
||
public String getHostAddress() { | ||
return hostAddress; | ||
} | ||
|
||
public void setHostAddress(String hostAddress) { | ||
this.hostAddress = hostAddress; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...abase-controller/src/main/java/com/huaweicloud/sermant/database/utils/SqlParserUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.utils; | ||
|
||
import com.huaweicloud.sermant.core.utils.StringUtils; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* sql解析工具类 | ||
* | ||
* @author daizhenyu | ||
* @since 2024-01-25 | ||
**/ | ||
public class SqlParserUtils { | ||
private static Pattern writePattern = | ||
Pattern.compile("INSERT\\b|UPDATE\\b|DELETE\\b|CREATE\\b|ALTER\\b|DROP\\b|TRUNCATE\\b", | ||
Pattern.CASE_INSENSITIVE); | ||
|
||
private SqlParserUtils() { | ||
} | ||
|
||
/** | ||
* 解析sql并判断是否为写操作 | ||
* | ||
* @param sql sql语句 | ||
* @return 是否为写操作 | ||
*/ | ||
public static boolean isWriteOperation(String sql) { | ||
if (StringUtils.isEmpty(sql)) { | ||
return false; | ||
} | ||
return writePattern.matcher(sql).find(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rc/main/java/com/huaweicloud/sermant/mongodb/interceptors/AbstractMongoDbInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.mongodb.interceptors; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.database.config.DatabaseWriteProhibitionManager; | ||
import com.huaweicloud.sermant.database.controller.DatabaseController; | ||
import com.huaweicloud.sermant.database.entity.DatabaseInfo; | ||
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor; | ||
|
||
/** | ||
* mongodb抽象interceptor | ||
* | ||
* @author daizhenyu | ||
* @since 2024-02-02 | ||
**/ | ||
public abstract class AbstractMongoDbInterceptor extends AbstractDatabaseInterceptor { | ||
@Override | ||
public ExecuteContext doBefore(ExecuteContext context) { | ||
String database = ((DatabaseInfo) context.getLocalFieldValue(DATABASE_INFO)).getDatabaseName(); | ||
if (DatabaseWriteProhibitionManager.getMongoDbProhibitionDatabases().contains(database)) { | ||
DatabaseController.disableDatabaseWriteOperation(database, context); | ||
} | ||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ain/java/com/huaweicloud/sermant/mongodb/interceptors/ExecuteWriteCommandInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.huaweicloud.sermant.mongodb.interceptors; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.database.entity.DatabaseInfo; | ||
import com.huaweicloud.sermant.database.handler.DatabaseHandler; | ||
|
||
import com.mongodb.ServerAddress; | ||
import com.mongodb.connection.ConnectionDescription; | ||
import com.mongodb.internal.connection.Connection; | ||
|
||
/** | ||
* 执行executeWriteCommand方法的拦截器 | ||
* | ||
* @author daizhenyu | ||
* @since 2024-01-18 | ||
**/ | ||
public class ExecuteWriteCommandInterceptor extends AbstractMongoDbInterceptor { | ||
private static final int PARAM_INDEX = 4; | ||
|
||
/** | ||
* 无参构造方法 | ||
*/ | ||
public ExecuteWriteCommandInterceptor() { | ||
} | ||
|
||
/** | ||
* 有参构造方法 | ||
* | ||
* @param handler 写操作处理器 | ||
*/ | ||
public ExecuteWriteCommandInterceptor(DatabaseHandler handler) { | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
protected void createAndCacheDatabaseInfo(ExecuteContext context) { | ||
DatabaseInfo info = new DatabaseInfo(); | ||
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.setHostAddress(serverAddress.getHost()); | ||
info.setPort(serverAddress.getPort()); | ||
} | ||
} | ||
} |
Oops, something went wrong.