From 9fc8bcf9a822cc647a722f4ffcc2e113a4f2edb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C4=9Bmec?= Date: Fri, 25 Dec 2020 20:07:46 +0100 Subject: [PATCH] Make exporting easier with docker-compose --- .env | 4 ++++ Dockerfile | 9 +++++++++ README.md | 21 +++++++++++++++------ bitwarden-to-keepass.py | 39 +++++++++++++++++++++++++++++++++------ docker-compose.yaml | 12 ++++++++++++ exports/.gitkeep | 0 6 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 docker-compose.yaml create mode 100644 exports/.gitkeep diff --git a/.env b/.env new file mode 100644 index 0000000..6af09d1 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +DATABASE_PASSWORD=CHANGE_ME +#DATABASE_KEYFILE= +BW_PATH=/usr/local/bin/bw +DATABASE_PATH=/exports/bitwarden-export.kdbx diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cb1f7a2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.9-slim-buster + +WORKDIR /bitwarden-to-keepass +COPY . . + +RUN apt update && apt install -y npm && \ + pip install -r requirements.txt && \ + npm i -g @bitwarden/cli && \ + apt purge -y npm diff --git a/README.md b/README.md index 9d4c66e..ad14a38 100644 --- a/README.md +++ b/README.md @@ -2,23 +2,32 @@ Export (most of) your Bitwarden items into KeePass database. ## How it works? -It uses official [bitwarden-cli](https://help.bitwarden.com/article/cli/) client to export your items from Bitwarden vault and move them into your KeePass database - that includes logins (with TOTP seeds, URIs, custom fields, attachments, notes) and secure notes. +It uses official [bitwarden-cli](https://bitwarden.com/help/article/cli/) client to export your items from Bitwarden vault and move them into your KeePass database - that includes logins (with TOTP seeds, URIs, custom fields, attachments, notes) and secure notes. -## Install +# Usage with docker (docker-compose) - recommended +- Clone this repository +- Edit `.env` file + - ⚠️ make sure to set your own `DATABASE_PASSWORD` - used as password for KeePass database +- Run +``` +docker-compose run bitwarden-to-keepass +``` +- You will be interactively asked to login with [bitwarden-cli](https://bitwarden.com/help/article/cli/) +- After the process is finished your database export is in `exports` directory + +## Usage without docker (venv) - Clone this repository - Run ``` make build ``` - -## Run/usage - You can either **create new (empty) KeePass database** (tested with [KeePassXC](https://github.com/keepassxreboot/keepassxc) but it will probably work with others) right now, otherwise one will be created when the script is executed - Go into the virtual environment ``` 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). +- [Download](https://bitwarden.com/help/article/cli/#download-and-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 [--database-keyfile DATABASE_KEYFILE] [--bw-path BW_PATH] -``` +``` \ No newline at end of file diff --git a/bitwarden-to-keepass.py b/bitwarden-to-keepass.py index 864dab2..7dc2648 100644 --- a/bitwarden-to-keepass.py +++ b/bitwarden-to-keepass.py @@ -72,7 +72,7 @@ def bitwarden_to_keepass(args): entry.set_custom_property(str(field['name']), field['value']) for attachment in bw_item.get_attachments(): - attachment_tmp_path = f'./attachment_tmp/{attachment["fileName"]}' + attachment_tmp_path = f'/tmp/attachment/{attachment["fileName"]}' attachment_path = subprocess.check_output(f'{quote(args.bw_path)} get attachment' f' --raw {quote(attachment["id"])} ' f'--itemid {quote(bw_item.get_id())} ' @@ -99,12 +99,39 @@ def check_args(args): return True +def environ_or_required(key): + return ( + {'default': os.environ.get(key)} if os.environ.get(key) + else {'required': True} + ) + + 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. If database does not exists it will be created.', 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') +parser.add_argument( + '--bw-session', + help='Session generated from bitwarden-cli (bw login)', + **environ_or_required('BW_SESSION'), +) +parser.add_argument( + '--database-path', + help='Path to KeePass database. If database does not exists it will be created.', + **environ_or_required('DATABASE_PATH'), +) +parser.add_argument( + '--database-password', + help='Password for KeePass database', + **environ_or_required('DATABASE_PASSWORD'), +) +parser.add_argument( + '--database-keyfile', + help='Path to Key File for KeePass database', + default=os.environ.get('DATABASE_KEYFILE', None), +) +parser.add_argument( + '--bw-path', + help='Path for bw binary', + default=os.environ.get('BW_PATH', 'bw'), +) args = parser.parse_args() check_args(args) and bitwarden_to_keepass(args) diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..1e6d654 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +version: '3.8' +services: + bitwarden-to-keepass: + build: . + command: bash -c 'export BW_SESSION=`$BW_PATH login --raw` && python3 bitwarden-to-keepass.py && $BW_PATH lock' + volumes: + - ./exports:/exports + tmpfs: + - /tmp + - '/root/.config/Bitwarden CLI' + env_file: + - .env diff --git a/exports/.gitkeep b/exports/.gitkeep new file mode 100644 index 0000000..e69de29