-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Enable multiple secrets dirs #372
Changes from all commits
1ff5008
f23349f
bfc9d7a
58b032f
fa433b1
90f56bc
376737e
5c5e465
048ff00
d59fcc3
cff5aaa
7f8e0fc
6289ae7
037bf84
039d22f
5e85247
170c68d
09e8c05
74604ed
6e05557
a8e6b36
098eaaa
16d4606
3a4cd95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -574,7 +574,7 @@ class SecretsSettingsSource(PydanticBaseEnvSettingsSource): | |
def __init__( | ||
self, | ||
settings_cls: type[BaseSettings], | ||
secrets_dir: str | Path | None = None, | ||
secrets_dir: PathType | None = None, | ||
case_sensitive: bool | None = None, | ||
env_prefix: str | None = None, | ||
env_ignore_empty: bool | None = None, | ||
|
@@ -595,14 +595,22 @@ def __call__(self) -> dict[str, Any]: | |
if self.secrets_dir is None: | ||
return secrets | ||
|
||
self.secrets_path = Path(self.secrets_dir).expanduser() | ||
secrets_dirs = [self.secrets_dir] if isinstance(self.secrets_dir, (str, os.PathLike)) else self.secrets_dir | ||
secrets_paths = [Path(p).expanduser() for p in secrets_dirs] | ||
self.secrets_paths = [] | ||
|
||
if not self.secrets_path.exists(): | ||
warnings.warn(f'directory "{self.secrets_path}" does not exist') | ||
for path in secrets_paths: | ||
if not path.exists(): | ||
warnings.warn(f'directory "{path}" does not exist') | ||
else: | ||
self.secrets_paths.append(path) | ||
|
||
if not len(self.secrets_paths): | ||
return secrets | ||
|
||
if not self.secrets_path.is_dir(): | ||
raise SettingsError(f'secrets_dir must reference a directory, not a {path_type_label(self.secrets_path)}') | ||
for path in self.secrets_paths: | ||
if not path.is_dir(): | ||
raise SettingsError(f'secrets_dir must reference a directory, not a {path_type_label(path)}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a test for this error that tests single dir. please add a test for multiple directories as well. |
||
|
||
return super().__call__() | ||
|
||
|
@@ -640,18 +648,20 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str, | |
""" | ||
|
||
for field_key, env_name, value_is_complex in self._extract_field_info(field, field_name): | ||
path = self.find_case_path(self.secrets_path, env_name, self.case_sensitive) | ||
if not path: | ||
# path does not exist, we currently don't return a warning for this | ||
continue | ||
# paths reversed to match the last-wins behaviour of `env_file` | ||
for secrets_path in reversed(self.secrets_paths): | ||
path = self.find_case_path(secrets_path, env_name, self.case_sensitive) | ||
if not path: | ||
# path does not exist, we currently don't return a warning for this | ||
continue | ||
|
||
if path.is_file(): | ||
return path.read_text().strip(), field_key, value_is_complex | ||
else: | ||
warnings.warn( | ||
f'attempted to load secret file "{path}" but found a {path_type_label(path)} instead.', | ||
stacklevel=4, | ||
) | ||
if path.is_file(): | ||
return path.read_text().strip(), field_key, value_is_complex | ||
else: | ||
warnings.warn( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a test for this warning that tests single dir. please add a test for multiple directories as well. |
||
f'attempted to load secret file "{path}" but found a {path_type_label(path)} instead.', | ||
stacklevel=4, | ||
) | ||
|
||
return None, field_key, value_is_complex | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a test for this warning that tests single dir. please add a test for multiple directories as well.