Pengzna commented on code in PR #16428: URL: https://github.com/apache/iotdb/pull/16428#discussion_r2355000535
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/audit/DNAuditLogger.java: ########## @@ -0,0 +1,304 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.iotdb.db.audit; + +import org.apache.iotdb.commons.audit.AbstractAuditLogger; +import org.apache.iotdb.commons.audit.AuditEventType; +import org.apache.iotdb.commons.audit.AuditLogFields; +import org.apache.iotdb.commons.audit.AuditLogOperation; +import org.apache.iotdb.commons.audit.PrivilegeLevel; +import org.apache.iotdb.commons.auth.entity.PrivilegeType; +import org.apache.iotdb.commons.exception.IllegalPathException; +import org.apache.iotdb.commons.pipe.config.constant.SystemConstant; +import org.apache.iotdb.commons.utils.CommonDateTimeUtils; +import org.apache.iotdb.db.auth.AuthorityChecker; +import org.apache.iotdb.db.conf.IoTDBConfig; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.db.protocol.session.IClientSession; +import org.apache.iotdb.db.protocol.session.SessionManager; +import org.apache.iotdb.db.queryengine.common.SessionInfo; +import org.apache.iotdb.db.queryengine.plan.Coordinator; +import org.apache.iotdb.db.queryengine.plan.analyze.ClusterPartitionFetcher; +import org.apache.iotdb.db.queryengine.plan.analyze.cache.schema.DataNodeDevicePathCache; +import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; +import org.apache.iotdb.db.queryengine.plan.parser.StatementGenerator; +import org.apache.iotdb.db.queryengine.plan.planner.LocalExecutionPlanner; +import org.apache.iotdb.db.queryengine.plan.relational.metadata.Metadata; +import org.apache.iotdb.db.queryengine.plan.relational.sql.parser.SqlParser; +import org.apache.iotdb.db.queryengine.plan.statement.Statement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowStatement; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.apache.tsfile.common.conf.TSFileConfig; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.utils.Binary; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.validation.constraints.NotNull; + +import java.time.ZoneId; +import java.util.List; + +import static org.apache.iotdb.db.pipe.receiver.protocol.legacy.loader.ILoader.SCHEMA_FETCHER; + +public class DNAuditLogger extends AbstractAuditLogger { + private static final Logger logger = LoggerFactory.getLogger(DNAuditLogger.class); + + private static final String LOG = "log"; + private static final String USERNAME = "username"; + private static final String CLI_HOSTNAME = "cli_hostname"; + private static final String RESULT = "result"; + private static final String AUDIT_EVENT_TYPE = "audit_event_type"; + private static final String OPERATION_TYPE = "operation_type"; + private static final String PRIVILEGE_TYPE = "privilege_type"; + private static final String PRIVILEGE_LEVEL = "privilege_level"; + private static final String DATABASE = "database"; + private static final String SQL_STRING = "sql_string"; + + private static final String AUDIT_LOG_DEVICE = "root.__audit.log.%s.%s"; + private static final String AUDIT_LOGIN_LOG_DEVICE = "root.__audit.login.%s.%s"; + private static final Coordinator COORDINATOR = Coordinator.getInstance(); + private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig(); + private static final SessionInfo sessionInfo = + new SessionInfo(0, AuthorityChecker.SUPER_USER, ZoneId.systemDefault()); + + private static final List<AuditLogOperation> auditLogOperationList = + config.getAuditableOperationType(); + + private static final PrivilegeLevel auditablePrivilegeLevel = config.getAuditableOperationLevel(); + + private static final String auditableOperationResult = config.getAuditableOperationResult(); + + private static final SessionManager SESSION_MANAGER = SessionManager.getInstance(); + + private static final DataNodeDevicePathCache DEVICE_PATH_CACHE = + DataNodeDevicePathCache.getInstance(); + private static boolean tableViewisInitialized = false; + + private DNAuditLogger() { + // Empty constructor + } + + @NotNull + private static InsertRowStatement generateInsertStatement( + AuditLogFields auditLogFields, String log) throws IllegalPathException { + String username = auditLogFields.getUsername(); + String address = auditLogFields.getCliHostname(); + AuditEventType type = auditLogFields.getAuditType(); + AuditLogOperation operation = auditLogFields.getOperationType(); + PrivilegeType privilegeType = auditLogFields.getPrivilegeType(); + PrivilegeLevel privilegeLevel = judgePrivilegeLevel(privilegeType); + String dataNodeId = String.valueOf(config.getDataNodeId()); + InsertRowStatement insertStatement = new InsertRowStatement(); + insertStatement.setDevicePath( + DEVICE_PATH_CACHE.getPartialPath(String.format(AUDIT_LOG_DEVICE, dataNodeId, username))); + insertStatement.setTime(CommonDateTimeUtils.currentTime()); + insertStatement.setMeasurements( + new String[] { + USERNAME, + CLI_HOSTNAME, + AUDIT_EVENT_TYPE, + OPERATION_TYPE, + PRIVILEGE_TYPE, + PRIVILEGE_LEVEL, + RESULT, + DATABASE, + SQL_STRING, + LOG + }); + insertStatement.setAligned(false); + insertStatement.setValues( + new Object[] { + new Binary(username == null ? "null" : username, TSFileConfig.STRING_CHARSET), + new Binary(address == null ? "null" : address, TSFileConfig.STRING_CHARSET), + new Binary(type == null ? "null" : type.toString(), TSFileConfig.STRING_CHARSET), + new Binary( + operation == null ? "null" : operation.toString(), TSFileConfig.STRING_CHARSET), + new Binary( + privilegeType == null ? "null" : privilegeType.toString(), + TSFileConfig.STRING_CHARSET), + new Binary( + privilegeLevel == null ? "null" : privilegeLevel.toString(), + TSFileConfig.STRING_CHARSET), + auditLogFields.isResult(), + new Binary( + auditLogFields.getDatabase() == null ? "null" : auditLogFields.getDatabase(), + TSFileConfig.STRING_CHARSET), + new Binary( + auditLogFields.getSqlString() == null ? "null" : auditLogFields.getSqlString(), + TSFileConfig.STRING_CHARSET), + new Binary(log == null ? "null" : log, TSFileConfig.STRING_CHARSET) + }); + insertStatement.setDataTypes( + new TSDataType[] { + TSDataType.TEXT, + TSDataType.TEXT, + TSDataType.TEXT, + TSDataType.TEXT, + TSDataType.TEXT, + TSDataType.TEXT, + TSDataType.BOOLEAN, + TSDataType.TEXT, + TSDataType.TEXT, + TSDataType.TEXT + }); + return insertStatement; + } + + public static void log(AuditLogFields auditLogFields, String log) throws IllegalPathException { + if (!tableViewisInitialized) { Review Comment: `tableViewIsIntialized`, please💪🏻🔥⚡️ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
