Skip to content

Add cache-backed CherryPy session store to rest_cherrypy for shared salt-api sessions#69245

Open
ggiesen wants to merge 2 commits into
saltstack:masterfrom
ggiesen:add-cache-backed-salt-api-sessions
Open

Add cache-backed CherryPy session store to rest_cherrypy for shared salt-api sessions#69245
ggiesen wants to merge 2 commits into
saltstack:masterfrom
ggiesen:add-cache-backed-salt-api-sessions

Conversation

@ggiesen
Copy link
Copy Markdown
Contributor

@ggiesen ggiesen commented May 27, 2026

What does this PR do?

Adds a session_store: cache option to the rest_cherrypy netapi. When set, CherryPy sessions are stored in the master's configured salt.cache instead of CherryPy's default per-worker RamSession, so salt-api sessions are shared across multiple salt-api workers and master-cluster members.

The driver follows the cache setting (localfs by default), so a master cluster with a shared cachedir needs nothing extra; deployments without shared storage can point cache at a networked driver such as redis or etcd3. The default remains RamSession, so existing deployments are unaffected.

What issues does this PR fix or reference?

References #68999, #38139

Previous Behavior

The X-Auth-Token doubles as the CherryPy session id, and the auth gate checks the CherryPy session in addition to the Salt token. With the default in-RAM session store the session lives only on the worker that handled /login, so a client whose later requests reach a different worker is rejected with a 401, even though the Salt token is valid and already shared via the master token cache. This breaks salt-api behind a load balancer and on multi-member master clusters unless sticky sessions are configured.

New Behavior

With session_store: cache, the session is written to Salt's shared cache, so any worker can resolve it and authentication succeeds regardless of which worker handles the request.

The /events and /websocket out-of-band token lookup (which previously read CherryPy's RAM session dict directly) now goes through a backend-agnostic helper, so those endpoints work with either backend.

This was validated against a running master with multiple salt-api workers sharing a cachedir: a token issued by one worker authenticates against another only with session_store: cache; the default RamSession rejects it with a 401. New unit tests cover the backend lifecycle, cross-instance resolution, expiry, cleanup, the lookup helper (both backends), and the config wiring.

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

  • Docs
  • Changelog
  • Tests written/updated

Commits signed with GPG?

No

The X-Auth-Token doubles as the CherryPy session id, and the auth gate
checks the CherryPy session in addition to the Salt token. With CherryPy's
default in-RAM session store the session lives only on the worker that
handled /login, so a client whose later requests reach a different worker
gets a 401 even though the Salt token is valid and already shared via the
master token cache. This breaks salt-api behind a load balancer and on
multi-member master clusters unless sticky sessions are configured.

Add a SaltCacheSession backend (opt-in via "session_store: cache") that
stores sessions in the master's configured salt.cache instead, so any
worker can resolve them. The driver follows the "cache" setting (localfs
by default), so a master cluster with a shared cachedir needs nothing
extra; deployments without shared storage can point cache at redis or
etcd3.

Route the /events and /websocket out-of-band token lookup through a
backend-agnostic helper, since they previously assumed the RAM session's
internal dict.

Adds unit tests for the backend lifecycle, cross-instance resolution,
expiry, cleanup, the lookup helper, and the config wiring.
@ggiesen ggiesen requested a review from a team as a code owner May 27, 2026 19:46
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.

1 participant