Fix pylint warnings: logging, globals, exceptions, imports, duplicates
Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/576427d4-4f6a-46d2-b75f-6862ecbcf526 Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -5,7 +5,7 @@ from typing import List, Optional, Union
|
||||
|
||||
# Try to import from pydantic_settings first (newer versions)
|
||||
try:
|
||||
from pydantic import EmailStr, validator
|
||||
from pydantic import EmailStr, validator # pylint: disable=ungrouped-imports
|
||||
from pydantic_settings import BaseSettings
|
||||
except ImportError:
|
||||
# Fall back to older pydantic version
|
||||
@@ -47,7 +47,7 @@ class Settings(BaseSettings):
|
||||
CLOUDFLARE_ZONE_ID: Optional[str] = None
|
||||
|
||||
@validator("SECRET_KEY", pre=True, always=True)
|
||||
def validate_secret_key(cls, v: Optional[str]) -> str:
|
||||
def validate_secret_key(cls, v: Optional[str]) -> str: # pylint: disable=no-self-argument
|
||||
"""Validate and generate SECRET_KEY if not provided."""
|
||||
# Default insecure key that should never be used
|
||||
DEFAULT_INSECURE_KEY = "CHANGE_THIS_TO_A_RANDOM_SECRET_IN_PRODUCTION"
|
||||
@@ -66,17 +66,18 @@ class Settings(BaseSettings):
|
||||
# Check if key is too short
|
||||
if len(v) < 32:
|
||||
logger.warning(
|
||||
f"SECRET_KEY is too short ({len(v)} characters). "
|
||||
"Recommended minimum is 32 characters for security."
|
||||
"SECRET_KEY is too short (%s characters). "
|
||||
"Recommended minimum is 32 characters for security.",
|
||||
len(v),
|
||||
)
|
||||
|
||||
return v
|
||||
|
||||
@validator("BACKEND_CORS_ORIGINS", pre=True)
|
||||
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> List[str]:
|
||||
def assemble_cors_origins(cls, v: Union[str, List[str]]) -> List[str]: # pylint: disable=no-self-argument
|
||||
if isinstance(v, str) and not v.startswith("["):
|
||||
return [i.strip() for i in v.split(",")]
|
||||
elif isinstance(v, (list, str)):
|
||||
if isinstance(v, (list, str)):
|
||||
return v
|
||||
raise ValueError(v)
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ def add_api_key(api_key: str) -> bool:
|
||||
if api_key in _api_keys:
|
||||
return False
|
||||
_api_keys.add(api_key)
|
||||
logger.info(f"API key added (ends with: ...{api_key[-8:]})")
|
||||
logger.info("API key added (ends with: ...%s)", api_key[-8:])
|
||||
return True
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ def verify_api_key(api_key: str) -> bool:
|
||||
return api_key in _api_keys
|
||||
|
||||
|
||||
async def get_api_key(api_key_header: Optional[str] = Security(api_key_header)) -> str:
|
||||
async def get_api_key(api_key_value: Optional[str] = Security(api_key_header)) -> str:
|
||||
"""
|
||||
Dependency to verify API key authentication.
|
||||
|
||||
@@ -105,24 +105,23 @@ async def get_api_key(api_key_header: Optional[str] = Security(api_key_header))
|
||||
Raises:
|
||||
HTTPException: If API key is missing or invalid
|
||||
"""
|
||||
if not api_key_header:
|
||||
if not api_key_value:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Missing API key",
|
||||
headers={"WWW-Authenticate": "ApiKey"},
|
||||
)
|
||||
|
||||
if not verify_api_key(api_key_header):
|
||||
logger.warning(
|
||||
f"Invalid API key attempt: ...{api_key_header[-8:] if len(api_key_header) >= 8 else 'invalid'}"
|
||||
)
|
||||
if not verify_api_key(api_key_value):
|
||||
suffix = api_key_value[-8:] if len(api_key_value) >= 8 else "invalid"
|
||||
logger.warning("Invalid API key attempt: ...%s", suffix)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid API key",
|
||||
headers={"WWW-Authenticate": "ApiKey"},
|
||||
)
|
||||
|
||||
return api_key_header
|
||||
return api_key_value
|
||||
|
||||
|
||||
async def verify_token(
|
||||
@@ -153,12 +152,12 @@ async def verify_token(
|
||||
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
||||
return payload
|
||||
except JWTError as e:
|
||||
logger.warning(f"Invalid JWT token: {str(e)}")
|
||||
logger.warning("Invalid JWT token: %s", str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid authentication token",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
) from e
|
||||
|
||||
|
||||
async def require_admin_auth(
|
||||
@@ -192,7 +191,7 @@ async def require_admin_auth(
|
||||
)
|
||||
return {"auth_type": "jwt", "payload": payload}
|
||||
except JWTError as e:
|
||||
logger.warning(f"Invalid JWT token: {str(e)}")
|
||||
logger.warning("Invalid JWT token: %s", str(e))
|
||||
|
||||
# No valid authentication provided
|
||||
raise HTTPException(
|
||||
|
||||
Reference in New Issue
Block a user