-
Notifications
You must be signed in to change notification settings - Fork 1
✨(configuration) add configuration Value to support file path in environment #15
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
lunika
merged 1 commit into
suitenumerique:main
from
soyouzpanda:feature/configuration-environment-secrets
May 20, 2025
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Custom value classes for django-configurations.""" | ||
|
||
import os | ||
|
||
from configurations import values | ||
|
||
|
||
class SecretFileValue(values.Value): | ||
""" | ||
Class used to interpret value from environment variables with reading file support. | ||
|
||
The value set is either (in order of priority): | ||
* The content of the file referenced by the environment variable | ||
`{name}_{file_suffix}` if set. | ||
* The value of the environment variable `{name}` if set. | ||
* The default value | ||
""" | ||
|
||
file_suffix = "FILE" | ||
|
||
def __init__(self, *args, **kwargs): | ||
"""Initialize the value.""" | ||
super().__init__(*args, **kwargs) | ||
if "file_suffix" in kwargs: | ||
self.file_suffix = kwargs["file_suffix"] | ||
|
||
def setup(self, name): | ||
"""Get the value from environment variables.""" | ||
value = self.default | ||
if self.environ: | ||
full_environ_name = self.full_environ_name(name) | ||
full_environ_name_file = f"{full_environ_name}_{self.file_suffix}" | ||
if full_environ_name_file in os.environ: | ||
filename = os.environ[full_environ_name_file] | ||
if not os.path.exists(filename): | ||
raise ValueError(f"Path {filename!r} does not exist.") | ||
try: | ||
with open(filename) as file: | ||
value = self.to_python(file.read().removesuffix("\n")) | ||
except (OSError, PermissionError) as err: | ||
raise ValueError(f"Path {filename!r} cannot be read: {err!r}") from err | ||
elif full_environ_name in os.environ: | ||
value = self.to_python(os.environ[full_environ_name]) | ||
elif self.environ_required: | ||
raise ValueError( | ||
f"Value {name!r} is required to be set as the " | ||
f"environment variable {full_environ_name_file!r} or {full_environ_name!r}" | ||
) | ||
self.value = value | ||
return value |
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 @@ | ||
"""Test configuration.""" |
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 @@ | ||
TestSecretInFile |
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,79 @@ | ||
"""Tests for SecretFileValue.""" | ||
|
||
import os | ||
|
||
import pytest | ||
|
||
from lasuite.configuration.values import SecretFileValue | ||
|
||
FILE_SECRET_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_secret") | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _mock_clear_env(monkeypatch): | ||
"""Reset environment variables.""" | ||
monkeypatch.delenv("DJANGO_TEST_SECRET_KEY", raising=False) | ||
monkeypatch.delenv("DJANGO_TEST_SECRET_KEY_FILE", raising=False) | ||
monkeypatch.delenv("DJANGO_TEST_SECRET_KEY_PATH", raising=False) | ||
|
||
|
||
@pytest.fixture | ||
def _mock_secret_key_env(monkeypatch): | ||
"""Set secret key in environment variable.""" | ||
monkeypatch.setenv("DJANGO_TEST_SECRET_KEY", "TestSecretInEnv") | ||
|
||
|
||
@pytest.fixture | ||
def _mock_secret_key_file_env(monkeypatch): | ||
"""Set secret key path in environment variable.""" | ||
monkeypatch.setenv("DJANGO_TEST_SECRET_KEY_FILE", FILE_SECRET_PATH) | ||
|
||
|
||
@pytest.fixture | ||
def _mock_secret_key_path_env(monkeypatch): | ||
"""Set secret key path in environment variable with another `file_suffix`.""" | ||
monkeypatch.setenv("DJANGO_TEST_SECRET_KEY_PATH", FILE_SECRET_PATH) | ||
|
||
|
||
def test_secret_default(): | ||
"""Test call with no environment variable.""" | ||
value = SecretFileValue("DefaultTestSecret") | ||
assert value.setup("TEST_SECRET_KEY") == "DefaultTestSecret" | ||
|
||
|
||
@pytest.mark.usefixtures("_mock_secret_key_env") | ||
def test_secret_in_env(): | ||
"""Test call with secret key environment variable.""" | ||
value = SecretFileValue("DefaultTestSecret") | ||
assert os.environ["DJANGO_TEST_SECRET_KEY"] == "TestSecretInEnv" | ||
assert value.setup("TEST_SECRET_KEY") == "TestSecretInEnv" | ||
|
||
|
||
@pytest.mark.usefixtures("_mock_secret_key_file_env") | ||
def test_secret_in_file(): | ||
"""Test call with secret key file environment variable.""" | ||
value = SecretFileValue("DefaultTestSecret") | ||
assert os.environ["DJANGO_TEST_SECRET_KEY_FILE"] == FILE_SECRET_PATH | ||
assert value.setup("TEST_SECRET_KEY") == "TestSecretInFile" | ||
|
||
|
||
def test_secret_default_suffix(): | ||
"""Test call with no environment variable and non default `file_suffix`.""" | ||
value = SecretFileValue("DefaultTestSecret", file_suffix="PATH") | ||
assert value.setup("TEST_SECRET_KEY") == "DefaultTestSecret" | ||
|
||
|
||
@pytest.mark.usefixtures("_mock_secret_key_env") | ||
def test_secret_in_env_suffix(): | ||
"""Test call with secret key environment variable and non default `file_suffix`.""" | ||
value = SecretFileValue("DefaultTestSecret", file_suffix="PATH") | ||
assert os.environ["DJANGO_TEST_SECRET_KEY"] == "TestSecretInEnv" | ||
assert value.setup("TEST_SECRET_KEY") == "TestSecretInEnv" | ||
|
||
|
||
@pytest.mark.usefixtures("_mock_secret_key_path_env") | ||
def test_secret_in_file_suffix(): | ||
"""Test call with secret key file environment variable and non default `file_suffix`.""" | ||
value = SecretFileValue("DefaultTestSecret", file_suffix="PATH") | ||
assert os.environ["DJANGO_TEST_SECRET_KEY_PATH"] == FILE_SECRET_PATH | ||
assert value.setup("TEST_SECRET_KEY") == "TestSecretInFile" |
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.