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

189 lines
8.3 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
def __init__(self,
config: 'Config',
*,
rougailconfig: "RougailConfig"=None,
):
# this is the tiramisu config object
self.config = config
if rougailconfig is None:
from rougail.config import RougailConfig
rougailconfig = RougailConfig
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']
self.rougailconfig = rougailconfig
if 'bitwarden' not in user_data:
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-02-17 09:34:26 +01:00
bitwarden_command_line = None
one_is_find = False
if 'ROUGAIL_BITWARDEN_MOCK_ENABLE' not in environ:
if which('rbw'):
one_is_find = True
try:
cpe = run(['rbw', 'unlocked'], capture_output=True)
except Exception as exc:
pass
else:
if cpe.returncode == 0:
bitwarden_command_line = 'rbw'
if bitwarden_command_line is None and which('bw'):
one_is_find = True
try:
cpe = run(['bw', 'status'], capture_output=True)
except Exception as exc:
pass
else:
if cpe.returncode == 0:
try:
data = loads(cpe.stdout.decode('utf8'))
if data["status"] == "unlocked":
bitwarden_command_line = 'bw'
except:
pass
if bitwarden_command_line is None:
if one_is_find:
raise ExtentionError(_('please unlock Bitwarden password database'))
raise ExtentionError(_('cannot find Bitwarden command (rbw or bw) please install it'))
2025-02-17 09:34:26 +01:00
self.bitwarden_command_line = bitwarden_command_line
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-02-05 11:30:51 +01:00
return {'errors': self.errors,
'warnings': self.warnings,
}
2025-02-17 09:34:26 +01:00
def run_commandline(self, cmd) -> str:
cpe = run(cmd, capture_output=True)
err = cpe.stderr.decode('utf8')
if cpe.returncode != 0 or err:
raise Exception('{0} ({1})'.format(err, cpe.returncode))
return cpe.stdout.decode('utf8')
def get_key_from_commandline(self, key_bitwarden: str, allow_multiple: bool) -> list[str]:
if self.bitwarden_command_line == 'rbw':
if allow_multiple:
keys = []
items = self.run_commandline(["rbw", "search", key_bitwarden]).strip()
for item in items.split('\n'):
if item.count('@') != 1:
continue
keys.append(item.split('@', 1)[-1])
else:
keys = [key_bitwarden]
datas = []
for key in keys:
data = loads(self.run_commandline(["rbw", "get", key, '--raw']).strip())
datas.append({'name': key, 'login': data["data"]})
return datas
return loads(self.run_commandline(["bw", "list", "items", "--search", key_bitwarden, '--nointeraction']))
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-02-12 15:36:48 +01:00
elif option.information.get('bitwarden', False):
path = option.path()
if not option.owner.isdefault():
if option.isfollower():
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()))
else:
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-02-12 15:36:48 +01:00
type_ = option.information.get('type')
if option.isleader():
leader_values = []
self.leader_informations[path] = []
values = option.value.get()
for val in values:
2025-02-12 15:36:48 +01:00
names, values = self.get_values(path, type_, val, allow_multiple=True)
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():
leader_path = optiondescription.leader().path()
if leader_path in self.leader_informations:
key_bitwarden = self.leader_informations[leader_path][option.index()]
else:
key_bitwarden = option.value.get()
else:
key_bitwarden = option.value.get()
option.value.set(self.get_values(path, type_, key_bitwarden)[1])
def get_values(self, path, type_, key_bitwarden, *, allow_multiple=False):
if not isinstance(key_bitwarden, str):
self.errors.append(_('the default value for "{0}" must be the Bitwarden item name').format(path))
return None, None
if 'ROUGAIL_BITWARDEN_MOCK_ENABLE' in environ:
2025-02-19 09:27:47 +01:00
if allow_multiple:
return ['example_login'], ['Ex4mpL3_P4ssw0rD']
return 'example_login', 'Ex4mpL3_P4ssw0rD'
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-02-17 09:34:26 +01: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:
self.errors.append(_('cannot find {0} "{1}" from Bitwarden for "{2}"').format(type_, key_bitwarden, path))
return None, None
if len(data) != 1:
names = [d["name"] for d in data]
if allow_multiple:
ret = []
return names, [self.get_value(key_bitwarden, path, type_, d) for d in data]
self.errors.append(_('several items found with name "{0}" from Bitwarden for "{1}": "{2}"').format(key_bitwarden, path, "\", \"".join(names)))
return None, None
return data[0]['name'], self.get_value(key_bitwarden, path, type_, data[0])
def get_value(self, key_bitwarden: str, path: str, type_: str, data: dict) -> str:
try:
if type_ == 'secret':
return data['login']['password']
return data['login']['username']
except Exception as exc:
self.errors.append(_('unexpected datas "{0}" from Bitwarden for "{1}": {2}').format(key_bitwarden, path, exc))
return None