fix(security): restore scoped asset scanner writes

This commit is contained in:
ogt
2026-07-11 20:49:34 +08:00
parent 7695cd7dfd
commit ad3219c6cf
2 changed files with 108 additions and 6 deletions

View File

@@ -41,6 +41,7 @@ _SCAN_INTERVAL_SEC = 3600 # 每 1 小時
_FIRST_DELAY_SEC = 60 # 啟動後等 60s 再首掃 (其他 service init)
_KUBECTL_TIMEOUT_SEC = 30
_LOOP_BACKOFF_SEC = 300 # 異常時 backoff 5 分鐘
_PROJECT_ID = "awoooi"
# 7 個自動化覆蓋維度 (ADR-090 §3.5)
_COVERAGE_DIMENSIONS = (
@@ -522,6 +523,7 @@ async def _collect_prometheus_targets() -> tuple[list[dict[str, Any]], list[dict
target → host 建 depends_on relationship.
"""
import httpx
from src.core.config import settings
assets: list[dict[str, Any]] = []
@@ -636,9 +638,10 @@ async def _start_discovery_run(
"""
try:
from sqlalchemy import text as _sql
from src.db.base import get_db_context
async with get_db_context() as db:
async with get_db_context(_PROJECT_ID) as db:
row = await db.execute(
_sql("""
INSERT INTO asset_discovery_run (
@@ -677,9 +680,10 @@ async def _finish_discovery_run(
) -> None:
try:
from sqlalchemy import text as _sql
from src.db.base import get_db_context
async with get_db_context() as db:
async with get_db_context(_PROJECT_ID) as db:
await db.execute(
_sql("""
UPDATE asset_discovery_run
@@ -719,13 +723,14 @@ async def _upsert_assets(
return 0, 0
from sqlalchemy import text as _sql
from src.db.base import get_db_context
new_count = 0
modified_count = 0
try:
async with get_db_context() as db:
async with get_db_context(_PROJECT_ID) as db:
for a in assets:
# xmax = 0 表示 INSERT (新),否則表示 UPDATE (修改)
row = await db.execute(
@@ -781,11 +786,12 @@ async def _upsert_relationships(relationships: list[dict[str, str]]) -> int:
return 0
from sqlalchemy import text as _sql
from src.db.base import get_db_context
written = 0
try:
async with get_db_context() as db:
async with get_db_context(_PROJECT_ID) as db:
for rel in relationships:
try:
# 用 asset_key → asset_id 解析,同時 UPSERT
@@ -826,9 +832,10 @@ async def _write_coverage_snapshots(run_id: str) -> None:
"""
try:
from sqlalchemy import text as _sql
from src.db.base import get_db_context
async with get_db_context() as db:
async with get_db_context(_PROJECT_ID) as db:
# 一次性 INSERT: 取所有 active asset × 7 dimensions
await db.execute(
_sql("""
@@ -871,6 +878,7 @@ async def _log_aol_asset_discovered(
"""寫 automation_operation_log(asset_discovered)。失敗只 log 不阻塞。"""
try:
from sqlalchemy import text as _sql
from src.db.base import get_db_context
aol_status = "success" if status == "success" else "failed"
@@ -887,7 +895,7 @@ async def _log_aol_asset_discovered(
"modified_assets": modified,
}
async with get_db_context() as db:
async with get_db_context(_PROJECT_ID) as db:
await db.execute(
_sql("""
INSERT INTO automation_operation_log (