From dd2a87b5291acdf9c207d5e20eee603c6a1ab4ae Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 5 Dec 2026 22:09:29 +0100 Subject: WIP Somewhat blocked by https://bugzilla.redhat.com/show_bug.cgi?id=2521543 --- scripts/p7s/bitwarden.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'scripts/p7s/bitwarden.py') 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) -- cgit v1.2.3