fix(stock-webai): final review notes — env default + 1-time auth error log

(1) docker-compose: ${WEBAI_API_KEY} → ${WEBAI_API_KEY:-} matches
project convention, avoids "variable not set" warning when NAS .env
lacks the key during initial deploy.

(2) auth.py: ERROR log when WEBAI_API_KEY env unset fires only on
first miss, then silent (module-level _WEBAI_AUTH_WARNED flag).
Flag resets when env becomes configured, so future regressions log
again. Eliminates log spam under web-ai polling (~3/min).

All 102 tests still pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-15 08:56:03 +09:00
parent 13da2226c3
commit 6062445c12
2 changed files with 12 additions and 3 deletions

View File

@@ -43,7 +43,7 @@ services:
- OLLAMA_URL=${OLLAMA_URL:-http://192.168.45.59:11435}
- OLLAMA_MODEL=${OLLAMA_MODEL:-qwen3:14b}
- CORS_ALLOW_ORIGINS=${CORS_ALLOW_ORIGINS:-http://localhost:3007,http://localhost:8080}
- WEBAI_API_KEY=${WEBAI_API_KEY}
- WEBAI_API_KEY=${WEBAI_API_KEY:-}
volumes:
- ${RUNTIME_PATH}/data/stock:/app/data
healthcheck:

View File

@@ -6,6 +6,8 @@ from starlette.requests import Request
logger = logging.getLogger("stock")
_WEBAI_AUTH_WARNED = False
def verify_webai_key(
request: Request,
@@ -14,14 +16,21 @@ def verify_webai_key(
"""
/api/webai/* 보호용 FastAPI dependency.
- WEBAI_API_KEY env 미설정 → 503 (다른 endpoint 무영향)
- WEBAI_API_KEY env 미설정 → 503 (다른 endpoint 무영향). 1회만 ERROR 로그.
- 헤더 누락 또는 키 불일치 → 401 + logger.warning(ip)
"""
global _WEBAI_AUTH_WARNED
configured = os.getenv("WEBAI_API_KEY", "").strip()
if not configured:
logger.error("WEBAI_API_KEY not configured — refusing /api/webai/* request")
if not _WEBAI_AUTH_WARNED:
logger.error("WEBAI_API_KEY not configured — refusing /api/webai/* requests")
_WEBAI_AUTH_WARNED = True
raise HTTPException(status_code=503, detail="webai auth not configured")
# env 가 다시 설정되면 flag 해제 → 미래 regression 시 다시 알림
if _WEBAI_AUTH_WARNED:
_WEBAI_AUTH_WARNED = False
if not x_webai_key or x_webai_key != configured:
remote = request.client.host if request.client else "?"
logger.warning("auth_fail path=%s remote=%s", request.url.path, remote)