Display a command to allow the user to re-use the newly created session

This commit is contained in:
Scott Wallace 2022-03-25 13:43:13 +00:00
parent 1b1746edf3
commit 1d05c037f3
Signed by: scott
GPG key ID: AA742FDC5AFE2A72

View file

@ -81,7 +81,12 @@ def get_session():
universal_newlines=True, universal_newlines=True,
check=True, check=True,
) )
return proc_session.stdout session = proc_session.stdout
logging.info(
'To re-use this BitWarden session run: export BW_SESSION="%s"',
session,
)
return session
def get_folders(session, foldername): def get_folders(session, foldername):
@ -132,24 +137,32 @@ def add_ssh_keys(session, items, keyname):
""" """
for item in items: for item in items:
try: try:
private_key_file = [k['value'] for k in item['fields'] private_key_file = [
if k['name'] == keyname and k['type'] == 0][0] k['value']
for k in item['fields']
if k['name'] == keyname and k['type'] == 0
][0]
except IndexError: except IndexError:
logging.warning('No "%s" field found for item %s', keyname, item['name']) logging.warning('No "%s" field found for item %s', keyname, item['name'])
continue continue
except KeyError as e: except KeyError as e:
logging.debug('No key "%s" found in item %s - skipping', e.args[0], item['name']) logging.debug(
'No key "%s" found in item %s - skipping', e.args[0], item['name']
)
continue continue
logging.debug('Private key file declared') logging.debug('Private key file declared')
try: try:
private_key_id = [k['id'] for k in item['attachments'] private_key_id = [
if k['fileName'] == private_key_file][0] k['id']
for k in item['attachments']
if k['fileName'] == private_key_file
][0]
except IndexError: except IndexError:
logging.warning( logging.warning(
'No attachment called "%s" found for item %s', 'No attachment called "%s" found for item %s',
private_key_file, private_key_file,
item['name'] item['name'],
) )
continue continue
logging.debug('Private key ID found') logging.debug('Private key ID found')
@ -167,13 +180,17 @@ def ssh_add(session, item_id, key_id):
logging.debug('Item ID: %s', item_id) logging.debug('Item ID: %s', item_id)
logging.debug('Key ID: %s', key_id) logging.debug('Key ID: %s', key_id)
proc_attachment = subprocess.run([ proc_attachment = subprocess.run(
[
'bw', 'bw',
'get', 'get',
'attachment', key_id, 'attachment',
'--itemid', item_id, key_id,
'--itemid',
item_id,
'--raw', '--raw',
'--session', session '--session',
session,
], ],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True, universal_newlines=True,
@ -195,30 +212,33 @@ def ssh_add(session, item_id, key_id):
if __name__ == '__main__': if __name__ == '__main__':
def parse_args(): def parse_args():
""" """
Function to parse command line arguments Function to parse command line arguments
""" """
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
'-d', '--debug', '-d',
'--debug',
action='store_true', action='store_true',
help='show debug output', help='show debug output',
) )
parser.add_argument( parser.add_argument(
'-f', '--foldername', '-f',
'--foldername',
default='ssh-agent', default='ssh-agent',
help='folder name to use to search for SSH keys', help='folder name to use to search for SSH keys',
) )
parser.add_argument( parser.add_argument(
'-c', '--customfield', '-c',
'--customfield',
default='private', default='private',
help='custom field name where private key filename is stored', help='custom field name where private key filename is stored',
) )
return parser.parse_args() return parser.parse_args()
def main(): def main():
""" """
Main program logic Main program logic