-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add optional swarm API key auth #1
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
rtmalikian
wants to merge
1
commit into
main
Choose a base branch
from
hermes/daily-improvement-20260525-llm-swarm
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
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,37 @@ | ||
| import os | ||
| from typing import Dict | ||
|
|
||
| from fastapi import Header, HTTPException, status | ||
|
|
||
|
|
||
| def configured_api_key() -> str: | ||
| """Return the optional shared API key used for swarm requests.""" | ||
| return os.getenv("SWARM_API_KEY", "").strip() | ||
|
|
||
|
|
||
| def auth_headers() -> Dict[str, str]: | ||
| """Return Authorization headers for outgoing swarm requests when configured.""" | ||
| api_key = configured_api_key() | ||
| if not api_key: | ||
| return {} | ||
| return {"Authorization": f"Bearer {api_key}"} | ||
|
|
||
|
|
||
| async def require_swarm_api_key(authorization: str = Header(default="")) -> None: | ||
| """Require a bearer token only when SWARM_API_KEY is configured. | ||
|
|
||
| Leaving SWARM_API_KEY unset preserves the prototype's unauthenticated local | ||
| development behavior. Setting it enables a shared secret for tracker and | ||
| node-to-node HTTP calls. | ||
| """ | ||
| api_key = configured_api_key() | ||
| if not api_key: | ||
| return | ||
|
|
||
| expected = f"Bearer {api_key}" | ||
| if authorization != expected: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||
| detail="Missing or invalid swarm API key", | ||
| headers={"WWW-Authenticate": "Bearer"}, | ||
| ) |
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,83 @@ | ||
| import importlib | ||
| from pathlib import Path | ||
| import sys | ||
| import types | ||
|
|
||
| from fastapi.testclient import TestClient | ||
|
|
||
| PROJECT_ROOT = Path(__file__).resolve().parents[1] | ||
| if str(PROJECT_ROOT) not in sys.path: | ||
| sys.path.insert(0, str(PROJECT_ROOT)) | ||
|
|
||
|
|
||
| def reload_module(name): | ||
| sys.modules.pop(name, None) | ||
| return importlib.import_module(name) | ||
|
|
||
|
|
||
| def test_tracker_rejects_register_without_swarm_api_key(monkeypatch): | ||
| monkeypatch.setenv("SWARM_API_KEY", "test-secret") | ||
| tracker = reload_module("tracker") | ||
| tracker.peers_db.clear() | ||
|
|
||
| client = TestClient(tracker.app) | ||
| response = client.post( | ||
| "/register", | ||
| json={ | ||
| "node_id": "node-a", | ||
| "url": "http://node-a:9000", | ||
| "model_id": "swarm-mesh-v1", | ||
| "layer_start": 0, | ||
| "layer_end": 4, | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 401 | ||
| assert tracker.peers_db == {} | ||
|
|
||
|
|
||
| def test_tracker_accepts_register_with_swarm_api_key(monkeypatch): | ||
| monkeypatch.setenv("SWARM_API_KEY", "test-secret") | ||
| tracker = reload_module("tracker") | ||
| tracker.peers_db.clear() | ||
|
|
||
| client = TestClient(tracker.app) | ||
| response = client.post( | ||
| "/register", | ||
| headers={"Authorization": "Bearer test-secret"}, | ||
| json={ | ||
| "node_id": "node-a", | ||
| "url": "http://node-a:9000", | ||
| "model_id": "swarm-mesh-v1", | ||
| "layer_start": 0, | ||
| "layer_end": 4, | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert "node-a" in tracker.peers_db | ||
|
|
||
|
|
||
| def test_swarm_node_rejects_layer_processing_without_swarm_api_key(monkeypatch): | ||
| monkeypatch.setenv("SWARM_API_KEY", "test-secret") | ||
| fake_llama_cpp = types.ModuleType("llama_cpp") | ||
| fake_llama_cpp.Llama = object | ||
| monkeypatch.setitem(sys.modules, "llama_cpp", fake_llama_cpp) | ||
| swarm_node = reload_module("swarm_node") | ||
| swarm_node.config = swarm_node.NodeConfig(port=9000) | ||
|
|
||
| client = TestClient(swarm_node.app) | ||
| response = client.post( | ||
| "/process_layers", | ||
| json={ | ||
| "model_id": "swarm-mesh-v1", | ||
| "layer_start": 0, | ||
| "layer_end": 4, | ||
| "tensor_data": [1.0, 2.0], | ||
| "shape": [1, 2], | ||
| "prompt_tokens": [1], | ||
| "current_token_index": 0, | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 401 |
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
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.
When
SWARM_API_KEYis enabled and a node has a mismatched/missing key,/registerreturns401, but this code logs a successful registration unconditionally becausehttpxdoes not raise on non-2xx responses unlessraise_for_status()is called. In that configuration the node appears registered locally, but it never enterspeers_dbon the tracker, which breaks downstream peer discovery and forwarding while hiding the real auth failure.Useful? React with 👍 / 👎.