924060929 commented on code in PR #70123:
URL: https://github.com/apache/doris/pull/70123#discussion_r4764100214


##########
fe/fe-core/src/main/java/org/apache/doris/qe/VariableMgr.java:
##########
@@ -423,9 +467,37 @@ public static void setVarForNonMasterFE(SessionVariable 
sessionVariable, SetVar
             }
             
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, 
setVar.getVariable());
         }
+        // The master validated these settings. Keep the callback's legacy 
assignment order.
+        if (!validatedByMaster) {
+            checkAuditVariableValue(varCtx, getValueToSet(setVar, varCtx));
+        }
         setVarInternal(sessionVariable, setVar, varCtx);
     }
 
+    /** Apply the master's already-replayed audit GLOBAL assignment to the 
issuing session if it has a field. */
+    public static boolean applyReplayedAuditGlobal(SessionVariable session, 
String name) throws DdlException {
+        VarContext context = getVarContext(name);
+        if (context == null) {
+            return false;
+        }
+        String canonicalName = getName(context);
+        boolean policyApplies = QueryAuditPolicy.isAlwaysOn()
+                || 
GlobalVariable.AUDIT_PLUGIN_MAX_SQL_LENGTH.equals(canonicalName);

Review Comment:
   [P3] Leftover from the removed off-mode negative 
`audit_plugin_max_sql_length` check: `QueryAuditPolicy` no longer applies when 
`isAlwaysOn()` is false, so `|| 
GlobalVariable.AUDIT_PLUGIN_MAX_SQL_LENGTH.equals(canonicalName)` can be 
removed.



##########
threat-model.md:
##########
@@ -606,6 +609,31 @@ provenance.
     should be filed against the §4.14 follow-up rather than closed as
     noise.
 
+13. **Query audit that database accounts cannot turn off, when
+    `query_audit_always_on` is on** *(maintainer, M20)*. *Condition*:
+    the FE runs with `query_audit_always_on = true` (§4.5a; default
+    off). Then every MySQL statement that FE receives produces a query audit
+    event — failed statements and server-side `COM_STMT_EXECUTE`
+    included — handed to both `fe.audit.log` and
+    `__internal_schema.audit_log`, and no database account, `root`
+    included, can change a protected audit setting to disable either path,
+    empty the recorded statement, or truncate it below the factory default
+    through SQL (`SET
+    [GLOBAL]`, `SET_VAR`, `UNSET`), the FE HTTP config interface
+    (`/api/_set_config`), or session settings. Those changes are rejected.
+    Rejected MySQL statements are audited. Only someone who can edit the FE's
+    configuration files and restart it can turn the switch off.
+    *Violation symptom*: with the switch on, a supported entry still

Review Comment:
   [P2] *Condition* was narrowed to changing a protected audit setting, but 
*Violation symptom* still states "a supported entry still stops query audit 
records from being produced or written" while *Not covered* does not explicitly 
exclude global load/transaction operational switches (e.g., `disable_load_job`, 
`max_stream_load_timeout_second`, `max_running_txn_num_per_db`). Please align 
*Violation symptom* and *Not covered* with the narrowed scope to avoid triage 
ambiguity.



##########
fe/fe-common/src/main/java/org/apache/doris/qe/QueryAuditPolicy.java:
##########
@@ -0,0 +1,430 @@
+// 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, the 
protected audit switches stay on and
+ * the protected audit limits remain at least as strong as their factory 
defaults. 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 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, the audit settings take effect as configured.
+ */
+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,

Review Comment:
   [P1] After removing the Stream Load timeout clamping in 
`FrontendServiceImpl`, `LOAD_TIMEOUT` using `Rule.AT_LEAST` (600) with no upper 
bound allows an ADMIN to disable `__internal_schema.audit_log` via the 
protected variable itself:
   
   `SET GLOBAL audit_plugin_load_timeout = 1000000;`
   
   This passes `QueryAuditPolicy.checkVariableValue`, and every subsequent 
`AuditStreamLoader` batch fails in 
`GlobalTransactionMgrIface.checkValidTimeoutSecond` (`1000000 > 
Config.max_stream_load_timeout_second`, default 259200) and is discarded.
   
   Consider changing `LOAD_TIMEOUT` to `Rule.FIXED` (600, like 
`MAX_BATCH_BYTES`/`MAX_BATCH_INTERVAL`) or enforcing an upper bound `<= 
Config.max_stream_load_timeout_second`.



-- 
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]

Reply via email to