2020-04-13 10:51:37 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Extracts SSH keys from Bitwarden vault
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
2023-07-17 11:48:29 +01:00
|
|
|
from typing import Any
|
2020-04-16 12:11:18 +01:00
|
|
|
|
|
|
|
|
2022-10-13 15:23:49 +01:00
|
|
|
def get_session(session: str) -> str:
|
2020-04-13 10:51:37 +01:00
|
|
|
"""
|
|
|
|
Function to return a valid Bitwarden session
|
|
|
|
"""
|
|
|
|
# Check for an existing, user-supplied Bitwarden session
|
2022-10-13 15:23:49 +01:00
|
|
|
if not session:
|
2023-01-14 16:29:39 +00:00
|
|
|
session = os.environ.get("BW_SESSION", "")
|
2022-03-25 14:27:37 +00:00
|
|
|
if session:
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Existing Bitwarden session found")
|
2021-01-26 19:29:22 +00:00
|
|
|
return session
|
2020-04-13 10:51:37 +01:00
|
|
|
|
|
|
|
# Check if we're already logged in
|
2023-01-26 11:08:04 +00:00
|
|
|
proc_logged = subprocess.run(["bw", "login", "--check", "--quiet"], check=False)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2021-01-26 19:29:22 +00:00
|
|
|
if proc_logged.returncode:
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Not logged into Bitwarden")
|
|
|
|
operation = "login"
|
2020-04-13 10:51:37 +01:00
|
|
|
else:
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Bitwarden vault is locked")
|
|
|
|
operation = "unlock"
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2021-01-26 19:29:22 +00:00
|
|
|
proc_session = subprocess.run(
|
2023-01-14 16:29:39 +00:00
|
|
|
["bw", "--raw", operation],
|
2020-04-13 10:51:37 +01:00
|
|
|
stdout=subprocess.PIPE,
|
2021-11-22 07:41:55 +00:00
|
|
|
universal_newlines=True,
|
2021-01-26 21:17:01 +00:00
|
|
|
check=True,
|
2020-04-13 10:51:37 +01:00
|
|
|
)
|
2022-03-25 13:43:13 +00:00
|
|
|
session = proc_session.stdout
|
|
|
|
logging.info(
|
|
|
|
'To re-use this BitWarden session run: export BW_SESSION="%s"',
|
|
|
|
session,
|
|
|
|
)
|
|
|
|
return session
|
2020-04-13 10:51:37 +01:00
|
|
|
|
|
|
|
|
2022-03-25 14:13:36 +00:00
|
|
|
def get_folders(session: str, foldername: str) -> str:
|
2020-04-13 10:51:37 +01:00
|
|
|
"""
|
|
|
|
Function to return the ID of the folder that matches the provided name
|
|
|
|
"""
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Folder name: %s", foldername)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2021-01-26 19:29:22 +00:00
|
|
|
proc_folders = subprocess.run(
|
2023-01-14 16:29:39 +00:00
|
|
|
["bw", "list", "folders", "--search", foldername, "--session", session],
|
2020-04-13 10:51:37 +01:00
|
|
|
stdout=subprocess.PIPE,
|
2021-11-22 07:41:55 +00:00
|
|
|
universal_newlines=True,
|
2021-01-26 21:17:01 +00:00
|
|
|
check=True,
|
2023-01-14 16:29:39 +00:00
|
|
|
encoding="utf-8",
|
2020-04-13 10:51:37 +01:00
|
|
|
)
|
|
|
|
|
2021-01-26 19:29:22 +00:00
|
|
|
folders = json.loads(proc_folders.stdout)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
|
|
|
if not folders:
|
|
|
|
logging.error('"%s" folder not found', foldername)
|
2023-01-14 16:29:39 +00:00
|
|
|
return ""
|
2020-04-13 10:51:37 +01:00
|
|
|
|
|
|
|
# Do we have any folders
|
|
|
|
if len(folders) != 1:
|
|
|
|
logging.error('%d folders with the name "%s" found', len(folders), foldername)
|
2023-01-14 16:29:39 +00:00
|
|
|
return ""
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2023-01-14 16:29:39 +00:00
|
|
|
return str(folders[0]["id"])
|
2020-04-13 10:51:37 +01:00
|
|
|
|
|
|
|
|
2023-06-14 13:38:00 +01:00
|
|
|
def folder_items(session: str, folder_id: str) -> list[dict[str, Any]]:
|
2020-04-13 10:51:37 +01:00
|
|
|
"""
|
|
|
|
Function to return items from a folder
|
|
|
|
"""
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Folder ID: %s", folder_id)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2021-01-26 19:29:22 +00:00
|
|
|
proc_items = subprocess.run(
|
2023-01-14 16:29:39 +00:00
|
|
|
["bw", "list", "items", "--folderid", folder_id, "--session", session],
|
2020-04-13 10:51:37 +01:00
|
|
|
stdout=subprocess.PIPE,
|
2021-11-22 07:41:55 +00:00
|
|
|
universal_newlines=True,
|
2021-01-26 21:17:01 +00:00
|
|
|
check=True,
|
2023-01-14 16:29:39 +00:00
|
|
|
encoding="utf-8",
|
2020-04-13 10:51:37 +01:00
|
|
|
)
|
|
|
|
|
2023-06-14 13:38:00 +01:00
|
|
|
data: list[dict[str, Any]] = json.loads(proc_items.stdout)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2022-03-25 14:13:36 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
2022-05-02 13:08:57 +01:00
|
|
|
def add_ssh_keys(
|
|
|
|
session: str,
|
2023-06-14 13:38:00 +01:00
|
|
|
items: list[dict[str, Any]],
|
2022-05-02 13:08:57 +01:00
|
|
|
keyname: str,
|
|
|
|
pwkeyname: str,
|
|
|
|
) -> None:
|
2020-04-13 10:51:37 +01:00
|
|
|
"""
|
|
|
|
Function to attempt to get keys from a vault item
|
|
|
|
"""
|
|
|
|
for item in items:
|
|
|
|
try:
|
2022-03-25 13:43:13 +00:00
|
|
|
private_key_file = [
|
2023-01-14 16:29:39 +00:00
|
|
|
k["value"] for k in item["fields"] if k["name"] == keyname
|
2022-03-25 13:43:13 +00:00
|
|
|
][0]
|
2020-04-13 10:51:37 +01:00
|
|
|
except IndexError:
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.warning('No "%s" field found for item %s', keyname, item["name"])
|
2020-04-13 10:51:37 +01:00
|
|
|
continue
|
2022-03-25 14:13:36 +00:00
|
|
|
except KeyError as error:
|
2022-03-25 13:43:13 +00:00
|
|
|
logging.debug(
|
2023-01-14 16:29:39 +00:00
|
|
|
'No key "%s" found in item %s - skipping', error.args[0], item["name"]
|
2022-03-25 13:43:13 +00:00
|
|
|
)
|
2021-10-15 20:12:40 +01:00
|
|
|
continue
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Private key file declared")
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2023-06-14 13:38:00 +01:00
|
|
|
private_key_pw = ""
|
2022-04-25 16:32:24 +01:00
|
|
|
try:
|
|
|
|
private_key_pw = [
|
2023-01-14 16:29:39 +00:00
|
|
|
k["value"] for k in item["fields"] if k["name"] == pwkeyname
|
2022-04-25 16:32:24 +01:00
|
|
|
][0]
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Passphrase declared")
|
2022-04-25 16:32:24 +01:00
|
|
|
except IndexError:
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.warning('No "%s" field found for item %s', pwkeyname, item["name"])
|
2022-04-25 16:32:24 +01:00
|
|
|
except KeyError as error:
|
|
|
|
logging.debug(
|
2023-01-14 16:29:39 +00:00
|
|
|
'No key "%s" found in item %s - skipping', error.args[0], item["name"]
|
2022-04-25 16:32:24 +01:00
|
|
|
)
|
|
|
|
|
2020-04-13 10:51:37 +01:00
|
|
|
try:
|
2022-03-25 13:43:13 +00:00
|
|
|
private_key_id = [
|
2023-01-14 16:29:39 +00:00
|
|
|
k["id"]
|
|
|
|
for k in item["attachments"]
|
|
|
|
if k["fileName"] == private_key_file
|
2022-03-25 13:43:13 +00:00
|
|
|
][0]
|
2020-04-13 10:51:37 +01:00
|
|
|
except IndexError:
|
|
|
|
logging.warning(
|
|
|
|
'No attachment called "%s" found for item %s',
|
|
|
|
private_key_file,
|
2023-01-14 16:29:39 +00:00
|
|
|
item["name"],
|
2020-04-13 10:51:37 +01:00
|
|
|
)
|
|
|
|
continue
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Private key ID found")
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2021-05-11 09:13:04 +01:00
|
|
|
try:
|
2023-01-14 16:29:39 +00:00
|
|
|
ssh_add(session, item["id"], private_key_id, private_key_pw)
|
2021-05-11 09:13:04 +01:00
|
|
|
except subprocess.SubprocessError:
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.warning("Could not add key to the SSH agent")
|
2020-04-13 10:51:37 +01:00
|
|
|
|
|
|
|
|
2023-06-14 13:38:00 +01:00
|
|
|
def ssh_add(session: str, item_id: str, key_id: str, key_pw: str = "") -> None:
|
2020-04-13 10:51:37 +01:00
|
|
|
"""
|
|
|
|
Function to get the key contents from the Bitwarden vault
|
|
|
|
"""
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Item ID: %s", item_id)
|
|
|
|
logging.debug("Key ID: %s", key_id)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2022-03-25 13:43:13 +00:00
|
|
|
proc_attachment = subprocess.run(
|
|
|
|
[
|
2023-01-14 16:29:39 +00:00
|
|
|
"bw",
|
|
|
|
"get",
|
|
|
|
"attachment",
|
2022-03-25 13:43:13 +00:00
|
|
|
key_id,
|
2023-01-14 16:29:39 +00:00
|
|
|
"--itemid",
|
2022-03-25 13:43:13 +00:00
|
|
|
item_id,
|
2023-01-14 16:29:39 +00:00
|
|
|
"--raw",
|
|
|
|
"--session",
|
2022-03-25 13:43:13 +00:00
|
|
|
session,
|
2021-01-26 19:29:22 +00:00
|
|
|
],
|
|
|
|
stdout=subprocess.PIPE,
|
2021-11-22 07:41:55 +00:00
|
|
|
universal_newlines=True,
|
2021-01-26 21:17:01 +00:00
|
|
|
check=True,
|
2021-01-26 19:29:22 +00:00
|
|
|
)
|
2022-04-27 11:21:59 +01:00
|
|
|
ssh_key = proc_attachment.stdout
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2022-04-25 16:32:24 +01:00
|
|
|
if key_pw:
|
2022-05-02 13:08:57 +01:00
|
|
|
envdict = dict(
|
|
|
|
os.environ,
|
|
|
|
SSH_ASKPASS=os.path.realpath(__file__),
|
|
|
|
SSH_KEY_PASSPHRASE=key_pw,
|
|
|
|
)
|
2022-04-25 16:32:24 +01:00
|
|
|
else:
|
2022-04-27 11:21:59 +01:00
|
|
|
envdict = dict(os.environ, SSH_ASKPASS_REQUIRE="never")
|
2022-08-21 12:11:20 +01:00
|
|
|
|
2021-01-26 19:29:22 +00:00
|
|
|
logging.debug("Running ssh-add")
|
|
|
|
# CAVEAT: `ssh-add` provides no useful output, even with maximum verbosity
|
2021-05-11 09:13:04 +01:00
|
|
|
subprocess.run(
|
2023-01-14 16:29:39 +00:00
|
|
|
["ssh-add", "-"],
|
2023-06-14 13:58:53 +01:00
|
|
|
input=ssh_key.encode("utf-8"),
|
2021-05-11 11:19:14 +01:00
|
|
|
# Works even if ssh-askpass is not installed
|
2022-04-27 11:21:59 +01:00
|
|
|
env=envdict,
|
2023-06-14 13:58:53 +01:00
|
|
|
universal_newlines=False,
|
2021-01-26 21:17:01 +00:00
|
|
|
check=True,
|
2022-08-21 12:11:20 +01:00
|
|
|
)
|
2022-04-25 16:32:24 +01:00
|
|
|
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2023-01-14 16:29:39 +00:00
|
|
|
if __name__ == "__main__":
|
2022-03-25 13:43:13 +00:00
|
|
|
|
2022-03-25 14:13:36 +00:00
|
|
|
def parse_args() -> argparse.Namespace:
|
2020-04-13 10:51:37 +01:00
|
|
|
"""
|
|
|
|
Function to parse command line arguments
|
|
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
2023-01-14 16:29:39 +00:00
|
|
|
"-d",
|
|
|
|
"--debug",
|
|
|
|
action="store_true",
|
|
|
|
help="show debug output",
|
2020-04-13 10:51:37 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-01-14 16:29:39 +00:00
|
|
|
"-f",
|
|
|
|
"--foldername",
|
|
|
|
default="ssh-agent",
|
|
|
|
help="folder name to use to search for SSH keys",
|
2020-04-13 10:51:37 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-01-14 16:29:39 +00:00
|
|
|
"-c",
|
|
|
|
"--customfield",
|
|
|
|
default="private",
|
|
|
|
help="custom field name where private key filename is stored",
|
2020-04-13 10:51:37 +01:00
|
|
|
)
|
2022-04-25 16:32:24 +01:00
|
|
|
parser.add_argument(
|
2023-01-14 16:29:39 +00:00
|
|
|
"-p",
|
|
|
|
"--passphrasefield",
|
|
|
|
default="passphrase",
|
|
|
|
help="custom field name where key passphrase is stored",
|
2022-04-25 16:32:24 +01:00
|
|
|
)
|
2022-10-13 15:23:49 +01:00
|
|
|
parser.add_argument(
|
2023-01-14 16:29:39 +00:00
|
|
|
"-s",
|
|
|
|
"--session",
|
|
|
|
default="",
|
|
|
|
help="session key of bitwarden",
|
2022-10-13 15:23:49 +01:00
|
|
|
)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
2022-03-25 14:13:36 +00:00
|
|
|
def main() -> None:
|
2020-04-13 10:51:37 +01:00
|
|
|
"""
|
|
|
|
Main program logic
|
|
|
|
"""
|
|
|
|
|
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
if args.debug:
|
|
|
|
loglevel = logging.DEBUG
|
|
|
|
else:
|
|
|
|
loglevel = logging.INFO
|
|
|
|
|
|
|
|
logging.basicConfig(level=loglevel)
|
|
|
|
|
2021-01-26 19:29:22 +00:00
|
|
|
try:
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.info("Getting Bitwarden session")
|
2022-10-13 15:23:49 +01:00
|
|
|
session = get_session(args.session)
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Session = %s", session)
|
2021-01-26 19:29:22 +00:00
|
|
|
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.info("Getting folder list")
|
2021-01-26 19:29:22 +00:00
|
|
|
folder_id = get_folders(session, args.foldername)
|
|
|
|
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.info("Getting folder items")
|
2021-01-26 19:29:22 +00:00
|
|
|
items = folder_items(session, folder_id)
|
|
|
|
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.info("Attempting to add keys to ssh-agent")
|
2022-04-25 16:32:24 +01:00
|
|
|
add_ssh_keys(session, items, args.customfield, args.passphrasefield)
|
2022-03-25 14:13:36 +00:00
|
|
|
except subprocess.CalledProcessError as error:
|
|
|
|
if error.stderr:
|
2022-05-02 13:08:57 +01:00
|
|
|
logging.error('"%s" error: %s', error.cmd[0], error.stderr)
|
2023-01-14 16:29:39 +00:00
|
|
|
logging.debug("Error running %s", error.cmd)
|
2020-04-13 10:51:37 +01:00
|
|
|
|
2023-01-14 16:29:39 +00:00
|
|
|
if os.environ.get("SSH_ASKPASS") and os.environ.get(
|
|
|
|
"SSH_ASKPASS"
|
|
|
|
) == os.path.realpath(__file__):
|
|
|
|
print(os.environ.get("SSH_KEY_PASSPHRASE"))
|
2022-04-27 11:21:59 +01:00
|
|
|
else:
|
|
|
|
main()
|