add bwpass.attr() to get any other saved attributes

This commit is contained in:
Franzi 2021-07-02 16:44:46 +02:00
parent 71e6490d21
commit 47bcea8293
Signed by: kunsi
GPG key ID: 12E3D2136B818350
2 changed files with 41 additions and 7 deletions

View file

@ -4,12 +4,12 @@ from subprocess import check_output, CalledProcessError
from bundlewrap.exceptions import FaultUnavailable from bundlewrap.exceptions import FaultUnavailable
from bundlewrap.utils import Fault from bundlewrap.utils import Fault
from bundlewrap.utils.dict import merge_dict from bundlewrap.utils.dicts import merge_dict
from bundlewrap.utils.text import bold, yellow from bundlewrap.utils.text import bold, yellow
from bundlewrap.utils.ui import io from bundlewrap.utils.ui import io
ENVIRONMENT = merge_dict(environ, { ENVIRONMENT = merge_dict(environ, {
'PASSWORD_STORE_DIR': expanduser(environ.get('BW_PASS_DIR', '~/.password-store')). 'PASSWORD_STORE_DIR': expanduser(environ.get('BW_PASS_DIR', '~/.password-store')),
}) })
DUMMY_MODE = environ.get('BW_PASS_DUMMY_MODE', '0') == '1' DUMMY_MODE = environ.get('BW_PASS_DUMMY_MODE', '0') == '1'
@ -41,10 +41,21 @@ def _get_contents_from_pass(identifier: str):
identifier, identifier,
)) ))
cache[identifier] = {} if not pass_output:
cache[identifier]['password'] = pass_output[0] raise ValueError('BUG: `pass show {}` did not return any output!'.format(
identifier
))
# TODO import all other options set in pass cache[identifier] = {
'password': pass_output[0].strip(),
'attrs': {},
}
if len(pass_output) > 1:
for line in pass_output[1:]:
attr, value = line.split(':', 1)
cache[identifier]['attrs'][attr] = value.strip()
return cache[identifier] return cache[identifier]
@ -63,3 +74,26 @@ def password(identifier):
_password, _password,
identifier=identifier, identifier=identifier,
) )
def _attr(identifier, attr):
if DUMMY_MODE:
return 'PASS DUMMY {} ATTRIBUTE'.format(attr)
else:
secret = _get_contents_from_pass(identifier)
try:
return secret['attrs'][attr]
except KeyError:
raise FaultUnavailable('attribute {} not found for identifier {}'.format(
attr,
identifier,
))
def attr(identifier, attr):
return Fault(
'bwpass attribute {}'.format(attr),
_attr,
identifier=identifier,
attr=attr,
)

View file

@ -2,7 +2,7 @@ from setuptools import setup
setup( setup(
name="bundlewrap-pass", name="bundlewrap-pass",
version="0.0.1", version="1.0.0",
description="Get passwordstore entries via bundlewrap", description="Get passwordstore entries via bundlewrap",
author="Franziska Kunsmann", author="Franziska Kunsmann",
author_email="hi@kunsmann.eu", author_email="hi@kunsmann.eu",
@ -10,7 +10,7 @@ setup(
py_modules=['bwpass'], py_modules=['bwpass'],
keywords=["configuration", "config", "management"], keywords=["configuration", "config", "management"],
classifiers=[ classifiers=[
# "Development Status :: 5 - Production/Stable", "Development Status :: 5 - Production/Stable",
"Intended Audience :: System Administrators", "Intended Audience :: System Administrators",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Natural Language :: English", "Natural Language :: English",