rougail-user-data-bitwarden/src/rougail/user_data_bitwarden/data.py

288 lines
10 KiB
Python
Raw Normal View History

2025-02-05 11:30:51 +01:00
"""
Silique (https://www.silique.fr)
Copyright (C) 2025
distribued with GPL-2 or later license
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 subprocess import run
from json import loads
2025-02-12 15:36:48 +01:00
from os import environ
2025-02-17 09:34:26 +01:00
from shutil import which
2025-02-05 11:30:51 +01:00
from rougail.error import ExtentionError
from .i18n import _
class RougailUserDataBitwarden:
force_apply_user_data = True
2025-05-11 19:11:17 +02:00
def __init__(
self,
config: "Config",
*,
rougailconfig: "RougailConfig" = None,
):
2025-02-05 11:30:51 +01:00
# this is the tiramisu config object
self.config = config
if rougailconfig is None:
from rougail.config import RougailConfig
2025-05-11 19:11:17 +02:00
2025-02-05 11:30:51 +01:00
rougailconfig = RougailConfig
2025-05-11 19:11:17 +02:00
user_data = rougailconfig["step.user_data"]
if "bitwarden" not in user_data:
user_data.append("bitwarden")
rougailconfig["step.user_data"] = user_data
user_data = rougailconfig["step.user_data"]
2025-02-05 11:30:51 +01:00
self.rougailconfig = rougailconfig
2025-05-11 19:11:17 +02:00
if "bitwarden" not in user_data:
2025-02-05 11:30:51 +01:00
raise ExtentionError(_('"bitwarden" is not set in step.user_data'))
self.errors = []
self.warnings = []
2025-02-12 15:36:48 +01:00
self.leader_informations = {}
2025-03-01 18:04:38 +01:00
self.bitwarden_command_line = self.get_command(rougailconfig)
def get_command(self, rougailconfig):
2025-05-11 19:11:17 +02:00
if "ROUGAIL_BITWARDEN_MOCK_ENABLE" in environ:
2025-03-01 18:04:38 +01:00
return None
2025-02-17 09:34:26 +01:00
one_is_find = False
2025-03-01 18:04:38 +01:00
force_command = rougailconfig["bitwarden.command"]
if force_command:
commands = [force_command]
else:
2025-05-11 19:11:17 +02:00
commands = ["rbw", "bw"]
2025-03-01 18:04:38 +01:00
for command in commands:
status = self.test_command(command)
if status is False:
one_is_find = True
2025-03-01 18:04:38 +01:00
elif status:
return command
if not force_command:
command_string = _('"rbw" or "bw"')
else:
command_string = _('"{0}"').format(force_command)
if one_is_find:
2025-05-11 19:11:17 +02:00
raise ExtentionError(
_("please unlock Bitwarden password database with {0}").format(
command_string
)
)
raise ExtentionError(
_("cannot find Bitwarden command {0} please install it").format(
command_string
)
)
2025-03-01 18:04:38 +01:00
def test_command(self, command):
if not which(command):
return None
2025-05-11 19:11:17 +02:00
if command == "rbw":
cmd = ["rbw", "unlocked"]
2025-03-01 18:04:38 +01:00
else:
2025-05-11 19:11:17 +02:00
cmd = ["bw", "status"]
2025-03-01 18:04:38 +01:00
try:
cpe = run(cmd, capture_output=True)
except Exception as exc:
return False
if cpe.returncode != 0:
return False
2025-05-11 19:11:17 +02:00
if command == "rbw":
2025-03-01 18:04:38 +01:00
return True
try:
2025-05-11 19:11:17 +02:00
data = loads(cpe.stdout.decode("utf8"))
2025-03-01 18:04:38 +01:00
if data["status"] == "unlocked":
return True
except:
pass
return False
2025-02-05 11:30:51 +01:00
def run(self):
2025-02-17 15:25:59 +01:00
self.set_passwords(self.config.forcepermissive)
2025-05-11 19:11:17 +02:00
return {
"errors": self.errors,
"warnings": self.warnings,
}
2025-02-05 11:30:51 +01:00
2025-02-17 09:34:26 +01:00
def run_commandline(self, cmd) -> str:
cpe = run(cmd, capture_output=True)
2025-05-11 19:11:17 +02:00
err = cpe.stderr.decode("utf8")
2025-02-17 09:34:26 +01:00
if cpe.returncode != 0 or err:
2025-05-11 19:11:17 +02:00
raise Exception("{0} ({1})".format(err, cpe.returncode))
return cpe.stdout.decode("utf8")
2025-02-17 09:34:26 +01:00
2025-05-11 19:11:17 +02:00
def get_key_from_commandline(
self, key_bitwarden: str, allow_multiple: bool
) -> list[str]:
if self.bitwarden_command_line == "rbw":
2025-03-01 18:04:38 +01:00
keys = []
items = self.run_commandline(["rbw", "search", key_bitwarden]).strip()
if items:
2025-05-11 19:11:17 +02:00
items = items.split("\n")
2025-02-17 09:34:26 +01:00
else:
2025-03-01 18:04:38 +01:00
items = []
for item in items:
2025-05-11 19:11:17 +02:00
# if item.count('@') != 1:
2025-03-01 18:04:38 +01:00
# continue
2025-03-19 15:31:52 +01:00
if "@" in item:
2025-05-11 19:11:17 +02:00
keys.append(item.split("@", 1)[-1])
2025-03-19 15:31:52 +01:00
else:
2025-05-11 19:11:17 +02:00
keys.append(item.rsplit("/", 1)[-1])
2025-03-01 18:04:38 +01:00
if not allow_multiple:
if not items:
return []
if len(items) > 1:
2025-05-11 19:11:17 +02:00
return [{"name": key} for key in keys]
2025-02-17 09:34:26 +01:00
keys = [key_bitwarden]
datas = []
for key in keys:
2025-05-11 19:11:17 +02:00
data = loads(
self.run_commandline(
["rbw", "get", key, "--raw", "--ignorecase"]
).strip()
)
datas.append({"name": key, "login": data["data"]})
2025-02-17 09:34:26 +01:00
return datas
2025-05-11 19:11:17 +02:00
return loads(
self.run_commandline(
["bw", "list", "items", "--search", key_bitwarden, "--nointeraction"]
)
)
2025-02-17 09:34:26 +01:00
2025-02-05 11:30:51 +01:00
def set_passwords(self, optiondescription):
for option in optiondescription:
if option.isoptiondescription():
self.set_passwords(option)
2025-05-11 19:11:17 +02:00
elif option.information.get("bitwarden", False):
2025-02-12 15:36:48 +01:00
path = option.path()
if not option.owner.isdefault():
if option.isfollower():
2025-05-11 19:11:17 +02:00
self.errors.append(
_(
'the value for "{0}" at index {1} is already set while it should be filled in by Bitwarden'
).format(path, option.index())
)
2025-02-12 15:36:48 +01:00
else:
2025-05-11 19:11:17 +02:00
self.errors.append(
_(
'the value for "{0}" is already set while it should be filled in by Bitwarden'
).format(path)
)
2025-02-05 11:30:51 +01:00
continue
2025-05-11 19:11:17 +02:00
type_ = option.information.get("type")
2025-02-12 15:36:48 +01:00
if option.isleader():
leader_values = []
self.leader_informations[path] = []
values = option.value.get()
for val in values:
2025-05-11 19:11:17 +02:00
names, values = self.get_values(
path, type_, val, allow_multiple=True
)
2025-02-12 15:36:48 +01:00
if isinstance(values, list):
leader_values.extend(values)
self.leader_informations[path].extend(names)
else:
leader_values.append(values)
self.leader_informations[path].append(names)
option.value.set(leader_values)
else:
if option.isfollower():
2025-05-11 19:11:17 +02:00
leader_path = optiondescription.leader().path()
2025-02-12 15:36:48 +01:00
if leader_path in self.leader_informations:
2025-05-11 19:11:17 +02:00
key_bitwarden = self.leader_informations[leader_path][
option.index()
]
2025-02-12 15:36:48 +01:00
else:
key_bitwarden = option.value.get()
else:
key_bitwarden = option.value.get()
option.value.set(self.get_values(path, type_, key_bitwarden)[1])
2025-05-11 19:11:17 +02:00
option.permissive.add("novalidator")
2025-02-12 15:36:48 +01:00
def get_values(self, path, type_, key_bitwarden, *, allow_multiple=False):
if not isinstance(key_bitwarden, str):
2025-05-11 19:11:17 +02:00
self.errors.append(
_('the default value for "{0}" must be the Bitwarden item name').format(
path
)
)
2025-02-12 15:36:48 +01:00
return None, None
2025-05-11 19:11:17 +02:00
if "ROUGAIL_BITWARDEN_MOCK_ENABLE" in environ:
if type_ == "secret":
value = "Ex4mpL3_P4ssw0rD"
else:
2025-05-11 19:11:17 +02:00
value = "example_login"
2025-02-19 09:27:47 +01:00
if allow_multiple:
return [key_bitwarden], [value]
return key_bitwarden, value
2025-02-12 15:36:48 +01:00
try:
2025-02-17 09:34:26 +01:00
data = self.get_key_from_commandline(key_bitwarden, allow_multiple)
2025-02-12 15:36:48 +01:00
except Exception as exc:
2025-05-11 19:11:17 +02:00
self.errors.append(
_(
'cannot execute the "{0}" commandline from Bitwarden for "{1}": {2}'
).format(self.bitwarden_command_line, path, exc)
)
2025-02-12 15:36:48 +01:00
return None, None
if not data:
2025-05-11 19:11:17 +02:00
self.errors.append(
_('item "{0}" in Bitwarden is not found for "{1}"').format(
key_bitwarden, path
)
)
2025-02-12 15:36:48 +01:00
return None, None
if len(data) != 1:
names = [d["name"] for d in data]
if allow_multiple:
ret = []
2025-05-11 19:11:17 +02:00
return names, [
self.get_value(key_bitwarden, path, type_, d) for d in data
]
self.errors.append(
_(
'several items found with name "{0}" in Bitwarden for "{1}": "{2}"'
).format(key_bitwarden, path, '", "'.join(names))
)
2025-02-12 15:36:48 +01:00
return None, None
2025-05-11 19:11:17 +02:00
return data[0]["name"], self.get_value(key_bitwarden, path, type_, data[0])
2025-02-12 15:36:48 +01:00
def get_value(self, key_bitwarden: str, path: str, type_: str, data: dict) -> str:
try:
2025-05-11 19:11:17 +02:00
if type_ == "secret":
value = data["login"]["password"]
2025-04-09 14:20:56 +02:00
else:
2025-05-11 19:11:17 +02:00
value = data["login"]["username"]
2025-02-12 15:36:48 +01:00
except Exception as exc:
2025-05-11 19:11:17 +02:00
self.errors.append(
_('unexpected datas "{0}" from Bitwarden for "{1}": {2}').format(
key_bitwarden, path, exc
)
)
2025-04-09 14:20:56 +02:00
value = None
else:
if value is None:
2025-05-11 19:11:17 +02:00
if type_ == "secret":
bw_type = _("password")
2025-04-09 14:20:56 +02:00
else:
2025-05-11 19:11:17 +02:00
bw_type = _("username")
self.errors.append(
_('item "{0}" in Bitwarden has no {1} for "{2}"').format(
key_bitwarden, bw_type, path
)
)
2025-04-09 14:20:56 +02:00
return value