diff --git a/src/rougail/user_data_bitwarden/annotator.py b/src/rougail/user_data_bitwarden/annotator.py index a22f75d..5b68c1b 100644 --- a/src/rougail/user_data_bitwarden/annotator.py +++ b/src/rougail/user_data_bitwarden/annotator.py @@ -8,25 +8,26 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ + from rougail.error import DictConsistencyError from rougail.annotator.variable import Walk class Annotator(Walk): """Annotate for bitwarden""" - + level = 90 - + def __init__( self, objectspace, diff --git a/src/rougail/user_data_bitwarden/data.py b/src/rougail/user_data_bitwarden/data.py index 40c8e22..4cd5b80 100644 --- a/src/rougail/user_data_bitwarden/data.py +++ b/src/rougail/user_data_bitwarden/data.py @@ -32,11 +32,16 @@ from .i18n import _ class FakeBW: def get_key( - self, - key_bitwarden: str, - leader: bool, - ): - return [{'name': key_bitwarden, 'login': {'username': "example_login", 'password': "Ex4mpL3_P4ssw0rD"}}] + self, + key_bitwarden: str, + leader: bool, + ): + return [ + { + "name": key_bitwarden, + "login": {"username": "example_login", "password": "Ex4mpL3_P4ssw0rD"}, + } + ] class RBW: @@ -48,10 +53,10 @@ class RBW: return cpe.returncode == 0 def get_key( - self, - key_bitwarden: str, - leader: bool, - ): + self, + key_bitwarden: str, + leader: bool, + ): keys = [] items = run_commandline(["rbw", "search", key_bitwarden]).strip() if items: @@ -72,9 +77,7 @@ class RBW: datas = [] for key in sorted(keys): data = loads( - run_commandline( - ["rbw", "get", key, "--raw", "--ignorecase"] - ).strip() + run_commandline(["rbw", "get", key, "--raw", "--ignorecase"]).strip() ) datas.append({"name": key, "login": data["data"]}) return datas @@ -97,10 +100,10 @@ class BW: return False def get_key( - self, - key_bitwarden: str, - *args, - ): + self, + key_bitwarden: str, + *args, + ): return loads( run_commandline( ["bw", "list", "items", "--search", key_bitwarden, "--nointeraction"] @@ -143,9 +146,10 @@ class RougailUserDataBitwarden: self.errors = [] self.warnings = [] self.cache = {} - self.commands = {'rbw': RBW(), - 'bw': BW(), - } + self.commands = { + "rbw": RBW(), + "bw": BW(), + } def run(self): values = {} @@ -153,13 +157,13 @@ class RougailUserDataBitwarden: self.set_passwords(self.config.unrestraint, values) return [ { - "source": 'Bitwarden', + "source": "Bitwarden", "errors": self.errors, "warnings": self.warnings, "values": values, "options": { "secret_manager": True, - } + }, } ] @@ -186,7 +190,11 @@ class RougailUserDataBitwarden: if option.isoptiondescription(): self.set_passwords(option, values) elif option.information.get("bitwarden", False): - values[option.path(uncalculated=True)] = (set_password, self.cache, self.command) + values[option.path(uncalculated=True)] = ( + set_password, + self.cache, + self.command, + ) def set_password(cache, command, *, option): @@ -195,9 +203,7 @@ def set_password(cache, command, *, option): if leader: key = key[0] if not isinstance(key, str): - raise ConfigError( - _('the default value must be the Bitwarden item name') - ) + raise ConfigError(_("the default value must be the Bitwarden item name")) if key in cache: data = cache[key] if option.isfollower(): @@ -208,27 +214,21 @@ def set_password(cache, command, *, option): data = command.get_key(key, leader) except Exception as exc: raise ConfigError( - _( - 'cannot execute the "{0}" commandline from Bitwarden: {1}' - ).format(command, exc) + _('cannot execute the "{0}" commandline from Bitwarden: {1}').format( + command, exc + ) ) cache[key] = data.copy() if not data: - raise ConfigError( - _('item "{0}" in Bitwarden is not found').format( - key - ) - ) + raise ConfigError(_('item "{0}" in Bitwarden is not found').format(key)) type_ = option.information.get("type") if leader: - return [ - get_value(key, type_, d) for d in data - ] + return [get_value(key, type_, d) for d in data] if len(data) != 1: raise ConfigError( - _( - 'several items found with name "{0}" in Bitwarden: "{1}"' - ).format(key, '", "'.join([d["name"] for d in data])) + _('several items found with name "{0}" in Bitwarden: "{1}"').format( + key, '", "'.join([d["name"] for d in data]) + ) ) return get_value(key, type_, data[0]) @@ -241,9 +241,7 @@ def get_value(key_bitwarden: str, type_: str, data: dict) -> str: value = data["login"]["username"] except Exception as exc: raise ConfigError( - _('unexpected datas "{0}" from Bitwarden: {1}').format( - key_bitwarden, exc - ) + _('unexpected datas "{0}" from Bitwarden: {1}').format(key_bitwarden, exc) ) if value is None: if type_ == "secret": @@ -251,8 +249,6 @@ def get_value(key_bitwarden: str, type_: str, data: dict) -> str: else: bw_type = _("username") raise ConfigError( - _('item "{0}" in Bitwarden has no {1}').format( - key_bitwarden, bw_type - ) + _('item "{0}" in Bitwarden has no {1}').format(key_bitwarden, bw_type) ) return value