askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
exchange_admin.py · 3.6 KB · 73 lines
| 1 | """action_exchange_admin -- the operator's side of the exchange. |
| 2 | |
| 3 | POST {op, ...}. Only an admin or a user with the `owner` role (checked here |
| 4 | as well as by the execute rule, because every write below is a trusted |
| 5 | storage-level write and this object is the gate): |
| 6 | |
| 7 | - check: what this box needs to trade, each with its fix in plain words -- |
| 8 | the trading name, the key and where its backup is, the daemon and the |
| 9 | scheduler, the totals reactions, who holds the owner and manager roles, |
| 10 | partners waiting, packages missing. Changes nothing. |
| 11 | - setup {domain, company_name, document_types?}: name this box and make its |
| 12 | first key. The domain cannot be changed afterwards. |
| 13 | - record_key_backup {where}: say where the private key file is backed up |
| 14 | (the box cannot see it; the check reports it). |
| 15 | - rotate_key: a new active key vouched for by the old one; the old key is |
| 16 | listed as retired and its private half deleted. |
| 17 | - connect {domain}: fetch the partner's profile from its own domain, pin its |
| 18 | keys and send it a signed connection request. |
| 19 | - accept {partner_id}: accept a pending request (checks that the key it was |
| 20 | signed with is the one the requester's domain publishes), connect, and |
| 21 | tell the partner. Accepting again resends the acceptance. |
| 22 | - disconnect {partner_id, reason?}: refuse everything from this partner from |
| 23 | now on, and tell them (a signed connection_end; their box records us as |
| 24 | disconnected, with the reason). |
| 25 | - set_types {partner_id, receive_types?, send_types?}: which document types |
| 26 | may cross in each direction. |
| 27 | - remap {inbox_id}: try mapping a document that needs attention again. |
| 28 | - requeue {outbox_id}: send a document whose delivery stopped again. |
| 29 | """ |
| 30 | |
| 31 | import object_exchange |
| 32 | import object_exchange_box as box |
| 33 | |
| 34 | |
| 35 | def POST(request): |
| 36 | base = box.base_dir() |
| 37 | op = str(request.get("op") or "") |
| 38 | try: |
| 39 | access = box.require_manager(request.get("_identity"), base) |
| 40 | if op == "check": |
| 41 | return box.readiness(base) |
| 42 | if op == "record_key_backup": |
| 43 | return box.record_key_backup(base, str(request.get("where") or "")) |
| 44 | if op == "setup": |
| 45 | types = request.get("document_types") |
| 46 | if isinstance(types, str): |
| 47 | types = [t.strip() for t in types.split(",") if t.strip()] |
| 48 | return {"ok": True, "profile": box.setup( |
| 49 | base, domain=request.get("domain") or "", |
| 50 | company_name=request.get("company_name") or "", |
| 51 | document_types=types or None)} |
| 52 | if op == "rotate_key": |
| 53 | return box.rotate_key(base) |
| 54 | if op == "connect": |
| 55 | return box.connect(base, request.get("domain") or "", local_owner=access.user_id) |
| 56 | if op == "accept": |
| 57 | return box.accept(base, request.get("partner_id") or "", local_owner=access.user_id) |
| 58 | if op == "disconnect": |
| 59 | return box.disconnect(base, request.get("partner_id") or "", |
| 60 | reason=str(request.get("reason") or "")) |
| 61 | if op == "set_types": |
| 62 | return box.set_types(base, request.get("partner_id") or "", |
| 63 | receive_types=request.get("receive_types"), |
| 64 | send_types=request.get("send_types")) |
| 65 | if op == "remap": |
| 66 | return box.remap(base, request.get("inbox_id") or "") |
| 67 | if op == "requeue": |
| 68 | return box.requeue(base, request.get("outbox_id") or "") |
| 69 | return {"status": 400, "error": f"Unknown op {op!r}."} |
| 70 | except box.Refusal as refusal: |
| 71 | return refusal.answer() |
| 72 | except object_exchange.ExchangeUnavailable as exc: |
| 73 | return {"status": 409, "code": "exchange_unavailable", "error": str(exc)} |