Skip to content

fix(frontend): enforce fresh authentication snapshots across CNs - #27763

Merged
XuPeng-SH merged 19 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-cross-cn-auth-freshness
Aug 28, 2026
Merged

XuPeng-SH merged 19 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-cross-cn-auth-freshness

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

What changed

This PR makes normal-user authentication establish one fresh catalog snapshot
before reading account, user, password, lock, role, grant, system-variable, or
login-database state.

  • Capture the serving CN's HLC uncertainty upper bound and install the smallest
    timestamp that dominates every HLC value at that bound.
  • Wait for the local logtail through the effective monotonic session minimum
    before constructing the authentication executor or entering user
    transaction admission. TxnClient.New then rechecks the same minimum.
  • Preserve a larger caller minimum in both transaction freshness modes.
  • Keep max-clock-offset as the ordering bound even when active local clock
    monitoring is disabled, in process and embedded launchers.
  • Make the handshake request context the single timeout owner of authentication
    freshness waiting and transaction creation. Ordinary transactions retain
    CreateTxnOpTimeout.
  • Reject authentication-enabled CN configurations that cannot cover the
    pairwise clock geometry: connectTimeout > 2 * max-clock-offset + 1ns, with
    checked arithmetic. skipCheckUser=true bypasses only this unreachable
    authentication-specific budget check; general clock validation still runs.
  • Revalidate public embedded Adjust/pre-start mutations before creating
    resources. The operator's immutable service type owns validation, and a
    callback cannot relabel a CN as a TN to bypass the authentication budget.
  • Let the built-in timestamp waiter observe transaction-client closure directly,
    avoiding a derived context and context.AfterFunc allocation per login while
    retaining the compatibility fallback for legacy waiter implementations.

The complete design and validation map are in
docs/design/authentication_catalog_freshness.md.

Root cause

With sacrificing freshness enabled, a new session has no prior commit timestamp,
so authentication could start from the latest logtail already applied by its
local CN. A security mutation could already have committed and returned on
another CN while this CN still read the previous catalog generation.

The observed failure was REVOKE followed by an immediate GRANT of the stored
default role. A new connection routed to a lagging CN validated the old revoked
generation, fell back to public, and denied a table read that the regranted
role permits. The same stale-snapshot shape applies to account state,
user/password and lock state, explicit role grants, and login database
resolution, so the repair belongs at the authentication snapshot boundary.

Correctness and unhappy paths

  • The login linearization point is the HLC uncertainty-bound capture under the
    existing cluster clock contract.
  • The pre-admission wait must report a watermark at or beyond the effective
    session minimum; missing runtime/clock/transaction client, negative offset,
    timestamp overflow, wait failure, cancellation, client close, and regressing
    watermark all fail closed.
  • A failed pre-admission wait creates no transaction operator. The timestamp
    waiter owns and removes its entry on success, cancellation, notification, or
    close. Later transaction-creation failure retains the existing abort owner.
  • Bootstrap-only special users and skipCheckUser retain their existing
    catalog-free paths.
  • Embedded post-parse mutations are revalidated before resource creation;
    invalid budgets and service-identity mismatches leave no cleanup obligation.
  • One waiter exists per in-flight normal login, bounded by the handshake
    deadline and connection count. No new goroutine, RPC, persisted state,
    collection, or rolling-upgrade dependency is introduced.

This repairs new-login catalog freshness. Existing authenticated-session role
revocation remains the separate lifecycle handled by PR #27737.

Performance

  • The default ordinary-SQL transaction hot path is unchanged.
  • The unavoidable uncertainty/logtail wait occurs before transaction admission,
    so it no longer consumes a max-active user-transaction slot. Connection
    pooling amortizes this connection-establishment cost for TP workloads.
  • A normal login adds one local HLC read and reuses the existing timestamp
    waiter; it adds no cross-CN RPC or goroutine.
  • The built-in waiter close fast path removes one derived context and
    context.AfterFunc allocation from each authentication wait.
  • The non-default freshness-preserving branch adds one timestamp comparison and
    no allocation, lock, or I/O.
  • Embedded revalidation is startup-only and occurs before resource allocation;
    it has no query-path cost.

Validation

Candidate head: 9a646c2a33 on main 745d8364c5 (Go 1.26.4,
-mod=readonly, repository CGo/native libraries).

  • Focused config/authentication/waiter tests passed, including skipCheckUser,
    monotonic existing minimum, missing dependencies, downstream failure,
    watermark regression, executor ordering, cancellation, client close,
    post-parse invalid budget, and mutable-service-type bypass.
  • Full normal suites passed for pkg/config, cmd/mo-service, pkg/embed,
    pkg/frontend, pkg/txn/client, and pkg/txn/clock.
  • Full race suites passed for pkg/embed, pkg/frontend, and pkg/txn/client.
  • The cancellation/close/snapshot barrier tests passed 100 repetitions under
    the race detector; they use no sleeps, polling, retries, or weakened
    assertions.
  • go vet passed for every changed Go package; formatting, module-file, and
    diff checks passed.
  • Exact-head make build passed with CommitID=9a646c2a33.
  • The existing public revoke/regrant semantics were exercised through Proxy on
    one LogService, one TN, and two CNs. New sessions immediately observed revoke
    fallback and regrant on alternating CN connections; a deliberately retained
    old session kept its old role, matching the separate fix(frontend): invalidate revoked active roles in existing sessions #27737 scope.

Incident evidence:
https://github.com/matrixorigin/matrixone/actions/runs/33067372509/job/98502820591?pr=27758

Related: #27717, #27737, #27758

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/L Denotes a PR that changes [500,999] lines labels Aug 27, 2026
@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Follow-up pushed in 7056b96921 after a full design/unhappy-path pass:

  • skipCheckUser=true no longer fails an authentication deadline check for a path it bypasses; negative/general clock validation still fails closed.
  • The catalog-freshness wait now completes before the background executor and user-transaction admission are created, so the uncertainty interval cannot consume a max-active slot. The transaction rechecks the same monotonic minimum before catalog reads.
  • Missing dependencies, waiter failure, a watermark below the fence, request cancellation, and txn-client close are covered explicitly. The built-in close-aware waiter also avoids the derived-context/AfterFunc allocation, with the legacy fallback retained.

Changed-package normal tests, full frontend/txn-client race suites, 100x deterministic cancellation/close race tests, changed-package go vet, a real Proxy + 2-CN revoke/regrant exercise, and exact-head make build all passed. The PR description and design document now reflect the revised ownership and performance boundary. Ready for re-review; I did not wait for remote CI.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Completed repeated deep-review/fix cycles on head 9a646c2 after merging current main 745d836.

Round 1 found a public embedded-startup validation bypass: ServiceOperator.Adjust runs after file validation, so an invalid authentication clock budget could reach Start. Commit 0cb6968 revalidates before resource creation and adds a deterministic no-cleanup regression test.

Round 2 found the same bypass through identity mutation: Adjust could change cfg.ServiceType to TN while the operator still starts a CN from its immutable serviceType. Commit c249f72 rejects the identity mismatch and validates the budget against the immutable runtime type.

The subsequent full-suite feedback loop found two compatibility regressions in test/partial-config callers. Commits 3c60f8d and 9a646c2 preserve the Sirius gate fixture identity and restore non-CN/partial-config defaulting without reopening the runtime bypass.

Final audit result: no remaining blocking correctness, lifecycle, performance, or unhappy-path finding. Q1 ownership closes on every prewait/create failure; Q2 request cancel, client close, and waiter close all terminate; Q3 retained waiter state is bounded by concurrent handshakes and removed on every terminal path. Default ordinary SQL remains unchanged; startup checks are off-path, and the only non-default txn hot-path addition is one timestamp comparison.

Exact-head evidence: full normal suites for pkg/config, cmd/mo-service, pkg/embed, pkg/frontend, pkg/txn/client, pkg/txn/clock; full race suites for pkg/embed, pkg/frontend, pkg/txn/client; focused cancellation/close/snapshot race tests x100; vet for all changed packages; diff/module checks; and make build with CommitID=9a646c2a33 all passed. PR body and design evidence are synchronized. Ready for re-review; remote CI was not awaited.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Repeated deep-review pass completed on exact head 9a646c2a3336d64341182edd14ea638ea1404b4c.

  • Design/correctness: re-traced the security-catalog commit timestamp from TN HLC through the minimum applied logtail watermark and applied.Next() snapshot. The authentication fence remains strictly beyond the pairwise clock bound, is waited before txn admission, and the one background transaction inherits the same monotonic session minimum for every catalog read.
  • Unhappy paths (Q1-Q3): request cancellation, txn-client close, waiter close, pre-admission failure, and post-admission snapshot failure all terminate and release their single owner. The pooled waiter entry cannot be reused while notifier/canceller/closer still references it; concurrent notify/cancel/close tests remain race-clean. Missing runtime/clock/client, timestamp overflow, waiter error, and an insufficient watermark fail closed.
  • Performance: ordinary SQL and the default sacrificing-freshness snapshot path gain no new allocation. Authentication pays one HLC read plus the required logtail fence; its second txn-client check is normally an atomic fast path. Freshness-preserving transactions add only one timestamp comparison. The built-in waiter avoids the legacy derived-context/AfterFunc allocation.
  • Compatibility/startup: file, programmatic-default, and post-Adjust embedded entry points all validate the effective CN budget before resources; service-type mutation cannot bypass the immutable operator identity. Special-user/skipCheckUser bypasses remain isolated.

Fresh evidence this pass: normal tests passed for pkg/config, pkg/txn/clock, the changed pkg/embed/pkg/frontend paths, and the affected pkg/txn/client paths; focused txn-client and frontend lifecycle suites passed under -race. All exact-head remote UT/SCA/coverage/build/multi-CN checks are successful. Current main is four commits ahead, but has no changed-file intersection with this PR and GitHub reports the head mergeable.

Result: no new blocking correctness, lifecycle, performance, compatibility, or unhappy-path finding. The remaining CHANGES_REQUESTED state is the old review on eddebbb8f2; its two findings are addressed by later commits and the corrected PR body.

This branch had an error being deployed

1 failed deployment
ci — 24e00b34 Deployed Aug 28, 2026 by XuPeng-SH via Matrixone Utils CI / Coverage #48886
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants