Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 88f58bf4c0 | |||
| ca0dc72b89 | |||
| 2fc99ea098 | |||
| 9a9235f2af | |||
| 42f79283f0 | |||
| d6b9d7af3e | |||
| 0f72843150 | |||
| fd6b14fb32 | |||
| 9f9aa2e762 | |||
| 454baaf3a1 |
@@ -0,0 +1,9 @@
|
|||||||
|
[run]
|
||||||
|
branch = True
|
||||||
|
source = .
|
||||||
|
|
||||||
|
[report]
|
||||||
|
omit =
|
||||||
|
bot_bottle/egress_addon.py
|
||||||
|
bot_bottle/cli/tui.py
|
||||||
|
tests/*
|
||||||
@@ -39,8 +39,14 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
python-version: "3.12"
|
python-version: "3.12"
|
||||||
|
|
||||||
|
- name: Install dev requirements
|
||||||
|
run: python3 -m pip install -r requirements-dev.txt
|
||||||
|
|
||||||
- name: Run unit tests
|
- name: Run unit tests
|
||||||
run: python3 -m unittest discover -t . -s tests/unit -v
|
run: python3 -m coverage run -m unittest discover -t . -s tests/unit -v
|
||||||
|
|
||||||
|
- name: Report unit coverage
|
||||||
|
run: python3 -m coverage report -m
|
||||||
|
|
||||||
integration:
|
integration:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
@@ -22,3 +22,4 @@ venv/
|
|||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
.ruff_cache/
|
.ruff_cache/
|
||||||
|
.coverage
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from ..bottle_state import egress_state_dir
|
from ..bottle_state import egress_state_dir
|
||||||
from ..egress import EGRESS_ROUTES_FILENAME
|
from ..egress import EGRESS_ROUTES_FILENAME
|
||||||
from ..egress_addon_core import load_routes
|
from ..egress_addon_core import LOG_OFF, load_config
|
||||||
|
|
||||||
|
|
||||||
class EgressApplyError(RuntimeError):
|
class EgressApplyError(RuntimeError):
|
||||||
@@ -33,11 +33,15 @@ class EgressApplicator(ABC):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def validate_routes_content(content: str) -> None:
|
def validate_routes_content(content: str) -> None:
|
||||||
try:
|
try:
|
||||||
load_routes(content)
|
config = load_config(content)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise EgressApplyError(
|
raise EgressApplyError(
|
||||||
f"proposed routes.yaml is not valid: {e}"
|
f"proposed routes.yaml is not valid: {e}"
|
||||||
) from e
|
) from e
|
||||||
|
if config.log != LOG_OFF:
|
||||||
|
raise EgressApplyError(
|
||||||
|
"proposed routes.yaml must not change egress logging"
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _routes_path(slug: str) -> Path:
|
def _routes_path(slug: str) -> Path:
|
||||||
|
|||||||
@@ -68,6 +68,11 @@ def build_image(ref: str, context: str, *, dockerfile: str = "") -> None:
|
|||||||
_ensure_builder_dns()
|
_ensure_builder_dns()
|
||||||
args = [_CONTAINER, "build", "-t", ref, "--dns", dns_server()]
|
args = [_CONTAINER, "build", "-t", ref, "--dns", dns_server()]
|
||||||
if dockerfile:
|
if dockerfile:
|
||||||
|
# `container build` resolves -f relative to the current working
|
||||||
|
# directory, not the build context. Anchor a relative Dockerfile to
|
||||||
|
# the context so builds work from any cwd.
|
||||||
|
if not os.path.isabs(dockerfile):
|
||||||
|
dockerfile = os.path.join(context, dockerfile)
|
||||||
args.extend(["-f", dockerfile])
|
args.extend(["-f", dockerfile])
|
||||||
args.append(context)
|
args.append(context)
|
||||||
subprocess.run(args, check=True)
|
subprocess.run(args, check=True)
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
act on them (approve / modify / reject).
|
act on them (approve / modify / reject).
|
||||||
|
|
||||||
Curses-based TUI; modify-then-approve shells out to $EDITOR. The
|
Curses-based TUI; modify-then-approve shells out to $EDITOR. The
|
||||||
approval handler wires to PRD 0016 (capability-block), which rebuilds
|
Egress proposals are queued for operator review as full routes.yaml
|
||||||
the bottle Dockerfile. Egress proposals are queued for operator review
|
updates.
|
||||||
as full routes.yaml updates.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@@ -22,10 +21,6 @@ from pathlib import Path
|
|||||||
|
|
||||||
from .. import supervise as _supervise
|
from .. import supervise as _supervise
|
||||||
from ..bottle_state import read_metadata
|
from ..bottle_state import read_metadata
|
||||||
# from ..backend.docker.capability_apply import (
|
|
||||||
# CapabilityApplyError,
|
|
||||||
# apply_capability_change,
|
|
||||||
# )
|
|
||||||
from ..backend.docker.egress_apply import (
|
from ..backend.docker.egress_apply import (
|
||||||
EgressApplyError,
|
EgressApplyError,
|
||||||
applicator as _docker_applicator,
|
applicator as _docker_applicator,
|
||||||
@@ -38,10 +33,6 @@ from ..backend.smolmachines.egress_apply import (
|
|||||||
)
|
)
|
||||||
from ..log import Die, error, info
|
from ..log import Die, error, info
|
||||||
|
|
||||||
|
|
||||||
class CapabilityApplyError(RuntimeError):
|
|
||||||
"""Placeholder while capability_apply is disabled."""
|
|
||||||
|
|
||||||
from ..supervise import (
|
from ..supervise import (
|
||||||
COMPONENT_FOR_TOOL,
|
COMPONENT_FOR_TOOL,
|
||||||
AuditEntry,
|
AuditEntry,
|
||||||
@@ -50,12 +41,10 @@ from ..supervise import (
|
|||||||
STATUS_APPROVED,
|
STATUS_APPROVED,
|
||||||
STATUS_MODIFIED,
|
STATUS_MODIFIED,
|
||||||
STATUS_REJECTED,
|
STATUS_REJECTED,
|
||||||
TOOL_CAPABILITY_BLOCK,
|
|
||||||
TOOL_EGRESS_ALLOW,
|
TOOL_EGRESS_ALLOW,
|
||||||
TOOL_EGRESS_BLOCK,
|
TOOL_EGRESS_BLOCK,
|
||||||
TOOL_GITLEAKS_ALLOW,
|
TOOL_GITLEAKS_ALLOW,
|
||||||
TOOL_EGRESS_TOKEN_ALLOW,
|
TOOL_EGRESS_TOKEN_ALLOW,
|
||||||
archive_proposal,
|
|
||||||
list_pending_proposals,
|
list_pending_proposals,
|
||||||
render_diff,
|
render_diff,
|
||||||
write_audit_entry,
|
write_audit_entry,
|
||||||
@@ -83,7 +72,7 @@ class QueuedProposal:
|
|||||||
# Errors any remediation engine may raise. Caught by the TUI key
|
# Errors any remediation engine may raise. Caught by the TUI key
|
||||||
# handlers and surfaced in the status line so a failed apply keeps
|
# handlers and surfaced in the status line so a failed apply keeps
|
||||||
# the proposal pending rather than crashing curses.
|
# the proposal pending rather than crashing curses.
|
||||||
ApplyError = (CapabilityApplyError, EgressApplyError)
|
ApplyError = (EgressApplyError,)
|
||||||
|
|
||||||
|
|
||||||
def apply_routes_change(slug: str, content: str) -> tuple[str, str]:
|
def apply_routes_change(slug: str, content: str) -> tuple[str, str]:
|
||||||
@@ -143,8 +132,6 @@ def _detail_lines(
|
|||||||
|
|
||||||
|
|
||||||
def _suffix_for_tool(tool: str) -> str:
|
def _suffix_for_tool(tool: str) -> str:
|
||||||
if tool == TOOL_CAPABILITY_BLOCK:
|
|
||||||
return ".dockerfile"
|
|
||||||
if tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
if tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
||||||
return ".yaml"
|
return ".yaml"
|
||||||
if tool in (TOOL_GITLEAKS_ALLOW, TOOL_EGRESS_TOKEN_ALLOW):
|
if tool in (TOOL_GITLEAKS_ALLOW, TOOL_EGRESS_TOKEN_ALLOW):
|
||||||
@@ -166,17 +153,6 @@ def approve(
|
|||||||
file_to_apply = final_file if final_file is not None else qp.proposal.proposed_file
|
file_to_apply = final_file if final_file is not None else qp.proposal.proposed_file
|
||||||
|
|
||||||
diff_before, diff_after = "", ""
|
diff_before, diff_after = "", ""
|
||||||
# if qp.proposal.tool == TOOL_CAPABILITY_BLOCK:
|
|
||||||
# _meta = read_metadata(qp.proposal.bottle_slug)
|
|
||||||
# if _meta is not None and not _meta.compose_project:
|
|
||||||
# raise CapabilityApplyError(
|
|
||||||
# "capability-block remediation is not supported for smolmachines "
|
|
||||||
# "bottles. Reject this proposal or handle the capability change "
|
|
||||||
# "manually, then restart the bottle."
|
|
||||||
# )
|
|
||||||
# diff_before, diff_after = apply_capability_change(
|
|
||||||
# qp.proposal.bottle_slug, file_to_apply,
|
|
||||||
# )
|
|
||||||
if qp.proposal.tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
if qp.proposal.tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
||||||
diff_before, diff_after = apply_routes_change(
|
diff_before, diff_after = apply_routes_change(
|
||||||
qp.proposal.bottle_slug,
|
qp.proposal.bottle_slug,
|
||||||
@@ -194,9 +170,6 @@ def approve(
|
|||||||
qp, action=status, notes=notes,
|
qp, action=status, notes=notes,
|
||||||
diff_before=diff_before, diff_after=diff_after,
|
diff_before=diff_before, diff_after=diff_after,
|
||||||
)
|
)
|
||||||
if qp.proposal.tool == TOOL_CAPABILITY_BLOCK:
|
|
||||||
archive_proposal(qp.queue_dir, qp.proposal.id)
|
|
||||||
|
|
||||||
|
|
||||||
def reject(qp: QueuedProposal, *, reason: str) -> None:
|
def reject(qp: QueuedProposal, *, reason: str) -> None:
|
||||||
"""Write a rejection response and an audit entry."""
|
"""Write a rejection response and an audit entry."""
|
||||||
@@ -346,7 +319,7 @@ def _list_once() -> int:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def _try_init_green() -> int:
|
def _try_init_green() -> int: # pragma: no cover
|
||||||
"""Initialise a green color pair and return its attr, or 0."""
|
"""Initialise a green color pair and return its attr, or 0."""
|
||||||
try:
|
try:
|
||||||
curses.start_color()
|
curses.start_color()
|
||||||
@@ -357,7 +330,7 @@ def _try_init_green() -> int:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def _main_loop(stdscr: "curses._CursesWindow") -> None: # type: ignore
|
def _main_loop(stdscr: "curses._CursesWindow") -> None: # type: ignore # pragma: no cover
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
stdscr.timeout(_REFRESH_INTERVAL_MS)
|
stdscr.timeout(_REFRESH_INTERVAL_MS)
|
||||||
green_attr = _try_init_green()
|
green_attr = _try_init_green()
|
||||||
@@ -447,7 +420,7 @@ def _render(
|
|||||||
status_line: str,
|
status_line: str,
|
||||||
*,
|
*,
|
||||||
green_attr: int = 0, # noqa: F841 — unused, but required by interface
|
green_attr: int = 0, # noqa: F841 — unused, but required by interface
|
||||||
) -> None:
|
) -> None: # pragma: no cover
|
||||||
stdscr.erase()
|
stdscr.erase()
|
||||||
h, w = stdscr.getmaxyx()
|
h, w = stdscr.getmaxyx()
|
||||||
header = f"bot-bottle supervise ({len(pending)} pending)"
|
header = f"bot-bottle supervise ({len(pending)} pending)"
|
||||||
@@ -498,7 +471,7 @@ def _detail_view(
|
|||||||
qp: QueuedProposal,
|
qp: QueuedProposal,
|
||||||
*,
|
*,
|
||||||
green_attr: int = 0,
|
green_attr: int = 0,
|
||||||
) -> None:
|
) -> None: # pragma: no cover
|
||||||
"""Render the full proposal. Scrollable. Press q to return."""
|
"""Render the full proposal. Scrollable. Press q to return."""
|
||||||
lines = _detail_lines(qp, green_attr=green_attr)
|
lines = _detail_lines(qp, green_attr=green_attr)
|
||||||
offset = 0
|
offset = 0
|
||||||
@@ -550,7 +523,7 @@ def _detail_view(
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def _modify(stdscr: "curses._CursesWindow", qp: QueuedProposal) -> str | None: # type: ignore
|
def _modify(stdscr: "curses._CursesWindow", qp: QueuedProposal) -> str | None: # type: ignore # pragma: no cover
|
||||||
"""Suspend curses, open $EDITOR on the proposed file, return edited content."""
|
"""Suspend curses, open $EDITOR on the proposed file, return edited content."""
|
||||||
suffix = _suffix_for_tool(qp.proposal.tool)
|
suffix = _suffix_for_tool(qp.proposal.tool)
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
@@ -561,7 +534,7 @@ def _modify(stdscr: "curses._CursesWindow", qp: QueuedProposal) -> str | None:
|
|||||||
return edited
|
return edited
|
||||||
|
|
||||||
|
|
||||||
def _prompt(stdscr: "curses._CursesWindow", label: str) -> str: # type: ignore
|
def _prompt(stdscr: "curses._CursesWindow", label: str) -> str: # type: ignore # pragma: no cover
|
||||||
"""One-line input at the bottom of the screen."""
|
"""One-line input at the bottom of the screen."""
|
||||||
curses.curs_set(1)
|
curses.curs_set(1)
|
||||||
h, _ = stdscr.getmaxyx()
|
h, _ = stdscr.getmaxyx()
|
||||||
|
|||||||
@@ -439,15 +439,6 @@ def route_to_yaml_dict(r: Route) -> dict[str, object]:
|
|||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def load_routes(text: str) -> tuple[Route, ...]:
|
|
||||||
"""Parse YAML text → routes."""
|
|
||||||
try:
|
|
||||||
payload = parse_yaml_subset(text)
|
|
||||||
except YamlSubsetError as e:
|
|
||||||
raise ValueError(f"routes payload: invalid YAML: {e}") from e
|
|
||||||
return parse_routes(payload)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_config(payload: object) -> "Config":
|
def parse_config(payload: object) -> "Config":
|
||||||
"""Parse a full egress config payload (top-level log level + routes)."""
|
"""Parse a full egress config payload (top-level log level + routes)."""
|
||||||
if not isinstance(payload, dict):
|
if not isinstance(payload, dict):
|
||||||
@@ -862,7 +853,6 @@ __all__ = [
|
|||||||
"is_git_push_request",
|
"is_git_push_request",
|
||||||
"is_git_fetch_request",
|
"is_git_fetch_request",
|
||||||
"load_config",
|
"load_config",
|
||||||
"load_routes",
|
|
||||||
"match_route",
|
"match_route",
|
||||||
"outbound_scan_headers",
|
"outbound_scan_headers",
|
||||||
"parse_config",
|
"parse_config",
|
||||||
|
|||||||
@@ -47,11 +47,11 @@ from pathlib import Path
|
|||||||
try:
|
try:
|
||||||
# Same-directory imports inside the bundle container; these files are
|
# Same-directory imports inside the bundle container; these files are
|
||||||
# COPYed flat under /app by Dockerfile.sidecars.
|
# COPYed flat under /app by Dockerfile.sidecars.
|
||||||
from egress_addon_core import load_routes
|
from egress_addon_core import LOG_OFF, load_config
|
||||||
import supervise as _sv
|
import supervise as _sv
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
# Package imports for host-side tests and tooling.
|
# Package imports for host-side tests and tooling.
|
||||||
from .egress_addon_core import load_routes
|
from .egress_addon_core import LOG_OFF, load_config
|
||||||
from . import supervise as _sv
|
from . import supervise as _sv
|
||||||
|
|
||||||
|
|
||||||
@@ -297,12 +297,17 @@ def validate_proposed_file(tool: str, content: str) -> None:
|
|||||||
pass
|
pass
|
||||||
elif tool in (_sv.TOOL_EGRESS_ALLOW, _sv.TOOL_EGRESS_BLOCK):
|
elif tool in (_sv.TOOL_EGRESS_ALLOW, _sv.TOOL_EGRESS_BLOCK):
|
||||||
try:
|
try:
|
||||||
load_routes(content)
|
config = load_config(content)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise _RpcError(
|
raise _RpcError(
|
||||||
ERR_INVALID_PARAMS,
|
ERR_INVALID_PARAMS,
|
||||||
f"{tool}: proposed routes.yaml is not valid: {e}",
|
f"{tool}: proposed routes.yaml is not valid: {e}",
|
||||||
) from e
|
) from e
|
||||||
|
if config.log != LOG_OFF:
|
||||||
|
raise _RpcError(
|
||||||
|
ERR_INVALID_PARAMS,
|
||||||
|
f"{tool}: proposed routes.yaml must not change egress logging",
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise _RpcError(ERR_INVALID_PARAMS, f"unknown tool {tool!r}")
|
raise _RpcError(ERR_INVALID_PARAMS, f"unknown tool {tool!r}")
|
||||||
|
|
||||||
|
|||||||
@@ -4,3 +4,4 @@
|
|||||||
|
|
||||||
pylint>=3.0.0
|
pylint>=3.0.0
|
||||||
pyright>=1.1.300
|
pyright>=1.1.300
|
||||||
|
coverage>=7.0.0
|
||||||
|
|||||||
@@ -92,9 +92,9 @@ class TestSandboxEscape(unittest.TestCase):
|
|||||||
"on PATH: curl -sSL https://smolmachines.com/install.sh | sh"
|
"on PATH: curl -sSL https://smolmachines.com/install.sh | sh"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Throwaway "identity file" for the git-gate's `identity` field.
|
# Throwaway static key for the git-gate fixture. It need not
|
||||||
# It need not be a real SSH key: test 5 reaches gitleaks before
|
# be a real SSH key: test 5 reaches gitleaks before any SSH
|
||||||
# any SSH attempt anyway.
|
# attempt anyway.
|
||||||
fd, kp = tempfile.mkstemp(prefix="sandbox-test-key.")
|
fd, kp = tempfile.mkstemp(prefix="sandbox-test-key.")
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
cls._key_path = Path(kp)
|
cls._key_path = Path(kp)
|
||||||
@@ -123,7 +123,10 @@ class TestSandboxEscape(unittest.TestCase):
|
|||||||
"git-gate": {"repos": {
|
"git-gate": {"repos": {
|
||||||
"throwaway": {
|
"throwaway": {
|
||||||
"url": "ssh://git@unreachable.invalid:22/throwaway.git",
|
"url": "ssh://git@unreachable.invalid:22/throwaway.git",
|
||||||
"identity": str(cls._key_path),
|
"key": {
|
||||||
|
"provider": "static",
|
||||||
|
"path": str(cls._key_path),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -198,6 +198,7 @@ class TestSmolmachinesLaunch(unittest.TestCase):
|
|||||||
# connect fails, which is the property chunk 3 will
|
# connect fails, which is the property chunk 3 will
|
||||||
# preserve once egress is actually running.
|
# preserve once egress is actually running.
|
||||||
r = self.bottle.exec(
|
r = self.bottle.exec(
|
||||||
|
"env -u HTTPS_PROXY -u HTTP_PROXY -u https_proxy -u http_proxy "
|
||||||
f"curl -s --show-error --max-time 3 http://{self.plan.bundle_ip}:9099 "
|
f"curl -s --show-error --max-time 3 http://{self.plan.bundle_ip}:9099 "
|
||||||
"2>&1 || true"
|
"2>&1 || true"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -322,7 +322,7 @@ class TestRenderRoutes(unittest.TestCase):
|
|||||||
self.assertEqual([], parse_yaml_subset(rendered)["routes"])
|
self.assertEqual([], parse_yaml_subset(rendered)["routes"])
|
||||||
|
|
||||||
def test_round_trip_through_addon_core(self):
|
def test_round_trip_through_addon_core(self):
|
||||||
from bot_bottle.egress_addon_core import load_routes
|
from bot_bottle.egress_addon_core import load_config
|
||||||
b = _bottle([
|
b = _bottle([
|
||||||
{"host": "api.github.com",
|
{"host": "api.github.com",
|
||||||
"auth": {"scheme": "Bearer", "token_ref": "GH_PAT"},
|
"auth": {"scheme": "Bearer", "token_ref": "GH_PAT"},
|
||||||
@@ -333,7 +333,7 @@ class TestRenderRoutes(unittest.TestCase):
|
|||||||
{"host": "api.anthropic.com"},
|
{"host": "api.anthropic.com"},
|
||||||
])
|
])
|
||||||
routes = egress_routes_for_bottle(b)
|
routes = egress_routes_for_bottle(b)
|
||||||
addon_routes = load_routes(egress_render_routes(routes))
|
addon_routes = load_config(egress_render_routes(routes)).routes
|
||||||
self.assertEqual(3, len(addon_routes))
|
self.assertEqual(3, len(addon_routes))
|
||||||
self.assertEqual("Bearer", addon_routes[0].auth_scheme)
|
self.assertEqual("Bearer", addon_routes[0].auth_scheme)
|
||||||
self.assertEqual("EGRESS_TOKEN_0", addon_routes[0].token_env)
|
self.assertEqual("EGRESS_TOKEN_0", addon_routes[0].token_env)
|
||||||
@@ -341,26 +341,26 @@ class TestRenderRoutes(unittest.TestCase):
|
|||||||
self.assertEqual("", addon_routes[2].auth_scheme)
|
self.assertEqual("", addon_routes[2].auth_scheme)
|
||||||
|
|
||||||
def test_dlp_round_trips(self):
|
def test_dlp_round_trips(self):
|
||||||
from bot_bottle.egress_addon_core import load_routes
|
from bot_bottle.egress_addon_core import load_config
|
||||||
b = _bottle([{"host": "x.example", "dlp": {
|
b = _bottle([{"host": "x.example", "dlp": {
|
||||||
"outbound_detectors": ["token_patterns"],
|
"outbound_detectors": ["token_patterns"],
|
||||||
"inbound_detectors": False,
|
"inbound_detectors": False,
|
||||||
}}])
|
}}])
|
||||||
routes = egress_routes_for_bottle(b)
|
routes = egress_routes_for_bottle(b)
|
||||||
rendered = egress_render_routes(routes)
|
rendered = egress_render_routes(routes)
|
||||||
addon_routes = load_routes(rendered)
|
addon_routes = load_config(rendered).routes
|
||||||
self.assertEqual(("token_patterns",), addon_routes[0].outbound_detectors)
|
self.assertEqual(("token_patterns",), addon_routes[0].outbound_detectors)
|
||||||
self.assertEqual((), addon_routes[0].inbound_detectors)
|
self.assertEqual((), addon_routes[0].inbound_detectors)
|
||||||
|
|
||||||
def test_outbound_on_match_round_trips(self):
|
def test_outbound_on_match_round_trips(self):
|
||||||
from bot_bottle.egress_addon_core import load_routes
|
from bot_bottle.egress_addon_core import load_config
|
||||||
b = _bottle([{"host": "logs.example", "dlp": {
|
b = _bottle([{"host": "logs.example", "dlp": {
|
||||||
"outbound_on_match": "redact",
|
"outbound_on_match": "redact",
|
||||||
}}])
|
}}])
|
||||||
routes = egress_routes_for_bottle(b)
|
routes = egress_routes_for_bottle(b)
|
||||||
rendered = egress_render_routes(routes)
|
rendered = egress_render_routes(routes)
|
||||||
self.assertIn('outbound_on_match: "redact"', rendered)
|
self.assertIn('outbound_on_match: "redact"', rendered)
|
||||||
addon_routes = load_routes(rendered)
|
addon_routes = load_config(rendered).routes
|
||||||
self.assertEqual("redact", addon_routes[0].outbound_on_match)
|
self.assertEqual("redact", addon_routes[0].outbound_on_match)
|
||||||
|
|
||||||
def test_outbound_on_match_default_omitted_from_render(self):
|
def test_outbound_on_match_default_omitted_from_render(self):
|
||||||
@@ -370,12 +370,12 @@ class TestRenderRoutes(unittest.TestCase):
|
|||||||
self.assertNotIn("outbound_on_match", rendered)
|
self.assertNotIn("outbound_on_match", rendered)
|
||||||
|
|
||||||
def test_git_fetch_policy_round_trips(self):
|
def test_git_fetch_policy_round_trips(self):
|
||||||
from bot_bottle.egress_addon_core import load_routes
|
from bot_bottle.egress_addon_core import load_config
|
||||||
b = _bottle([{"host": "github.com", "git": {"fetch": True}}])
|
b = _bottle([{"host": "github.com", "git": {"fetch": True}}])
|
||||||
routes = egress_routes_for_bottle(b)
|
routes = egress_routes_for_bottle(b)
|
||||||
rendered = egress_render_routes(routes)
|
rendered = egress_render_routes(routes)
|
||||||
self.assertEqual({"fetch": True}, self._parsed(routes)[0]["git"])
|
self.assertEqual({"fetch": True}, self._parsed(routes)[0]["git"])
|
||||||
addon_routes = load_routes(rendered)
|
addon_routes = load_config(rendered).routes
|
||||||
self.assertTrue(addon_routes[0].git_fetch)
|
self.assertTrue(addon_routes[0].git_fetch)
|
||||||
|
|
||||||
def test_log_zero_omitted_from_render(self):
|
def test_log_zero_omitted_from_render(self):
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ from bot_bottle.egress_addon_core import (
|
|||||||
is_git_fetch_request,
|
is_git_fetch_request,
|
||||||
is_git_push_request,
|
is_git_push_request,
|
||||||
load_config,
|
load_config,
|
||||||
load_routes,
|
|
||||||
match_route,
|
match_route,
|
||||||
outbound_scan_headers,
|
outbound_scan_headers,
|
||||||
parse_config,
|
parse_config,
|
||||||
@@ -289,47 +288,6 @@ class TestParseDlp(unittest.TestCase):
|
|||||||
}]})
|
}]})
|
||||||
|
|
||||||
|
|
||||||
# --- load_routes ---------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
class TestLoadRoutes(unittest.TestCase):
|
|
||||||
def test_yaml_text_round_trip(self):
|
|
||||||
routes = load_routes(
|
|
||||||
'routes:\n'
|
|
||||||
' - host: "api.example"\n'
|
|
||||||
)
|
|
||||||
self.assertEqual(1, len(routes))
|
|
||||||
self.assertEqual("api.example", routes[0].host)
|
|
||||||
|
|
||||||
def test_full_route_shape_parses(self):
|
|
||||||
routes = load_routes(
|
|
||||||
'routes:\n'
|
|
||||||
' - host: "api.example"\n'
|
|
||||||
' auth_scheme: "Bearer"\n'
|
|
||||||
' token_env: "EGRESS_TOKEN_0"\n'
|
|
||||||
' matches:\n'
|
|
||||||
' - paths:\n'
|
|
||||||
' - value: "/v1/"\n'
|
|
||||||
' - type: "exact"\n'
|
|
||||||
' value: "/messages"\n'
|
|
||||||
)
|
|
||||||
self.assertEqual(1, len(routes))
|
|
||||||
r = routes[0]
|
|
||||||
self.assertEqual("api.example", r.host)
|
|
||||||
self.assertEqual("Bearer", r.auth_scheme)
|
|
||||||
self.assertEqual("EGRESS_TOKEN_0", r.token_env)
|
|
||||||
self.assertEqual(1, len(r.matches))
|
|
||||||
self.assertEqual(2, len(r.matches[0].paths))
|
|
||||||
|
|
||||||
def test_empty_routes_list(self):
|
|
||||||
routes = load_routes("routes: []\n")
|
|
||||||
self.assertEqual((), routes)
|
|
||||||
|
|
||||||
def test_invalid_yaml_raises_value_error(self):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
load_routes("routes:\n\t- host: x\n")
|
|
||||||
|
|
||||||
|
|
||||||
# --- load_config / parse_config ------------------------------------------
|
# --- load_config / parse_config ------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
@@ -378,6 +336,33 @@ class TestLoadConfig(unittest.TestCase):
|
|||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
parse_config("not a dict")
|
parse_config("not a dict")
|
||||||
|
|
||||||
|
def test_empty_routes_list(self):
|
||||||
|
cfg = load_config("routes: []\n")
|
||||||
|
self.assertEqual((), cfg.routes)
|
||||||
|
|
||||||
|
def test_full_route_shape_parses(self):
|
||||||
|
cfg = load_config(
|
||||||
|
'routes:\n'
|
||||||
|
' - host: "api.example"\n'
|
||||||
|
' auth_scheme: "Bearer"\n'
|
||||||
|
' token_env: "EGRESS_TOKEN_0"\n'
|
||||||
|
' matches:\n'
|
||||||
|
' - paths:\n'
|
||||||
|
' - value: "/v1/"\n'
|
||||||
|
' - type: "exact"\n'
|
||||||
|
' value: "/messages"\n'
|
||||||
|
)
|
||||||
|
r = cfg.routes[0]
|
||||||
|
self.assertEqual("api.example", r.host)
|
||||||
|
self.assertEqual("Bearer", r.auth_scheme)
|
||||||
|
self.assertEqual("EGRESS_TOKEN_0", r.token_env)
|
||||||
|
self.assertEqual(1, len(r.matches))
|
||||||
|
self.assertEqual(2, len(r.matches[0].paths))
|
||||||
|
|
||||||
|
def test_invalid_yaml_raises_value_error(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
load_config("routes:\n\t- host: x\n")
|
||||||
|
|
||||||
|
|
||||||
# --- evaluate_matches ---------------------------------------------------
|
# --- evaluate_matches ---------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,15 @@ class TestValidateRoutesContent(unittest.TestCase):
|
|||||||
' auth_scheme: "Bearer"\n'
|
' auth_scheme: "Bearer"\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_rejects_log_full(self):
|
||||||
|
with self.assertRaises(EgressApplyError) as cm:
|
||||||
|
applicator.validate_routes_content(
|
||||||
|
'log: 2\n'
|
||||||
|
'routes:\n'
|
||||||
|
' - host: "x.example"\n'
|
||||||
|
)
|
||||||
|
self.assertIn("must not change egress logging", str(cm.exception))
|
||||||
|
|
||||||
|
|
||||||
class TestApplyRoutesChange(unittest.TestCase):
|
class TestApplyRoutesChange(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|||||||
@@ -73,6 +73,33 @@ resolver #2
|
|||||||
)
|
)
|
||||||
self.assertTrue(run.call_args_list[-1].kwargs["check"])
|
self.assertTrue(run.call_args_list[-1].kwargs["check"])
|
||||||
|
|
||||||
|
def test_build_image_anchors_relative_dockerfile_to_context(self):
|
||||||
|
status = util.subprocess.CompletedProcess(
|
||||||
|
args=[],
|
||||||
|
returncode=0,
|
||||||
|
stdout=(
|
||||||
|
'[{"status":{"state":"running"},'
|
||||||
|
'"configuration":{"dns":{"nameservers":["9.9.9.9"]}}}]'
|
||||||
|
),
|
||||||
|
stderr="",
|
||||||
|
)
|
||||||
|
with patch.object(util.subprocess, "run", return_value=status) as run, \
|
||||||
|
patch.object(util.os, "environ", {
|
||||||
|
"BOT_BOTTLE_MACOS_CONTAINER_DNS": "9.9.9.9",
|
||||||
|
}):
|
||||||
|
util.build_image(
|
||||||
|
"bot-bottle-sidecars:latest",
|
||||||
|
"/repo",
|
||||||
|
dockerfile="Dockerfile.sidecars",
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
[
|
||||||
|
"container", "build", "-t", "bot-bottle-sidecars:latest",
|
||||||
|
"--dns", "9.9.9.9", "-f", "/repo/Dockerfile.sidecars", "/repo",
|
||||||
|
],
|
||||||
|
run.call_args_list[-1].args[0],
|
||||||
|
)
|
||||||
|
|
||||||
def test_commit_container_execs_tar_and_builds_image(self):
|
def test_commit_container_execs_tar_and_builds_image(self):
|
||||||
# stderr is bytes because subprocess.run uses stderr=PIPE without text=True
|
# stderr is bytes because subprocess.run uses stderr=PIPE without text=True
|
||||||
completed = util.subprocess.CompletedProcess(
|
completed = util.subprocess.CompletedProcess(
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import supervise as _sv # noqa: E402 # type: ignore
|
|||||||
|
|
||||||
from bot_bottle import supervise_server # noqa: E402
|
from bot_bottle import supervise_server # noqa: E402
|
||||||
from bot_bottle.supervise_server import (
|
from bot_bottle.supervise_server import (
|
||||||
|
ERR_INTERNAL,
|
||||||
ERR_INVALID_PARAMS,
|
ERR_INVALID_PARAMS,
|
||||||
ERR_INVALID_REQUEST,
|
ERR_INVALID_REQUEST,
|
||||||
ERR_METHOD_NOT_FOUND,
|
ERR_METHOD_NOT_FOUND,
|
||||||
@@ -29,7 +30,9 @@ from bot_bottle.supervise_server import (
|
|||||||
PROPOSED_FILE_FIELD,
|
PROPOSED_FILE_FIELD,
|
||||||
ServerConfig,
|
ServerConfig,
|
||||||
TOOL_DEFINITIONS,
|
TOOL_DEFINITIONS,
|
||||||
|
_RpcClientError,
|
||||||
_RpcError,
|
_RpcError,
|
||||||
|
_RpcInternalError,
|
||||||
_response_timeout_from_env,
|
_response_timeout_from_env,
|
||||||
format_response_text,
|
format_response_text,
|
||||||
handle_initialize,
|
handle_initialize,
|
||||||
@@ -47,15 +50,15 @@ from bot_bottle.supervise_server import (
|
|||||||
|
|
||||||
|
|
||||||
class TestValidation(unittest.TestCase):
|
class TestValidation(unittest.TestCase):
|
||||||
def test_capability_block_accepts_anything_nonempty(self):
|
|
||||||
validate_proposed_file(
|
|
||||||
_sv.TOOL_CAPABILITY_BLOCK,
|
|
||||||
"FROM python:3.13\nRUN apk add git\n",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_empty_proposed_file_rejected_for_tools_with_file_field(self):
|
def test_empty_proposed_file_rejected_for_tools_with_file_field(self):
|
||||||
with self.assertRaises(_RpcError):
|
with self.assertRaises(_RpcError):
|
||||||
validate_proposed_file(_sv.TOOL_CAPABILITY_BLOCK, " \n\t")
|
validate_proposed_file(_sv.TOOL_EGRESS_ALLOW, " \n\t")
|
||||||
|
|
||||||
|
def test_capability_block_rejected_as_unknown_tool(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
validate_proposed_file("capability-block", "FROM python:3.13\n")
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
self.assertIn("unknown tool", cm.exception.message)
|
||||||
|
|
||||||
def test_egress_routes_yaml_is_validated(self):
|
def test_egress_routes_yaml_is_validated(self):
|
||||||
validate_proposed_file(
|
validate_proposed_file(
|
||||||
@@ -67,6 +70,74 @@ class TestValidation(unittest.TestCase):
|
|||||||
with self.assertRaises(_RpcError):
|
with self.assertRaises(_RpcError):
|
||||||
validate_proposed_file(_sv.TOOL_EGRESS_BLOCK, "routes: nope\n")
|
validate_proposed_file(_sv.TOOL_EGRESS_BLOCK, "routes: nope\n")
|
||||||
|
|
||||||
|
def test_egress_routes_yaml_rejects_log_full(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
validate_proposed_file(
|
||||||
|
_sv.TOOL_EGRESS_ALLOW,
|
||||||
|
"log: 2\nroutes:\n - host: example.com\n",
|
||||||
|
)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
self.assertIn("must not change egress logging", cm.exception.message)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Error taxonomy --------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestRpcErrorTaxonomy(unittest.TestCase):
|
||||||
|
def test_rpc_client_error_is_rpc_error(self):
|
||||||
|
e = _RpcClientError(ERR_INVALID_PARAMS, "bad param")
|
||||||
|
self.assertIsInstance(e, _RpcError)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, e.code)
|
||||||
|
self.assertEqual("bad param", e.message)
|
||||||
|
|
||||||
|
def test_rpc_internal_error_is_rpc_error(self):
|
||||||
|
e = _RpcInternalError("disk full")
|
||||||
|
self.assertIsInstance(e, _RpcError)
|
||||||
|
self.assertEqual(ERR_INTERNAL, e.code)
|
||||||
|
self.assertEqual("disk full", e.message)
|
||||||
|
|
||||||
|
def test_rpc_internal_error_preserves_cause(self):
|
||||||
|
cause = OSError("no space left on device")
|
||||||
|
try:
|
||||||
|
raise _RpcInternalError("failed to write") from cause
|
||||||
|
except _RpcInternalError as e:
|
||||||
|
self.assertIs(cause, e.__cause__)
|
||||||
|
|
||||||
|
def test_parse_error_is_client_error(self):
|
||||||
|
with self.assertRaises(_RpcClientError):
|
||||||
|
parse_jsonrpc(b"{bad json")
|
||||||
|
|
||||||
|
def test_validation_error_is_client_error(self):
|
||||||
|
with self.assertRaises(_RpcClientError):
|
||||||
|
validate_proposed_file(_sv.TOOL_EGRESS_ALLOW, "routes: nope\n")
|
||||||
|
|
||||||
|
def test_unknown_tool_in_tools_call_is_client_error(self):
|
||||||
|
config = ServerConfig(bottle_slug="dev", queue_dir=Path("/unused"))
|
||||||
|
with self.assertRaises(_RpcClientError) as cm:
|
||||||
|
handle_tools_call({"name": "no-such-tool", "arguments": {}}, config)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
|
||||||
|
|
||||||
|
class TestRpcInternalErrorOnIoFailure(unittest.TestCase):
|
||||||
|
def test_write_proposal_os_error_raises_internal(self):
|
||||||
|
config = ServerConfig(
|
||||||
|
bottle_slug="dev",
|
||||||
|
queue_dir=Path("/dev/null/cannot-exist"),
|
||||||
|
)
|
||||||
|
with self.assertRaises(_RpcInternalError) as cm:
|
||||||
|
handle_tools_call(
|
||||||
|
{
|
||||||
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
|
"arguments": {
|
||||||
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
|
"justification": "x",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
config,
|
||||||
|
)
|
||||||
|
self.assertEqual(ERR_INTERNAL, cm.exception.code)
|
||||||
|
self.assertIsNotNone(cm.exception.__cause__)
|
||||||
|
|
||||||
|
|
||||||
# --- JSON-RPC parsing ------------------------------------------------------
|
# --- JSON-RPC parsing ------------------------------------------------------
|
||||||
|
|
||||||
@@ -148,7 +219,6 @@ class TestHandleToolsList(unittest.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted([
|
sorted([
|
||||||
_sv.TOOL_EGRESS_ALLOW,
|
_sv.TOOL_EGRESS_ALLOW,
|
||||||
_sv.TOOL_CAPABILITY_BLOCK,
|
|
||||||
_sv.TOOL_EGRESS_BLOCK,
|
_sv.TOOL_EGRESS_BLOCK,
|
||||||
_sv.TOOL_LIST_EGRESS_ROUTES,
|
_sv.TOOL_LIST_EGRESS_ROUTES,
|
||||||
]),
|
]),
|
||||||
@@ -224,10 +294,10 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
try:
|
try:
|
||||||
result = handle_tools_call(
|
result = handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_BLOCK,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "need git",
|
"justification": "need example.com",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
self.config,
|
self.config,
|
||||||
@@ -264,9 +334,9 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
try:
|
try:
|
||||||
result = handle_tools_call(
|
result = handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "needed for tests",
|
"justification": "needed for tests",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -288,20 +358,52 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
with self.assertRaises(_RpcError):
|
with self.assertRaises(_RpcError):
|
||||||
handle_tools_call(
|
handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {"dockerfile": "FROM python:3.13\n"},
|
"arguments": {"routes_yaml": "routes:\n - host: example.com\n"},
|
||||||
},
|
},
|
||||||
self.config,
|
self.config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_missing_name_raises(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
handle_tools_call({"arguments": {}}, self.config)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
|
||||||
|
def test_arguments_must_be_object(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
handle_tools_call(
|
||||||
|
{
|
||||||
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
|
"arguments": [],
|
||||||
|
},
|
||||||
|
self.config,
|
||||||
|
)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
self.assertIn("must be an object", cm.exception.message)
|
||||||
|
|
||||||
|
def test_capability_block_call_raises_unknown_tool(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
handle_tools_call(
|
||||||
|
{
|
||||||
|
"name": "capability-block",
|
||||||
|
"arguments": {
|
||||||
|
"dockerfile": "FROM python:3.13\n",
|
||||||
|
"justification": "need git",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
self.config,
|
||||||
|
)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
self.assertIn("unknown tool", cm.exception.message)
|
||||||
|
|
||||||
def test_archives_proposal_after_response(self):
|
def test_archives_proposal_after_response(self):
|
||||||
responder = self._respond_when_proposal_appears(_sv.STATUS_APPROVED)
|
responder = self._respond_when_proposal_appears(_sv.STATUS_APPROVED)
|
||||||
try:
|
try:
|
||||||
handle_tools_call(
|
handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "x",
|
"justification": "x",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -323,10 +425,10 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
result = handle_tools_call(
|
result = handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "need a capability",
|
"justification": "need egress",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
config,
|
config,
|
||||||
@@ -341,6 +443,31 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class TestHandleListEgressRoutes(unittest.TestCase):
|
class TestHandleListEgressRoutes(unittest.TestCase):
|
||||||
|
def test_success_returns_body_text(self):
|
||||||
|
class _Resp:
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: object) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
return b"[{\"host\": \"example.com\"}]"
|
||||||
|
|
||||||
|
class _Opener:
|
||||||
|
def open(self, *args, **kwargs): # noqa: ANN001, ANN002, ANN003 # type: ignore
|
||||||
|
return _Resp()
|
||||||
|
|
||||||
|
with patch.object(supervise_server.urllib.request, "build_opener", return_value=_Opener()):
|
||||||
|
result = handle_list_egress_routes(
|
||||||
|
{},
|
||||||
|
ServerConfig(bottle_slug="dev", queue_dir=Path("/unused")),
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertFalse(result["isError"]) # type: ignore[index]
|
||||||
|
text = result["content"][0]["text"] # type: ignore[index]
|
||||||
|
self.assertIn("example.com", text)
|
||||||
|
|
||||||
def test_url_error_returns_tool_error(self):
|
def test_url_error_returns_tool_error(self):
|
||||||
class _Opener:
|
class _Opener:
|
||||||
def open(self, *args, **kwargs): # noqa: ANN001, ANN002, ANN003 # type: ignore
|
def open(self, *args, **kwargs): # noqa: ANN001, ANN002, ANN003 # type: ignore
|
||||||
@@ -400,6 +527,13 @@ class TestFormatResponseText(unittest.TestCase):
|
|||||||
self.assertIn("the operator modified", text.lower())
|
self.assertIn("the operator modified", text.lower())
|
||||||
|
|
||||||
|
|
||||||
|
class TestFormatPendingResponseText(unittest.TestCase):
|
||||||
|
def test_formats_timeout_message(self):
|
||||||
|
text = supervise_server.format_pending_response_text(12.5)
|
||||||
|
self.assertIn("status: pending", text)
|
||||||
|
self.assertIn("12.5s", text)
|
||||||
|
|
||||||
|
|
||||||
# --- End-to-end HTTP sanity ------------------------------------------------
|
# --- End-to-end HTTP sanity ------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
@@ -450,7 +584,7 @@ class TestHttpEndToEnd(unittest.TestCase):
|
|||||||
self.assertEqual("2.0", result["jsonrpc"])
|
self.assertEqual("2.0", result["jsonrpc"])
|
||||||
self.assertEqual(1, result["id"])
|
self.assertEqual(1, result["id"])
|
||||||
names = [t["name"] for t in result["result"]["tools"]] # type: ignore[index]
|
names = [t["name"] for t in result["result"]["tools"]] # type: ignore[index]
|
||||||
self.assertIn(_sv.TOOL_CAPABILITY_BLOCK, names)
|
self.assertNotIn("capability-block", names)
|
||||||
self.assertIn(_sv.TOOL_EGRESS_ALLOW, names)
|
self.assertIn(_sv.TOOL_EGRESS_ALLOW, names)
|
||||||
self.assertIn(_sv.TOOL_EGRESS_BLOCK, names)
|
self.assertIn(_sv.TOOL_EGRESS_BLOCK, names)
|
||||||
|
|
||||||
@@ -460,6 +594,26 @@ class TestHttpEndToEnd(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(ERR_METHOD_NOT_FOUND, result["error"]["code"]) # type: ignore[index]
|
self.assertEqual(ERR_METHOD_NOT_FOUND, result["error"]["code"]) # type: ignore[index]
|
||||||
|
|
||||||
|
def test_internal_error_returns_err_internal_over_http(self):
|
||||||
|
with patch.object(
|
||||||
|
supervise_server._sv, "write_proposal",
|
||||||
|
side_effect=OSError("disk full"),
|
||||||
|
):
|
||||||
|
result = self._post_jsonrpc({
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": 99,
|
||||||
|
"method": "tools/call",
|
||||||
|
"params": {
|
||||||
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
|
"arguments": {
|
||||||
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
|
"justification": "x",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
self.assertIn("error", result)
|
||||||
|
self.assertEqual(ERR_INTERNAL, result["error"]["code"]) # type: ignore[index]
|
||||||
|
|
||||||
def test_health_endpoint(self):
|
def test_health_endpoint(self):
|
||||||
conn = http.client.HTTPConnection("127.0.0.1", self.port, timeout=5)
|
conn = http.client.HTTPConnection("127.0.0.1", self.port, timeout=5)
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user