From 0d91e58679c5743efd7a84d3f02454299894b229 Mon Sep 17 00:00:00 2001 From: John Andersen Date: Wed, 8 Nov 2023 11:39:57 +0100 Subject: [PATCH] federation activitypub bovine: Remove subprocess call to create mechanical_bull config.toml replace with Python Asciinema: https://asciinema.org/a/619871 Signed-off-by: John Andersen --- .../federation_activitypub_bovine.py | 75 +++++++++---------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/scitt_emulator/federation_activitypub_bovine.py b/scitt_emulator/federation_activitypub_bovine.py index 417bf287..c50d90b1 100644 --- a/scitt_emulator/federation_activitypub_bovine.py +++ b/scitt_emulator/federation_activitypub_bovine.py @@ -3,10 +3,12 @@ import json import enum import types +import pprint import atexit import base64 import socket import inspect +import tomllib import logging import asyncio import pathlib @@ -20,20 +22,16 @@ from pathlib import Path from typing import Optional -import tomli import tomli_w import bovine import aiohttp from bovine_store import BovineAdminStore from bovine_herd import BovineHerd from bovine_pubsub import BovinePubSub -from bovine.activitystreams import factories_for_actor_object from bovine.clients import lookup_uri_with_webfinger +from bovine.crypto import generate_ed25519_private_key, private_key_to_did_key from mechanical_bull.handlers import ( - HandlerEvent, - HandlerAPIVersion, load_handlers, - build_handler, ) from scitt_emulator.scitt import SCITTServiceEmulator @@ -43,8 +41,6 @@ logger = logging.getLogger(__name__) -import pprint - class SCITTFederationActivityPubBovine(SCITTFederation): def __init__(self, app, signals, config_path): @@ -86,21 +82,24 @@ async def initialize_service(self): config_toml_path = pathlib.Path(self.workspace, "config.toml") if not config_toml_path.exists(): logger.info("Actor client config does not exist, creating...") - cmd = [ - sys.executable, - "-um", - "mechanical_bull.add_user", - "--accept", - self.handle_name, - self.domain, - ] - subprocess.check_call( - cmd, - cwd=self.workspace, - ) - logger.info("Actor client config created") + private_key = generate_ed25519_private_key() + did_key = private_key_to_did_key(private_key) + config_toml_obj = { + self.handle_name: { + "secret": private_key, + "host": self.domain, + "domain": self.domain, + "handlers": { + "mechanical_bull.actions.accept_follow_request": True, + }, + }, + } + config_toml_path.write_text(tomli_w.dumps(config_toml_obj)) + logger.info("Actor client config.toml created") + else: + config_toml_obj = tomllib.loads(config_toml_path.read_text()) + did_key = config_toml_obj[self.handle_name]["secret"] - config_toml_obj = tomli.loads(config_toml_path.read_text()) # Enable handler() function in this file for this actor config_toml_obj[self.handle_name]["handlers"][ inspect.getmodule(sys.modules[__name__]).__spec__.name @@ -108,10 +107,6 @@ async def initialize_service(self): "signals": self.signals, "following": self.config.get("following", {}), } - # Extract public key from private key in config file - did_key = bovine.crypto.private_key_to_did_key( - config_toml_obj[self.handle_name]["secret"], - ) bovine_store = self.app.config["bovine_store"] _account, actor_url = await bovine_store.get_account_url_for_identity(did_key) @@ -134,24 +129,15 @@ async def initialize_service(self): ].get_account_url_for_identity(did_key) logger.info("Actor key added in database. actor_url is %s", actor_url) - # Run client handlers - async def mechanical_bull_loop(config): - try: - for client_name, client_config in config.items(): - if isinstance(client_config, dict): - handlers = load_handlers(client_config["handlers"]) - client_config["domain"] = client_config["host"] - self.app.add_background_task( - loop, client_name, client_config, handlers - ) - except Exception as e: - logger.exception(e) - # async with aiohttp.ClientSession(trust_env=True) as client_session: async with contextlib.AsyncExitStack() as async_exit_stack: # await mechanical_bull_loop(config_toml_obj) self.app.config["bovine_async_exit_stack"] = async_exit_stack - self.app.add_background_task(mechanical_bull_loop, config_toml_obj) + self.app.add_background_task( + mechanical_bull_loop, + config_toml_obj, + add_background_task=self.app.add_background_task, + ) yield @@ -473,3 +459,14 @@ async def loop(client_name, client_config, handlers): logger.exception(e) await asyncio.sleep(2**i) i += 1 + + +# Run client handlers using call_compat +async def mechanical_bull_loop(config, *, add_background_task=asyncio.create_task): + try: + for client_name, client_config in config.items(): + if isinstance(client_config, dict): + handlers = load_handlers(client_config["handlers"]) + add_background_task(loop, client_name, client_config, handlers) + except Exception as e: + logger.exception(e)