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


##########
fe/fe-core/src/main/java/org/apache/doris/mysql/authenticate/AuthenticatorManager.java:
##########
@@ -370,6 +372,21 @@ boolean finishSuccessfulAuthentication(ConnectContext 
context, String remoteIp,
         if (refuseIfAccountLocked(context, response.getUserIdentity())) {
             return false;
         }
+        if (Env.getCurrentEnv().getAuth().isSecureInitialization()) {
+            try {
+                AuthenticationProtocol protocol = AuthenticationProtocol.MYSQL;
+                if (context.getMysqlChannel().hasProxyProtocol()) {
+                    protocol = AuthenticationProtocol.PROXIED_MYSQL;
+                }
+                boolean resetRequired = 
Env.getCurrentEnv().getAuth().checkBootstrapAuthentication(
+                        response.getUserIdentity(), remoteIp, protocol);

Review Comment:
   [P2] Unconditionally calling `checkBootstrapAuthentication` here rejects 
`admin`/`root` logins authenticated via external authenticators (e.g. LDAP, 
authentication plugins, or `authentication_chain`) when their local Doris 
password is still empty, and reports `using password: NO` because 
`suppliedPassword` is hardcoded to `false`. This also diverges from HTTP/Flight 
(`Auth.checkPlainPassword`), where the LDAP branch bypasses 
`BootstrapAuthPolicy`. Consider either scoping this check to password-skipping 
certificate auth (since native password auth is already checked in 
`DefaultAuthenticator`) or enforcing the same rule on the HTTP/Flight LDAP path 
for consistency.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AlterUserCommand.java:
##########
@@ -51,6 +52,22 @@ public <R, C> R accept(PlanVisitor<R, C> visitor, C context) 
{
     public void doRun(ConnectContext ctx, StmtExecutor executor) throws 
Exception {
         validate();
         Env.getCurrentEnv().getAuth().alterUser(alterUserInfo);
+        if (alterUserInfo.getOpType() == AlterUserOpType.SET_PASSWORD) {
+            Env.getCurrentEnv().getAuth().afterPasswordChange(ctx, 
alterUserInfo.getUserIdent(),
+                    alterUserInfo.getPassword());
+        }
+    }
+
+    @Override
+    public void afterForwardToMaster(ConnectContext ctx) {
+        if (ctx.getExecutor().getProxyStatusCode() != 0) {

Review Comment:
   [P2] `afterForwardToMaster` implicitly relies on 
`BootstrapPasswordGuard.check` having called `command.validate()` on the 
follower beforehand. For non-bootstrap sessions 
(`!ctx.isPasswordResetRequired()`), `alterUserInfo` has not been validated on 
the follower, so `alterUserInfo.getUserIdent()` is unanalyzed and 
`getPassword()` is null. Guarding early with `if 
(!ctx.isPasswordResetRequired() || ctx.getExecutor().getProxyStatusCode() != 0) 
{ return; }` avoids touching unvalidated state and makes the precondition 
explicit.



##########
fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java:
##########
@@ -553,14 +562,19 @@ private void handleChangeUser() throws IOException {
 
         // Check password.
         List<UserIdentity> currentUserIdentity = Lists.newArrayList();
+        AuthenticationProtocol protocol = AuthenticationProtocol.MYSQL;
+        if (ctx.getMysqlChannel().hasProxyProtocol()) {
+            protocol = AuthenticationProtocol.PROXIED_MYSQL;
+        }
         try {
             Env.getCurrentEnv().getAuth()
-                .checkPassword(userName, ctx.remoteIP, password, 
authPluginData, currentUserIdentity);
+                .checkPassword(userName, ctx.remoteIP, password, 
authPluginData, currentUserIdentity, protocol);
         } catch (AuthenticationException e) {
             ctx.getState().setError(ErrorCode.ERR_ACCESS_DENIED_ERROR, 
"Authentication failed.");
             return;
         }
         ctx.setCurrentUserIdentity(currentUserIdentity.get(0));

Review Comment:
   [P3] Instead of discarding the return value of `checkPassword(...)` and 
hardcoding `ctx.setPasswordResetRequired(false)`, assign the returned 
`passwordResetRequired` flag directly 
(`ctx.setPasswordResetRequired(passwordResetRequired)`). While `root`/`admin` 
are currently blocked above, propagating the return value prevents a future 
bypass if that restriction ever changes.



##########
fe/fe-core/src/main/java/org/apache/doris/qe/BootstrapPasswordGuard.java:
##########
@@ -0,0 +1,172 @@
+// 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.alter.AlterUserOpType;
+import org.apache.doris.analysis.SetType;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.UserException;
+import org.apache.doris.mysql.MysqlCommand;
+import org.apache.doris.nereids.analyzer.UnboundAlias;
+import org.apache.doris.nereids.analyzer.UnboundFunction;
+import org.apache.doris.nereids.analyzer.UnboundOneRowRelation;
+import org.apache.doris.nereids.analyzer.UnboundResultSink;
+import org.apache.doris.nereids.analyzer.UnboundVariable;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentUser;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.commands.AlterUserCommand;
+import org.apache.doris.nereids.trees.plans.commands.ExecuteCommand;
+import org.apache.doris.nereids.trees.plans.commands.PrepareCommand;
+import org.apache.doris.nereids.trees.plans.commands.SetOptionsCommand;
+import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
+import org.apache.doris.nereids.trees.plans.commands.info.AlterUserInfo;
+import 
org.apache.doris.nereids.trees.plans.commands.info.SetCharsetAndCollateVarOp;
+import org.apache.doris.nereids.trees.plans.commands.info.SetNamesVarOp;
+import org.apache.doris.nereids.trees.plans.commands.info.SetPassVarOp;
+import org.apache.doris.nereids.trees.plans.commands.info.SetSessionVarOp;
+import org.apache.doris.nereids.trees.plans.commands.info.SetVarOp;
+import org.apache.doris.nereids.trees.plans.logical.LogicalLimit;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+
+import java.util.Locale;
+import java.util.Set;
+
+/** Restricts a client bootstrap session without changing internal users or 
their privileges. */
+public final class BootstrapPasswordGuard {
+    private static final Set<String> CONNECTION_FUNCTIONS = Set.of(
+            "connection_id", "current_user", "database", "schema", 
"session_user", "user", "version");
+
+    private BootstrapPasswordGuard() {
+    }
+
+    public static boolean isAllowedCommand(MysqlCommand command) {
+        switch (command) {
+            case COM_QUERY:
+            case COM_QUIT:
+            case COM_PING:
+            case COM_CHANGE_USER:
+            case COM_RESET_CONNECTION:
+            case COM_SET_OPTION:
+            case COM_STMT_PREPARE:
+            case COM_STMT_EXECUTE:
+            case COM_STMT_CLOSE:
+            case COM_STMT_RESET:
+            case COM_STMT_FETCH:
+            case COM_STMT_SEND_LONG_DATA:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static void check(ConnectContext ctx, LogicalPlan plan) throws 
UserException {
+        if (!ctx.isPasswordResetRequired()) {
+            return;
+        }
+        if (isAllowed(ctx, plan)) {
+            return;
+        }
+        throw new 
AnalysisException(ErrorCode.ERR_MUST_CHANGE_PASSWORD.formatErrorMsg(),
+                ErrorCode.ERR_MUST_CHANGE_PASSWORD);
+    }
+
+    private static boolean isAllowed(ConnectContext ctx, LogicalPlan plan) 
throws UserException {
+        if (plan instanceof ExecuteCommand) {
+            PreparedStatementContext prepared = 
ctx.getPreparedStementContext(((ExecuteCommand) plan).getStmtName());
+            return prepared != null && isAllowed(ctx, 
prepared.command.getLogicalPlan());
+        }
+        if (plan instanceof PrepareCommand) {
+            return isAllowed(ctx, ((PrepareCommand) plan).getLogicalPlan());
+        }
+        if (plan instanceof ShowVariablesCommand) {
+            return true;
+        }
+        if (plan instanceof AlterUserCommand) {
+            AlterUserCommand command = (AlterUserCommand) plan;
+            command.validate();

Review Comment:
   [P3] Calling `command.validate()` (and `password.validate(ctx)` below) 
inside the guard causes invalid `ALTER USER`/`SET PASSWORD` statements during 
bootstrap (e.g., `ALTER USER root ACCOUNT_LOCK`) to throw validation errors 
instead of `ERR_MUST_CHANGE_PASSWORD (1820)`, and runs `validate()` twice on 
valid statements.



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