924060929 commented on code in PR #70123: URL: https://github.com/apache/doris/pull/70123#discussion_r4750275367
########## 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] `Config.disable_load_job` is mutable (`masterOnly = true`) and unprotected, so an ADMIN can still permanently stop writes to `__internal_schema.audit_log` at runtime via `ADMIN SET FRONTEND CONFIG ("disable_load_job" = "true")` (or `/api/_set_config`). Both `GlobalTransactionMgr` and `CloudGlobalTransactionMgr` then reject `beginTransaction`/`commitTransaction` for the built-in `AuditLoader` stream loads and every audit batch is discarded. Prefer exempting internal always-on audit loads (`__internal_schema.audit_log` with cluster token) from `Config.disable_load_job` in `GlobalTransactionMgr`/`CloudGlobalTransactionMgr` so operators can still pause user loads during maintenance without breaking audit; alternatively, protect `disable_load_job` in `QueryAuditPolicy`. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetSessionVarOp.java: ########## @@ -147,9 +150,26 @@ public String toSql() { return sb.toString(); } + public boolean setsProtectedPreparedAuditValue() { Review Comment: [P2] `setsProtectedPreparedAuditValue()` and `restoreProtectedPreparedAuditValue()` hardcode `SessionVariable.ENABLE_PREPARED_STMT_AUDIT_LOG`. If another protected session variable is added to `QueryAuditPolicy` later, this special case in `SetOptionsCommand.afterForwardToMaster` will be easy to miss. Consider generalizing this against `QueryAuditPolicy.protectedVariableNames()` and `effectiveVariableValue()`. ########## fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java: ########## @@ -1572,6 +1574,43 @@ public TLoadTxnBeginResult loadTxnBegin(TLoadTxnBeginRequest request) throws TEx return result; } + static long getLoadTxnTimeout(TLoadTxnBeginRequest request) throws AnalysisException { + 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) throws AnalysisException { Review Comment: [P1] `Config.max_stream_load_timeout_second`, `Config.min_load_timeout_second`, and `Config.max_running_txn_num_per_db` are all runtime-mutable configs without policy protection: - Setting `max_stream_load_timeout_second = 0` or `min_load_timeout_second = 300000` makes `minimum > maximum` here and causes every audit stream load to fail with `AnalysisException`, permanently disabling `__internal_schema.audit_log`. - Setting `max_stream_load_timeout_second = 1` clamps every audit stream load timeout to 1s, bypassing the `audit_plugin_load_timeout >= 600` guarantee. - Setting `max_running_txn_num_per_db = 0` rejects `beginTransaction` on `__internal_schema`. Consider decoupling internal always-on audit loads from these user-load configs (keeping only the `Integer.MAX_VALUE / 1000` overflow cap), or enforcing safe bounds on these configs via `QueryAuditPolicy`. ########## fe/fe-common/src/main/java/org/apache/doris/common/ConfigBase.java: ########## @@ -140,6 +141,7 @@ public void handle(Field field, String confVal) throws Exception { private static String confFile; private static String customConfFile; + private static String lastReadableCustomAuditPolicy; Review Comment: [P2] Coupling `ConfigBase` directly to `QueryAuditPolicy.QUERY_AUDIT_ALWAYS_ON` via `lastReadableCustomAuditPolicy` special-cases a single config key in the generic config base class (the same `resetPersist` issue also applies to other non-mutable security configs such as `enable_all_http_auth`). A cleaner abstraction would be preserving non-mutable (or explicitly marked) keys generically on `resetPersist` instead of hardcoding one audit key here. -- 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]
