From 7537044de05da111246f89630ba62f1ff72c1ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C4=9Bmec?= Date: Sat, 2 Nov 2019 10:17:18 +0100 Subject: [PATCH] Allow KeePass database with Key File --- README.md | 2 +- bitwarden-to-keepass.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index aed8ee5..0f8fb4f 100644 --- a/README.md +++ b/README.md @@ -20,5 +20,5 @@ source .venv/bin/activate - [Download](https://help.bitwarden.com/article/cli/#download--install) official bitwarden-cli and do `bw login` (you need `BW_SESSION` for export to work). - Run ``` -python3 bitwarden-to-keepass.py --bw-session BW_SESSION --database-path DATABASE_PATH --database-password DATABASE_PASSWORD [--bw_path BW_PATH] +python3 bitwarden-to-keepass.py --bw-session BW_SESSION --database-path DATABASE_PATH --database-password DATABASE_PASSWORD [--database-keyfile DATABASE_KEYFILE] [--bw-path BW_PATH] ``` diff --git a/bitwarden-to-keepass.py b/bitwarden-to-keepass.py index 9e31516..fbf56b7 100644 --- a/bitwarden-to-keepass.py +++ b/bitwarden-to-keepass.py @@ -6,8 +6,8 @@ import sys from argparse import ArgumentParser -from construct import ChecksumError from pykeepass import PyKeePass +from pykeepass.exceptions import CredentialsIntegrityError from item import Item, Types as ItemTypes @@ -16,8 +16,8 @@ logging.basicConfig(stream=sys.stdout, level=logging.INFO) def bitwarden_to_keepass(args): try: - kp = PyKeePass(args.database_path, password=args.database_password) - except ChecksumError as e: + kp = PyKeePass(args.database_path, password=args.database_password, keyfile=args.database_keyfile) + except CredentialsIntegrityError as e: logging.error(f'Wrong password for KeePass database: {e}') return @@ -75,6 +75,11 @@ def check_args(args): logging.error('KeePass database is not readable/writable.') return False + if args.database_keyfile: + if not os.path.isfile(args.database_keyfile) or not os.access(args.database_keyfile, os.R_OK): + logging.error('Key File for KeePass database is not readable.') + return False + if not os.path.isfile(args.bw_path) or not os.access(args.bw_path, os.X_OK): logging.error('bitwarden-cli was not found or not executable. Did you set correct \'--bw-path\'?') return False @@ -86,6 +91,7 @@ parser = ArgumentParser() parser.add_argument('--bw-session', help='Session generated from bitwarden-cli (bw login)', required=True) parser.add_argument('--database-path', help='Path to KeePass database', required=True) parser.add_argument('--database-password', help='Password for KeePass database', required=True) +parser.add_argument('--database-keyfile', help='Path to Key File for KeePass database', default=None) parser.add_argument('--bw-path', help='Path for bw binary', default='bw') args = parser.parse_args()