Skip to content

Commit

Permalink
Invoke and password mask update (#15)
Browse files Browse the repository at this point in the history
kmagusiak authored Oct 20, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 7def0c3 commit 971e250
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion alphaconf/__init__.py
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@
"""A list of functions which given a key indicate whether it's a secret"""
SECRET_MASKS = [
# mask if contains a kind of secret and it's not in a file
re.compile(r'.*(key|password|secret)s?(?!_file)(_|$)').match,
re.compile(r'.*(key|password|secret)s?(?!_file)(_|$)|^private(_key|$)').match,
]

#######################################
25 changes: 19 additions & 6 deletions alphaconf/internal/__init__.py
Original file line number Diff line number Diff line change
@@ -306,10 +306,20 @@ def masked_configuration(
lambda v: v if v is None or v == '???' else '*****',
)
if mask_base and 'base' not in mask_keys:
# first remove all values if the object is not resolved
config['base'] = Application.__mask_config(
config['base'],
lambda p: isinstance(OmegaConf.select(self.configuration, p), DictConfig),
lambda v: list(v) if isinstance(v, dict) else v,
lambda p: not isinstance(
OmegaConf.select(self.configuration, p, throw_on_resolution_failure=False),
DictConfig,
),
lambda v: {},
)
# then collapse dict[str,None] into a list[str]
config['base'] = Application.__mask_config(
config['base'],
lambda _: True,
lambda v: list(v) if isinstance(v, dict) and not any(v.values()) else v,
)
if mask_keys:
config = Application.__mask_config(config, lambda p: p in mask_keys, lambda _: None)
@@ -325,19 +335,22 @@ def __mask_config(obj, check, replace, path=''):
:param key: Current path
:return: The modified config
"""
if check(path):
obj = replace(obj)
if isinstance(obj, dict):
result = {}
for key, value in obj.items():
new = Application.__mask_config(value, check, replace, f"{path}.{key}")
result[key] = new
new = Application.__mask_config(
value, check, replace, f"{path}.{key}" if path else key
)
if new is not None:
result[key] = new
obj = result
elif isinstance(obj, list):
obj = [
Application.__mask_config(v, check, replace, f"{path}[{i}]")
for i, v in enumerate(obj)
]
if check(path):
obj = replace(obj)
return obj

def print_help(self, *, usage=None, description=None, arguments=True):
2 changes: 1 addition & 1 deletion alphaconf/invoke.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ def __init__(self, namespace, **properties) -> None:
self.namespace = namespace
self.argument_parser.add_argument(
InvokeAction,
metavar="invoke arguments",
metavar="-- invoke arguments",
help="Rest is passed to invoke",
)

4 changes: 2 additions & 2 deletions example-inv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from invoke import Collection, task
from invoke import Collection, Task, task

import alphaconf.invoke

@@ -20,6 +20,6 @@ def doit(ctx, param=None):


# add some default configuration and run/configure invoke's namespace
ns = Collection(doit)
ns = Collection(*[v for v in globals().values() if isinstance(v, Task)])
alphaconf.setup_configuration({'backup': 'all'})
alphaconf.invoke.run(__name__, ns)

0 comments on commit 971e250

Please sign in to comment.