Spaces:
Running

ioc-core / src /config /settings.py
hellium1999's picture
fix: CORS_ORIGINS parsing error in pydantic-settings
e52eb73
Raw History Blame
3.57 kB
"""Application settings and configuration."""
from typing import Any
from pydantic import Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""Application settings with environment variable support.
Pydantic BaseSettings automatically reads from environment variables
and .env files with type validation on startup.
"""
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
extra="ignore",
)
# API Information
APP_NAME: str = "IOC Core API"
APP_DESCRIPTION: str = (
"Threat Intelligence API for analyzing hashes, IPs, and domains"
)
VERSION: str = "1.0.0"
# API Keys
ABUSE_IP_DB_API_KEY: str | None = None
VIRUS_TOTAL_API_KEY: str | None = None
URL_SCAN_API_KEY: str | None = None
# abuse.ch unified API key (ThreatFox, URLhaus, MalwareBazaar)
ABUSE_CH_API_KEY: str | None = None
# OTX AlienVault API key
OTX_API_KEY: str | None = None
# HuggingFace API token for AI explanations
HF_API_TOKEN: str | None = None
# Supabase for dashboard analytics
SUPABASE_URL: str | None = None
SUPABASE_KEY: str | None = None
# API Configuration
REQUEST_TIMEOUT: int = 15 # Default timeout (reduced from 30s)
MAX_AGE_IN_DAYS: int = 365
# Per-source timeout configuration (seconds) - optimized for performance
SOURCE_TIMEOUTS: dict = Field(default={
"virustotal": 15, # Primary source, reliable
"abuseipdb": 10, # Very fast API
"urlscan": 10, # Fast search API
"threatfox": 10, # Fast
"urlhaus": 10, # Fast
"malwarebazaar": 10, # Fast
"otx": 8, # Slow API, often times out - fail fast!
})
# Redis Cache Configuration
REDIS_URL: str = "redis://localhost:6379/0"
# Oracle Autonomous Database Configuration
# DSN is the TNS name from tnsnames.ora (e.g., iocdb_high)
ORACLE_DSN: str | None = None
ORACLE_USER: str = "ADMIN"
ORACLE_PASSWORD: str | None = None
# Wallet directory for mTLS authentication
ORACLE_WALLET_DIR: str | None = None
# Server Configuration
HOST: str = "0.0.0.0"
PORT: int = 8000
RELOAD: bool = True
# CORS Configuration
# Comma-separated list of allowed origins, or "*" for all (dev only)
CORS_ORIGINS: Any = Field(default=["*"])
@field_validator("CORS_ORIGINS", mode="before")
@classmethod
def parse_cors_origins(cls, v: str | list[str] | None) -> list[str]:
"""Parse comma-separated CORS origins from env var."""
if v is None:
return ["*"]
if isinstance(v, str):
# Handle possible empty string or single asterisk
if not v or v.strip() == "*":
return ["*"]
return [origin.strip() for origin in v.split(",") if origin.strip()]
if isinstance(v, list):
return [str(origin).strip() for origin in v if origin]
return ["*"]
# Logging
LOG_LEVEL: str = "INFO"
def validate_api_keys(self) -> dict[str, bool]:
"""Check which API keys are configured."""
return {
"abuse_ip_db": bool(self.ABUSE_IP_DB_API_KEY),
"virus_total": bool(self.VIRUS_TOTAL_API_KEY),
"url_scan": bool(self.URL_SCAN_API_KEY),
"abuse_ch": bool(self.ABUSE_CH_API_KEY),
"otx": bool(self.OTX_API_KEY),
}
# Global settings instance
settings = Settings()