-
Notifications
You must be signed in to change notification settings - Fork 0
Add configuration validation on application startup #7
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
Merged
Merged
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
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,149 @@ | ||
| import asyncio | ||
| import logging | ||
| import os | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| import aiohttp | ||
| import boto3 # type: ignore | ||
| from botocore.exceptions import ClientError # type: ignore | ||
| from openbb_fmp import FMPEquityHistoricalFetcher | ||
|
|
||
| from .models import AppConfig | ||
|
|
||
|
|
||
| async def check_openrouter(api_key: str) -> None: | ||
| """Validate OpenRouter reachability and API key. | ||
|
|
||
| Performs a lightweight GET to the models endpoint. Raises RuntimeError on failure. | ||
| """ | ||
| if not api_key: | ||
| raise RuntimeError("OPENROUTER_API_KEY is missing.") | ||
|
|
||
| # Ensure downstream libs that rely on env var can see it | ||
| os.environ["OPENROUTER_API_KEY"] = api_key | ||
|
|
||
| timeout = aiohttp.ClientTimeout(total=5) | ||
| headers = {"Authorization": f"Bearer {api_key}"} | ||
| url = "https://openrouter.ai/api/v1/key" | ||
| try: | ||
| async with aiohttp.ClientSession(timeout=timeout) as session: | ||
| async with session.get(url, headers=headers) as resp: | ||
| if resp.status == 200: | ||
| return | ||
| if resp.status in (401, 403): | ||
| raise RuntimeError( | ||
| "OpenRouter validation failed: unauthorized. Check OPENROUTER_API_KEY." | ||
| ) | ||
| raise RuntimeError( | ||
| f"OpenRouter validation failed: HTTP {resp.status}. Service may be unavailable." | ||
| ) | ||
| except asyncio.TimeoutError as e: | ||
| raise RuntimeError("OpenRouter validation failed: request timed out.") from e | ||
| except aiohttp.ClientError as e: | ||
| raise RuntimeError(f"OpenRouter validation failed: {e}") from e | ||
|
|
||
|
|
||
| def check_s3(endpoint: str, access_key: str, secret_key: str, bucket: str) -> None: | ||
| """Validate S3/compatible storage credentials and bucket access. | ||
|
|
||
| Calls head_bucket and a zero-key list to confirm access. Raises RuntimeError on failure. | ||
| """ | ||
| try: | ||
| s3 = boto3.client( | ||
| "s3", | ||
| endpoint_url=endpoint, | ||
| aws_access_key_id=access_key, | ||
| aws_secret_access_key=secret_key, | ||
| ) | ||
| # Existence and access | ||
| s3.head_bucket(Bucket=bucket) | ||
| # Minimal read attempt | ||
| s3.list_objects_v2(Bucket=bucket, MaxKeys=0) | ||
| except ClientError as e: | ||
| code = e.response.get("Error", {}).get("Code", "Unknown") | ||
| msg = e.response.get("Error", {}).get("Message", str(e)) | ||
| raise RuntimeError(f"S3 validation failed: {code} - {msg}") from e | ||
| except Exception as e: # pragma: no cover - safety net | ||
| raise RuntimeError(f"S3 validation failed: {e}") from e | ||
|
|
||
|
|
||
| def check_local_storage(path: str) -> None: | ||
| """Ensure local storage path exists and is writable. | ||
|
|
||
| Creates the folder if missing and performs a write test. | ||
| """ | ||
| if not path: | ||
| raise RuntimeError("DATA_FOLDER_PATH must be set when S3 is disabled.") | ||
|
|
||
| try: | ||
| os.makedirs(path, exist_ok=True) | ||
| except OSError as e: | ||
| raise RuntimeError(f"Failed to create data folder at '{path}': {e}") from e | ||
|
|
||
| test_file = os.path.join(path, ".allocator_bot_write_test") | ||
| try: | ||
| with open(test_file, "w") as f: | ||
| f.write("ok") | ||
| os.remove(test_file) | ||
| except OSError as e: | ||
| raise RuntimeError(f"Data folder is not writable at '{path}': {e}") from e | ||
|
|
||
|
|
||
| async def check_fmp(key: str) -> None: | ||
| """Validate FMP key by fetching a tiny slice of data. | ||
|
|
||
| Uses OpenBB FMP fetcher for a single symbol and short date window. | ||
| """ | ||
| if not key: | ||
| raise RuntimeError("FMP_API_KEY is missing.") | ||
|
|
||
| end_date = datetime.now(timezone.utc).date() | ||
| start_date = end_date - timedelta(days=5) | ||
|
|
||
| try: | ||
| data = await FMPEquityHistoricalFetcher.fetch_data( | ||
| params={ | ||
| "symbol": "AAPL", | ||
| "start_date": start_date.isoformat(), | ||
| "end_date": end_date.isoformat(), | ||
| }, | ||
| credentials={"fmp_api_key": key}, | ||
| ) | ||
| # Some accounts may have limited history; consider any non-exception as success | ||
| if data is None: | ||
| raise RuntimeError("FMP validation failed: empty response.") | ||
| except Exception as e: | ||
| raise RuntimeError( | ||
| "FMP validation failed: invalid key or network error." | ||
piiq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) from e | ||
|
|
||
|
|
||
| async def validate_environment(config: AppConfig) -> None: | ||
| """Run all environment validations. Raises on first failure. | ||
|
|
||
| Set VALIDATION_SKIP=true to bypass in development environments. | ||
| """ | ||
| if os.getenv("VALIDATION_SKIP", "false").lower() == "true": | ||
| logging.warning("VALIDATION_SKIP=true: Skipping external credential checks.") | ||
| return | ||
|
|
||
| # OpenRouter | ||
| await check_openrouter(config.openrouter_api_key) | ||
|
|
||
| # Storage | ||
| if config.s3_enabled: | ||
| check_s3( | ||
| endpoint=str(config.s3_endpoint), | ||
| access_key=str(config.s3_access_key), | ||
| secret_key=str(config.s3_secret_key), | ||
| bucket=str(config.s3_bucket_name), | ||
| ) | ||
| else: | ||
| if config.data_folder_path is None: | ||
| raise RuntimeError("DATA_FOLDER_PATH must be set when S3 is not enabled.") | ||
| check_local_storage(config.data_folder_path) | ||
|
|
||
| # FMP | ||
| await check_fmp(str(config.fmp_api_key)) | ||
|
|
||
| logging.info("Environment validation succeeded.") | ||
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.
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.