Skip to content

ak-agent: fix token exchange cache key and cached expiry - #1677

Merged
BeryJu merged 1 commit into
goauthentik:mainfrom
synapdeck:typedrat/agent-token-cache-fix
Oct 12, 2026
Merged

BeryJu merged 1 commit into
goauthentik:mainfrom
synapdeck:typedrat/agent-token-cache-fix

Conversation

@typedrat

Copy link
Copy Markdown
Contributor

Two bugs in the agent's cached_token_exchange.

The cache was read under a key that includes the requested scopes and a hash of the actor token, but always written under ["token-cache", audience]. So scoped and actor exchanges never got a cache hit, and a plain request for an audience could be handed a token that was minted for another caller's scopes or actor. Reads and writes now go through one exchange_cache_key helper.

A cache hit also returned the token's original expires_in instead of the time it has left. Anything that computes now + expires_in overstated the expiry; the kubectl ExecCredential from ak auth kubectl does this, so kubectl could keep using a token past its real expiry. Hits now return the remaining seconds.

Unit tests cover the cache key and the remaining-time calculation.

The exchange cache was written under ["token-cache", audience] but read
under a key that also included scopes and an actor-token hash, so scoped
or actor exchanges never hit the cache, and a plain request could be
served a token minted with scopes or an actor. Read and write now share
one key.

Cache hits also returned the token's original expires_in rather than
the time left, so callers computing now + expires_in (e.g. the kubectl
ExecCredential) overstated expiry.
@typedrat
typedrat marked this pull request as ready for review October 12, 2026 18:52
@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 46.74%. Comparing base (38e95d3) to head (113976a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1677      +/-   ##
==========================================
- Coverage   49.72%   46.74%   -2.99%     
==========================================
  Files         209      205       -4     
  Lines       21683    16983    -4700     
  Branches    16516    16252     -264     
==========================================
- Hits        10782     7938    -2844     
+ Misses      10519     8613    -1906     
- Partials      382      432      +50     
Flag Coverage Δ
rs-e2e-24.04 ?
rs-e2e-25.10 ?
rs-e2e-26.04 ?
rs-macos-26-ak-agent 41.69% <100.00%> (+2.11%) ⬆️
rs-macos-26-ak-agent-desktop 0.39% <0.00%> (-0.01%) ⬇️
rs-macos-26-ak-api-cli 100.00% <ø> (ø)
rs-macos-26-ak-api-cli-gen 66.71% <ø> (ø)
rs-macos-26-ak-browser-support 23.68% <ø> (ø)
rs-macos-26-ak-cli 21.19% <ø> (ø)
rs-macos-26-ak-flow-executor 13.07% <ø> (ø)
rs-macos-26-ak-meta 0.00% <ø> (ø)
rs-macos-26-ak-meta-macros 0.00% <ø> (ø)
rs-macos-26-ak-platform 67.17% <ø> (-0.05%) ⬇️
rs-macos-26-ak-platform-authz 0.00% <ø> (ø)
rs-macos-26-ak-platform-facts 83.44% <ø> (ø)
rs-macos-26-ak-platform-keyring 65.64% <ø> (ø)
rs-macos-26-ak-sysd 38.08% <ø> (ø)
rs-ubuntu-26.04-ak-agent 40.05% <100.00%> (+0.47%) ⬆️
rs-ubuntu-26.04-ak-agent-desktop 0.40% <0.00%> (-0.01%) ⬇️
rs-ubuntu-26.04-ak-api-cli 100.00% <ø> (ø)
rs-ubuntu-26.04-ak-api-cli-gen 66.71% <ø> (ø)
rs-ubuntu-26.04-ak-browser-support 23.68% <ø> (ø)
rs-ubuntu-26.04-ak-cli 21.19% <ø> (ø)
rs-ubuntu-26.04-ak-flow-executor 13.07% <ø> (ø)
rs-ubuntu-26.04-ak-meta 0.00% <ø> (ø)
rs-ubuntu-26.04-ak-meta-macros 0.00% <ø> (ø)
rs-ubuntu-26.04-ak-nss 77.37% <ø> (ø)
rs-ubuntu-26.04-ak-pam 0.00% <ø> (ø)
rs-ubuntu-26.04-ak-platform 66.63% <ø> (ø)
rs-ubuntu-26.04-ak-platform-authz 0.00% <ø> (ø)
rs-ubuntu-26.04-ak-platform-facts 82.64% <ø> (ø)
rs-ubuntu-26.04-ak-platform-keyring 53.08% <ø> (ø)
rs-ubuntu-26.04-ak-sysd 36.73% <ø> (ø)
rs-ubuntu-26.04-arm-ak-agent 40.05% <100.00%> (+0.47%) ⬆️
rs-ubuntu-26.04-arm-ak-agent-desktop 0.40% <0.00%> (-0.01%) ⬇️
rs-ubuntu-26.04-arm-ak-api-cli 100.00% <ø> (ø)
rs-ubuntu-26.04-arm-ak-api-cli-gen 66.71% <ø> (ø)
rs-ubuntu-26.04-arm-ak-browser-support 23.68% <ø> (ø)
rs-ubuntu-26.04-arm-ak-cli 21.19% <ø> (ø)
rs-ubuntu-26.04-arm-ak-flow-executor 13.07% <ø> (ø)
rs-ubuntu-26.04-arm-ak-meta 0.00% <ø> (ø)
rs-ubuntu-26.04-arm-ak-meta-macros 0.00% <ø> (ø)
rs-ubuntu-26.04-arm-ak-nss 77.37% <ø> (ø)
rs-ubuntu-26.04-arm-ak-pam 0.00% <ø> (ø)
rs-ubuntu-26.04-arm-ak-platform 66.63% <ø> (-0.05%) ⬇️
rs-ubuntu-26.04-arm-ak-platform-authz 0.00% <ø> (ø)
rs-ubuntu-26.04-arm-ak-platform-facts 82.64% <ø> (ø)
rs-ubuntu-26.04-arm-ak-platform-keyring 53.08% <ø> (ø)
rs-ubuntu-26.04-arm-ak-sysd 36.73% <ø> (ø)
rs-windows-2025-ak-agent 38.39% <100.00%> (+2.30%) ⬆️
rs-windows-2025-ak-agent-desktop 0.38% <0.00%> (-0.01%) ⬇️
rs-windows-2025-ak-api-cli 100.00% <ø> (ø)
rs-windows-2025-ak-api-cli-gen 66.71% <ø> (ø)
rs-windows-2025-ak-browser-support 23.68% <ø> (ø)
rs-windows-2025-ak-cli ?
rs-windows-2025-ak-flow-executor 13.07% <ø> (ø)
rs-windows-2025-ak-meta 0.00% <ø> (ø)
rs-windows-2025-ak-meta-macros 0.00% <ø> (ø)
rs-windows-2025-ak-platform 63.19% <ø> (-0.05%) ⬇️
rs-windows-2025-ak-platform-authz 0.00% <ø> (ø)
rs-windows-2025-ak-platform-facts 78.89% <ø> (ø)
rs-windows-2025-ak-platform-keyring 76.25% <ø> (ø)
rs-windows-2025-ak-sysd 35.64% <ø> (ø)
rs-windows-2025-ee/wcp 44.39% <ø> (ø)
swift 3.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@github-project-automation github-project-automation Bot moved this from Todo to In Progress in authentik Core Oct 12, 2026
@BeryJu
BeryJu merged commit 0dc9927 into goauthentik:main Oct 12, 2026
112 of 117 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in authentik Core Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants