Compare commits

..

No commits in common. "24fcf6f8141507ff0331e0918aac85c8d3ac1533" and "d30680cbedf9dd1aa48b61dc651ba9b08b152fea" have entirely different histories.

7 changed files with 90 additions and 157 deletions

View file

@ -1,9 +1,3 @@
## 0.1.0a20 (2025-05-12)
### Fix
- black
## 0.1.0a19 (2025-05-02) ## 0.1.0a19 (2025-05-02)
### Fix ### Fix

View file

@ -5,7 +5,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2025-05-12 08:39+0200\n" "POT-Creation-Date: 2025-04-29 23:02+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -15,63 +15,63 @@ msgstr ""
"Generated-By: pygettext.py 1.5\n" "Generated-By: pygettext.py 1.5\n"
#: src/rougail/user_data_bitwarden/data.py:54 #: src/rougail/user_data_bitwarden/data.py:52
msgid "\"bitwarden\" is not set in step.user_data" msgid "\"bitwarden\" is not set in step.user_data"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:76 #: src/rougail/user_data_bitwarden/data.py:74
msgid "\"rbw\" or \"bw\"" msgid "\"rbw\" or \"bw\""
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:78 #: src/rougail/user_data_bitwarden/data.py:76
msgid "\"{0}\"" msgid "\"{0}\""
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:81 #: src/rougail/user_data_bitwarden/data.py:78
msgid "please unlock Bitwarden password database with {0}" msgid "please unlock Bitwarden password database with {0}"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:86 #: src/rougail/user_data_bitwarden/data.py:79
msgid "cannot find Bitwarden command {0} please install it" msgid "cannot find Bitwarden command {0} please install it"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:175 #: src/rougail/user_data_bitwarden/data.py:153
msgid "the value for \"{0}\" at index {1} is already set while it should be filled in by Bitwarden" msgid "the value for \"{0}\" at index {1} is already set while it should be filled in by Bitwarden"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:181 #: src/rougail/user_data_bitwarden/data.py:155
msgid "the value for \"{0}\" is already set while it should be filled in by Bitwarden" msgid "the value for \"{0}\" is already set while it should be filled in by Bitwarden"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:219 #: src/rougail/user_data_bitwarden/data.py:185
msgid "the default value for \"{0}\" must be the Bitwarden item name" msgid "the default value for \"{0}\" must be the Bitwarden item name"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:236 #: src/rougail/user_data_bitwarden/data.py:198
msgid "cannot execute the \"{0}\" commandline from Bitwarden for \"{1}\": {2}" msgid "cannot execute the \"{0}\" commandline from Bitwarden for \"{1}\": {2}"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:243 #: src/rougail/user_data_bitwarden/data.py:201
msgid "item \"{0}\" in Bitwarden is not found for \"{1}\"" msgid "item \"{0}\" in Bitwarden is not found for \"{1}\""
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:256 #: src/rougail/user_data_bitwarden/data.py:208
msgid "several items found with name \"{0}\" in Bitwarden for \"{1}\": \"{2}\"" msgid "several items found with name \"{0}\" in Bitwarden for \"{1}\": \"{2}\""
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:271 #: src/rougail/user_data_bitwarden/data.py:219
msgid "unexpected datas \"{0}\" from Bitwarden for \"{1}\": {2}" msgid "unexpected datas \"{0}\" from Bitwarden for \"{1}\": {2}"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:279 #: src/rougail/user_data_bitwarden/data.py:224
msgid "password" msgid "password"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:281 #: src/rougail/user_data_bitwarden/data.py:226
msgid "username" msgid "username"
msgstr "" msgstr ""
#: src/rougail/user_data_bitwarden/data.py:283 #: src/rougail/user_data_bitwarden/data.py:227
msgid "item \"{0}\" in Bitwarden has no {1} for \"{2}\"" msgid "item \"{0}\" in Bitwarden has no {1} for \"{2}\""
msgstr "" msgstr ""

View file

@ -4,7 +4,7 @@ requires = ["flit_core >=3.8.0,<4"]
[project] [project]
name = "rougail.user_data_bitwarden" name = "rougail.user_data_bitwarden"
version = "0.1.0a20" version = "0.1.0a19"
authors = [{name = "Emmanuel Garette", email = "gnunux@gnunux.info"}] authors = [{name = "Emmanuel Garette", email = "gnunux@gnunux.info"}]
readme = "README.md" readme = "README.md"
description = "Rougail user_data Bitwarden" description = "Rougail user_data Bitwarden"

View file

@ -24,4 +24,4 @@ from .__version__ import __version__
RougailUserData = RougailUserDataBitwarden RougailUserData = RougailUserDataBitwarden
__all__ = ("RougailUserDataBitwarden",) __all__ = ('RougailUserDataBitwarden',)

View file

@ -1 +1 @@
__version__ = "0.1.0a20" __version__ = "0.1.0a19"

View file

@ -20,8 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
""" """
def get_rougail_config( def get_rougail_config(*,
*,
backward_compatibility=True, backward_compatibility=True,
) -> dict: ) -> dict:
options = """ options = """
@ -48,12 +47,11 @@ bitwarden:
- bw - bw
mandatory: false mandatory: false
""" """
return { return {'name': 'bitwarden',
"name": "bitwarden", 'process': 'user data',
"process": "user data",
"options": options, "options": options,
"level": 90, 'level': 90,
} }
__all__ = ("get_rougail_config",) __all__ = ('get_rougail_config',)

View file

@ -32,9 +32,8 @@ from .i18n import _
class RougailUserDataBitwarden: class RougailUserDataBitwarden:
force_apply_user_data = True force_apply_user_data = True
def __init__( def __init__(self,
self, config: 'Config',
config: "Config",
*, *,
rougailconfig: "RougailConfig"=None, rougailconfig: "RougailConfig"=None,
): ):
@ -42,15 +41,14 @@ class RougailUserDataBitwarden:
self.config = config self.config = config
if rougailconfig is None: if rougailconfig is None:
from rougail.config import RougailConfig from rougail.config import RougailConfig
rougailconfig = RougailConfig rougailconfig = RougailConfig
user_data = rougailconfig["step.user_data"] user_data = rougailconfig['step.user_data']
if "bitwarden" not in user_data: if 'bitwarden' not in user_data:
user_data.append("bitwarden") user_data.append('bitwarden')
rougailconfig["step.user_data"] = user_data rougailconfig['step.user_data'] = user_data
user_data = rougailconfig["step.user_data"] user_data = rougailconfig['step.user_data']
self.rougailconfig = rougailconfig self.rougailconfig = rougailconfig
if "bitwarden" not in user_data: if 'bitwarden' not in user_data:
raise ExtentionError(_('"bitwarden" is not set in step.user_data')) raise ExtentionError(_('"bitwarden" is not set in step.user_data'))
self.errors = [] self.errors = []
self.warnings = [] self.warnings = []
@ -58,14 +56,14 @@ class RougailUserDataBitwarden:
self.bitwarden_command_line = self.get_command(rougailconfig) self.bitwarden_command_line = self.get_command(rougailconfig)
def get_command(self, rougailconfig): def get_command(self, rougailconfig):
if "ROUGAIL_BITWARDEN_MOCK_ENABLE" in environ: if 'ROUGAIL_BITWARDEN_MOCK_ENABLE' in environ:
return None return None
one_is_find = False one_is_find = False
force_command = rougailconfig["bitwarden.command"] force_command = rougailconfig["bitwarden.command"]
if force_command: if force_command:
commands = [force_command] commands = [force_command]
else: else:
commands = ["rbw", "bw"] commands = ['rbw', 'bw']
for command in commands: for command in commands:
status = self.test_command(command) status = self.test_command(command)
if status is False: if status is False:
@ -77,34 +75,26 @@ class RougailUserDataBitwarden:
else: else:
command_string = _('"{0}"').format(force_command) command_string = _('"{0}"').format(force_command)
if one_is_find: if one_is_find:
raise ExtentionError( raise ExtentionError(_('please unlock Bitwarden password database with {0}').format(command_string))
_("please unlock Bitwarden password database with {0}").format( raise ExtentionError(_('cannot find Bitwarden command {0} please install it').format(command_string))
command_string
)
)
raise ExtentionError(
_("cannot find Bitwarden command {0} please install it").format(
command_string
)
)
def test_command(self, command): def test_command(self, command):
if not which(command): if not which(command):
return None return None
if command == "rbw": if command == 'rbw':
cmd = ["rbw", "unlocked"] cmd = ['rbw', 'unlocked']
else: else:
cmd = ["bw", "status"] cmd = ['bw', 'status']
try: try:
cpe = run(cmd, capture_output=True) cpe = run(cmd, capture_output=True)
except Exception as exc: except Exception as exc:
return False return False
if cpe.returncode != 0: if cpe.returncode != 0:
return False return False
if command == "rbw": if command == 'rbw':
return True return True
try: try:
data = loads(cpe.stdout.decode("utf8")) data = loads(cpe.stdout.decode('utf8'))
if data["status"] == "unlocked": if data["status"] == "unlocked":
return True return True
except: except:
@ -113,85 +103,64 @@ class RougailUserDataBitwarden:
def run(self): def run(self):
self.set_passwords(self.config.forcepermissive) self.set_passwords(self.config.forcepermissive)
return { return {'errors': self.errors,
"errors": self.errors, 'warnings': self.warnings,
"warnings": self.warnings,
} }
def run_commandline(self, cmd) -> str: def run_commandline(self, cmd) -> str:
cpe = run(cmd, capture_output=True) cpe = run(cmd, capture_output=True)
err = cpe.stderr.decode("utf8") err = cpe.stderr.decode('utf8')
if cpe.returncode != 0 or err: if cpe.returncode != 0 or err:
raise Exception("{0} ({1})".format(err, cpe.returncode)) raise Exception('{0} ({1})'.format(err, cpe.returncode))
return cpe.stdout.decode("utf8") return cpe.stdout.decode('utf8')
def get_key_from_commandline( def get_key_from_commandline(self, key_bitwarden: str, allow_multiple: bool) -> list[str]:
self, key_bitwarden: str, allow_multiple: bool if self.bitwarden_command_line == 'rbw':
) -> list[str]:
if self.bitwarden_command_line == "rbw":
keys = [] keys = []
items = self.run_commandline(["rbw", "search", key_bitwarden]).strip() items = self.run_commandline(["rbw", "search", key_bitwarden]).strip()
if items: if items:
items = items.split("\n") items = items.split('\n')
else: else:
items = [] items = []
for item in items: for item in items:
#if item.count('@') != 1: #if item.count('@') != 1:
# continue # continue
if "@" in item: if "@" in item:
keys.append(item.split("@", 1)[-1]) keys.append(item.split('@', 1)[-1])
else: else:
keys.append(item.rsplit("/", 1)[-1]) keys.append(item.rsplit('/', 1)[-1])
if not allow_multiple: if not allow_multiple:
if not items: if not items:
return [] return []
if len(items) > 1: if len(items) > 1:
return [{"name": key} for key in keys] return [{'name': key} for key in keys]
keys = [key_bitwarden] keys = [key_bitwarden]
datas = [] datas = []
for key in keys: for key in keys:
data = loads( data = loads(self.run_commandline(["rbw", "get", key, '--raw', '--ignorecase']).strip())
self.run_commandline( datas.append({'name': key, 'login': data["data"]})
["rbw", "get", key, "--raw", "--ignorecase"]
).strip()
)
datas.append({"name": key, "login": data["data"]})
return datas return datas
return loads( return loads(self.run_commandline(["bw", "list", "items", "--search", key_bitwarden, '--nointeraction']))
self.run_commandline(
["bw", "list", "items", "--search", key_bitwarden, "--nointeraction"]
)
)
def set_passwords(self, optiondescription): def set_passwords(self, optiondescription):
for option in optiondescription: for option in optiondescription:
if option.isoptiondescription(): if option.isoptiondescription():
self.set_passwords(option) self.set_passwords(option)
elif option.information.get("bitwarden", False): elif option.information.get('bitwarden', False):
path = option.path() path = option.path()
if not option.owner.isdefault(): if not option.owner.isdefault():
if option.isfollower(): if option.isfollower():
self.errors.append( 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()))
_(
'the value for "{0}" at index {1} is already set while it should be filled in by Bitwarden'
).format(path, option.index())
)
else: else:
self.errors.append( self.errors.append(_('the value for "{0}" is already set while it should be filled in by Bitwarden').format(path))
_(
'the value for "{0}" is already set while it should be filled in by Bitwarden'
).format(path)
)
continue continue
type_ = option.information.get("type") type_ = option.information.get('type')
if option.isleader(): if option.isleader():
leader_values = [] leader_values = []
self.leader_informations[path] = [] self.leader_informations[path] = []
values = option.value.get() values = option.value.get()
for val in values: for val in values:
names, values = self.get_values( names, values = self.get_values(path, type_, val, allow_multiple=True)
path, type_, val, allow_multiple=True
)
if isinstance(values, list): if isinstance(values, list):
leader_values.extend(values) leader_values.extend(values)
self.leader_informations[path].extend(names) self.leader_informations[path].extend(names)
@ -203,85 +172,57 @@ class RougailUserDataBitwarden:
if option.isfollower(): if option.isfollower():
leader_path = optiondescription.leader().path() leader_path = optiondescription.leader().path()
if leader_path in self.leader_informations: if leader_path in self.leader_informations:
key_bitwarden = self.leader_informations[leader_path][ key_bitwarden = self.leader_informations[leader_path][option.index()]
option.index()
]
else: else:
key_bitwarden = option.value.get() key_bitwarden = option.value.get()
else: else:
key_bitwarden = option.value.get() key_bitwarden = option.value.get()
option.value.set(self.get_values(path, type_, key_bitwarden)[1]) option.value.set(self.get_values(path, type_, key_bitwarden)[1])
option.permissive.add("novalidator") option.permissive.add('novalidator')
def get_values(self, path, type_, key_bitwarden, *, allow_multiple=False): def get_values(self, path, type_, key_bitwarden, *, allow_multiple=False):
if not isinstance(key_bitwarden, str): if not isinstance(key_bitwarden, str):
self.errors.append( self.errors.append(_('the default value for "{0}" must be the Bitwarden item name').format(path))
_('the default value for "{0}" must be the Bitwarden item name').format(
path
)
)
return None, None return None, None
if "ROUGAIL_BITWARDEN_MOCK_ENABLE" in environ: if 'ROUGAIL_BITWARDEN_MOCK_ENABLE' in environ:
if type_ == "secret": if type_ == 'secret':
value = "Ex4mpL3_P4ssw0rD" value = 'Ex4mpL3_P4ssw0rD'
else: else:
value = "example_login" value = 'example_login'
if allow_multiple: if allow_multiple:
return [key_bitwarden], [value] return [key_bitwarden], [value]
return key_bitwarden, value return key_bitwarden, value
try: try:
data = self.get_key_from_commandline(key_bitwarden, allow_multiple) data = self.get_key_from_commandline(key_bitwarden, allow_multiple)
except Exception as exc: except Exception as exc:
self.errors.append( self.errors.append(_('cannot execute the "{0}" commandline from Bitwarden for "{1}": {2}').format(self.bitwarden_command_line, path, exc))
_(
'cannot execute the "{0}" commandline from Bitwarden for "{1}": {2}'
).format(self.bitwarden_command_line, path, exc)
)
return None, None return None, None
if not data: if not data:
self.errors.append( self.errors.append(_('item "{0}" in Bitwarden is not found for "{1}"').format(key_bitwarden, path))
_('item "{0}" in Bitwarden is not found for "{1}"').format(
key_bitwarden, path
)
)
return None, None return None, None
if len(data) != 1: if len(data) != 1:
names = [d["name"] for d in data] names = [d["name"] for d in data]
if allow_multiple: if allow_multiple:
ret = [] ret = []
return names, [ return names, [self.get_value(key_bitwarden, path, type_, d) for d in data]
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)))
]
self.errors.append(
_(
'several items found with name "{0}" in Bitwarden for "{1}": "{2}"'
).format(key_bitwarden, path, '", "'.join(names))
)
return None, None return None, None
return data[0]["name"], self.get_value(key_bitwarden, path, type_, data[0]) 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: def get_value(self, key_bitwarden: str, path: str, type_: str, data: dict) -> str:
try: try:
if type_ == "secret": if type_ == 'secret':
value = data["login"]["password"] value = data['login']['password']
else: else:
value = data["login"]["username"] value = data['login']['username']
except Exception as exc: except Exception as exc:
self.errors.append( self.errors.append(_('unexpected datas "{0}" from Bitwarden for "{1}": {2}').format(key_bitwarden, path, exc))
_('unexpected datas "{0}" from Bitwarden for "{1}": {2}').format(
key_bitwarden, path, exc
)
)
value = None value = None
else: else:
if value is None: if value is None:
if type_ == "secret": if type_ == 'secret':
bw_type = _("password") bw_type = _('password')
else: else:
bw_type = _("username") bw_type = _('username')
self.errors.append( self.errors.append(_('item "{0}" in Bitwarden has no {1} for "{2}"').format(key_bitwarden, bw_type, path))
_('item "{0}" in Bitwarden has no {1} for "{2}"').format(
key_bitwarden, bw_type, path
)
)
return value return value