-
Notifications
You must be signed in to change notification settings - Fork 3
Add optional callback-style way to resolve secret values from config #111
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
Draft
akaIDIOT
wants to merge
14
commits into
main
Choose a base branch
from
feature/resolve-secrets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d3097ae
Define protocols for a Secrets implementation and a callback providin…
akaIDIOT b300055
Add parameterizable implementations of Secrets.{matches,resolve}
akaIDIOT 79c9de8
Add utility type for the simple case, implementing a Secrets from a s…
akaIDIOT 74ed218
Add utility to optionally auto-wrap a callback to a Secrets
akaIDIOT cb03f4f
Add secrets parameter to Configuration
akaIDIOT d278716
Copy secrets when wrapping delegates
akaIDIOT 0d6e077
Call matches() and resolve() during get() to resolve secrets from source
akaIDIOT 069ce48
Transparently pass secrets to Configuration at load-time
akaIDIOT 460926e
Apply merging logic for missing to secrets during merge
akaIDIOT 14e87d8
Add line comments and shuffle logging in secrets module
akaIDIOT 4fd3bd0
Shut up, flake8
akaIDIOT 4d0d029
(Re-)apply styling rules
akaIDIOT d124354
Use a match statement in place of a condition tree
akaIDIOT 80ff700
WIP: Add multiple types of secret matchers
akaIDIOT 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,92 @@ | ||
| import logging | ||
| import typing | ||
| from functools import partial | ||
|
|
||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @typing.runtime_checkable | ||
| class Secrets(typing.Protocol): | ||
| def resolve(self, value: typing.Any) -> typing.Any: ... | ||
|
|
||
|
|
||
| @typing.runtime_checkable | ||
| class MappingSecrets(Secrets, typing.Protocol): | ||
| # e.g. {"$secret": {"service": "github.com", "username": "akaIDIOT"}} | ||
| def matches_mapping(self, value: typing.Mapping[str, typing.Any]) -> bool: ... | ||
|
|
||
|
|
||
| @typing.runtime_checkable | ||
| class SequenceSecrets(Secrets, typing.Protocol): | ||
| # e.g. ["$secret", "github.com", "akaIDIOT"] # TODO: references inside of that sequence would likely not work? | ||
| def matches_sequence(self, value: typing.Sequence[typing.Any]) -> bool: ... | ||
|
|
||
|
|
||
| @typing.runtime_checkable | ||
| class StrSecrets(Secrets, typing.Protocol): | ||
| # e.g. "secret!akaIDIOT@github.com" # TODO: references inside of that str would likely not work? | ||
| def matches_str(self, value: str) -> bool: ... | ||
|
|
||
|
|
||
| @typing.runtime_checkable | ||
| class SecretCallback(typing.Protocol): | ||
| def __call__(self, *args: typing.Any) -> typing.Any: ... | ||
|
|
||
|
|
||
| # FIXME: load_name(..., secrets=keyring.get_password) does not yet specify a matching value (type) | ||
| # FIXME: the rest is very geared towards MappingSecrets | ||
|
|
||
|
|
||
| def to_secrets(secrets: Secrets | SecretCallback | None) -> Secrets | None: | ||
| if not secrets: | ||
| return None | ||
| elif isinstance(secrets, SecretCallback): | ||
| return SingleKeyCallback(secrets) | ||
| else: | ||
| return secrets | ||
|
|
||
|
|
||
| def is_single_key_secret(value: typing.Mapping[str, typing.Any], *, key: str) -> bool: | ||
| return len(value) == 1 and key in value | ||
|
|
||
|
|
||
| def resolve_n_key_secret_callback( | ||
| value: typing.Mapping[str, typing.Any], *, callback: SecretCallback, single_key: str, args: typing.Iterable[str] | ||
| ) -> typing.Any: | ||
| try: | ||
| mapping = value[single_key] | ||
| LOG.debug(f'getting values for ({", ".join(args)}) to use as secret retrieval parameters') | ||
| parameters = tuple(mapping[arg] for arg in args) | ||
| # logging parameters' *values* might still leak things a user would rather not log | ||
| LOG.info(f'passing {len(parameters)} to secret callback {callback}') | ||
| return callback(*parameters) | ||
| except KeyError as e: | ||
| if missing_key := e.args[0] if e.args[0] == single_key else f'{single_key}.{e.args[0]}': | ||
| LOG.warning(f'resolving secret failed, missing key {missing_key}') | ||
| else: | ||
| LOG.warning('resolving secret failed') | ||
| # logging out of the way, there's not actually anything we can do to fix the error here | ||
| # if the caller was Configuration.get(), it will handle the KeyError according to it's policies | ||
| raise | ||
|
|
||
|
|
||
| class SingleKeyCallback: | ||
| def __init__( | ||
| self, | ||
| callback: SecretCallback, | ||
| single_key: str = '$secret', | ||
| args: typing.Iterable[str] = ('service', 'username'), | ||
| ): | ||
| # use is_single_key_secret and resolve_n_key_secret_callback to turn the callback we've been handed here into | ||
| # something that will implement the Secrets protocol | ||
| self.matches_mapping = partial( | ||
| is_single_key_secret, | ||
| key=single_key, | ||
| ) | ||
| self.resolve = partial( | ||
| resolve_n_key_secret_callback, | ||
| callback=callback, | ||
| single_key=single_key, | ||
| args=args, | ||
| ) | ||
|
Member
Author
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. This is a very composition over inheritance approach to an implementation conforming to the |
||
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.
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.
Yes, yes, this need many comments on what / why the f things are as they are, soon™