github-actions[bot] commented on code in PR #70123: URL: https://github.com/apache/doris/pull/70123#discussion_r4689176112
########## fe/fe-common/src/main/java/org/apache/doris/qe/QueryAuditPolicy.java: ########## @@ -0,0 +1,449 @@ +// 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.doris.qe; + +import org.apache.doris.common.Config; +import org.apache.doris.common.ConfigBase; +import org.apache.doris.common.ConfigException; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.function.IntConsumer; +import java.util.function.IntSupplier; + +/** + * The rules of the FE config query_audit_always_on. While it is on, no parameter that ADMIN or a user can change at + * runtime may disable query auditing or hollow it out: the audit switches stay on, and no audit limit is weaker than + * its factory default. A limit whose smaller values drop events or cut statements short has its default as the + * minimum, one whose larger values hold events back has it as the maximum, and a batching knob, harmful in both + * directions, is fixed at it. The master also fits built-in audit load timeouts to its transaction and load sink + * limits before beginning the transaction and planning the load. The config itself changes only on disk, + * with a restart. + * + * <p>The rules are defined here only, for three sides: + * <ul> + * <li>reading: the effective value of each protected variable. Old values reach the readers from the image, the + * edit log, forwarded sessions and sessions opened before a change, so reading is where the rules hold;</li> + * <li>writing: SET, SET_VAR, UNSET and runtime config changes reject a value that breaks a rule, so that the + * attempt fails visibly and is audited itself;</li> + * <li>startup: the values in fe.conf and fe_custom.conf, which loading never checks, are corrected in memory.</li> + * </ul> + * + * <p>With the config off, every value takes effect as set, except a negative audit_plugin_max_sql_length: it counts + * as 0 and SET rejects it, because truncating a statement by it threw and dropped the audit event. + */ +public final class QueryAuditPolicy { + public static final String QUERY_AUDIT_ALWAYS_ON = "query_audit_always_on"; + + // Factory defaults of the protected parameters, which are their bounds while the policy is on. GlobalVariable + // and Config initialize the fields with them. + public static final long DEFAULT_AUDIT_PLUGIN_MAX_BATCH_BYTES = 50 * 1024 * 1024; + public static final long DEFAULT_AUDIT_PLUGIN_MAX_BATCH_INTERVAL_SEC = 60; + public static final int DEFAULT_AUDIT_PLUGIN_MAX_SQL_LENGTH = 2097152; + public static final int DEFAULT_AUDIT_PLUGIN_MAX_INSERT_STMT_LENGTH = Integer.MAX_VALUE; + public static final int DEFAULT_AUDIT_PLUGIN_LOAD_TIMEOUT_S = 600; + public static final int DEFAULT_AUDIT_EVENT_LOG_QUEUE_SIZE = 250000; + public static final int DEFAULT_QUERY_AUDIT_LOG_TIMEOUT_MS = 5000; + public static final int DEFAULT_BE_REPORT_QUERY_STATISTICS_TIMEOUT_MS = 60000; + public static final int DEFAULT_WORKLOAD_RUNTIME_STATUS_THREAD_INTERVAL_MS = 2000; + + private static final Logger LOG = LogManager.getLogger(QueryAuditPolicy.class); + + // The name of a session variable, which SessionVariable in fe-core defines. + private static final String ENABLE_PREPARED_STMT_AUDIT_LOG = "enable_prepared_stmt_audit_log"; + // The module of audit_log_modules that writes queries to fe.audit.log. + private static final String QUERY_AUDIT_LOG_MODULE = "query"; + private static final String SKIP_AUDIT_USER_LIST = "skip_audit_user_list"; + private static final String AUDIT_LOG_MODULES = "audit_log_modules"; + + private static final Protected MAX_SQL_LENGTH = new Protected(GlobalVariable.AUDIT_PLUGIN_MAX_SQL_LENGTH, + Rule.AT_LEAST, DEFAULT_AUDIT_PLUGIN_MAX_SQL_LENGTH); + // Its default is unlimited, so INSERT ... VALUES is truncated only by audit_plugin_max_sql_length. + private static final Protected MAX_INSERT_STMT_LENGTH = new Protected( + GlobalVariable.AUDIT_PLUGIN_MAX_INSERT_STMT_LENGTH, Rule.AT_LEAST, + DEFAULT_AUDIT_PLUGIN_MAX_INSERT_STMT_LENGTH); + // A batch whose load times out is dropped, not retried. + private static final Protected LOAD_TIMEOUT = new Protected(GlobalVariable.AUDIT_PLUGIN_LOAD_TIMEOUT, + Rule.AT_LEAST, DEFAULT_AUDIT_PLUGIN_LOAD_TIMEOUT_S); + // Larger batches keep events in memory, which a restart loses; smaller ones load each event on its own, and the + // loader's queue drops events once loads fall behind. + private static final Protected MAX_BATCH_BYTES = new Protected(GlobalVariable.AUDIT_PLUGIN_MAX_BATCH_BYTES, + Rule.FIXED, DEFAULT_AUDIT_PLUGIN_MAX_BATCH_BYTES); + private static final Protected MAX_BATCH_INTERVAL = new Protected( + GlobalVariable.AUDIT_PLUGIN_MAX_BATCH_INTERVAL_SEC, Rule.FIXED, + DEFAULT_AUDIT_PLUGIN_MAX_BATCH_INTERVAL_SEC); + + private static final ImmutableMap<String, Protected> VARIABLES = ImmutableMap.<String, Protected>builder() + .put(GlobalVariable.ENABLE_AUDIT_PLUGIN, Protected.forcedOn(GlobalVariable.ENABLE_AUDIT_PLUGIN)) + .put(ENABLE_PREPARED_STMT_AUDIT_LOG, Protected.forcedOn(ENABLE_PREPARED_STMT_AUDIT_LOG)) + .put(MAX_SQL_LENGTH.name, MAX_SQL_LENGTH) + .put(MAX_INSERT_STMT_LENGTH.name, MAX_INSERT_STMT_LENGTH) + .put(LOAD_TIMEOUT.name, LOAD_TIMEOUT) + .put(MAX_BATCH_BYTES.name, MAX_BATCH_BYTES) + .put(MAX_BATCH_INTERVAL.name, MAX_BATCH_INTERVAL) + .build(); + + // The mutable FE configs. They get no read-side check: their values come only from startup, which corrects them, + // and from ConfigBase.setMutableConfig, whose ProtectedConfHandler rejects what breaks the rules. + private static final ImmutableMap<String, ProtectedConfig> CONFIGS = ImmutableMap.<String, ProtectedConfig>builder() Review Comment: [P1] Prevent the load-disable switch from disabling always-on audit With `query_audit_always_on=true`, `ADMIN SET FRONTEND CONFIG ("disable_load_job" = "true")` still succeeds because that mutable config has no policy callback. Every subsequent built-in audit batch then fails in `GlobalTransactionMgr.beginTransaction` (and `CloudGlobalTransactionMgr.beginTransaction`), which reject all loads before any internal-coordinator exemption; `AuditLoader.loadIfNecessary` clears each failed batch. This leaves `fe.audit.log` running while permanently disabling the required audit-table destination through one supported runtime setting. Please protect this switch too, or consistently exempt authenticated built-in audit transactions from its begin/commit/publish checks. This is a lasting runtime disable path, rather than an incidental load failure. ########## fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java: ########## @@ -1534,6 +1536,36 @@ public TLoadTxnBeginResult loadTxnBegin(TLoadTxnBeginRequest request) throws TEx return result; } + static long getLoadTxnTimeout(TLoadTxnBeginRequest request) { + long timeout = request.isSetTimeout() ? request.getTimeout() : Config.stream_load_default_timeout_second; + if (!isAlwaysOnAuditLoad(request.isSetAuthCode(), request.getToken(), request.getDb(), request.getTbl())) { + return timeout; + } + // Called after token authentication on the master: a sending FE may have different timeout limits. + return boundAuditLoadTimeout(timeout); + } + + private static boolean isAlwaysOnAuditLoad(boolean hasAuthCode, String token, String db, String table) { + return QueryAuditPolicy.isAlwaysOn() && !hasAuthCode && !Strings.isNullOrEmpty(token) + && FeConstants.INTERNAL_DB_NAME.equals(db) && AuditLoader.AUDIT_LOG_TABLE.equals(table); + } + + private static long boundAuditLoadTimeout(long timeout) { + // The V1 load sink stores its RPC timeout in signed milliseconds. + long maximum = Math.min(Config.max_stream_load_timeout_second, Integer.MAX_VALUE / 1000); + return Math.max(Config.min_load_timeout_second, Math.min(timeout, maximum)); Review Comment: [P2] Validate that the audit timeout bounds have a safe intersection The lower bound can override both maxima calculated above. For example, `ADMIN SET FRONTEND CONFIG ("max_stream_load_timeout_second" = "0")` is accepted while always-on audit is enabled; with the default minimum of 1 this returns 1, and `checkValidTimeoutSecond` rejects every subsequent audit transaction because 1 exceeds the configured maximum. Even ordered positive bounds can defeat the overflow cap: minimum 2147484 and maximum 2147483647 return 2147484, which overflows the V1 sink's signed millisecond timeout. Neither config has a validation callback. Please reject policy-incompatible bounds or give internal audit loads a safe independent range, so runtime settings cannot make every batch fail despite this normalization. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
