Repository navigation
[BFA-130] Default MCP public origin from BASE_DOMAIN #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
arnold-retool
wants to merge
2
commits into
arnold/mcp-routing-mode
from
arnold/mcp-public-origin-defaults
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| """Check MCP public routes and migration errors in rendered Helm manifests.""" | ||
|
|
||
| import re | ||
| import subprocess | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| ROOT = Path(__file__).resolve().parents[1] | ||
| BASE = [ | ||
| "helm", "template", "routing", "charts/retool", | ||
| "--values", "charts/retool/ci/test-install-values.yaml", | ||
| "--values", "charts/retool/ci/test-mcp-enabled-option.yaml", | ||
| "--set", "ingress.hosts[0].host=retool.example.com", | ||
| "--set", "ingress.hosts[0].paths[0].path=/", | ||
| "--set", "env.BASE_DOMAIN=https://retool.example.com", | ||
| "--set", "mcp.config.mcpServiceExternalUrl=https://retool.example.com", | ||
| ] | ||
| MAIN = "routing-retool" | ||
| MCP = "routing-retool-mcp" | ||
| BACKEND_API = "routing-retool-backend-internal" | ||
|
|
||
|
|
||
| def render(*settings): | ||
| command = BASE.copy() | ||
| for setting in settings: | ||
| command.extend(("--set", setting)) | ||
| return subprocess.run(command, cwd=ROOT, text=True, capture_output=True) | ||
|
|
||
|
|
||
| def manifest(output, filename): | ||
| marker = f"# Source: retool/templates/{filename}\n" | ||
| return output.rsplit(marker, 1)[1].split("\n---\n", 1)[0] | ||
|
|
||
|
|
||
| def ingress_routes(output): | ||
| document = manifest(output, "ingress.yaml") | ||
| return re.findall( | ||
| r"(?m)^\s+- path: (\S+)\n\s+pathType: (\S+)\n\s+backend:\n\s+service:\n\s+name: (\S+)\n\s+port:\n\s+number: (\d+)", | ||
| document, | ||
| ) | ||
|
|
||
|
|
||
| def http_routes(output): | ||
| document = manifest(output, "httproute.yaml") | ||
| return re.findall( | ||
| r"(?m)^\s+- matches:\n\s+- path:\n\s+type: (\S+)\n\s+value: (\S+)\n\s+backendRefs:\n\s+- name: (\S+)\n\s+port: (\d+)", | ||
| document, | ||
| ) | ||
|
|
||
|
|
||
| INGRESS_DIRECT = [ | ||
| ("/.well-known/oauth-authorization-server", "Exact", BACKEND_API, "3001"), | ||
| ("/.well-known/oauth-protected-resource", "Exact", MCP, "4010"), | ||
| ("/mcp", "Prefix", MCP, "4010"), | ||
| ("/", "ImplementationSpecific", MAIN, "3000"), | ||
| ] | ||
| HTTP_DIRECT = [ | ||
| ("Exact", "/.well-known/oauth-authorization-server", BACKEND_API, "3001"), | ||
| ("Exact", "/.well-known/oauth-protected-resource", MCP, "4010"), | ||
| ("PathPrefix", "/mcp", MCP, "4010"), | ||
| ("PathPrefix", "/", MAIN, "3000"), | ||
| ] | ||
|
|
||
|
|
||
| class RoutingTests(unittest.TestCase): | ||
| def assert_rendered(self, *settings): | ||
| result = render(*settings) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| return result.stdout | ||
|
|
||
| def assert_rejected(self, message, *settings): | ||
| result = render(*settings) | ||
| self.assertNotEqual(result.returncode, 0, result.stdout) | ||
| self.assertIn(message, result.stderr) | ||
|
|
||
| def test_default_backend_relay_uses_main_service_for_both_route_types(self): | ||
| output = self.assert_rendered() | ||
| self.assertEqual(ingress_routes(output), [INGRESS_DIRECT[-1]]) | ||
| self.assertEqual(http_routes(output), [HTTP_DIRECT[-1]]) | ||
| self.assertIn('value: "http://routing-retool-mcp:4010"', manifest(output, "deployment_backend.yaml")) | ||
| self.assertIn("- name: MCP_SERVICE_INGRESS_DOMAIN", manifest(output, "deployment_backend.yaml")) | ||
|
|
||
| def test_direct_renders_legacy_mappings_for_both_route_types(self): | ||
| output = self.assert_rendered("mcp.routing.mode=direct") | ||
| self.assertEqual(ingress_routes(output), INGRESS_DIRECT) | ||
| self.assertEqual(http_routes(output), HTTP_DIRECT) | ||
| self.assertIn("- name: MCP_SERVICE_INGRESS_DOMAIN", manifest(output, "deployment_backend.yaml")) | ||
|
|
||
| def test_hostname_ingress_branch_follows_mode(self): | ||
| output = self.assert_rendered("ingress.hostName=retool.example.com") | ||
| self.assertEqual(ingress_routes(output), [INGRESS_DIRECT[-1]]) | ||
| output = self.assert_rendered("ingress.hostName=retool.example.com", "mcp.routing.mode=direct") | ||
| self.assertEqual(ingress_routes(output), INGRESS_DIRECT) | ||
|
|
||
| def test_explicit_true_legacy_flags_work_in_direct_mode(self): | ||
| output = self.assert_rendered("mcp.routing.mode=direct", "mcp.ingress.enabled=true", "mcp.httpRoute.enabled=true") | ||
| self.assertEqual(ingress_routes(output), INGRESS_DIRECT) | ||
| self.assertEqual(http_routes(output), HTTP_DIRECT) | ||
|
|
||
| def test_disabled_mcp_has_only_main_routes(self): | ||
| output = self.assert_rendered("mcp.enabled=false", "mcp.routing.mode=direct", "mcp.ingress.enabled=true", "mcp.httpRoute.enabled=true") | ||
| self.assertEqual(ingress_routes(output), [INGRESS_DIRECT[-1]]) | ||
| self.assertEqual(http_routes(output), [HTTP_DIRECT[-1]]) | ||
| self.assertNotIn("- name: MCP_SERVICE_INGRESS_DOMAIN", manifest(output, "deployment_backend.yaml")) | ||
|
|
||
| def test_explicit_false_skips_direct_rules_on_that_surface(self): | ||
| output = self.assert_rendered("mcp.routing.mode=direct", "mcp.ingress.enabled=false") | ||
| self.assertEqual(ingress_routes(output), [INGRESS_DIRECT[-1]]) | ||
| self.assertEqual(http_routes(output), HTTP_DIRECT) | ||
| output = self.assert_rendered("mcp.routing.mode=direct", "mcp.httpRoute.enabled=false") | ||
| self.assertEqual(ingress_routes(output), INGRESS_DIRECT) | ||
| self.assertEqual(http_routes(output), [HTTP_DIRECT[-1]]) | ||
|
|
||
| def test_custom_legacy_ports_survive_in_direct_mode(self): | ||
| output = self.assert_rendered( | ||
| "mcp.routing.mode=direct", | ||
| "mcp.ingress.paths[0].path=/.well-known/oauth-authorization-server", | ||
| "mcp.ingress.paths[0].pathType=Exact", | ||
| "mcp.ingress.paths[0].target=backendInternal", | ||
| "mcp.ingress.paths[0].port=3002", | ||
| "mcp.httpRoute.rules[0].path=/mcp", | ||
| "mcp.httpRoute.rules[0].pathType=PathPrefix", | ||
| "mcp.httpRoute.rules[0].port=4020", | ||
| ) | ||
| self.assertEqual(ingress_routes(output)[0], ("/.well-known/oauth-authorization-server", "Exact", BACKEND_API, "3002")) | ||
| self.assertEqual(http_routes(output)[0], ("PathPrefix", "/mcp", MCP, "4020")) | ||
|
|
||
| def test_explicit_false_is_accepted_in_backend_relay(self): | ||
| output = self.assert_rendered("mcp.ingress.enabled=false", "mcp.httpRoute.enabled=false") | ||
| self.assertEqual(ingress_routes(output), [INGRESS_DIRECT[-1]]) | ||
| self.assertEqual(http_routes(output), [HTTP_DIRECT[-1]]) | ||
|
|
||
| def test_old_explicit_true_flags_fail_with_migration_guidance(self): | ||
| for surface in ("ingress", "httpRoute"): | ||
| with self.subTest(surface=surface): | ||
| self.assert_rejected( | ||
| f"mcp.{surface}.enabled=true conflicts with mcp.routing.mode=backendRelay", | ||
| f"mcp.{surface}.enabled=true", | ||
| ) | ||
|
|
||
| def test_unknown_mode_fails_even_if_mcp_and_public_routes_are_disabled(self): | ||
| self.assert_rejected('mcp.routing.mode must be "backendRelay" or "direct"', "mcp.routing.mode=other") | ||
| self.assert_rejected( | ||
| 'mcp.routing.mode must be "backendRelay" or "direct"', | ||
| "mcp.routing.mode=other", "mcp.enabled=false", "ingress.enabled=false", "httpRoute.enabled=false", | ||
| ) | ||
|
|
||
| def test_base_domain_is_the_mcp_public_fallback_in_both_modes(self): | ||
| for mode in ("backendRelay", "direct"): | ||
| with self.subTest(mode=mode): | ||
| output = self.assert_rendered( | ||
| f"mcp.routing.mode={mode}", | ||
| "mcp.config.mcpServiceExternalUrl=", | ||
| "mcp.config.oauthMainDomain=", | ||
| ) | ||
| mcp = manifest(output, "deployment_mcp.yaml") | ||
| self.assertIn('- name: "BASE_DOMAIN"\n value: "https://retool.example.com"', mcp) | ||
| self.assertIn('name: OAUTH_MAIN_DOMAIN\n value: "retool.example.com"', mcp) | ||
| self.assertNotIn("- name: MCP_SERVICE_EXTERNAL_URL", mcp) | ||
|
|
||
| def test_explicit_origin_override_is_shared_with_backend(self): | ||
| output = self.assert_rendered() | ||
| for deployment in ("deployment_mcp.yaml", "deployment_backend.yaml"): | ||
| self.assertIn( | ||
| 'name: MCP_SERVICE_EXTERNAL_URL\n value: "https://retool.example.com"', | ||
| manifest(output, deployment), | ||
| ) | ||
|
|
||
| def test_environment_overrides_are_not_duplicated(self): | ||
| output = self.assert_rendered( | ||
| "env.MCP_SERVICE_EXTERNAL_URL=https://retool.example.com", | ||
| "mcp.environmentVariables[0].name=MCP_SERVICE_EXTERNAL_URL", | ||
| "mcp.environmentVariables[0].value=https://retool.example.com", | ||
| ) | ||
| for deployment in ("deployment_backend.yaml", "deployment_mcp.yaml"): | ||
| self.assertEqual( | ||
| len(re.findall(r'name: "?MCP_SERVICE_EXTERNAL_URL"?', manifest(output, deployment))), 1, | ||
| ) | ||
|
|
||
| def test_mismatched_chart_managed_hosts_fail_in_both_modes(self): | ||
| for mode in ("backendRelay", "direct"): | ||
| with self.subTest(mode=mode): | ||
| self.assert_rejected( | ||
| 'env.BASE_DOMAIN host "retool.example.com" does not match a chart-managed ingress host', | ||
| f"mcp.routing.mode={mode}", "ingress.hosts[0].host=other.example.com", | ||
| ) | ||
| self.assert_rejected( | ||
| 'does not match a chart-managed HTTPRoute hostname', | ||
| f"mcp.routing.mode={mode}", "httpRoute.hostnames[0]=other.example.com", | ||
| ) | ||
| self.assert_rejected( | ||
| 'mcp.config.mcpServiceExternalUrl host "other.example.com" does not match a chart-managed ingress host', | ||
| "mcp.environmentVariables[0].name=MCP_SERVICE_EXTERNAL_URL", | ||
| "mcp.environmentVariables[0].value=https://other.example.com", | ||
| ) | ||
|
|
||
| def test_external_ingress_and_custom_host_remain_supported(self): | ||
| self.assert_rendered( | ||
| "ingress.enabled=false", "httpRoute.enabled=false", | ||
| "env.BASE_DOMAIN=https://public.example.com", | ||
| ) | ||
| self.assert_rendered("ingress.hosts[1].host=custom.space.example.org") | ||
|
|
||
| def test_unresolved_base_domain_can_be_secret_backed(self): | ||
| command = BASE.copy() | ||
| base_index = command.index("env.BASE_DOMAIN=https://retool.example.com") | ||
| del command[base_index - 1:base_index + 1] | ||
| result = subprocess.run( | ||
| command + [ | ||
| "--set", "mcp.config.mcpServiceExternalUrl=", | ||
| "--set-json", 'env.BASE_DOMAIN={"valueFrom":{"secretKeyRef":{"name":"public-origin","key":"url"}}}', | ||
| ], | ||
| cwd=ROOT, text=True, capture_output=True, | ||
| ) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| mcp = manifest(result.stdout, "deployment_mcp.yaml") | ||
| self.assertIn('name: "BASE_DOMAIN"', mcp) | ||
| self.assertIn('name: public-origin', mcp) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
MCP_SERVICE_EXTERNAL_URLinmcp.environmentVariableshas no scheme, the new check addshttps://to validate it, but this copy sends the unchanged value to the backend. The backend can then produce bad discovery or upload links. Reject the bare hostname or normalize it before copying it.