From 74370897e0c2f70c6c81c58076493b3905973f13 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Thu, 3 Sep 2026 12:18:57 +0530 Subject: [PATCH 01/30] [feature](auth) SU: session-narrowed identity switch `SU 'user'[@'host'] WITH ROLES ('r1'[, ...]) [WORKLOAD GROUP 'wg']` switches the authenticated session to another user with a mandatory role list -- MySQL's proxy-user model, narrowed. A middle tier (an AI or BI gateway, an MCP server) authenticates once as itself and then runs each end-user session as that person, restricted to an explicit subset of the person's roles, so Doris -- not the gateway -- enforces the table privileges, row policies and workload placement, and the audit log names the person. Semantics: - the role list REPLACES the target's role union for the session and never widens it; every requested role must already be granted to the target; - current_user() returns the target, so user-bound row policies and current_user()-keyed predicates evaluate as the person; the audit log records the person as the user; - session-only and one-shot: a second SU in a switched session is refused; resetConnection() / COM_CHANGE_USER revert to the AUTHENTICATED identity, never to the target's full roles; nothing is persisted; - a builtin session_is_narrowed() (BOOLEAN, folded on the FE like current_user()) lets policies and diagnostics tell a switched session apart; - the implicit information_schema / mysql read baseline is kept in a narrowed session, so clients keep working; direct/default-role grants are dropped; - the narrowed session keeps the person's own WORKLOAD GROUP usage (placement follows the person; never wider than the person's own session). One choke point: Auth.getRolesByUserWithLdap returns the session override when one is set on the ConnectContext; everything downstream (privilege checks, row policies, SHOW GRANTS for the session) follows from that, with no change to the privilege tables. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Raghvendra Singh --- .../java/org/apache/doris/common/Config.java | 16 + .../doris/catalog/BuiltinScalarFunctions.java | 2 + .../apache/doris/mysql/privilege/Auth.java | 137 ++++++- .../nereids/parser/LogicalPlanBuilder.java | 11 + .../rules/FoldConstantRuleOnFE.java | 8 + .../functions/scalar/SessionIsNarrowed.java | 74 ++++ .../visitor/ScalarFunctionVisitor.java | 5 + .../doris/nereids/trees/plans/PlanType.java | 1 + .../trees/plans/commands/SuUserCommand.java | 169 +++++++++ .../org/apache/doris/qe/ConnectContext.java | 43 +++ .../doris/qe/MysqlConnectProcessor.java | 3 + .../org/apache/doris/qe/StmtExecutor.java | 11 + .../mysql/privilege/SuUserNarrowingTest.java | 336 ++++++++++++++++++ .../org/apache/doris/nereids/DorisLexer.g4 | 1 + .../org/apache/doris/nereids/DorisParser.g4 | 4 + 15 files changed, 820 insertions(+), 1 deletion(-) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SessionIsNarrowed.java create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java create mode 100644 fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java diff --git a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java index 3d1cecbf2d3929..7e080a1daf73b6 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java +++ b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java @@ -3016,6 +3016,22 @@ public void handle(Field field, String value) throws Exception { options = {"default", "ranger-doris"}) public static String access_controller_type = "default"; + @ConfField(mutable = true, masterOnly = false, description = + "Comma-separated list of non-ADMIN qualified users allowed to execute SU " + + "(session-narrowed identity switch). Empty = ADMIN only. Per-FE; set on every FE.") + public static String switch_user_users = ""; + + @ConfField(mutable = true, masterOnly = false, description = + "Whose granted roles bound the SU role override: 'target' (default, the switched-to user's " + + "granted roles, pure narrowing) or 'switcher' (the switching account's granted roles).") + public static String switch_user_role_ceiling = "target"; + + @ConfField(mutable = true, masterOnly = false, description = + "Roles matching this regex are DORMANT in normal sessions (excluded from privilege checks and " + + "row-policy matching for the session's own identity) and activate only when explicitly requested " + + "via SU. Empty = disabled. Invalid regex = disabled (logged). Per-FE; set on every FE.") + public static String su_only_roles_pattern = ""; + /* https://forums.oracle.com/ords/apexds/post/je-log-checksumexception-2812 when meeting disk damage or other reason described in the oracle forums and fe cannot start due to `com.sleepycat.je.log.ChecksumException`, we diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java index 2d7aaecd38d4d3..b3be1f4a7328a8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java @@ -469,6 +469,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsDiff; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsSub; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha1; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha2; @@ -1086,6 +1087,7 @@ public class BuiltinScalarFunctions implements FunctionHelper { scalar(MilliSecondTimestamp.class, "millisecond_timestamp"), scalar(MicroSecondTimestamp.class, "microsecond_timestamp"), scalar(RandomBytes.class, "random_bytes"), + scalar(SessionIsNarrowed.class, "session_is_narrowed"), scalar(SessionUser.class, "session_user"), scalar(Sha1.class, "sha1", "sha"), scalar(Sha2.class, "sha2"), diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index c04dc12afec942..e13c280685f783 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -29,6 +29,7 @@ import org.apache.doris.catalog.DatabaseIf; import org.apache.doris.catalog.Env; import org.apache.doris.catalog.InfoSchemaDb; +import org.apache.doris.catalog.MysqlDb; import org.apache.doris.catalog.TableIf; import org.apache.doris.cloud.datasource.CloudInternalCatalog; import org.apache.doris.cloud.proto.Cloud; @@ -313,16 +314,150 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { } ConnectContext ctx = ConnectContext.get(); if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity())) { + Set sessionRoleOverride = ctx.getSessionRoleOverride(); + if (sessionRoleOverride != null) { + // SU-narrowed session: the override REPLACES every role source (user grants, LDAP, + // authenticated roles). SuUserCommand enforced the ceiling at switch time. + Set narrowed = Sets.newHashSet(); + for (String roleName : sessionRoleOverride) { + Role role = roleManager.getRole(roleName); + if (role != null) { + narrowed.add(role); + } + } + // Preserve the implicit information_schema/mysql read access that every session's + // default role carries, so a narrowed session can still do basic metadata/client + // operations (BI drivers, SHOW, information_schema listings). These databases are + // self-filtered by the session's actual privileges — a narrowed session sees only + // rows for objects it can access — so keeping them widens NO data access while every + // privilege-bearing role (and the user's direct grants in the default role) stays + // dropped. + Role baseline = narrowingBaselineRole(); + if (baseline != null) { + narrowed.add(baseline); + } + return narrowed; + } for (String roleName : ctx.getAuthenticatedRoles()) { Role role = roleManager.getRole(roleName); if (role != null) { roles.add(role); } } + // Dormant (SU-only) roles never activate in a normal session for the session's OWN + // identity: excluded here so grants AND row policies stay inert until an SU switch + // explicitly requests them. Introspection of OTHER identities is unaffected (guard above). + roles.removeIf(role -> isSuOnlyRole(role.getRoleName())); } return roles; } + private static volatile String suOnlyPatternSource = null; + private static volatile java.util.regex.Pattern suOnlyPattern = null; + + /** Whether a role is dormant outside SU sessions (Config.su_only_roles_pattern; invalid/empty = never). */ + public static boolean isSuOnlyRole(String roleName) { + String src = Config.su_only_roles_pattern; + if (src == null || src.isEmpty()) { + return false; + } + java.util.regex.Pattern p = suOnlyPattern; + if (p == null || !src.equals(suOnlyPatternSource)) { + try { + p = java.util.regex.Pattern.compile(src); + } catch (Exception e) { + LOG.error("invalid su_only_roles_pattern '{}' — dormant-role gating DISABLED", src, e); + p = null; + } + suOnlyPattern = p; + suOnlyPatternSource = src; + } + return p != null && p.matcher(roleName).find(); + } + + /** + * RAW granted role names for an identity (user grants + LDAP), bypassing session shaping + * (SU override and dormant filtering). Used for the SU ceiling check and existence probing. + */ + public Set getGrantedRoleNamesRaw(UserIdentity userIdentity) { + Set names = Sets.newHashSet(userRoleManager.getRolesByUser(userIdentity)); + if (isLdapAuthEnabled()) { + Set ldapRoles = ldapManager.getUserRoles(userIdentity.getQualifiedUser()); + if (!CollectionUtils.isEmpty(ldapRoles)) { + for (Role r : ldapRoles) { + names.add(r.getRoleName()); + } + } + } + return names; + } + + // The implicit information_schema/mysql read grants that RoleManager.createDefaultRole gives + // every user's default role, isolated into a synthetic role so an SU-narrowed session keeps + // them (self-filtered by the session's real privileges) without re-including the default role's + // privilege-bearing direct grants. Built once. + private static volatile Role narrowingBaselineRoleCache = null; + + private static Role narrowingBaselineRole() { + Role cached = narrowingBaselineRoleCache; + if (cached != null) { + return cached; + } + try { + List tablePatterns = Lists.newArrayList(); + TablePattern info = new TablePattern(DEFAULT_CATALOG, InfoSchemaDb.DATABASE_NAME, "*"); + info.analyze(); + tablePatterns.add(info); + TablePattern mysql = new TablePattern(DEFAULT_CATALOG, MysqlDb.DATABASE_NAME, "*"); + mysql.analyze(); + tablePatterns.add(mysql); + WorkloadGroupPattern wg = new WorkloadGroupPattern(WorkloadGroupMgr.DEFAULT_GROUP_NAME); + wg.analyze(); + Role role = new Role("su_narrowing_baseline", tablePatterns, + PrivBitSet.of(Privilege.SELECT_PRIV), wg, PrivBitSet.of(Privilege.USAGE_PRIV)); + narrowingBaselineRoleCache = role; + return role; + } catch (Exception e) { + LOG.warn("failed to build SU narrowing baseline role (information_schema/mysql reads " + + "will not be preserved in narrowed sessions)", e); + return null; + } + } + + /** + * The role set consulted for WORKLOAD GROUP privilege checks. /** + * The role set consulted for WORKLOAD GROUP privilege checks. Identical to + * {@link #getRolesByUserWithLdap} for a normal session. In an SU-narrowed session the + * narrowed set is widened with the effective identity's own granted roles (its default + * role (where direct grants live) plus explicit and LDAP roles; dormant roles stay + * excluded unless requested), so the narrowed session may use exactly the workload groups + * the person's own session could use. A workload group is placement, not data authority: + * the narrowed session runs in the target's default_workload_group (the same resolution as + * the person's own session), and the person's USAGE on it is typically a direct grant that + * narrowing would otherwise drop. Only the workload-group predicate reads this set; + * table, database, resource and cloud checks stay on the narrowed set, so data access + * cannot widen through it. + */ + private Set getRolesForWorkloadGroupCheck(UserIdentity userIdentity) { + Set roles = getRolesByUserWithLdap(userIdentity); + ConnectContext ctx = ConnectContext.get(); + if (ctx == null || ctx.getSessionRoleOverride() == null + || !userIdentity.equals(ctx.getCurrentUserIdentity())) { + return roles; + } + Set widened = Sets.newHashSet(roles); + for (String roleName : getGrantedRoleNamesRaw(userIdentity)) { + if (isSuOnlyRole(roleName)) { + continue; + } + Role role = roleManager.getRole(roleName); + if (role != null) { + widened.add(role); + } + } + return widened; + } + public Set getRoleNamesByUserWithLdap(UserIdentity user, boolean showUserDefaultRole) { Set rolesByUserWithLdap = getRolesByUserWithLdap(user); Set res = Sets.newHashSetWithExpectedSize(rolesByUserWithLdap.size()); @@ -607,7 +742,7 @@ protected boolean checkWorkloadGroupPriv(UserIdentity currentUser, String worklo return true; } - Set roles = getRolesByUserWithLdap(currentUser); + Set roles = getRolesForWorkloadGroupCheck(currentUser); PrivBitSet savedPrivs = PrivBitSet.of(); for (Role role : roles) { if (role.checkWorkloadGroupPriv(workloadGroupName, wanted, savedPrivs)) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 1a3624a3527d77..f1080792b95632 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -907,6 +907,7 @@ import org.apache.doris.nereids.trees.plans.commands.ShowWhiteListCommand; import org.apache.doris.nereids.trees.plans.commands.ShowWorkloadGroupsCommand; import org.apache.doris.nereids.trees.plans.commands.StartTransactionCommand; +import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; import org.apache.doris.nereids.trees.plans.commands.SyncCommand; import org.apache.doris.nereids.trees.plans.commands.TransactionBeginCommand; import org.apache.doris.nereids.trees.plans.commands.TransactionCommitCommand; @@ -9276,6 +9277,16 @@ public LogicalPlan visitGrantRole(DorisParser.GrantRoleContext ctx) { return new GrantRoleCommand(userIdentity, roles); } + @Override + public LogicalPlan visitSuUser(DorisParser.SuUserContext ctx) { + UserIdentity userIdentity = visitUserIdentify(ctx.userIdentify()); + List roles = ctx.roles.stream() + .map(this::visitIdentifierOrText) + .collect(ImmutableList.toImmutableList()); + String workloadGroup = ctx.wg == null ? null : visitIdentifierOrText(ctx.wg); + return new SuUserCommand(userIdentity, roles, workloadGroup); + } + @Override public AccessPrivilegeWithCols visitPrivilege(DorisParser.PrivilegeContext ctx) { AccessPrivilegeWithCols accessPrivilegeWithCols; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java index 8455e178709313..158641b9f3c443 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java @@ -75,6 +75,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.NullIf; import org.apache.doris.nereids.trees.expressions.functions.scalar.Nvl; import org.apache.doris.nereids.trees.expressions.functions.scalar.Password; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.ShortCircuitIf; import org.apache.doris.nereids.trees.expressions.functions.scalar.User; @@ -182,6 +183,7 @@ public List> buildRules() { matches(Not.class, this::visitNot), matches(Database.class, this::visitDatabase), matches(CurrentUser.class, this::visitCurrentUser), + matches(SessionIsNarrowed.class, this::visitSessionIsNarrowed), matches(CurrentCatalog.class, this::visitCurrentCatalog), matches(User.class, this::visitUser), matches(ConnectionId.class, this::visitConnectionId), @@ -399,6 +401,12 @@ public Expression visitCurrentUser(CurrentUser currentUser, ExpressionRewriteCon return new VarcharLiteral(res); } + @Override + public Expression visitSessionIsNarrowed(SessionIsNarrowed fn, ExpressionRewriteContext context) { + ConnectContext cctx = context.cascadesContext.getConnectContext(); + return BooleanLiteral.of(cctx != null && cctx.getSessionRoleOverride() != null); + } + @Override public Expression visitCurrentCatalog(CurrentCatalog currentCatalog, ExpressionRewriteContext context) { if (!foldContextDependentExpressions) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SessionIsNarrowed.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SessionIsNarrowed.java new file mode 100644 index 00000000000000..3dc23039650344 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SessionIsNarrowed.java @@ -0,0 +1,74 @@ +// 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.nereids.trees.expressions.functions.scalar; + +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable; +import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.shape.LeafExpression; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.types.BooleanType; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** + * session_is_narrowed(): TRUE iff the current session is an SU-narrowed session + * (see SuUserCommand). FE-constant-folded at plan time from ConnectContext, like + * current_user(); intended for row-policy predicates and diagnostics. + */ +public class SessionIsNarrowed extends ScalarFunction + implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable { + + public static final List SIGNATURES = ImmutableList.of( + FunctionSignature.ret(BooleanType.INSTANCE).args() + ); + + public SessionIsNarrowed() { + super("session_is_narrowed", ImmutableList.of()); + } + + /** constructor for withChildren and reuse signature */ + private SessionIsNarrowed(ScalarFunctionParams functionParams) { + super(functionParams); + } + + @Override + public List getSignatures() { + return SIGNATURES; + } + + @Override + public Expression withChildren(List children) { + Preconditions.checkArgument(children.isEmpty()); + return new SessionIsNarrowed(getFunctionParams(children)); + } + + @Override + public R accept(ExpressionVisitor visitor, C context) { + return visitor.visitSessionIsNarrowed(this, context); + } + + @Override + public boolean isDeterministic() { + return false; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java index b5120939365edf..d5767c0df490ab 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java @@ -488,6 +488,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsDiff; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsSub; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha1; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha2; @@ -1182,6 +1183,10 @@ default R visitCurrentUser(CurrentUser currentUser, C context) { return visitScalarFunction(currentUser, context); } + default R visitSessionIsNarrowed(SessionIsNarrowed sessionIsNarrowed, C context) { + return visitScalarFunction(sessionIsNarrowed, context); + } + default R visitCutIpv6(CutIpv6 cutIpv6, C context) { return visitScalarFunction(cutIpv6, context); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java index 67a602471bbb7a..ef39efb23e1ab0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java @@ -461,6 +461,7 @@ public enum PlanType { SHOW_WORKLOAD_GROUP_COMMAND, TRUNCATE_TABLE_COMMAND, GRANT_ROLE_COMMAND, + SU_USER_COMMAND, GRANT_RESOURCE_PRIVILEGE_COMMAND, GRANT_TABLE_PRIVILEGE_COMMAND, REVOKE_TABLE_PRIVILEGE_COMMAND, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java new file mode 100644 index 00000000000000..bfe8f8fea15dd9 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java @@ -0,0 +1,169 @@ +// 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.nereids.trees.plans.commands; + +import org.apache.doris.analysis.StmtType; +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.Config; +import org.apache.doris.common.FeNameFormat; +import org.apache.doris.mysql.privilege.Auth; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.StmtExecutor; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableSet; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; +import java.util.Objects; +import java.util.Set; + +/** + * SU 'user'[@'host'] WITH ROLES ('role1'[, ...]) [WORKLOAD GROUP 'wg'] + * + *

Session-narrowed identity switch: the session becomes the TARGET identity (audit, + * current_user(), user-bound row policies, user properties) with a role set that REPLACES + * every role source — enforced at the single choke point {@code Auth.getRolesByUserWithLdap}. + * Session-only state; nothing is persisted. + * + *

Fail-closed contract: + *

    + *
  • Allowed only for ADMIN or accounts listed in {@code Config.switch_user_users}; a session + * that skips SU keeps the switching account's own (minimal) grants.
  • + *
  • NARROWING-ONLY LAW: every requested role must already be GRANTED to the ceiling identity — + * the target user ({@code switch_user_role_ceiling=target}, default; the session can never + * exceed the person's real authority) or the switching account ({@code =switcher}).
  • + *
  • An SU'd session can never SU again, and no SET ROLE exists to widen it. Connection reset + * reverts to the authenticated identity ({@link ConnectContext#revertSessionNarrowing}).
  • + *
  • Roles matching {@code Config.su_only_roles_pattern} are dormant in normal sessions and + * activate only through this command (see {@code Auth.isSuOnlyRole}).
  • + *
+ */ +public class SuUserCommand extends Command implements NoForward { + private static final Logger LOG = LogManager.getLogger(SuUserCommand.class); + + private final UserIdentity userIdentity; + private final List roles; + private final String workloadGroup; // nullable + + public SuUserCommand(UserIdentity userIdentity, List roles, String workloadGroup) { + super(PlanType.SU_USER_COMMAND); + Preconditions.checkArgument(!roles.isEmpty(), "SU requires at least one role"); + this.userIdentity = Objects.requireNonNull(userIdentity, "userIdentity is null"); + this.roles = Objects.requireNonNull(roles, "roles is null"); + this.workloadGroup = workloadGroup; + } + + @Override + public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { + // Re-SU refusal: a narrowed session must never widen or re-target itself. + if (ctx.getAuthenticatedIdentity() != null) { + throw new AnalysisException("SU is not allowed in an already-switched session"); + } + // Gate: ADMIN, or explicitly listed switcher accounts (per-FE config). + boolean isAdmin = Env.getCurrentEnv().getAccessManager() + .checkGlobalPriv(ctx, PrivPredicate.ADMIN); + if (!isAdmin && !isListedSwitcher(ctx.getQualifiedUser())) { + throw new AnalysisException("Access denied: SU requires ADMIN or membership in " + + "the switch_user_users FE config"); + } + + userIdentity.analyze(); + for (String role : roles) { + FeNameFormat.checkRoleName(role, true /* can be admin */, "Invalid role in SU"); + } + + Auth auth = Env.getCurrentEnv().getAuth(); + Set targetRoles = auth.getGrantedRoleNamesRaw(userIdentity); + if (targetRoles.isEmpty()) { + // every existing user holds at least its default role + throw new AnalysisException("SU target user does not exist: " + userIdentity); + } + // NARROWING-ONLY LAW: requested roles must be a subset of the ceiling identity's + // RAW granted roles (dormant roles count as granted — SU is their only activation). + boolean switcherCeiling = "switcher".equalsIgnoreCase(Config.switch_user_role_ceiling); + Set ceiling = switcherCeiling + ? auth.getGrantedRoleNamesRaw(ctx.getCurrentUserIdentity()) + : targetRoles; + for (String role : roles) { + if (!ceiling.contains(role)) { + throw new AnalysisException("SU role '" + role + "' is not granted to the " + + (switcherCeiling ? "switching account" : "target user") + + " — SU can only narrow, never mint authority"); + } + } + + UserIdentity authenticated = ctx.getCurrentUserIdentity(); + ctx.setAuthenticatedIdentity(authenticated); + ctx.setCurrentUserIdentity(userIdentity); + ctx.setSessionRoleOverride(ImmutableSet.copyOf(roles)); + if (!Strings.isNullOrEmpty(workloadGroup)) { + // Placement only. USAGE on the session's workload group (this clause, or the + // target's default_workload_group when omitted) is checked per query against the + // narrowed set widened with the target's own granted roles + // (Auth.getRolesForWorkloadGroupCheck): the person's lane follows the person. + ctx.getSessionVariable().setWorkloadGroup(workloadGroup); + } + // The SU linkage event: authenticated + effective identity, roles, connection — the + // statement itself also lands in the audit log under the effective user. + LOG.info("SU: connection {} narrowed. authenticated={} effective={} roles={} workloadGroup={}", + ctx.getConnectionId(), authenticated, userIdentity, roles, + workloadGroup == null ? "" : workloadGroup); + } + + private static boolean isListedSwitcher(String qualifiedUser) { + if (Strings.isNullOrEmpty(Config.switch_user_users) || Strings.isNullOrEmpty(qualifiedUser)) { + return false; + } + for (String allowed : Config.switch_user_users.split(",")) { + if (qualifiedUser.equals(allowed.trim())) { + return true; + } + } + return false; + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitCommand(this, context); + } + + @Override + public StmtType stmtType() { + return StmtType.OTHER; + } + + public UserIdentity getUserIdentity() { + return userIdentity; + } + + public List getRoles() { + return roles; + } + + public String getWorkloadGroup() { + return workloadGroup; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java index 8d93e63de81941..b77a7627cbd81a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java @@ -187,6 +187,14 @@ public String protocolName() { protected volatile Principal authenticatedPrincipal; // Roles granted during authentication and bound to the current session. protected volatile Set authenticatedRoles = Collections.emptySet(); + + // SU (session-narrowed identity switch, session-only state — never persisted): + // authenticatedIdentity = the identity that AUTHENTICATED this connection; non-null iff the + // session has been switched (also the re-SU refusal marker). sessionRoleOverride = the role + // set that REPLACES every role source for privilege checks and row-policy matching, consulted + // in Auth.getRolesByUserWithLdap under the same current-identity guard as authenticatedRoles. + protected volatile UserIdentity authenticatedIdentity; + protected volatile Set sessionRoleOverride; // Variables belong to this session. protected volatile SessionVariable sessionVariable; // Store user variable in this connection @@ -378,6 +386,41 @@ public void resetConnection() throws UserException { insertResult = null; command = MysqlCommand.COM_SLEEP; returnRows = 0; + // A narrowed session reverts to the AUTHENTICATED identity on connection reset — + // dropping only the override would widen the session to the target's full roles. + revertSessionNarrowing(); + } + + /** Revert an SU switch: back to the authenticated identity, override cleared. No-op if not switched. */ + public void revertSessionNarrowing() { + UserIdentity authed = this.authenticatedIdentity; + if (authed != null) { + this.currentUserIdentity = authed; + this.authenticatedIdentity = null; + this.sessionRoleOverride = null; + } + } + + /** Drop SU state without restoring identity — only for paths that fully re-authenticate. */ + public void clearSessionNarrowing() { + this.authenticatedIdentity = null; + this.sessionRoleOverride = null; + } + + public UserIdentity getAuthenticatedIdentity() { + return authenticatedIdentity; + } + + public void setAuthenticatedIdentity(UserIdentity authenticatedIdentity) { + this.authenticatedIdentity = authenticatedIdentity; + } + + public Set getSessionRoleOverride() { + return sessionRoleOverride; + } + + public void setSessionRoleOverride(Set roles) { + this.sessionRoleOverride = roles == null ? null : Collections.unmodifiableSet(new HashSet<>(roles)); } private void resetSessionVariable() { diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java index c5a0931df1e213..6948722a04986a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java @@ -479,6 +479,9 @@ protected void handleFieldList(String tableName) throws ConnectionException { } private void handleChangeUser() throws IOException { + // A full re-authentication replaces the session identity — drop any SU narrowing first + // (never revert: the incoming credentials decide the new identity). + getConnectContext().clearSessionNarrowing(); // Random bytes generated when creating connection. byte[] authPluginData = getConnectContext().getAuthPluginData(); Preconditions.checkNotNull(authPluginData, "Auth plugin data is null."); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index 054b8904799e07..cf7dc54c34e92c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -1525,6 +1525,17 @@ public boolean isProfileSafeStmt() { } private void forwardToMaster() throws Exception { + // FAIL CLOSED for session-narrowed (SU) sessions: the narrowing (sessionRoleOverride) + // is local ConnectContext state and is NOT carried in the forward RPC, so the master + // would rebuild the session with the target identity but NO override and authorize the + // statement against the target's FULL role set. A narrowed serving session's reads run + // locally on this FE; a statement that must be forwarded (SHOW-class metadata forwarded + // to master, DDL/DML/GRANT) is refused rather than run un-narrowed on the master. + if (context.getSessionRoleOverride() != null) { + throw new UserException("This statement would be forwarded to the master FE, which is not " + + "allowed in a session-narrowed (SU) session, because the narrowing does not " + + "propagate to the master. Run it in a non-narrowed session."); + } masterOpExecutor = new MasterOpExecutor(originStmt, context, redirectStatus, isQuery()); if (LOG.isDebugEnabled()) { LOG.debug("need to transfer to Master. stmt: {}", context.getStmtId()); diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java new file mode 100644 index 00000000000000..70038c44813c0b --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -0,0 +1,336 @@ +// 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.mysql.privilege; + +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.Config; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand; +import org.apache.doris.nereids.trees.plans.commands.GrantResourcePrivilegeCommand; +import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.utframe.TestWithFeService; + +import com.google.common.collect.Sets; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.Set; + +/** + * SU (session-narrowed identity switch) acceptance tests: + * (a) a narrowed session loses the target's personal grants (override REPLACES the role union); + * (c) re-SU is refused; (d) a session that skips SU keeps only the switcher's own grants; + * (f) narrowing never leaks into checks of OTHER identities; (g) the narrowed session keeps the + * person's own WORKLOAD GROUP usage (placement follows the person; never wider than her own + * session); plus the dormant (SU-only) role gating, both ceiling modes, the config gate, and + * the parser round trip. + * End-to-end row-policy + audit coverage runs on the scratch rig (gate battery), not here. + */ +public class SuUserNarrowingTest extends TestWithFeService { + + private static final String CTL = InternalCatalog.INTERNAL_CATALOG_NAME; + + @Override + protected void runBeforeAll() throws Exception { + // GRANT validates database existence on the internal catalog + createDatabase("test"); + createDatabase("perso"); + createDatabase("perso2"); + } + + private UserIdentity ident(String name) { + return UserIdentity.createAnalyzedUserIdentWithIp(name, "%"); + } + + private boolean canSelectDb(UserIdentity user, String db) { + return Env.getCurrentEnv().getAuth().checkDbPriv(user, CTL, db, PrivPredicate.SELECT); + } + + private boolean canSelectInfoSchema(UserIdentity user) { + return Env.getCurrentEnv().getAuth().checkDbPriv(user, CTL, + org.apache.doris.catalog.InfoSchemaDb.DATABASE_NAME, PrivPredicate.SELECT); + } + + private boolean canUseWorkloadGroup(UserIdentity user, String wg) { + return Env.getCurrentEnv().getAuth().checkWorkloadGroupPriv(user, wg, PrivPredicate.USAGE); + } + + private void createWorkloadGroup(String name) throws Exception { + LogicalPlan plan = new NereidsParser().parseSingle("CREATE WORKLOAD GROUP IF NOT EXISTS " + name + + " PROPERTIES ('min_memory_percent'='10', 'max_memory_percent'='30%')"); + Assert.assertTrue(plan instanceof CreateWorkloadGroupCommand); + ((CreateWorkloadGroupCommand) plan).run(connectContext, null); + } + + private void grantWorkloadGroupUsage(String sql) throws Exception { + LogicalPlan plan = new NereidsParser().parseSingle(sql); + Assert.assertTrue(plan instanceof GrantResourcePrivilegeCommand); + ((GrantResourcePrivilegeCommand) plan).run(connectContext, null); + } + + @Test + public void testOverrideReplacesRoleUnionAndDoesNotLeak() throws Exception { + addUser("alice", true); + addUser("bystander", true); + createRole("space_a"); + createRole("bystander_r"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'space_a';"); + grantRole("GRANT 'space_a' TO 'alice'@'%'"); + grantRole("GRANT 'bystander_r' TO 'bystander'@'%'"); + // personal (default-role) grant on another db + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'alice'@'%';"); + + UserIdentity alice = ident("alice"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(alice); + ctx.setThreadLocalInfo(); + try { + // un-narrowed: personal + role grants both live + Assert.assertTrue(canSelectDb(alice, "perso")); + Assert.assertTrue(canSelectDb(alice, "test")); + + ctx.setSessionRoleOverride(Collections.singleton("space_a")); + // (a) the override REPLACES everything: the personal grant is gone... + Assert.assertFalse(canSelectDb(alice, "perso")); + // ...while the requested role's grants remain + Assert.assertTrue(canSelectDb(alice, "test")); + + // (f) checks against OTHER identities are untouched by this session's narrowing + Set bystanderRoles = Env.getCurrentEnv().getAuth() + .getRoleNamesByUserWithLdap(ident("bystander"), false); + Assert.assertTrue(bystanderRoles.contains("bystander_r")); + + // revert restores the full union + ctx.setSessionRoleOverride(null); + Assert.assertTrue(canSelectDb(alice, "perso")); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testDormantRolesInertUntilRequested() throws Exception { + addUser("dorm_user", true); + createRole("space_dorm"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'space_dorm';"); + grantRole("GRANT 'space_dorm' TO 'dorm_user'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.perso2.* TO 'dorm_user'@'%';"); + + UserIdentity user = ident("dorm_user"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(user); + ctx.setThreadLocalInfo(); + String savedPattern = Config.su_only_roles_pattern; + try { + Config.su_only_roles_pattern = "^space_"; + // dormant in a normal session: the role's grants are inert, personal grants live + Assert.assertFalse(canSelectDb(user, "test")); + Assert.assertTrue(canSelectDb(user, "perso2")); + // the RAW view still carries it (the SU ceiling and admin introspection see it) + Assert.assertTrue(Env.getCurrentEnv().getAuth() + .getGrantedRoleNamesRaw(user).contains("space_dorm")); + // explicit activation via the override path + ctx.setSessionRoleOverride(Collections.singleton("space_dorm")); + Assert.assertTrue(canSelectDb(user, "test")); + // invalid pattern degrades to disabled (logged), never to an exception + ctx.setSessionRoleOverride(null); + Config.su_only_roles_pattern = "([bad"; + Assert.assertTrue(canSelectDb(user, "test")); + } finally { + Config.su_only_roles_pattern = savedPattern; + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testSuCommandContract() throws Exception { + addUser("svc", true); + addUser("bobby", true); + createRole("r_bobby"); + createRole("r_ungranted"); + createRole("r_svc_only"); + grantRole("GRANT 'r_bobby' TO 'bobby'@'%'"); + grantRole("GRANT 'r_svc_only' TO 'svc'@'%'"); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("svc")); + ctx.setThreadLocalInfo(); + String savedUsers = Config.switch_user_users; + String savedCeiling = Config.switch_user_role_ceiling; + try { + // gate: neither ADMIN nor listed -> refused; (d) the session stays the service account + Assert.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_bobby"), null).run(ctx, null)); + Assert.assertNull(ctx.getAuthenticatedIdentity()); + Assert.assertFalse(canSelectDb(ident("svc"), "test")); + + Config.switch_user_users = ctx.getQualifiedUser(); + + // nonexistent target refused + Assert.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("ghost", "%"), + Collections.singletonList("r_bobby"), null).run(ctx, null)); + + // NARROWING-ONLY LAW (default ceiling = target): role exists but is not bobby's + Assert.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_ungranted"), null).run(ctx, null)); + + // legal switch + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_bobby"), "wg_probe").run(ctx, null); + Assert.assertEquals("bobby", ctx.getCurrentUserIdentity().getQualifiedUser()); + Assert.assertEquals(Collections.singleton("r_bobby"), ctx.getSessionRoleOverride()); + Assert.assertEquals("svc", ctx.getAuthenticatedIdentity().getQualifiedUser()); + Assert.assertEquals("wg_probe", ctx.getSessionVariable().getWorkloadGroup()); + + // (c) re-SU refused in a switched session + Assert.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("svc", "%"), + Collections.singletonList("r_svc_only"), null).run(ctx, null)); + + // reset reverts to the AUTHENTICATED identity, never widens the target + ctx.revertSessionNarrowing(); + Assert.assertEquals("svc", ctx.getCurrentUserIdentity().getQualifiedUser()); + Assert.assertNull(ctx.getSessionRoleOverride()); + + // switcher ceiling: a role granted to the SERVICE, not the target + Config.switch_user_role_ceiling = "switcher"; + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_svc_only"), null).run(ctx, null); + Assert.assertEquals(Collections.singleton("r_svc_only"), ctx.getSessionRoleOverride()); + } finally { + Config.switch_user_users = savedUsers; + Config.switch_user_role_ceiling = savedCeiling; + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testNarrowedSessionKeepsPersonsWorkloadGroupUsage() throws Exception { + addUser("wg_person", true); + createRole("space_wg"); + createRole("space_wg_lane"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'space_wg';"); + grantRole("GRANT 'space_wg','space_wg_lane' TO 'wg_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'wg_person'@'%';"); + createWorkloadGroup("wg_person_lane"); + createWorkloadGroup("wg_via_role"); + createWorkloadGroup("wg_nobody"); + // the login shape: USAGE granted DIRECTLY to the account (its default role), not via a role + grantWorkloadGroupUsage("GRANT USAGE_PRIV ON WORKLOAD GROUP 'wg_person_lane' TO 'wg_person'@'%';"); + // a group reachable only through a role the person holds + grantWorkloadGroupUsage("GRANT USAGE_PRIV ON WORKLOAD GROUP 'wg_via_role' TO ROLE 'space_wg_lane';"); + + UserIdentity person = ident("wg_person"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(person); + ctx.setThreadLocalInfo(); + String savedPattern = Config.su_only_roles_pattern; + try { + // the person's own session + Assert.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); + Assert.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); + Assert.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); + + ctx.setSessionRoleOverride(Collections.singleton("space_wg")); + // data narrowing is intact: personal grant gone, requested role live + Assert.assertFalse(canSelectDb(person, "perso")); + Assert.assertTrue(canSelectDb(person, "test")); + // (g) the direct USAGE survives narrowing — the person's lane follows the person + Assert.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); + // ...and so does role-carried USAGE, even though that role was not requested + Assert.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); + // never wider than the person's own session + Assert.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); + + // a DORMANT role's USAGE stays inert unless the switch requested it + Config.su_only_roles_pattern = "^space_wg_lane$"; + Assert.assertFalse(canUseWorkloadGroup(person, "wg_via_role")); + Assert.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); + ctx.setSessionRoleOverride(Sets.newHashSet("space_wg", "space_wg_lane")); + Assert.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); + + // the widening is scoped to the session's OWN identity: another identity's check is untouched + ctx.setSessionRoleOverride(Collections.singleton("space_wg")); + Assert.assertFalse(canUseWorkloadGroup(ident("bystander_wg"), "wg_person_lane")); + } finally { + Config.su_only_roles_pattern = savedPattern; + ctx.setSessionRoleOverride(null); + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testNarrowedSessionPreservesInformationSchemaButDropsDirectGrants() throws Exception { + addUser("iuser", true); + createRole("ispace"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ispace';"); + grantRole("GRANT 'ispace' TO 'iuser'@'%'"); + // a personal (default-role) grant on another db + the implicit information_schema read + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'iuser'@'%';"); + + UserIdentity iuser = ident("iuser"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(iuser); + ctx.setThreadLocalInfo(); + try { + // un-narrowed: information_schema readable (default role), personal + role grants live + Assert.assertTrue(canSelectInfoSchema(iuser)); + Assert.assertTrue(canSelectDb(iuser, "perso")); + + ctx.setSessionRoleOverride(Collections.singleton("ispace")); + // narrowed: the requested role's grant stays, the DIRECT/default-role grant is dropped... + Assert.assertTrue(canSelectDb(iuser, "test")); + Assert.assertFalse(canSelectDb(iuser, "perso")); + // ...but the implicit information_schema read IS PRESERVED (the narrowing baseline role), + // so a narrowed session can still do metadata/client operations. + Assert.assertTrue(canSelectInfoSchema(iuser)); + + ctx.setSessionRoleOverride(null); + Assert.assertTrue(canSelectDb(iuser, "perso")); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testParserRoundTrip() { + Object plan = new NereidsParser().parseSingle( + "SU 'alice'@'%' WITH ROLES ('space_a', 'r2') WORKLOAD GROUP 'wg_space_5'"); + Assert.assertTrue(plan instanceof SuUserCommand); + SuUserCommand cmd = (SuUserCommand) plan; + Assert.assertEquals(2, cmd.getRoles().size()); + Assert.assertEquals("wg_space_5", cmd.getWorkloadGroup()); + + Object plain = new NereidsParser().parseSingle("SU 'alice' WITH ROLES ('space_a')"); + Assert.assertTrue(plain instanceof SuUserCommand); + Assert.assertNull(((SuUserCommand) plain).getWorkloadGroup()); + + // `su` stays usable as an identifier (nonReserved) + Object select = new NereidsParser().parseSingle("SELECT su FROM t1"); + Assert.assertNotNull(select); + } +} diff --git a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 index f23c812d449657..309e9135568c5d 100644 --- a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 +++ b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 @@ -556,6 +556,7 @@ STREAMS: 'STREAMS'; STREAMING: 'STREAMING'; STRING: 'STRING'; STRUCT: 'STRUCT'; +SU: 'SU'; SUBJECT: 'SUBJECT'; SUBSTR: 'SUBSTR'; SUBSTRING: 'SUBSTRING'; diff --git a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index 33d0115ebfcdb8..5727554409b429 100644 --- a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -952,6 +952,9 @@ grantRevokeStatement identifierOrTextOrAsterisk FROM (userIdentify | ROLE identifierOrText) #revokeResourcePrivilege | REVOKE privilegeList ON multipartIdentifierOrAsterisk FROM (userIdentify | ROLE identifierOrText) #revokeTablePrivilege + | SU userIdentify WITH ROLES LEFT_PAREN + roles+=identifierOrText (COMMA roles+=identifierOrText)* RIGHT_PAREN + (WORKLOAD GROUP wg=identifierOrText)? #suUser ; privilege @@ -2619,6 +2622,7 @@ nonReserved | STREAMING | STRING | STRUCT + | SU | SUBJECT | SUBSTR | SUBSTRING From 2e2d50c13aaf0ceef9723c4cb6fe7e8b4ed2ac70 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 8 Sep 2026 16:39:33 +0530 Subject: [PATCH 02/30] [fix](auth) SU: carry the session's narrowed role set across every FE/BE boundary A session narrowed by `SU ... WITH ROLES (...)` is narrowed by ConnectContext state that Auth.getRolesByUserWithLdap consults. Two kinds of work leave that context behind and were authorized against the target's FULL role union: 1. BE->FE metadata RPCs. information_schema and mysql tables, the metadata table functions and processlist are served by BE scanners that call back into an FE handler thread carrying only current_user_ident, so a narrowed session saw off-scope database, table, column and view NAMES, the results of every per-table / per-database / catalog / ADMIN check in MetadataGenerator, and every session in processlist (data access itself was already scoped by table privilege and row policy at execution time). 2. Statements forwarded to the master FE, which rebuilds the session from TMasterOpRequest; the origin refused to forward at all. Carry the narrowing instead, on the thrift ids already reserved for exactly this: current_roles on TGetDbsParams / TGetTablesParams / TDescribeTablesParams / TSchemaTableRequestParams / TMetadataTableRequestParams / TShowProcessListRequest, current_roles + is_su_user on TMasterOpRequest, and current_roles on the schema and metadata scan plan nodes (SchemaScanNode / MetadataScanNode stamp it; the BE lifts it into the scanner parameter and every scanner that identifies the caller forwards it). On the FE, one helper (FrontendServiceImpl.withSessionRoleOverride) runs a handler under a thread-local narrowing keyed by the caller identity, consulted by Auth.getRolesByUserWithLdap and cleared in a finally because handler threads are pooled; getDbNames / getTableNames / listTableStatus / describeTables / fetchSchemaTableData / showProcessList delegate to their unchanged bodies through it. A narrowed request that names no caller is refused rather than run against the full union. Forward-to-master: the origin sets is_su_user + current_roles from the session, and ConnectProcessor.applyForwardedSessionNarrowing installs them on the proxy session before executing (no role list = empty set, fail closed). An older master silently ignores both fields and a rolling upgrade upgrades the master last, so the origin still refuses to forward unless the master reports the same build in its heartbeat (Env.masterRunsSameBuild; Frontend.localBuildVersion is the one definition of that string). SU itself stays NoForward. Tests: SuUserNarrowingTest (the no-ConnectContext handler path), ForwardedSessionNarrowingTest (request carries the narrowing only when switched; the master installs it and drops personal grants; no roles = empty; no flag = unnarrowed), NarrowedMetadataRpcTest (DATABASE_PROPERTIES through the real handler: personal grant gone, role grant kept, nothing sticks to the thread, no identity = refused). Co-Authored-By: Claude Fable 5.1 Signed-off-by: Raghvendra Singh --- be/src/exec/operator/meta_scan_operator.cpp | 5 +- be/src/exec/operator/meta_scan_operator.h | 3 + be/src/exec/operator/schema_scan_operator.cpp | 6 + be/src/exec/scan/meta_scanner.cpp | 23 ++- be/src/exec/scan/meta_scanner.h | 8 +- ...ma_authentication_integrations_scanner.cpp | 3 + ...chema_catalog_meta_cache_stats_scanner.cpp | 3 + .../schema_columns_scanner.cpp | 9 ++ .../schema_extensions_scanner.cpp | 3 + .../schema_files_scanner.cpp | 3 + .../schema_metadata_name_ids_scanner.cpp | 6 + .../schema_per_db_scanner.cpp | 6 + .../schema_processlist_scanner.cpp | 3 + .../schema_profiling_scanner.cpp | 3 + .../schema_role_mappings_scanner.cpp | 3 + be/src/information_schema/schema_scanner.h | 3 + .../schema_schema_privileges_scanner.cpp | 3 + .../schema_schemata_scanner.cpp | 3 + .../schema_sql_block_rule_status_scanner.cpp | 3 + .../schema_table_privileges_scanner.cpp | 3 + .../schema_tables_scanner.cpp | 6 + .../schema_user_privileges_scanner.cpp | 3 + .../schema_view_dependency_scanner.cpp | 3 + .../schema_views_scanner.cpp | 6 + .../schema_workload_group_privileges.cpp | 3 + .../schema_workload_groups_scanner.cpp | 3 + .../schema_workload_sched_policy_scanner.cpp | 3 + .../java/org/apache/doris/catalog/Env.java | 21 +++ .../tvf/source/MetadataScanNode.java | 8 + .../apache/doris/mysql/privilege/Auth.java | 64 +++++--- .../apache/doris/planner/SchemaScanNode.java | 9 ++ .../org/apache/doris/qe/ConnectProcessor.java | 19 +++ .../org/apache/doris/qe/FEOpExecutor.java | 8 + .../org/apache/doris/qe/StmtExecutor.java | 20 +-- .../doris/service/FrontendServiceImpl.java | 79 +++++++++ .../org/apache/doris/system/Frontend.java | 6 + .../org/apache/doris/system/HeartbeatMgr.java | 3 +- .../mysql/privilege/SuUserNarrowingTest.java | 31 ++++ .../qe/ForwardedSessionNarrowingTest.java | 152 ++++++++++++++++++ .../service/NarrowedMetadataRpcTest.java | 101 ++++++++++++ gensrc/thrift/FrontendService.thrift | 52 +++--- gensrc/thrift/PlanNodes.thrift | 6 + 42 files changed, 650 insertions(+), 58 deletions(-) create mode 100644 fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java create mode 100644 fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java diff --git a/be/src/exec/operator/meta_scan_operator.cpp b/be/src/exec/operator/meta_scan_operator.cpp index c94c7e213de8eb..f394cbf55d2bea 100644 --- a/be/src/exec/operator/meta_scan_operator.cpp +++ b/be/src/exec/operator/meta_scan_operator.cpp @@ -30,7 +30,7 @@ Status MetaScanLocalState::_init_scanners(std::list* scanners) { for (auto& scan_range : _scan_ranges) { std::shared_ptr scanner = MetaScanner::create_shared(state(), this, p._tuple_id, scan_range, p._limit, - custom_profile(), p._user_identity); + custom_profile(), p._user_identity, p._current_roles); RETURN_IF_ERROR(scanner->init(state(), _conjuncts)); scanners->push_back(scanner); } @@ -55,6 +55,9 @@ MetaScanOperatorX::MetaScanOperatorX(ObjectPool* pool, const TPlanNode& tnode, i if (tnode.meta_scan_node.__isset.current_user_ident) { _user_identity = tnode.meta_scan_node.current_user_ident; } + if (tnode.meta_scan_node.__isset.current_roles) { + _current_roles = tnode.meta_scan_node.current_roles; + } } } // namespace doris diff --git a/be/src/exec/operator/meta_scan_operator.h b/be/src/exec/operator/meta_scan_operator.h index e14e9282f4f273..e7e8d1d731afc7 100644 --- a/be/src/exec/operator/meta_scan_operator.h +++ b/be/src/exec/operator/meta_scan_operator.h @@ -19,6 +19,7 @@ #include +#include #include #include "common/status.h" @@ -61,6 +62,8 @@ class MetaScanOperatorX final : public ScanOperatorX { TupleId _tuple_id; TUserIdentity _user_identity; + // SU narrowing: the session's active role subset (TMetaScanNode.current_roles). Empty = none. + std::set _current_roles; }; /// Instantiated once in scan_operator.cpp; suppresses per-TU implicit instantiation. diff --git a/be/src/exec/operator/schema_scan_operator.cpp b/be/src/exec/operator/schema_scan_operator.cpp index aa9add4beeba3c..cd97c9d77b1e7b 100644 --- a/be/src/exec/operator/schema_scan_operator.cpp +++ b/be/src/exec/operator/schema_scan_operator.cpp @@ -113,6 +113,12 @@ Status SchemaScanOperatorX::init(const TPlanNode& tnode, RuntimeState* state) { } } + // SU narrowing: carry the SU-narrowed role subset from the FE plan to the scanners so the + // BE->FE metadata RPCs narrow name visibility (empty/unset = no narrowing, prior behavior). + if (tnode.schema_scan_node.__isset.current_roles) { + _common_scanner_param->current_roles = tnode.schema_scan_node.current_roles; + } + if (tnode.schema_scan_node.__isset.ip) { _common_scanner_param->ip = state->obj_pool()->add(new std::string(tnode.schema_scan_node.ip)); diff --git a/be/src/exec/scan/meta_scanner.cpp b/be/src/exec/scan/meta_scanner.cpp index 282713257d7758..49659e7545250b 100644 --- a/be/src/exec/scan/meta_scanner.cpp +++ b/be/src/exec/scan/meta_scanner.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "common/cast_set.h" #include "common/logging.h" @@ -55,13 +56,21 @@ namespace doris { MetaScanner::MetaScanner(RuntimeState* state, ScanLocalStateBase* local_state, TupleId tuple_id, const TScanRangeParams& scan_range, int64_t limit, RuntimeProfile* profile, - TUserIdentity user_identity) + TUserIdentity user_identity, std::set current_roles) : Scanner(state, local_state, limit, profile), _meta_eos(false), _tuple_id(tuple_id), _user_identity(user_identity), + _current_roles(std::move(current_roles)), _scan_range(scan_range.scan_range) {} +void MetaScanner::_set_session_identity(TMetadataTableRequestParams* params) const { + params->__set_current_user_ident(_user_identity); + if (!_current_roles.empty()) { + params->__set_current_roles(_current_roles); + } +} + Status MetaScanner::_open_impl(RuntimeState* state) { VLOG_CRITICAL << "MetaScanner::open"; RETURN_IF_ERROR(Scanner::_open_impl(state)); @@ -313,6 +322,7 @@ Status MetaScanner::_build_backends_metadata_request(const TMetaScanRange& meta_ // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::BACKENDS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_backends_metadata_params(meta_scan_range.backends_params); request->__set_metada_table_params(metadata_table_params); @@ -332,6 +342,7 @@ Status MetaScanner::_build_frontends_metadata_request(const TMetaScanRange& meta // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::FRONTENDS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_frontends_metadata_params(meta_scan_range.frontends_params); request->__set_metada_table_params(metadata_table_params); @@ -351,6 +362,7 @@ Status MetaScanner::_build_frontends_disks_metadata_request(const TMetaScanRange // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::FRONTENDS_DISKS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_frontends_metadata_params(meta_scan_range.frontends_params); request->__set_metada_table_params(metadata_table_params); @@ -368,7 +380,7 @@ Status MetaScanner::_build_workload_sched_policy_metadata_request( // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::WORKLOAD_SCHED_POLICY); - metadata_table_params.__set_current_user_ident(_user_identity); + _set_session_identity(&metadata_table_params); request->__set_metada_table_params(metadata_table_params); return Status::OK(); @@ -384,7 +396,7 @@ Status MetaScanner::_build_catalogs_metadata_request(const TMetaScanRange& meta_ // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::CATALOGS); - metadata_table_params.__set_current_user_ident(_user_identity); + _set_session_identity(&metadata_table_params); request->__set_metada_table_params(metadata_table_params); return Status::OK(); @@ -404,6 +416,7 @@ Status MetaScanner::_build_materialized_views_metadata_request( // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::MATERIALIZED_VIEWS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_materialized_views_metadata_params( meta_scan_range.materialized_views_params); @@ -425,6 +438,7 @@ Status MetaScanner::_build_partitions_metadata_request(const TMetaScanRange& met // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::PARTITIONS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_partitions_metadata_params(meta_scan_range.partitions_params); request->__set_metada_table_params(metadata_table_params); @@ -444,6 +458,7 @@ Status MetaScanner::_build_jobs_metadata_request(const TMetaScanRange& meta_scan // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::JOBS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_jobs_metadata_params(meta_scan_range.jobs_params); request->__set_metada_table_params(metadata_table_params); @@ -463,6 +478,7 @@ Status MetaScanner::_build_tasks_metadata_request(const TMetaScanRange& meta_sca // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::TASKS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_tasks_metadata_params(meta_scan_range.tasks_params); request->__set_metada_table_params(metadata_table_params); @@ -483,6 +499,7 @@ Status MetaScanner::_build_partition_values_metadata_request( // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::PARTITION_VALUES); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_partition_values_metadata_params( meta_scan_range.partition_values_params); diff --git a/be/src/exec/scan/meta_scanner.h b/be/src/exec/scan/meta_scanner.h index 64725c72d98739..2662c997b9629f 100644 --- a/be/src/exec/scan/meta_scanner.h +++ b/be/src/exec/scan/meta_scanner.h @@ -21,6 +21,7 @@ #include #include +#include #include #include "common/factory_creator.h" @@ -49,7 +50,7 @@ class MetaScanner : public Scanner { public: MetaScanner(RuntimeState* state, ScanLocalStateBase* local_state, TupleId tuple_id, const TScanRangeParams& scan_range, int64_t limit, RuntimeProfile* profile, - TUserIdentity user_identity); + TUserIdentity user_identity, std::set current_roles); Status _open_impl(RuntimeState* state) override; Status close(RuntimeState* state) override; @@ -86,6 +87,11 @@ class MetaScanner : public Scanner { bool _meta_eos; TupleId _tuple_id; TUserIdentity _user_identity; + // SU narrowing: the session's active role subset, forwarded on every metadata request so the + // FE narrows its privilege checks to it. Empty = not narrowed. + std::set _current_roles; + // Stamps who is asking (and the narrowing, if any) on a metadata request. + void _set_session_identity(TMetadataTableRequestParams* params) const; const TupleDescriptor* _tuple_desc = nullptr; std::vector _batch_data; const TScanRange& _scan_range; diff --git a/be/src/information_schema/schema_authentication_integrations_scanner.cpp b/be/src/information_schema/schema_authentication_integrations_scanner.cpp index 3f3943326470b9..ce443103d41471 100644 --- a/be/src/information_schema/schema_authentication_integrations_scanner.cpp +++ b/be/src/information_schema/schema_authentication_integrations_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaAuthenticationIntegrationsScanner::_get_authentication_integrations schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } if (_param->common_param->frontend_conjuncts) { schema_table_request_params.__set_frontend_conjuncts( *_param->common_param->frontend_conjuncts); diff --git a/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp b/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp index 8abe2b4e4b3068..b23cbf1e5745a8 100644 --- a/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp +++ b/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp @@ -94,6 +94,9 @@ Status SchemaCatalogMetaCacheStatsScanner::_fetch_from_fe(size_t column_count, schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::CATALOG_META_CACHE_STATS); diff --git a/be/src/information_schema/schema_columns_scanner.cpp b/be/src/information_schema/schema_columns_scanner.cpp index 2952caf8aa608f..d6ea1c9e4c9adb 100644 --- a/be/src/information_schema/schema_columns_scanner.cpp +++ b/be/src/information_schema/schema_columns_scanner.cpp @@ -85,6 +85,9 @@ Status SchemaColumnsScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -339,6 +342,9 @@ Status SchemaColumnsScanner::_get_new_desc() { if (nullptr != _param->common_param->current_user_ident) { desc_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + desc_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { desc_params.__set_user(*(_param->common_param->user)); @@ -374,6 +380,9 @@ Status SchemaColumnsScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_extensions_scanner.cpp b/be/src/information_schema/schema_extensions_scanner.cpp index a31d35ed7a7443..504d402f66a4ed 100644 --- a/be/src/information_schema/schema_extensions_scanner.cpp +++ b/be/src/information_schema/schema_extensions_scanner.cpp @@ -65,6 +65,9 @@ Status SchemaExtensionsScanner::_get_extensions_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::EXTENSIONS); diff --git a/be/src/information_schema/schema_files_scanner.cpp b/be/src/information_schema/schema_files_scanner.cpp index 2171c121fa5a6d..12d879b6040627 100644 --- a/be/src/information_schema/schema_files_scanner.cpp +++ b/be/src/information_schema/schema_files_scanner.cpp @@ -93,6 +93,9 @@ Status SchemaFilesScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_metadata_name_ids_scanner.cpp b/be/src/information_schema/schema_metadata_name_ids_scanner.cpp index 790f34920cb0fa..7f0538e34da418 100644 --- a/be/src/information_schema/schema_metadata_name_ids_scanner.cpp +++ b/be/src/information_schema/schema_metadata_name_ids_scanner.cpp @@ -66,6 +66,9 @@ Status SchemaMetadataNameIdsScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -105,6 +108,9 @@ Status SchemaMetadataNameIdsScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_per_db_scanner.cpp b/be/src/information_schema/schema_per_db_scanner.cpp index 2f4f3f11a2731d..7d858070a66c70 100644 --- a/be/src/information_schema/schema_per_db_scanner.cpp +++ b/be/src/information_schema/schema_per_db_scanner.cpp @@ -52,6 +52,9 @@ Status SchemaPerDbScanner::start(RuntimeState* state) { } if (_param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } // The planner lifts an exact `TABLE_SCHEMA = '...'` out of the query for us. Without // it every visible database is listed and asked for its rows, and everything but one @@ -82,6 +85,9 @@ Status SchemaPerDbScanner::get_onedb_info_from_fe(int64_t db_id) { schema_table_request_params.columns_name.emplace_back(column.name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } schema_table_request_params.__set_catalog(*_param->common_param->catalog); schema_table_request_params.__set_dbId(db_id); // Same reason as the database pattern above: an exact `TABLE_NAME = '...'` lets the FE diff --git a/be/src/information_schema/schema_processlist_scanner.cpp b/be/src/information_schema/schema_processlist_scanner.cpp index 1795b4aeabbad7..e504623a464737 100644 --- a/be/src/information_schema/schema_processlist_scanner.cpp +++ b/be/src/information_schema/schema_processlist_scanner.cpp @@ -72,6 +72,9 @@ Status SchemaProcessListScanner::start(RuntimeState* state) { request.__set_show_full_sql(true); request.__set_time_zone(state->timezone()); request.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + request.__set_current_roles(_param->common_param->current_roles); + } for (const auto& fe_addr : _param->common_param->fe_addr_list) { TShowProcessListResult tmp_ret; diff --git a/be/src/information_schema/schema_profiling_scanner.cpp b/be/src/information_schema/schema_profiling_scanner.cpp index 145f9fa4a84d3d..4a5415f25f4738 100644 --- a/be/src/information_schema/schema_profiling_scanner.cpp +++ b/be/src/information_schema/schema_profiling_scanner.cpp @@ -71,6 +71,9 @@ Status SchemaProfilingScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_role_mappings_scanner.cpp b/be/src/information_schema/schema_role_mappings_scanner.cpp index f0c08f23230c16..aefb4001ba9322 100644 --- a/be/src/information_schema/schema_role_mappings_scanner.cpp +++ b/be/src/information_schema/schema_role_mappings_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaRoleMappingsScanner::_get_role_mappings_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } if (_param->common_param->frontend_conjuncts) { schema_table_request_params.__set_frontend_conjuncts( *_param->common_param->frontend_conjuncts); diff --git a/be/src/information_schema/schema_scanner.h b/be/src/information_schema/schema_scanner.h index 886c7269ea542d..d634a2141331c0 100644 --- a/be/src/information_schema/schema_scanner.h +++ b/be/src/information_schema/schema_scanner.h @@ -73,6 +73,9 @@ struct SchemaScannerCommonParam { const std::string* catalog = nullptr; std::set fe_addr_list; std::set required_columns; + // SU narrowing: the session's active role subset (TSchemaScanNode.current_roles), forwarded + // on the BE->FE metadata RPCs so they narrow name visibility. Empty = not narrowed. + std::set current_roles; }; // scanner parameter from frontend diff --git a/be/src/information_schema/schema_schema_privileges_scanner.cpp b/be/src/information_schema/schema_schema_privileges_scanner.cpp index 453e135c298a79..17a130e282f3ac 100644 --- a/be/src/information_schema/schema_schema_privileges_scanner.cpp +++ b/be/src/information_schema/schema_schema_privileges_scanner.cpp @@ -61,6 +61,9 @@ Status SchemaSchemaPrivilegesScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_schemata_scanner.cpp b/be/src/information_schema/schema_schemata_scanner.cpp index e9d25684780e92..0bf0ebf3db371a 100644 --- a/be/src/information_schema/schema_schemata_scanner.cpp +++ b/be/src/information_schema/schema_schemata_scanner.cpp @@ -60,6 +60,9 @@ Status SchemaSchemataScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp b/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp index f52d5399bc4df7..0203df685eefee 100644 --- a/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp +++ b/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp @@ -72,6 +72,9 @@ Status SchemaSqlBlockRuleStatusScanner::_get_sql_block_rule_status_block_from_fe TSchemaTableRequestParams schema_table_request_params; schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } schema_table_request_params.__set_frontend_conjuncts(*_param->common_param->frontend_conjuncts); TFetchSchemaTableDataRequest request; diff --git a/be/src/information_schema/schema_table_privileges_scanner.cpp b/be/src/information_schema/schema_table_privileges_scanner.cpp index c9b70c905b437c..79e9fbe313dc0a 100644 --- a/be/src/information_schema/schema_table_privileges_scanner.cpp +++ b/be/src/information_schema/schema_table_privileges_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaTablePrivilegesScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_tables_scanner.cpp b/be/src/information_schema/schema_tables_scanner.cpp index ef94909fd734f0..2d68568f012a14 100644 --- a/be/src/information_schema/schema_tables_scanner.cpp +++ b/be/src/information_schema/schema_tables_scanner.cpp @@ -82,6 +82,9 @@ Status SchemaTablesScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -117,6 +120,9 @@ Status SchemaTablesScanner::_get_new_table() { table_params.__set_required_columns(_param->common_param->required_columns); if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_user_privileges_scanner.cpp b/be/src/information_schema/schema_user_privileges_scanner.cpp index 9cfe5fe6fa00db..471f570547dfd6 100644 --- a/be/src/information_schema/schema_user_privileges_scanner.cpp +++ b/be/src/information_schema/schema_user_privileges_scanner.cpp @@ -60,6 +60,9 @@ Status SchemaUserPrivilegesScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_view_dependency_scanner.cpp b/be/src/information_schema/schema_view_dependency_scanner.cpp index a3e6842f736fb1..d9cf1bd9033063 100644 --- a/be/src/information_schema/schema_view_dependency_scanner.cpp +++ b/be/src/information_schema/schema_view_dependency_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaViewDependencyScanner::_get_view_dependency_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_view_dependency_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } schema_table_request_params.__set_frontend_conjuncts(*_param->common_param->frontend_conjuncts); TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::VIEW_DEPENDENCY); diff --git a/be/src/information_schema/schema_views_scanner.cpp b/be/src/information_schema/schema_views_scanner.cpp index 8d99f2b24fa71b..fa30e30f7223ee 100644 --- a/be/src/information_schema/schema_views_scanner.cpp +++ b/be/src/information_schema/schema_views_scanner.cpp @@ -64,6 +64,9 @@ Status SchemaViewsScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -91,6 +94,9 @@ Status SchemaViewsScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_workload_group_privileges.cpp b/be/src/information_schema/schema_workload_group_privileges.cpp index 458d89239b6d06..b831382c13430a 100644 --- a/be/src/information_schema/schema_workload_group_privileges.cpp +++ b/be/src/information_schema/schema_workload_group_privileges.cpp @@ -55,6 +55,9 @@ Status SchemaWorkloadGroupPrivilegesScanner::_get_workload_group_privs_block_fro schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::WORKLOAD_GROUP_PRIVILEGES); diff --git a/be/src/information_schema/schema_workload_groups_scanner.cpp b/be/src/information_schema/schema_workload_groups_scanner.cpp index 527696de3cd759..e8c005279fad45 100644 --- a/be/src/information_schema/schema_workload_groups_scanner.cpp +++ b/be/src/information_schema/schema_workload_groups_scanner.cpp @@ -68,6 +68,9 @@ Status SchemaWorkloadGroupsScanner::_get_workload_groups_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::WORKLOAD_GROUPS); diff --git a/be/src/information_schema/schema_workload_sched_policy_scanner.cpp b/be/src/information_schema/schema_workload_sched_policy_scanner.cpp index 12a73f8ec1928d..cfd475e4b46686 100644 --- a/be/src/information_schema/schema_workload_sched_policy_scanner.cpp +++ b/be/src/information_schema/schema_workload_sched_policy_scanner.cpp @@ -59,6 +59,9 @@ Status SchemaWorkloadSchedulePolicyScanner::_get_workload_schedule_policy_block_ schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::WORKLOAD_SCHEDULE_POLICY); diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index 4b040857c46fd2..762341f7a29be0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -5623,6 +5623,27 @@ public String getMasterHost() { return this.masterInfo.getHost(); } + /** + * Whether the master FE reports (through its heartbeat) the same build as this FE. Gates the + * forwarding of request fields an older master would silently ignore, such as a session's SU + * role narrowing: the master is upgraded last in a rolling upgrade, so a newer follower must not + * hand such a statement to a master that cannot apply the field. An unknown or dead master is + * treated as a different build. + */ + public boolean masterRunsSameBuild() { + if (isMaster()) { + return true; + } + String masterHost = getMasterHost(); + int masterRpcPort = getMasterRpcPort(); + for (Frontend fe : frontends.values()) { + if (fe.getHost().equals(masterHost) && fe.getRpcPort() == masterRpcPort) { + return fe.isAlive() && Frontend.localBuildVersion().equals(fe.getVersion()); + } + } + return false; + } + public PolicyMgr getPolicyMgr() { diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java index ce8e53c251e599..131a627fc64f52 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java @@ -37,7 +37,9 @@ import com.google.common.collect.Lists; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class MetadataScanNode extends ExternalScanNode { @@ -65,6 +67,12 @@ protected void toThrift(TPlanNode planNode) { metaScanNode.setMetadataType(this.tvf.getMetadataType()); TUserIdentity tCurrentUser = ConnectContext.get().getCurrentUserIdentity().toThrift(); metaScanNode.setCurrentUserIdent(tCurrentUser); + // SU narrowing: carry the session's active role subset so the BE->FE metadata requests + // narrow their privilege checks to the same set the session sees; null = not narrowed. + Set sessionRoleOverride = ConnectContext.get().getSessionRoleOverride(); + if (sessionRoleOverride != null) { + metaScanNode.setCurrentRoles(new HashSet<>(sessionRoleOverride)); + } planNode.setMetaScanNode(metaScanNode); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index e13c280685f783..88a4dbb0d10c48 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -297,7 +297,49 @@ public void checkPasswordForUserIdentity(UserIdentity userIdentity, byte[] remot } } + // SU narrowing: FE-side SU narrowing for BE->FE metadata RPC handler threads, which have NO + // ConnectContext. The handler installs the calling session's narrowed role NAMES here (keyed by + // the target identity) around its privilege filter; getRolesByUserWithLdap then narrows exactly + // as a live SU session would. The handler MUST clear it in a finally (thrift threads are pooled). + private static final ThreadLocal>> RPC_SESSION_NARROWING = + new ThreadLocal<>(); + + public static void setRpcSessionNarrowing(UserIdentity user, Set roleNames) { + Map> m = Maps.newHashMap(); + m.put(user, roleNames); + RPC_SESSION_NARROWING.set(m); + } + + public static void clearRpcSessionNarrowing() { + RPC_SESSION_NARROWING.remove(); + } + + // The SU-narrowed role set: exactly the override roles + the implicit information_schema/mysql + // read baseline (see narrowingBaselineRole). Shared by the live-session and RPC-handler paths. + private Set narrowedRoleSet(Set overrideRoleNames) { + Set narrowed = Sets.newHashSet(); + for (String roleName : overrideRoleNames) { + Role role = roleManager.getRole(roleName); + if (role != null) { + narrowed.add(role); + } + } + Role baseline = narrowingBaselineRole(); + if (baseline != null) { + narrowed.add(baseline); + } + return narrowed; + } + public Set getRolesByUserWithLdap(UserIdentity userIdentity) { + Map> rpcNarrow = RPC_SESSION_NARROWING.get(); + if (rpcNarrow != null) { + Set rpcOverride = rpcNarrow.get(userIdentity); + if (rpcOverride != null) { + // SU narrowing: a BE->FE metadata RPC carried this session's narrowed role subset. + return narrowedRoleSet(rpcOverride); + } + } Set roles = Sets.newHashSet(); Set roleNames = userRoleManager.getRolesByUser(userIdentity); for (String roleName : roleNames) { @@ -318,25 +360,9 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { if (sessionRoleOverride != null) { // SU-narrowed session: the override REPLACES every role source (user grants, LDAP, // authenticated roles). SuUserCommand enforced the ceiling at switch time. - Set narrowed = Sets.newHashSet(); - for (String roleName : sessionRoleOverride) { - Role role = roleManager.getRole(roleName); - if (role != null) { - narrowed.add(role); - } - } - // Preserve the implicit information_schema/mysql read access that every session's - // default role carries, so a narrowed session can still do basic metadata/client - // operations (BI drivers, SHOW, information_schema listings). These databases are - // self-filtered by the session's actual privileges — a narrowed session sees only - // rows for objects it can access — so keeping them widens NO data access while every - // privilege-bearing role (and the user's direct grants in the default role) stays - // dropped. - Role baseline = narrowingBaselineRole(); - if (baseline != null) { - narrowed.add(baseline); - } - return narrowed; + // SU-narrowed session: the override REPLACES every role source; return the + // override roles + the information_schema/mysql read baseline (see narrowedRoleSet). + return narrowedRoleSet(sessionRoleOverride); } for (String roleName : ctx.getAuthenticatedRoles()) { Role role = roleManager.getRole(roleName); diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java index ef6e34b8dad0f4..ed5bedabb56876 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java @@ -45,7 +45,9 @@ import org.apache.logging.log4j.Logger; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * Full scan of an SCHEMA table. @@ -162,6 +164,13 @@ protected void toThrift(TPlanNode msg) { TUserIdentity tCurrentUser = ConnectContext.get().getCurrentUserIdentity().toThrift(); msg.schema_scan_node.setCurrentUserIdent(tCurrentUser); + // SU narrowing: carry the session's active role subset (current_roles) in the plan so the + // BE->FE metadata RPCs (getDbNames/getTableNames/listTableStatus/describeTables) narrow + // name visibility to the same set the session sees; null = normal session (no narrowing). + Set sessionRoleOverride = ConnectContext.get().getSessionRoleOverride(); + if (sessionRoleOverride != null) { + msg.schema_scan_node.setCurrentRoles(new HashSet<>(sessionRoleOverride)); + } msg.schema_scan_node.setFrontendConjuncts(GsonUtils.GSON.toJson(frontendConjuncts)); setFeAddrList(msg); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java index b5699152df7079..b5d918c16de741 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java @@ -81,6 +81,7 @@ import org.apache.doris.thrift.TUniqueId; import org.apache.doris.transaction.TransactionEntry; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; @@ -95,10 +96,12 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.OptionalLong; +import java.util.Set; import java.util.UUID; /** @@ -546,6 +549,21 @@ protected void handleQueryException(Throwable throwable, String origStmt, auditAfterExec(origStmt, parsedStmt, statistics, true); } + /** + * Install the SU narrowing a forwarded statement carries (is_su_user + current_roles) on this + * proxy session, so every privilege check on the master resolves the same narrowed role set the + * origin FE would. Fail closed: a switched session that carries no role list narrows to the + * empty set (the information_schema/mysql baseline only), never to the target's full roles. + */ + @VisibleForTesting + static void applyForwardedSessionNarrowing(ConnectContext ctx, TMasterOpRequest request) { + if (!request.isSetIsSuUser() || !request.isIsSuUser()) { + return; + } + Set roles = request.isSetCurrentRoles() ? request.getCurrentRoles() : Collections.emptySet(); + ctx.setSessionRoleOverride(roles); + } + public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException { ctx.setDatabase(request.db); ctx.setEnv(Env.getCurrentEnv()); @@ -562,6 +580,7 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException } else { ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp(request.user, "%")); } + applyForwardedSessionNarrowing(ctx, request); if (request.isFoldConstantByBe()) { ctx.getSessionVariable().setEnableFoldConstantByBe(request.foldConstantByBe); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java index 404814ebeb3369..673c70547c4aba 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java @@ -50,6 +50,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -194,6 +195,13 @@ protected TMasterOpRequest buildStmtForwardParams() throws AnalysisException { params.setUserIp(ctx.getRemoteIP()); params.setStmtId(ctx.getStmtId()); params.setCurrentUserIdent(ctx.getCurrentUserIdentity().toThrift()); + Set sessionRoleOverride = ctx.getSessionRoleOverride(); + if (sessionRoleOverride != null) { + // Session-narrowed (SU) session: carry the active role subset so the master authorizes + // the forwarded statement against exactly this set, never the target's full role union. + params.setIsSuUser(true); + params.setCurrentRoles(new HashSet<>(sessionRoleOverride)); + } params.setSessionId(ctx.getSessionId()); params.setMoreResultExists(moreStmtExists); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index cf7dc54c34e92c..6203ad78c07d4c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -1525,16 +1525,16 @@ public boolean isProfileSafeStmt() { } private void forwardToMaster() throws Exception { - // FAIL CLOSED for session-narrowed (SU) sessions: the narrowing (sessionRoleOverride) - // is local ConnectContext state and is NOT carried in the forward RPC, so the master - // would rebuild the session with the target identity but NO override and authorize the - // statement against the target's FULL role set. A narrowed serving session's reads run - // locally on this FE; a statement that must be forwarded (SHOW-class metadata forwarded - // to master, DDL/DML/GRANT) is refused rather than run un-narrowed on the master. - if (context.getSessionRoleOverride() != null) { - throw new UserException("This statement would be forwarded to the master FE, which is not " - + "allowed in a session-narrowed (SU) session, because the narrowing does not " - + "propagate to the master. Run it in a non-narrowed session."); + // A session-narrowed (SU) session carries its active role subset in the forward request + // (TMasterOpRequest.is_su_user / current_roles) and the master installs it before + // executing. An older master silently ignores those fields and would authorize the + // statement against the target's FULL role union, so fail closed unless the master is + // known to run this same build (a rolling upgrade upgrades the master last). + if (context.getSessionRoleOverride() != null && !Env.getCurrentEnv().masterRunsSameBuild()) { + throw new UserException("This statement would be forwarded to the master FE, which runs a " + + "different build than this FE; a session-narrowed (SU) session forwards only when " + + "the master is known to apply the narrowing. Retry once the rolling upgrade " + + "completes, or run it in a non-narrowed session."); } masterOpExecutor = new MasterOpExecutor(originStmt, context, redirectStatus, isQuery()); if (LOG.isDebugEnabled()) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java index dc6ae5ecc34994..64288350b1ad24 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java +++ b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java @@ -110,6 +110,7 @@ import org.apache.doris.master.MasterImpl; import org.apache.doris.meta.MetaContext; import org.apache.doris.mysql.privilege.AccessControllerManager; +import org.apache.doris.mysql.privilege.Auth; import org.apache.doris.mysql.privilege.PrivPredicate; import org.apache.doris.nereids.trees.plans.PlanNodeAndHash; import org.apache.doris.nereids.trees.plans.commands.RestoreCommand; @@ -325,6 +326,7 @@ import org.apache.doris.thrift.TUpdateFollowerPartitionStatsCacheRequest; import org.apache.doris.thrift.TUpdateFollowerStatsCacheRequest; import org.apache.doris.thrift.TUpdatePlanStatsCacheRequest; +import org.apache.doris.thrift.TUserIdentity; import org.apache.doris.thrift.TWaitingTxnStatusRequest; import org.apache.doris.thrift.TWaitingTxnStatusResult; import org.apache.doris.transaction.BeginTransactionException; @@ -500,8 +502,41 @@ public TConfirmUnusedRemoteFilesResult confirmUnusedRemoteFiles(TConfirmUnusedRe return res; } + /** + * Runs a metadata RPC handler under the calling session's SU-narrowed role subset when the + * request carries one, so every privilege check inside the handler resolves the same narrowed + * set a live SU session would (Auth.getRolesByUserWithLdap consults the thread-local). It is + * cleared in a finally because handler threads are pooled. A request without an override runs + * unchanged. + */ + private static T withSessionRoleOverride(TUserIdentity currentUserIdent, String user, String userIp, + Set currentRoles, MetadataRpcHandler handler) throws TException { + if (currentRoles == null) { + return handler.run(); + } + UserIdentity narrowedUser = currentUserIdent != null + ? UserIdentity.fromThrift(currentUserIdent) + : UserIdentity.createAnalyzedUserIdentWithIp(user, userIp); + Auth.setRpcSessionNarrowing(narrowedUser, Sets.newHashSet(currentRoles)); + try { + return handler.run(); + } finally { + Auth.clearRpcSessionNarrowing(); + } + } + + @FunctionalInterface + private interface MetadataRpcHandler { + T run() throws TException; + } + @Override public TGetDbsResult getDbNames(TGetDbsParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> getDbNamesImpl(params)); + } + + private TGetDbsResult getDbNamesImpl(TGetDbsParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get db request: {}", params); } @@ -595,6 +630,11 @@ private String getDbNameFromMysqlTableSchema(String ctl, String db) { @Override public TGetTablesResult getTableNames(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> getTableNamesImpl(params)); + } + + private TGetTablesResult getTableNamesImpl(TGetTablesParams params) throws TException { try { if (LOG.isDebugEnabled()) { LOG.debug("get table name request: {}", params); @@ -657,6 +697,11 @@ public TGetTablesResult getTableNames(TGetTablesParams params) throws TException @Override public TListTableStatusResult listTableStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listTableStatusImpl(params)); + } + + private TListTableStatusResult listTableStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list table request: {}", params); } @@ -956,6 +1001,11 @@ public TListPrivilegesResult listUserPrivilegeStatus(TGetTablesParams params) th @Override public TDescribeTablesResult describeTables(TDescribeTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> describeTablesImpl(params)); + } + + private TDescribeTablesResult describeTablesImpl(TDescribeTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get desc tables request: {}", params); } @@ -3416,6 +3466,28 @@ public TStatus syncCloudVersion(TFrontendSyncCloudVersionRequest request) @Override public TFetchSchemaTableDataResult fetchSchemaTableData(TFetchSchemaTableDataRequest request) throws TException { + // information_schema tables and metadata table functions: the request names the caller and, + // for a session-narrowed (SU) session, the active role subset; MetadataGenerator's privilege + // checks then resolve that set instead of the caller's full role union. + TUserIdentity currentUserIdent = null; + Set currentRoles = null; + if (request.isSetSchemaTableParams()) { + currentUserIdent = request.getSchemaTableParams().current_user_ident; + currentRoles = request.getSchemaTableParams().current_roles; + } else if (request.isSetMetadaTableParams()) { + currentUserIdent = request.getMetadaTableParams().current_user_ident; + currentRoles = request.getMetadaTableParams().current_roles; + } + if (currentRoles != null && currentUserIdent == null) { + // a narrowed request must say whom it narrows; never fall back to the full role union + return MetadataGenerator.errorResult("current user ident is not set for a narrowed request"); + } + return withSessionRoleOverride(currentUserIdent, null, null, currentRoles, + () -> fetchSchemaTableDataImpl(request)); + } + + private TFetchSchemaTableDataResult fetchSchemaTableDataImpl(TFetchSchemaTableDataRequest request) + throws TException { try { if (!request.isSetSchemaTableName()) { return MetadataGenerator.errorResult("Fetch schema table name is not set"); @@ -5758,6 +5830,13 @@ public TStatus reportCommitTxnResult(TReportCommitTxnResultRequest request) thro @Override public TShowProcessListResult showProcessList(TShowProcessListRequest request) throws TException { + // a narrowed session's ADMIN check inside listConnectionForRpc resolves the narrowed set, so a + // tenant-narrowed administrator sees only the sessions of the identity it runs as + return withSessionRoleOverride(request.current_user_ident, null, null, request.current_roles, + () -> showProcessListImpl(request)); + } + + private TShowProcessListResult showProcessListImpl(TShowProcessListRequest request) throws TException { if (!request.isSetCurrentUserIdent()) { throw new TException("Current user identity is not set"); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java b/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java index 62970a9315cc6a..37c2cde273feb8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java +++ b/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java @@ -19,6 +19,7 @@ import org.apache.doris.catalog.Env; import org.apache.doris.common.Config; +import org.apache.doris.common.Version; import org.apache.doris.common.io.Text; import org.apache.doris.common.io.Writable; import org.apache.doris.ha.BDBHA; @@ -93,6 +94,11 @@ public String getVersion() { return version; } + /** The build string this FE reports in its heartbeat; other FEs compare their own against it. */ + public static String localBuildVersion() { + return Version.DORIS_BUILD_VERSION + "-" + Version.DORIS_BUILD_SHORT_HASH; + } + public String getLocalResourceGroup() { return localResourceGroup; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java b/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java index 345cd287be8bee..b4772a2e430ae5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java @@ -25,7 +25,6 @@ import org.apache.doris.common.FeConstants; import org.apache.doris.common.ThreadPoolManager; import org.apache.doris.common.UserException; -import org.apache.doris.common.Version; import org.apache.doris.common.util.DebugPointUtil; import org.apache.doris.common.util.HttpURLUtil; import org.apache.doris.common.util.MasterDaemon; @@ -494,7 +493,7 @@ public HeartbeatResponse call() { return new FrontendHbResponse(fe.getNodeName(), Config.query_port, Config.rpc_port, Config.arrow_flight_sql_port, Env.getCurrentEnv().getMaxJournalId(), System.currentTimeMillis(), - Version.DORIS_BUILD_VERSION + "-" + Version.DORIS_BUILD_SHORT_HASH, + Frontend.localBuildVersion(), ExecuteEnv.getInstance().getStartupTime(), ExecuteEnv.getInstance().getDiskInfos(), ExecuteEnv.getInstance().getProcessUUID(), Config.local_resource_group); } else { diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index 70038c44813c0b..3923f025905509 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -333,4 +333,35 @@ public void testParserRoundTrip() { Object select = new NereidsParser().parseSingle("SELECT su FROM t1"); Assert.assertNotNull(select); } + + // SU narrowing: a BE->FE metadata RPC (getDbNames/getTableNames/listTableStatus/describeTables) + // runs on a handler thread with NO ConnectContext. Auth.setRpcSessionNarrowing lets the handler + // apply the calling session's narrowed role subset there, so information_schema name visibility + // matches the session instead of resolving the target's FULL roles (the pre-fix leak). + @Test + public void testRpcSessionNarrowingAppliesOnMetadataRpcThread() throws Exception { + addUser("rpcuser", true); + createRole("rpc_space"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'rpc_space';"); + grantRole("GRANT 'rpc_space' TO 'rpcuser'@'%'"); + // a personal (default-role) grant on another db, which the narrowing must DROP + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'rpcuser'@'%';"); + UserIdentity rpcuser = ident("rpcuser"); + try { + // no narrowing installed -> full roles resolved (the pre-fix behavior/leak): + // both the personal grant and the role grant are visible. + Assert.assertTrue(canSelectDb(rpcuser, "perso")); + Assert.assertTrue(canSelectDb(rpcuser, "test")); + + // install exactly what the metadata RPC carries in current_roles + Auth.setRpcSessionNarrowing(rpcuser, Collections.singleton("rpc_space")); + Assert.assertFalse(canSelectDb(rpcuser, "perso")); // personal grant DROPPED + Assert.assertTrue(canSelectDb(rpcuser, "test")); // requested role KEPT + Assert.assertTrue(canSelectInfoSchema(rpcuser)); // info_schema baseline preserved + } finally { + Auth.clearRpcSessionNarrowing(); + } + // cleared -> full roles restored (no leak of the narrowing to later requests on this thread) + Assert.assertTrue(canSelectDb(rpcuser, "perso")); + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java new file mode 100644 index 00000000000000..bf4233d86c0235 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java @@ -0,0 +1,152 @@ +// 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.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.thrift.TMasterOpRequest; +import org.apache.doris.thrift.TNetworkAddress; +import org.apache.doris.utframe.TestWithFeService; + +import com.google.common.collect.Sets; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +/** + * A session-narrowed (SU) session forwarding a statement to the master: the origin FE carries the + * session's active role subset in TMasterOpRequest (is_su_user + current_roles) and the master + * installs it on the proxy session before executing, so the statement is authorized against exactly + * that set and never the target's full role union. + */ +public class ForwardedSessionNarrowingTest extends TestWithFeService { + + private static final String CTL = InternalCatalog.INTERNAL_CATALOG_NAME; + + @Override + protected void runBeforeAll() throws Exception { + // GRANT validates database existence on the internal catalog + createDatabase("test"); + createDatabase("perso"); + } + + private UserIdentity ident(String name) { + return UserIdentity.createAnalyzedUserIdentWithIp(name, "%"); + } + + private boolean canSelectDb(UserIdentity user, String db) { + return Env.getCurrentEnv().getAccessManager().checkDbPriv(user, CTL, db, PrivPredicate.SELECT); + } + + private static TMasterOpRequest forwardRequestOf(ConnectContext ctx) throws AnalysisException { + return new ForwardParamsProbe(ctx).build(); + } + + /** Exposes the request the origin FE would send for this session. */ + private static class ForwardParamsProbe extends FEOpExecutor { + private ForwardParamsProbe(ConnectContext ctx) { + super(new TNetworkAddress("127.0.0.1", 9020), new OriginStatement("show frontends", 0), ctx, true); + } + + private TMasterOpRequest build() throws AnalysisException { + return buildStmtForwardParams(); + } + } + + @Test + public void testOriginCarriesNarrowingOnlyWhenSwitched() throws Exception { + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("alice")); + ctx.setRemoteIP("127.0.0.1"); + + TMasterOpRequest plain = forwardRequestOf(ctx); + Assert.assertFalse(plain.isSetIsSuUser()); + Assert.assertFalse(plain.isSetCurrentRoles()); + + ctx.setSessionRoleOverride(Sets.newHashSet("tenant_a", "tenant_a_scoped")); + TMasterOpRequest narrowed = forwardRequestOf(ctx); + Assert.assertTrue(narrowed.isSetIsSuUser() && narrowed.isIsSuUser()); + Assert.assertEquals(Sets.newHashSet("tenant_a", "tenant_a_scoped"), narrowed.getCurrentRoles()); + // the identity the master rebuilds is still the session's (switched-to) identity + Assert.assertEquals(ctx.getQualifiedUser(), narrowed.getUser()); + } + + @Test + public void testMasterInstallsCarriedNarrowingOnProxySession() throws Exception { + addUser("bob", true); + createRole("tenant_b"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'tenant_b';"); + grantRole("GRANT 'tenant_b' TO 'bob'@'%'"); + // a personal (default-role) grant the narrowing must drop on the master as well + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'bob'@'%';"); + UserIdentity bob = ident("bob"); + + TMasterOpRequest request = new TMasterOpRequest(); + request.setIsSuUser(true); + request.setCurrentRoles(Sets.newHashSet("tenant_b")); + + ConnectContext proxy = new ConnectContext(); + proxy.setCurrentUserIdentity(bob); + ConnectProcessor.applyForwardedSessionNarrowing(proxy, request); + Assert.assertEquals(Collections.singleton("tenant_b"), proxy.getSessionRoleOverride()); + + proxy.setThreadLocalInfo(); + try { + Assert.assertFalse(canSelectDb(bob, "perso")); // personal grant dropped + Assert.assertTrue(canSelectDb(bob, "test")); // carried role kept + } finally { + connectContext.setThreadLocalInfo(); + } + // the same identity checked outside the proxy session sees its full union + Assert.assertTrue(canSelectDb(bob, "perso")); + } + + @Test + public void testMasterFailsClosedWhenSwitchedRequestCarriesNoRoles() { + TMasterOpRequest request = new TMasterOpRequest(); + request.setIsSuUser(true); + ConnectContext proxy = new ConnectContext(); + ConnectProcessor.applyForwardedSessionNarrowing(proxy, request); + Assert.assertNotNull(proxy.getSessionRoleOverride()); + Assert.assertTrue(proxy.getSessionRoleOverride().isEmpty()); + } + + @Test + public void testUnswitchedRequestLeavesProxySessionUnnarrowed() { + ConnectContext proxy = new ConnectContext(); + ConnectProcessor.applyForwardedSessionNarrowing(proxy, new TMasterOpRequest()); + Assert.assertNull(proxy.getSessionRoleOverride()); + + // a role list without the switched flag is not a narrowing + TMasterOpRequest rolesOnly = new TMasterOpRequest(); + rolesOnly.setCurrentRoles(Sets.newHashSet("tenant_c")); + ConnectProcessor.applyForwardedSessionNarrowing(proxy, rolesOnly); + Assert.assertNull(proxy.getSessionRoleOverride()); + } + + @Test + public void testMasterItselfAlwaysRunsTheSameBuild() { + // the single-FE test environment is its own master: nothing to forward, nothing to refuse + Assert.assertTrue(Env.getCurrentEnv().isMaster()); + Assert.assertTrue(Env.getCurrentEnv().masterRunsSameBuild()); + } +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java new file mode 100644 index 00000000000000..c30699deecda84 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java @@ -0,0 +1,101 @@ +// 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.service; + +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.thrift.TFetchSchemaTableDataRequest; +import org.apache.doris.thrift.TFetchSchemaTableDataResult; +import org.apache.doris.thrift.TSchemaTableName; +import org.apache.doris.thrift.TSchemaTableRequestParams; +import org.apache.doris.thrift.TStatusCode; +import org.apache.doris.utframe.TestWithFeService; + +import com.google.common.collect.Sets; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.Set; + +/** + * A BE->FE schema-table request from a session-narrowed (SU) session carries the session's active + * role subset (TSchemaTableRequestParams.current_roles); the handler installs it so MetadataGenerator's + * privilege checks resolve that set instead of the caller's full role union. + */ +public class NarrowedMetadataRpcTest extends TestWithFeService { + + private final ExecuteEnv exeEnv = Mockito.mock(ExecuteEnv.class); + + @Override + protected void runBeforeAll() throws Exception { + createDatabase("test"); + createDatabase("perso"); + addUser("carol", true); + createRole("tenant_c"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'tenant_c';"); + grantRole("GRANT 'tenant_c' TO 'carol'@'%'"); + // a personal (default-role) grant the narrowing must drop + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'carol'@'%';"); + } + + private static TFetchSchemaTableDataRequest databaseProperties(String db, Set currentRoles) { + TSchemaTableRequestParams params = new TSchemaTableRequestParams(); + params.setCurrentUserIdent(UserIdentity.createAnalyzedUserIdentWithIp("carol", "%").toThrift()); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + params.setDbId(Env.getCurrentInternalCatalog().getDbNullable(db).getId()); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + TFetchSchemaTableDataRequest request = new TFetchSchemaTableDataRequest(); + request.setSchemaTableName(TSchemaTableName.DATABASE_PROPERTIES); + request.setSchemaTableParams(params); + return request; + } + + private static int rows(TFetchSchemaTableDataResult result) { + Assertions.assertEquals(TStatusCode.OK, result.getStatus().getStatusCode()); + return result.getDataBatch().size(); + } + + @Test + public void testSchemaTableRequestNarrowsPrivilegeChecks() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + // un-narrowed: the personal grant on perso and the role grant on test both show + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("perso", null)))); + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("test", null)))); + + // narrowed to the tenant role: the personal grant is gone, the role grant remains + Set narrowed = Sets.newHashSet("tenant_c"); + Assertions.assertEquals(0, rows(impl.fetchSchemaTableData(databaseProperties("perso", narrowed)))); + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("test", narrowed)))); + + // the narrowing did not stick to the handler thread + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("perso", null)))); + } + + @Test + public void testNarrowedRequestWithoutIdentityFailsClosed() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + TFetchSchemaTableDataRequest request = databaseProperties("test", Sets.newHashSet("tenant_c")); + request.getSchemaTableParams().unsetCurrentUserIdent(); + TFetchSchemaTableDataResult result = impl.fetchSchemaTableData(request); + Assertions.assertNotEquals(TStatusCode.OK, result.getStatus().getStatusCode()); + } +} diff --git a/gensrc/thrift/FrontendService.thrift b/gensrc/thrift/FrontendService.thrift index 7d96a9e7415bce..581b55bb33c4a3 100644 --- a/gensrc/thrift/FrontendService.thrift +++ b/gensrc/thrift/FrontendService.thrift @@ -65,9 +65,10 @@ struct TDescribeTablesParams { 5: optional Types.TUserIdentity current_user_ident // to replace the user and user ip 6: optional bool show_hidden_columns = false 7: optional string catalog - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 8: optional set reserved_field_8 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows name visibility to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 8: optional set current_roles // Report COLUMN_KEY the way MySQL does. Forwarded from the schema scan node because // this request carries no session of its own. 9: optional bool mysql_compatible_index_metadata = false @@ -101,9 +102,10 @@ struct TGetDbsParams { 4: optional Types.TUserIdentity current_user_ident // to replace the user and user ip 5: optional string catalog 6: optional bool get_null_catalog //if catalog is empty , get dbName ="NULL" and dbId = -1. - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 7: optional set reserved_field_7 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows name visibility to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 7: optional set current_roles } // getDbNames returns a list of database names , database ids and catalog names ,catalog ids @@ -128,9 +130,10 @@ struct TGetTablesParams { 6: optional string type 7: optional string catalog 8: optional string table - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 9: optional set reserved_field_9 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows name visibility to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 9: optional set current_roles // Columns needed by schema table callers. If unset, the callee returns the // full table status for backward compatibility. 10: optional set required_columns @@ -428,10 +431,12 @@ struct TMasterOpRequest { 32: optional bool moreResultExists // Server has more result to send 33: optional map connect_attributes - // Reserved for downstream fields `current_roles` and `is_su_user` to keep - // thrift field ids wire-compatible across maintained branches. Do not reuse these ids. - 34: optional set reserved_field_34 - 35: optional bool reserved_field_35 + // Session-narrowed (SU) session: the active role subset that REPLACES the target's role + // union while the master executes this statement, and the flag that the session is + // switched. A switched session that carries no role list narrows to the empty set on the + // master (fail closed). Ids 34/35 were reserved for exactly these fields. + 34: optional set current_roles + 35: optional bool is_su_user 36: optional string connectingFeLocalResourceGroup // selectdb cloud @@ -967,9 +972,10 @@ struct TMetadataTableRequestParams { 12: optional PlanNodes.TMetaCacheStatsParams meta_cache_stats_params 13: optional PlanNodes.TPartitionValuesMetadataParams partition_values_metadata_params 14: optional PlanNodes.THudiMetadataParams hudi_metadata_params - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 15: optional set reserved_field_15 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows its privilege checks to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 15: optional set current_roles 16: optional PlanNodes.TLanceIndexMetadataParams lance_index_metadata_params } @@ -982,9 +988,10 @@ struct TSchemaTableRequestParams { 6: optional string time_zone // used for DATETIME field 7: optional string frontend_conjuncts 8: optional i64 thread_id // mysql connection id for fetching ConnectContext if needed - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 9: optional set reserved_field_9 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows its privilege checks to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 9: optional set current_roles // The one table the query asked for, when it pinned one with `TABLE_NAME = '...'`. // Lets the FE answer from that table instead of walking the whole database. 10: optional string table_name @@ -1628,9 +1635,10 @@ struct TShowProcessListRequest { 1: optional bool show_full_sql 2: optional Types.TUserIdentity current_user_ident 3: optional string time_zone - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 4: optional set reserved_field_4 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows its privilege checks to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 4: optional set current_roles } struct TShowProcessListResult { diff --git a/gensrc/thrift/PlanNodes.thrift b/gensrc/thrift/PlanNodes.thrift index cd02c8127ec903..a3ec3a5ac4a393 100644 --- a/gensrc/thrift/PlanNodes.thrift +++ b/gensrc/thrift/PlanNodes.thrift @@ -1056,12 +1056,18 @@ struct TSchemaScanNode { // Captured from the session at plan time, because the FE cannot see the session of the // query when the BE calls back into it for schema metadata. 17: optional bool mysql_compatible_index_metadata = false + // SU-narrowed session's active role subset; the BE forwards it on its metadata RPCs + // (current_roles on TGetDbsParams / TGetTablesParams / TDescribeTablesParams). Unset = not narrowed. + 18: optional set current_roles } struct TMetaScanNode { 1: required Types.TTupleId tuple_id 2: optional Types.TMetadataType metadata_type 3: optional Types.TUserIdentity current_user_ident + // SU-narrowed session's active role subset; the BE forwards it on its metadata RPCs + // (current_roles on TMetadataTableRequestParams). Unset = not narrowed. + 4: optional set current_roles } struct TTestExternalScanNode { From 1402f55ca552269fb164f850fb4a9cbc4749e027 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 8 Sep 2026 16:40:13 +0530 Subject: [PATCH 03/30] [feature](audit) Record the account that authenticated the connection beside the effective user The audit log names the session's effective user. Under a session-narrowed (SU) switch that is the target identity, so the row no longer says who actually logged in and ran the statement through it. Add an `authenticated_user` audit field and audit_log column, right after `user`: the account that authenticated the connection, equal to `user` unless the session was switched. Every row carries it, so "everything run through " is one predicate. Existing audit_log tables gain the column through the internal-schema upgrade path (add + reorder to the schema order); the loader keeps one field per schema column, which AuditLoaderTest now asserts explicitly. AuditLogHelperTest covers the mapping before, during and after a switch. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Raghvendra Singh --- .../apache/doris/catalog/InternalSchema.java | 2 ++ .../org/apache/doris/plugin/AuditEvent.java | 9 ++++++++ .../doris/plugin/audit/AuditLoader.java | 1 + .../org/apache/doris/qe/AuditLogHelper.java | 13 +++++++++++ .../doris/plugin/audit/AuditLoaderTest.java | 21 +++++++++++++++++ .../apache/doris/qe/AuditLogHelperTest.java | 23 +++++++++++++++++++ 6 files changed, 69 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java index 1e429a1b288f26..c3edefa8aa8f65 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java @@ -146,6 +146,8 @@ public class InternalSchema { ScalarType.createVarchar(128), ColumnNullableType.NULLABLE)); AUDIT_SCHEMA.add(new ColumnDef("user", ScalarType.createVarchar(128), ColumnNullableType.NULLABLE)); + AUDIT_SCHEMA.add(new ColumnDef("authenticated_user", + ScalarType.createVarchar(128), ColumnNullableType.NULLABLE)); AUDIT_SCHEMA.add(new ColumnDef("frontend_ip", ScalarType.createVarchar(1024), ColumnNullableType.NULLABLE)); // default ctl and db diff --git a/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java b/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java index 37432e0f7d62a8..f5593e96521b56 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java +++ b/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java @@ -64,6 +64,10 @@ public enum EventType { public String clientIp = ""; @AuditField(value = "User", colName = "user") public String user = ""; + // the account that authenticated the connection: equal to `user` unless the session was + // switched (SU), in which case `user` is the effective identity and this is the switcher + @AuditField(value = "AuthenticatedUser", colName = "authenticated_user") + public String authenticatedUser = ""; @AuditField(value = "FeIp", colName = "frontend_ip") public String feIp = ""; @@ -206,6 +210,11 @@ public AuditEventBuilder setUser(String user) { return this; } + public AuditEventBuilder setAuthenticatedUser(String authenticatedUser) { + auditEvent.authenticatedUser = authenticatedUser; + return this; + } + public AuditEventBuilder setCtl(String ctl) { auditEvent.ctl = ctl; return this; diff --git a/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java b/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java index e1cd07d8073a58..3920da0795c88f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java +++ b/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java @@ -155,6 +155,7 @@ private void fillLogBuffer(AuditEvent event, StringBuilder logBuffer) { // cs info appendField(logBuffer, event.clientIp); appendField(logBuffer, event.user); + appendField(logBuffer, event.authenticatedUser); appendField(logBuffer, event.feIp); // default ctl and db diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java index fc71c3f1a0e86d..d7bac2ee969110 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java @@ -50,6 +50,7 @@ import org.apache.doris.resource.workloadgroup.QueueToken; import org.apache.doris.service.FrontendOptions; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; @@ -220,6 +221,17 @@ private static int countValues(List children) { return cnt; } + /** + * The account that authenticated the connection: the switcher of a session-narrowed (SU) session, + * otherwise the session user itself. Never null, so the audit row always names who logged in. + */ + @VisibleForTesting + static String authenticatedUser(ConnectContext ctx) { + String user = ctx.getAuthenticatedIdentity() == null + ? ctx.getQualifiedUser() : ctx.getAuthenticatedIdentity().getQualifiedUser(); + return user == null ? "" : user; + } + private static void logAuditLogImpl(ConnectContext ctx, String origStmt, StatementBase parsedStmt, org.apache.doris.proto.Data.PQueryStatistics statistics, boolean printFuzzyVariables) { // slow query @@ -256,6 +268,7 @@ private static void logAuditLogImpl(ConnectContext ctx, String origStmt, Stateme .setTimestamp(ctx.getStartTime()) .setClientIp(ctx.getClientIP()) .setUser(ctx.getQualifiedUser()) + .setAuthenticatedUser(authenticatedUser(ctx)) .setFeIp(FrontendOptions.getLocalHostAddress()) .setCtl(catalog == null ? InternalCatalog.INTERNAL_CATALOG_NAME : catalog.getName()) .setDb(ctx.getDatabase()) diff --git a/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java index 21590db8fd3181..2da404bc95b528 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java @@ -122,6 +122,27 @@ public void testDelimiterInjectionDoesNotAlterFraming() { Assertions.assertTrue(evil.toString().contains("DROP TABLE finance.ledger")); } + // The row carries exactly one field per audit_log column, in InternalSchema.AUDIT_SCHEMA order + // (the loader's `columns` header maps by that order), and the account that authenticated the + // connection rides right beside the effective user. + @Test + public void testAuthenticatedUserRidesBesideUser() { + AuditLoader auditLoader = new AuditLoader(); + StringBuilder buffer = new StringBuilder(); + Deencapsulation.invoke(auditLoader, "fillLogBuffer", + new AuditEvent.AuditEventBuilder() + .setClientIp("10.0.0.9").setUser("alice").setAuthenticatedUser("svc_gateway") + .setDb("sales").setStmt("select 1").build(), + buffer); + List names = InternalSchema.AUDIT_SCHEMA.stream().map(ColumnDef::getName) + .collect(Collectors.toList()); + String[] fields = buffer.toString().split(String.valueOf(AuditLoader.AUDIT_TABLE_COL_SEPARATOR), -1); + Assert.assertEquals("one field per audit_log column", names.size(), fields.length); + Assert.assertEquals(names.indexOf("user") + 1, names.indexOf("authenticated_user")); + Assert.assertEquals("alice", fields[names.indexOf("user")]); + Assert.assertEquals("svc_gateway", fields[names.indexOf("authenticated_user")]); + } + // The sanitizer must be a no-op for ordinary statements: no data loss, no mutation. @Test public void testCleanStatementIsPreserved() { diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java index 1eb6eb4791f20b..003ad2d9301a9e 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java @@ -110,4 +110,27 @@ public void testGetCloudClusterForAuditPrefersEffectiveCluster() throws Exceptio Assertions.assertEquals("hint_cluster", AuditLogHelper.getCloudClusterForAudit(ctx)); } + + @Test + public void testAuthenticatedUserFollowsTheSwitcher() { + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("alice", "%")); + // an ordinary session: the account that authenticated is the session user + Assert.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + + // a session-narrowed (SU) session: the effective identity is the target, the switcher stays + // the authenticated account -- the audit row names both + ctx.setAuthenticatedIdentity(ctx.getCurrentUserIdentity()); + ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("bob", "%")); + Assert.assertEquals("bob", ctx.getQualifiedUser()); + Assert.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + + // reverting the switch restores the plain mapping + ctx.revertSessionNarrowing(); + Assert.assertEquals("alice", ctx.getQualifiedUser()); + Assert.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + + // never null, even before any identity is set + Assert.assertEquals("", AuditLogHelper.authenticatedUser(new ConnectContext())); + } } From 6ac650e269c32a54d3484b028f9b036ee79c8008 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 8 Sep 2026 16:44:58 +0530 Subject: [PATCH 04/30] [feature](auth) SU: gate the switch on a native PROXY_PRIV; the ceiling is the target's granted roles Replace the FE-config stand-ins the switch shipped with. The gate becomes a grantable privilege in MySQL's own vocabulary: PROXY_PRIV (bit 15, GLOBAL scope, implied by ADMIN_PRIV, grantable and revocable only on *.*.* like ADMIN_PRIV and NODE_PRIV, listed by SHOW GRANTS like any other privilege). The role ceiling is always the target user's granted roles -- the session can never exceed the person's real authority -- so the switcher-side ceiling option goes. The dormant role pattern goes too: with the role list mandatory the service is already bounded by what the person holds, and a role the person holds is one they may use directly. Removed: Config.switch_user_users, switch_user_role_ceiling, su_only_roles_pattern and Auth.isSuOnlyRole. Auth.getGrantedRoleNamesRaw is now getGrantedRoleNames. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Raghvendra Singh --- .../java/org/apache/doris/common/Config.java | 16 ---- .../apache/doris/catalog/AccessPrivilege.java | 7 +- .../apache/doris/mysql/privilege/Auth.java | 46 ++--------- .../doris/mysql/privilege/PrivPredicate.java | 3 + .../doris/mysql/privilege/Privilege.java | 7 +- .../commands/GrantTablePrivilegeCommand.java | 5 +- .../trees/plans/commands/SuUserCommand.java | 66 +++++----------- .../mysql/privilege/SuUserNarrowingTest.java | 76 ++++--------------- 8 files changed, 59 insertions(+), 167 deletions(-) diff --git a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java index 7e080a1daf73b6..3d1cecbf2d3929 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java +++ b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java @@ -3016,22 +3016,6 @@ public void handle(Field field, String value) throws Exception { options = {"default", "ranger-doris"}) public static String access_controller_type = "default"; - @ConfField(mutable = true, masterOnly = false, description = - "Comma-separated list of non-ADMIN qualified users allowed to execute SU " - + "(session-narrowed identity switch). Empty = ADMIN only. Per-FE; set on every FE.") - public static String switch_user_users = ""; - - @ConfField(mutable = true, masterOnly = false, description = - "Whose granted roles bound the SU role override: 'target' (default, the switched-to user's " - + "granted roles, pure narrowing) or 'switcher' (the switching account's granted roles).") - public static String switch_user_role_ceiling = "target"; - - @ConfField(mutable = true, masterOnly = false, description = - "Roles matching this regex are DORMANT in normal sessions (excluded from privilege checks and " - + "row-policy matching for the session's own identity) and activate only when explicitly requested " - + "via SU. Empty = disabled. Invalid regex = disabled (logged). Per-FE; set on every FE.") - public static String su_only_roles_pattern = ""; - /* https://forums.oracle.com/ords/apexds/post/je-log-checksumexception-2812 when meeting disk damage or other reason described in the oracle forums and fe cannot start due to `com.sleepycat.je.log.ChecksumException`, we diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java index dba702de95debb..6dfd3c06c1a778 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java @@ -39,7 +39,8 @@ public enum AccessPrivilege { DROP_PRIV(10, "Privilege for dropping database or table"), ADMIN_PRIV(11, "All privileges except NODE_PRIV"), USAGE_PRIV(12, "Privilege for use resource"), - SHOW_VIEW_PRIV(13, "Privilege for show view"); + SHOW_VIEW_PRIV(13, "Privilege for show view"), + PROXY_PRIV(14, "Privilege for switching the session to another user"); private int flag; private String desc; @@ -50,7 +51,7 @@ private AccessPrivilege(int flag, String desc) { } public List toDorisPrivilege() { - Preconditions.checkState(flag > 0 && flag < 14); + Preconditions.checkState(flag > 0 && flag < 15); switch (flag) { case 1: case 6: @@ -78,6 +79,8 @@ public List toDorisPrivilege() { return Lists.newArrayList(Privilege.USAGE_PRIV); case 13: return Lists.newArrayList(Privilege.SHOW_VIEW_PRIV); + case 14: + return Lists.newArrayList(Privilege.PROXY_PRIV); default: return null; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index 88a4dbb0d10c48..12d1c3359667bc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -359,8 +359,7 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { Set sessionRoleOverride = ctx.getSessionRoleOverride(); if (sessionRoleOverride != null) { // SU-narrowed session: the override REPLACES every role source (user grants, LDAP, - // authenticated roles). SuUserCommand enforced the ceiling at switch time. - // SU-narrowed session: the override REPLACES every role source; return the + // authenticated roles); SuUserCommand enforced the ceiling at switch time. Return the // override roles + the information_schema/mysql read baseline (see narrowedRoleSet). return narrowedRoleSet(sessionRoleOverride); } @@ -370,42 +369,15 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { roles.add(role); } } - // Dormant (SU-only) roles never activate in a normal session for the session's OWN - // identity: excluded here so grants AND row policies stay inert until an SU switch - // explicitly requests them. Introspection of OTHER identities is unaffected (guard above). - roles.removeIf(role -> isSuOnlyRole(role.getRoleName())); } return roles; } - private static volatile String suOnlyPatternSource = null; - private static volatile java.util.regex.Pattern suOnlyPattern = null; - - /** Whether a role is dormant outside SU sessions (Config.su_only_roles_pattern; invalid/empty = never). */ - public static boolean isSuOnlyRole(String roleName) { - String src = Config.su_only_roles_pattern; - if (src == null || src.isEmpty()) { - return false; - } - java.util.regex.Pattern p = suOnlyPattern; - if (p == null || !src.equals(suOnlyPatternSource)) { - try { - p = java.util.regex.Pattern.compile(src); - } catch (Exception e) { - LOG.error("invalid su_only_roles_pattern '{}' — dormant-role gating DISABLED", src, e); - p = null; - } - suOnlyPattern = p; - suOnlyPatternSource = src; - } - return p != null && p.matcher(roleName).find(); - } - /** - * RAW granted role names for an identity (user grants + LDAP), bypassing session shaping - * (SU override and dormant filtering). Used for the SU ceiling check and existence probing. + * The role names granted to an identity (user grants + LDAP), ignoring any SU narrowing of the + * current session. Used for the SU ceiling check and for existence probing. */ - public Set getGrantedRoleNamesRaw(UserIdentity userIdentity) { + public Set getGrantedRoleNames(UserIdentity userIdentity) { Set names = Sets.newHashSet(userRoleManager.getRolesByUser(userIdentity)); if (isLdapAuthEnabled()) { Set ldapRoles = ldapManager.getUserRoles(userIdentity.getQualifiedUser()); @@ -451,12 +423,11 @@ private static Role narrowingBaselineRole() { } /** - * The role set consulted for WORKLOAD GROUP privilege checks. /** * The role set consulted for WORKLOAD GROUP privilege checks. Identical to * {@link #getRolesByUserWithLdap} for a normal session. In an SU-narrowed session the * narrowed set is widened with the effective identity's own granted roles (its default - * role (where direct grants live) plus explicit and LDAP roles; dormant roles stay - * excluded unless requested), so the narrowed session may use exactly the workload groups + * role (where direct grants live) plus explicit and LDAP roles), so the narrowed session + * may use exactly the workload groups * the person's own session could use. A workload group is placement, not data authority: * the narrowed session runs in the target's default_workload_group (the same resolution as * the person's own session), and the person's USAGE on it is typically a direct grant that @@ -472,10 +443,7 @@ private Set getRolesForWorkloadGroupCheck(UserIdentity userIdentity) { return roles; } Set widened = Sets.newHashSet(roles); - for (String roleName : getGrantedRoleNamesRaw(userIdentity)) { - if (isSuOnlyRole(roleName)) { - continue; - } + for (String roleName : getGrantedRoleNames(userIdentity)) { Role role = roleManager.getRole(roleName); if (role != null) { widened.add(role); diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java index 2ba443a1b31bad..b1f9dc3c6828c9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java @@ -53,6 +53,9 @@ public class PrivPredicate { // admin user privs public static final PrivPredicate ADMIN = PrivPredicate.of(PrivBitSet.of(Privilege.ADMIN_PRIV), Operator.OR); + // switch the session to another user (SU): PROXY_PRIV, which ADMIN_PRIV implies + public static final PrivPredicate PROXY = PrivPredicate.of(PrivBitSet.of(Privilege.ADMIN_PRIV, + Privilege.PROXY_PRIV), Operator.OR); public static final PrivPredicate ADMIN_OR_NODE = PrivPredicate.of( PrivBitSet.of(Privilege.ADMIN_PRIV, Privilege.NODE_PRIV), diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java index 412430749bdd2d..c530a47f8949ee 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java @@ -48,7 +48,8 @@ public enum Privilege { CLUSTER_USAGE_PRIV("Cluster_usage_priv", 12, "Privilege for using cluster", "RESOURCE"), // 13 placeholder for stage STAGE_USAGE_PRIV("Stage_usage_priv", 13, "Privilege for using stage", "RESOURCE"), - SHOW_VIEW_PRIV("Show_view_priv", 14, "Privilege for show create view", "GLOBAL,CATALOG,DATABASE,TABLE"); + SHOW_VIEW_PRIV("Show_view_priv", 14, "Privilege for show create view", "GLOBAL,CATALOG,DATABASE,TABLE"), + PROXY_PRIV("Proxy_priv", 15, "Privilege for switching the session to another user (SU)", "GLOBAL"); public static final Map privileges; @@ -73,6 +74,7 @@ public enum Privilege { privileges.put(12, CLUSTER_USAGE_PRIV); privileges.put(13, STAGE_USAGE_PRIV); privileges.put(14, SHOW_VIEW_PRIV); + privileges.put(15, PROXY_PRIV); } @@ -86,6 +88,7 @@ public enum Privilege { CREATE_PRIV, DROP_PRIV, SHOW_VIEW_PRIV, + PROXY_PRIV, }; // only GRANT_PRIV and USAGE_PRIV can grant on workloadGroup @@ -98,6 +101,7 @@ public enum Privilege { CREATE_PRIV, DROP_PRIV, SHOW_VIEW_PRIV, + PROXY_PRIV, }; public static final Privilege[] notBelongToTablePrivileges = { @@ -116,6 +120,7 @@ public enum Privilege { .put(USAGE_PRIV, "USAGE") .put(CLUSTER_USAGE_PRIV, "USAGE") .put(SHOW_VIEW_PRIV, "SHOW VIEW") + .put(PROXY_PRIV, "PROXY") .build(); private String name; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java index 0672cf57be57ae..b560e6cbcc049a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java @@ -142,8 +142,9 @@ public static void checkTablePrivileges(Collection privileges, TableP Privilege.checkIncorrectPrivilege(Privilege.notBelongToTablePrivileges, privileges); // Rule 2 if (tblPattern.getPrivLevel() != Auth.PrivLevel.GLOBAL && (privileges.contains(Privilege.ADMIN_PRIV) - || privileges.contains(Privilege.NODE_PRIV))) { - throw new AnalysisException("ADMIN_PRIV and NODE_PRIV can only be granted/revoke on/from *.*.*"); + || privileges.contains(Privilege.NODE_PRIV) || privileges.contains(Privilege.PROXY_PRIV))) { + throw new AnalysisException( + "ADMIN_PRIV, NODE_PRIV and PROXY_PRIV can only be granted/revoke on/from *.*.*"); } // Rule 3 diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java index bfe8f8fea15dd9..691112b06cdc21 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java @@ -21,7 +21,6 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; -import org.apache.doris.common.Config; import org.apache.doris.common.FeNameFormat; import org.apache.doris.mysql.privilege.Auth; import org.apache.doris.mysql.privilege.PrivPredicate; @@ -43,22 +42,19 @@ /** * SU 'user'[@'host'] WITH ROLES ('role1'[, ...]) [WORKLOAD GROUP 'wg'] * - *

Session-narrowed identity switch: the session becomes the TARGET identity (audit, - * current_user(), user-bound row policies, user properties) with a role set that REPLACES - * every role source — enforced at the single choke point {@code Auth.getRolesByUserWithLdap}. - * Session-only state; nothing is persisted. + *

Session-narrowed identity switch, MySQL's proxy-user model with a mandatory role list: the + * session becomes the TARGET identity (audit, current_user(), user-bound row policies, user + * properties) with a role set that REPLACES every role source, enforced at the single choke point + * {@code Auth.getRolesByUserWithLdap}. Session-only state; nothing is persisted. * - *

Fail-closed contract: + *

Contract: *

    - *
  • Allowed only for ADMIN or accounts listed in {@code Config.switch_user_users}; a session - * that skips SU keeps the switching account's own (minimal) grants.
  • - *
  • NARROWING-ONLY LAW: every requested role must already be GRANTED to the ceiling identity — - * the target user ({@code switch_user_role_ceiling=target}, default; the session can never - * exceed the person's real authority) or the switching account ({@code =switcher}).
  • - *
  • An SU'd session can never SU again, and no SET ROLE exists to widen it. Connection reset + *
  • Allowed only for accounts holding {@code PROXY_PRIV} (or {@code ADMIN_PRIV}, which implies + * it); a session that does not switch keeps the switching account's own grants.
  • + *
  • Narrowing only: every requested role must already be GRANTED to the target user, so the + * session can never exceed the person's real authority.
  • + *
  • One-shot: a switched session cannot SU again, and nothing widens it. A connection reset * reverts to the authenticated identity ({@link ConnectContext#revertSessionNarrowing}).
  • - *
  • Roles matching {@code Config.su_only_roles_pattern} are dormant in normal sessions and - * activate only through this command (see {@code Auth.isSuOnlyRole}).
  • *
*/ public class SuUserCommand extends Command implements NoForward { @@ -78,16 +74,12 @@ public SuUserCommand(UserIdentity userIdentity, List roles, String workl @Override public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { - // Re-SU refusal: a narrowed session must never widen or re-target itself. + // a switched session must never widen or re-target itself if (ctx.getAuthenticatedIdentity() != null) { throw new AnalysisException("SU is not allowed in an already-switched session"); } - // Gate: ADMIN, or explicitly listed switcher accounts (per-FE config). - boolean isAdmin = Env.getCurrentEnv().getAccessManager() - .checkGlobalPriv(ctx, PrivPredicate.ADMIN); - if (!isAdmin && !isListedSwitcher(ctx.getQualifiedUser())) { - throw new AnalysisException("Access denied: SU requires ADMIN or membership in " - + "the switch_user_users FE config"); + if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.PROXY)) { + throw new AnalysisException("Access denied: SU requires PROXY_PRIV (or ADMIN_PRIV)"); } userIdentity.analyze(); @@ -96,22 +88,16 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { } Auth auth = Env.getCurrentEnv().getAuth(); - Set targetRoles = auth.getGrantedRoleNamesRaw(userIdentity); + Set targetRoles = auth.getGrantedRoleNames(userIdentity); if (targetRoles.isEmpty()) { // every existing user holds at least its default role throw new AnalysisException("SU target user does not exist: " + userIdentity); } - // NARROWING-ONLY LAW: requested roles must be a subset of the ceiling identity's - // RAW granted roles (dormant roles count as granted — SU is their only activation). - boolean switcherCeiling = "switcher".equalsIgnoreCase(Config.switch_user_role_ceiling); - Set ceiling = switcherCeiling - ? auth.getGrantedRoleNamesRaw(ctx.getCurrentUserIdentity()) - : targetRoles; + // narrowing only: the requested roles must be a subset of the target's granted roles for (String role : roles) { - if (!ceiling.contains(role)) { - throw new AnalysisException("SU role '" + role + "' is not granted to the " - + (switcherCeiling ? "switching account" : "target user") - + " — SU can only narrow, never mint authority"); + if (!targetRoles.contains(role)) { + throw new AnalysisException("SU role '" + role + "' is not granted to the target user" + + " -- SU can only narrow, never mint authority"); } } @@ -126,25 +112,13 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { // (Auth.getRolesForWorkloadGroupCheck): the person's lane follows the person. ctx.getSessionVariable().setWorkloadGroup(workloadGroup); } - // The SU linkage event: authenticated + effective identity, roles, connection — the - // statement itself also lands in the audit log under the effective user. + // the switch itself: authenticated + effective identity, roles, connection -- the + // statement also lands in the audit log under the effective user LOG.info("SU: connection {} narrowed. authenticated={} effective={} roles={} workloadGroup={}", ctx.getConnectionId(), authenticated, userIdentity, roles, workloadGroup == null ? "" : workloadGroup); } - private static boolean isListedSwitcher(String qualifiedUser) { - if (Strings.isNullOrEmpty(Config.switch_user_users) || Strings.isNullOrEmpty(qualifiedUser)) { - return false; - } - for (String allowed : Config.switch_user_users.split(",")) { - if (qualifiedUser.equals(allowed.trim())) { - return true; - } - } - return false; - } - @Override public R accept(PlanVisitor visitor, C context) { return visitor.visitCommand(this, context); diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index 3923f025905509..f727ab16d51326 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -20,7 +20,6 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; -import org.apache.doris.common.Config; import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.nereids.parser.NereidsParser; import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand; @@ -30,7 +29,6 @@ import org.apache.doris.qe.ConnectContext; import org.apache.doris.utframe.TestWithFeService; -import com.google.common.collect.Sets; import org.junit.Assert; import org.junit.jupiter.api.Test; @@ -42,10 +40,10 @@ * (a) a narrowed session loses the target's personal grants (override REPLACES the role union); * (c) re-SU is refused; (d) a session that skips SU keeps only the switcher's own grants; * (f) narrowing never leaks into checks of OTHER identities; (g) the narrowed session keeps the - * person's own WORKLOAD GROUP usage (placement follows the person; never wider than her own - * session); plus the dormant (SU-only) role gating, both ceiling modes, the config gate, and - * the parser round trip. - * End-to-end row-policy + audit coverage runs on the scratch rig (gate battery), not here. + * person's own WORKLOAD GROUP usage (placement follows the person; never wider than their own + * session); plus the PROXY_PRIV gate, the target-grants ceiling, the parser round trip and the + * no-ConnectContext metadata-RPC path. + * End-to-end row-policy + audit coverage belongs to the regression suite, not here. */ public class SuUserNarrowingTest extends TestWithFeService { @@ -129,40 +127,6 @@ public void testOverrideReplacesRoleUnionAndDoesNotLeak() throws Exception { } } - @Test - public void testDormantRolesInertUntilRequested() throws Exception { - addUser("dorm_user", true); - createRole("space_dorm"); - grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'space_dorm';"); - grantRole("GRANT 'space_dorm' TO 'dorm_user'@'%'"); - grantPriv("GRANT SELECT_PRIV ON internal.perso2.* TO 'dorm_user'@'%';"); - - UserIdentity user = ident("dorm_user"); - ConnectContext ctx = new ConnectContext(); - ctx.setCurrentUserIdentity(user); - ctx.setThreadLocalInfo(); - String savedPattern = Config.su_only_roles_pattern; - try { - Config.su_only_roles_pattern = "^space_"; - // dormant in a normal session: the role's grants are inert, personal grants live - Assert.assertFalse(canSelectDb(user, "test")); - Assert.assertTrue(canSelectDb(user, "perso2")); - // the RAW view still carries it (the SU ceiling and admin introspection see it) - Assert.assertTrue(Env.getCurrentEnv().getAuth() - .getGrantedRoleNamesRaw(user).contains("space_dorm")); - // explicit activation via the override path - ctx.setSessionRoleOverride(Collections.singleton("space_dorm")); - Assert.assertTrue(canSelectDb(user, "test")); - // invalid pattern degrades to disabled (logged), never to an exception - ctx.setSessionRoleOverride(null); - Config.su_only_roles_pattern = "([bad"; - Assert.assertTrue(canSelectDb(user, "test")); - } finally { - Config.su_only_roles_pattern = savedPattern; - connectContext.setThreadLocalInfo(); - } - } - @Test public void testSuCommandContract() throws Exception { addUser("svc", true); @@ -176,17 +140,19 @@ public void testSuCommandContract() throws Exception { ConnectContext ctx = new ConnectContext(); ctx.setCurrentUserIdentity(ident("svc")); ctx.setThreadLocalInfo(); - String savedUsers = Config.switch_user_users; - String savedCeiling = Config.switch_user_role_ceiling; try { - // gate: neither ADMIN nor listed -> refused; (d) the session stays the service account + // gate: neither ADMIN_PRIV nor PROXY_PRIV -> refused; (d) the session stays the service account Assert.assertThrows(AnalysisException.class, () -> new SuUserCommand(new UserIdentity("bobby", "%"), Collections.singletonList("r_bobby"), null).run(ctx, null)); Assert.assertNull(ctx.getAuthenticatedIdentity()); Assert.assertFalse(canSelectDb(ident("svc"), "test")); - Config.switch_user_users = ctx.getQualifiedUser(); + // PROXY_PRIV is the native gate (ADMIN_PRIV implies it), and it is global-only + Assert.assertThrows(Exception.class, + () -> grantPriv("GRANT PROXY_PRIV ON internal.test.* TO 'svc'@'%';")); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'svc'@'%';"); + Assert.assertTrue(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.PROXY)); // nonexistent target refused Assert.assertThrows(AnalysisException.class, () -> @@ -216,14 +182,12 @@ public void testSuCommandContract() throws Exception { Assert.assertEquals("svc", ctx.getCurrentUserIdentity().getQualifiedUser()); Assert.assertNull(ctx.getSessionRoleOverride()); - // switcher ceiling: a role granted to the SERVICE, not the target - Config.switch_user_role_ceiling = "switcher"; - new SuUserCommand(new UserIdentity("bobby", "%"), - Collections.singletonList("r_svc_only"), null).run(ctx, null); - Assert.assertEquals(Collections.singleton("r_svc_only"), ctx.getSessionRoleOverride()); + // the ceiling is the TARGET's grants: a role the service holds but the target does not is refused + Assert.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_svc_only"), null).run(ctx, null)); + Assert.assertNull(ctx.getSessionRoleOverride()); } finally { - Config.switch_user_users = savedUsers; - Config.switch_user_role_ceiling = savedCeiling; connectContext.setThreadLocalInfo(); } } @@ -248,7 +212,6 @@ public void testNarrowedSessionKeepsPersonsWorkloadGroupUsage() throws Exception ConnectContext ctx = new ConnectContext(); ctx.setCurrentUserIdentity(person); ctx.setThreadLocalInfo(); - String savedPattern = Config.su_only_roles_pattern; try { // the person's own session Assert.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); @@ -266,18 +229,9 @@ public void testNarrowedSessionKeepsPersonsWorkloadGroupUsage() throws Exception // never wider than the person's own session Assert.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); - // a DORMANT role's USAGE stays inert unless the switch requested it - Config.su_only_roles_pattern = "^space_wg_lane$"; - Assert.assertFalse(canUseWorkloadGroup(person, "wg_via_role")); - Assert.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); - ctx.setSessionRoleOverride(Sets.newHashSet("space_wg", "space_wg_lane")); - Assert.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); - // the widening is scoped to the session's OWN identity: another identity's check is untouched - ctx.setSessionRoleOverride(Collections.singleton("space_wg")); Assert.assertFalse(canUseWorkloadGroup(ident("bystander_wg"), "wg_person_lane")); } finally { - Config.su_only_roles_pattern = savedPattern; ctx.setSessionRoleOverride(null); connectContext.setThreadLocalInfo(); } From b3d0767caed9542c6ae02494f163bab8da9c9dd7 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 8 Sep 2026 16:48:59 +0530 Subject: [PATCH 05/30] [test](auth) SU: regression suite for the session-narrowed identity switch A service account holding PROXY_PRIV switches a session to a person restricted to one of the person's roles: the requested role's grant works, the person's direct grant is inert, name visibility (SHOW DATABASES, information_schema.tables) follows the narrowed set, the switch is one-shot, a role the person does not hold is refused, an account without PROXY_PRIV cannot switch, PROXY_PRIV is global-only and listed by SHOW GRANTS, and the person's own login is unaffected. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Raghvendra Singh --- .../auth_p0/test_su_session_narrowing.groovy | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 regression-test/suites/auth_p0/test_su_session_narrowing.groovy diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy new file mode 100644 index 00000000000000..eadbadc2e2ded6 --- /dev/null +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -0,0 +1,140 @@ +// 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. + +// SU 'user' WITH ROLES (...): a session-narrowed identity switch (MySQL's proxy-user model with a +// mandatory role list). A service account holding PROXY_PRIV runs a session as a person, restricted +// to a subset of the person's granted roles; Doris enforces the narrowed privileges, the person's +// direct grants are inert under the switch, name visibility follows the narrowed set, the switch is +// one-shot, and the same person's own login is unaffected. +suite("test_su_session_narrowing", "p0,auth") { + String svc = 'su_narrow_svc' + String person = 'su_narrow_person' + String pwd = 'C123_567p' + String tenantRole = 'su_narrow_tenant' + String otherRole = 'su_narrow_other' + String roleDb = 'su_narrow_role_db' + String ownDb = 'su_narrow_own_db' + + try_sql("DROP USER ${svc}") + try_sql("DROP USER ${person}") + try_sql("DROP ROLE ${tenantRole}") + try_sql("DROP ROLE ${otherRole}") + sql """DROP DATABASE IF EXISTS ${roleDb}""" + sql """DROP DATABASE IF EXISTS ${ownDb}""" + + sql """CREATE DATABASE ${roleDb}""" + sql """CREATE DATABASE ${ownDb}""" + sql """CREATE TABLE ${roleDb}.t (k INT) DISTRIBUTED BY HASH(k) BUCKETS 1 + PROPERTIES ('replication_num' = '1')""" + sql """CREATE TABLE ${ownDb}.t (k INT) DISTRIBUTED BY HASH(k) BUCKETS 1 + PROPERTIES ('replication_num' = '1')""" + sql """INSERT INTO ${roleDb}.t VALUES (1), (2)""" + sql """INSERT INTO ${ownDb}.t VALUES (1)""" + + sql """CREATE ROLE ${tenantRole}""" + sql """CREATE ROLE ${otherRole}""" + sql """CREATE USER '${person}' IDENTIFIED BY '${pwd}'""" + sql """CREATE USER '${svc}' IDENTIFIED BY '${pwd}'""" + // the person: a tenant role on one database, a direct grant on another + sql """GRANT SELECT_PRIV ON ${roleDb}.* TO ROLE '${tenantRole}'""" + sql """GRANT '${tenantRole}' TO '${person}'@'%'""" + sql """GRANT SELECT_PRIV ON ${ownDb}.* TO '${person}'@'%'""" + // the service account: nothing but the right to switch + sql """GRANT PROXY_PRIV ON *.*.* TO '${svc}'@'%'""" + + try { + // PROXY_PRIV is a global privilege, listed like any other + test { + sql """GRANT PROXY_PRIV ON ${roleDb}.* TO '${svc}'@'%'""" + exception "PROXY_PRIV" + } + def grants = sql """SHOW GRANTS FOR '${svc}'@'%'""" + assertTrue(grants.toString().contains("Proxy_priv")) + + // without PROXY_PRIV (or ADMIN_PRIV) the switch is refused + connect(person, pwd, context.config.jdbcUrl) { + test { + sql """SU '${person}'@'%' WITH ROLES ('${tenantRole}')""" + exception "PROXY_PRIV" + } + } + + // the ceiling is the target's granted roles: a role the person does not hold is refused + connect(svc, pwd, context.config.jdbcUrl) { + test { + sql """SU '${person}'@'%' WITH ROLES ('${otherRole}')""" + exception "not granted" + } + } + + connect(svc, pwd, context.config.jdbcUrl) { + // before the switch the service account sees nothing of the person's + test { + sql """SELECT * FROM ${roleDb}.t""" + exception "denied" + } + + sql """SU '${person}'@'%' WITH ROLES ('${tenantRole}')""" + + // the session is the person, narrowed to the requested role + def who = sql """SELECT current_user()""" + assertTrue(who[0][0].toString().contains(person)) + def narrowed = sql """SELECT CAST(session_is_narrowed() AS INT)""" + assertEquals(1, narrowed[0][0] as int) + + // the requested role's grant works ... + def rows = sql """SELECT COUNT(*) FROM ${roleDb}.t""" + assertEquals(2, rows[0][0] as int) + // ... the person's direct grant is inert under the switch + test { + sql """SELECT * FROM ${ownDb}.t""" + exception "denied" + } + + // name visibility follows the narrowed set, on the FE and through the BE scanners + def dbs = sql """SHOW DATABASES LIKE '${ownDb}'""" + assertEquals(0, dbs.size()) + def visibleRole = sql """SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${roleDb}'""" + assertEquals(1, visibleRole[0][0] as int) + def visibleOwn = sql """SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${ownDb}'""" + assertEquals(0, visibleOwn[0][0] as int) + + // one-shot: a switched session cannot switch again, not even to itself + test { + sql """SU '${svc}'@'%' WITH ROLES ('${tenantRole}')""" + exception "already-switched" + } + } + + // the person's own login is untouched: both grants live, nothing is narrowed + connect(person, pwd, context.config.jdbcUrl) { + def own = sql """SELECT COUNT(*) FROM ${ownDb}.t""" + assertEquals(1, own[0][0] as int) + def role = sql """SELECT COUNT(*) FROM ${roleDb}.t""" + assertEquals(2, role[0][0] as int) + def narrowed = sql """SELECT CAST(session_is_narrowed() AS INT)""" + assertEquals(0, narrowed[0][0] as int) + } + } finally { + try_sql("DROP USER ${svc}") + try_sql("DROP USER ${person}") + try_sql("DROP ROLE ${tenantRole}") + try_sql("DROP ROLE ${otherRole}") + try_sql("DROP DATABASE IF EXISTS ${roleDb}") + try_sql("DROP DATABASE IF EXISTS ${ownDb}") + } +} From 6d5d828bc0cd9cf8a733f88470996a805b2e43fa Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 8 Sep 2026 17:13:25 +0530 Subject: [PATCH 06/30] [test](auth) SU tests: JUnit 5 assertions, and two test fixes The FE build rejects JUnit 3/4 in tests; the SU and audit tests used org.junit.Assert. Switch them to org.junit.jupiter.api.Assertions (the one message argument moves to the end of its call). Two fixes found by running them: the contract test installs the service account's ConnectContext as the thread-local before exercising the gate, so its GRANT PROXY_PRIV was checked against the service account -- grant as the admin session; and the regression suite grants the framework's default database to both test accounts, since every connection carries it and Doris refuses a connection without a privilege on it. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Raghvendra Singh --- .../mysql/privilege/SuUserNarrowingTest.java | 116 +++++++++--------- .../doris/plugin/audit/AuditLoaderTest.java | 8 +- .../apache/doris/qe/AuditLogHelperTest.java | 12 +- .../qe/ForwardedSessionNarrowingTest.java | 32 ++--- .../auth_p0/test_su_session_narrowing.groovy | 3 + 5 files changed, 90 insertions(+), 81 deletions(-) diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index f727ab16d51326..bcd506abcb03bf 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -29,7 +29,7 @@ import org.apache.doris.qe.ConnectContext; import org.apache.doris.utframe.TestWithFeService; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -77,13 +77,13 @@ private boolean canUseWorkloadGroup(UserIdentity user, String wg) { private void createWorkloadGroup(String name) throws Exception { LogicalPlan plan = new NereidsParser().parseSingle("CREATE WORKLOAD GROUP IF NOT EXISTS " + name + " PROPERTIES ('min_memory_percent'='10', 'max_memory_percent'='30%')"); - Assert.assertTrue(plan instanceof CreateWorkloadGroupCommand); + Assertions.assertTrue(plan instanceof CreateWorkloadGroupCommand); ((CreateWorkloadGroupCommand) plan).run(connectContext, null); } private void grantWorkloadGroupUsage(String sql) throws Exception { LogicalPlan plan = new NereidsParser().parseSingle(sql); - Assert.assertTrue(plan instanceof GrantResourcePrivilegeCommand); + Assertions.assertTrue(plan instanceof GrantResourcePrivilegeCommand); ((GrantResourcePrivilegeCommand) plan).run(connectContext, null); } @@ -105,23 +105,23 @@ public void testOverrideReplacesRoleUnionAndDoesNotLeak() throws Exception { ctx.setThreadLocalInfo(); try { // un-narrowed: personal + role grants both live - Assert.assertTrue(canSelectDb(alice, "perso")); - Assert.assertTrue(canSelectDb(alice, "test")); + Assertions.assertTrue(canSelectDb(alice, "perso")); + Assertions.assertTrue(canSelectDb(alice, "test")); ctx.setSessionRoleOverride(Collections.singleton("space_a")); // (a) the override REPLACES everything: the personal grant is gone... - Assert.assertFalse(canSelectDb(alice, "perso")); + Assertions.assertFalse(canSelectDb(alice, "perso")); // ...while the requested role's grants remain - Assert.assertTrue(canSelectDb(alice, "test")); + Assertions.assertTrue(canSelectDb(alice, "test")); // (f) checks against OTHER identities are untouched by this session's narrowing Set bystanderRoles = Env.getCurrentEnv().getAuth() .getRoleNamesByUserWithLdap(ident("bystander"), false); - Assert.assertTrue(bystanderRoles.contains("bystander_r")); + Assertions.assertTrue(bystanderRoles.contains("bystander_r")); // revert restores the full union ctx.setSessionRoleOverride(null); - Assert.assertTrue(canSelectDb(alice, "perso")); + Assertions.assertTrue(canSelectDb(alice, "perso")); } finally { connectContext.setThreadLocalInfo(); } @@ -142,51 +142,57 @@ public void testSuCommandContract() throws Exception { ctx.setThreadLocalInfo(); try { // gate: neither ADMIN_PRIV nor PROXY_PRIV -> refused; (d) the session stays the service account - Assert.assertThrows(AnalysisException.class, () -> + Assertions.assertThrows(AnalysisException.class, () -> new SuUserCommand(new UserIdentity("bobby", "%"), Collections.singletonList("r_bobby"), null).run(ctx, null)); - Assert.assertNull(ctx.getAuthenticatedIdentity()); - Assert.assertFalse(canSelectDb(ident("svc"), "test")); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertFalse(canSelectDb(ident("svc"), "test")); - // PROXY_PRIV is the native gate (ADMIN_PRIV implies it), and it is global-only - Assert.assertThrows(Exception.class, - () -> grantPriv("GRANT PROXY_PRIV ON internal.test.* TO 'svc'@'%';")); - grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'svc'@'%';"); - Assert.assertTrue(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.PROXY)); + // PROXY_PRIV is the native gate (ADMIN_PRIV implies it), and it is global-only; the grants + // themselves run as the test's admin session, not as the service account + connectContext.setThreadLocalInfo(); + try { + Assertions.assertThrows(Exception.class, + () -> grantPriv("GRANT PROXY_PRIV ON internal.test.* TO 'svc'@'%';")); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'svc'@'%';"); + } finally { + ctx.setThreadLocalInfo(); + } + Assertions.assertTrue(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.PROXY)); // nonexistent target refused - Assert.assertThrows(AnalysisException.class, () -> + Assertions.assertThrows(AnalysisException.class, () -> new SuUserCommand(new UserIdentity("ghost", "%"), Collections.singletonList("r_bobby"), null).run(ctx, null)); // NARROWING-ONLY LAW (default ceiling = target): role exists but is not bobby's - Assert.assertThrows(AnalysisException.class, () -> + Assertions.assertThrows(AnalysisException.class, () -> new SuUserCommand(new UserIdentity("bobby", "%"), Collections.singletonList("r_ungranted"), null).run(ctx, null)); // legal switch new SuUserCommand(new UserIdentity("bobby", "%"), Collections.singletonList("r_bobby"), "wg_probe").run(ctx, null); - Assert.assertEquals("bobby", ctx.getCurrentUserIdentity().getQualifiedUser()); - Assert.assertEquals(Collections.singleton("r_bobby"), ctx.getSessionRoleOverride()); - Assert.assertEquals("svc", ctx.getAuthenticatedIdentity().getQualifiedUser()); - Assert.assertEquals("wg_probe", ctx.getSessionVariable().getWorkloadGroup()); + Assertions.assertEquals("bobby", ctx.getCurrentUserIdentity().getQualifiedUser()); + Assertions.assertEquals(Collections.singleton("r_bobby"), ctx.getSessionRoleOverride()); + Assertions.assertEquals("svc", ctx.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertEquals("wg_probe", ctx.getSessionVariable().getWorkloadGroup()); // (c) re-SU refused in a switched session - Assert.assertThrows(AnalysisException.class, () -> + Assertions.assertThrows(AnalysisException.class, () -> new SuUserCommand(new UserIdentity("svc", "%"), Collections.singletonList("r_svc_only"), null).run(ctx, null)); // reset reverts to the AUTHENTICATED identity, never widens the target ctx.revertSessionNarrowing(); - Assert.assertEquals("svc", ctx.getCurrentUserIdentity().getQualifiedUser()); - Assert.assertNull(ctx.getSessionRoleOverride()); + Assertions.assertEquals("svc", ctx.getCurrentUserIdentity().getQualifiedUser()); + Assertions.assertNull(ctx.getSessionRoleOverride()); // the ceiling is the TARGET's grants: a role the service holds but the target does not is refused - Assert.assertThrows(AnalysisException.class, () -> + Assertions.assertThrows(AnalysisException.class, () -> new SuUserCommand(new UserIdentity("bobby", "%"), Collections.singletonList("r_svc_only"), null).run(ctx, null)); - Assert.assertNull(ctx.getSessionRoleOverride()); + Assertions.assertNull(ctx.getSessionRoleOverride()); } finally { connectContext.setThreadLocalInfo(); } @@ -214,23 +220,23 @@ public void testNarrowedSessionKeepsPersonsWorkloadGroupUsage() throws Exception ctx.setThreadLocalInfo(); try { // the person's own session - Assert.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); - Assert.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); - Assert.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); + Assertions.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); ctx.setSessionRoleOverride(Collections.singleton("space_wg")); // data narrowing is intact: personal grant gone, requested role live - Assert.assertFalse(canSelectDb(person, "perso")); - Assert.assertTrue(canSelectDb(person, "test")); + Assertions.assertFalse(canSelectDb(person, "perso")); + Assertions.assertTrue(canSelectDb(person, "test")); // (g) the direct USAGE survives narrowing — the person's lane follows the person - Assert.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); // ...and so does role-carried USAGE, even though that role was not requested - Assert.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); // never wider than the person's own session - Assert.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); + Assertions.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); // the widening is scoped to the session's OWN identity: another identity's check is untouched - Assert.assertFalse(canUseWorkloadGroup(ident("bystander_wg"), "wg_person_lane")); + Assertions.assertFalse(canUseWorkloadGroup(ident("bystander_wg"), "wg_person_lane")); } finally { ctx.setSessionRoleOverride(null); connectContext.setThreadLocalInfo(); @@ -252,19 +258,19 @@ public void testNarrowedSessionPreservesInformationSchemaButDropsDirectGrants() ctx.setThreadLocalInfo(); try { // un-narrowed: information_schema readable (default role), personal + role grants live - Assert.assertTrue(canSelectInfoSchema(iuser)); - Assert.assertTrue(canSelectDb(iuser, "perso")); + Assertions.assertTrue(canSelectInfoSchema(iuser)); + Assertions.assertTrue(canSelectDb(iuser, "perso")); ctx.setSessionRoleOverride(Collections.singleton("ispace")); // narrowed: the requested role's grant stays, the DIRECT/default-role grant is dropped... - Assert.assertTrue(canSelectDb(iuser, "test")); - Assert.assertFalse(canSelectDb(iuser, "perso")); + Assertions.assertTrue(canSelectDb(iuser, "test")); + Assertions.assertFalse(canSelectDb(iuser, "perso")); // ...but the implicit information_schema read IS PRESERVED (the narrowing baseline role), // so a narrowed session can still do metadata/client operations. - Assert.assertTrue(canSelectInfoSchema(iuser)); + Assertions.assertTrue(canSelectInfoSchema(iuser)); ctx.setSessionRoleOverride(null); - Assert.assertTrue(canSelectDb(iuser, "perso")); + Assertions.assertTrue(canSelectDb(iuser, "perso")); } finally { connectContext.setThreadLocalInfo(); } @@ -274,18 +280,18 @@ public void testNarrowedSessionPreservesInformationSchemaButDropsDirectGrants() public void testParserRoundTrip() { Object plan = new NereidsParser().parseSingle( "SU 'alice'@'%' WITH ROLES ('space_a', 'r2') WORKLOAD GROUP 'wg_space_5'"); - Assert.assertTrue(plan instanceof SuUserCommand); + Assertions.assertTrue(plan instanceof SuUserCommand); SuUserCommand cmd = (SuUserCommand) plan; - Assert.assertEquals(2, cmd.getRoles().size()); - Assert.assertEquals("wg_space_5", cmd.getWorkloadGroup()); + Assertions.assertEquals(2, cmd.getRoles().size()); + Assertions.assertEquals("wg_space_5", cmd.getWorkloadGroup()); Object plain = new NereidsParser().parseSingle("SU 'alice' WITH ROLES ('space_a')"); - Assert.assertTrue(plain instanceof SuUserCommand); - Assert.assertNull(((SuUserCommand) plain).getWorkloadGroup()); + Assertions.assertTrue(plain instanceof SuUserCommand); + Assertions.assertNull(((SuUserCommand) plain).getWorkloadGroup()); // `su` stays usable as an identifier (nonReserved) Object select = new NereidsParser().parseSingle("SELECT su FROM t1"); - Assert.assertNotNull(select); + Assertions.assertNotNull(select); } // SU narrowing: a BE->FE metadata RPC (getDbNames/getTableNames/listTableStatus/describeTables) @@ -304,18 +310,18 @@ public void testRpcSessionNarrowingAppliesOnMetadataRpcThread() throws Exception try { // no narrowing installed -> full roles resolved (the pre-fix behavior/leak): // both the personal grant and the role grant are visible. - Assert.assertTrue(canSelectDb(rpcuser, "perso")); - Assert.assertTrue(canSelectDb(rpcuser, "test")); + Assertions.assertTrue(canSelectDb(rpcuser, "perso")); + Assertions.assertTrue(canSelectDb(rpcuser, "test")); // install exactly what the metadata RPC carries in current_roles Auth.setRpcSessionNarrowing(rpcuser, Collections.singleton("rpc_space")); - Assert.assertFalse(canSelectDb(rpcuser, "perso")); // personal grant DROPPED - Assert.assertTrue(canSelectDb(rpcuser, "test")); // requested role KEPT - Assert.assertTrue(canSelectInfoSchema(rpcuser)); // info_schema baseline preserved + Assertions.assertFalse(canSelectDb(rpcuser, "perso")); // personal grant DROPPED + Assertions.assertTrue(canSelectDb(rpcuser, "test")); // requested role KEPT + Assertions.assertTrue(canSelectInfoSchema(rpcuser)); // info_schema baseline preserved } finally { Auth.clearRpcSessionNarrowing(); } // cleared -> full roles restored (no leak of the narrowing to later requests on this thread) - Assert.assertTrue(canSelectDb(rpcuser, "perso")); + Assertions.assertTrue(canSelectDb(rpcuser, "perso")); } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java index 2da404bc95b528..29c61964ee9368 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java @@ -137,10 +137,10 @@ public void testAuthenticatedUserRidesBesideUser() { List names = InternalSchema.AUDIT_SCHEMA.stream().map(ColumnDef::getName) .collect(Collectors.toList()); String[] fields = buffer.toString().split(String.valueOf(AuditLoader.AUDIT_TABLE_COL_SEPARATOR), -1); - Assert.assertEquals("one field per audit_log column", names.size(), fields.length); - Assert.assertEquals(names.indexOf("user") + 1, names.indexOf("authenticated_user")); - Assert.assertEquals("alice", fields[names.indexOf("user")]); - Assert.assertEquals("svc_gateway", fields[names.indexOf("authenticated_user")]); + Assertions.assertEquals(names.size(), fields.length, "one field per audit_log column"); + Assertions.assertEquals(names.indexOf("user") + 1, names.indexOf("authenticated_user")); + Assertions.assertEquals("alice", fields[names.indexOf("user")]); + Assertions.assertEquals("svc_gateway", fields[names.indexOf("authenticated_user")]); } // The sanitizer must be a no-op for ordinary statements: no data loss, no mutation. diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java index 003ad2d9301a9e..bdd4550bec985c 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java @@ -116,21 +116,21 @@ public void testAuthenticatedUserFollowsTheSwitcher() { ConnectContext ctx = new ConnectContext(); ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("alice", "%")); // an ordinary session: the account that authenticated is the session user - Assert.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + Assertions.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); // a session-narrowed (SU) session: the effective identity is the target, the switcher stays // the authenticated account -- the audit row names both ctx.setAuthenticatedIdentity(ctx.getCurrentUserIdentity()); ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("bob", "%")); - Assert.assertEquals("bob", ctx.getQualifiedUser()); - Assert.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + Assertions.assertEquals("bob", ctx.getQualifiedUser()); + Assertions.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); // reverting the switch restores the plain mapping ctx.revertSessionNarrowing(); - Assert.assertEquals("alice", ctx.getQualifiedUser()); - Assert.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + Assertions.assertEquals("alice", ctx.getQualifiedUser()); + Assertions.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); // never null, even before any identity is set - Assert.assertEquals("", AuditLogHelper.authenticatedUser(new ConnectContext())); + Assertions.assertEquals("", AuditLogHelper.authenticatedUser(new ConnectContext())); } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java index bf4233d86c0235..444f19af7e0452 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java @@ -27,7 +27,7 @@ import org.apache.doris.utframe.TestWithFeService; import com.google.common.collect.Sets; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -79,15 +79,15 @@ public void testOriginCarriesNarrowingOnlyWhenSwitched() throws Exception { ctx.setRemoteIP("127.0.0.1"); TMasterOpRequest plain = forwardRequestOf(ctx); - Assert.assertFalse(plain.isSetIsSuUser()); - Assert.assertFalse(plain.isSetCurrentRoles()); + Assertions.assertFalse(plain.isSetIsSuUser()); + Assertions.assertFalse(plain.isSetCurrentRoles()); ctx.setSessionRoleOverride(Sets.newHashSet("tenant_a", "tenant_a_scoped")); TMasterOpRequest narrowed = forwardRequestOf(ctx); - Assert.assertTrue(narrowed.isSetIsSuUser() && narrowed.isIsSuUser()); - Assert.assertEquals(Sets.newHashSet("tenant_a", "tenant_a_scoped"), narrowed.getCurrentRoles()); + Assertions.assertTrue(narrowed.isSetIsSuUser() && narrowed.isIsSuUser()); + Assertions.assertEquals(Sets.newHashSet("tenant_a", "tenant_a_scoped"), narrowed.getCurrentRoles()); // the identity the master rebuilds is still the session's (switched-to) identity - Assert.assertEquals(ctx.getQualifiedUser(), narrowed.getUser()); + Assertions.assertEquals(ctx.getQualifiedUser(), narrowed.getUser()); } @Test @@ -107,17 +107,17 @@ public void testMasterInstallsCarriedNarrowingOnProxySession() throws Exception ConnectContext proxy = new ConnectContext(); proxy.setCurrentUserIdentity(bob); ConnectProcessor.applyForwardedSessionNarrowing(proxy, request); - Assert.assertEquals(Collections.singleton("tenant_b"), proxy.getSessionRoleOverride()); + Assertions.assertEquals(Collections.singleton("tenant_b"), proxy.getSessionRoleOverride()); proxy.setThreadLocalInfo(); try { - Assert.assertFalse(canSelectDb(bob, "perso")); // personal grant dropped - Assert.assertTrue(canSelectDb(bob, "test")); // carried role kept + Assertions.assertFalse(canSelectDb(bob, "perso")); // personal grant dropped + Assertions.assertTrue(canSelectDb(bob, "test")); // carried role kept } finally { connectContext.setThreadLocalInfo(); } // the same identity checked outside the proxy session sees its full union - Assert.assertTrue(canSelectDb(bob, "perso")); + Assertions.assertTrue(canSelectDb(bob, "perso")); } @Test @@ -126,27 +126,27 @@ public void testMasterFailsClosedWhenSwitchedRequestCarriesNoRoles() { request.setIsSuUser(true); ConnectContext proxy = new ConnectContext(); ConnectProcessor.applyForwardedSessionNarrowing(proxy, request); - Assert.assertNotNull(proxy.getSessionRoleOverride()); - Assert.assertTrue(proxy.getSessionRoleOverride().isEmpty()); + Assertions.assertNotNull(proxy.getSessionRoleOverride()); + Assertions.assertTrue(proxy.getSessionRoleOverride().isEmpty()); } @Test public void testUnswitchedRequestLeavesProxySessionUnnarrowed() { ConnectContext proxy = new ConnectContext(); ConnectProcessor.applyForwardedSessionNarrowing(proxy, new TMasterOpRequest()); - Assert.assertNull(proxy.getSessionRoleOverride()); + Assertions.assertNull(proxy.getSessionRoleOverride()); // a role list without the switched flag is not a narrowing TMasterOpRequest rolesOnly = new TMasterOpRequest(); rolesOnly.setCurrentRoles(Sets.newHashSet("tenant_c")); ConnectProcessor.applyForwardedSessionNarrowing(proxy, rolesOnly); - Assert.assertNull(proxy.getSessionRoleOverride()); + Assertions.assertNull(proxy.getSessionRoleOverride()); } @Test public void testMasterItselfAlwaysRunsTheSameBuild() { // the single-FE test environment is its own master: nothing to forward, nothing to refuse - Assert.assertTrue(Env.getCurrentEnv().isMaster()); - Assert.assertTrue(Env.getCurrentEnv().masterRunsSameBuild()); + Assertions.assertTrue(Env.getCurrentEnv().isMaster()); + Assertions.assertTrue(Env.getCurrentEnv().masterRunsSameBuild()); } } diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy index eadbadc2e2ded6..70b6bed641dbaf 100644 --- a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -55,6 +55,9 @@ suite("test_su_session_narrowing", "p0,auth") { sql """GRANT SELECT_PRIV ON ${ownDb}.* TO '${person}'@'%'""" // the service account: nothing but the right to switch sql """GRANT PROXY_PRIV ON *.*.* TO '${svc}'@'%'""" + // the framework connects with the default database in the URL, which needs a privilege on it + sql """GRANT SELECT_PRIV ON ${context.config.defaultDb}.* TO '${svc}'@'%'""" + sql """GRANT SELECT_PRIV ON ${context.config.defaultDb}.* TO '${person}'@'%'""" try { // PROXY_PRIV is a global privilege, listed like any other From 44e499234bafd81aac80c1bbbc048fd03322e48b Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 15 Sep 2026 17:12:50 +0530 Subject: [PATCH 07/30] [fix](auth) SU: register PROXY_PRIV with the authorization plugin SPI Since the authorization plugin SPI landed (PR #66770), every privilege check in the FE goes through AccessTranslation, whose class initialization refuses a privilege that has no access action. PROXY_PRIV had none, so on top of current master the first privilege check of any FE - CREATE DATABASE in the test harness included - failed with ExceptionInInitializerError. PROXY_PRIV now stands for a PROXY action: - AccessAction gains PROXY (only ever granted globally, like NODE), and AccessTranslation maps the privilege and the action to each other. - The recorded plugin API surface gains the constant, and the api major is bumped to 2.0 (minor zeroed) in the same commit, as the surface rule asks for any addition: a plugin built against 1.x would otherwise be admitted and meet an action it has never seen. - The Doris Ranger service has no access type for switching the session, so DorisAccessType answers PROXY with ADMIN: a Ranger-authorized cluster grants SU to its administrators and to nobody else, which is exactly what the engine's own predicate (ADMIN_PRIV implies PROXY_PRIV) already says. - The access-control behaviour baseline gains the PROXY column: it is answered wherever ADMIN is, because no fixture user holds PROXY_PRIV. Signed-off-by: Raghvendra Singh Co-Authored-By: Claude Fable 5.1 --- .../doris/authorization/AccessAction.java | 4 +- .../ranger/doris/DorisAccessType.java | 5 + .../fe-authorization-spi/README.md | 2 +- .../spi/AuthorizationPluginSurfaceTest.java | 5 +- .../authorization-plugin-surface.txt | 1 + fe/fe-authorization/pom.xml | 2 +- .../mysql/privilege/AccessTranslation.java | 2 + .../access-control-behavior-baseline.txt | 122 +++++++++--------- 8 files changed, 77 insertions(+), 66 deletions(-) diff --git a/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java b/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java index f6e776b5b7048d..460c5645fe45eb 100644 --- a/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java +++ b/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java @@ -54,5 +54,7 @@ public enum AccessAction { /** Using a stage. Distinct from {@link #USAGE}: it is a privilege of its own. */ STAGE_USAGE, /** Reading a view's definition. */ - SHOW_VIEW + SHOW_VIEW, + /** Switching the session to another user (SU). Only ever granted globally. */ + PROXY } diff --git a/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java b/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java index 711375303bc2e3..78463773c0b1bd 100644 --- a/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java +++ b/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java @@ -63,6 +63,11 @@ public static DorisAccessType of(AccessAction action) { return USAGE; case SHOW_VIEW: return SHOW_VIEW; + case PROXY: + // The Doris Ranger service has no access type for switching the session to another user; the + // closest question it can answer is administration, which already implies PROXY_PRIV in the + // engine, so a Ranger-authorized cluster grants SU to its administrators and to nobody else. + return ADMIN; default: // Guessing would ask Ranger about an access type its service definition does not have, and // every such request is denied - an action added to Doris would silently stop being grantable. diff --git a/fe/fe-authorization/fe-authorization-spi/README.md b/fe/fe-authorization/fe-authorization-spi/README.md index 776909b88284da..ac5a61cd9adcb4 100644 --- a/fe/fe-authorization/fe-authorization-spi/README.md +++ b/fe/fe-authorization/fe-authorization-spi/README.md @@ -164,7 +164,7 @@ Every plugin jar must declare, in its MANIFEST, the authorization plugin API it - 1.0 + 2.0 diff --git a/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java b/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java index 186d58331dbb90..a6adc92c4623f6 100644 --- a/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java +++ b/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java @@ -136,8 +136,9 @@ public void authorizationApiMajorTracksTheRecordedSurfaceChange() throws IOExcep Assertions.assertNotNull(in, "missing authorization plugin API version resource"); version.load(in); } - // First published surface of this family. - Assertions.assertEquals("1.0", version.getProperty("api.version")); + // 1.0 was the first published surface of this family; 2.0 added AccessAction.PROXY (the privilege + // behind SU), a constant a plugin built against 1.x has never seen. + Assertions.assertEquals("2.0", version.getProperty("api.version")); } @Test diff --git a/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt b/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt index c43ceadcfdc461..a1de6432a34263 100644 --- a/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt +++ b/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt @@ -7,6 +7,7 @@ org.apache.doris.authorization.AccessAction@DROP org.apache.doris.authorization.AccessAction@GRANT org.apache.doris.authorization.AccessAction@LOAD org.apache.doris.authorization.AccessAction@NODE +org.apache.doris.authorization.AccessAction@PROXY org.apache.doris.authorization.AccessAction@SELECT org.apache.doris.authorization.AccessAction@SHOW_VIEW org.apache.doris.authorization.AccessAction@STAGE_USAGE diff --git a/fe/fe-authorization/pom.xml b/fe/fe-authorization/pom.xml index 425e08677031c3..9248c6c6aea132 100644 --- a/fe/fe-authorization/pom.xml +++ b/fe/fe-authorization/pom.xml @@ -43,7 +43,7 @@ under the License. branch into a refusal, which is a behaviour change for a plugin nobody rebuilt. A change to fe-extension-spi means bumping all five families. --> - 1.0 + 2.0 diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java index e172d62d97538a..bcfb970fba8840 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java @@ -108,6 +108,7 @@ public final class AccessTranslation { ACTION_OF_PRIVILEGE.put(Privilege.CLUSTER_USAGE_PRIV, AccessAction.CLUSTER_USAGE); ACTION_OF_PRIVILEGE.put(Privilege.STAGE_USAGE_PRIV, AccessAction.STAGE_USAGE); ACTION_OF_PRIVILEGE.put(Privilege.SHOW_VIEW_PRIV, AccessAction.SHOW_VIEW); + ACTION_OF_PRIVILEGE.put(Privilege.PROXY_PRIV, AccessAction.PROXY); // The retired bit indices carry the same meaning as the ones that replaced them, so they translate // to the same action - the same normalization Role.compatibilityAuthIndexChange() applies to stored // grants. @@ -128,6 +129,7 @@ public final class AccessTranslation { PRIVILEGE_OF_ACTION.put(AccessAction.CLUSTER_USAGE, Privilege.CLUSTER_USAGE_PRIV); PRIVILEGE_OF_ACTION.put(AccessAction.STAGE_USAGE, Privilege.STAGE_USAGE_PRIV); PRIVILEGE_OF_ACTION.put(AccessAction.SHOW_VIEW, Privilege.SHOW_VIEW_PRIV); + PRIVILEGE_OF_ACTION.put(AccessAction.PROXY, Privilege.PROXY_PRIV); // Sorted by name so that two constants naming the same privileges with the same match - today // SHOW_RESOURCES and SHOW_WORKLOAD_GROUP - always resolve to the same one of the pair. They ask the diff --git a/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt b/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt index 8103c329ff5cf5..027410838eefc2 100644 --- a/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt +++ b/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt @@ -1,61 +1,61 @@ -ACTIONS: ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ACTIONS: ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE USERS: admin_user,node_user,local_user,ranger_user,nobody -builtin | - | admin_user | global | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | global | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | global | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | global | - builtin | - | ranger_user | global | - builtin | - | nobody | global | - -builtin | - | admin_user | resource:res1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | resource:res1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | resource:res1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | resource:res1 | ALL,SHOW_RESOURCES,SHOW_WORKLOAD_GROUP,USAGE builtin | - | ranger_user | resource:res1 | - builtin | - | nobody | resource:res1 | - -builtin | - | admin_user | workloadgrp:wg1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | workloadgrp:wg1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | workloadgrp:wg1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | workloadgrp:wg1 | ALL,SHOW_RESOURCES,SHOW_WORKLOAD_GROUP,USAGE builtin | - | ranger_user | workloadgrp:wg1 | - builtin | - | nobody | workloadgrp:wg1 | - -builtin | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | admin_user | vault:sv1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | vault:sv1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | vault:sv1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | vault:sv1 | - builtin | - | ranger_user | vault:sv1 | - builtin | - | nobody | vault:sv1 | - -builtin | internal | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR,SHOW builtin | internal | local_user | catalog | SHOW builtin | internal | ranger_user | catalog | SHOW builtin | internal | nobody | catalog | SHOW -builtin | internal | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR,SHOW builtin | internal | local_user | catalog:skipchk | SHOW builtin | internal | ranger_user | catalog:skipchk | SHOW builtin | internal | nobody | catalog:skipchk | SHOW -builtin | internal | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR builtin | internal | local_user | database | SHOW builtin | internal | ranger_user | database | - builtin | internal | nobody | database | - -builtin | internal | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR builtin | internal | local_user | table | SELECT,SHOW builtin | internal | ranger_user | table | - builtin | internal | nobody | table | - -builtin | internal | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | internal | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | internal | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | internal | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | internal | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | admin_user | rowfilter | - builtin | internal | node_user | rowfilter | - builtin | internal | local_user | rowfilter | [builtin_row_policy | pcol1 = 1 | RESTRICTIVE] @@ -71,36 +71,36 @@ builtin | internal | node_user | mask:pcol2 | - builtin | internal | local_user | mask:pcol2 | - builtin | internal | ranger_user | mask:pcol2 | - builtin | internal | nobody | mask:pcol2 | - -builtin | ext_plain | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | catalog | SHOW builtin | ext_plain | ranger_user | catalog | - builtin | ext_plain | nobody | catalog | - -builtin | ext_plain | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | catalog:skipchk | SHOW builtin | ext_plain | ranger_user | catalog:skipchk | - builtin | ext_plain | nobody | catalog:skipchk | - -builtin | ext_plain | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | database | SHOW builtin | ext_plain | ranger_user | database | - builtin | ext_plain | nobody | database | - -builtin | ext_plain | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | table | SELECT,SHOW builtin | ext_plain | ranger_user | table | - builtin | ext_plain | nobody | table | - -builtin | ext_plain | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | ext_plain | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | ext_plain | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | ext_plain | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | ext_plain | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | admin_user | rowfilter | - builtin | ext_plain | node_user | rowfilter | - builtin | ext_plain | local_user | rowfilter | - @@ -116,32 +116,32 @@ builtin | ext_plain | node_user | mask:pcol2 | - builtin | ext_plain | local_user | mask:pcol2 | - builtin | ext_plain | ranger_user | mask:pcol2 | - builtin | ext_plain | nobody | mask:pcol2 | - -builtin | ext_ranger | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | catalog | SHOW builtin | ext_ranger | ranger_user | catalog | - builtin | ext_ranger | nobody | catalog | - -builtin | ext_ranger | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR,SELECT,SHOW builtin | ext_ranger | local_user | catalog:skipchk | SELECT,SHOW builtin | ext_ranger | ranger_user | catalog:skipchk | SELECT,SHOW builtin | ext_ranger | nobody | catalog:skipchk | SELECT,SHOW -builtin | ext_ranger | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | database | - builtin | ext_ranger | ranger_user | database | SHOW builtin | ext_ranger | nobody | database | - -builtin | ext_ranger | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | table | - builtin | ext_ranger | ranger_user | table | ALL,SELECT,SHOW builtin | ext_ranger | nobody | table | - -builtin | ext_ranger | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | column:pcol1 | - builtin | ext_ranger | ranger_user | column:pcol1 | ALL,SELECT,SHOW builtin | ext_ranger | nobody | column:pcol1 | - -builtin | ext_ranger | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | column:pcol2 | - builtin | ext_ranger | ranger_user | column:pcol2 | ALL,SELECT,SHOW @@ -161,32 +161,32 @@ builtin | ext_ranger | node_user | mask:pcol2 | - builtin | ext_ranger | local_user | mask:pcol2 | - builtin | ext_ranger | ranger_user | mask:pcol2 | - builtin | ext_ranger | nobody | mask:pcol2 | - -builtin | ext_legacy | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_legacy | local_user | catalog | SHOW builtin | ext_legacy | ranger_user | catalog | - builtin | ext_legacy | nobody | catalog | - -builtin | ext_legacy | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR,SELECT,SHOW builtin | ext_legacy | local_user | catalog:skipchk | SELECT,SHOW builtin | ext_legacy | ranger_user | catalog:skipchk | SELECT,SHOW builtin | ext_legacy | nobody | catalog:skipchk | SELECT,SHOW -builtin | ext_legacy | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR -builtin | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | ranger_user | database | - builtin | ext_legacy | nobody | database | - -builtin | ext_legacy | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR -builtin | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | ranger_user | table | - builtin | ext_legacy | nobody | table | - -builtin | ext_legacy | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR -builtin | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | ranger_user | column:pcol1 | - builtin | ext_legacy | nobody | column:pcol1 | - -builtin | ext_legacy | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_legacy | local_user | column:pcol2 | - builtin | ext_legacy | ranger_user | column:pcol2 | - @@ -221,11 +221,11 @@ ranger | - | node_user | workloadgrp:wg1 | - ranger | - | local_user | workloadgrp:wg1 | - ranger | - | ranger_user | workloadgrp:wg1 | ALL,SHOW_RESOURCES,SHOW_WORKLOAD_GROUP,USAGE ranger | - | nobody | workloadgrp:wg1 | - -ranger | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | - | admin_user | vault:sv1 | - ranger | - | node_user | vault:sv1 | - ranger | - | local_user | vault:sv1 | - @@ -378,17 +378,17 @@ ranger | ext_legacy | ranger_user | catalog:skipchk | SELECT,SHOW ranger | ext_legacy | nobody | catalog:skipchk | SELECT,SHOW ranger | ext_legacy | admin_user | database | - ranger | ext_legacy | node_user | database | - -ranger | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | ext_legacy | ranger_user | database | - ranger | ext_legacy | nobody | database | - ranger | ext_legacy | admin_user | table | - ranger | ext_legacy | node_user | table | - -ranger | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | ext_legacy | ranger_user | table | - ranger | ext_legacy | nobody | table | - ranger | ext_legacy | admin_user | column:pcol1 | - ranger | ext_legacy | node_user | column:pcol1 | - -ranger | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | ext_legacy | ranger_user | column:pcol1 | - ranger | ext_legacy | nobody | column:pcol1 | - ranger | ext_legacy | admin_user | column:pcol2 | - From 75d0238a314cee5bad6f22ec53ebae3d65b87f5d Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 15 Sep 2026 17:42:30 +0530 Subject: [PATCH 08/30] [test](auth) SU: give the forward-request probe a MySQL capability Since the execution layer fills the forward request through the session's protocol adapter (PR #67900), building one reads the client's negotiated capabilities; a bare test ConnectContext has none, so the probe that checks what the origin FE sends for a narrowed session now sets the default capability first, as FEOpExecutorFlightForwardTest does. Signed-off-by: Raghvendra Singh Co-Authored-By: Claude Fable 5.1 --- .../org/apache/doris/qe/ForwardedSessionNarrowingTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java index 444f19af7e0452..bea2e4d487da21 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java @@ -21,6 +21,7 @@ import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.mysql.MysqlCapability; import org.apache.doris.mysql.privilege.PrivPredicate; import org.apache.doris.thrift.TMasterOpRequest; import org.apache.doris.thrift.TNetworkAddress; @@ -77,6 +78,8 @@ public void testOriginCarriesNarrowingOnlyWhenSwitched() throws Exception { ConnectContext ctx = new ConnectContext(); ctx.setCurrentUserIdentity(ident("alice")); ctx.setRemoteIP("127.0.0.1"); + // the forward request carries the client's negotiated capabilities; a bare test context has none + ctx.setCapability(MysqlCapability.DEFAULT_CAPABILITY); TMasterOpRequest plain = forwardRequestOf(ctx); Assertions.assertFalse(plain.isSetIsSuUser()); From 39e05e62a311620b1b3fc9838ba69c3c2cf82935 Mon Sep 17 00:00:00 2001 From: morningman Date: Fri, 30 Oct 2026 11:54:33 +0800 Subject: [PATCH 09/30] [fix](fe) Preserve SU narrowing when changing users fails ### What problem does this PR solve? Issue Number: N/A Related PR: #67672 Problem Summary: After SU switches a connection to a user with a restricted role set, COM_CHANGE_USER clears that restriction before checking the replacement credentials. A forbidden root/admin target or a failed authentication returns an error while keeping the previous effective identity. Subsequent privilege checks then use that user's full role union, including direct grants excluded by SU. Clear SU state only after password authentication succeeds, immediately before installing the replacement identity. Failed changes retain the existing identity and narrowed roles. Add tests using the real authorization manager and MySQL change-user handler for forbidden accounts, failed authentication, successful reauthentication, and connection reset. ### Release note Keep SU session role restrictions active when COM_CHANGE_USER fails. ### Check List (For Author) - Test: Unit Test - 78 FE unit tests passed through run-fe-ut.sh, including 4 new connection lifecycle tests. Two new tests failed on the original implementation. - FE build and Checkstyle passed through build.sh --fe. - Behavior changed: Yes. A failed COM_CHANGE_USER preserves the current SU identity and role restriction. - Does this need documentation: No. This fix enforces the existing SU session contract; documentation for the feature remains tracked in #67672. --- .../doris/qe/MysqlConnectProcessor.java | 6 +- .../doris/qe/MysqlConnectProcessorSuTest.java | 154 ++++++++++++++++++ 2 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java index 6948722a04986a..f613952612ef63 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java @@ -479,9 +479,6 @@ protected void handleFieldList(String tableName) throws ConnectionException { } private void handleChangeUser() throws IOException { - // A full re-authentication replaces the session identity — drop any SU narrowing first - // (never revert: the incoming credentials decide the new identity). - getConnectContext().clearSessionNarrowing(); // Random bytes generated when creating connection. byte[] authPluginData = getConnectContext().getAuthPluginData(); Preconditions.checkNotNull(authPluginData, "Auth plugin data is null."); @@ -542,6 +539,9 @@ private void handleChangeUser() throws IOException { ctx.getState().setError(ErrorCode.ERR_ACCESS_DENIED_ERROR, "Authentication failed."); return; } + // A failed change keeps the current identity, so it must also keep its narrowed roles. + // Clear SU state only after the incoming credentials establish a new identity. + ctx.clearSessionNarrowing(); ctx.setCurrentUserIdentity(currentUserIdentity.get(0)); // Change default db if set. diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java new file mode 100644 index 00000000000000..0a6735341d8394 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java @@ -0,0 +1,154 @@ +// 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.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.mysql.MysqlHandshakePacket; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.mysql.protocol.MysqlProtocolAdapter; +import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; +import org.apache.doris.utframe.TestWithFeService; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; +import java.util.Collections; + +public class MysqlConnectProcessorSuTest extends TestWithFeService { + @Override + protected void runBeforeAll() throws Exception { + createDatabase("su_change_tenant"); + createDatabase("su_change_personal"); + addUser("su_change_svc", false); + addUser("su_change_person", false); + addUser("su_change_login", false); + createRole("su_change_role"); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'su_change_svc'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.su_change_tenant.* TO ROLE 'su_change_role'"); + grantRole("GRANT 'su_change_role' TO 'su_change_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.su_change_personal.* TO 'su_change_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.su_change_personal.* TO 'su_change_login'@'%'"); + } + + private ConnectContext switchedContext() throws Exception { + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("su_change_svc", "%")); + ctx.setRemoteIP("127.0.0.1"); + MysqlProtocolAdapter.of(ctx).setHandshakePacket(new MysqlHandshakePacket(0)); + ctx.setThreadLocalInfo(); + new SuUserCommand(new UserIdentity("su_change_person", "%"), + Collections.singletonList("su_change_role"), null).run(ctx, null); + return ctx; + } + + private boolean canSelect(ConnectContext ctx, String db) { + return Env.getCurrentEnv().getAccessManager().checkDbPriv(ctx.getCurrentUserIdentity(), + InternalCatalog.INTERNAL_CATALOG_NAME, db, PrivPredicate.SELECT); + } + + private void changeUser(ConnectContext ctx, String user) throws Exception { + // COM_CHANGE_USER payload, with an empty native password and no default database. + ByteBuffer packet = ByteBuffer.allocate(256).order(ByteOrder.LITTLE_ENDIAN); + packet.put(user.getBytes(StandardCharsets.UTF_8)).put((byte) 0); + packet.put((byte) 0); + packet.put((byte) 0); + packet.putShort((short) 33); + packet.put(MysqlHandshakePacket.AUTH_PLUGIN_NAME.getBytes(StandardCharsets.UTF_8)).put((byte) 0); + packet.flip(); + MysqlConnectProcessor processor = new MysqlConnectProcessor(ctx); + Field packetField = MysqlConnectProcessor.class.getDeclaredField("packetBuf"); + packetField.setAccessible(true); + packetField.set(processor, packet); + Method handler = MysqlConnectProcessor.class.getDeclaredMethod("handleChangeUser"); + handler.setAccessible(true); + handler.invoke(processor); + } + + private void assertStillNarrowed(ConnectContext ctx) { + Assertions.assertEquals("su_change_person", ctx.getQualifiedUser()); + Assertions.assertTrue(canSelect(ctx, "su_change_tenant")); + Assertions.assertFalse(canSelect(ctx, "su_change_personal")); + Assertions.assertEquals("su_change_svc", ctx.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertEquals(Collections.singleton("su_change_role"), ctx.getSessionRoleOverride()); + Assertions.assertFalse(ctx.isKilled()); + } + + @Test + public void testForbiddenChangeUserPreservesNarrowing() throws Exception { + try { + for (String user : new String[] {"root", "admin"}) { + ConnectContext ctx = switchedContext(); + changeUser(ctx, user); + Assertions.assertEquals(QueryState.MysqlStateType.ERR, ctx.getState().getStateType()); + assertStillNarrowed(ctx); + } + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testFailedAuthenticationPreservesNarrowing() throws Exception { + try { + ConnectContext ctx = switchedContext(); + changeUser(ctx, "su_change_nonexistent"); + Assertions.assertEquals(QueryState.MysqlStateType.ERR, ctx.getState().getStateType()); + assertStillNarrowed(ctx); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testSuccessfulChangeUserClearsNarrowing() throws Exception { + try { + ConnectContext ctx = switchedContext(); + changeUser(ctx, "su_change_login"); + Assertions.assertEquals(QueryState.MysqlStateType.OK, ctx.getState().getStateType()); + Assertions.assertEquals("su_change_login", ctx.getQualifiedUser()); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertNull(ctx.getSessionRoleOverride()); + Assertions.assertTrue(canSelect(ctx, "su_change_personal")); + Assertions.assertFalse(canSelect(ctx, "su_change_tenant")); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testConnectionResetRestoresAuthenticatedIdentity() throws Exception { + try { + ConnectContext ctx = switchedContext(); + ctx.resetConnection(); + Assertions.assertEquals("su_change_svc", ctx.getQualifiedUser()); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertNull(ctx.getSessionRoleOverride()); + Assertions.assertFalse(canSelect(ctx, "su_change_personal")); + Assertions.assertFalse(canSelect(ctx, "su_change_tenant")); + } finally { + connectContext.setThreadLocalInfo(); + } + } +} From 4f1b87501726124f9d14519e61a76ae914da258f Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Mon, 28 Sep 2026 23:40:03 +0530 Subject: [PATCH 10/30] [fix](auth) SU: make "session is narrowed" an explicit ConnectContext predicate The SU narrowing used a null sessionRoleOverride as the "not narrowed" sentinel and every reader null-checked the collection getter. A Mockito-mocked ConnectContext (CloudAuthTest since the JUnit 4 removal, #67396) answers an EMPTY set for that getter, so root looked narrowed to zero roles and every privileged command was denied. Add ConnectContext.isSessionRoleNarrowed() and read it at every site (Auth, FEOpExecutor, StmtExecutor, SchemaScanNode, MetadataScanNode, the session_is_narrowed() fold). An empty override set keeps its meaning (narrowed to the baseline only); the predicate is the sentinel. Signed-off-by: Raghvendra Singh Co-Authored-By: Claude Fable 5.1 (cherry picked from commit 8a71d77dbf572a4b18fe57530e5f5c796ecb0bb8) --- .../doris/datasource/tvf/source/MetadataScanNode.java | 6 ++---- .../main/java/org/apache/doris/mysql/privilege/Auth.java | 6 +++--- .../rules/expression/rules/FoldConstantRuleOnFE.java | 2 +- .../java/org/apache/doris/planner/SchemaScanNode.java | 6 ++---- .../main/java/org/apache/doris/qe/ConnectContext.java | 9 +++++++++ .../src/main/java/org/apache/doris/qe/FEOpExecutor.java | 4 ++-- .../src/main/java/org/apache/doris/qe/StmtExecutor.java | 2 +- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java index 131a627fc64f52..bdb5439a71533d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java @@ -39,7 +39,6 @@ import java.util.HashSet; import java.util.List; -import java.util.Set; public class MetadataScanNode extends ExternalScanNode { @@ -69,9 +68,8 @@ protected void toThrift(TPlanNode planNode) { metaScanNode.setCurrentUserIdent(tCurrentUser); // SU narrowing: carry the session's active role subset so the BE->FE metadata requests // narrow their privilege checks to the same set the session sees; null = not narrowed. - Set sessionRoleOverride = ConnectContext.get().getSessionRoleOverride(); - if (sessionRoleOverride != null) { - metaScanNode.setCurrentRoles(new HashSet<>(sessionRoleOverride)); + if (ConnectContext.get().isSessionRoleNarrowed()) { + metaScanNode.setCurrentRoles(new HashSet<>(ConnectContext.get().getSessionRoleOverride())); } planNode.setMetaScanNode(metaScanNode); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index 12d1c3359667bc..fdb9f72d4b2d6f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -356,8 +356,8 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { } ConnectContext ctx = ConnectContext.get(); if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity())) { - Set sessionRoleOverride = ctx.getSessionRoleOverride(); - if (sessionRoleOverride != null) { + if (ctx.isSessionRoleNarrowed()) { + Set sessionRoleOverride = ctx.getSessionRoleOverride(); // SU-narrowed session: the override REPLACES every role source (user grants, LDAP, // authenticated roles); SuUserCommand enforced the ceiling at switch time. Return the // override roles + the information_schema/mysql read baseline (see narrowedRoleSet). @@ -438,7 +438,7 @@ private static Role narrowingBaselineRole() { private Set getRolesForWorkloadGroupCheck(UserIdentity userIdentity) { Set roles = getRolesByUserWithLdap(userIdentity); ConnectContext ctx = ConnectContext.get(); - if (ctx == null || ctx.getSessionRoleOverride() == null + if (ctx == null || !ctx.isSessionRoleNarrowed() || !userIdentity.equals(ctx.getCurrentUserIdentity())) { return roles; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java index 158641b9f3c443..e1109b8808ac1a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java @@ -404,7 +404,7 @@ public Expression visitCurrentUser(CurrentUser currentUser, ExpressionRewriteCon @Override public Expression visitSessionIsNarrowed(SessionIsNarrowed fn, ExpressionRewriteContext context) { ConnectContext cctx = context.cascadesContext.getConnectContext(); - return BooleanLiteral.of(cctx != null && cctx.getSessionRoleOverride() != null); + return BooleanLiteral.of(cctx != null && cctx.isSessionRoleNarrowed()); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java index ed5bedabb56876..df18c129858eaa 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java @@ -47,7 +47,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.Set; /** * Full scan of an SCHEMA table. @@ -167,9 +166,8 @@ protected void toThrift(TPlanNode msg) { // SU narrowing: carry the session's active role subset (current_roles) in the plan so the // BE->FE metadata RPCs (getDbNames/getTableNames/listTableStatus/describeTables) narrow // name visibility to the same set the session sees; null = normal session (no narrowing). - Set sessionRoleOverride = ConnectContext.get().getSessionRoleOverride(); - if (sessionRoleOverride != null) { - msg.schema_scan_node.setCurrentRoles(new HashSet<>(sessionRoleOverride)); + if (ConnectContext.get().isSessionRoleNarrowed()) { + msg.schema_scan_node.setCurrentRoles(new HashSet<>(ConnectContext.get().getSessionRoleOverride())); } msg.schema_scan_node.setFrontendConjuncts(GsonUtils.GSON.toJson(frontendConjuncts)); setFeAddrList(msg); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java index b77a7627cbd81a..10526eb0210e07 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java @@ -415,6 +415,15 @@ public void setAuthenticatedIdentity(UserIdentity authenticatedIdentity) { this.authenticatedIdentity = authenticatedIdentity; } + /** + * True when SU narrowed this session: the override set (possibly empty) then replaces every role + * source. This predicate, not a null check on getSessionRoleOverride(), is the sentinel; a mocked + * context answers false here while a mocked collection getter answers an empty set. + */ + public boolean isSessionRoleNarrowed() { + return sessionRoleOverride != null; + } + public Set getSessionRoleOverride() { return sessionRoleOverride; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java index 673c70547c4aba..1ea09ef9200611 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java @@ -195,8 +195,8 @@ protected TMasterOpRequest buildStmtForwardParams() throws AnalysisException { params.setUserIp(ctx.getRemoteIP()); params.setStmtId(ctx.getStmtId()); params.setCurrentUserIdent(ctx.getCurrentUserIdentity().toThrift()); - Set sessionRoleOverride = ctx.getSessionRoleOverride(); - if (sessionRoleOverride != null) { + if (ctx.isSessionRoleNarrowed()) { + Set sessionRoleOverride = ctx.getSessionRoleOverride(); // Session-narrowed (SU) session: carry the active role subset so the master authorizes // the forwarded statement against exactly this set, never the target's full role union. params.setIsSuUser(true); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index 6203ad78c07d4c..fa62b76899d4bd 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -1530,7 +1530,7 @@ private void forwardToMaster() throws Exception { // executing. An older master silently ignores those fields and would authorize the // statement against the target's FULL role union, so fail closed unless the master is // known to run this same build (a rolling upgrade upgrades the master last). - if (context.getSessionRoleOverride() != null && !Env.getCurrentEnv().masterRunsSameBuild()) { + if (context.isSessionRoleNarrowed() && !Env.getCurrentEnv().masterRunsSameBuild()) { throw new UserException("This statement would be forwarded to the master FE, which runs a " + "different build than this FE; a session-narrowed (SU) session forwards only when " + "the master is known to apply the narrowing. Retry once the rolling upgrade " From 1be3992b54b0740d7ceee8563cbf5600a79a2786 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Sat, 31 Oct 2026 20:04:12 +0530 Subject: [PATCH 11/30] [test](audit) Expect authenticated_user in the audit_log schema listing The SU audit column sits after user in AUDIT_SCHEMA; the nonConcurrent audit suite's expected DESC output still listed the old schema. --- regression-test/data/audit/test_audit_log_behavior.out | 1 + 1 file changed, 1 insertion(+) diff --git a/regression-test/data/audit/test_audit_log_behavior.out b/regression-test/data/audit/test_audit_log_behavior.out index 40ed22056b07e2..d620965d8b2dd2 100644 --- a/regression-test/data/audit/test_audit_log_behavior.out +++ b/regression-test/data/audit/test_audit_log_behavior.out @@ -4,6 +4,7 @@ query_id varchar(128) Yes true \N time datetime(3) Yes true \N client_ip varchar(128) Yes true \N user varchar(128) Yes false \N NONE +authenticated_user varchar(128) Yes false \N NONE frontend_ip varchar(1024) Yes false \N NONE catalog varchar(128) Yes false \N NONE db varchar(128) Yes false \N NONE From a90d4ebe456b6b5c7f6d11d3d44eaea100678167 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Sat, 31 Oct 2026 20:20:07 +0530 Subject: [PATCH 12/30] [fix](auth) SU: run every BE->FE metadata RPC handler under the carried narrowing listTableMetadataNameIds, listTablePrivilegeStatus, listSchemaPrivilegeStatus and listUserPrivilegeStatus ran without withSessionRoleOverride, so a session-narrowed (SU) session's information_schema reads through those scanners were authorized against the target's full role union instead of the carried subset. TShowUserRequest carried no role subset at all: it gains current_roles, the BE user scanner sends it like the other scanners, and showUser runs under it, so mysql.user under SU shows what the narrowed roles allow. Review findings P1-1 and P1-2 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../schema_user_scanner.cpp | 3 ++ .../doris/service/FrontendServiceImpl.java | 28 ++++++++++++++++++- .../service/FrontendServiceImplTest.java | 17 +++++++++++ gensrc/thrift/FrontendService.thrift | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/be/src/information_schema/schema_user_scanner.cpp b/be/src/information_schema/schema_user_scanner.cpp index 260ee8a814fee4..661fbcde383bcb 100644 --- a/be/src/information_schema/schema_user_scanner.cpp +++ b/be/src/information_schema/schema_user_scanner.cpp @@ -73,6 +73,9 @@ Status SchemaUserScanner::start(RuntimeState* state) { if (nullptr != _param->common_param->current_user_ident) { request.__set_current_user_ident(*_param->common_param->current_user_ident); } + if (!_param->common_param->current_roles.empty()) { + request.__set_current_roles(_param->common_param->current_roles); + } RETURN_IF_ERROR(SchemaHelper::show_user(*(_param->common_param->ip), _param->common_param->port, request, &_user_result)); diff --git a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java index 64288350b1ad24..6bd6aefc647595 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java +++ b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java @@ -861,6 +861,12 @@ private static boolean needTableStatusStats(Set requiredColumns) { } public TListTableMetadataNameIdsResult listTableMetadataNameIds(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listTableMetadataNameIdsImpl(params)); + } + + private TListTableMetadataNameIdsResult listTableMetadataNameIdsImpl(TGetTablesParams params) + throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list simple table request: {}", params); } @@ -944,6 +950,11 @@ public TListTableMetadataNameIdsResult listTableMetadataNameIds(TGetTablesParams @Override public TListPrivilegesResult listTablePrivilegeStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listTablePrivilegeStatusImpl(params)); + } + + private TListPrivilegesResult listTablePrivilegeStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list table privileges request: {}", params); } @@ -963,6 +974,11 @@ public TListPrivilegesResult listTablePrivilegeStatus(TGetTablesParams params) t @Override public TListPrivilegesResult listSchemaPrivilegeStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listSchemaPrivilegeStatusImpl(params)); + } + + private TListPrivilegesResult listSchemaPrivilegeStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list schema privileges request: {}", params); } @@ -982,6 +998,11 @@ public TListPrivilegesResult listSchemaPrivilegeStatus(TGetTablesParams params) @Override public TListPrivilegesResult listUserPrivilegeStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listUserPrivilegeStatusImpl(params)); + } + + private TListPrivilegesResult listUserPrivilegeStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list user privileges request: {}", params); } @@ -5857,7 +5878,12 @@ private TShowProcessListResult showProcessListImpl(TShowProcessListRequest reque } @Override - public TShowUserResult showUser(TShowUserRequest request) { + public TShowUserResult showUser(TShowUserRequest request) throws TException { + return withSessionRoleOverride(request.current_user_ident, null, null, request.current_roles, + () -> showUserImpl(request)); + } + + private TShowUserResult showUserImpl(TShowUserRequest request) { UserIdentity currentUser = null; if (request.isSetCurrentUserIdent()) { currentUser = UserIdentity.fromThrift(request.current_user_ident); diff --git a/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java b/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java index 6c139546424c67..55c4e532187178 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java @@ -907,6 +907,23 @@ public void testShowUser() throws Exception { // Fail closed: a request without a caller identity (e.g. a pre-upgrade BE that does not // set the field) exposes no rows rather than leaking every account. Assertions.assertTrue(impl.showUser(new TShowUserRequest()).getUserinfoList().isEmpty()); + + // An SU-narrowed caller is authorized against the role subset the request carries: a + // role administrator narrowed to a plain role sees only its own row. + addUser("show_user_adm", true); + executeCommand("create role show_user_adm_role"); + executeCommand("create role show_user_plain_role"); + executeCommand("grant grant_priv on *.*.* to role 'show_user_adm_role'"); + executeCommand("grant 'show_user_adm_role', 'show_user_plain_role' to 'show_user_adm'@'%'"); + TShowUserRequest admRequest = new TShowUserRequest(); + admRequest.setCurrentUserIdent( + UserIdentity.createAnalyzedUserIdentWithIp("show_user_adm", "%").toThrift()); + Assertions.assertTrue(impl.showUser(admRequest).getUserinfoList().size() >= 2, + "a role administrator sees every account"); + admRequest.setCurrentRoles(Collections.singleton("show_user_plain_role")); + List> narrowedRows = impl.showUser(admRequest).getUserinfoList(); + Assertions.assertEquals(1, narrowedRows.size(), "narrowed to a plain role: own row only"); + Assertions.assertEquals("show_user_adm", narrowedRows.get(0).get(userNameIdx)); } @Test diff --git a/gensrc/thrift/FrontendService.thrift b/gensrc/thrift/FrontendService.thrift index 581b55bb33c4a3..4065cc4fc61de5 100644 --- a/gensrc/thrift/FrontendService.thrift +++ b/gensrc/thrift/FrontendService.thrift @@ -1647,6 +1647,7 @@ struct TShowProcessListResult { struct TShowUserRequest { 1: optional Types.TUserIdentity current_user_ident // to filter rows by the requesting user's privileges + 2: optional set current_roles // the requesting session's SU-narrowed role subset, if any } struct TShowUserResult { From c5e6524569d43af992b1d0121d4b2930bf388a37 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Sat, 31 Oct 2026 20:20:07 +0530 Subject: [PATCH 13/30] [fix](audit) SU: name the authenticating account on forwarded statements and the REST query path A statement forwarded to the master carried only the narrowed role subset, so the master's audit row recorded the switched-to target as the account that logged in. TMasterOpRequest gains authenticated_user_ident: the origin FE sets it beside is_su_user/current_roles and the master installs it on the proxy session, so authenticated_user is the switcher on both FEs. The REST table query plan path now fills authenticated_user like the MySQL path does. Review finding P2-3 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../doris/httpv2/rest/TableQueryPlanAction.java | 1 + .../main/java/org/apache/doris/qe/AuditLogHelper.java | 4 +--- .../java/org/apache/doris/qe/ConnectProcessor.java | 4 ++++ .../main/java/org/apache/doris/qe/FEOpExecutor.java | 4 ++++ .../doris/qe/ForwardedSessionNarrowingTest.java | 11 ++++++++++- gensrc/thrift/FrontendService.thrift | 2 ++ 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java b/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java index 985da4f9f36a4f..4561ab2d330416 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java @@ -362,6 +362,7 @@ private static void addToAuditLog(ConnectContext ctx, String origStmt, Statement .setTimestamp(ctx.getStartTime()) .setClientIp(ctx.getClientIP()) .setUser(ctx.getQualifiedUser()) + .setAuthenticatedUser(AuditLogHelper.authenticatedUser(ctx)) .setFeIp(FrontendOptions.getLocalHostAddress()) .setCtl(catalog == null ? InternalCatalog.INTERNAL_CATALOG_NAME : catalog.getName()) .setDb(ctx.getDatabase()) diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java index d7bac2ee969110..4146fabfb517e1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java @@ -50,7 +50,6 @@ import org.apache.doris.resource.workloadgroup.QueueToken; import org.apache.doris.service.FrontendOptions; -import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; @@ -225,8 +224,7 @@ private static int countValues(List children) { * The account that authenticated the connection: the switcher of a session-narrowed (SU) session, * otherwise the session user itself. Never null, so the audit row always names who logged in. */ - @VisibleForTesting - static String authenticatedUser(ConnectContext ctx) { + public static String authenticatedUser(ConnectContext ctx) { String user = ctx.getAuthenticatedIdentity() == null ? ctx.getQualifiedUser() : ctx.getAuthenticatedIdentity().getQualifiedUser(); return user == null ? "" : user; diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java index b5d918c16de741..ac8fd3a5fa5683 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java @@ -562,6 +562,10 @@ static void applyForwardedSessionNarrowing(ConnectContext ctx, TMasterOpRequest } Set roles = request.isSetCurrentRoles() ? request.getCurrentRoles() : Collections.emptySet(); ctx.setSessionRoleOverride(roles); + if (request.isSetAuthenticatedUserIdent()) { + // audit only: the master's row names the account that authenticated the connection + ctx.setAuthenticatedIdentity(UserIdentity.fromThrift(request.getAuthenticatedUserIdent())); + } } public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException { diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java index 1ea09ef9200611..fae508f5e95a60 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java @@ -201,6 +201,10 @@ protected TMasterOpRequest buildStmtForwardParams() throws AnalysisException { // the forwarded statement against exactly this set, never the target's full role union. params.setIsSuUser(true); params.setCurrentRoles(new HashSet<>(sessionRoleOverride)); + if (ctx.getAuthenticatedIdentity() != null) { + // the master's audit row names the account that authenticated the connection + params.setAuthenticatedUserIdent(ctx.getAuthenticatedIdentity().toThrift()); + } } params.setSessionId(ctx.getSessionId()); params.setMoreResultExists(moreStmtExists); diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java index bea2e4d487da21..e3c361a3e241ee 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java @@ -84,13 +84,17 @@ public void testOriginCarriesNarrowingOnlyWhenSwitched() throws Exception { TMasterOpRequest plain = forwardRequestOf(ctx); Assertions.assertFalse(plain.isSetIsSuUser()); Assertions.assertFalse(plain.isSetCurrentRoles()); + Assertions.assertFalse(plain.isSetAuthenticatedUserIdent()); + ctx.setAuthenticatedIdentity(ident("svc")); ctx.setSessionRoleOverride(Sets.newHashSet("tenant_a", "tenant_a_scoped")); TMasterOpRequest narrowed = forwardRequestOf(ctx); Assertions.assertTrue(narrowed.isSetIsSuUser() && narrowed.isIsSuUser()); Assertions.assertEquals(Sets.newHashSet("tenant_a", "tenant_a_scoped"), narrowed.getCurrentRoles()); - // the identity the master rebuilds is still the session's (switched-to) identity + // the identity the master rebuilds is still the session's (switched-to) identity ... Assertions.assertEquals(ctx.getQualifiedUser(), narrowed.getUser()); + // ... and the account that authenticated the connection rides along for the master's audit row + Assertions.assertEquals(ident("svc").toThrift(), narrowed.getAuthenticatedUserIdent()); } @Test @@ -106,11 +110,15 @@ public void testMasterInstallsCarriedNarrowingOnProxySession() throws Exception TMasterOpRequest request = new TMasterOpRequest(); request.setIsSuUser(true); request.setCurrentRoles(Sets.newHashSet("tenant_b")); + request.setAuthenticatedUserIdent(ident("svc").toThrift()); ConnectContext proxy = new ConnectContext(); proxy.setCurrentUserIdentity(bob); ConnectProcessor.applyForwardedSessionNarrowing(proxy, request); Assertions.assertEquals(Collections.singleton("tenant_b"), proxy.getSessionRoleOverride()); + // the master's audit row names the switcher, the effective identity stays the target + Assertions.assertEquals("svc", proxy.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertEquals("bob", proxy.getCurrentUserIdentity().getQualifiedUser()); proxy.setThreadLocalInfo(); try { @@ -138,6 +146,7 @@ public void testUnswitchedRequestLeavesProxySessionUnnarrowed() { ConnectContext proxy = new ConnectContext(); ConnectProcessor.applyForwardedSessionNarrowing(proxy, new TMasterOpRequest()); Assertions.assertNull(proxy.getSessionRoleOverride()); + Assertions.assertNull(proxy.getAuthenticatedIdentity()); // a role list without the switched flag is not a narrowing TMasterOpRequest rolesOnly = new TMasterOpRequest(); diff --git a/gensrc/thrift/FrontendService.thrift b/gensrc/thrift/FrontendService.thrift index 4065cc4fc61de5..0c9dd2ea0fbcd3 100644 --- a/gensrc/thrift/FrontendService.thrift +++ b/gensrc/thrift/FrontendService.thrift @@ -438,6 +438,8 @@ struct TMasterOpRequest { 34: optional set current_roles 35: optional bool is_su_user 36: optional string connectingFeLocalResourceGroup + // SU: the account that authenticated the connection, so the master's audit row names it too + 37: optional Types.TUserIdentity authenticated_user_ident // selectdb cloud 1000: optional string cloud_cluster From 77a3f4fee17e3ddfafde6acae761de2ffba56b41 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Sat, 31 Oct 2026 20:20:07 +0530 Subject: [PATCH 14/30] [fix](auth) SU: placement follows the person for compute groups too, plus four review fixes - Compute-group (CLUSTER) USAGE joins workload-group USAGE on the placement role set: the narrowed set widened with the person's own granted roles. Stage and storage-vault usage are data access and stay narrowed. The SHOW output that enumerates workload-group and compute-group privileges reads the same set, so a narrowed session lists exactly what it can use (P1-3). The regression suite grants the cloud-mode compute group to both accounts, which is why its SU session failed in cloud_p0. - session_is_narrowed() honours the context-dependent fold guard like current_user() (P1-5). - resetConnection reverts the switch before the session variables are rebuilt, so the per-user defaults applied there are the authenticated account's (P2-1). - SU without a WORKLOAD GROUP clause clears the switcher's lane, so the per-query resolution falls through to the target's default group (P2-2). - The granted-role snapshot behind the SU ceiling runs under Auth's read lock (P2-4). Tests: SuUserNarrowingTest (lane reset), CloudAuthTest (compute-group placement under narrowing, in the mocked cloud harness the cluster check needs), FoldConstantTest (fold guard). Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/mysql/privilege/Auth.java | 72 +++++++++++-------- .../rules/FoldConstantRuleOnFE.java | 3 + .../trees/plans/commands/SuUserCommand.java | 16 ++--- .../org/apache/doris/qe/ConnectContext.java | 8 ++- .../doris/mysql/privilege/CloudAuthTest.java | 58 +++++++++++++++ .../mysql/privilege/SuUserNarrowingTest.java | 31 ++++++++ .../rules/expression/FoldConstantTest.java | 24 +++++++ .../auth_p0/test_su_session_narrowing.groovy | 8 +++ 8 files changed, 180 insertions(+), 40 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index fdb9f72d4b2d6f..ac1bda992f15ec 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -378,16 +378,21 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { * current session. Used for the SU ceiling check and for existence probing. */ public Set getGrantedRoleNames(UserIdentity userIdentity) { - Set names = Sets.newHashSet(userRoleManager.getRolesByUser(userIdentity)); - if (isLdapAuthEnabled()) { - Set ldapRoles = ldapManager.getUserRoles(userIdentity.getQualifiedUser()); - if (!CollectionUtils.isEmpty(ldapRoles)) { - for (Role r : ldapRoles) { - names.add(r.getRoleName()); + readLock(); + try { + Set names = Sets.newHashSet(userRoleManager.getRolesByUser(userIdentity)); + if (isLdapAuthEnabled()) { + Set ldapRoles = ldapManager.getUserRoles(userIdentity.getQualifiedUser()); + if (!CollectionUtils.isEmpty(ldapRoles)) { + for (Role r : ldapRoles) { + names.add(r.getRoleName()); + } } } + return names; + } finally { + readUnlock(); } - return names; } // The implicit information_schema/mysql read grants that RoleManager.createDefaultRole gives @@ -423,19 +428,19 @@ private static Role narrowingBaselineRole() { } /** - * The role set consulted for WORKLOAD GROUP privilege checks. Identical to - * {@link #getRolesByUserWithLdap} for a normal session. In an SU-narrowed session the - * narrowed set is widened with the effective identity's own granted roles (its default - * role (where direct grants live) plus explicit and LDAP roles), so the narrowed session - * may use exactly the workload groups - * the person's own session could use. A workload group is placement, not data authority: - * the narrowed session runs in the target's default_workload_group (the same resolution as - * the person's own session), and the person's USAGE on it is typically a direct grant that - * narrowing would otherwise drop. Only the workload-group predicate reads this set; - * table, database, resource and cloud checks stay on the narrowed set, so data access - * cannot widen through it. + * The role set consulted for PLACEMENT privilege checks: WORKLOAD GROUP usage and, in cloud + * mode, compute-group (CLUSTER) usage. Identical to {@link #getRolesByUserWithLdap} for a + * normal session. In an SU-narrowed session the narrowed set is widened with the effective + * identity's own granted roles (its default role (where direct grants live) plus explicit + * and LDAP roles), so the narrowed session may use exactly the workload groups and compute + * groups the person's own session could use. Placement is not data authority: the narrowed + * session runs in the target's default workload group and compute group (the same + * resolution as the person's own session), and the person's USAGE on them is typically a + * direct grant that narrowing would otherwise drop. Only the two placement predicates (and + * the SHOW output that enumerates them) read this set; table, database, resource, stage and + * storage-vault checks stay on the narrowed set, so data access cannot widen through it. */ - private Set getRolesForWorkloadGroupCheck(UserIdentity userIdentity) { + private Set getRolesForPlacementCheck(UserIdentity userIdentity) { Set roles = getRolesByUserWithLdap(userIdentity); ConnectContext ctx = ConnectContext.get(); if (ctx == null || !ctx.isSessionRoleNarrowed() @@ -736,7 +741,7 @@ protected boolean checkWorkloadGroupPriv(UserIdentity currentUser, String worklo return true; } - Set roles = getRolesForWorkloadGroupCheck(currentUser); + Set roles = getRolesForPlacementCheck(currentUser); PrivBitSet savedPrivs = PrivBitSet.of(); for (Role role : roles) { if (role.checkWorkloadGroupPriv(workloadGroupName, wanted, savedPrivs)) { @@ -760,7 +765,10 @@ protected boolean checkCloudPriv(UserIdentity currentUser, String cloudName, return true; } } - Set roles = getRolesByUserWithLdap(currentUser); + // compute-group USAGE is placement (see getRolesForPlacementCheck); stage and + // storage-vault usage are data access and stay on the narrowed set + Set roles = type == ResourceTypeEnum.CLUSTER + ? getRolesForPlacementCheck(currentUser) : getRolesByUserWithLdap(currentUser); PrivBitSet savedPrivs = PrivBitSet.of(); for (Role role : roles) { if (role.checkCloudPriv(cloudName, wanted, type, savedPrivs)) { @@ -1909,8 +1917,14 @@ public void getUserRoleWorkloadGroupPrivs(List> result, UserIdentit Set currentUserRole = null; if (!isCurrentUserAdmin) { - currentUserRole = userRoleManager.getRolesByUser(currentUserIdentity, false); - currentUserRole = currentUserRole == null ? new HashSet<>() : currentUserRole; + // the same role set the USAGE check consults: an SU-narrowed session lists the + // placement set, not the identity's full role union + currentUserRole = new HashSet<>(); + for (Role role : getRolesForPlacementCheck(currentUserIdentity)) { + if (!role.getRoleName().startsWith(RoleManager.DEFAULT_ROLE_PREFIX)) { + currentUserRole.add(role.getRoleName()); + } + } } roleManager.getRoleWorkloadGroupPrivs(result, currentUserRole); } finally { @@ -1920,18 +1934,18 @@ public void getUserRoleWorkloadGroupPrivs(List> result, UserIdentit private ResourcePrivTable getUserCloudClusterPrivTable(UserIdentity userIdentity) { ResourcePrivTable table = new ResourcePrivTable(); - Set roles = userRoleManager.getRolesByUser(userIdentity); - for (String roleName : roles) { - table.merge(roleManager.getRole(roleName).getCloudClusterPrivTable()); + // the same role set checkCloudPriv consults for compute groups (placement) + for (Role role : getRolesForPlacementCheck(userIdentity)) { + table.merge(role.getCloudClusterPrivTable()); } return table; } private ResourcePrivTable getUserCloudStagePrivTable(UserIdentity userIdentity) { ResourcePrivTable table = new ResourcePrivTable(); - Set roles = userRoleManager.getRolesByUser(userIdentity); - for (String roleName : roles) { - table.merge(roleManager.getRole(roleName).getCloudStagePrivTable()); + // stage usage is data access: the narrowed set, like checkCloudPriv + for (Role role : getRolesByUserWithLdap(userIdentity)) { + table.merge(role.getCloudStagePrivTable()); } return table; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java index e1109b8808ac1a..e09121facb8974 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java @@ -403,6 +403,9 @@ public Expression visitCurrentUser(CurrentUser currentUser, ExpressionRewriteCon @Override public Expression visitSessionIsNarrowed(SessionIsNarrowed fn, ExpressionRewriteContext context) { + if (!foldContextDependentExpressions) { + return fn; + } ConnectContext cctx = context.cascadesContext.getConnectContext(); return BooleanLiteral.of(cctx != null && cctx.isSessionRoleNarrowed()); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java index 691112b06cdc21..6e6fb7978041f5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java @@ -105,18 +105,18 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { ctx.setAuthenticatedIdentity(authenticated); ctx.setCurrentUserIdentity(userIdentity); ctx.setSessionRoleOverride(ImmutableSet.copyOf(roles)); - if (!Strings.isNullOrEmpty(workloadGroup)) { - // Placement only. USAGE on the session's workload group (this clause, or the - // target's default_workload_group when omitted) is checked per query against the - // narrowed set widened with the target's own granted roles - // (Auth.getRolesForWorkloadGroupCheck): the person's lane follows the person. - ctx.getSessionVariable().setWorkloadGroup(workloadGroup); - } + // Placement only. USAGE on the session's workload group (this clause, or the target's + // default_workload_group when omitted) is checked per query against the narrowed set + // widened with the target's own granted roles (Auth.getRolesForPlacementCheck): the + // person's lane follows the person. An omitted clause therefore CLEARS any group the + // switcher's session had set, so the per-query resolution falls through to the target's + // default instead of keeping the switcher's lane. + ctx.getSessionVariable().setWorkloadGroup(Strings.nullToEmpty(workloadGroup)); // the switch itself: authenticated + effective identity, roles, connection -- the // statement also lands in the audit log under the effective user LOG.info("SU: connection {} narrowed. authenticated={} effective={} roles={} workloadGroup={}", ctx.getConnectionId(), authenticated, userIdentity, roles, - workloadGroup == null ? "" : workloadGroup); + Strings.isNullOrEmpty(workloadGroup) ? "" : workloadGroup); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java index 10526eb0210e07..9f19260042c428 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java @@ -377,6 +377,11 @@ public void resetConnection() throws UserException { cleanupTemporaryTables(true); dbToTempTableNamesMap.clear(); } + // A narrowed session reverts to the AUTHENTICATED identity on connection reset — + // dropping only the override would widen the session to the target's full roles. + // It reverts BEFORE the session variables are rebuilt, so the per-user defaults applied + // there are the authenticated account's, not the switched-to target's. + revertSessionNarrowing(); resetSessionVariable(); userVars = new HashMap<>(); preparedStatementContextMap.clear(); @@ -386,9 +391,6 @@ public void resetConnection() throws UserException { insertResult = null; command = MysqlCommand.COM_SLEEP; returnRows = 0; - // A narrowed session reverts to the AUTHENTICATED identity on connection reset — - // dropping only the override would widen the session to the target's full roles. - revertSessionNarrowing(); } /** Revert an SU switch: back to the authenticated identity, override cleared. No-op if not switched. */ diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java index 8bff846542f31d..50320fe3698933 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java @@ -35,6 +35,7 @@ import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand; import org.apache.doris.nereids.trees.plans.commands.DropUserCommand; import org.apache.doris.nereids.trees.plans.commands.GrantResourcePrivilegeCommand; +import org.apache.doris.nereids.trees.plans.commands.GrantRoleCommand; import org.apache.doris.nereids.trees.plans.commands.GrantTablePrivilegeCommand; import org.apache.doris.nereids.trees.plans.commands.RevokeResourcePrivilegeCommand; import org.apache.doris.nereids.trees.plans.commands.ShowGrantsCommand; @@ -53,6 +54,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -477,4 +479,60 @@ public void testVirtualComputeGroup() throws Exception { Assertions.assertTrue(dropplan5 instanceof DropUserCommand); Assertions.assertDoesNotThrow(() -> ((DropUserCommand) dropplan5).run(connectContext, null)); } + + /** + * Compute-group (CLUSTER) USAGE is placement, like workload-group USAGE: an SU-narrowed session + * keeps exactly the compute groups the person's own session could use (direct grant and + * role-carried), never more, and the widening is scoped to the session's own identity. + * Lives here because the cluster privilege check resolves virtual compute groups through the + * cloud system-info service, which only this mocked cloud harness provides. + */ + @Test + public void testNarrowedSessionKeepsPersonsComputeGroupUsage() throws Exception { + NereidsParser parser = new NereidsParser(); + ((CreateRoleCommand) parser.parseSingle("CREATE ROLE su_cg_tenant")).run(connectContext, null); + ((CreateRoleCommand) parser.parseSingle("CREATE ROLE su_cg_lane")).run(connectContext, null); + UserIdentity person = new UserIdentity("su_cg_person", "%"); + person.analyze(); + CreateUserCommand createPerson = new CreateUserCommand( + new CreateUserInfo(false, new UserDesc(person), "su_cg_tenant", null, null)); + createPerson.getInfo().validate(); + auth.createUser(createPerson.getInfo()); + GrantRoleCommand grantLane = new GrantRoleCommand(person, Lists.newArrayList("su_cg_lane")); + grantLane.validate(); + auth.grantRoleCommand(grantLane); + // the login shape: USAGE granted DIRECTLY to the account (its default role) ... + ((GrantResourcePrivilegeCommand) parser.parseSingle( + "GRANT usage_priv ON cluster 'su_cg_person_lane' TO 'su_cg_person'@'%'")).run(connectContext, null); + // ... and one reachable only through a role the person holds but does not request + ((GrantResourcePrivilegeCommand) parser.parseSingle( + "GRANT usage_priv ON cluster 'su_cg_via_role' TO ROLE 'su_cg_lane'")).run(connectContext, null); + UserIdentity bystander = new UserIdentity("su_cg_bystander", "%"); + bystander.analyze(); + + // a real session for the person in place of the mocked one + ConnectContext session = new ConnectContext(); + session.setCurrentUserIdentity(person); + mockedCtxStatic.when(ConnectContext::get).thenReturn(session); + try { + // the person's own session + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_person_lane")); + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_via_role")); + Assertions.assertFalse(canUseComputeGroup(person, "su_cg_nobody")); + + // SU narrowed the session to the tenant role: placement still follows the person + session.setSessionRoleOverride(Collections.singleton("su_cg_tenant")); + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_person_lane")); + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_via_role")); + // never wider than the person's own session, and scoped to the session's own identity + Assertions.assertFalse(canUseComputeGroup(person, "su_cg_nobody")); + Assertions.assertFalse(canUseComputeGroup(bystander, "su_cg_person_lane")); + } finally { + mockedCtxStatic.when(ConnectContext::get).thenReturn(ctx); + } + } + + private boolean canUseComputeGroup(UserIdentity user, String computeGroup) { + return accessManager.checkCloudPriv(user, computeGroup, PrivPredicate.USAGE, ResourceTypeEnum.CLUSTER); + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index bcd506abcb03bf..82f50284a17600 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -298,6 +298,37 @@ public void testParserRoundTrip() { // runs on a handler thread with NO ConnectContext. Auth.setRpcSessionNarrowing lets the handler // apply the calling session's narrowed role subset there, so information_schema name visibility // matches the session instead of resolving the target's FULL roles (the pre-fix leak). + @Test + public void testOmittedWorkloadGroupClauseClearsSwitchersLane() throws Exception { + addUser("lane_svc", true); + addUser("lane_person", true); + createRole("lane_role"); + grantRole("GRANT 'lane_role' TO 'lane_person'@'%'"); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'lane_svc'@'%';"); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("lane_svc")); + // the switcher's own session runs in a lane of its own + ctx.getSessionVariable().setWorkloadGroup("wg_switcher_lane"); + ctx.setThreadLocalInfo(); + try { + new SuUserCommand(new UserIdentity("lane_person", "%"), + Collections.singletonList("lane_role"), null).run(ctx, null); + // no WORKLOAD GROUP clause: the switcher's lane is dropped, so the per-query + // resolution falls through to the target's default_workload_group + Assertions.assertEquals("", ctx.getSessionVariable().getWorkloadGroup()); + + ctx.revertSessionNarrowing(); + ctx.getSessionVariable().setWorkloadGroup("wg_switcher_lane"); + new SuUserCommand(new UserIdentity("lane_person", "%"), + Collections.singletonList("lane_role"), "wg_named").run(ctx, null); + // an explicit clause places the session exactly there + Assertions.assertEquals("wg_named", ctx.getSessionVariable().getWorkloadGroup()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + @Test public void testRpcSessionNarrowingAppliesOnMetadataRpcThread() throws Exception { addUser("rpcuser", true); diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java index be328b7317ac43..543992b80d43e1 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java @@ -94,6 +94,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.Sec; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondMicrosecondAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; import org.apache.doris.nereids.trees.expressions.functions.scalar.ShortCircuitIf; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sign; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sin; @@ -152,9 +153,11 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.lang.reflect.Field; import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; +import java.util.Collections; import java.util.List; class FoldConstantTest extends ExpressionRewriteTestHelper { @@ -1906,4 +1909,25 @@ private void assertInvalidCast(String expression, String expectedMessage) { () -> new ExpressionNormalization().rewrite(analyzed, context)); Assertions.assertEquals(expectedMessage, exception.getMessage()); } + + @Test + public void testSessionIsNarrowedFoldsOnlyWithSessionContext() throws Exception { + SessionIsNarrowed fn = new SessionIsNarrowed(); + // the context-free fold (expressions that must not depend on the session) leaves it alone, + // like current_user() + Field contextFreeField = FoldConstantRuleOnFE.class.getDeclaredField("CONTEXT_FREE_VISITOR_INSTANCE"); + contextFreeField.setAccessible(true); + FoldConstantRuleOnFE contextFree = (FoldConstantRuleOnFE) contextFreeField.get(null); + Assertions.assertSame(fn, contextFree.rewrite(fn, context)); + // the session-aware fold answers from the connection's SU state + Assertions.assertEquals(BooleanLiteral.FALSE, + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(fn, context)); + cascadesContext.getConnectContext().setSessionRoleOverride(Collections.singleton("tenant")); + try { + Assertions.assertEquals(BooleanLiteral.TRUE, + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(fn, context)); + } finally { + cascadesContext.getConnectContext().setSessionRoleOverride(null); + } + } } diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy index 70b6bed641dbaf..0ea19cbda7c210 100644 --- a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -58,6 +58,14 @@ suite("test_su_session_narrowing", "p0,auth") { // the framework connects with the default database in the URL, which needs a privilege on it sql """GRANT SELECT_PRIV ON ${context.config.defaultDb}.* TO '${svc}'@'%'""" sql """GRANT SELECT_PRIV ON ${context.config.defaultDb}.* TO '${person}'@'%'""" + if (isCloudMode()) { + // compute-group USAGE is placement: the narrowed session runs in the person's lane + def clusters = sql """SHOW CLUSTERS""" + assertTrue(!clusters.isEmpty()) + def validCluster = clusters[0][0] + sql """GRANT USAGE_PRIV ON CLUSTER `${validCluster}` TO '${person}'@'%'""" + sql """GRANT USAGE_PRIV ON CLUSTER `${validCluster}` TO '${svc}'@'%'""" + } try { // PROXY_PRIV is a global privilege, listed like any other From 3ba24eaf26d191e715096f2caa1df20a59183216 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Sat, 31 Oct 2026 20:20:07 +0530 Subject: [PATCH 15/30] [fix](auth) SU: information_schema scans that leave this FE fail closed on a mixed-build FE set The BE answers most information_schema scans through the coordinating FE itself, which applies the carried narrowing. Two shapes leave it: the master-routed TABLES scan (enable_schema_scan_from_master_fe) and the tables that fan out to every alive FE. An FE of an older build ignores current_roles and authorizes the target's full role union, so a session-narrowed session refuses those scans while the FE set runs mixed builds, exactly as a forwarded statement already does (Env.masterRunsSameBuild). Review finding P1-4 on the SU pull request (the rolling-upgrade window). Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/planner/SchemaScanNode.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java index df18c129858eaa..56f43ff15306bf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java @@ -97,12 +97,43 @@ public List getFrontendConjuncts() { public void finalizeForNereids() throws UserException { if (ConnectContext.get().getSessionVariable().enableSchemaScanFromMasterFe && tableName.equalsIgnoreCase("tables")) { + if (ConnectContext.get().isSessionRoleNarrowed() && !Env.getCurrentEnv().masterRunsSameBuild()) { + throw new UserException(mixedBuildMessage("the master FE")); + } frontendIP = Env.getCurrentEnv().getMasterHost(); frontendPort = Env.getCurrentEnv().getMasterRpcPort(); } else { frontendIP = FrontendOptions.getLocalHostAddress(); frontendPort = Config.rpc_port; } + TableIf tableIf = desc.getTable(); + if (ConnectContext.get().isSessionRoleNarrowed() && tableIf instanceof SchemaTable + && ((SchemaTable) tableIf).shouldFetchAllFe() && !allAliveFrontendsRunSameBuild()) { + throw new UserException(mixedBuildMessage("every alive FE")); + } + } + + /** + * A session-narrowed (SU) session's role subset rides in the BE's metadata RPCs to the FE + * (TGetTablesParams.current_roles and friends). An FE of an older build silently ignores the + * field and authorizes against the target's FULL role union, so a scan whose RPCs leave this + * FE (the master, or every alive FE) fails closed while the FE set runs mixed builds, exactly + * as a forwarded statement does (Env.masterRunsSameBuild). + */ + private static String mixedBuildMessage(String target) { + return "This information_schema scan of a session-narrowed (SU) session is answered by " + target + + ", which runs a different FE build than this FE and could not apply the narrowing. " + + "Retry once the rolling upgrade completes."; + } + + private static boolean allAliveFrontendsRunSameBuild() { + String local = Frontend.localBuildVersion(); + for (Frontend fe : Env.getCurrentEnv().getFrontends(null)) { + if (fe.isAlive() && !local.equals(fe.getVersion())) { + return false; + } + } + return true; } private void setFeAddrList(TPlanNode msg) { From f49d3bb50f5e8bb738e65b44e94f97ff16239e5c Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 00:41:48 +0530 Subject: [PATCH 16/30] [fix](auth) SU: PROXY_PRIV never climbs the administrative ladder; no "self" credential change under the switch PROXY_PRIV was ADMIN_PRIV in disguise. Nothing refused root or admin as a target and the role-name check ran with canBeAdmin=true, so `SU 'admin'@'%' WITH ROLES ('admin')` handed any PROXY_PRIV holder ADMIN_PRIV on *.*.*; every account granted the admin role, and every custom role carrying a global ADMIN/NODE/GRANT bit, was another way up. With the "self" branch of SET PASSWORD the switched session could then reset the target's password and keep the cluster. The switch now refuses the built-in accounts as targets (the COM_CHANGE_USER rule), refuses the built-in operator/admin roles and the per-user default role (checkRoleName with canBeAdmin=false), and after the granted-roles ceiling compares the union of the requested roles' GLOBAL privilege bits with what the switching account holds: a role conferring ADMIN_PRIV, NODE_PRIV or GRANT_PRIV is refused unless the switcher already holds that bit (Auth.getGlobalPrivsOfRoles). Narrowing can only narrow. The two "is this the user itself" exemptions that keyed on currentUserIdentity now also require that the session was not switched: SET PASSWORD and the COMMON keys of SET PROPERTY (default_workload_group, default_init_catalog, compute group, quota.*) take the GRANT / ADMIN privilege of the narrowed set in an SU session, as they do for any other account. The PROXY gate reports the access- denied error code (1227) with the PROXY_PRIV token like its sibling gates. Tests: SuUserNarrowingTest gains the ladder cases (admin/root target, admin/ operator/default role names, ADMIN_PRIV- and GRANT_PRIV-bearing roles for a PROXY-only switcher and for an administrator, a non-analyzable identity) and the closed self branches; the regression suite refuses SU to admin, the operator role, and SET PASSWORD / SET PROPERTY inside the narrowed session. Review findings F-01, F-06, F-16 and F-25 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/mysql/privilege/Auth.java | 22 ++++ .../trees/plans/commands/SuUserCommand.java | 36 +++++- .../plans/commands/info/SetPassVarOp.java | 5 +- .../commands/info/SetUserPropertyVarOp.java | 4 +- .../mysql/privilege/SuUserNarrowingTest.java | 117 ++++++++++++++++++ .../auth_p0/test_su_session_narrowing.groovy | 21 ++++ 6 files changed, 200 insertions(+), 5 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index ac1bda992f15ec..1aac6dadf8a65a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -84,6 +84,7 @@ import java.io.DataOutput; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -373,6 +374,27 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { return roles; } + /** + * The global privilege bits the named roles confer, union-ed; a name that is not a role + * contributes nothing. Used by the SU escalation check: a switch may not confer an + * administrative bit the switching account does not already hold. + */ + public PrivBitSet getGlobalPrivsOfRoles(Collection roleNames) { + readLock(); + try { + PrivBitSet conferred = PrivBitSet.of(); + for (String roleName : roleNames) { + Role role = roleManager.getRole(roleName); + if (role != null) { + role.getGlobalPrivTable().getPrivs(conferred); + } + } + return conferred; + } finally { + readUnlock(); + } + } + /** * The role names granted to an identity (user grants + LDAP), ignoring any SU narrowing of the * current session. Used for the SU ceiling check and for existence probing. diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java index 6e6fb7978041f5..21fc5fe902ec89 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java @@ -21,9 +21,13 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; import org.apache.doris.common.FeNameFormat; import org.apache.doris.mysql.privilege.Auth; +import org.apache.doris.mysql.privilege.PrivBitSet; import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.mysql.privilege.Privilege; import org.apache.doris.nereids.trees.plans.PlanType; import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; import org.apache.doris.qe.ConnectContext; @@ -79,12 +83,20 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { throw new AnalysisException("SU is not allowed in an already-switched session"); } if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.PROXY)) { - throw new AnalysisException("Access denied: SU requires PROXY_PRIV (or ADMIN_PRIV)"); + // the access-denied error code (1227) like every sibling privilege gate; the token is what + // clients and the regression suite match + ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "PROXY_PRIV"); } userIdentity.analyze(); + // The built-in accounts are never a proxy target, the same rule as COM_CHANGE_USER: the admin + // account holds the admin role, so a switch to it would hand every PROXY_PRIV holder ADMIN_PRIV. + if (userIdentity.isSystemUser()) { + throw new AnalysisException("SU to root or admin is forbidden"); + } for (String role : roles) { - FeNameFormat.checkRoleName(role, true /* can be admin */, "Invalid role in SU"); + // the built-in operator/admin roles carry NODE_PRIV/ADMIN_PRIV: a switch never confers them + FeNameFormat.checkRoleName(role, false /* admin not allowed */, "Invalid role in SU"); } Auth auth = Env.getCurrentEnv().getAuth(); @@ -100,6 +112,10 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { + " -- SU can only narrow, never mint authority"); } } + // PROXY_PRIV is a lesser privilege than ADMIN_PRIV: the switch may never move the switcher up + // the administrative ladder. A requested role that confers a global ADMIN_PRIV, NODE_PRIV or + // GRANT_PRIV is refused unless the switching account already holds that bit itself. + checkNoAdministrativeEscalation(ctx, auth.getGlobalPrivsOfRoles(roles)); UserIdentity authenticated = ctx.getCurrentUserIdentity(); ctx.setAuthenticatedIdentity(authenticated); @@ -119,6 +135,22 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { Strings.isNullOrEmpty(workloadGroup) ? "" : workloadGroup); } + private static void checkNoAdministrativeEscalation(ConnectContext ctx, PrivBitSet conferred) + throws AnalysisException { + refuseConferredPrivilege(ctx, conferred, Privilege.ADMIN_PRIV, PrivPredicate.ADMIN); + refuseConferredPrivilege(ctx, conferred, Privilege.NODE_PRIV, PrivPredicate.OPERATOR); + refuseConferredPrivilege(ctx, conferred, Privilege.GRANT_PRIV, PrivPredicate.GRANT); + } + + private static void refuseConferredPrivilege(ConnectContext ctx, PrivBitSet conferred, Privilege privilege, + PrivPredicate heldBySwitcher) throws AnalysisException { + if (conferred.containsPrivs(privilege) + && !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, heldBySwitcher)) { + throw new AnalysisException("SU roles confer global " + privilege + " which the switching account" + + " does not hold -- SU can only narrow, never mint authority"); + } + } + @Override public R accept(PlanVisitor visitor, C context) { return visitor.visitCommand(this, context); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java index 0453ffeb52cc7c..3185f498be07c9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java @@ -62,8 +62,9 @@ public void validate(ConnectContext ctx) throws UserException { } // check privs. - // 1. this is user itself - if (isSelf) { + // 1. this is user itself -- but never through a session-narrowed (SU) session: the switcher is + // not the person, so a credential change needs the GRANT privilege of the narrowed set + if (isSelf && ctx.getAuthenticatedIdentity() == null) { return; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java index bfbb42458e2e0f..aaf69245cc7725 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java @@ -77,7 +77,9 @@ public void validate(ConnectContext ctx) throws UserException { return; } } - boolean isSelf = user.equals(ctx.getQualifiedUser()); + // "self" never through a session-narrowed (SU) session: the switcher is not the person, so the + // person's account defaults may only be written with the ADMIN privilege of the narrowed set + boolean isSelf = ctx.getAuthenticatedIdentity() == null && user.equals(ctx.getQualifiedUser()); for (Pattern commPattern : UserProperty.COMMON_PROPERTIES) { Matcher matcher = commPattern.matcher(key); if (matcher.find()) { diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index 82f50284a17600..d1945b946e26a5 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -17,6 +17,7 @@ package org.apache.doris.mysql.privilege; +import org.apache.doris.analysis.PassVar; import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; @@ -25,6 +26,8 @@ import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand; import org.apache.doris.nereids.trees.plans.commands.GrantResourcePrivilegeCommand; import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; +import org.apache.doris.nereids.trees.plans.commands.info.SetPassVarOp; +import org.apache.doris.nereids.trees.plans.commands.info.SetUserPropertyVarOp; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; import org.apache.doris.qe.ConnectContext; import org.apache.doris.utframe.TestWithFeService; @@ -87,6 +90,25 @@ private void grantWorkloadGroupUsage(String sql) throws Exception { ((GrantResourcePrivilegeCommand) plan).run(connectContext, null); } + /** A session of a service account that holds nothing but the right to switch. */ + private ConnectContext proxySession(String switcher) throws Exception { + addUser(switcher, true); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO '" + switcher + "'@'%';"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident(switcher)); + ctx.setRemoteIP("127.0.0.1"); + ctx.setThreadLocalInfo(); + return ctx; + } + + private static void assertSwitchRefused(ConnectContext ctx, UserIdentity target, String role) { + Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(target, Collections.singletonList(role), null).run(ctx, null)); + // a refused switch leaves the session exactly as it was + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertFalse(ctx.isSessionRoleNarrowed()); + } + @Test public void testOverrideReplacesRoleUnionAndDoesNotLeak() throws Exception { addUser("alice", true); @@ -355,4 +377,99 @@ public void testRpcSessionNarrowingAppliesOnMetadataRpcThread() throws Exception // cleared -> full roles restored (no leak of the narrowing to later requests on this thread) Assertions.assertTrue(canSelectDb(rpcuser, "perso")); } + + /** + * PROXY_PRIV is a lesser privilege than ADMIN_PRIV: the built-in accounts are never a target, the + * built-in administrative roles and the per-user default role are never requested, and a role that + * confers a global ADMIN_PRIV / NODE_PRIV / GRANT_PRIV is refused unless the switching account + * already holds that bit itself. + */ + @Test + public void testSwitchNeverClimbsTheAdministrativeLadder() throws Exception { + addUser("ladder_person", true); + addUser("ladder_admin_holder", true); + createRole("ladder_plain"); + createRole("ladder_ops_admin"); + createRole("ladder_granter"); + grantRole("GRANT 'ladder_plain','ladder_ops_admin','ladder_granter' TO 'ladder_person'@'%'"); + grantRole("GRANT 'admin' TO 'ladder_admin_holder'@'%'"); + grantPriv("GRANT ADMIN_PRIV ON *.*.* TO ROLE 'ladder_ops_admin';"); + grantPriv("GRANT GRANT_PRIV ON *.*.* TO ROLE 'ladder_granter';"); + UserIdentity person = new UserIdentity("ladder_person", "%"); + + ConnectContext ctx = proxySession("ladder_svc"); + try { + // the built-in accounts are never a target, whatever role is named + assertSwitchRefused(ctx, new UserIdentity("admin", "%"), "admin"); + assertSwitchRefused(ctx, new UserIdentity("root", "%"), "operator"); + // the built-in administrative roles and the default role are never requested, even when + // the target holds them + assertSwitchRefused(ctx, new UserIdentity("ladder_admin_holder", "%"), "admin"); + assertSwitchRefused(ctx, person, "operator"); + assertSwitchRefused(ctx, person, "default_role_rbac_ladder_person@%"); + // a granted role that confers a global administrative bit the switcher does not hold + assertSwitchRefused(ctx, person, "ladder_ops_admin"); + assertSwitchRefused(ctx, person, "ladder_granter"); + // an identity that does not analyze never reaches the switch + assertSwitchRefused(ctx, new UserIdentity("bad user", "%"), "ladder_plain"); + // the plain role is what PROXY_PRIV buys + new SuUserCommand(person, Collections.singletonList("ladder_plain"), null).run(ctx, null); + Assertions.assertEquals("ladder_person", ctx.getQualifiedUser()); + Assertions.assertFalse(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.ADMIN)); + Assertions.assertFalse(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.GRANT)); + } finally { + connectContext.setThreadLocalInfo(); + } + + // an administrator already holds every bit: narrowing a person to an administrative role is + // no escalation for it + ConnectContext admin = new ConnectContext(); + admin.setCurrentUserIdentity(ident("root")); + admin.setThreadLocalInfo(); + try { + new SuUserCommand(person, Collections.singletonList("ladder_ops_admin"), null).run(admin, null); + Assertions.assertEquals("ladder_person", admin.getQualifiedUser()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + /** + * After the switch the current identity IS the target, but the switcher is not the person: the + * "self" exemptions of SET PASSWORD and SET PROPERTY are closed under the switch, so a credential + * or account-default change needs the GRANT / ADMIN privilege of the narrowed set. + */ + @Test + public void testSelfBranchesAreClosedUnderTheSwitch() throws Exception { + addUser("self_person", true); + createRole("self_role"); + grantRole("GRANT 'self_role' TO 'self_person'@'%'"); + UserIdentity person = new UserIdentity("self_person", "%"); + + // the person's own session keeps both self branches + ConnectContext own = new ConnectContext(); + own.setCurrentUserIdentity(ident("self_person")); + own.setThreadLocalInfo(); + try { + new SetPassVarOp(null, new PassVar("", true)).validate(own); + new SetPassVarOp(new UserIdentity("self_person", "%"), new PassVar("", true)).validate(own); + new SetUserPropertyVarOp("self_person", "default_init_catalog", "internal").validate(own); + } finally { + connectContext.setThreadLocalInfo(); + } + + ConnectContext ctx = proxySession("self_svc"); + try { + new SuUserCommand(person, Collections.singletonList("self_role"), null).run(ctx, null); + Assertions.assertEquals("self_person", ctx.getQualifiedUser()); + Assertions.assertThrows(AnalysisException.class, () -> + new SetPassVarOp(null, new PassVar("", true)).validate(ctx)); + Assertions.assertThrows(AnalysisException.class, () -> + new SetPassVarOp(new UserIdentity("self_person", "%"), new PassVar("", true)).validate(ctx)); + Assertions.assertThrows(AnalysisException.class, () -> + new SetUserPropertyVarOp("self_person", "default_init_catalog", "internal").validate(ctx)); + } finally { + connectContext.setThreadLocalInfo(); + } + } } diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy index 0ea19cbda7c210..bc46a001a6af08 100644 --- a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -90,6 +90,16 @@ suite("test_su_session_narrowing", "p0,auth") { sql """SU '${person}'@'%' WITH ROLES ('${otherRole}')""" exception "not granted" } + // PROXY_PRIV is a lesser privilege than ADMIN_PRIV: the built-in accounts are never a + // target and the built-in administrative roles are never requested + test { + sql """SU 'admin'@'%' WITH ROLES ('admin')""" + exception "forbidden" + } + test { + sql """SU '${person}'@'%' WITH ROLES ('operator')""" + exception "Invalid role" + } } connect(svc, pwd, context.config.jdbcUrl) { @@ -129,6 +139,17 @@ suite("test_su_session_narrowing", "p0,auth") { sql """SU '${svc}'@'%' WITH ROLES ('${tenantRole}')""" exception "already-switched" } + + // the switcher is not the person: the "self" exemptions of SET PASSWORD and SET PROPERTY + // are closed under the switch + test { + sql """SET PASSWORD = PASSWORD('${pwd}')""" + exception "denied" + } + test { + sql """SET PROPERTY 'default_init_catalog' = 'internal'""" + exception "denied" + } } // the person's own login is untouched: both grants live, nothing is narrowed From fe7b588375347eaa269864f8737f7129d9629678 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 00:43:55 +0530 Subject: [PATCH 17/30] [fix](auth) SU: journal the heartbeat build so followers can forward, and gate every SQL forward masterRunsSameBuild() was false on every follower and observer, forever. Only the master runs HeartbeatMgr; the other FEs learn heartbeat state by replaying the HbPackage journal, which is Gson-serialized under the exclusion strategy that skips every field without @SerializedName -- and FrontendHbResponse.version had none. So fe.getVersion() of the master's entry was always null off-master, and every forwarded statement of a narrowed session on a non-master FE failed with the "different build" error on a fully upgraded cluster. The version is now annotated; an older FE replaying a newer master's package ignores the extra key (Gson), and a newer follower replaying an older master's package keeps null and stays closed, as intended. The gate itself protected forwardToMaster only. KILL QUERY for a query not found locally fans out as SQL to every other FE through FEOpExecutor, the one other FE->FE SQL sender, where an old remote FE would run the proxy session with the target's full role union. The gate moves into FEOpExecutor.buildStmtForwardParams, the single place that sets is_su_user, keyed on the receiving FE's address: Env.frontendRunsSameBuild(host, rpcPort) (this FE itself, or an alive FE whose journaled build equals ours); masterRunsSameBuild() delegates to it. Tests: ForwardedSessionNarrowingTest replays a FrontendHbResponse through the Gson round trip of an HbPackage onto a Frontend and asserts the build lands; a narrowed forward to an FE of unknown build is refused while the known one is taken; the probe now targets this FE's own address. Review findings F-02 and F-20 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../java/org/apache/doris/catalog/Env.java | 17 ++++- .../org/apache/doris/qe/FEOpExecutor.java | 16 +++- .../org/apache/doris/qe/StmtExecutor.java | 12 +-- .../doris/system/FrontendHbResponse.java | 3 + .../qe/ForwardedSessionNarrowingTest.java | 74 ++++++++++++++++++- 5 files changed, 102 insertions(+), 20 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index 762341f7a29be0..524b6436005f73 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -5634,10 +5634,21 @@ public boolean masterRunsSameBuild() { if (isMaster()) { return true; } - String masterHost = getMasterHost(); - int masterRpcPort = getMasterRpcPort(); + return frontendRunsSameBuild(getMasterHost(), getMasterRpcPort()); + } + + /** + * Whether the FE at this RPC address is this FE itself or reports the same build as this FE. + * Every FE sees every other FE's build: the master heartbeats all of them and journals the + * responses (HbPackage, FrontendHbResponse.version), which the other FEs replay. An FE this FE + * does not know, or a dead one, counts as a different build. + */ + public boolean frontendRunsSameBuild(String host, int rpcPort) { + if (host.equals(selfNode.getHost()) && rpcPort == Config.rpc_port) { + return true; + } for (Frontend fe : frontends.values()) { - if (fe.getHost().equals(masterHost) && fe.getRpcPort() == masterRpcPort) { + if (fe.getHost().equals(host) && fe.getRpcPort() == rpcPort) { return fe.isAlive() && Frontend.localBuildVersion().equals(fe.getVersion()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java index fae508f5e95a60..3f50f11069ef89 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java @@ -196,9 +196,21 @@ protected TMasterOpRequest buildStmtForwardParams() throws AnalysisException { params.setStmtId(ctx.getStmtId()); params.setCurrentUserIdent(ctx.getCurrentUserIdentity().toThrift()); if (ctx.isSessionRoleNarrowed()) { + // A session-narrowed (SU) session carries its active role subset in the request + // (is_su_user / current_roles) and the receiving FE installs it before executing. An + // older FE silently ignores those fields and would authorize the statement against the + // target's FULL role union, so every SQL forward from a narrowed session -- to the master + // or, for KILL QUERY, to any other FE -- fails closed unless the receiver is known to run + // this same build (a rolling upgrade upgrades the master last). + if (!Env.getCurrentEnv().frontendRunsSameBuild(feAddr.getHostname(), feAddr.getPort())) { + throw new AnalysisException("This statement would be forwarded to FE " + feAddr.getHostname() + + ":" + feAddr.getPort() + ", which runs a different build than this FE; a " + + "session-narrowed (SU) session forwards only to an FE known to apply the narrowing. " + + "Retry once the rolling upgrade completes, or run it in a non-narrowed session."); + } Set sessionRoleOverride = ctx.getSessionRoleOverride(); - // Session-narrowed (SU) session: carry the active role subset so the master authorizes - // the forwarded statement against exactly this set, never the target's full role union. + // carry the active role subset so the receiving FE authorizes the forwarded statement + // against exactly this set, never the target's full role union params.setIsSuUser(true); params.setCurrentRoles(new HashSet<>(sessionRoleOverride)); if (ctx.getAuthenticatedIdentity() != null) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index fa62b76899d4bd..99c2c72bbe222a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -1525,17 +1525,7 @@ public boolean isProfileSafeStmt() { } private void forwardToMaster() throws Exception { - // A session-narrowed (SU) session carries its active role subset in the forward request - // (TMasterOpRequest.is_su_user / current_roles) and the master installs it before - // executing. An older master silently ignores those fields and would authorize the - // statement against the target's FULL role union, so fail closed unless the master is - // known to run this same build (a rolling upgrade upgrades the master last). - if (context.isSessionRoleNarrowed() && !Env.getCurrentEnv().masterRunsSameBuild()) { - throw new UserException("This statement would be forwarded to the master FE, which runs a " - + "different build than this FE; a session-narrowed (SU) session forwards only when " - + "the master is known to apply the narrowing. Retry once the rolling upgrade " - + "completes, or run it in a non-narrowed session."); - } + // a session-narrowed (SU) session's same-build gate sits in FEOpExecutor.buildStmtForwardParams masterOpExecutor = new MasterOpExecutor(originStmt, context, redirectStatus, isQuery()); if (LOG.isDebugEnabled()) { LOG.debug("need to transfer to Master. stmt: {}", context.getStmtId()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java b/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java index 3a16cfd5a97edb..cd2586f20a49f1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java +++ b/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java @@ -40,6 +40,9 @@ public class FrontendHbResponse extends HeartbeatResponse implements Writable { private int arrowFlightSqlPort; @SerializedName(value = "replayedJournalId") private long replayedJournalId; + // Journaled: a non-master FE learns the build every FE runs only through the master's + // HbPackage replay, and Env.frontendRunsSameBuild compares its own build against it. + @SerializedName(value = "version") private String version; private long feStartTime; private long processUUID; diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java index e3c361a3e241ee..c417ef285d0741 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java @@ -20,9 +20,15 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.Config; import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.ha.FrontendNodeType; import org.apache.doris.mysql.MysqlCapability; import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.persist.HbPackage; +import org.apache.doris.persist.gson.GsonUtils; +import org.apache.doris.system.Frontend; +import org.apache.doris.system.FrontendHbResponse; import org.apache.doris.thrift.TMasterOpRequest; import org.apache.doris.thrift.TNetworkAddress; import org.apache.doris.utframe.TestWithFeService; @@ -59,13 +65,18 @@ private boolean canSelectDb(UserIdentity user, String db) { } private static TMasterOpRequest forwardRequestOf(ConnectContext ctx) throws AnalysisException { - return new ForwardParamsProbe(ctx).build(); + return new ForwardParamsProbe(ctx, selfAddress()).build(); } - /** Exposes the request the origin FE would send for this session. */ + /** The single test FE, as an FE address: the one receiver that is known to run this build. */ + private static TNetworkAddress selfAddress() { + return new TNetworkAddress(Env.getCurrentEnv().getSelfNode().getHost(), Config.rpc_port); + } + + /** Exposes the request the origin FE would send for this session to the FE at the address. */ private static class ForwardParamsProbe extends FEOpExecutor { - private ForwardParamsProbe(ConnectContext ctx) { - super(new TNetworkAddress("127.0.0.1", 9020), new OriginStatement("show frontends", 0), ctx, true); + private ForwardParamsProbe(ConnectContext ctx, TNetworkAddress receiver) { + super(receiver, new OriginStatement("show frontends", 0), ctx, true); } private TMasterOpRequest build() throws AnalysisException { @@ -160,5 +171,60 @@ public void testMasterItselfAlwaysRunsTheSameBuild() { // the single-FE test environment is its own master: nothing to forward, nothing to refuse Assertions.assertTrue(Env.getCurrentEnv().isMaster()); Assertions.assertTrue(Env.getCurrentEnv().masterRunsSameBuild()); + TNetworkAddress self = selfAddress(); + Assertions.assertTrue(Env.getCurrentEnv().frontendRunsSameBuild(self.getHostname(), self.getPort())); + // an FE this FE does not know counts as a different build + Assertions.assertFalse(Env.getCurrentEnv().frontendRunsSameBuild("203.0.113.9", 9020)); + } + + /** + * The same-build gate sits at the single place that sets is_su_user, so every SQL forward of a + * narrowed session -- to the master, or for KILL QUERY to any other FE -- fails closed unless the + * receiving FE is known to run this build. A plain session forwards anywhere. + */ + @Test + public void testNarrowedForwardToAnFeOfUnknownBuildIsRefused() throws Exception { + TNetworkAddress unknown = new TNetworkAddress("203.0.113.9", 9020); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("alice")); + ctx.setRemoteIP("127.0.0.1"); + ctx.setCapability(MysqlCapability.DEFAULT_CAPABILITY); + Assertions.assertFalse(new ForwardParamsProbe(ctx, unknown).build().isSetIsSuUser()); + + ctx.setAuthenticatedIdentity(ident("svc")); + ctx.setSessionRoleOverride(Collections.singleton("tenant_a")); + AnalysisException refused = Assertions.assertThrows(AnalysisException.class, + () -> new ForwardParamsProbe(ctx, unknown).build()); + Assertions.assertTrue(refused.getMessage().contains("different build"), refused.getMessage()); + // the FE known to run this build still takes the narrowed forward + Assertions.assertTrue(forwardRequestOf(ctx).isIsSuUser()); + } + + /** + * A non-master FE learns the master's build only from the HbPackage journal the master writes + * after every heartbeat round: the version must survive the Gson round trip of that journal and + * land on the replayed Frontend, or masterRunsSameBuild() is false off-master forever. + */ + @Test + public void testHeartbeatJournalCarriesTheBuildToReplayingFrontends() { + String build = Frontend.localBuildVersion(); + FrontendHbResponse hb = new FrontendHbResponse("fe_probe", 9030, 9020, 0, 7L, System.currentTimeMillis(), + build, 0L, Collections.emptyList(), 1L, ""); + FrontendHbResponse roundTripped = GsonUtils.GSON.fromJson(GsonUtils.GSON.toJson(hb), + FrontendHbResponse.class); + Assertions.assertEquals(build, roundTripped.getVersion()); + + HbPackage hbPackage = new HbPackage(); + hbPackage.addHbResponse(hb); + HbPackage replayed = GsonUtils.GSON.fromJson(GsonUtils.GSON.toJson(hbPackage), HbPackage.class); + Assertions.assertEquals(1, replayed.getHbResults().size()); + FrontendHbResponse fromJournal = (FrontendHbResponse) replayed.getHbResults().get(0); + + // what a follower's HeartbeatMgr.replayHearbeat does with the journaled response + Frontend fe = new Frontend(FrontendNodeType.FOLLOWER, "fe_probe", "203.0.113.9", 9010); + Assertions.assertTrue(fe.handleHbResponse(fromJournal, true)); + Assertions.assertTrue(fe.isAlive()); + Assertions.assertEquals(build, fe.getVersion()); + Assertions.assertEquals(9020, fe.getRpcPort()); } } From 25a095daf2c8ed974bdb48a2df63b68d42c2b588 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 00:47:59 +0530 Subject: [PATCH 18/30] [fix](auth) SU: the narrowed set is a live intersection with the target's grants, listed consistently The override was pinned by name at switch time: narrowedRoleSet resolved the names through roleManager alone and the ceiling (getGrantedRoleNames) ran once, in SuUserCommand. A REVOKE of the role from the target, a DROP USER, or a DROP ROLE + CREATE ROLE under the same name were not honoured for the life of a pooled gateway session, while the person's own session re-reads its grants on every check. narrowedRoleSet now takes the target and intersects the override with getGrantedRoleNames(target) on every resolution -- live session, forwarded proxy session and BE->FE RPC thread alike -- so a revoked role drops out at the next check and a dropped target keeps only the information_schema/mysql baseline. The live-session path also resolved the full role map and the LDAP roles before discarding them when an override existed; the override test now comes right after the RPC thread-local, before any of that work. SHOW GRANTS mixed three resolutions: the Roles column (non-LDAP branch) and the storage-vault column read the raw user->role map, so a narrowed session listed roles it does not have, while every other column was narrowed. Both now resolve through getRolesByUserWithLdap like the rest. The synthetic baseline role was named `su_narrowing_baseline`, a name a row policy could be bound to and that the LDAP branch of SHOW GRANTS listed; it now carries the default-role prefix (so it can be neither CREATEd nor bound -- both validate the role) and is skipped by getRoleNamesByUserWithLdap, so no listing shows it. It is built once at class load from constant inputs and can no longer be null: the unreachable catch(Exception)/null branch is gone, a failure to build it fails the FE fast. Tests: SuUserNarrowingTest covers REVOKE after the switch, DROP ROLE + CREATE ROLE under the same name, DROP USER of the target, the SHOW GRANTS Roles column inside the narrowed session, and the unnameable baseline role. Review findings F-03, F-11, F-12 and F-23 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/mysql/privilege/Auth.java | 76 ++++++++-------- .../mysql/privilege/SuUserNarrowingTest.java | 90 +++++++++++++++++++ 2 files changed, 131 insertions(+), 35 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index 1aac6dadf8a65a..edfdc52b71a8a2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -315,20 +315,25 @@ public static void clearRpcSessionNarrowing() { RPC_SESSION_NARROWING.remove(); } - // The SU-narrowed role set: exactly the override roles + the implicit information_schema/mysql - // read baseline (see narrowingBaselineRole). Shared by the live-session and RPC-handler paths. - private Set narrowedRoleSet(Set overrideRoleNames) { + // The SU-narrowed role set of a target identity: the override roles the target STILL holds + the + // implicit information_schema/mysql read baseline (SU_NARROWING_BASELINE). The ceiling is + // re-evaluated on every resolution, exactly as a normal session re-reads its grants on every + // check: a role revoked from the target after the switch (or dropped and re-created under the + // same name, or every role after DROP USER) drops out of the open narrowed session at once. + // Shared by the live-session, forwarded and RPC-handler paths. + private Set narrowedRoleSet(UserIdentity target, Set overrideRoleNames) { + Set stillGranted = getGrantedRoleNames(target); Set narrowed = Sets.newHashSet(); for (String roleName : overrideRoleNames) { + if (!stillGranted.contains(roleName)) { + continue; + } Role role = roleManager.getRole(roleName); if (role != null) { narrowed.add(role); } } - Role baseline = narrowingBaselineRole(); - if (baseline != null) { - narrowed.add(baseline); - } + narrowed.add(SU_NARROWING_BASELINE); return narrowed; } @@ -338,9 +343,16 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { Set rpcOverride = rpcNarrow.get(userIdentity); if (rpcOverride != null) { // SU narrowing: a BE->FE metadata RPC carried this session's narrowed role subset. - return narrowedRoleSet(rpcOverride); + return narrowedRoleSet(userIdentity, rpcOverride); } } + ConnectContext ctx = ConnectContext.get(); + if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity()) && ctx.isSessionRoleNarrowed()) { + // SU-narrowed session: the override REPLACES every role source (user grants, LDAP, + // authenticated roles), so none of them is resolved first. Return the override roles the + // target still holds + the information_schema/mysql read baseline (see narrowedRoleSet). + return narrowedRoleSet(userIdentity, ctx.getSessionRoleOverride()); + } Set roles = Sets.newHashSet(); Set roleNames = userRoleManager.getRolesByUser(userIdentity); for (String roleName : roleNames) { @@ -355,15 +367,7 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { roles.addAll(ldapRoles); } } - ConnectContext ctx = ConnectContext.get(); if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity())) { - if (ctx.isSessionRoleNarrowed()) { - Set sessionRoleOverride = ctx.getSessionRoleOverride(); - // SU-narrowed session: the override REPLACES every role source (user grants, LDAP, - // authenticated roles); SuUserCommand enforced the ceiling at switch time. Return the - // override roles + the information_schema/mysql read baseline (see narrowedRoleSet). - return narrowedRoleSet(sessionRoleOverride); - } for (String roleName : ctx.getAuthenticatedRoles()) { Role role = roleManager.getRole(roleName); if (role != null) { @@ -420,14 +424,14 @@ public Set getGrantedRoleNames(UserIdentity userIdentity) { // The implicit information_schema/mysql read grants that RoleManager.createDefaultRole gives // every user's default role, isolated into a synthetic role so an SU-narrowed session keeps // them (self-filtered by the session's real privileges) without re-including the default role's - // privilege-bearing direct grants. Built once. - private static volatile Role narrowingBaselineRoleCache = null; + // privilege-bearing direct grants. Its name carries the default-role prefix, so it can be neither + // CREATEd nor bound by a row policy (both validate the role), and it is hidden from every role + // listing (getRoleNamesByUserWithLdap). Built once, at class load, from constant inputs: a + // failure here is a build defect, and the FE must not start without the role. + public static final String SU_NARROWING_BASELINE_ROLE = RoleManager.DEFAULT_ROLE_PREFIX + "su_narrowing_baseline"; + private static final Role SU_NARROWING_BASELINE = buildNarrowingBaselineRole(); - private static Role narrowingBaselineRole() { - Role cached = narrowingBaselineRoleCache; - if (cached != null) { - return cached; - } + private static Role buildNarrowingBaselineRole() { try { List tablePatterns = Lists.newArrayList(); TablePattern info = new TablePattern(DEFAULT_CATALOG, InfoSchemaDb.DATABASE_NAME, "*"); @@ -438,14 +442,10 @@ private static Role narrowingBaselineRole() { tablePatterns.add(mysql); WorkloadGroupPattern wg = new WorkloadGroupPattern(WorkloadGroupMgr.DEFAULT_GROUP_NAME); wg.analyze(); - Role role = new Role("su_narrowing_baseline", tablePatterns, + return new Role(SU_NARROWING_BASELINE_ROLE, tablePatterns, PrivBitSet.of(Privilege.SELECT_PRIV), wg, PrivBitSet.of(Privilege.USAGE_PRIV)); - narrowingBaselineRoleCache = role; - return role; - } catch (Exception e) { - LOG.warn("failed to build SU narrowing baseline role (information_schema/mysql reads " - + "will not be preserved in narrowed sessions)", e); - return null; + } catch (AnalysisException e) { + throw new IllegalStateException("failed to build the SU narrowing baseline role", e); } } @@ -484,6 +484,10 @@ public Set getRoleNamesByUserWithLdap(UserIdentity user, boolean showUse Set res = Sets.newHashSetWithExpectedSize(rolesByUserWithLdap.size()); for (Role role : rolesByUserWithLdap) { String roleName = role.getRoleName(); + if (roleName.equals(SU_NARROWING_BASELINE_ROLE)) { + // the synthetic SU baseline is an implementation detail, never a role anybody granted + continue; + } if (showUserDefaultRole || !roleName.startsWith(RoleManager.DEFAULT_ROLE_PREFIX)) { res.add(roleName); } @@ -1764,8 +1768,10 @@ private void getUserAuthInfo(List> userAuthInfos, UserIdentity user userAuthInfo.add(requireSan); userAuthInfo.add(Strings.nullToEmpty(user.getAuthIntegration())); // ============== Roles ============== - userAuthInfo.add(Joiner.on(",").join(userRoleManager - .getRolesByUser(userIdent, ConnectContext.get().getSessionVariable().showUserDefaultRole))); + // the same resolution as every privilege column below: an SU-narrowed session lists + // its narrowed set, not the identity's full role union + userAuthInfo.add(Joiner.on(",").join(getRoleNamesByUserWithLdap(userIdent, + ConnectContext.get().getSessionVariable().showUserDefaultRole))); } } // ==============GlobalPrivs============== @@ -1974,9 +1980,9 @@ private ResourcePrivTable getUserCloudStagePrivTable(UserIdentity userIdentity) private ResourcePrivTable getUserStorageVaultPrivTable(UserIdentity userIdentity) { ResourcePrivTable table = new ResourcePrivTable(); - Set roles = userRoleManager.getRolesByUser(userIdentity); - for (String roleName : roles) { - table.merge(roleManager.getRole(roleName).getStorageVaultPrivTable()); + // storage-vault usage is data access: the narrowed set, like checkCloudPriv + for (Role role : getRolesByUserWithLdap(userIdentity)) { + table.merge(role.getStorageVaultPrivTable()); } return table; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index d1945b946e26a5..3c628ff6ae46c2 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -21,8 +21,10 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.proc.AuthProcDir; import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.trees.plans.commands.Command; import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand; import org.apache.doris.nereids.trees.plans.commands.GrantResourcePrivilegeCommand; import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; @@ -30,6 +32,7 @@ import org.apache.doris.nereids.trees.plans.commands.info.SetUserPropertyVarOp; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.StmtExecutor; import org.apache.doris.utframe.TestWithFeService; import org.junit.jupiter.api.Assertions; @@ -101,6 +104,23 @@ private ConnectContext proxySession(String switcher) throws Exception { return ctx; } + /** Runs an administrative statement on the test's admin session, then restores the thread's context. */ + private void runAsAdmin(String sql) throws Exception { + LogicalPlan plan = new NereidsParser().parseSingle(sql); + Assertions.assertTrue(plan instanceof Command, sql); + ConnectContext current = ConnectContext.get(); + connectContext.setThreadLocalInfo(); + try { + ((Command) plan).run(connectContext, new StmtExecutor(connectContext, sql)); + } finally { + current.setThreadLocalInfo(); + } + } + + private static String shownRoles(UserIdentity user) { + return Env.getCurrentEnv().getAuth().getAuthInfo(user).get(0).get(AuthProcDir.TITLE_NAMES.indexOf("Roles")); + } + private static void assertSwitchRefused(ConnectContext ctx, UserIdentity target, String role) { Assertions.assertThrows(AnalysisException.class, () -> new SuUserCommand(target, Collections.singletonList(role), null).run(ctx, null)); @@ -472,4 +492,74 @@ public void testSelfBranchesAreClosedUnderTheSwitch() throws Exception { connectContext.setThreadLocalInfo(); } } + + /** + * The narrowed set is a live intersection with the target's grants, not a snapshot taken at the + * switch: a role revoked from the target afterwards (or dropped and re-created under its name, + * or every role after DROP USER) drops out of the open narrowed session on the next check, + * exactly as it would for the person's own session. SHOW GRANTS lists that same set, and never + * the synthetic baseline role. + */ + @Test + public void testCeilingIsReEvaluatedOnEveryCheck() throws Exception { + addUser("ceil_person", true); + addUser("ceil_gone", true); + createRole("ceil_role"); + createRole("ceil_gone_role"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ceil_role';"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ceil_gone_role';"); + grantRole("GRANT 'ceil_role' TO 'ceil_person'@'%'"); + grantRole("GRANT 'ceil_gone_role' TO 'ceil_gone'@'%'"); + UserIdentity person = ident("ceil_person"); + UserIdentity gone = ident("ceil_gone"); + + ConnectContext ctx = proxySession("ceil_svc"); + try { + new SuUserCommand(new UserIdentity("ceil_person", "%"), + Collections.singletonList("ceil_role"), null).run(ctx, null); + Assertions.assertTrue(canSelectDb(person, "test")); + Assertions.assertEquals("ceil_role", shownRoles(person)); + + // a REVOKE from the target is honoured by the open narrowed session at once + runAsAdmin("REVOKE 'ceil_role' FROM 'ceil_person'@'%'"); + Assertions.assertFalse(canSelectDb(person, "test")); + // the information_schema/mysql baseline stays, and is not listed as a role + Assertions.assertTrue(canSelectInfoSchema(person)); + Assertions.assertEquals("", shownRoles(person)); + + // DROP ROLE + CREATE ROLE under the same name is not a grant to the target + runAsAdmin("DROP ROLE ceil_role"); + runAsAdmin("CREATE ROLE ceil_role"); + runAsAdmin("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ceil_role'"); + Assertions.assertFalse(canSelectDb(person, "test")); + // granting it to the target again is + runAsAdmin("GRANT 'ceil_role' TO 'ceil_person'@'%'"); + Assertions.assertTrue(canSelectDb(person, "test")); + Assertions.assertEquals("ceil_role", shownRoles(person)); + } finally { + connectContext.setThreadLocalInfo(); + } + + ConnectContext ctx2 = proxySession("ceil_svc2"); + try { + new SuUserCommand(new UserIdentity("ceil_gone", "%"), + Collections.singletonList("ceil_gone_role"), null).run(ctx2, null); + Assertions.assertTrue(canSelectDb(gone, "test")); + // a dropped target holds nothing: the session keeps only the metadata baseline + runAsAdmin("DROP USER 'ceil_gone'@'%'"); + Assertions.assertFalse(canSelectDb(gone, "test")); + Assertions.assertTrue(canSelectInfoSchema(gone)); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testSyntheticBaselineRoleCannotBeNamed() { + // the default-role prefix keeps it out of CREATE ROLE, out of row-policy binding (which + // validates the role) and out of every role listing + Assertions.assertTrue(Auth.SU_NARROWING_BASELINE_ROLE.startsWith(RoleManager.DEFAULT_ROLE_PREFIX)); + Assertions.assertThrows(Exception.class, () -> runAsAdmin("CREATE ROLE " + Auth.SU_NARROWING_BASELINE_ROLE)); + Assertions.assertFalse(Env.getCurrentEnv().getAuth().doesRoleExist(Auth.SU_NARROWING_BASELINE_ROLE)); + } } From c542fa252fd724506975401ccc6d0d5f66c96770 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 00:48:56 +0530 Subject: [PATCH 19/30] [fix](auth) SU: the authorization-SPI role bridge resolves through the narrowing choke point EngineAuthorizationContext.rolesOf, what a ranger-hive or third-party authorization source is told when it asks the engine who holds which roles, read Auth.getRolesByUser(ident, false): the raw user->role map, never getRolesByUserWithLdap. So a role-bound Ranger policy on a ranger-hive catalog evaluated the target's FULL role union for a session-narrowed (SU) session, and `SU alice WITH ROLES ('tenant_42')` could read what a policy for tenant_43 allowed, while the same session against an internal table was narrowed. The bridge now calls Auth.getEffectiveRoleNames: getRoleNamesByUserWithLdap without the default role, which honours the live override, the forwarded proxy narrowing and the RPC thread-local exactly as the built-in checks do, includes the LDAP-mapped roles (Doris roles the identity holds), and never names the synthetic SU baseline. A source is never told a role the session cannot exercise. Tests: EngineAuthorizationContextTest mocks the new resolver; SuUserNarrowingTest asserts, against the real Auth, that rolesOf is narrowed by a session override and by the RPC thread-local, and that another identity's roles are untouched. Review finding F-04 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/mysql/privilege/Auth.java | 11 +++++ .../privilege/EngineAuthorizationContext.java | 6 ++- .../EngineAuthorizationContextTest.java | 11 +++-- .../mysql/privilege/SuUserNarrowingTest.java | 44 +++++++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index edfdc52b71a8a2..ccb60f14dcbbdb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -479,6 +479,17 @@ private Set getRolesForPlacementCheck(UserIdentity userIdentity) { return widened; } + /** + * The role names an identity effectively holds right now, as every built-in privilege check + * resolves them: an SU narrowing of the current session (or of the RPC thread) replaces the + * union, LDAP roles are included, the per-user default role and the synthetic SU baseline are + * not. This is what an authorization source is told when it asks the engine who holds which + * roles, so a policy written against Doris roles sees the same set the engine enforces. + */ + public Set getEffectiveRoleNames(UserIdentity user) { + return getRoleNamesByUserWithLdap(user, false); + } + public Set getRoleNamesByUserWithLdap(UserIdentity user, boolean showUserDefaultRole) { Set rolesByUserWithLdap = getRolesByUserWithLdap(user); Set res = Sets.newHashSetWithExpectedSize(rolesByUserWithLdap.size()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java index 5a1b63d1f6a164..2df7b31d2841dc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java @@ -70,11 +70,13 @@ public void servedBy(AuthorizationPlugin source) { * *

Without the per-user default role: it is an artefact of how the built-in model stores a user's own * grants, not a role anybody names in a policy, and a source that saw it would be matching on a name the - * administrator never wrote. + * administrator never wrote. Resolved the way the built-in checks resolve roles, so a session-narrowed + * (SU) session's override and the narrowing a BE->FE RPC carries reach a source's role-bound policies + * exactly as they reach the engine's own; a source is never told roles the session cannot exercise. */ @Override public Set rolesOf(AuthorizedSubject subject) { - return auth.getRolesByUser(AccessTranslation.userIdentityOf(subject), false); + return auth.getEffectiveRoleNames(AccessTranslation.userIdentityOf(subject)); } @Override diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java index e080448b212a8c..7b4c4d35486e1c 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java @@ -61,22 +61,21 @@ public void checkPrivilege(AuthorizedSubject subject, AuthorizedResource resourc } /** - * Roles come without the per-user default role: it is how the built-in model stores a user's own grants, - * not a role an administrator writes a policy against. + * Roles come as the engine's own checks resolve them (without the per-user default role, under the + * session's SU narrowing if any): a source's role-bound policies see the same set the engine enforces. */ @Test - public void testRolesAreReadWithoutTheInternalDefaultRole() { + public void testRolesAreReadAsTheEngineResolvesThem() { Auth auth = Mockito.mock(Auth.class); Set roles = ImmutableSet.of("analyst"); - Mockito.when(auth.getRolesByUser(Mockito.any(UserIdentity.class), Mockito.eq(false))).thenReturn(roles); + Mockito.when(auth.getEffectiveRoleNames(Mockito.any(UserIdentity.class))).thenReturn(roles); EngineAuthorizationContext context = new EngineAuthorizationContext( new AccessControllerManager(auth), auth); context.servedBy(new Recording()); Assertions.assertEquals(roles, context.rolesOf(SUBJECT)); - Mockito.verify(auth).getRolesByUser( - UserIdentity.createAnalyzedUserIdentWithIp("user1", "%"), false); + Mockito.verify(auth).getEffectiveRoleNames(UserIdentity.createAnalyzedUserIdentWithIp("user1", "%")); } @Test diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index 3c628ff6ae46c2..9b72111337c05d 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -35,6 +35,7 @@ import org.apache.doris.qe.StmtExecutor; import org.apache.doris.utframe.TestWithFeService; +import com.google.common.collect.Sets; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -562,4 +563,47 @@ public void testSyntheticBaselineRoleCannotBeNamed() { Assertions.assertThrows(Exception.class, () -> runAsAdmin("CREATE ROLE " + Auth.SU_NARROWING_BASELINE_ROLE)); Assertions.assertFalse(Env.getCurrentEnv().getAuth().doesRoleExist(Auth.SU_NARROWING_BASELINE_ROLE)); } + + /** + * The authorization-SPI role bridge (what a ranger-hive or third-party source is told when it asks + * who holds which roles) resolves through the same choke point as the built-in checks: a narrowed + * session's override, and the narrowing a BE->FE RPC carries, reach role-bound policies of every + * source; the per-user default role and the synthetic baseline never do. + */ + @Test + public void testAuthorizationSpiRoleBridgeIsNarrowed() throws Exception { + addUser("spi_person", true); + createRole("spi_tenant_a"); + createRole("spi_tenant_b"); + grantRole("GRANT 'spi_tenant_a','spi_tenant_b' TO 'spi_person'@'%'"); + UserIdentity person = ident("spi_person"); + EngineAuthorizationContext bridge = new EngineAuthorizationContext( + Env.getCurrentEnv().getAccessManager(), Env.getCurrentEnv().getAuth()); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(person); + ctx.setThreadLocalInfo(); + try { + Assertions.assertEquals(Sets.newHashSet("spi_tenant_a", "spi_tenant_b"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + ctx.setSessionRoleOverride(Collections.singleton("spi_tenant_a")); + Assertions.assertEquals(Collections.singleton("spi_tenant_a"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + // another identity's roles are untouched by this session's narrowing + Assertions.assertTrue(bridge.rolesOf(AccessTranslation.subjectOf(ident("bystander"))).isEmpty()); + } finally { + connectContext.setThreadLocalInfo(); + } + + // the narrowing a metadata RPC carries (no ConnectContext on the handler thread) + Auth.setRpcSessionNarrowing(person, Collections.singleton("spi_tenant_b")); + try { + Assertions.assertEquals(Collections.singleton("spi_tenant_b"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + } finally { + Auth.clearRpcSessionNarrowing(); + } + Assertions.assertEquals(Sets.newHashSet("spi_tenant_a", "spi_tenant_b"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + } } From 748c4c987fa0aca3d25eaec21581b435c8ca4271 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 00:52:01 +0530 Subject: [PATCH 20/30] [fix](auth) SU: placement widening answers USAGE only, on both legs; the GRANT gate stays narrowed The workload-group "placement" widening handed the whole widened role set to Auth.checkWorkloadGroupPriv, which is also the gate of GRANT/REVOKE ... ON WORKLOAD GROUP (predicate USAGE AND GRANT), and Role.checkWorkloadGroupPriv ORs each role's GLOBAL privileges into the shared savedPrivs first. A session narrowed to a role without workload-group grants could therefore grant or revoke group privileges to third parties with the GRANT_PRIV that lives only in the person's direct grants -- the grants the switch is meant to leave inert. The same shape covered the compute-group (CLUSTER) leg of checkCloudPriv. The widening is now one question: "may this session run here?". After the narrowed set has said no, and only when the predicate is satisfiable by USAGE alone (so the GRANT gate's USAGE+GRANT never reaches it), the identity's own granted roles outside the override are consulted for the USAGE bit of their workload-group / compute-group grant on the named group (through the owning virtual compute group as well) -- never their global privileges, never GRANT_PRIV. The listings of grants go back to the narrowed set: the SHOW GRANTS compute-group column and workload_group_privileges list what the narrowed session holds; the lanes it may run in are answered by the USAGE question, which SHOW WORKLOAD GROUPS and SHOW CLUSTERS ask. The widening keyed on the live ConnectContext, so SHOW WORKLOAD GROUPS (this FE) and information_schema.workload_groups (through the BE, on a handler thread with the RPC thread-local) disagreed for one session. The narrowing in force is now read from either source (Auth.narrowingOf), so both legs answer alike. Tests: SuUserNarrowingTest adds the leak case -- a direct USAGE+GRANT on a group, SU to a role without it: the group stays usable, the GRANT gate and the GRANT statement are refused, and the RPC leg answers the same; the regression suite compares SHOW WORKLOAD GROUPS with information_schema.workload_groups inside the narrowed session. The existing workload-group and compute-group placement tests keep their assertions. Review findings F-05 and F-13 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/mysql/privilege/Auth.java | 128 +++++++++++++----- .../trees/plans/commands/SuUserCommand.java | 4 +- .../mysql/privilege/SuUserNarrowingTest.java | 51 +++++++ .../auth_p0/test_su_session_narrowing.groovy | 5 + 4 files changed, 150 insertions(+), 38 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index ccb60f14dcbbdb..5ede9f47e45363 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -33,6 +33,7 @@ import org.apache.doris.catalog.TableIf; import org.apache.doris.cloud.datasource.CloudInternalCatalog; import org.apache.doris.cloud.proto.Cloud; +import org.apache.doris.cloud.system.CloudSystemInfoService; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.AuthenticationException; import org.apache.doris.common.AuthorizationException; @@ -93,6 +94,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Predicate; import java.util.stream.Collectors; @@ -450,33 +452,89 @@ private static Role buildNarrowingBaselineRole() { } /** - * The role set consulted for PLACEMENT privilege checks: WORKLOAD GROUP usage and, in cloud - * mode, compute-group (CLUSTER) usage. Identical to {@link #getRolesByUserWithLdap} for a - * normal session. In an SU-narrowed session the narrowed set is widened with the effective - * identity's own granted roles (its default role (where direct grants live) plus explicit - * and LDAP roles), so the narrowed session may use exactly the workload groups and compute - * groups the person's own session could use. Placement is not data authority: the narrowed - * session runs in the target's default workload group and compute group (the same - * resolution as the person's own session), and the person's USAGE on them is typically a - * direct grant that narrowing would otherwise drop. Only the two placement predicates (and - * the SHOW output that enumerates them) read this set; table, database, resource, stage and - * storage-vault checks stay on the narrowed set, so data access cannot widen through it. + * The SU narrowing in force for this identity on this thread -- the live session's override, + * or the subset a BE->FE metadata RPC carried -- or null when it is not narrowed. The two + * legs of every placement answer (this FE's SHOW output, the information_schema scan through + * the BE) read the same narrowing, so they agree. */ - private Set getRolesForPlacementCheck(UserIdentity userIdentity) { - Set roles = getRolesByUserWithLdap(userIdentity); + private static Set narrowingOf(UserIdentity userIdentity) { + Map> rpcNarrow = RPC_SESSION_NARROWING.get(); + if (rpcNarrow != null && rpcNarrow.containsKey(userIdentity)) { + return rpcNarrow.get(userIdentity); + } ConnectContext ctx = ConnectContext.get(); - if (ctx == null || !ctx.isSessionRoleNarrowed() - || !userIdentity.equals(ctx.getCurrentUserIdentity())) { - return roles; + if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity()) && ctx.isSessionRoleNarrowed()) { + return ctx.getSessionRoleOverride(); + } + return null; + } + + /** + * PLACEMENT widening: in an SU-narrowed session, the effective identity's own granted roles + * that the override left out (its default role, where direct grants live, plus explicit and + * LDAP roles). Placement is not data authority: the narrowed session runs in the person's + * workload group and compute group, and the person's USAGE on them is typically a direct grant + * that narrowing would otherwise drop, so the person's lane follows the person. Only the USAGE + * bit of these roles' workload-group and compute-group grants reaches through + * ({@link #placementUsageThroughOwnRoles}): never their global privileges, never GRANT_PRIV. + * GRANT/REVOKE ... ON WORKLOAD GROUP / CLUSTER, the listings of grants (SHOW GRANTS, + * workload_group_privileges) and every data check stay on the narrowed set. Empty for a + * session that is not narrowed. + */ + private Set placementOnlyRoles(UserIdentity userIdentity) { + Set override = narrowingOf(userIdentity); + if (override == null) { + return Collections.emptySet(); } - Set widened = Sets.newHashSet(roles); + Set ownRoles = Sets.newHashSet(); for (String roleName : getGrantedRoleNames(userIdentity)) { + if (override.contains(roleName)) { + continue; + } Role role = roleManager.getRole(roleName); if (role != null) { - widened.add(role); + ownRoles.add(role); } } - return widened; + return ownRoles; + } + + /** + * The one question the placement widening answers: "may this session run here?". Consulted + * after the narrowed set said no, and only when the predicate is satisfiable by USAGE alone, + * so an administrative predicate (the GRANT gate's USAGE+GRANT) never reaches it; each own + * role contributes its workload-group / compute-group grant on the named group and nothing + * else. + */ + private boolean placementUsageThroughOwnRoles(UserIdentity currentUser, PrivPredicate wanted, + Predicate grantsUsage) { + if (!Privilege.satisfy(PrivBitSet.of(Privilege.USAGE_PRIV), wanted)) { + return false; + } + for (Role role : placementOnlyRoles(currentUser)) { + if (grantsUsage.test(role)) { + return true; + } + } + return false; + } + + private static boolean grantsWorkloadGroupUsage(Role role, String workloadGroupName) { + PrivBitSet privs = PrivBitSet.of(); + role.getWorkloadGroupPrivTable().getPrivs(workloadGroupName, privs); + return privs.containsPrivs(Privilege.USAGE_PRIV); + } + + private static boolean grantsComputeGroupUsage(Role role, String computeGroupName) { + PrivBitSet privs = PrivBitSet.of(); + role.getCloudClusterPrivTable().getPrivs(computeGroupName, privs); + // a grant on the owning virtual compute group covers its members (Role.checkCloudPriv) + String virtualComputeGroup = ((CloudSystemInfoService) Env.getCurrentSystemInfo()) + .ownedByVirtualComputeGroup(computeGroupName); + if (virtualComputeGroup != null) { + role.getCloudClusterPrivTable().getPrivs(virtualComputeGroup, privs); + } + return privs.containsPrivs(Privilege.USAGE_PRIV); } /** @@ -778,14 +836,16 @@ protected boolean checkWorkloadGroupPriv(UserIdentity currentUser, String worklo return true; } - Set roles = getRolesForPlacementCheck(currentUser); + Set roles = getRolesByUserWithLdap(currentUser); PrivBitSet savedPrivs = PrivBitSet.of(); for (Role role : roles) { if (role.checkWorkloadGroupPriv(workloadGroupName, wanted, savedPrivs)) { return true; } } - return false; + // placement: USAGE through the person's own roles in an SU-narrowed session + return placementUsageThroughOwnRoles(currentUser, wanted, + role -> grantsWorkloadGroupUsage(role, workloadGroupName)); } finally { readUnlock(); } @@ -802,17 +862,18 @@ protected boolean checkCloudPriv(UserIdentity currentUser, String cloudName, return true; } } - // compute-group USAGE is placement (see getRolesForPlacementCheck); stage and - // storage-vault usage are data access and stay on the narrowed set - Set roles = type == ResourceTypeEnum.CLUSTER - ? getRolesForPlacementCheck(currentUser) : getRolesByUserWithLdap(currentUser); + Set roles = getRolesByUserWithLdap(currentUser); PrivBitSet savedPrivs = PrivBitSet.of(); for (Role role : roles) { if (role.checkCloudPriv(cloudName, wanted, type, savedPrivs)) { return true; } } - return false; + // compute-group USAGE is placement (see placementOnlyRoles); stage and storage-vault + // usage are data access and stay on the narrowed set + return type == ResourceTypeEnum.CLUSTER + && placementUsageThroughOwnRoles(currentUser, wanted, + role -> grantsComputeGroupUsage(role, cloudName)); } finally { readUnlock(); } @@ -1956,14 +2017,9 @@ public void getUserRoleWorkloadGroupPrivs(List> result, UserIdentit Set currentUserRole = null; if (!isCurrentUserAdmin) { - // the same role set the USAGE check consults: an SU-narrowed session lists the - // placement set, not the identity's full role union - currentUserRole = new HashSet<>(); - for (Role role : getRolesForPlacementCheck(currentUserIdentity)) { - if (!role.getRoleName().startsWith(RoleManager.DEFAULT_ROLE_PREFIX)) { - currentUserRole.add(role.getRoleName()); - } - } + // a listing of GRANTS: the narrowed set in an SU-narrowed session (the lanes the + // session may run in are answered by the USAGE check, not listed here) + currentUserRole = getRoleNamesByUserWithLdap(currentUserIdentity, false); } roleManager.getRoleWorkloadGroupPrivs(result, currentUserRole); } finally { @@ -1973,8 +2029,8 @@ public void getUserRoleWorkloadGroupPrivs(List> result, UserIdentit private ResourcePrivTable getUserCloudClusterPrivTable(UserIdentity userIdentity) { ResourcePrivTable table = new ResourcePrivTable(); - // the same role set checkCloudPriv consults for compute groups (placement) - for (Role role : getRolesForPlacementCheck(userIdentity)) { + // a listing of GRANTS: the narrowed set, like every other SHOW GRANTS column + for (Role role : getRolesByUserWithLdap(userIdentity)) { table.merge(role.getCloudClusterPrivTable()); } return table; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java index 21fc5fe902ec89..698c3bf1a83ee4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java @@ -122,8 +122,8 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { ctx.setCurrentUserIdentity(userIdentity); ctx.setSessionRoleOverride(ImmutableSet.copyOf(roles)); // Placement only. USAGE on the session's workload group (this clause, or the target's - // default_workload_group when omitted) is checked per query against the narrowed set - // widened with the target's own granted roles (Auth.getRolesForPlacementCheck): the + // default_workload_group when omitted) is checked per query against the narrowed set, then + // through the USAGE grants of the target's own roles (Auth.placementOnlyRoles): the // person's lane follows the person. An omitted clause therefore CLEARS any group the // switcher's session had set, so the per-query resolution falls through to the target's // default instead of keeping the switcher's lane. diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index 9b72111337c05d..d7cee376357382 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -17,6 +17,7 @@ package org.apache.doris.mysql.privilege; +import org.apache.doris.analysis.CompoundPredicate.Operator; import org.apache.doris.analysis.PassVar; import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; @@ -606,4 +607,54 @@ public void testAuthorizationSpiRoleBridgeIsNarrowed() throws Exception { Assertions.assertEquals(Sets.newHashSet("spi_tenant_a", "spi_tenant_b"), bridge.rolesOf(AccessTranslation.subjectOf(person))); } + + /** + * The placement widening answers "may this session run here?" and nothing else: a direct + * GRANT_PRIV on a workload group (which lives in the person's default role, outside the + * narrowed set) lets the person's own session grant the group to others, but never the narrowed + * session -- GRANT/REVOKE ... ON WORKLOAD GROUP stays on the narrowed set. Both legs of the + * answer, the live session and the narrowing a BE->FE RPC carries, agree. + */ + @Test + public void testPlacementWideningNeverReachesTheGrantGate() throws Exception { + addUser("wg_granter", true); + addUser("wg_mallory", true); + createRole("wg_tenant"); + grantRole("GRANT 'wg_tenant' TO 'wg_granter'@'%'"); + createWorkloadGroup("wg_x"); + grantWorkloadGroupUsage("GRANT USAGE_PRIV, GRANT_PRIV ON WORKLOAD GROUP 'wg_x' TO 'wg_granter'@'%';"); + UserIdentity granter = ident("wg_granter"); + PrivPredicate grantGate = PrivPredicate.of(PrivBitSet.of(Privilege.USAGE_PRIV, Privilege.GRANT_PRIV), + Operator.AND); + Auth auth = Env.getCurrentEnv().getAuth(); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(granter); + ctx.setThreadLocalInfo(); + try { + // the person's own session may use the group and grant it + Assertions.assertTrue(canUseWorkloadGroup(granter, "wg_x")); + Assertions.assertTrue(auth.checkWorkloadGroupPriv(granter, "wg_x", grantGate)); + + ctx.setSessionRoleOverride(Collections.singleton("wg_tenant")); + // placement follows the person ... + Assertions.assertTrue(canUseWorkloadGroup(granter, "wg_x")); + // ... but the GRANT gate stays on the narrowed set: the direct GRANT_PRIV is inert + Assertions.assertFalse(auth.checkWorkloadGroupPriv(granter, "wg_x", grantGate)); + Assertions.assertThrows(AnalysisException.class, () -> grantWorkloadGroupUsage( + "GRANT USAGE_PRIV ON WORKLOAD GROUP 'wg_x' TO 'wg_mallory'@'%';")); + Assertions.assertFalse(canUseWorkloadGroup(ident("wg_mallory"), "wg_x")); + } finally { + connectContext.setThreadLocalInfo(); + } + + // the RPC leg (no ConnectContext on the handler thread) answers the same + Auth.setRpcSessionNarrowing(granter, Collections.singleton("wg_tenant")); + try { + Assertions.assertTrue(canUseWorkloadGroup(granter, "wg_x")); + Assertions.assertFalse(auth.checkWorkloadGroupPriv(granter, "wg_x", grantGate)); + } finally { + Auth.clearRpcSessionNarrowing(); + } + } } diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy index bc46a001a6af08..eb22038169077e 100644 --- a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -133,6 +133,11 @@ suite("test_su_session_narrowing", "p0,auth") { assertEquals(1, visibleRole[0][0] as int) def visibleOwn = sql """SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${ownDb}'""" assertEquals(0, visibleOwn[0][0] as int) + // placement listings agree on both legs: SHOW WORKLOAD GROUPS (answered by this FE) and + // information_schema.workload_groups (answered through the BE) list the same groups + def shownGroups = sql """SHOW WORKLOAD GROUPS""" + def scannedGroups = sql """SELECT COUNT(*) FROM information_schema.workload_groups""" + assertEquals(shownGroups.size(), scannedGroups[0][0] as int) // one-shot: a switched session cannot switch again, not even to itself test { From 74c0d88afbffd6b9ac09c8770ca5b552a906e54f Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 00:55:21 +0530 Subject: [PATCH 21/30] [fix](auth) SU: refuse EXPORT and CREATE JOB in a narrowed session; task contexts and KEY folds follow the session A persisted job stores a UserIdentity and nothing else about the session that minted it, and its tasks re-plan a read later on a job thread under that identity alone -- the target's FULL role union: the excluded roles' PERMISSIVE row policies apply, session_is_narrowed() folds FALSE, and the job outlives the session and any later REVOKE. EXPORT needs SELECT only and ships SELECT ... INTO OUTFILE to a switcher-chosen path; CREATE JOB ... ON STREAMING needs LOAD only and lands the re-planned SELECT (a JOIN with a policy-filtered table is kept) into a table the session can read, on every new file; ON SCHEDULE re-plans with the same policy view. Both are now refused in a session-narrowed (SU) session with the access-denied error code: "... is not allowed in a session-narrowed (SU) session; the job would run outside the session's role narrowing". Persisting the narrowing on ExportJob/AbstractJob and installing it in the task context is a possible follow-up; it needs the forwarded authenticated identity and a task-time re-check of the ceiling, and is not done here. Two smaller sinks of the same shape are closed in place. A child ConnectContext built for a task thread copied getCurrentUserIdentity() only -- the target without the override -- so the connector rewrite's INSERT-SELECT (and the routine-load workload-group probe) ran under the full union; both now use ConnectContext.inheritIdentityFrom(parent), which carries the identity and the narrowing together. A routine-load task folds `KEY db.k` references of the column mapping per task under the stored identity (SHOW on the key's database); the same authorization now runs at creation under the live session, so a narrowed session that may not read the key cannot create a job that resolves it. Tests: SuUserNarrowingTest refuses EXPORT and both CREATE JOB forms inside a narrowed session, authorizes a KEY reference at creation under the narrowed set, and shows a child context denying what the session denies; the regression suite refuses EXPORT and CREATE JOB ON SCHEDULE in the narrowed session. Review findings F-07, F-08, F-17 and F-26 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../trees/plans/commands/ExportCommand.java | 9 +++ .../execute/ConnectorRewriteGroupTask.java | 3 +- .../plans/commands/info/CreateJobInfo.java | 9 +++ .../commands/info/CreateRoutineLoadInfo.java | 31 +++++++- .../org/apache/doris/qe/ConnectContext.java | 12 ++++ .../mysql/privilege/SuUserNarrowingTest.java | 70 +++++++++++++++++++ .../auth_p0/test_su_session_narrowing.groovy | 12 ++++ 7 files changed, 144 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java index 38fc69b4e23c38..76283cfca7982c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java @@ -133,6 +133,15 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { TableNameInfo tableNameInfo = new TableNameInfo(qualifiedTableName.get(0), qualifiedTableName.get(1), qualifiedTableName.get(2)); + // The export task re-plans SELECT ... INTO OUTFILE later, on a job thread, under the stored + // identity alone: the session's role narrowing cannot follow it there (the excluded roles' + // row policies would apply, session_is_narrowed() would fold FALSE), and the job outlives + // the session. A session-narrowed (SU) session therefore must not mint one. + if (ctx.isSessionRoleNarrowed()) { + ErrorReport.reportAnalysisException("EXPORT is not allowed in a session-narrowed (SU) session; " + + "the job would run outside the session's role narrowing", + ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR); + } // check auth if (!Env.getCurrentEnv().getAccessManager() .checkTblPriv(ctx, tableNameInfo.getCtl(), tableNameInfo.getDb(), tableNameInfo.getTbl(), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java index ead91a50b3eca8..e298a08242aab8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java @@ -256,7 +256,8 @@ private ConnectContext buildConnectContext() { taskContext.setSessionVariable(VariableMgr.cloneSessionVariable(connectContext.getSessionVariable())); taskContext.setEnv(Env.getCurrentEnv()); taskContext.setDatabase(connectContext.getDatabase()); - taskContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); + // the identity AND its SU narrowing: the rewrite's INSERT-SELECT is authorized as the session + taskContext.inheritIdentityFrom(connectContext); taskContext.setRemoteIP(connectContext.getRemoteIP()); UUID uuid = UUID.randomUUID(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java index f70f046fec2f4b..a2332e6e5ba55c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java @@ -242,6 +242,15 @@ private void buildRecurringJob(TimerDefinition timerDefinition, } protected void checkAuth() throws AnalysisException { + // Every task of the job re-plans the INSERT's SELECT later, on a job thread, under the stored + // createUser alone: the session's role narrowing cannot follow it there (the excluded roles' + // row policies would apply), and the job outlives the session. A session-narrowed (SU) + // session therefore must not mint one, in either form. + if (ConnectContext.get().isSessionRoleNarrowed()) { + ErrorReport.reportAnalysisException("CREATE JOB is not allowed in a session-narrowed (SU) session; " + + "the job would run outside the session's role narrowing", + ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR); + } if (streamingJob) { StreamingInsertJob.checkPrivilege(ConnectContext.get(), executeSql, targetDb); return; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java index e8a75c0299b4d6..8f90c6fbe79663 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java @@ -28,10 +28,13 @@ import org.apache.doris.catalog.info.PartitionNamesInfo; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.Config; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; import org.apache.doris.common.FeNameFormat; import org.apache.doris.common.UserException; import org.apache.doris.common.util.TimeUtils; import org.apache.doris.common.util.Util; +import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.datasource.property.fileformat.CsvFileFormatProperties; import org.apache.doris.datasource.property.fileformat.FileFormatProperties; import org.apache.doris.datasource.property.fileformat.JsonFileFormatProperties; @@ -40,6 +43,9 @@ import org.apache.doris.load.routineload.AbstractDataSourceProperties; import org.apache.doris.load.routineload.RoutineLoadDataSourcePropertyFactory; import org.apache.doris.load.routineload.RoutineLoadJob; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.functions.scalar.EncryptKeyRef; import org.apache.doris.nereids.trees.plans.commands.load.LoadColumnClause; import org.apache.doris.nereids.trees.plans.commands.load.LoadColumnDesc; import org.apache.doris.nereids.trees.plans.commands.load.LoadDeleteOnClause; @@ -55,6 +61,7 @@ import org.apache.doris.thrift.TPartialUpdateNewRowPolicy; import org.apache.doris.thrift.TUniqueKeyUpdateMode; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; @@ -66,6 +73,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Predicate; /** @@ -475,6 +483,26 @@ private void validateFlexiblePartialUpdate(OlapTable table) throws AnalysisExcep } } + /** + * A routine-load task folds every `KEY db.k` reference of the column mapping per task, on a job + * thread under the stored identity alone (RoutineLoadJob.plan -> RewriteEncryptKeyRef -> + * checkDbPriv SHOW), where the creating session's SU narrowing cannot follow. The same + * authorization runs here, at creation, under the live session: a narrowed session that may not + * read the key itself cannot create a job that resolves it. + */ + @VisibleForTesting + public static void checkEncryptKeyRefs(Expression expression, ConnectContext ctx) throws AnalysisException { + Set keyRefs = expression.collect(EncryptKeyRef.class::isInstance); + for (EncryptKeyRef keyRef : keyRefs) { + String dbName = Strings.isNullOrEmpty(keyRef.getDbName()) ? ctx.getDatabase() : keyRef.getDbName(); + if (!Env.getCurrentEnv().getAccessManager() + .checkDbPriv(ctx, InternalCatalog.INTERNAL_CATALOG_NAME, dbName, PrivPredicate.SHOW)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_DB_ACCESS_DENIED_ERROR, + PrivPredicate.SHOW.getPrivs().toString(), dbName); + } + } + } + /** * check load properties * @param ctx connect context @@ -515,6 +543,7 @@ public static RoutineLoadDesc checkLoadProperties(ConnectContext ctx, Map(); for (LoadColumnDesc columnDesc : ((LoadColumnClause) loadProperty).getColumns()) { if (columnDesc.getExpression() != null) { + checkEncryptKeyRefs(columnDesc.getExpression(), ctx); Expr expr = PlanUtils.translateToLegacyExpr(columnDesc.getExpression(), table, ctx); columnInfos.add(new ImportColumnDesc(columnDesc.getColumnName(), expr)); } else { @@ -602,7 +631,7 @@ public void checkJobProperties() throws UserException { String inputWorkloadGroupStr = jobProperties.get(WORKLOAD_GROUP); if (!StringUtils.isEmpty(inputWorkloadGroupStr)) { ConnectContext tmpCtx = new ConnectContext(); - tmpCtx.setCurrentUserIdentity(ConnectContext.get().getCurrentUserIdentity()); + tmpCtx.inheritIdentityFrom(ConnectContext.get()); tmpCtx.getSessionVariable().setWorkloadGroup(inputWorkloadGroupStr); if (Config.isCloudMode()) { tmpCtx.setCloudCluster(ConnectContext.get().getCloudCluster()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java index 9f19260042c428..fd07b14ec2eb3d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java @@ -403,6 +403,18 @@ public void revertSessionNarrowing() { } } + /** + * Makes this (child) context run as the parent's session does: the current identity together + * with the SU narrowing state, so a privilege check on the child resolves the same role set the + * parent session sees. Copying getCurrentUserIdentity() alone would carry the TARGET identity + * without the override, i.e. the target's full role union. + */ + public void inheritIdentityFrom(ConnectContext parent) { + this.currentUserIdentity = parent.currentUserIdentity; + this.authenticatedIdentity = parent.authenticatedIdentity; + this.sessionRoleOverride = parent.sessionRoleOverride; + } + /** Drop SU state without restoring identity — only for paths that fully re-authenticate. */ public void clearSessionNarrowing() { this.authenticatedIdentity = null; diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index d7cee376357382..c74f5fabf00d2c 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -25,10 +25,14 @@ import org.apache.doris.common.proc.AuthProcDir; import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.plans.commands.Command; +import org.apache.doris.nereids.trees.plans.commands.CreateJobCommand; import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand; +import org.apache.doris.nereids.trees.plans.commands.ExportCommand; import org.apache.doris.nereids.trees.plans.commands.GrantResourcePrivilegeCommand; import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; +import org.apache.doris.nereids.trees.plans.commands.info.CreateRoutineLoadInfo; import org.apache.doris.nereids.trees.plans.commands.info.SetPassVarOp; import org.apache.doris.nereids.trees.plans.commands.info.SetUserPropertyVarOp; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; @@ -657,4 +661,70 @@ public void testPlacementWideningNeverReachesTheGrantGate() throws Exception { Auth.clearRpcSessionNarrowing(); } } + + /** + * A persisted job re-plans its read later, on a job thread, under the stored identity alone, + * where the session's narrowing cannot follow: a narrowed session mints none (EXPORT, CREATE JOB + * in both forms), and a routine load's KEY references are authorized at creation under the live + * session. A child context built for a task thread inherits the narrowing with the identity. + */ + @Test + public void testNarrowedSessionMintsNoPersistedExecutor() throws Exception { + createTable("create table test.su_job_t (k int) distributed by hash(k) buckets 1 " + + "properties('replication_num' = '1')"); + addUser("job_person", true); + createRole("job_role"); + grantPriv("GRANT SELECT_PRIV, LOAD_PRIV ON internal.test.* TO ROLE 'job_role';"); + grantRole("GRANT 'job_role' TO 'job_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'job_person'@'%';"); + UserIdentity person = ident("job_person"); + + ConnectContext ctx = proxySession("job_svc"); + try { + new SuUserCommand(new UserIdentity("job_person", "%"), + Collections.singletonList("job_role"), null).run(ctx, null); + ctx.setDatabase("test"); + + LogicalPlan export = new NereidsParser().parseSingle( + "EXPORT TABLE test.su_job_t TO \"file:///tmp/su_job/\""); + Assertions.assertTrue(export instanceof ExportCommand); + AnalysisException refused = Assertions.assertThrows(AnalysisException.class, + () -> ((ExportCommand) export).run(ctx, null)); + Assertions.assertTrue(refused.getMessage().contains("session-narrowed"), refused.getMessage()); + + for (String sql : new String[] { + "CREATE JOB su_job_s ON SCHEDULE EVERY 1 DAY DO INSERT INTO test.su_job_t" + + " SELECT * FROM test.su_job_t", + "CREATE JOB su_job_f ON STREAMING DO INSERT INTO test.su_job_t" + + " SELECT * FROM S3(\"uri\" = \"s3://bucket/x.csv\", \"format\" = \"csv\")"}) { + LogicalPlan job = new NereidsParser().parseSingle(sql); + Assertions.assertTrue(job instanceof CreateJobCommand, sql); + AnalysisException jobRefused = Assertions.assertThrows(AnalysisException.class, + () -> ((CreateJobCommand) job).run(ctx, null)); + Assertions.assertTrue(jobRefused.getMessage().contains("session-narrowed"), jobRefused.getMessage()); + } + + // the KEY reference of a routine-load column mapping is authorized at creation, as the + // narrowed session: SHOW on perso lives only in the excluded direct grant + Expression mapping = new NereidsParser().parseExpression("aes_decrypt(c1, KEY perso.k)"); + Assertions.assertThrows(AnalysisException.class, + () -> CreateRoutineLoadInfo.checkEncryptKeyRefs(mapping, ctx)); + CreateRoutineLoadInfo.checkEncryptKeyRefs( + new NereidsParser().parseExpression("aes_decrypt(c1, KEY test.k)"), ctx); + + // a child context of the narrowed session (a task thread) denies what the session denies + ConnectContext child = new ConnectContext(); + child.inheritIdentityFrom(ctx); + child.setThreadLocalInfo(); + try { + Assertions.assertEquals("job_svc", child.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertTrue(canSelectDb(person, "test")); + Assertions.assertFalse(canSelectDb(person, "perso")); + } finally { + ctx.setThreadLocalInfo(); + } + } finally { + connectContext.setThreadLocalInfo(); + } + } } diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy index eb22038169077e..5fcb888bedad0d 100644 --- a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -145,6 +145,18 @@ suite("test_su_session_narrowing", "p0,auth") { exception "already-switched" } + // a persisted job re-plans its read later under the stored identity, where the + // narrowing cannot follow: a narrowed session mints none + test { + sql """EXPORT TABLE ${roleDb}.t TO "file:///tmp/${roleDb}/" """ + exception "session-narrowed" + } + test { + sql """CREATE JOB ${roleDb}_job ON SCHEDULE EVERY 1 DAY + DO INSERT INTO ${roleDb}.t SELECT * FROM ${roleDb}.t""" + exception "session-narrowed" + } + // the switcher is not the person: the "self" exemptions of SET PASSWORD and SET PROPERTY // are closed under the switch test { From 63ba60666971ddaf39e3be0046307cdff1b21419 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 00:58:55 +0530 Subject: [PATCH 22/30] [fix](auth) SU: SHOW PROCESSLIST names the role subset on its FE fan-out; a switched forward without roles is refused The FE->FE SHOW PROCESSLIST fan-out (fetch_all_fe_for_system_table, on by default) built TShowProcessListRequest with current_user_ident only: the receiving handler was wrapped, but this sender was never swept, so every remote FE ran listConnectionForRpc's ADMIN check against the target's full role union and a tenant-narrowed session whose target holds an excluded ADMIN-bearing role saw every user's session with its SQL on every other FE. The request builder (ShowProcessListCommand.buildRequest) now sets current_roles when the session is narrowed, the shape FEOpExecutor and SchemaScanNode already use; and because an FE of an older build ignores the field, the fan-out fails closed while the FE set runs mixed builds, through Env.aliveFrontendsRunSameBuild -- the check SchemaScanNode carried privately for its all-FE scans, now shared. The two sides of the current_roles wire field had two meanings: the BE forwards a plan's set only when it is non-empty ("empty = not narrowed"), while the master narrowed a switched forward without a role list to the empty set ("empty = nothing"). The master now refuses such a request (and an explicitly empty one) with the access-denied error code instead of installing an empty override, so an installed narrowing is never empty and "absent = not narrowed" is the one meaning everywhere; the grammar already requires at least one role on a live session. The refusal is reported to the origin FE through the normal proxy result (applyForwardedSessionNarrowing runs first inside the execution try, with its own catch). Tests: ShowProcessListCommandTest covers the request builder for a plain and a narrowed session; ForwardedSessionNarrowingTest replaces the "narrows to the empty set" case with the refusal of a switched request without roles and with an empty set. Review findings F-09 and F-10 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../java/org/apache/doris/catalog/Env.java | 15 +++++++++ .../commands/ShowProcessListCommand.java | 31 +++++++++++++++++-- .../apache/doris/planner/SchemaScanNode.java | 13 +++----- .../org/apache/doris/qe/ConnectProcessor.java | 24 ++++++++++---- .../commands/ShowProcessListCommandTest.java | 27 ++++++++++++++++ .../qe/ForwardedSessionNarrowingTest.java | 27 +++++++++++----- gensrc/thrift/FrontendService.thrift | 5 +-- 7 files changed, 115 insertions(+), 27 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index 524b6436005f73..138247bdcf046d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -5643,6 +5643,21 @@ public boolean masterRunsSameBuild() { * responses (HbPackage, FrontendHbResponse.version), which the other FEs replay. An FE this FE * does not know, or a dead one, counts as a different build. */ + /** + * Whether every alive FE reports the same build as this FE (see {@link #frontendRunsSameBuild}): + * the gate of a session-narrowed (SU) session's requests that fan out to every FE, which an FE + * of an older build would answer against the target's full role union. + */ + public boolean aliveFrontendsRunSameBuild() { + String local = Frontend.localBuildVersion(); + for (Frontend fe : frontends.values()) { + if (fe.isAlive() && !local.equals(fe.getVersion())) { + return false; + } + } + return true; + } + public boolean frontendRunsSameBuild(String host, int rpcPort) { if (host.equals(selfNode.getHost()) && rpcPort == Config.rpc_port) { return true; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java index e316ca0b3e9e0c..d61940a17d2deb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java @@ -22,6 +22,7 @@ import org.apache.doris.catalog.Table; import org.apache.doris.common.ClientPool; import org.apache.doris.common.Pair; +import org.apache.doris.common.UserException; import org.apache.doris.common.proc.FrontendsProcNode; import org.apache.doris.nereids.trees.plans.PlanType; import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; @@ -39,6 +40,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -82,10 +84,18 @@ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exc } if (isShowAllFe) { + // Every other FE runs the ADMIN check of listConnectionForRpc against the role subset the + // request names. An FE of an older build ignores the field and lists every user's session + // with its SQL, so a session-narrowed (SU) session's fan-out fails closed while the FE set + // runs mixed builds, as the information_schema scans that leave this FE do. + if (ctx.isSessionRoleNarrowed() && !Env.getCurrentEnv().aliveFrontendsRunSameBuild()) { + throw new UserException("SHOW PROCESSLIST of a session-narrowed (SU) session is answered by every " + + "alive FE, and one runs a different FE build than this FE and could not apply the " + + "narrowing. Retry once the rolling upgrade completes, or with " + + "fetch_all_fe_for_system_table = false."); + } try { - TShowProcessListRequest request = new TShowProcessListRequest(); - request.setShowFullSql(isShowFullSql); - request.setCurrentUserIdent(ConnectContext.get().getCurrentUserIdentity().toThrift()); + TShowProcessListRequest request = buildRequest(ctx, isShowFullSql); List> frontends = FrontendsProcNode.getFrontendWithRpcPort(Env.getCurrentEnv(), false); FrontendService.Client client = null; @@ -125,6 +135,21 @@ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exc return new ShowResultSet(getMetaData(), rowSet); } + /** + * The request every other FE answers: who is asking and, for a session-narrowed (SU) session, the + * role subset it runs under -- the same shape as FEOpExecutor and SchemaScanNode carry it. + */ + @VisibleForTesting + static TShowProcessListRequest buildRequest(ConnectContext ctx, boolean isShowFullSql) { + TShowProcessListRequest request = new TShowProcessListRequest(); + request.setShowFullSql(isShowFullSql); + request.setCurrentUserIdent(ctx.getCurrentUserIdentity().toThrift()); + if (ctx.isSessionRoleNarrowed()) { + request.setCurrentRoles(new HashSet<>(ctx.getSessionRoleOverride())); + } + return request; + } + /** * A row from another frontend has that frontend's columns, which differ from this one's while the * cluster runs two versions: an older frontend's row ends before the columns added since (Protocol), diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java index 56f43ff15306bf..8a16c8bfd14627 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java @@ -108,7 +108,7 @@ public void finalizeForNereids() throws UserException { } TableIf tableIf = desc.getTable(); if (ConnectContext.get().isSessionRoleNarrowed() && tableIf instanceof SchemaTable - && ((SchemaTable) tableIf).shouldFetchAllFe() && !allAliveFrontendsRunSameBuild()) { + && ((SchemaTable) tableIf).shouldFetchAllFe() && !Env.getCurrentEnv().aliveFrontendsRunSameBuild()) { throw new UserException(mixedBuildMessage("every alive FE")); } } @@ -126,16 +126,11 @@ private static String mixedBuildMessage(String target) { + "Retry once the rolling upgrade completes."; } - private static boolean allAliveFrontendsRunSameBuild() { - String local = Frontend.localBuildVersion(); - for (Frontend fe : Env.getCurrentEnv().getFrontends(null)) { - if (fe.isAlive() && !local.equals(fe.getVersion())) { - return false; - } + if (ConnectContext.get().isSessionRoleNarrowed() && tableIf instanceof SchemaTable + && ((SchemaTable) tableIf).shouldFetchAllFe() && !Env.getCurrentEnv().aliveFrontendsRunSameBuild()) { + throw new UserException(mixedBuildMessage("every alive FE")); } - return true; } - private void setFeAddrList(TPlanNode msg) { List feAddrList = new ArrayList(); TableIf tableIf = desc.getTable(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java index ac8fd3a5fa5683..bd7deb141c20d5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java @@ -41,6 +41,7 @@ import org.apache.doris.cloud.qe.ComputeGroupException; import org.apache.doris.cloud.system.CloudSystemInfoService; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.AuthorizationException; import org.apache.doris.common.Config; import org.apache.doris.common.ConnectionException; import org.apache.doris.common.ErrorCode; @@ -96,7 +97,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -552,15 +552,22 @@ protected void handleQueryException(Throwable throwable, String origStmt, /** * Install the SU narrowing a forwarded statement carries (is_su_user + current_roles) on this * proxy session, so every privilege check on the master resolves the same narrowed role set the - * origin FE would. Fail closed: a switched session that carries no role list narrows to the - * empty set (the information_schema/mysql baseline only), never to the target's full roles. + * origin FE would. Fail closed: a switched session that names no role subset is refused rather + * than narrowed to nothing, so an installed override is never empty and the wire field has one + * meaning on every side -- absent means "not narrowed" (the BE forwards a plan's current_roles + * only when the set is non-empty; the FE grammar requires at least one role). */ @VisibleForTesting - static void applyForwardedSessionNarrowing(ConnectContext ctx, TMasterOpRequest request) { + static void applyForwardedSessionNarrowing(ConnectContext ctx, TMasterOpRequest request) + throws AuthorizationException { if (!request.isSetIsSuUser() || !request.isIsSuUser()) { return; } - Set roles = request.isSetCurrentRoles() ? request.getCurrentRoles() : Collections.emptySet(); + if (!request.isSetCurrentRoles() || request.getCurrentRoles().isEmpty()) { + throw new AuthorizationException("A forwarded statement of a session-narrowed (SU) session names no " + + "role subset; refusing to run it against the target's full role union"); + } + Set roles = request.getCurrentRoles(); ctx.setSessionRoleOverride(roles); if (request.isSetAuthenticatedUserIdent()) { // audit only: the master's row names the account that authenticated the connection @@ -584,7 +591,6 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException } else { ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp(request.user, "%")); } - applyForwardedSessionNarrowing(ctx, request); if (request.isFoldConstantByBe()) { ctx.getSessionVariable().setEnableFoldConstantByBe(request.foldConstantByBe); } @@ -608,6 +614,8 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException ctx.setThreadLocalInfo(); StmtExecutor executor = null; try { + // the narrowing, or the refusal of a switched request that names none, before anything runs + applyForwardedSessionNarrowing(ctx, request); MysqlProtocolAdapter.of(ctx).restoreFromForwardRequest(ctx, request); // 0 for compatibility. int idx = request.isSetStmtIdx() ? request.getStmtIdx() : 0; @@ -676,6 +684,10 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException } else { executor.queryRetry(queryId); } + } catch (AuthorizationException e) { + // a switched forward that names no role subset: refused, reported to the origin FE as such + LOG.warn("forwarded statement refused: {}", e.getMessage()); + ctx.getState().setError(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, e.getMessage()); } catch (IOException e) { // Client failed. LOG.warn("Process one query failed because IOException: ", e); diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java index f8eb5bff3ff2f1..d4493fcea77009 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java @@ -17,9 +17,13 @@ package org.apache.doris.nereids.trees.plans.commands; +import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.SchemaTable; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.thrift.TShowProcessListRequest; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -37,6 +41,29 @@ public void testMetaDataIsTheProcesslistSchemaTable() { Assertions.assertEquals("Protocol", names.get(names.size() - 1)); } + // The request the other frontends answer names who is asking and, for a session-narrowed (SU) + // session, the role subset it runs under, so their ADMIN check is narrowed like the local one. + @Test + public void testRequestNamesTheNarrowedRoleSubset() { + UserIdentity alice = UserIdentity.createAnalyzedUserIdentWithIp("alice", "%"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(alice); + + TShowProcessListRequest plain = ShowProcessListCommand.buildRequest(ctx, true); + Assertions.assertTrue(plain.isShowFullSql()); + Assertions.assertEquals(alice.toThrift(), plain.getCurrentUserIdent()); + Assertions.assertFalse(plain.isSetCurrentRoles()); + + ctx.setAuthenticatedIdentity(UserIdentity.createAnalyzedUserIdentWithIp("svc", "%")); + ctx.setSessionRoleOverride(Sets.newHashSet("tenant_a")); + TShowProcessListRequest narrowed = ShowProcessListCommand.buildRequest(ctx, false); + Assertions.assertFalse(narrowed.isShowFullSql()); + // the identity the other FE checks is still the session's (switched-to) identity ... + Assertions.assertEquals(alice.toThrift(), narrowed.getCurrentUserIdent()); + // ... narrowed to the subset the session runs under + Assertions.assertEquals(Sets.newHashSet("tenant_a"), narrowed.getCurrentRoles()); + } + // SHOW PROCESSLIST merges the rows the other frontends answer with; while the cluster runs two // versions those rows have another number of columns, and the result set must stay rectangular. @Test diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java index c417ef285d0741..bed37551293b8c 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java @@ -20,6 +20,7 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.AuthorizationException; import org.apache.doris.common.Config; import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.ha.FrontendNodeType; @@ -142,18 +143,30 @@ public void testMasterInstallsCarriedNarrowingOnProxySession() throws Exception Assertions.assertTrue(canSelectDb(bob, "perso")); } + /** + * A switched request that names no role subset (or an empty one) is refused, never narrowed to + * nothing: an installed override is never empty, so "absent = not narrowed" is the one meaning of + * current_roles on every side of the wire. + */ @Test - public void testMasterFailsClosedWhenSwitchedRequestCarriesNoRoles() { - TMasterOpRequest request = new TMasterOpRequest(); - request.setIsSuUser(true); + public void testMasterRefusesASwitchedRequestWithoutRoles() { + TMasterOpRequest noRoles = new TMasterOpRequest(); + noRoles.setIsSuUser(true); ConnectContext proxy = new ConnectContext(); - ConnectProcessor.applyForwardedSessionNarrowing(proxy, request); - Assertions.assertNotNull(proxy.getSessionRoleOverride()); - Assertions.assertTrue(proxy.getSessionRoleOverride().isEmpty()); + Assertions.assertThrows(AuthorizationException.class, + () -> ConnectProcessor.applyForwardedSessionNarrowing(proxy, noRoles)); + Assertions.assertFalse(proxy.isSessionRoleNarrowed()); + + TMasterOpRequest emptyRoles = new TMasterOpRequest(); + emptyRoles.setIsSuUser(true); + emptyRoles.setCurrentRoles(Sets.newHashSet()); + Assertions.assertThrows(AuthorizationException.class, + () -> ConnectProcessor.applyForwardedSessionNarrowing(proxy, emptyRoles)); + Assertions.assertFalse(proxy.isSessionRoleNarrowed()); } @Test - public void testUnswitchedRequestLeavesProxySessionUnnarrowed() { + public void testUnswitchedRequestLeavesProxySessionUnnarrowed() throws Exception { ConnectContext proxy = new ConnectContext(); ConnectProcessor.applyForwardedSessionNarrowing(proxy, new TMasterOpRequest()); Assertions.assertNull(proxy.getSessionRoleOverride()); diff --git a/gensrc/thrift/FrontendService.thrift b/gensrc/thrift/FrontendService.thrift index 0c9dd2ea0fbcd3..b8a4f27781b384 100644 --- a/gensrc/thrift/FrontendService.thrift +++ b/gensrc/thrift/FrontendService.thrift @@ -433,8 +433,9 @@ struct TMasterOpRequest { // Session-narrowed (SU) session: the active role subset that REPLACES the target's role // union while the master executes this statement, and the flag that the session is - // switched. A switched session that carries no role list narrows to the empty set on the - // master (fail closed). Ids 34/35 were reserved for exactly these fields. + // switched. A switched session that carries no (or an empty) role list is refused by the + // master (fail closed): an installed narrowing is never empty. Ids 34/35 were reserved + // for exactly these fields. 34: optional set current_roles 35: optional bool is_su_user 36: optional string connectingFeLocalResourceGroup From 5af70969c529afc5a61a8d509019705f692041fc Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 06:54:58 +0530 Subject: [PATCH 23/30] [fix](auth) SU: the target's account state is checked, an LDAP-only target is named honestly, and the switch drops the switcher's caches Four session-lifecycle gaps of the switch, closed in SuUserCommand and ConnectContext. A login runs PasswordPolicyManager.checkAccountLockedAndPasswordExpiration; the switch did not, so an account locked by ACCOUNT_LOCK or by failed logins, or one whose password expired, could still be assumed through SU. The switch now runs the same check on the target and refuses with its reason. (MySQL judges a proxy login by the proxying account's lock; a switch to a locked person is refused here, since the administrator's lock is meant to stop that person's sessions.) With LDAP authentication the existence probe (getGrantedRoleNames, keyed by name) accepted a target with no Doris user entry under ANY host string, so the session ran as an identity that never existed, with a switcher-chosen host in current_user() and the audit row. A target without a local entry must now exist in LDAP (Auth.ldapUserExists) and be named with host '%', the way an LDAP-only account is known. SU left the per-connection prepared-statement map intact, so a short-circuit point query prepared before the switch executed afterwards with the plan that was authorized under the switcher, audited as the target; and the cloud compute-group cache (cloudCluster) was neither re-derived at the switch nor cleared when it ended, so a narrowed session failed on, or silently ran in, the switcher's group, and a reverted switcher kept the target's. The switch is now one ConnectContext operation (applySessionNarrowing) that drops both caches; leaving the switch (revert on reset, clear on COM_CHANGE_USER) drops the cached group and the workload-group lane the switch chose, so the identity that owns the session next resolves placement for itself -- the two leave-the-switch paths agree. A non-switched session's lane is untouched by COM_CHANGE_USER as before. Tests: SuUserNarrowingTest refuses a locked target and switches once unlocked, and shows the prepared statements and the compute-group cache dropped at the switch and the cache dropped on revert and on clear; MysqlConnectProcessorSuTest shows a successful COM_CHANGE_USER leaving the switch's lane. The LDAP shape cannot be exercised in the unit harness (no LDAP) and is traced from the code. Review findings F-14, F-15, F-18, F-19 and F-29 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/mysql/privilege/Auth.java | 5 ++ .../trees/plans/commands/SuUserCommand.java | 26 +++++-- .../org/apache/doris/qe/ConnectContext.java | 28 ++++++++ .../mysql/privilege/SuUserNarrowingTest.java | 72 +++++++++++++++++++ .../doris/qe/MysqlConnectProcessorSuTest.java | 21 +++++- 5 files changed, 147 insertions(+), 5 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index 5ede9f47e45363..460fb1f6b2655d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -879,6 +879,11 @@ && placementUsageThroughOwnRoles(currentUser, wanted, } } + /** Whether LDAP authentication is on and names this user: the existence probe of an LDAP-only account. */ + public boolean ldapUserExists(String qualifiedUser) { + return isLdapAuthEnabled() && ldapManager.doesUserExist(qualifiedUser); + } + // Check if LDAP authentication is enabled. private boolean isLdapAuthEnabled() { return AuthenticateType.getAuthTypeConfig() == AuthenticateType.LDAP; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java index 698c3bf1a83ee4..3315fe5c35e7d1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java @@ -21,6 +21,7 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.AuthenticationException; import org.apache.doris.common.ErrorCode; import org.apache.doris.common.ErrorReport; import org.apache.doris.common.FeNameFormat; @@ -35,7 +36,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import com.google.common.collect.ImmutableSet; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -105,6 +105,25 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { // every existing user holds at least its default role throw new AnalysisException("SU target user does not exist: " + userIdentity); } + if (!auth.doesUserExist(userIdentity)) { + // No local account: the roles came from LDAP, which is keyed by name alone. The target must + // exist there, and it is named the way an LDAP-only login is (no host of the switcher's + // choosing), so current_user() and the audit row name an identity that exists. + if (!auth.ldapUserExists(userIdentity.getQualifiedUser())) { + throw new AnalysisException("SU target user does not exist: " + userIdentity); + } + if (!"%".equals(userIdentity.getHost())) { + throw new AnalysisException("SU target " + userIdentity + " is an LDAP account without a Doris" + + " user entry: name it with host '%'"); + } + } + // the account state a login checks: a locked account, or one whose password expired, cannot + // be assumed through a switch either + try { + auth.getPasswdPolicyManager().checkAccountLockedAndPasswordExpiration(userIdentity); + } catch (AuthenticationException e) { + throw new AnalysisException("SU target " + userIdentity + " cannot be assumed: " + e.getMessage(), e); + } // narrowing only: the requested roles must be a subset of the target's granted roles for (String role : roles) { if (!targetRoles.contains(role)) { @@ -118,9 +137,8 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { checkNoAdministrativeEscalation(ctx, auth.getGlobalPrivsOfRoles(roles)); UserIdentity authenticated = ctx.getCurrentUserIdentity(); - ctx.setAuthenticatedIdentity(authenticated); - ctx.setCurrentUserIdentity(userIdentity); - ctx.setSessionRoleOverride(ImmutableSet.copyOf(roles)); + // the switch, and with it the switcher's cached compute group and prepared statements + ctx.applySessionNarrowing(userIdentity, roles); // Placement only. USAGE on the session's workload group (this clause, or the target's // default_workload_group when omitted) is checked per query against the narrowed set, then // through the USAGE grants of the target's own roles (Auth.placementOnlyRoles): the diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java index fd07b14ec2eb3d..abca51d2944f3c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java @@ -98,6 +98,7 @@ import java.nio.ByteBuffer; import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -393,6 +394,21 @@ public void resetConnection() throws UserException { returnRows = 0; } + /** + * The SU switch: this session becomes the target identity under the role override, and the + * identity that authenticated the connection is kept for the audit row and the revert. What the + * session cached for the switcher goes with it: the compute group chosen for it (the next + * statement resolves placement for the target) and the prepared statements, whose plans were + * authorized under the switcher and would otherwise execute as the target. + */ + public void applySessionNarrowing(UserIdentity target, Collection roles) { + this.authenticatedIdentity = this.currentUserIdentity; + this.currentUserIdentity = target; + this.sessionRoleOverride = Collections.unmodifiableSet(new HashSet<>(roles)); + this.cloudCluster = null; + this.preparedStatementContextMap.clear(); + } + /** Revert an SU switch: back to the authenticated identity, override cleared. No-op if not switched. */ public void revertSessionNarrowing() { UserIdentity authed = this.authenticatedIdentity; @@ -400,9 +416,18 @@ public void revertSessionNarrowing() { this.currentUserIdentity = authed; this.authenticatedIdentity = null; this.sessionRoleOverride = null; + leaveSwitchedPlacement(); } } + // Leaving the switch leaves its placement: the compute group cached for the target and the + // workload-group lane the switch chose (its clause, or the cleared switcher's lane) go, so the + // identity that owns the session next resolves both for itself -- on COM_CHANGE_USER as on reset. + private void leaveSwitchedPlacement() { + this.cloudCluster = null; + this.sessionVariable.setWorkloadGroup(""); + } + /** * Makes this (child) context run as the parent's session does: the current identity together * with the SU narrowing state, so a privilege check on the child resolves the same role set the @@ -417,6 +442,9 @@ public void inheritIdentityFrom(ConnectContext parent) { /** Drop SU state without restoring identity — only for paths that fully re-authenticate. */ public void clearSessionNarrowing() { + if (this.authenticatedIdentity != null) { + leaveSwitchedPlacement(); + } this.authenticatedIdentity = null; this.sessionRoleOverride = null; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java index c74f5fabf00d2c..d920f98347cfab 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -22,6 +22,7 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.jmockit.Deencapsulation; import org.apache.doris.common.proc.AuthProcDir; import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.nereids.parser.NereidsParser; @@ -45,6 +46,7 @@ import org.junit.jupiter.api.Test; import java.util.Collections; +import java.util.Map; import java.util.Set; /** @@ -727,4 +729,74 @@ public void testNarrowedSessionMintsNoPersistedExecutor() throws Exception { connectContext.setThreadLocalInfo(); } } + + /** + * The account state a login checks is checked by the switch too: a target locked by ACCOUNT_LOCK + * (or by failed logins, or with an expired password) cannot be assumed until it is unlocked. + */ + @Test + public void testLockedTargetCannotBeAssumed() throws Exception { + addUser("locked_person", true); + createRole("locked_role"); + grantRole("GRANT 'locked_role' TO 'locked_person'@'%'"); + UserIdentity person = ident("locked_person"); + PasswordPolicyManager policies = Env.getCurrentEnv().getAuth().getPasswdPolicyManager(); + + ConnectContext ctx = proxySession("locked_svc"); + try { + policies.lockUser(person); + try { + AnalysisException refused = Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("locked_person", "%"), + Collections.singletonList("locked_role"), null).run(ctx, null)); + Assertions.assertTrue(refused.getMessage().contains("cannot be assumed"), refused.getMessage()); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + } finally { + policies.unlockUser(person); + } + new SuUserCommand(new UserIdentity("locked_person", "%"), + Collections.singletonList("locked_role"), null).run(ctx, null); + Assertions.assertEquals("locked_person", ctx.getQualifiedUser()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + /** + * What the session cached for one identity does not survive the switch or its end: the + * prepared statements (their plans were authorized under the switcher) go at the switch; the + * compute group chosen for the current identity goes at the switch and when it is left. + */ + @Test + public void testSwitchDropsTheSwitchersPreparedStatementsAndComputeGroup() throws Exception { + addUser("cache_person", true); + createRole("cache_role"); + grantRole("GRANT 'cache_role' TO 'cache_person'@'%'"); + + ConnectContext ctx = proxySession("cache_svc"); + try { + ctx.addPreparedStatementContext("stmt_of_switcher", null); + Map prepared = Deencapsulation.getField(ctx, "preparedStatementContextMap"); + Assertions.assertEquals(1, prepared.size()); + Deencapsulation.setField(ctx, "cloudCluster", "group_of_switcher"); + + new SuUserCommand(new UserIdentity("cache_person", "%"), + Collections.singletonList("cache_role"), null).run(ctx, null); + Assertions.assertTrue(prepared.isEmpty()); + Assertions.assertNull(Deencapsulation.getField(ctx, "cloudCluster")); + + // the group the target's statements chose goes when the switch is left, either way + Deencapsulation.setField(ctx, "cloudCluster", "group_of_target"); + ctx.revertSessionNarrowing(); + Assertions.assertNull(Deencapsulation.getField(ctx, "cloudCluster")); + + new SuUserCommand(new UserIdentity("cache_person", "%"), + Collections.singletonList("cache_role"), null).run(ctx, null); + Deencapsulation.setField(ctx, "cloudCluster", "group_of_target"); + ctx.clearSessionNarrowing(); + Assertions.assertNull(Deencapsulation.getField(ctx, "cloudCluster")); + } finally { + connectContext.setThreadLocalInfo(); + } + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java index 0a6735341d8394..06b35071e09acc 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java @@ -53,13 +53,17 @@ protected void runBeforeAll() throws Exception { } private ConnectContext switchedContext() throws Exception { + return switchedContext(null); + } + + private ConnectContext switchedContext(String workloadGroup) throws Exception { ConnectContext ctx = new ConnectContext(); ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("su_change_svc", "%")); ctx.setRemoteIP("127.0.0.1"); MysqlProtocolAdapter.of(ctx).setHandshakePacket(new MysqlHandshakePacket(0)); ctx.setThreadLocalInfo(); new SuUserCommand(new UserIdentity("su_change_person", "%"), - Collections.singletonList("su_change_role"), null).run(ctx, null); + Collections.singletonList("su_change_role"), workloadGroup).run(ctx, null); return ctx; } @@ -137,6 +141,21 @@ public void testSuccessfulChangeUserClearsNarrowing() throws Exception { } } + @Test + public void testSuccessfulChangeUserLeavesTheSwitchersLane() throws Exception { + try { + ConnectContext ctx = switchedContext("su_change_lane"); + Assertions.assertEquals("su_change_lane", ctx.getSessionVariable().getWorkloadGroup()); + changeUser(ctx, "su_change_login"); + Assertions.assertEquals(QueryState.MysqlStateType.OK, ctx.getState().getStateType()); + Assertions.assertEquals("su_change_login", ctx.getQualifiedUser()); + // the lane the switch chose goes with the switch: the new identity resolves its own + Assertions.assertEquals("", ctx.getSessionVariable().getWorkloadGroup()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + @Test public void testConnectionResetRestoresAuthenticatedIdentity() throws Exception { try { From 9d68b6cb046c8903eff8f3a193bcbc6f969c1ef2 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 07:01:18 +0530 Subject: [PATCH 24/30] [fix](auth) SU: user() names the authenticating account, the SQL-cache key carries the narrowing, and the tests cover the headline behaviours Four small points and the test gaps of the review, in one commit. user() / session_user() folded getUserWithLoginRemoteIpString(), which derives from currentUserIdentity -- the target after the switch -- so a client could not tell a proxied session from the person's own. MySQL's proxy model keeps USER() on the account that authenticated and CURRENT_USER() on the proxied one; user() now folds from the authenticated identity when the session is switched, while session_user() and current_user() stay on the effective identity. The FE SQL-cache key was catalog.db:identity:sql, so a person's own session and a gateway session narrowed as that person shared one entry and, whenever their privilege or policy views differed, each hit of one evicted the other's (the correctness gate on every hit was never affected). The key now carries the sorted override of a narrowed session. The SchemaScanNode note states the BE-before-FE dependency: an older BE drops the plan node's current_roles and its RPCs reach the FE without roles, where the handler runs un-narrowed -- the mirror of an older FE ignoring the field, and the reason BEs are upgraded first. Tests. The COM_CHANGE_USER case named "failed authentication" took the unknown- user branch (the fixture users have no password); it is renamed, and a rejected- password case for an account with a password (a 20-byte scramble that cannot match) now exercises the mismatch branch. NarrowedMetadataRpcTest covers every name-visibility wrapper (getDbNames, getTableNames, listTableStatus, describeTables) with the caller named by identity and by the deprecated user/user_ip pair, plus showProcessList with a target whose excluded role carries ADMIN_PRIV against a registered bystander session. The regression suite adds the headline row-policy behaviour: two PERMISSIVE policies bound to two of the person's roles and a RESTRICTIVE one bound to the person -- the person's own session sees both roles' rows, the session narrowed to one role sees that role's rows only. The negative unit cases of the ladder (default role, operator, admin, a non-analyzable identity), REVOKE after the switch and SHOW GRANTS inside the narrowed session landed with their fixes earlier in this series. An audit_log assertion stays out of the p0 suite: the audit plugin is a global switch the nonConcurrent audit suite flips, and AuditLogHelperTest covers the identity mapping. On the PR body (corrections to carry there): a narrowed request that names no caller is refused by fetchSchemaTableData only -- the other wrappers key the narrowing on the identity they synthesize from user/user_ip; the FrontendService fields sit on reserved ids, while TSchemaScanNode.18 and TMetaScanNode.4 are fresh ids; the reset/COM_CHANGE_USER revert is MySQL-protocol-only -- on Arrow Flight SQL the switch lasts until CloseSession. Review findings F-21, F-22, F-24, F-27, F-28, F-30 and F-31 on the SU pull request. Co-Authored-By: Claude Fable 5.1 --- .../common/cache/NereidsSqlCacheManager.java | 11 +- .../rules/FoldConstantRuleOnFE.java | 4 +- .../apache/doris/planner/SchemaScanNode.java | 2 + .../org/apache/doris/qe/ConnectContext.java | 8 + .../rules/expression/FoldConstantTest.java | 36 +++++ .../doris/qe/MysqlConnectProcessorSuTest.java | 33 +++- .../service/NarrowedMetadataRpcTest.java | 149 ++++++++++++++++++ .../auth_p0/test_su_session_narrowing.groovy | 34 +++- 8 files changed, 266 insertions(+), 11 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java b/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java index 5c9fb5fb33e995..12f9ef0b62d658 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java +++ b/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java @@ -95,6 +95,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; /** * NereidsSqlCacheManager @@ -326,8 +327,14 @@ private String generateCacheKey(ConnectContext connectContext, String sqlOrMd5) String currentCatalogName = currentCatalog != null ? currentCatalog.getName() : ""; String currentDatabase = connectContext.getDatabase(); String currentDatabaseName = currentDatabase != null ? currentDatabase : ""; - return currentCatalogName + "." + currentDatabaseName + ":" + connectContext.getCurrentUserIdentity().toString() - + ":" + sqlOrMd5; + String key = currentCatalogName + "." + currentDatabaseName + ":" + + connectContext.getCurrentUserIdentity().toString() + ":" + sqlOrMd5; + if (connectContext.isSessionRoleNarrowed()) { + // a session-narrowed (SU) session and the identity's own session see different privilege + // and policy views: their entries are kept apart instead of evicting each other on every hit + key = key + ":su=" + new TreeSet<>(connectContext.getSessionRoleOverride()); + } + return key; } private String normalizeSql(String sql) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java index e09121facb8974..306a79f0fcbb3e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java @@ -424,7 +424,9 @@ public Expression visitUser(User user, ExpressionRewriteContext context) { if (!foldContextDependentExpressions) { return user; } - String res = context.cascadesContext.getConnectContext().getUserWithLoginRemoteIpString(); + // MySQL proxy parity: USER() is the account that authenticated the connection (the switcher + // of a session-narrowed session); CURRENT_USER() and SESSION_USER() name the effective one + String res = context.cascadesContext.getConnectContext().getAuthenticatedUserWithLoginRemoteIpString(); return new VarcharLiteral(res); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java index 8a16c8bfd14627..6186f091a4f7bf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java @@ -192,6 +192,8 @@ protected void toThrift(TPlanNode msg) { // SU narrowing: carry the session's active role subset (current_roles) in the plan so the // BE->FE metadata RPCs (getDbNames/getTableNames/listTableStatus/describeTables) narrow // name visibility to the same set the session sees; null = normal session (no narrowing). + // A BE of an older build drops the field and its RPCs reach the FE without roles, where the + // handler runs un-narrowed: BEs are upgraded before FEs, the documented rolling-upgrade order. if (ConnectContext.get().isSessionRoleNarrowed()) { msg.schema_scan_node.setCurrentRoles(new HashSet<>(ConnectContext.get().getSessionRoleOverride())); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java index abca51d2944f3c..eab00ab0fe0f2b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java @@ -845,6 +845,14 @@ public String getUserWithLoginRemoteIpString() { return UserIdentity.createAnalyzedUserIdentWithIp(getQualifiedUser(), remoteIP).toString(); } + // used for select user(): the account that authenticated the connection, which in a + // session-narrowed (SU) session is the switcher -- MySQL's USER() names the proxying account + // and CURRENT_USER() the proxied one + public String getAuthenticatedUserWithLoginRemoteIpString() { + String user = authenticatedIdentity != null ? authenticatedIdentity.getQualifiedUser() : getQualifiedUser(); + return UserIdentity.createAnalyzedUserIdentWithIp(user, remoteIP).toString(); + } + public void setCurrentUserIdentity(UserIdentity currentUserIdentity) { this.currentUserIdentity = currentUserIdentity; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java index 543992b80d43e1..01f0a6a5b7bbfe 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java @@ -17,6 +17,7 @@ package org.apache.doris.nereids.rules.expression; +import org.apache.doris.analysis.UserIdentity; import org.apache.doris.nereids.analyzer.UnboundRelation; import org.apache.doris.nereids.exceptions.AnalysisException; import org.apache.doris.nereids.exceptions.CastException; @@ -95,6 +96,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondMicrosecondAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.ShortCircuitIf; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sign; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sin; @@ -108,6 +110,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeFormat; import org.apache.doris.nereids.trees.expressions.functions.scalar.ToDays; import org.apache.doris.nereids.trees.expressions.functions.scalar.UnixTimestamp; +import org.apache.doris.nereids.trees.expressions.functions.scalar.User; import org.apache.doris.nereids.trees.expressions.literal.ArrayLiteral; import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral; import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; @@ -145,6 +148,7 @@ import org.apache.doris.nereids.types.VarcharType; import org.apache.doris.nereids.types.VariantType; import org.apache.doris.nereids.util.MemoTestUtils; +import org.apache.doris.qe.ConnectContext; import org.apache.doris.qe.SessionVariable; import com.google.common.collect.ImmutableList; @@ -1930,4 +1934,36 @@ public void testSessionIsNarrowedFoldsOnlyWithSessionContext() throws Exception cascadesContext.getConnectContext().setSessionRoleOverride(null); } } + + /** + * MySQL proxy parity under SU: user() names the account that authenticated the connection (the + * switcher), while session_user() and current_user() name the effective identity (the target). + */ + @Test + public void testUserNamesTheAuthenticatingAccountUnderSu() { + ConnectContext ctx = cascadesContext.getConnectContext(); + UserIdentity before = ctx.getCurrentUserIdentity(); + UserIdentity person = UserIdentity.createAnalyzedUserIdentWithIp("person", "%"); + String personAtIp = UserIdentity.createAnalyzedUserIdentWithIp("person", ctx.getRemoteIP()).toString(); + String svcAtIp = UserIdentity.createAnalyzedUserIdentWithIp("svc", ctx.getRemoteIP()).toString(); + ctx.setCurrentUserIdentity(person); + try { + Assertions.assertEquals(new VarcharLiteral(personAtIp), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(new User(), context)); + + ctx.setAuthenticatedIdentity(UserIdentity.createAnalyzedUserIdentWithIp("svc", "%")); + ctx.setSessionRoleOverride(Collections.singleton("tenant")); + Assertions.assertEquals(new VarcharLiteral(svcAtIp), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(new User(), context)); + Assertions.assertEquals(new VarcharLiteral(personAtIp), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(new SessionUser(), context)); + Assertions.assertEquals(new VarcharLiteral(person.toString()), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite( + new org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentUser(), context)); + } finally { + ctx.setSessionRoleOverride(null); + ctx.setAuthenticatedIdentity(null); + ctx.setCurrentUserIdentity(before); + } + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java index 06b35071e09acc..8a86358ff082f2 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java @@ -23,6 +23,8 @@ import org.apache.doris.mysql.MysqlHandshakePacket; import org.apache.doris.mysql.privilege.PrivPredicate; import org.apache.doris.mysql.protocol.MysqlProtocolAdapter; +import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.trees.plans.commands.CreateUserCommand; import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; import org.apache.doris.utframe.TestWithFeService; @@ -34,6 +36,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Collections; public class MysqlConnectProcessorSuTest extends TestWithFeService { @@ -50,6 +53,10 @@ protected void runBeforeAll() throws Exception { grantRole("GRANT 'su_change_role' TO 'su_change_person'@'%'"); grantPriv("GRANT SELECT_PRIV ON internal.su_change_personal.* TO 'su_change_person'@'%'"); grantPriv("GRANT SELECT_PRIV ON internal.su_change_personal.* TO 'su_change_login'@'%'"); + // an account WITH a password, for the rejected-password branch of COM_CHANGE_USER + String createWithPassword = "CREATE USER 'su_change_pwd'@'%' IDENTIFIED BY 'Secret_123'"; + ((CreateUserCommand) new NereidsParser().parseSingle(createWithPassword)) + .run(connectContext, new StmtExecutor(connectContext, createWithPassword)); } private ConnectContext switchedContext() throws Exception { @@ -73,10 +80,15 @@ private boolean canSelect(ConnectContext ctx, String db) { } private void changeUser(ConnectContext ctx, String user) throws Exception { - // COM_CHANGE_USER payload, with an empty native password and no default database. + changeUser(ctx, user, new byte[0]); + } + + private void changeUser(ConnectContext ctx, String user, byte[] nativeScramble) throws Exception { + // COM_CHANGE_USER payload, with a native-password scramble (empty: no password) and no default database. ByteBuffer packet = ByteBuffer.allocate(256).order(ByteOrder.LITTLE_ENDIAN); packet.put(user.getBytes(StandardCharsets.UTF_8)).put((byte) 0); - packet.put((byte) 0); + packet.put((byte) nativeScramble.length); + packet.put(nativeScramble); packet.put((byte) 0); packet.putShort((short) 33); packet.put(MysqlHandshakePacket.AUTH_PLUGIN_NAME.getBytes(StandardCharsets.UTF_8)).put((byte) 0); @@ -114,7 +126,7 @@ public void testForbiddenChangeUserPreservesNarrowing() throws Exception { } @Test - public void testFailedAuthenticationPreservesNarrowing() throws Exception { + public void testUnknownUserPreservesNarrowing() throws Exception { try { ConnectContext ctx = switchedContext(); changeUser(ctx, "su_change_nonexistent"); @@ -125,6 +137,21 @@ public void testFailedAuthenticationPreservesNarrowing() throws Exception { } } + @Test + public void testRejectedPasswordPreservesNarrowing() throws Exception { + try { + ConnectContext ctx = switchedContext(); + // an existing account, a 20-byte native scramble that cannot match its password + byte[] wrongScramble = new byte[20]; + Arrays.fill(wrongScramble, (byte) 1); + changeUser(ctx, "su_change_pwd", wrongScramble); + Assertions.assertEquals(QueryState.MysqlStateType.ERR, ctx.getState().getStateType()); + assertStillNarrowed(ctx); + } finally { + connectContext.setThreadLocalInfo(); + } + } + @Test public void testSuccessfulChangeUserClearsNarrowing() throws Exception { try { diff --git a/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java index c30699deecda84..a4f570cba4dc5b 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java @@ -20,18 +20,26 @@ import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.Env; import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.ConnectPoolMgr; +import org.apache.doris.thrift.TDescribeTablesParams; import org.apache.doris.thrift.TFetchSchemaTableDataRequest; import org.apache.doris.thrift.TFetchSchemaTableDataResult; +import org.apache.doris.thrift.TGetDbsParams; +import org.apache.doris.thrift.TGetTablesParams; import org.apache.doris.thrift.TSchemaTableName; import org.apache.doris.thrift.TSchemaTableRequestParams; +import org.apache.doris.thrift.TShowProcessListRequest; import org.apache.doris.thrift.TStatusCode; import org.apache.doris.utframe.TestWithFeService; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import java.util.List; import java.util.Set; /** @@ -43,16 +51,82 @@ public class NarrowedMetadataRpcTest extends TestWithFeService { private final ExecuteEnv exeEnv = Mockito.mock(ExecuteEnv.class); + private static final Set NARROWED = Sets.newHashSet("tenant_c"); + private static final UserIdentity CAROL = UserIdentity.createAnalyzedUserIdentWithIp("carol", "%"); + @Override protected void runBeforeAll() throws Exception { createDatabase("test"); createDatabase("perso"); + createTable("create table perso.t1 (k int) distributed by hash(k) buckets 1" + + " properties('replication_num' = '1')"); + createTable("create table test.t1 (k int) distributed by hash(k) buckets 1" + + " properties('replication_num' = '1')"); addUser("carol", true); createRole("tenant_c"); grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'tenant_c';"); grantRole("GRANT 'tenant_c' TO 'carol'@'%'"); // a personal (default-role) grant the narrowing must drop grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'carol'@'%';"); + // dave: a plain tenant role and an ADMIN-bearing one the switch leaves out; a bystander session + addUser("dave", true); + addUser("bystander_rpc", true); + createRole("tenant_d"); + createRole("ops_d"); + grantPriv("GRANT ADMIN_PRIV ON *.*.* TO ROLE 'ops_d';"); + grantRole("GRANT 'tenant_d','ops_d' TO 'dave'@'%'"); + } + + /** The caller as the request names it: the identity struct, or the deprecated user/user_ip pair. */ + private static void nameCaller(TGetDbsParams params, boolean byIdentity) { + if (byIdentity) { + params.setCurrentUserIdent(CAROL.toThrift()); + } else { + params.setUser("carol"); + params.setUserIp("%"); + } + } + + private static void nameCaller(TGetTablesParams params, boolean byIdentity) { + if (byIdentity) { + params.setCurrentUserIdent(CAROL.toThrift()); + } else { + params.setUser("carol"); + params.setUserIp("%"); + } + } + + private static TGetDbsParams dbs(Set currentRoles, boolean byIdentity) { + TGetDbsParams params = new TGetDbsParams(); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + nameCaller(params, byIdentity); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; + } + + private static TGetTablesParams tables(String db, Set currentRoles, boolean byIdentity) { + TGetTablesParams params = new TGetTablesParams(); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + params.setDb(db); + nameCaller(params, byIdentity); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; + } + + private static TDescribeTablesParams describe(String db, Set currentRoles) { + TDescribeTablesParams params = new TDescribeTablesParams(); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + params.setDb(db); + params.setTablesName(Lists.newArrayList("t1")); + params.setCurrentUserIdent(CAROL.toThrift()); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; } private static TFetchSchemaTableDataRequest databaseProperties(String db, Set currentRoles) { @@ -98,4 +172,79 @@ public void testNarrowedRequestWithoutIdentityFailsClosed() throws Exception { TFetchSchemaTableDataResult result = impl.fetchSchemaTableData(request); Assertions.assertNotEquals(TStatusCode.OK, result.getStatus().getStatusCode()); } + + /** + * Every wrapped name-visibility handler narrows alike, whether the request names the caller by + * identity or by the deprecated user/user_ip pair: the personal grant on perso vanishes under the + * tenant role, the role grant on test stays, and nothing sticks to the handler thread. + */ + @Test + public void testEveryNameVisibilityHandlerNarrows() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + for (boolean byIdentity : new boolean[] {true, false}) { + Assertions.assertTrue(impl.getDbNames(dbs(null, byIdentity)).getDbs().contains("perso")); + Assertions.assertFalse(impl.getDbNames(dbs(NARROWED, byIdentity)).getDbs().contains("perso")); + Assertions.assertTrue(impl.getDbNames(dbs(NARROWED, byIdentity)).getDbs().contains("test")); + + Assertions.assertEquals(1, impl.getTableNames(tables("perso", null, byIdentity)).getTables().size()); + Assertions.assertEquals(0, impl.getTableNames(tables("perso", NARROWED, byIdentity)).getTables().size()); + Assertions.assertEquals(1, impl.getTableNames(tables("test", NARROWED, byIdentity)).getTables().size()); + + Assertions.assertEquals(1, impl.listTableStatus(tables("perso", null, byIdentity)).getTables().size()); + Assertions.assertEquals(0, impl.listTableStatus(tables("perso", NARROWED, byIdentity)).getTables().size()); + Assertions.assertEquals(1, impl.listTableStatus(tables("test", NARROWED, byIdentity)).getTables().size()); + + // the narrowing did not stick to the handler thread + Assertions.assertEquals(1, impl.listTableStatus(tables("perso", null, byIdentity)).getTables().size()); + } + Assertions.assertEquals(1, impl.describeTables(describe("perso", null)).getColumns().size()); + Assertions.assertEquals(0, impl.describeTables(describe("perso", NARROWED)).getColumns().size()); + Assertions.assertEquals(1, impl.describeTables(describe("test", NARROWED)).getColumns().size()); + } + + /** + * The FE->FE processlist fan-out: a target holding an ADMIN-bearing role the switch left out sees + * every session un-narrowed, and only its own sessions under the narrowing. + */ + @Test + public void testShowProcessListNarrowsTheAdminCheck() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + UserIdentity dave = UserIdentity.createAnalyzedUserIdentWithIp("dave", "%"); + ConnectPoolMgr pool = ExecuteEnv.getInstance().getScheduler().getConnectPoolMgr(); + ConnectContext daves = registered(pool, dave, 7001); + ConnectContext bystanders = registered(pool, + UserIdentity.createAnalyzedUserIdentWithIp("bystander_rpc", "%"), 7002); + try { + TShowProcessListRequest request = new TShowProcessListRequest(); + request.setShowFullSql(true); + request.setCurrentUserIdent(dave.toThrift()); + Set everyUser = usersListed(impl.showProcessList(request).getProcessList()); + Assertions.assertTrue(everyUser.contains("bystander_rpc"), everyUser.toString()); + + request.setCurrentRoles(Sets.newHashSet("tenant_d")); + Set narrowedUsers = usersListed(impl.showProcessList(request).getProcessList()); + Assertions.assertEquals(Sets.newHashSet("dave"), narrowedUsers); + } finally { + pool.unregisterConnection(daves); + pool.unregisterConnection(bystanders); + } + } + + private static ConnectContext registered(ConnectPoolMgr pool, UserIdentity user, int connectionId) { + ConnectContext ctx = new ConnectContext(); + ctx.setEnv(Env.getCurrentEnv()); + ctx.setCurrentUserIdentity(user); + ctx.setConnectionId(connectionId); + Assertions.assertEquals(-1, pool.registerConnection(ctx)); + return ctx; + } + + /** The User column (index 2) of every processlist row. */ + private static Set usersListed(List> rows) { + Set users = Sets.newHashSet(); + for (List row : rows) { + users.add(row.get(2)); + } + return users; + } } diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy index 5fcb888bedad0d..0c2db9f0e8e973 100644 --- a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -26,13 +26,18 @@ suite("test_su_session_narrowing", "p0,auth") { String pwd = 'C123_567p' String tenantRole = 'su_narrow_tenant' String otherRole = 'su_narrow_other' + String extraRole = 'su_narrow_extra' String roleDb = 'su_narrow_role_db' String ownDb = 'su_narrow_own_db' + try_sql("DROP ROW POLICY IF EXISTS su_narrow_tenant_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_extra_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_person_rows ON ${roleDb}.t") try_sql("DROP USER ${svc}") try_sql("DROP USER ${person}") try_sql("DROP ROLE ${tenantRole}") try_sql("DROP ROLE ${otherRole}") + try_sql("DROP ROLE ${extraRole}") sql """DROP DATABASE IF EXISTS ${roleDb}""" sql """DROP DATABASE IF EXISTS ${ownDb}""" @@ -47,12 +52,24 @@ suite("test_su_session_narrowing", "p0,auth") { sql """CREATE ROLE ${tenantRole}""" sql """CREATE ROLE ${otherRole}""" + sql """CREATE ROLE ${extraRole}""" sql """CREATE USER '${person}' IDENTIFIED BY '${pwd}'""" sql """CREATE USER '${svc}' IDENTIFIED BY '${pwd}'""" - // the person: a tenant role on one database, a direct grant on another + // the person: a tenant role on one database, a direct grant on another, and a second role + // the switch will leave out sql """GRANT SELECT_PRIV ON ${roleDb}.* TO ROLE '${tenantRole}'""" sql """GRANT '${tenantRole}' TO '${person}'@'%'""" + sql """GRANT '${extraRole}' TO '${person}'@'%'""" sql """GRANT SELECT_PRIV ON ${ownDb}.* TO '${person}'@'%'""" + // row policies: one PERMISSIVE per role (OR-ed for whoever holds the roles), one RESTRICTIVE + // bound to the person (follows the identity): the person's own session sees k IN (1, 2), a + // session narrowed to the tenant role sees k = 1 only + sql """CREATE ROW POLICY su_narrow_tenant_rows ON ${roleDb}.t AS PERMISSIVE + TO ROLE '${tenantRole}' USING (k = 1)""" + sql """CREATE ROW POLICY su_narrow_extra_rows ON ${roleDb}.t AS PERMISSIVE + TO ROLE '${extraRole}' USING (k = 2)""" + sql """CREATE ROW POLICY su_narrow_person_rows ON ${roleDb}.t AS RESTRICTIVE + TO '${person}'@'%' USING (k < 3)""" // the service account: nothing but the right to switch sql """GRANT PROXY_PRIV ON *.*.* TO '${svc}'@'%'""" // the framework connects with the default database in the URL, which needs a privilege on it @@ -117,9 +134,11 @@ suite("test_su_session_narrowing", "p0,auth") { def narrowed = sql """SELECT CAST(session_is_narrowed() AS INT)""" assertEquals(1, narrowed[0][0] as int) - // the requested role's grant works ... - def rows = sql """SELECT COUNT(*) FROM ${roleDb}.t""" - assertEquals(2, rows[0][0] as int) + // the requested role's grant works, under the requested role's row policy only: the + // excluded role's PERMISSIVE policy does not widen the rows, the person's own applies + def rows = sql """SELECT k FROM ${roleDb}.t ORDER BY k""" + assertEquals(1, rows.size()) + assertEquals(1, rows[0][0] as int) // ... the person's direct grant is inert under the switch test { sql """SELECT * FROM ${ownDb}.t""" @@ -169,7 +188,8 @@ suite("test_su_session_narrowing", "p0,auth") { } } - // the person's own login is untouched: both grants live, nothing is narrowed + // the person's own login is untouched: both grants live, both roles' policies apply, + // nothing is narrowed connect(person, pwd, context.config.jdbcUrl) { def own = sql """SELECT COUNT(*) FROM ${ownDb}.t""" assertEquals(1, own[0][0] as int) @@ -179,10 +199,14 @@ suite("test_su_session_narrowing", "p0,auth") { assertEquals(0, narrowed[0][0] as int) } } finally { + try_sql("DROP ROW POLICY IF EXISTS su_narrow_tenant_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_extra_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_person_rows ON ${roleDb}.t") try_sql("DROP USER ${svc}") try_sql("DROP USER ${person}") try_sql("DROP ROLE ${tenantRole}") try_sql("DROP ROLE ${otherRole}") + try_sql("DROP ROLE ${extraRole}") try_sql("DROP DATABASE IF EXISTS ${roleDb}") try_sql("DROP DATABASE IF EXISTS ${ownDb}") } From 814ed4a828924c849c3f7f83696f273d1da92405 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 07:01:47 +0530 Subject: [PATCH 25/30] [fix](auth) SU: drop the HashSet import Auth no longer uses The placement listing in getUserRoleWorkloadGroupPrivs was the last plain use of java.util.HashSet in Auth; with it on getRoleNamesByUserWithLdap the import is unused and checkstyle (UnusedImports) refuses the build. Co-Authored-By: Claude Fable 5.1 --- .../src/main/java/org/apache/doris/mysql/privilege/Auth.java | 1 - 1 file changed, 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index 460fb1f6b2655d..f801b987c66a5e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -87,7 +87,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; From e57998430ff37434ea56992767447361484a5c81 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 07:06:39 +0530 Subject: [PATCH 26/30] [fix](auth) SU: the routine-load workload-group lookup keeps its identity-only context The previous commit made CreateRoutineLoadInfo's temporary context inherit the session's narrowing (F-17). That context exists only to resolve the job's workload group by name for an existence check -- placement, not data access, and the narrowed session may use every group the person's own session may use (the placement rule of this series). Narrowing the lookup therefore changes nothing a user can observe, and master has since moved that lookup into a static helper that builds its own identity-only context (#69749), so the inherited form would only conflict on merge. The data-access site of F-17, the connector rewrite's INSERT ... SELECT task context, keeps inheritIdentityFrom. Co-Authored-By: Claude Fable 5.1 --- .../trees/plans/commands/info/CreateRoutineLoadInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java index 8f90c6fbe79663..d31ae0f8363092 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java @@ -631,7 +631,7 @@ public void checkJobProperties() throws UserException { String inputWorkloadGroupStr = jobProperties.get(WORKLOAD_GROUP); if (!StringUtils.isEmpty(inputWorkloadGroupStr)) { ConnectContext tmpCtx = new ConnectContext(); - tmpCtx.inheritIdentityFrom(ConnectContext.get()); + tmpCtx.setCurrentUserIdentity(ConnectContext.get().getCurrentUserIdentity()); tmpCtx.getSessionVariable().setWorkloadGroup(inputWorkloadGroupStr); if (Config.isCloudMode()) { tmpCtx.setCloudCluster(ConnectContext.get().getCloudCluster()); From c565d53020f3555a3d48f8d05c3d46e311fd1991 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 09:12:33 +0530 Subject: [PATCH 27/30] [fix](planner) SU: drop the stray copy of the all-FE build gate left at class level in SchemaScanNode Round-2 commit 3ba24eaf26d added the all-FE mixed-build gate of a session-narrowed information_schema scan twice: once inside finalizeForNereids, where it belongs, and once more as a stray copy right after mixedBuildMessage(), at class level, followed by a brace that closed the class early. Round-3 commit 63ba6066697 re-touched the copy when it moved the helper into Env, so it survived both commits unnoticed; javac rejects it ("illegal start of type"), so fe-core did not build. The surviving gate in finalizeForNereids and setFeAddrList both key on SchemaTable.shouldFetchAllFe(), so nothing else changes. Candidate review finding F-01 (Blocker) on apache doris PR 67672. Co-Authored-By: Claude Fable 5.1 --- .../main/java/org/apache/doris/planner/SchemaScanNode.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java index 6186f091a4f7bf..d466b31fea3cc9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java @@ -126,11 +126,6 @@ private static String mixedBuildMessage(String target) { + "Retry once the rolling upgrade completes."; } - if (ConnectContext.get().isSessionRoleNarrowed() && tableIf instanceof SchemaTable - && ((SchemaTable) tableIf).shouldFetchAllFe() && !Env.getCurrentEnv().aliveFrontendsRunSameBuild()) { - throw new UserException(mixedBuildMessage("every alive FE")); - } - } private void setFeAddrList(TPlanNode msg) { List feAddrList = new ArrayList(); TableIf tableIf = desc.getTable(); From 1923b97b741968effa7e5088ea8f147073705035 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 09:15:33 +0530 Subject: [PATCH 28/30] [fix](auth) SU: the compute-group placement widening tests the bit a CLUSTER grant is stored under grantsComputeGroupUsage asked the own role's cloud-cluster table for USAGE_PRIV, but a GRANT USAGE_PRIV ON CLUSTER never stores that bit: GrantResourcePrivilegeCommand (and its REVOKE twin) run PrivBitSet.convertResourcePrivToCloudPriv first, which replaces a lone USAGE_PRIV with CLUSTER_USAGE_PRIV. The normal check passes because Role.checkCloudInternal satisfies PrivPredicate.USAGE, an OR over ADMIN, USAGE, CLUSTER_USAGE and STAGE_USAGE; the widening asked for the one bit that is never there and so never fired -- a session narrowed to a tenant role was refused the person's own compute group, and the new CloudAuthTest case (testNarrowedSessionKeepsPersonsComputeGroupUsage) failed by construction. The helper now accepts CLUSTER_USAGE_PRIV too (containsPrivs is any-of). The workload-group twin is untouched: workload-group grants are not converted (only CLUSTER and STAGE are) and really are stored as USAGE_PRIV. Candidate review finding F-02 (Blocker) on apache doris PR 67672. Co-Authored-By: Claude Fable 5.1 --- .../src/main/java/org/apache/doris/mysql/privilege/Auth.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index f801b987c66a5e..1a2599c7135235 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -533,7 +533,8 @@ private static boolean grantsComputeGroupUsage(Role role, String computeGroupNam if (virtualComputeGroup != null) { role.getCloudClusterPrivTable().getPrivs(virtualComputeGroup, privs); } - return privs.containsPrivs(Privilege.USAGE_PRIV); + // GRANT USAGE_PRIV ON CLUSTER is stored as CLUSTER_USAGE_PRIV (PrivBitSet.convertResourcePrivToCloudPriv) + return privs.containsPrivs(Privilege.USAGE_PRIV, Privilege.CLUSTER_USAGE_PRIV); } /** From 826fd255d2e76643698796ec90f90995cfa7e1e7 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 09:18:27 +0530 Subject: [PATCH 29/30] [fix](auth) SU: listTableMetadataNameIds carries the RPC narrowing onto its executor thread; every wrapped handler is tested withSessionRoleOverride installs the BE->FE request's role subset in Auth.RPC_SESSION_NARROWING, a ThreadLocal of the thrift handler thread, and listTableMetadataNameIdsImpl then submits its whole walk -- including its only privilege decision, checkTblPriv(currentUser, ..., SHOW) -- to a fresh single-thread executor, where the ThreadLocal is unset and no ConnectContext exists: getRolesByUserWithLdap fell through to the user->role map, and a narrowed session's information_schema.metadata_name_ids listed every table the target may SHOW through any role, the ones SU excluded included. This is the second half of review thread T9 on the pull request, which the round-2 wrapper answered on the handler thread only. The handler now captures the narrowing withSessionRoleOverride installed (Auth.captureRpcSessionNarrowing: the map keyed by the identity the wrapper synthesized, so the key has one source of truth and the user/user_ip fallback needs no second copy of that rule) and installs it on the executor thread for the duration of the task (Auth.installRpcSessionNarrowing, cleared in a finally). Capturing beats hoisting the check: the executor exists for the external-catalog timeout, and the per-table check sits behind db.getTables(), which is what the timeout bounds. The other wrappers are untouched. NarrowedMetadataRpcTest.testEveryNameVisibilityHandlerNarrows covered four of the eight wrapped handlers; it now drives all eight, by identity and by the deprecated user/user_ip pair: listTableMetadataNameIds (the executor-thread case), listTablePrivilegeStatus and listSchemaPrivilegeStatus (the personal rows on perso vanish, the role's rows on test stay -- a table-level twin of each db-level grant joins the fixture), and listUserPrivilegeStatus (gated on the caller's own global SHOW, which dave holds only through the ADMIN-bearing role the switch leaves out: rows un-narrowed, none narrowed). Candidate review findings F-03 (Major) and F-04 (Minor) on apache doris PR 67672. Co-Authored-By: Claude Fable 5.1 --- .../apache/doris/mysql/privilege/Auth.java | 19 ++++++ .../doris/service/FrontendServiceImpl.java | 49 ++++++++------- .../service/NarrowedMetadataRpcTest.java | 61 +++++++++++++++++-- 3 files changed, 104 insertions(+), 25 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index 1a2599c7135235..07ea65531defc5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -316,6 +316,25 @@ public static void clearRpcSessionNarrowing() { RPC_SESSION_NARROWING.remove(); } + /** + * The RPC narrowing installed on this thread, or null when there is none. A handler that hands + * part of its work to another thread captures it here and installs it there with + * {@link #installRpcSessionNarrowing}: a ThreadLocal does not follow the work, and a privilege + * check on the other thread would otherwise resolve the target's full role union. + */ + public static Map> captureRpcSessionNarrowing() { + return RPC_SESSION_NARROWING.get(); + } + + /** Installs a captured narrowing on this thread (none when it is null); clear it in a finally. */ + public static void installRpcSessionNarrowing(Map> narrowing) { + if (narrowing == null) { + RPC_SESSION_NARROWING.remove(); + } else { + RPC_SESSION_NARROWING.set(narrowing); + } + } + // The SU-narrowed role set of a target identity: the override roles the target STILL holds + the // implicit information_schema/mysql read baseline (SU_NARROWING_BASELINE). The ceiling is // re-evaluated on every resolution, exactly as a normal session re-reads its grants on every diff --git a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java index 6bd6aefc647595..65ba2108c6c526 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java +++ b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java @@ -899,36 +899,43 @@ private TListTableMetadataNameIdsResult listTableMetadataNameIdsImpl(TGetTablesP } } PatternMatcher finalMatcher = matcher; + // the RPC narrowing withSessionRoleOverride installed is a ThreadLocal of the handler thread: + // carry it onto the executor thread, or the privilege check below runs un-narrowed + Map> narrowing = Auth.captureRpcSessionNarrowing(); ExecutorService executor = Executors.newSingleThreadExecutor(); Future future = executor.submit(() -> { - - CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(catalogName); - if (catalog != null) { - String dbName = getDbNameFromMysqlTableSchema(catalogName, params.db); - DatabaseIf db = catalog.getDbNullable(dbName); - if (db != null) { - List tables = db.getTables(); - for (TableIf table : tables) { - if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(currentUser, catalogName, dbName, - table.getName(), PrivPredicate.SHOW)) { - continue; - } - table.readLock(); - try { - if (finalMatcher != null && !finalMatcher.match(table.getName())) { + Auth.installRpcSessionNarrowing(narrowing); + try { + CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(catalogName); + if (catalog != null) { + String dbName = getDbNameFromMysqlTableSchema(catalogName, params.db); + DatabaseIf db = catalog.getDbNullable(dbName); + if (db != null) { + List tables = db.getTables(); + for (TableIf table : tables) { + if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(currentUser, catalogName, + dbName, table.getName(), PrivPredicate.SHOW)) { continue; } - TTableMetadataNameIds status = new TTableMetadataNameIds(); - status.setName(table.getName()); - status.setId(table.getId()); + table.readLock(); + try { + if (finalMatcher != null && !finalMatcher.match(table.getName())) { + continue; + } + TTableMetadataNameIds status = new TTableMetadataNameIds(); + status.setName(table.getName()); + status.setId(table.getId()); - tablesResult.add(status); - } finally { - table.readUnlock(); + tablesResult.add(status); + } finally { + table.readUnlock(); + } } } } + } finally { + Auth.clearRpcSessionNarrowing(); } }); try { diff --git a/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java index a4f570cba4dc5b..e91789610f498d 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java @@ -27,6 +27,7 @@ import org.apache.doris.thrift.TFetchSchemaTableDataResult; import org.apache.doris.thrift.TGetDbsParams; import org.apache.doris.thrift.TGetTablesParams; +import org.apache.doris.thrift.TListPrivilegesResult; import org.apache.doris.thrift.TSchemaTableName; import org.apache.doris.thrift.TSchemaTableRequestParams; import org.apache.doris.thrift.TShowProcessListRequest; @@ -65,9 +66,11 @@ protected void runBeforeAll() throws Exception { addUser("carol", true); createRole("tenant_c"); grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'tenant_c';"); + grantPriv("GRANT SELECT_PRIV ON internal.test.t1 TO ROLE 'tenant_c';"); grantRole("GRANT 'tenant_c' TO 'carol'@'%'"); - // a personal (default-role) grant the narrowing must drop + // personal (default-role) grants the narrowing must drop, one per privilege level grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'carol'@'%';"); + grantPriv("GRANT SELECT_PRIV ON internal.perso.t1 TO 'carol'@'%';"); // dave: a plain tenant role and an ADMIN-bearing one the switch leaves out; a bystander session addUser("dave", true); addUser("bystander_rpc", true); @@ -117,6 +120,32 @@ private static TGetTablesParams tables(String db, Set currentRoles, bool return params; } + /** A privilege-status request (TGetTablesParams without a db) from the named caller. */ + private static TGetTablesParams privileges(String user, Set currentRoles, boolean byIdentity) { + TGetTablesParams params = new TGetTablesParams(); + if (byIdentity) { + params.setCurrentUserIdent(UserIdentity.createAnalyzedUserIdentWithIp(user, "%").toThrift()); + } else { + params.setUser(user); + params.setUserIp("%"); + } + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; + } + + /** The rows of a privilege-status listing on the named schema. */ + private static long rowsOn(TListPrivilegesResult result, String schema) { + return result.getPrivileges().stream().filter(row -> schema.equals(row.getSchema())).count(); + } + + /** How many tables information_schema.metadata_name_ids lists in db for the caller. */ + private static int metadataNameIds(FrontendServiceImpl impl, String db, Set currentRoles, + boolean byIdentity) throws Exception { + return impl.listTableMetadataNameIds(tables(db, currentRoles, byIdentity)).getTables().size(); + } + private static TDescribeTablesParams describe(String db, Set currentRoles) { TDescribeTablesParams params = new TDescribeTablesParams(); params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); @@ -174,9 +203,12 @@ public void testNarrowedRequestWithoutIdentityFailsClosed() throws Exception { } /** - * Every wrapped name-visibility handler narrows alike, whether the request names the caller by - * identity or by the deprecated user/user_ip pair: the personal grant on perso vanishes under the - * tenant role, the role grant on test stays, and nothing sticks to the handler thread. + * Every one of the eight wrapped name-visibility handlers narrows alike, whether the request + * names the caller by identity or by the deprecated user/user_ip pair: the personal grants on + * perso vanish under the tenant role, the role grants on test stay, and nothing sticks to the + * handler thread. listTableMetadataNameIds decides on an executor thread of its own, and the + * global privilege listing is gated on the caller's own global SHOW, which dave holds only + * through the ADMIN-bearing role the switch leaves out. */ @Test public void testEveryNameVisibilityHandlerNarrows() throws Exception { @@ -194,8 +226,29 @@ public void testEveryNameVisibilityHandlerNarrows() throws Exception { Assertions.assertEquals(0, impl.listTableStatus(tables("perso", NARROWED, byIdentity)).getTables().size()); Assertions.assertEquals(1, impl.listTableStatus(tables("test", NARROWED, byIdentity)).getTables().size()); + // the check runs on the handler's executor thread: the narrowing must follow it there + Assertions.assertEquals(1, metadataNameIds(impl, "perso", null, byIdentity)); + Assertions.assertEquals(0, metadataNameIds(impl, "perso", NARROWED, byIdentity)); + Assertions.assertEquals(1, metadataNameIds(impl, "test", NARROWED, byIdentity)); + + TGetTablesParams own = privileges("carol", null, byIdentity); + TGetTablesParams narrowed = privileges("carol", NARROWED, byIdentity); + Assertions.assertEquals(1, rowsOn(impl.listTablePrivilegeStatus(own), "perso")); + Assertions.assertEquals(0, rowsOn(impl.listTablePrivilegeStatus(narrowed), "perso")); + Assertions.assertEquals(1, rowsOn(impl.listTablePrivilegeStatus(narrowed), "test")); + Assertions.assertEquals(1, rowsOn(impl.listSchemaPrivilegeStatus(own), "perso")); + Assertions.assertEquals(0, rowsOn(impl.listSchemaPrivilegeStatus(narrowed), "perso")); + Assertions.assertEquals(1, rowsOn(impl.listSchemaPrivilegeStatus(narrowed), "test")); + + TGetTablesParams daveNarrowed = privileges("dave", Sets.newHashSet("tenant_d"), byIdentity); + Assertions.assertFalse(impl.listUserPrivilegeStatus(privileges("dave", null, byIdentity)) + .getPrivileges().isEmpty()); + Assertions.assertTrue(impl.listUserPrivilegeStatus(daveNarrowed).getPrivileges().isEmpty()); + // the narrowing did not stick to the handler thread Assertions.assertEquals(1, impl.listTableStatus(tables("perso", null, byIdentity)).getTables().size()); + Assertions.assertEquals(1, metadataNameIds(impl, "perso", null, byIdentity)); + Assertions.assertEquals(1, rowsOn(impl.listTablePrivilegeStatus(own), "perso")); } Assertions.assertEquals(1, impl.describeTables(describe("perso", null)).getColumns().size()); Assertions.assertEquals(0, impl.describeTables(describe("perso", NARROWED)).getColumns().size()); From 815fb022174b77b2da4a78985f9dfad494978955 Mon Sep 17 00:00:00 2001 From: Raghvendra Singh Date: Tue, 3 Nov 2026 09:20:07 +0530 Subject: [PATCH 30/30] [fix](auth) SU: tidy -- the build-gate Javadoc sits on its own method, and the un-narrowed role resolver Auth left public is gone Env.aliveFrontendsRunSameBuild was inserted between frontendRunsSameBuild's Javadoc and its method, so the first block documented the wrong method and frontendRunsSameBuild carried none (checkstyle's InvalidJavadocPosition skips comment siblings; the javadoc tool and the IDE hover do not). Each method now carries its own. Auth.getRolesByUser(UserIdentity, boolean) lost its last caller when the authorization-SPI role bridge moved to getEffectiveRoleNames; a public resolver that bypasses the narrowing choke point by construction invited the next bridge to use it. Removed; no caller remains anywhere under fe/ (UserRoleManager's own two-argument form is not this change's). getGrantedRoleNames is the sanctioned un-narrowed resolver. Candidate review findings F-05 (Nit) and F-06 (Nit) on apache doris PR 67672. Co-Authored-By: Claude Fable 5.1 --- .../src/main/java/org/apache/doris/catalog/Env.java | 12 ++++++------ .../java/org/apache/doris/mysql/privilege/Auth.java | 9 --------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index 138247bdcf046d..b56ab4b56fc8fe 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -5637,12 +5637,6 @@ public boolean masterRunsSameBuild() { return frontendRunsSameBuild(getMasterHost(), getMasterRpcPort()); } - /** - * Whether the FE at this RPC address is this FE itself or reports the same build as this FE. - * Every FE sees every other FE's build: the master heartbeats all of them and journals the - * responses (HbPackage, FrontendHbResponse.version), which the other FEs replay. An FE this FE - * does not know, or a dead one, counts as a different build. - */ /** * Whether every alive FE reports the same build as this FE (see {@link #frontendRunsSameBuild}): * the gate of a session-narrowed (SU) session's requests that fan out to every FE, which an FE @@ -5658,6 +5652,12 @@ public boolean aliveFrontendsRunSameBuild() { return true; } + /** + * Whether the FE at this RPC address is this FE itself or reports the same build as this FE. + * Every FE sees every other FE's build: the master heartbeats all of them and journals the + * responses (HbPackage, FrontendHbResponse.version), which the other FEs replay. An FE this FE + * does not know, or a dead one, counts as a different build. + */ public boolean frontendRunsSameBuild(String host, int rpcPort) { if (host.equals(selfNode.getHost()) && rpcPort == Config.rpc_port) { return true; diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index 07ea65531defc5..b385e1301382b2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -223,15 +223,6 @@ public boolean checkPlainPasswordForTest(String remoteUser, String remoteHost, S } } - public Set getRolesByUser(UserIdentity user, boolean showUserDefaultRole) { - readLock(); - try { - return userRoleManager.getRolesByUser(user, showUserDefaultRole); - } finally { - readUnlock(); - } - } - public boolean requiresCertificateAuth(UserIdentity userIdentity) { return userIdentity != null && userIdentity.hasSanRequirement(); }