CRZbulabula commented on code in PR #16428: URL: https://github.com/apache/iotdb/pull/16428#discussion_r2355327679
########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/audit/CNAuditLogger.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.confignode.audit; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot; +import org.apache.iotdb.commons.audit.AbstractAuditLogger; +import org.apache.iotdb.commons.audit.AuditLogFields; +import org.apache.iotdb.confignode.client.async.CnToDnAsyncRequestType; +import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager; +import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext; +import org.apache.iotdb.confignode.consensus.request.read.region.GetRegionIdPlan; +import org.apache.iotdb.confignode.consensus.response.partition.GetRegionIdResp; +import org.apache.iotdb.confignode.manager.ConfigManager; +import org.apache.iotdb.confignode.manager.IManager; +import org.apache.iotdb.consensus.exception.ConsensusException; +import org.apache.iotdb.db.conf.IoTDBConfig; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.mpp.rpc.thrift.TAuditLogReq; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CNAuditLogger extends AbstractAuditLogger { + private static final Logger logger = LoggerFactory.getLogger(CNAuditLogger.class); + private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig(); + + private static final String AUDIT_LOG_DEVICE = "root.__audit.log.%s.%s"; + + protected final IManager configManager; + + public CNAuditLogger(ConfigManager configManager) { + this.configManager = configManager; + } + + public void log(AuditLogFields auditLogFields, String log) { + // find database "__audit"'s data_region + final GetRegionIdPlan plan = new GetRegionIdPlan(TConsensusGroupType.DataRegion); + plan.setDatabase("root.__audit"); + plan.setStartTimeSlotId(new TTimePartitionSlot(0)); + plan.setEndTimeSlotId(new TTimePartitionSlot(Long.MAX_VALUE)); + TConsensusGroupId regionId; + try { + GetRegionIdResp resp = (GetRegionIdResp) configManager.getConsensusManager().read(plan); Review Comment: ```suggestion List<TRegionReplicaSet> auditReplicaSets = configManager.getPartitionManager().getAllReplicaSets("root.__audit", TConsensusGroupType.DataRegion); ``` ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/audit/CNAuditLogger.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.confignode.audit; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot; +import org.apache.iotdb.commons.audit.AbstractAuditLogger; +import org.apache.iotdb.commons.audit.AuditLogFields; +import org.apache.iotdb.confignode.client.async.CnToDnAsyncRequestType; +import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager; +import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext; +import org.apache.iotdb.confignode.consensus.request.read.region.GetRegionIdPlan; +import org.apache.iotdb.confignode.consensus.response.partition.GetRegionIdResp; +import org.apache.iotdb.confignode.manager.ConfigManager; +import org.apache.iotdb.confignode.manager.IManager; +import org.apache.iotdb.consensus.exception.ConsensusException; +import org.apache.iotdb.db.conf.IoTDBConfig; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.mpp.rpc.thrift.TAuditLogReq; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CNAuditLogger extends AbstractAuditLogger { + private static final Logger logger = LoggerFactory.getLogger(CNAuditLogger.class); + private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig(); + + private static final String AUDIT_LOG_DEVICE = "root.__audit.log.%s.%s"; + + protected final IManager configManager; + + public CNAuditLogger(ConfigManager configManager) { + this.configManager = configManager; + } + + public void log(AuditLogFields auditLogFields, String log) { + // find database "__audit"'s data_region + final GetRegionIdPlan plan = new GetRegionIdPlan(TConsensusGroupType.DataRegion); + plan.setDatabase("root.__audit"); + plan.setStartTimeSlotId(new TTimePartitionSlot(0)); + plan.setEndTimeSlotId(new TTimePartitionSlot(Long.MAX_VALUE)); + TConsensusGroupId regionId; + try { + GetRegionIdResp resp = (GetRegionIdResp) configManager.getConsensusManager().read(plan); + List<TConsensusGroupId> dataRegionIdList = resp.getDataRegionIdList(); + if (resp.getStatus().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode() + || dataRegionIdList.isEmpty()) { + logger.error("Failed to get regionId of database root.__audit: {}", resp.getStatus()); + return; + } + regionId = dataRegionIdList.get(0); + } catch (ConsensusException e) { + logger.error("Failed to get regionId of database root.__audit", e); + return; + } + // use ConfigManager.getLoadManager().getLoadCache().getRegionLeaderMap() to get regionLeaderId + Map<TConsensusGroupId, Integer> regionLeaderMap = + configManager.getLoadManager().getLoadCache().getRegionLeaderMap(); + Integer regionLeaderId = regionLeaderMap.get(regionId); + Map<Integer, TDataNodeLocation> dataNodeMap = + configManager.getNodeManager().getRegisteredDataNodeLocations(); + TDataNodeLocation regionLeader = dataNodeMap.get(regionLeaderId); Review Comment: Pack these codes into a function `getRegionLeaderLocation(TConsensusGroupId consensusGroupId)`, such that u can benifit all developers who have the same requirement :) ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/audit/CNAuditLogger.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.confignode.audit; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot; +import org.apache.iotdb.commons.audit.AbstractAuditLogger; +import org.apache.iotdb.commons.audit.AuditLogFields; +import org.apache.iotdb.confignode.client.async.CnToDnAsyncRequestType; +import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager; +import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext; +import org.apache.iotdb.confignode.consensus.request.read.region.GetRegionIdPlan; +import org.apache.iotdb.confignode.consensus.response.partition.GetRegionIdResp; +import org.apache.iotdb.confignode.manager.ConfigManager; +import org.apache.iotdb.confignode.manager.IManager; +import org.apache.iotdb.consensus.exception.ConsensusException; +import org.apache.iotdb.db.conf.IoTDBConfig; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.mpp.rpc.thrift.TAuditLogReq; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CNAuditLogger extends AbstractAuditLogger { + private static final Logger logger = LoggerFactory.getLogger(CNAuditLogger.class); + private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig(); + + private static final String AUDIT_LOG_DEVICE = "root.__audit.log.%s.%s"; + + protected final IManager configManager; + + public CNAuditLogger(ConfigManager configManager) { + this.configManager = configManager; + } + + public void log(AuditLogFields auditLogFields, String log) { + // find database "__audit"'s data_region + final GetRegionIdPlan plan = new GetRegionIdPlan(TConsensusGroupType.DataRegion); + plan.setDatabase("root.__audit"); + plan.setStartTimeSlotId(new TTimePartitionSlot(0)); + plan.setEndTimeSlotId(new TTimePartitionSlot(Long.MAX_VALUE)); + TConsensusGroupId regionId; + try { + GetRegionIdResp resp = (GetRegionIdResp) configManager.getConsensusManager().read(plan); + List<TConsensusGroupId> dataRegionIdList = resp.getDataRegionIdList(); + if (resp.getStatus().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode() + || dataRegionIdList.isEmpty()) { + logger.error("Failed to get regionId of database root.__audit: {}", resp.getStatus()); + return; + } + regionId = dataRegionIdList.get(0); + } catch (ConsensusException e) { + logger.error("Failed to get regionId of database root.__audit", e); + return; + } + // use ConfigManager.getLoadManager().getLoadCache().getRegionLeaderMap() to get regionLeaderId + Map<TConsensusGroupId, Integer> regionLeaderMap = + configManager.getLoadManager().getLoadCache().getRegionLeaderMap(); + Integer regionLeaderId = regionLeaderMap.get(regionId); + Map<Integer, TDataNodeLocation> dataNodeMap = + configManager.getNodeManager().getRegisteredDataNodeLocations(); + TDataNodeLocation regionLeader = dataNodeMap.get(regionLeaderId); + Map<Integer, TDataNodeLocation> targetDatanodeMap = + Collections.singletonMap(regionLeaderId, regionLeader); + TAuditLogReq req = + new TAuditLogReq( + auditLogFields.getUsername(), + auditLogFields.getCliHostname(), + auditLogFields.getAuditType().toString(), + auditLogFields.getOperationType().toString(), + auditLogFields.getPrivilegeType() == null + ? null + : auditLogFields.getPrivilegeType().toString(), + auditLogFields.isResult(), + auditLogFields.getDatabase(), + auditLogFields.getSqlString(), + log); + DataNodeAsyncRequestContext<TAuditLogReq, TSStatus> clientHandler = + new DataNodeAsyncRequestContext<>( + CnToDnAsyncRequestType.WRITE_AUDIT_LOG, req, targetDatanodeMap); + CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler); + Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap(); Review Comment: The current implementation that employs the `DataNodeAsyncRequestContext` is NOT truely "asynchronous". Specifically, the 'sendAsyncRequestWithRetry()' method will wait for all retries complete before return. The term "async" in this case is for asynchronously sending the same requests to multiple DataNodes simultaneously. Considering the target of CNAudtiLogger, u'd better refer the implementation of HeartbeatService.pingRegisteredDataNode(). By appending a new writeAudtiLog() interface in `AsyncDataNodeHeartbeatClientPool`, the main thread is not required to wait until the write audit log request to be complete. ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/response/partition/GetRegionIdResp.java: ########## @@ -46,6 +46,10 @@ public void setStatus(TSStatus status) { this.status = status; } + public List<TConsensusGroupId> getDataRegionIdList() { + return dataRegionIdList; + } + Review Comment: Just revert this modification. ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/audit/CNAuditLogger.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.confignode.audit; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot; +import org.apache.iotdb.commons.audit.AbstractAuditLogger; +import org.apache.iotdb.commons.audit.AuditLogFields; +import org.apache.iotdb.confignode.client.async.CnToDnAsyncRequestType; +import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager; +import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext; +import org.apache.iotdb.confignode.consensus.request.read.region.GetRegionIdPlan; +import org.apache.iotdb.confignode.consensus.response.partition.GetRegionIdResp; +import org.apache.iotdb.confignode.manager.ConfigManager; +import org.apache.iotdb.confignode.manager.IManager; +import org.apache.iotdb.consensus.exception.ConsensusException; +import org.apache.iotdb.db.conf.IoTDBConfig; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.mpp.rpc.thrift.TAuditLogReq; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CNAuditLogger extends AbstractAuditLogger { + private static final Logger logger = LoggerFactory.getLogger(CNAuditLogger.class); + private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig(); + + private static final String AUDIT_LOG_DEVICE = "root.__audit.log.%s.%s"; Review Comment: ```suggestion private static final String AUDIT_LOG_DEVICE = "root.__audit.log.control"; ``` Since the audit logs generated through ConfigNode will are be system behaviours, we would need a special path for storage. ########## iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/audit/AuditLogOperation.java: ########## @@ -17,12 +17,13 @@ * under the License. */ -package org.apache.iotdb.db.audit; +package org.apache.iotdb.commons.audit; public enum AuditLogOperation { DDL, DML, QUERY, + CONTROL, NULL; Review Comment: The `NULL` can be removed, since its useless in our current implementation while we do not consider the compatibility with the previous version. ########## iotdb-protocol/thrift-datanode/src/main/thrift/datanode.thrift: ########## @@ -246,7 +246,7 @@ struct TUpdateTriggerLocationReq { struct TFireTriggerReq { 1: required string triggerName 2: required binary tablet - 3: required byte triggerEvent + 3: required i8 triggerEvent Review Comment: Why? ########## iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/audit/CNAuditLogger.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.confignode.audit; + +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType; +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; +import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot; +import org.apache.iotdb.commons.audit.AbstractAuditLogger; +import org.apache.iotdb.commons.audit.AuditLogFields; +import org.apache.iotdb.confignode.client.async.CnToDnAsyncRequestType; +import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager; +import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext; +import org.apache.iotdb.confignode.consensus.request.read.region.GetRegionIdPlan; +import org.apache.iotdb.confignode.consensus.response.partition.GetRegionIdResp; +import org.apache.iotdb.confignode.manager.ConfigManager; +import org.apache.iotdb.confignode.manager.IManager; +import org.apache.iotdb.consensus.exception.ConsensusException; +import org.apache.iotdb.db.conf.IoTDBConfig; +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.mpp.rpc.thrift.TAuditLogReq; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CNAuditLogger extends AbstractAuditLogger { Review Comment: The class `CNAuditLogger` could be a singleton for easier invocation. ########## 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) { + Statement statement = + StatementGenerator.createStatement( + "CREATE DATABASE " + SystemConstant.AUDIT_DATABASE, ZoneId.systemDefault()); + ExecutionResult result = + COORDINATOR.executeForTreeModel( + statement, + SESSION_MANAGER.requestQueryId(), + sessionInfo, + "", + ClusterPartitionFetcher.getInstance(), + SCHEMA_FETCHER); + if (result.status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode() + || result.status.getCode() == TSStatusCode.DATABASE_ALREADY_EXISTS.getStatusCode()) { + statement = + StatementGenerator.createStatement( + "CREATE VIEW __view_system.audit_log (\n" + + " dn_id STRING TAG,\n" + + " user_name STRING TAG,\n" + + " cli_hostname STRING FIELD,\n" + + " audit_event_type INT32 FIELD,\n" + + " operation_type INT32 FIELD,\n" + + " privilege_type INT32 FIELD,\n" + + " privilege_level INT32 FIELD,\n" + + " result BOOLEAN FIELD,\n" + + " database STRING FIELD,\n" + + " log STRING FIELD\n" + + ") AS root.__audit.log", + ZoneId.systemDefault()); + SqlParser relationSqlParser = new SqlParser(); + IClientSession session = SESSION_MANAGER.getCurrSession(); + Metadata metadata = LocalExecutionPlanner.getInstance().metadata; + COORDINATOR.executeForTableModel( + statement, + relationSqlParser, + session, + SESSION_MANAGER.requestQueryId(), + SESSION_MANAGER.getSessionInfoOfTableModel(session), + "", + metadata, + config.getQueryTimeoutThreshold()); + tableViewisInitialized = true; + } else { + logger.error("Failed to create database {} for audit log", SystemConstant.AUDIT_DATABASE); + } + } + String username = auditLogFields.getUsername(); + String address = auditLogFields.getCliHostname(); + AuditEventType type = auditLogFields.getAuditType(); + AuditLogOperation operation = auditLogFields.getOperationType(); + PrivilegeType privilegeType = auditLogFields.getPrivilegeType(); + PrivilegeLevel privilegeLevel = judgePrivilegeLevel(privilegeType); + boolean result = auditLogFields.isResult(); + String dataNodeId = String.valueOf(config.getDataNodeId()); + + // to do: check whether this event should be logged. + // if whitelist or blacklist is used, only ip on the whitelist or blacklist can be logged + + if (auditLogOperationList == null || !auditLogOperationList.contains(operation)) { + return; + } + if (auditablePrivilegeLevel == PrivilegeLevel.OBJECT + && privilegeLevel == PrivilegeLevel.GLOBAL) { + return; + } + if (!auditableOperationResult.equals("BOTH") + && ((auditableOperationResult.equals("SUCCESS") && !result) + || (auditableOperationResult.equals("FAILURE") && result))) { + return; + } Review Comment: Why not move them into the `AbstractAuditLogger`? ########## 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: WTF u mean `💪🏻🔥⚡️`??? ########## 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) { + Statement statement = + StatementGenerator.createStatement( + "CREATE DATABASE " + SystemConstant.AUDIT_DATABASE, ZoneId.systemDefault()); + ExecutionResult result = + COORDINATOR.executeForTreeModel( + statement, + SESSION_MANAGER.requestQueryId(), + sessionInfo, + "", + ClusterPartitionFetcher.getInstance(), + SCHEMA_FETCHER); + if (result.status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode() + || result.status.getCode() == TSStatusCode.DATABASE_ALREADY_EXISTS.getStatusCode()) { + statement = + StatementGenerator.createStatement( + "CREATE VIEW __view_system.audit_log (\n" + + " dn_id STRING TAG,\n" + + " user_name STRING TAG,\n" + + " cli_hostname STRING FIELD,\n" + + " audit_event_type INT32 FIELD,\n" + + " operation_type INT32 FIELD,\n" + + " privilege_type INT32 FIELD,\n" + + " privilege_level INT32 FIELD,\n" + + " result BOOLEAN FIELD,\n" + + " database STRING FIELD,\n" + + " log STRING FIELD\n" + + ") AS root.__audit.log", + ZoneId.systemDefault()); + SqlParser relationSqlParser = new SqlParser(); + IClientSession session = SESSION_MANAGER.getCurrSession(); + Metadata metadata = LocalExecutionPlanner.getInstance().metadata; + COORDINATOR.executeForTableModel( + statement, + relationSqlParser, + session, + SESSION_MANAGER.requestQueryId(), + SESSION_MANAGER.getSessionInfoOfTableModel(session), + "", + metadata, + config.getQueryTimeoutThreshold()); + tableViewisInitialized = true; Review Comment: Plz inquire the concurrent safety of ur current implementation. The `create database` is safe since the ConfigNode will reject repeated database creation, while I'm not sure about the `create view`. Because the `create view` could be executed simultaneously across cluster's DataNodes. ########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/DataNodeInternalRPCServiceImpl.java: ########## @@ -2962,6 +2968,29 @@ public TSStatus insertRecord(TSInsertRecordReq req) throws TException { } } + @Override + public TSStatus writeAuditLog(TAuditLogReq req) { + if (req == null) { + return RpcUtils.getStatus(TSStatusCode.ILLEGAL_PARAMETER, "Audit log is null"); + } Review Comment: No need to judge whether the `req` is null here. ########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java: ########## @@ -1046,8 +1047,17 @@ public class IoTDBConfig { Arrays.asList(AuditLogStorage.IOTDB, AuditLogStorage.LOGGER); /** Indicates the category collection of audit logs * */ - private List<AuditLogOperation> auditLogOperation = - Arrays.asList(AuditLogOperation.DML, AuditLogOperation.DDL, AuditLogOperation.QUERY); + private List<AuditLogOperation> auditableOperationType = + Arrays.asList( + AuditLogOperation.DML, + AuditLogOperation.DDL, + AuditLogOperation.QUERY, + AuditLogOperation.CONTROL); + + /** The level of privilege required to record audit logs * */ + private PrivilegeLevel auditableOperationLevel = PrivilegeLevel.GLOBAL; + + private String auditableOperationResult = "BOTH"; Review Comment: Replace `BOTH` with `SUCCESS, FAIL`. ########## 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 { Review Comment: The `DNAuditLogger` could be a singleton for easier invocation. -- 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]
