-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf64769
commit 5274caf
Showing
4 changed files
with
43 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,16 +1,42 @@ | ||
|
||
from fastapi import Depends, HTTPException, Security, status | ||
from fastapi.middleware.trustedhost import TrustedHostMiddleware | ||
from fastapi.security import APIKeyHeader, APIKeyQuery | ||
from starlette.types import Receive, Scope, Send | ||
|
||
from runner_manager.dependencies import get_settings | ||
from runner_manager.models.settings import Settings | ||
|
||
|
||
class TrustedHostHealthRoutes(TrustedHostMiddleware): | ||
"""A healthcheck endpoint that answers to GET requests on /_health""" | ||
|
||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
"""If the request is made on the path _health then execute the check on hosts""" | ||
if scope["path"] == "/_health/": | ||
print(scope) | ||
await super().__call__(scope, receive, send) | ||
else: | ||
await self.app(scope, receive, send) | ||
|
||
|
||
api_key_query = APIKeyQuery(name="api-key", auto_error=False) | ||
api_key_header = APIKeyHeader(name="x-api-key", auto_error=False) | ||
settings = get_settings() | ||
|
||
|
||
def get_api_key( | ||
api_key_query: str = Security(api_key_query), | ||
api_key_header: str = Security(api_key_header), | ||
settings: Settings = Depends(get_settings), | ||
) -> str: | ||
"""Get the API key from either the query parameter or the header""" | ||
if not settings.api_key: | ||
return "" | ||
if api_key_query in [settings.api_key.get_secret_value()]: | ||
return api_key_query | ||
if api_key_header in [settings.api_key.get_secret_value()]: | ||
return api_key_header | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid API Key", | ||
) |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
from fastapi import APIRouter, Response | ||
|
||
router = APIRouter(prefix="/_health") | ||
|
This file contains 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