Skip to content

Provide token storage using the salt.cache interface - #68039

Merged
dwoz merged 1 commit into
saltstack:masterfrom
mattp-:cachetoken
Mar 17, 2026
Merged

dwoz merged 1 commit into
saltstack:masterfrom
mattp-:cachetoken

Conversation

@mattp-

@mattp- mattp- commented May 27, 2025

Copy link
Copy Markdown
Contributor

What does this PR do?

Rather than implement custom drivers for tokens, the existing cache drivers can be leveraged for token management. This also moves token expiry responsibility to the token(cache) implementation with a naive fallback.

Implicitly the default token backend will move to either what the global 'cache' is set to, or, if overridden, the eauth_tokens.cache_driver opt. This means on upgrade, tokens will be invalidated. The only other token driver is 'rediscluster', which I've added a deprecation for, since cache.redis_cache provides the same functionality.

NOTE: this PR depends on #68030 being merged; once done I can rebase. 1e505cb is the only relevant commit for this PR.

What issues does this PR fix or reference?

Fixes

Previous Behavior

Remove this section if not relevant

New Behavior

Remove this section if not relevant

Merge requirements satisfied?

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

Commits signed with GPG?

Yes

@mattp-
mattp- requested a review from a team as a code owner May 27, 2025 17:36
@mattp- mattp- changed the title WIP: Provide token storage using the salt.cache interface Provide token storage using the salt.cache interface May 28, 2025
@whytewolf whytewolf added the test:full Run the full test suite label May 28, 2025
@twangboy twangboy added this to the Argon v3008.0 milestone May 28, 2025
@mattp-

mattp- commented Jun 6, 2025

Copy link
Copy Markdown
Contributor Author

@bdrx312 thanks for the review! @twangboy should be good now

@twangboy

twangboy commented Jun 6, 2025

Copy link
Copy Markdown
Contributor

Looks like the rest of the failures are failing on master. Once those are fixed, we can rebase.

twangboy
twangboy previously approved these changes Jun 6, 2025
twangboy
twangboy previously approved these changes Jun 11, 2025
twangboy
twangboy previously approved these changes Jun 17, 2025
twangboy
twangboy previously approved these changes Oct 1, 2025
Comment thread salt/cache/__init__.py
elif len(record) == 3:
(created_at, expires, data) = record

if (created_at + (expires or self.expire)) >= now:

@bdrx312 bdrx312 Nov 10, 2025 •

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.

If len(record) is 1, 2 or > 3 then created_at will not be defined and the same problem with data below on line 385.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mmm practically speaking i dont think that's possible to happen, but ill update to be more defensive

twangboy
twangboy previously approved these changes Nov 24, 2025
Rather than implement custom drivers for tokens, the existing cache
drivers can be leveraged for token management. This also moves token
expiry responsibility to the token(cache) implementation with a naive
fallback.

Implicitly the default token backend will move to either what the global
'cache' is set to, or, if overridden, the eauth_tokens.cache_driver
opt. This means on upgrade, tokens will be invalidated. The only other
token driver is 'rediscluster', which I've added a deprecation for,
since cache.redis_cache provides the same functionality.
@haijerc

haijerc commented Jun 4, 2026

Copy link
Copy Markdown

Is there a possibility that this update, gets tokens expired too early?

I upgraded from 3007, since then tokens seems to be valid for around <10 seconds. The log shows:
Token {{ token here }} could not be loaded (TokenExpiredError('')); removing it from the store.

I am running stock configuration 3008.0 with only rest_cherrypy enabled. The the token expiry timestamp when the token was created looks fine (10-12h).

@mattp-

mattp- commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Is there a possibility that this update, gets tokens expired too early?

I upgraded from 3007, since then tokens seems to be valid for around <10 seconds. The log shows: Token {{ token here }} could not be loaded (TokenExpiredError('')); removing it from the store.

I am running stock configuration 3008.0 with only rest_cherrypy enabled. The the token expiry timestamp when the token was created looks fine (10-12h).

it's possible.. but many things have changed in 3008. are you able to provide debug level logs, and/or a simple repro case?

@haijerc

haijerc commented Jun 4, 2026 •

Copy link
Copy Markdown

Thank you for the fast response, I have narrowed down the problem.
When I set loop_interval: 9000, the token does not expire anymore. The conclusion is that the token gets expired by one of the jobs executed in this loop. (Purging cache)

Jun 04 15:28:26 salt salt-master[570177]: [DEBUG ] Performing fileserver updates for items with an update interval of 60
Jun 04 15:28:26 salt salt-master[570177]: [DEBUG ] Updating roots fileserver cache
Jun 04 15:28:26 salt salt-master[570177]: [DEBUG ] Completed fileserver updates for items with an update interval of 60, waiting 60 seconds
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Including configuration from '/etc/salt/master.d/00-defaults.conf'
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master.d/00-defaults.conf
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Including configuration from '/etc/salt/master.d/05-api.conf'
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master.d/05-api.conf
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Including configuration from '/etc/salt/master.d/99-debug.conf'
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master.d/99-debug.conf
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Using cached minion ID from /etc/salt/minion_id: (minion id hidden)
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Grains refresh requested. Refreshing grains.
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Including configuration from '/etc/salt/master.d/00-defaults.conf'
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master.d/00-defaults.conf
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Including configuration from '/etc/salt/master.d/05-api.conf'
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master.d/05-api.conf
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Including configuration from '/etc/salt/master.d/99-debug.conf'
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Reading configuration from /etc/salt/master.d/99-debug.conf
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'core' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'disks' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'extra' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'lvm' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'mdadm' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'minion_process' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'opts' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'package' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'resources' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'truststore' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'local_cache' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] LazyLoaded local_cache.clean_old_jobs
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'localfs' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'localfs_key' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'mmap_cache' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] The functions from module 'mmap_key' are being loaded by dir() on the loaded module
Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Could not LazyLoad localfs.clean_expired: 'localfs.clean_expired' is not available.

Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] This salt-master instance has accepted 36 minion keys.
Jun 04 15:28:34 salt salt-master[570181]: [DEBUG ] Cache file "/var/cache/salt/master/tokens/31a5ce71bca9136868538b8e50004fafd0eade474cacfd65723a00b3eefefa55.p" does not exist
Jun 04 15:28:34 salt salt-master[570181]: [DEBUG ] Cache file "/var/cache/salt/master/tokens.p" does not exist
Jun 04 15:28:34 salt salt-master[570181]: [WARNING ] Token '31a5ce71bca9136868538b8e50004fafd0eade474cacfd65723a00b3eefefa55' could not be loaded (TokenExpiredError('')); removing it from the store.
Jun 04 15:28:34 salt salt-master[570181]: [DEBUG ] Token load traceback:
Jun 04 15:28:34 salt salt-master[570181]: Traceback (most recent call last):
Jun 04 15:28:34 salt salt-master[570181]: File "/opt/saltstack/salt/lib/python3.14/site-packages/salt/auth/init.py", line 343, in get_tok
Jun 04 15:28:34 salt salt-master[570181]: raise salt.exceptions.TokenExpiredError
Jun 04 15:28:34 salt salt-master[570181]: salt.exceptions.TokenExpiredError
Jun 04 15:28:34 salt salt-master[570181]: [WARNING ] Authentication failure of type "token" occurred.

Could it be this line:

Jun 04 15:28:29 salt salt-master[570167]: [DEBUG ] Could not LazyLoad localfs.clean_expired: 'localfs.clean_expired' is not available.

Or is it just that the general localfs cache gets dropped?

With maintenance_interval: 9999 and loop_interval: 60 the tokens still get expired, so the problem seems to be tied to the loop_interval.

@haijerc

haijerc commented Jun 4, 2026

Copy link
Copy Markdown

By the way, the expires inside the {{token}}.p file looks fine:

{'_expires': 3561224803,
'data': {'eauth': 'pam',
'expire': 1780634001.5236907,
'groups': ['user1',
'audio',
'cdrom',
'dip',
'floppy',
'netdev',
'plugdev',
'users',
'video'],
'name': 'user1',
'start': 1780590801.5236905,
'token': 'cfee9db1c1d8c0dd070f4e9be4bd676eae628d4302956855eb1d62513e052760'}}

@mattp-

mattp- commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@haijerc can you create a new issue tagged on 3008 and feel free to link this PR ? I don't think anyone is seeing your comments but me

@mattp-

mattp- commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

also delete that token on your infra if its valid :)

@mattp-

mattp- commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

if you don't mind sticking a log.debug below line 340 in salt/auth/init.py on your master

tdata = self.cache.fetch("tokens", tok)
log.debug(tdata)

and sharing it here (or in new issue) , it should show whats happening / why it's not being handled as expected

@haijerc

haijerc commented Jun 4, 2026

Copy link
Copy Markdown

@mattp I have tracked down the issue, yay! I will open a pull request with a code fix and see if it gets merged. I will mention this aswell.

@haijerc

haijerc commented Jun 4, 2026

Copy link
Copy Markdown

My suggested fix is at LoadAuth::clean_expired_tokens

Instead of
self.cache.clean_expired("tokens")

Use
for token in self.list_tokens(): token_data = self.get_tok(token) if ( "expire" not in token_data or token_data.get("expire", 0) < time.time() ): self.rm_token(token)

for cleaning the tokens, this respects the expiry value inside the cachefile.

Or would you prefer implementing localfs.clean_expired? This is eventually the better fix, but leads to duplicate expiry data locations or more changes to get it nicely done.

This branch was previously deployed

1 inactive deployment
ci — a1371ce7 Deployed Mar 17, 2026 by dwoz via Build Onedir Packages / RPM (arm64) #20679
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants