-
Notifications
You must be signed in to change notification settings - Fork 535
Add per-rollout auth to interception servers #1122
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
Open
teilomillet
wants to merge
6
commits into
PrimeIntellect-ai:main
Choose a base branch
from
teilomillet:teilomillet/interception-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b31db0
Add per-rollout bearer token auth to interception servers
teilomillet 4fe2654
Fix RLM auth and interception validation bugs
teilomillet cdba71f
Harden interception response serialization
teilomillet de9ee3f
Deduplicate rollout bearer auth checks
teilomillet dcc09b9
Use rollout auth token in OpenCode provider config with fallback
teilomillet 71a7585
Treat empty-string auth token as no-auth like None
teilomillet 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| """Tests for per-rollout authentication on the interception server. | ||
|
|
||
| Verifies that: | ||
| - Requests with valid tokens are accepted | ||
| - Requests with invalid/missing tokens are rejected (401) | ||
| - Unregistered rollout IDs are rejected (404) | ||
| - Graceful fallback: rollouts registered without a token skip auth | ||
| """ | ||
|
|
||
| import asyncio | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from aiohttp import ClientSession | ||
|
|
||
| from verifiers.utils.interception_utils import ( | ||
| InterceptionServer, | ||
| generate_interception_token, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def server(): | ||
| srv = InterceptionServer(port=0) | ||
| await srv.start() | ||
| yield srv | ||
| await srv.stop() | ||
|
|
||
|
|
||
| def _chat_payload(content: str = "hello") -> dict: | ||
| return { | ||
| "model": "test-model", | ||
| "messages": [{"role": "user", "content": content}], | ||
| } | ||
|
|
||
|
|
||
| async def _post( | ||
| base: str, | ||
| rollout_id: str, | ||
| token: str | None = None, | ||
| timeout: float = 0.5, | ||
| payload: Any | None = None, | ||
| ): | ||
| """POST to a rollout endpoint, return (status, body) or 'timeout'.""" | ||
| headers = {} | ||
| if token is not None: | ||
| headers["Authorization"] = f"Bearer {token}" | ||
| try: | ||
| async with ClientSession() as session: | ||
| async with session.post( | ||
| f"{base}/rollout/{rollout_id}/v1/chat/completions", | ||
| json=_chat_payload() if payload is None else payload, | ||
| headers=headers, | ||
| timeout=__import__("aiohttp").ClientTimeout(total=timeout), | ||
| ) as resp: | ||
| body = await resp.json() | ||
| return resp.status, body | ||
| except asyncio.TimeoutError: | ||
| return "accepted", None | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_valid_token_accepted(server: InterceptionServer): | ||
| """Request with the correct bearer token is accepted.""" | ||
| token = generate_interception_token() | ||
| server.register_rollout("rollout_auth_ok", auth_token=token) | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| result = await _post(base, "rollout_auth_ok", token=token) | ||
| # "accepted" means the server didn't reject — it's waiting for a model response | ||
| assert result[0] == "accepted" or result[0] == 200 | ||
|
|
||
| server.unregister_rollout("rollout_auth_ok") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_missing_token_rejected(server: InterceptionServer): | ||
| """Request with no Authorization header is rejected when auth is configured.""" | ||
| token = generate_interception_token() | ||
| server.register_rollout("rollout_no_token", auth_token=token) | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| status, body = await _post(base, "rollout_no_token", token=None) | ||
| assert status == 401 | ||
| assert body["error"] == "Unauthorized" | ||
|
|
||
| server.unregister_rollout("rollout_no_token") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_wrong_token_rejected(server: InterceptionServer): | ||
| """Request with an incorrect bearer token is rejected.""" | ||
| token = generate_interception_token() | ||
| server.register_rollout("rollout_bad_token", auth_token=token) | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| status, body = await _post(base, "rollout_bad_token", token="wrong-token") | ||
| assert status == 401 | ||
| assert body["error"] == "Unauthorized" | ||
|
|
||
| server.unregister_rollout("rollout_bad_token") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_unknown_rollout_404(server: InterceptionServer): | ||
| """Request to a non-existent rollout ID returns 404.""" | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| status, body = await _post(base, "rollout_nonexistent", token=None) | ||
| assert status == 404 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_no_token_graceful_fallback(server: InterceptionServer): | ||
| """Rollout registered without a token accepts any request (backwards compat).""" | ||
| server.register_rollout("rollout_no_auth") | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| result = await _post(base, "rollout_no_auth", token=None) | ||
| # Should be accepted (not 401), waiting for model response | ||
| assert result[0] == "accepted" or result[0] == 200 | ||
|
|
||
| server.unregister_rollout("rollout_no_auth") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_cross_rollout_blocked(server: InterceptionServer): | ||
| """Token for rollout A cannot be used to access rollout B.""" | ||
| token_a = generate_interception_token() | ||
| token_b = generate_interception_token() | ||
| server.register_rollout("rollout_a", auth_token=token_a) | ||
| server.register_rollout("rollout_b", auth_token=token_b) | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| # Use A's token to access B's endpoint | ||
| status, body = await _post(base, "rollout_b", token=token_a) | ||
| assert status == 401, "Cross-rollout access should be rejected" | ||
|
|
||
| server.unregister_rollout("rollout_a") | ||
| server.unregister_rollout("rollout_b") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_missing_messages_rejected(server: InterceptionServer): | ||
| """Authenticated requests without messages return a 400 instead of crashing.""" | ||
| token = generate_interception_token() | ||
| server.register_rollout("rollout_missing_messages", auth_token=token) | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| status, body = await _post( | ||
| base, | ||
| "rollout_missing_messages", | ||
| token=token, | ||
| payload={"model": "test-model"}, | ||
| ) | ||
| assert status == 400 | ||
| assert body["error"] == "Request body must include 'messages'" | ||
|
|
||
| server.unregister_rollout("rollout_missing_messages") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_non_object_body_rejected(server: InterceptionServer): | ||
| """Authenticated requests must send a JSON object.""" | ||
| token = generate_interception_token() | ||
| server.register_rollout("rollout_non_object", auth_token=token) | ||
| base = f"http://127.0.0.1:{server.port}" | ||
|
|
||
| status, body = await _post( | ||
| base, | ||
| "rollout_non_object", | ||
| token=token, | ||
| payload=["not", "an", "object"], | ||
| ) | ||
| assert status == 400 | ||
| assert body["error"] == "Request body must be a JSON object" | ||
|
|
||
| server.unregister_rollout("rollout_non_object") |
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.
Uh oh!
There was an error while loading. Please reload this page.