From dfa6b6de3c15839ee4f87176234875c69ab0c948 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 4 Jul 2022 20:25:27 +0200 Subject: [PATCH] Fix config wizard --- app/activitypub.py | 2 +- app/httpsig.py | 4 ++-- app/key.py | 23 +++++++---------------- app/outgoing_activities.py | 6 ++++-- scripts/config_wizard.py | 10 ++++++---- 5 files changed, 20 insertions(+), 25 deletions(-) diff --git a/app/activitypub.py b/app/activitypub.py index 8d5b42d..ea8141e 100644 --- a/app/activitypub.py +++ b/app/activitypub.py @@ -89,7 +89,7 @@ ME = { "publicKey": { "id": f"{config.ID}#main-key", "owner": config.ID, - "publicKeyPem": get_pubkey_as_pem(), + "publicKeyPem": get_pubkey_as_pem(config.KEY_PATH), }, "alsoKnownAs": [], } diff --git a/app/httpsig.py b/app/httpsig.py index 3a10c88..f236656 100644 --- a/app/httpsig.py +++ b/app/httpsig.py @@ -23,10 +23,10 @@ from sqlalchemy import select from app import activitypub as ap from app import config +from app.config import KEY_PATH from app.database import AsyncSession from app.database import get_db_session from app.key import Key -from app.key import get_key _KEY_CACHE: MutableMapping[str, Key] = LFUCache(256) @@ -208,5 +208,5 @@ class HTTPXSigAuth(httpx.Auth): k = Key(config.ID, f"{config.ID}#main-key") -k.load(get_key()) +k.load(KEY_PATH.read_text()) auth = HTTPXSigAuth(k) diff --git a/app/key.py b/app/key.py index c0bb077..429446d 100644 --- a/app/key.py +++ b/app/key.py @@ -1,33 +1,24 @@ import base64 +from pathlib import Path from typing import Any from Crypto.PublicKey import RSA from Crypto.Util import number -from app.config import KEY_PATH - -def key_exists() -> bool: - return KEY_PATH.exists() - - -def generate_key() -> None: - if key_exists(): - raise ValueError(f"Key at {KEY_PATH} already exists") +def generate_key(key_path: Path) -> None: + if key_path.exists(): + raise ValueError(f"Key at {key_path} already exists") k = RSA.generate(2048) privkey_pem = k.exportKey("PEM").decode("utf-8") - KEY_PATH.write_text(privkey_pem) + key_path.write_text(privkey_pem) -def get_pubkey_as_pem() -> str: - text = KEY_PATH.read_text() +def get_pubkey_as_pem(key_path: Path) -> str: + text = key_path.read_text() return RSA.import_key(text).public_key().export_key("PEM").decode("utf-8") -def get_key() -> str: - return KEY_PATH.read_text() - - class Key(object): DEFAULT_KEY_SIZE = 2048 diff --git a/app/outgoing_activities.py b/app/outgoing_activities.py index 3903132..24f4d2a 100644 --- a/app/outgoing_activities.py +++ b/app/outgoing_activities.py @@ -14,16 +14,16 @@ from app import activitypub as ap from app import config from app import ldsig from app import models +from app.config import KEY_PATH from app.database import AsyncSession from app.database import SessionLocal from app.database import now from app.key import Key -from app.key import get_key _MAX_RETRIES = 16 k = Key(config.ID, f"{config.ID}#main-key") -k.load(get_key()) +k.load(KEY_PATH.read_text()) async def new_outgoing_activity( @@ -118,6 +118,8 @@ def process_next_outgoing_activity(db: Session) -> bool: if retry_after_value := http_error.response.headers.get("Retry-After"): retry_after = _parse_retry_after(retry_after_value) _set_next_try(next_activity, retry_after) + elif http_error.response.status_code == 401: + _set_next_try(next_activity) elif 400 <= http_error.response.status_code < 500: logger.info(f"status_code={http_error.response.status_code} not retrying") next_activity.is_errored = True diff --git a/scripts/config_wizard.py b/scripts/config_wizard.py index 4a2e1d1..3a8bdaa 100644 --- a/scripts/config_wizard.py +++ b/scripts/config_wizard.py @@ -10,22 +10,24 @@ from markdown import markdown # type: ignore from prompt_toolkit import prompt from app.key import generate_key -from app.key import key_exists + +_ROOT_DIR = Path().parent.resolve() +_KEY_PATH = _ROOT_DIR / "data" / "key.pem" def main() -> None: print("Welcome to microblog.pub setup wizard\n") print("Generating key...") - if key_exists(): + if _KEY_PATH.exists(): yn = "" while yn not in ["y", "n"]: yn = prompt( "WARNING, a key already exists, overwrite it? (y/n): ", default="n" ).lower() if yn == "y": - generate_key() + generate_key(_KEY_PATH) else: - generate_key() + generate_key(_KEY_PATH) config_file = Path("data/me.toml")