Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
minor edits to be rebased back into prev commits
Browse files Browse the repository at this point in the history
Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
pdxjohnny committed Oct 16, 2023
1 parent 8111557 commit 78c7d7f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 48 deletions.
51 changes: 48 additions & 3 deletions docs/federation_activitypub.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@
- [7.](https://www.ietf.org/archive/id/draft-ietf-scitt-architecture-02.html#name-federation)
- https://www.w3.org/TR/activitypub/

```mermaid
flowchart TD
subgraph alice
subgraph aliceSCITT[SCITT]
alice_submit_claim[Submit Claim]
alice_receipt_created[Receipt Created]
alice_submit_claim --> alice_receipt_created
end
subgraph aliceActivityPubActor[ActivityPub Actor]
alice_inbox[Inbox]
end
alice_inbox --> alice_submit_claim
end
subgraph bob
subgraph bobSCITT[SCITT]
bob_submit_claim[Submit Claim]
bob_receipt_created[Receipt Created]
bob_submit_claim --> bob_receipt_created
end
subgraph bobActivityPubActor[ActivityPub Actor]
bob_inbox[Inbox]
end
bob_inbox --> bob_submit_claim
end
alice_receipt_created --> bob_inbox
bob_receipt_created --> alice_inbox
```

## Dependencies

Install the SCITT API Emulator with the `federation-activitypub-bovine` extra.
Expand Down Expand Up @@ -51,6 +84,7 @@ By the end of this tutorial you will have four terminals open.
- One for submitting claims to Bob and Alice's SCITT instances and querying
their ActivityPub Actors.


### Bring up the ActivityPub Server

First we install our dependencies
Expand Down Expand Up @@ -91,14 +125,16 @@ BovineHerd(app)
We'll run on port 5000 to avoid collisions with common default port choices.
Keep this running for the rest of the tutorial.

> TODO Integrate Quart app launch into `SCITTFederationActivityPubBovine`
> **TODO** Integrate Quart app launch into `SCITTFederationActivityPubBovine`
> initialization.
```console
$ hypercorn app:app -b 0.0.0.0:5000
[2023-10-16 02:44:48 -0700] [36467] [INFO] Running on http://0.0.0.0:5000 (CTRL + C to quit)
```

> Cleanup: `rm -f *sqlite* federation_*/config.toml`
### Bring up Bob's SCITT Instance

Populate Bob's federation config
Expand All @@ -118,11 +154,19 @@ Start the server
```console
$ rm -rf workspace_bob/
$ mkdir -p workspace_bob/storage/operations
$ scitt-emulator server --workspace workspace_bob/ --tree-alg CCF --port 6000 \
$ BOVINE_DB_URL="sqlite://${PWD}/bovine.sqlite3" scitt-emulator server \
--workspace workspace_bob/ --tree-alg CCF --port 6000 \
--federation scitt_emulator.federation_activitypub_bovine:SCITTFederationActivityPubBovine \
--federation-config-path federation_bob/config.json
```

> **TODO** Figure out why the server was restarting constantly if in
> scitt-api-emulator directory (sqlite3?).
>
> ```console
> $ rm -f ~/Documents/fediverse/scitt_federation_bob/config.toml && BOVINE_DB_URL="sqlite://${HOME}/Documents/fediverse/bovine/hacking/bovine.sqlite3" scitt-emulator server --workspace workspace_bob/ --tree-alg CCF --port 6000 --federation scitt_emulator.federation_activitypub_bovine:SCITTFederationActivityPubBovine --federation-config-path ~/Documents/fediverse/scitt_federation_bob/config.json
> ```
Create claim from allowed issuer (`.org`) and from non-allowed (`.com`).
```console
Expand Down Expand Up @@ -157,7 +201,8 @@ Start the server
```console
$ rm -rf workspace_alice/
$ mkdir -p workspace_alice/storage/operations
$ scitt-emulator server --workspace workspace_alice/ --tree-alg CCF --port 7000 \
$ BOVINE_DB_URL="sqlite://${PWD}/bovine.sqlite3" scitt-emulator server \
--workspace workspace_alice/ --tree-alg CCF --port 7000 \
--federation scitt_emulator.federation_activitypub_bovine:SCITTFederationActivityPubBovine \
--federation-config-path federation_alice/config.json
```
Expand Down
92 changes: 49 additions & 43 deletions scitt_emulator/federation_activitypub_bovine.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
"""
import sys
import json
import atexit
import base64
import pprint
import socket
import inspect
import logging
import asyncio
import pathlib
import traceback
import subprocess
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -69,42 +72,21 @@ def __init__(

self.domain = self.config["domain"]
self.handle_name = self.config["handle_name"]
self.workspace = Path(self.config["workspace"])
self.workspace = Path(self.config["workspace"]).expanduser()

self.federate_created_entries_socket_path = self.workspace.joinpath(
"federate_created_entries_socket",
)

def initialize_service(self):
# read, self.write = multiprocessing.Pipe(duplex=False)
# reader_process = multiprocessing.Process(target=self.reader, args=(read,))

# TODO Avoid creating user if already exists
cmd = [
sys.executable,
"-um",
"mechanical_bull.add_user",
"--accept",
self.handle_name,
domain,
]
add_user_output = subprocess.check_output(
cmd,
cwd=self.workspace,
)
did_key = [
word.replace("did:key:", "")
for word in add_user_output.decode().strip().split()
if word.startswith("did:key:")
][0]

cmd = [
sys.executable,
"-um",
"bovine_tool.register",
self.handle_name,
"--domain",
domain,
self.domain,
]
register_output = subprocess.check_output(
cmd,
Expand All @@ -115,27 +97,44 @@ def initialize_service(self):
cmd = [
sys.executable,
"-um",
"bovine_tool.manage",
"mechanical_bull.add_user",
"--accept",
self.handle_name,
"--did_key",
"key0",
did_key,
self.domain,
]
subprocess.check_call(
cmd,
cwd=self.workspace,
)

# Enable handler() function in this file for this actor
config_toml_path = pathlib.Path(self.workspace, "config.toml")
config_toml_obj = toml.loads(config_toml_path.read_text())
did_key = bovine.crypto.private_key_to_did_key(
config_toml_obj[self.handle_name]["secret"]
)
config_toml_obj[self.handle_name]["handlers"][
inspect.getmodule(sys.modules[__name__]).__spec__.name
] = {
"federate_created_entries_socket_path": self.federate_created_entries_socket_path,
"federate_created_entries_socket_path": str(
self.federate_created_entries_socket_path.resolve()
),
}
config_toml_path.write_text(toml.dumps(config_toml_obj))

cmd = [
sys.executable,
"-um",
"bovine_tool.manage",
bovine_name,
"--did_key",
"key0",
did_key,
]
subprocess.check_call(
cmd,
cwd=self.workspace,
)

cmd = [
sys.executable,
"-um",
Expand All @@ -145,10 +144,11 @@ def initialize_service(self):
cmd,
cwd=self.workspace,
)
atexit.register(self.mechanical_bull_proc.terminate)

def created_entry(self, entry_id: str, receipt: bytes):
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(self.federate_created_entries_socket_path)
client.connect(str(self.federate_created_entries_socket_path.resolve()))
client.send(receipt)
client.close()

Expand Down Expand Up @@ -191,24 +191,30 @@ async def federate_created_entries(
socket_path: Path,
):
async def federate_created_entry(reader, writer):
receipt = await reader.read()
note = (
client.object_factory.note(
content=base64.b64encode(receipt),
try:
logger.info("federate_created_entry() Reading... %r", reader)
receipt = await reader.read()
logger.info("federate_created_entry() Read: %r", receipt)
note = (
client.object_factory.note(
content=base64.b64encode(receipt).decode(),
)
.as_public()
.build()
)
.as_public()
.build()
)
activity = client.activity_factory.create(note).build()
logger.info("Sending... %r", activity)
await client.send_to_outbox(activity)
activity = client.activity_factory.create(note).build()
logger.info("Sending... %r", activity)
await client.send_to_outbox(activity)

writer.close()
await writer.wait_closed()
writer.close()
await writer.wait_closed()
except:
logger.error(traceback.format_exc())

logger.info("Attempting UNIX bind at %r", socket_path)
server = await asyncio.start_unix_server(
federate_created_entry,
path=str(socket_path.resolve()),
path=str(Path(socket_path).resolve()),
)
async with server:
logger.info("Awaiting receipts to federate at %r", socket_path)
Expand Down
5 changes: 3 additions & 2 deletions scitt_emulator/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ def create_flask_app(config):

federation = None
if app.config.get("federation", None):
federation = app.config["federation"](
app.federation = app.config["federation"](
config_path=app.config.get("federation_config_path", None),
storage_path=storage_path,
service_parameters_path=app.service_parameters_path
)
app.federation.initialize_service()

app.scitt_service = clazz(
storage_path=storage_path,
service_parameters_path=app.service_parameters_path,
federation=federation,
federation=app.federation,
)
app.scitt_service.initialize_service()
print(f"Service parameters: {app.service_parameters_path}")
Expand Down

0 comments on commit 78c7d7f

Please sign in to comment.