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