Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ea0c070cfe |
@@ -0,0 +1,57 @@
|
|||||||
|
"""Consolidated registration inputs (PRD 0070, docker slice).
|
||||||
|
|
||||||
|
Bridges the existing per-bottle `prepare` output to the consolidated
|
||||||
|
registry: turns a prepared bottle's egress plan into the backend-neutral
|
||||||
|
inputs `Orchestrator.launch_bottle` takes — the egress **policy** blob and
|
||||||
|
launch **metadata**.
|
||||||
|
|
||||||
|
The policy blob is the exact routes YAML the per-bottle egress sidecar used
|
||||||
|
to read from a file; in the consolidated model the multi-tenant gateway's
|
||||||
|
`PolicyResolver` fetches it from the registry per request (keyed by source
|
||||||
|
IP) instead. Same render, so consolidated and single-tenant egress apply
|
||||||
|
byte-identical policy — a bottle's allow-list doesn't change when it moves
|
||||||
|
onto the shared gateway.
|
||||||
|
|
||||||
|
Host-side glue (imports `bot_bottle.egress`), used by the launch path — not
|
||||||
|
by the lean orchestrator control-plane process itself.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from ..egress import EgressPlan, egress_render_routes
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class RegistrationInputs:
|
||||||
|
"""What `Orchestrator.launch_bottle` needs to register a bottle, derived
|
||||||
|
from its prepared plan. `policy` is served verbatim by the gateway's
|
||||||
|
`/resolve`; `metadata` is opaque forward-compat state — it carries the
|
||||||
|
human slug so the console / supervise can show a name, not just the
|
||||||
|
minted bottle id."""
|
||||||
|
|
||||||
|
policy: str
|
||||||
|
metadata: str
|
||||||
|
|
||||||
|
|
||||||
|
def egress_policy(plan: EgressPlan) -> str:
|
||||||
|
"""The bottle's egress policy blob: the routes YAML the gateway serves
|
||||||
|
and the addon parses with `load_config`. Identical to the per-bottle
|
||||||
|
`routes.yaml` render, so the consolidated path applies the same
|
||||||
|
allow-list."""
|
||||||
|
return egress_render_routes(plan.routes, log=plan.log)
|
||||||
|
|
||||||
|
|
||||||
|
def registration_inputs(plan: EgressPlan) -> RegistrationInputs:
|
||||||
|
"""Assemble the orchestrator registration inputs from a prepared egress
|
||||||
|
plan. `metadata` records the slug so the shared registry can map a minted
|
||||||
|
bottle id back to its human name."""
|
||||||
|
return RegistrationInputs(
|
||||||
|
policy=egress_policy(plan),
|
||||||
|
metadata=json.dumps({"slug": plan.slug}),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["RegistrationInputs", "egress_policy", "registration_inputs"]
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
"""Unit: consolidated registration inputs — egress policy round-trip (PRD 0070)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from bot_bottle.egress import EgressPlan, EgressRoute
|
||||||
|
from bot_bottle.egress_addon_core import LOG_BLOCKS, load_config
|
||||||
|
from bot_bottle.orchestrator.registration import (
|
||||||
|
RegistrationInputs,
|
||||||
|
egress_policy,
|
||||||
|
registration_inputs,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _plan(routes: tuple[EgressRoute, ...], *, slug: str = "demo", log: int = 0) -> EgressPlan:
|
||||||
|
return EgressPlan(
|
||||||
|
slug=slug,
|
||||||
|
routes_path=Path("/unused/routes.yaml"),
|
||||||
|
routes=routes,
|
||||||
|
token_env_map={},
|
||||||
|
log=log,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestEgressPolicy(unittest.TestCase):
|
||||||
|
def test_policy_round_trips_through_load_config(self) -> None:
|
||||||
|
# The policy the gateway serves must parse back to the same allow-list
|
||||||
|
# the per-bottle sidecar applied — moving onto the shared gateway must
|
||||||
|
# not change a bottle's egress.
|
||||||
|
routes = (EgressRoute(host="api.example.com"), EgressRoute(host="pypi.org"))
|
||||||
|
cfg = load_config(egress_policy(_plan(routes)))
|
||||||
|
self.assertEqual(("api.example.com", "pypi.org"), tuple(r.host for r in cfg.routes))
|
||||||
|
|
||||||
|
def test_policy_preserves_log_level(self) -> None:
|
||||||
|
plan = _plan((EgressRoute(host="x.example.com"),), log=LOG_BLOCKS)
|
||||||
|
self.assertEqual(LOG_BLOCKS, load_config(egress_policy(plan)).log)
|
||||||
|
|
||||||
|
def test_empty_routes_yield_deny_all(self) -> None:
|
||||||
|
cfg = load_config(egress_policy(_plan(())))
|
||||||
|
self.assertEqual((), cfg.routes) # no routes → default-deny
|
||||||
|
|
||||||
|
|
||||||
|
class TestRegistrationInputs(unittest.TestCase):
|
||||||
|
def test_bundles_policy_and_slug_metadata(self) -> None:
|
||||||
|
plan = _plan((EgressRoute(host="api.example.com"),), slug="my-bot")
|
||||||
|
inputs = registration_inputs(plan)
|
||||||
|
self.assertIsInstance(inputs, RegistrationInputs)
|
||||||
|
self.assertEqual("my-bot", json.loads(inputs.metadata)["slug"])
|
||||||
|
self.assertEqual(egress_policy(plan), inputs.policy) # same blob egress_policy renders
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user