|
26 | 26 | logger = logging.getLogger("compliance.utils.credentials")
|
27 | 27 |
|
28 | 28 |
|
| 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 | + |
29 | 40 | class Config:
|
30 | 41 | """Handle credentials configuration."""
|
31 | 42 |
|
32 |
| - def __init__(self, cfg_file="~/.credentials"): |
| 43 | + BACKENDS = {"1password": OnePasswordBackend, "configparser": ConfigParserBackend} |
| 44 | + |
| 45 | + def __init__(self, cfg_file="~/.credentials", backend_cfg=None): |
33 | 46 | """
|
34 | 47 | Create an instance of a dictionary-like configuration object.
|
35 | 48 |
|
36 | 49 | :param cfg_file: The path to the RawConfigParser compatible config file
|
37 | 50 | """
|
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")) |
41 | 65 |
|
42 | 66 | def __getitem__(self, section):
|
43 | 67 | """
|
|
0 commit comments