fix(egress): validate proposed full config
This commit is contained in:
@@ -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:
|
||||||
|
|||||||
@@ -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}")
|
||||||
|
|
||||||
|
|||||||
@@ -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):
|
||||||
|
|||||||
@@ -67,6 +67,15 @@ 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)
|
||||||
|
|
||||||
|
|
||||||
# --- JSON-RPC parsing ------------------------------------------------------
|
# --- JSON-RPC parsing ------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user