-
Notifications
You must be signed in to change notification settings - Fork 0
fix: re-read settings when CACHEKIT_MASTER_KEY appears + correct missing-key env var name #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Encryption-config resolution correctness. | ||
|
|
||
| #195: get_settings() built the config once and cached it process-wide. If the first build happened | ||
| before CACHEKIT_MASTER_KEY entered the environment (e.g. an import-time @cache decorator before the | ||
| app loads secrets), it froze master_key=None forever — encryption then silently never activated. | ||
| The singleton must re-read once the key appears. | ||
|
|
||
| #194: the missing-key error pointed users at REDIS_CACHE_MASTER_KEY, which is never read; the only | ||
| honored variable is CACHEKIT_MASTER_KEY. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from cachekit.config.singleton import get_settings, reset_settings | ||
| from cachekit.serializers.auto_serializer import AutoSerializer | ||
| from cachekit.serializers.encryption_wrapper import EncryptionError, EncryptionWrapper | ||
|
|
||
| _HEX_KEY = "a" * 64 # 32-byte hex master key | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestKeylessSingletonSelfHeals: | ||
| """#195: a keyless singleton must not freeze permanently once the key is set.""" | ||
|
|
||
| def test_get_settings_rereads_when_master_key_appears(self, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.delenv("CACHEKIT_MASTER_KEY", raising=False) | ||
| reset_settings() | ||
| try: | ||
| assert get_settings().master_key is None # first build: env not yet set -> keyless | ||
| monkeypatch.setenv("CACHEKIT_MASTER_KEY", _HEX_KEY) | ||
| assert get_settings().master_key is not None # must self-heal (the bug froze this at None) | ||
| finally: | ||
| reset_settings() | ||
|
|
||
| def test_get_settings_stable_once_key_present(self, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| # Guard: self-heal must not re-read on every call once the key is loaded. | ||
| monkeypatch.setenv("CACHEKIT_MASTER_KEY", _HEX_KEY) | ||
| reset_settings() | ||
| try: | ||
| assert get_settings() is get_settings() | ||
| finally: | ||
| reset_settings() | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestMissingKeyErrorNamesCorrectEnvVar: | ||
| """#194: the missing-key error must name the env var that is actually honored.""" | ||
|
|
||
| def test_error_names_cachekit_master_key(self, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.delenv("CACHEKIT_MASTER_KEY", raising=False) | ||
| reset_settings() | ||
| try: | ||
| with pytest.raises(EncryptionError) as exc: | ||
| EncryptionWrapper(serializer=AutoSerializer(), master_key=None) | ||
| msg = str(exc.value) | ||
| assert "CACHEKIT_MASTER_KEY" in msg | ||
| assert "REDIS_CACHE_MASTER_KEY" not in msg | ||
| finally: | ||
| reset_settings() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.