Skip to content

[feature](auth) SU ... WITH ROLES: proxy authentication with mandatory role narrowing, gated by PROXY_PRIV - #67672

Open
raghav-reglobe wants to merge 30 commits into
apache:masterfrom
raghav-reglobe:su-upstream
Open

raghav-reglobe wants to merge 30 commits into
apache:masterfrom
raghav-reglobe:su-upstream

Conversation

@raghav-reglobe

@raghav-reglobe raghav-reglobe commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #67506

Related PR: #63239 (reserved the current_roles / is_su_user ids on the FrontendService request structs; the plan-node fields, TMasterOpRequest.authenticated_user_ident and TShowUserRequest.current_roles take fresh ids), #67444 (the same identity-in-request pattern for mysql.user)

Problem Summary:

A multi-tenant middle tier — an AI or BI gateway, an MCP server, an embedded-analytics backend — can't make Doris enforce the end user's entitlements, nor attribute the query to that person. Its choices today are one shared service account (no per-person audit, app-level authz) or holding every person's password and getting their full role union. What it needs is what MySQL's PROXY privilege and Oracle's CONNECT THROUGH give: authenticate as the service, then run this session as a named user, restricted to an explicit subset of that user's roles.

This adds exactly that:

GRANT PROXY_PRIV ON *.*.* TO 'svc_gateway'@'%';
-- per end-user session, on the service's connection:
SU 'alice'@'%' WITH ROLES ('tenant_42', 'tenant_42_scoped') [WORKLOAD GROUP 'wg_tenant_42'];
  • PROXY_PRIV is a new global privilege in MySQL's vocabulary (bit 15; ADMIN_PRIV implies it; grantable only on *.*.* like ADMIN_PRIV/NODE_PRIV; shown by SHOW GRANTS). It is the only gate on SU.
  • The role list is mandatory and only narrows. It replaces the target's role union for the session; every role must already be granted to the target, so the session can never exceed the person's real authority. The person's direct/default-role grants are not active under the switch. (The dormant-role property the issue floated is withdrawn — with the list mandatory it isn't needed.)
  • current_user() is the target, so user-bound row policies and current_user()-keyed predicates evaluate as the person; the audit log records the person as user, and a new authenticated_user column/field beside it records who actually logged in (equal to user unless switched).
  • Session-only, one-shot. A second SU is refused. On the MySQL protocol, resetConnection()/COM_CHANGE_USER revert to the authenticated identity, never to the target's full roles; Arrow Flight SQL has no such command, so there the switch lasts until CloseSession. Nothing is persisted.
  • session_is_narrowed() (BOOLEAN, FE-folded like current_user()) lets policies tell a switched session apart.

One choke point: Auth.getRolesByUserWithLdap returns the session override when one is set; privilege checks, row policies and SHOW GRANTS follow from that with no change to the privilege tables. The information_schema/mysql read baseline is kept so clients keep working, and the narrowed session keeps the person's own workload-group and compute-group USAGE (placement follows the person); stage and storage-vault usage stay on the narrowed set.

Carrying the narrowing across the FE/BE boundary. Two kinds of work leave the ConnectContext behind and were authorized against the target's full role union: the BE→FE metadata RPCs behind information_schema/mysql tables, the metadata table functions and processlist (name and row leaks on the handler thread), and statements forwarded to the master. The narrowed role set now travels as current_roles on the FrontendService request structs that #63239 reserved the ids for (TGetDbsParams, TGetTablesParams, TDescribeTablesParams, TMetadataTableRequestParams, TSchemaTableRequestParams, TShowProcessListRequest, and TMasterOpRequest 34 with is_su_user 35), and on fresh ids where nothing was reserved: TSchemaScanNode 18 and TMetaScanNode 4 (the plan nodes), TMasterOpRequest 37 (authenticated_user_ident, so the master's audit row names the authenticating account too) and TShowUserRequest 2. Every fresh id is unused on branch-3.1, 4.0, 4.1, 4.2 and master. On the FE one helper runs each handler under a thread-local narrowing keyed by the caller identity; the master installs the carried set on the proxy session before executing. Fail-closed choices: a narrowed fetchSchemaTableData request that names no caller is refused with an error result; the other wrapped handlers key the narrowing on the identity they synthesize from the deprecated user/user_ip pair, the same identity their privilege checks already use; a switched forward without a role list is refused by the master; and because an older FE would silently ignore the fields (and a rolling upgrade upgrades the master last), a forward from a narrowed session is refused unless the destination FE reports the same build in its heartbeat — the same gate covers the SHOW PROCESSLIST fan-out and the all-FE information_schema scans, and every FE journals its build so followers can judge it.

Not in this PR, happy to follow up: a per-target GRANT PROXY_PRIV ON 'alice'@'%' TO ... form (needs a new priv table; the global form is MySQL's ''@'' wildcard case) and a no-list SU that behaves like MySQL PROXY if parity is preferred.

Release note

Add SU 'user' WITH ROLES (...) — a session-narrowed identity switch (MySQL-style proxy authentication with mandatory role narrowing) gated by a new global PROXY_PRIV, plus session_is_narrowed() and an authenticated_user audit column.

Check List (For Author)

  • Test

    • Regression test: auth_p0/test_su_session_narrowing.groovy
    • Unit Test: SuUserNarrowingTest, ForwardedSessionNarrowingTest, NarrowedMetadataRpcTest, MysqlConnectProcessorSuTest, CloudAuthTest, ShowProcessListCommandTest, EngineAuthorizationContextTest, FoldConstantTest, AuditLoaderTest, AuditLogHelperTest
    • Manual test: the whole feature has been running in production on a 2-FE / 4-BE cluster behind an MCP gateway for several days (with config stand-ins for the gate that this PR replaces with PROXY_PRIV)
  • Behavior changed:

    • Yes. New statement SU, new privilege PROXY_PRIV, new builtin session_is_narrowed(), new authenticated_user column on __internal_schema.audit_log (added through the existing schema-upgrade path) and field in fe.audit.log. Existing sessions are unaffected unless they run SU. In a narrowed session: EXPORT and CREATE JOB (ON STREAMING / ON SCHEDULE) are refused; the SET PASSWORD / SET PROPERTY self-shortcuts are closed; a locked or password-expired target cannot be assumed; a second SU, and SU to root, admin or the operator role, are refused; user() names the authenticating account while current_user()/session_user() name the effective one. Wire: new optional thrift fields only — on the ids [fix](thrift) Reserve frontend service field ids #63239 reserved for the FrontendService request structs, and on fresh ids (checked free on branch-3.1/4.0/4.1/4.2/master) for TSchemaScanNode 18, TMetaScanNode 4, TMasterOpRequest 37 and TShowUserRequest 2. The FE heartbeat response now journals the responder's build (FrontendHbResponse.version, a Gson field older FEs ignore and newer FEs tolerate missing); otherwise no metadata/editlog format change beyond the new privilege bit (an FE downgraded below this version ignores bit 15). Rolling-upgrade note in the description (narrowed forwards fail closed until the master is upgraded).
  • Does this need documentation?

    • Yes. Will open the doris-website PR once the shape here is agreed (SU statement, PROXY_PRIV, session_is_narrowed(), audit column).

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

🤖 Generated with Claude Code

@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@raghav-reglobe

Copy link
Copy Markdown
Contributor Author

Regression run, for the record: built FE + BE from this branch (aarch64, on top of current master), stood up a single-node cluster, and ran auth_p0/test_su_session_narrowing through the regression framework — 1 suite, 0 failed.

The six refusals the suite asserts on all fired with the expected messages: the global-only GRANT PROXY_PRIV rule, a switch attempted without PROXY_PRIV, a role the target doesn't hold, reads denied before the switch and for the person's direct grant under it, and a second SU in a switched session. The narrowed session sees only the tenant database in SHOW DATABASES / information_schema.tables, session_is_narrowed() reports correctly on both sides, and the person's own login is unaffected.

FE unit tests on current master: 70/70 across the SU classes plus AuthTest / FrontendServiceImplTest; BE compiles and links.

@raghav-reglobe

Copy link
Copy Markdown
Contributor Author

Rebased onto current master (28577df). The session-layer refactors (#67883, #67900) removed the block next to our forwarded-narrowing hook in ConnectProcessor; the rebase keeps only applyForwardedSessionNarrowing there, and ConnectProcessorFlightForwardOutcomeTest went away together with the method it tested.

One new commit on top. The authorization plugin SPI (#66770) resolves every privilege check through AccessTranslation, which refuses a privilege that has no access action at class-initialization time, so PROXY_PRIV needed one. It now maps to a new AccessAction.PROXY. That is a recorded-surface change, so the plugin API major moves to 2.0 in the same commit (surface baseline, pom, and the test literal). 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 what the engine's own predicate (ADMIN_PRIV implies PROXY_PRIV) already says. The access-control behaviour baseline gains the PROXY column wherever ADMIN is answered, since no fixture user holds PROXY_PRIV.

Gate on this head, built from the branch on top of current master: FE build and checkstyle clean; the SU suites plus AccessTranslationTest, AccessControlBehaviorBaselineTest and AuthorizationPluginSurfaceTest all green (91 tests: 88 in fe-core, 3 in the SPI module).

If you would rather not carry an API major bump inside this PR, the alternatives are a dedicated Ranger access type for PROXY or folding it under an existing action. Happy to go either way.

raghav-reglobe and others added 9 commits October 30, 2026 11:58
`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 <noreply@anthropic.com>
Signed-off-by: Raghvendra Singh <raghav@cashify.in>
…/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 <noreply@anthropic.com>
Signed-off-by: Raghvendra Singh <raghav@cashify.in>
… 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 <account>" 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 <noreply@anthropic.com>
Signed-off-by: Raghvendra Singh <raghav@cashify.in>
…ng 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 <noreply@anthropic.com>
Signed-off-by: Raghvendra Singh <raghav@cashify.in>
…witch

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 <noreply@anthropic.com>
Signed-off-by: Raghvendra Singh <raghav@cashify.in>
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 <noreply@anthropic.com>
Signed-off-by: Raghvendra Singh <raghav@cashify.in>
Since the authorization plugin SPI landed (PR apache#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 <raghav@cashify.in>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Since the execution layer fills the forward request through the session's
protocol adapter (PR apache#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 <raghav@cashify.in>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
### What problem does this PR solve?

Issue Number: N/A

Related PR: apache#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 apache#67672.
@morningman

Copy link
Copy Markdown
Contributor

run buildall

@morningman

Copy link
Copy Markdown
Contributor

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review of PR 67672 converged after two bounded rounds. The implementation does carry SU identity/role data through the main plan and statement-forwarding paths, and the Thrift additions are optional and wire-compatible across branch-3.1, 4.0, 4.1, 4.2, and master. The review found nine verified issues:

  • Four P1 authorization/compatibility gaps: TGetTables privilege metadata handlers ignore current_roles; SCH_USER/mysql.user ignores it; workload-group privilege enumeration bypasses the narrowed role filter; and BE-to-FE metadata callbacks can reach older FEs that silently ignore the new field during rolling upgrade.
  • One P1 expression-folding defect: session_is_narrowed() is evaluated in a context-free fold path that must preserve context-dependent functions and can dereference a null context.
  • Four P2 correctness/lifecycle gaps: reset applies target-specific timeout defaults before restoring the authenticated identity; omitted WORKLOAD GROUP keeps the switcher's explicit session setting; REST and forwarded audit paths lose authenticated-user attribution; and target-role ceiling snapshots race Auth's mutable role state without the read lock.

Checkpoint conclusions: the feature's stated narrowing goal is not fully achieved until these paths are fixed; the main data-visibility risk is in metadata and privilege enumeration rather than the core role replacement. Concurrency review found the unsynchronized role snapshot and the executor-thread boundary for ThreadLocal narrowing. Lifecycle review found the reset ordering and forwarded proxy audit identity issues. Protocol review found no field-id collision, required field, or API-versioning omission; the semantic old-FE gap is reported separately. Parallel metadata scanners and handlers were checked, and the existing review threads contain no duplicates. Tests and regression cases were inspected but not executed because this invocation is static-only. No additional user-provided focus was supplied. No unresolved candidates remain after the final sweep, so the review converged.

Review converged after 2 round(s).

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Install current_roles in every TGetTables metadata handler. These scanners now send the SU role subset here, but listTableMetadataNameIds, listTablePrivilegeStatus, listSchemaPrivilegeStatus, and listUserPrivilegeStatus never call withSessionRoleOverride; their Auth checks therefore run with no ConnectContext and resolve the target's full role union. A narrowed session can regain excluded direct SHOW/SELECT/GRANT rows, and the name/ID handler performs its check in a new executor thread where a handler ThreadLocal would not propagate. Apply the override inside each handler/task or pass the role set explicitly before checking privileges.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Carry narrowing through SCH_USER as well. This stores TSchemaScanNode.current_roles, but SchemaUserScanner still builds a TShowUserRequest with only the user identity and showUser is not wrapped. If the target has a direct GRANT/ADMIN role omitted from SU, mysql.user can expose the target's full account list and privilege topology. Add a role field to this request and apply/clear it in the handler (or use the common metadata path).

}
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Honor the narrowed roles when enumerating workload-group privileges. The RPC now carries current_roles here and the handler installs it, but getUserRoleWorkloadGroupPrivs later reads userRoleManager.getRolesByUser directly and emits every target role's workload-group grants. A SU session therefore sees excluded role privileges in information_schema.workload_group_privileges; derive the filter from the narrowed role set for this metadata enumeration while keeping the separate placement-check widening.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Gate BE metadata callbacks during rolling upgrades. This sends the SU role subset to FE RPCs, but old FEs treat the field as reserved/unknown and authorize the target's full role union. SchemaScanNode can call every alive FE for several schema tables, and other metadata paths call the master; unlike FE-to-master SQL forwarding, these calls have no same-build check. Route narrowed metadata only to compatible FEs or fail closed until all peers understand current_roles.

return new VarcharLiteral(res);
}

@Override

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve session_is_narrowed() in context-free folding. visitSessionIsNarrowed ignores foldContextDependentExpressions and dereferences context.cascadesContext; evaluateWithoutContext passes a null context, so expressions such as a metadata predicate can throw or be folded to FALSE for every normal session. Add the same guard as visitCurrentUser/visitSessionUser and only evaluate it when context folding is enabled.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Restore the authenticated identity before resetting session defaults. resetConnection calls resetSessionVariable() while currentUserIdentity is still the SU target, so applyUserSessionVariableDefaults() loads the target's per-user query/insert timeouts; revertSessionNarrowing() then switches back without recalculating them. After COM_RESET_CONNECTION, the original account runs with the target's settings. Revert first or reapply defaults after the revert.

// 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Recompute the target workload group when the clause is omitted. This only sets SessionVariable.workloadGroup for an explicit clause; WorkloadGroupMgr otherwise honors the switcher's already-set session value before looking up the target's default. Thus SET workload_group='switcher_lane'; SU target ... executes as the target in the wrong lane, contrary to the documented omitted-clause behavior.

@@ -194,6 +195,13 @@ protected TMasterOpRequest buildStmtForwardParams() throws AnalysisException {
params.setUserIp(ctx.getRemoteIP());
params.setStmtId(ctx.getStmtId());
params.setCurrentUserIdent(ctx.getCurrentUserIdentity().toThrift());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve the authenticated identity in every audit path. The forwarding additions carry only roles here; the master proxy applies them but leaves authenticatedIdentity null, so forwarded SU statements are audited with the target as both users. The REST AFTER_QUERY builder also omits setAuthenticatedUser, leaving the new column blank. Carry the switcher identity in the forward request and populate the field in all AFTER_QUERY emitters (or provide a safe common fallback).

}

Auth auth = Env.getCurrentEnv().getAuth();
Set<String> targetRoles = auth.getGrantedRoleNames(userIdentity);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Snapshot target grants under Auth's read lock. getGrantedRoleNames iterates UserRoleManager/LDAP state without acquiring Auth.readLock, while SU invokes it outside a lock; a concurrent GRANT/REVOKE or LDAP refresh can race the subset check and accept/reject inconsistently or throw during iteration. Protect the snapshot with the existing Auth lock (and release before any external work if needed).

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 75.22% (170/226) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 28754 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 39e05e62a311620b1b3fc9838ba69c3c2cf82935, data reload: false

------ Round 1 ----------------------------------
============================================
q1	17591	5157	5061	5061
q2	2107	349	257	257
q3	10162	1397	826	826
q4	4685	462	330	330
q5	7430	803	531	531
q6	205	184	153	153
q7	756	800	598	598
q8	9253	1345	1033	1033
q9	5595	4359	4200	4200
q10	6886	1295	1040	1040
q11	453	245	225	225
q12	644	405	272	272
q13	18311	3612	3035	3035
q14	279	274	242	242
q15	q16	728	723	661	661
q17	1229	751	578	578
q18	6957	6238	6183	6183
q19	1307	960	622	622
q20	387	334	225	225
q21	5939	2387	2507	2387
q22	418	357	295	295
Total cold run time: 101322 ms
Total hot run time: 28754 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	5966	5863	6168	5863
q2	742	635	586	586
q3	5672	5767	5345	5345
q4	2020	2163	1597	1597
q5	5734	5813	5616	5616
q6	241	195	150	150
q7	2242	2087	1779	1779
q8	2985	2725	2659	2659
q9	7981	7479	7484	7479
q10	3626	3563	3122	3122
q11	591	437	379	379
q12	671	665	499	499
q13	3307	3650	3050	3050
q14	286	281	267	267
q15	q16	671	698	618	618
q17	7779	7018	6906	6906
q18	12950	12217	12804	12217
q19	936	817	775	775
q20	2130	2125	1919	1919
q21	5544	4551	4682	4551
q22	509	452	420	420
Total cold run time: 72583 ms
Total hot run time: 65797 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 151070 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit 39e05e62a311620b1b3fc9838ba69c3c2cf82935, data reload: false

query5	4335	593	450	450
query6	420	209	189	189
query7	4804	444	228	228
query8	324	170	151	151
query9	8783	3203	3155	3155
query10	469	317	264	264
query11	5948	3852	3578	3578
query12	145	90	87	87
query13	1237	432	333	333
query14	6052	3929	3665	3665
query14_1	3463	3390	3429	3390
query15	212	204	187	187
query16	947	438	410	410
query17	851	683	515	515
query18	2411	408	297	297
query19	199	164	150	150
query20	85	83	79	79
query21	213	127	106	106
query22	12895	13017	12773	12773
query23	13589	12816	12407	12407
query23_1	12456	12461	12592	12461
query24	7355	834	477	477
query24_1	481	474	481	474
query25	555	397	340	340
query26	1257	255	138	138
query27	2789	437	283	283
query28	4631	2006	2023	2006
query29	1516	565	479	479
query30	298	213	179	179
query31	1001	930	815	815
query32	142	94	89	89
query33	527	296	236	236
query34	929	849	494	494
query35	738	742	663	663
query36	839	826	731	731
query37	126	95	81	81
query38	1765	1730	1661	1661
query39	803	788	743	743
query39_1	735	725	745	725
query40	214	114	94	94
query41	69	64	62	62
query42	92	85	87	85
query43	341	360	309	309
query44	1345	683	697	683
query45	180	183	169	169
query46	826	957	575	575
query47	3006	2918	2920	2918
query48	305	322	230	230
query49	585	412	299	299
query50	695	284	229	229
query51	10649	10658	10333	10333
query52	83	82	73	73
query53	193	226	155	155
query54	262	217	199	199
query55	73	70	66	66
query56	240	222	224	222
query57	1677	1622	1566	1566
query58	294	280	271	271
query59	2165	2174	1991	1991
query60	302	259	234	234
query61	161	167	169	167
query62	386	337	296	296
query63	197	174	162	162
query64	2879	1064	860	860
query65	3354	3276	3266	3266
query66	1756	414	298	298
query67	20440	20209	19829	19829
query68	3435	999	614	614
query69	413	293	253	253
query70	826	774	766	766
query71	287	231	213	213
query72	2964	2622	2265	2265
query73	540	526	289	289
query74	4666	4495	4331	4331
query75	2324	2311	1928	1928
query76	2410	1035	620	620
query77	357	370	272	272
query78	9918	9752	9179	9179
query79	991	850	535	535
query80	1243	414	352	352
query81	597	321	270	270
query82	579	134	105	105
query83	313	190	181	181
query84	307	129	103	103
query85	993	421	389	389
query86	402	236	234	234
query87	2011	1933	1800	1800
query88	3802	2823	2776	2776
query89	330	287	243	243
query90	1895	170	164	164
query91	156	144	124	124
query92	100	90	92	90
query93	1023	997	577	577
query94	636	301	248	248
query95	585	394	297	297
query96	679	498	243	243
query97	2447	2428	2321	2321
query98	165	158	145	145
query99	764	772	642	642
Total cold run time: 237072 ms
Total hot run time: 151070 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 24.29 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 39e05e62a311620b1b3fc9838ba69c3c2cf82935, data reload: false

query1	0.00	0.01	0.01
query2	0.09	0.06	0.05
query3	0.26	0.08	0.09
query4	1.61	0.10	0.10
query5	0.23	0.24	0.24
query6	1.18	0.67	0.68
query7	0.04	0.01	0.01
query8	0.06	0.04	0.04
query9	0.40	0.35	0.34
query10	0.57	0.59	0.57
query11	0.20	0.14	0.14
query12	0.18	0.15	0.15
query13	0.50	0.49	0.49
query14	1.03	1.02	1.00
query15	0.64	0.61	0.61
query16	0.35	0.35	0.35
query17	1.16	1.11	1.21
query18	0.22	0.19	0.19
query19	2.09	1.93	2.04
query20	0.02	0.02	0.01
query21	15.49	0.20	0.15
query22	4.83	0.05	0.05
query23	16.28	0.33	0.13
query24	2.74	0.79	0.38
query25	0.12	0.06	0.06
query26	1.60	0.21	0.15
query27	0.05	0.05	0.03
query28	4.25	0.78	0.35
query29	12.50	4.12	3.27
query30	0.28	0.13	0.09
query31	2.78	0.58	0.32
query32	3.22	0.60	0.51
query33	3.12	3.22	3.12
query34	15.26	4.03	3.40
query35	3.35	3.33	3.38
query36	0.71	0.50	0.49
query37	0.10	0.07	0.06
query38	0.05	0.04	0.04
query39	0.03	0.03	0.03
query40	0.17	0.16	0.15
query41	0.09	0.03	0.03
query42	0.04	0.03	0.02
query43	0.05	0.04	0.04
Total cold run time: 97.94 s
Total hot run time: 24.29 s

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 100% (0/0) 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 69.20% (32476/46928)
Line Coverage 53.91% (353076/654989)
Region Coverage 49.43% (290511/587685)
Branch Coverage 50.81% (136376/268395)

raghav-reglobe and others added 21 commits October 31, 2026 20:03
… 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, apache#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 <raghav@cashify.in>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit 8a71d77)
The SU audit column sits after user in AUDIT_SCHEMA; the nonConcurrent
audit suite's expected DESC output still listed the old schema.
…ed 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 <noreply@anthropic.com>
…nts 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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
…ed 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 <noreply@anthropic.com>
… "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 <noreply@anthropic.com>
… 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 <noreply@anthropic.com>
…et'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 <noreply@anthropic.com>
…e 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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
…ask 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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
…arget 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 <noreply@anthropic.com>
…e 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
…tity-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 (apache#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 <noreply@anthropic.com>
…at class level in SchemaScanNode

Round-2 commit 3ba24ea 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 63ba606
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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
…to 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 <noreply@anthropic.com>
…, 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 <noreply@anthropic.com>
@raghav-reglobe

Copy link
Copy Markdown
Contributor Author

Local pipeline review — ✅ PASS

schema: doris-repo-review/v1
status: PASS
pr: apache/doris#67672
commit: 815fb022174b77b2da4a78985f9dfad494978955
base: 46798454678d67c8dc47b048e9a6675ca79669bd
reviewed_at: 2026-11-03T09:58+05:30
reviewer: raghav-reglobe
model: claude-fable-5-1
effort: xhigh
findings: {blocker: 0, major: 0, minor: 0, nit: 2}
rounds: 2
converged: true

Notes for maintainers

  • fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/UserRoleManager.java:131 — Nit: the two-argument getRolesByUser(UserIdentity, boolean) lost its last caller when Auth's wrapper went; worth deleting in a follow-up so no un-narrowed role resolver stays reachable.
  • Reviewed as the unpushed candidate (21 commits ahead of 39e05e6) right before this fast-forward push; both earlier local runs of this PR (heads 39e05e6 and e579984) were inherited — 30 of their 31 code findings are closed at this head, the 31st was the PR body, corrected with this push.
  • Review threads T1–T9 are all addressed at this head; T9's second half (the privilege check listTableMetadataNameIds runs on an executor thread) was closed in this round by carrying the RPC narrowing onto that thread.
  • Design call: EXPORT and CREATE JOB (ON STREAMING / ON SCHEDULE) are refused in a narrowed session rather than persisting the narrowing into the job; persisting it is a possible follow-up.
  • Not verified locally: no build and no test run (no JDK on the review machine); compile correctness and the test expectations were walked by reading.

Reviewed locally with the doris-repo-review pipeline. Repository policy may accept this receipt for the matching commit; it is not a human Apache approval.

@raghav-reglobe

Copy link
Copy Markdown
Contributor Author

@morningman thank you for the rebase and the fix on top. I pushed 21 commits as a fast-forward on your head 39e05e6 (none of your commits are rewritten). Per thread:

  • T1 getGrantedRoleNames reads under the Auth read lock.
  • T2 the forward carries authenticated_user_ident (TMasterOpRequest 37), so the master's audit row names the authenticating account; the REST AFTER_QUERY path sets it the same way.
  • T3 an omitted WITH ROLES clause clears the switcher's own lane instead of inheriting it.
  • T4 the revert runs before resetSessionVariable.
  • T5 foldContextDependentExpressions is guarded for a narrowed session.
  • T6 the mixed-build gate is keyed on the destination FE of every forward (including the KILL and SHOW PROCESSLIST fan-outs and the all-FE information_schema scans), and every FE journals its build in the heartbeat response so followers can judge it.
  • T7 getUserRoleWorkloadGroupPrivs resolves through the narrowed resolver.
  • T8 TShowUserRequest.current_roles (id 2); showUser is wrapped and the BE sends it.
  • T9 every TGetTables-family handler is wrapped, and listTableMetadataNameIds now carries the narrowing onto its executor thread (that was the half still open after the first pass).

Beyond the threads, two rounds of the local pipeline-style review (receipt above) drove the rest of the delta: the compute-group placement widening tests the bit a CLUSTER grant is stored under, the forward refuses a switched request without a role list, and the fail-closed list grew. One design call to flag: EXPORT and CREATE JOB (ON STREAMING / ON SCHEDULE) are refused in a narrowed session rather than persisting the narrowing into the job; if you would rather have the narrowing persisted, that is a follow-up PR I am happy to do. The PR body is updated accordingly (fresh vs reserved thrift ids, the MySQL-protocol-only revert, the refusal list).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] PROXY_PRIV + SU <user> WITH ROLES (...): MySQL-style proxy authentication with mandatory role narrowing

3 participants