bitwarden-ssh-agent/ssh.py

69 lines
2 KiB
Python
Raw Normal View History

2020-03-07 17:22:01 +00:00
import subprocess
import os
import sys
import json
import tempfile
2020-03-07 17:22:01 +00:00
from urllib.parse import urlparse, urlencode
from urllib.request import urlopen, Request
from urllib.error import HTTPError
try:
subprocess.check_output(['bw', 'logout'])
except:
pass
try:
session = subprocess.check_output(['bw', '--raw', 'login'])
session = ['--session', session]
except:
print('Couldnt login!')
sys.exit(1)
try:
folders = subprocess.check_output(['bw','list', 'folders', '--search', 'ssh-agent'] + session)
folders = json.loads(folders)
if not folders:
raise AttributeError
if len(folders) != 1:
raise ValueError
except AttributeError:
print('Couldnt find ssh-agent folder!')
sys.exit(1)
except ValueError:
print('More than one ssh-agent folder found!')
sys.exit(1)
except:
print('Error retrieving folders.')
sys.exit(1)
folder = folders[0]['id']
try:
items = subprocess.check_output(['bw', 'list', 'items', '--folderid', folder, 'ssh-agent'] + session)
items = json.loads(items)
except Exception as e:
print('Cant fint items.')
print(e)
sys.exit(1)
keys = []
directory = tempfile.TemporaryDirectory()
temporary_file_name = os.path.join(directory.name + './private_key')
2020-03-07 17:22:01 +00:00
try:
for item in items:
private_key_file = [k['value'] for k in item['fields'] if k['name'] == 'private' and k['type'] == 0][0]
private_key_id = [k['id'] for k in item['attachments'] if k['fileName'] == private_key_file][0]
# would be nice if there was an option to retrieve the attachment file directly to the stdout
subprocess.check_output(['bw', 'get', 'attachment', private_key_id, '--itemid', item['id'], '--output', temporary_file_name] + session)
private_key = open(temporary_file_name, 'r').read()
os.remove(temporary_file_name)
2020-03-07 17:22:01 +00:00
keys.append({'private_key': private_key})
except:
print('Something happened.')
sys.exit(1)
print(';'.join([k['private_key'] for k in keys]))