Skip to content

Commit 0311f27

Browse files
committed
sync
1 parent 513ae76 commit 0311f27

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

compliance/utils/credentials.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,42 @@
2626
logger = logging.getLogger("compliance.utils.credentials")
2727

2828

29+
class OnePasswordBackend:
30+
pass
31+
32+
33+
class ConfigParserBackend:
34+
def __init__(self, **kwargs):
35+
self._cfg = RawConfigParser()
36+
self._cfg_file = kwargs.get("cfg_file")
37+
self._cfg.read(str(Path(self._cfg_file).expanduser()))
38+
39+
2940
class Config:
3041
"""Handle credentials configuration."""
3142

32-
def __init__(self, cfg_file="~/.credentials"):
43+
BACKENDS = {"1password": OnePasswordBackend, "configparser": ConfigParserBackend}
44+
45+
def __init__(self, cfg_file="~/.credentials", backend_cfg=None):
3346
"""
3447
Create an instance of a dictionary-like configuration object.
3548
3649
:param cfg_file: The path to the RawConfigParser compatible config file
3750
"""
38-
self._cfg = RawConfigParser()
39-
self._cfg.read(str(Path(cfg_file).expanduser()))
40-
self._cfg_file = cfg_file
51+
52+
if backend_cfg is None:
53+
backend_cfg = {"name": "configparser", "cfg_file": cfg_file}
54+
self._init_backend(backend_cfg)
55+
56+
def _init_backend(self, backend_cfg):
57+
name = backend_cfg.get("name")
58+
if backend_cfg.get("name") not in Config.BACKENDS:
59+
raise ValueError(f"Invalid credentials backend name: {name}")
60+
self._backend = Config.BACKENDS[name](**backend_cfg)
61+
62+
@classmethod
63+
def from_auditree_config(cls, at_cfg):
64+
return cls(backend_cfg=at_cfg.get("creds.backend"))
4165

4266
def __getitem__(self, section):
4367
"""

0 commit comments

Comments
 (0)