diff options
| author | alex <alex@pdp7.net> | 2026-12-05 22:09:29 +0100 |
|---|---|---|
| committer | alex <alex@pdp7.net> | 2026-12-06 13:23:04 +0100 |
| commit | dd2a87b5291acdf9c207d5e20eee603c6a1ab4ae (patch) | |
| tree | 92d699d7f2faee09ae0dfd337cef02b17d8284c9 /scripts/p7s/bitwarden.py | |
| parent | 6e6fc68fee5783e391d8b921ca44b90aeb67223c (diff) | |
WIP
Somewhat blocked by https://bugzilla.redhat.com/show_bug.cgi?id=2521543
Diffstat (limited to 'scripts/p7s/bitwarden.py')
| -rw-r--r-- | scripts/p7s/bitwarden.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/scripts/p7s/bitwarden.py b/scripts/p7s/bitwarden.py index ab6cc4f2..7818b26a 100644 --- a/scripts/p7s/bitwarden.py +++ b/scripts/p7s/bitwarden.py @@ -1,13 +1,19 @@ +import argparse import contextlib import json import os +import pathlib import subprocess +import tempfile +import urllib.request +import zipfile class Bitwarden(): @contextlib.contextmanager def login(self, server, email): - subprocess.run(["bw", "config", "server", server], check=True) + if subprocess.run(["bw", "config", "server"], check=True, text=True, stdout=subprocess.PIPE).stdout.strip() != server: + subprocess.run(["bw", "config", "server", server], check=True) status = self.status()["status"] if status == "unauthenticated": command = ["login", email] @@ -34,9 +40,40 @@ class Bitwarden(): def get_item(self, uuid): return json.loads(subprocess.run(["bw", "get", "item", uuid], check=True, stdout=subprocess.PIPE).stdout) + def get_attachment(self, attachment_id, item_id, destination): + subprocess.run(["bw", "get", "attachment", attachment_id, "--itemid", item_id, "--output", destination], check=True) def get_item(server, email, uuid): b = Bitwarden() with b.login(server, email): b.sync() return b.get_item(uuid) + + +@contextlib.contextmanager +def default(): + b = Bitwarden() + with b.login("https://vaultwarden.pdp7.net", "alex@corcoles.net"): + b.sync() + yield b + + +def install_cli(version): + with tempfile.TemporaryDirectory() as tempdir: + tempdir = pathlib.Path(tempdir) + zip = tempdir / "zip" + with urllib.request.urlopen(f"https://github.com/bitwarden/clients/releases/download/cli-v{version}/bw-oss-linux-{version}.zip") as urlopen: + zip.write_bytes(urlopen.read()) + with zipfile.ZipFile(zip) as zip: + with zip.open("bw") as bw: + bw_path = pathlib.Path.home() / ".local" / "bin" / "bw" + bw_path.unlink() + bw_path.write_bytes(bw.read()) + bw_path.chmod(0o555) + + +def install_cli_main(): + parser = argparse.ArgumentParser() + parser.add_argument("version") + args = parser.parse_args() + install_cli(args.version) |
