feat(监控): 完善系统监控、登录状态与访问审计能力
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"""监控API测试:权限系统监控API端点验证。"""
|
||||
|
||||
import inspect
|
||||
import json
|
||||
import uuid
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
@@ -10,6 +12,7 @@ from sqlalchemy import text
|
||||
from app.core.permission_monitor import set_permission_monitor, PermissionMonitor
|
||||
from app.api.v1 import permission_monitoring
|
||||
from app.api.v1.system_permissions import list_system_permissions
|
||||
from app.services.monitoring_server_location import MonitoringServerLocation
|
||||
|
||||
|
||||
class FakeIpInfo:
|
||||
@@ -30,6 +33,18 @@ class ProjectPmUserStub:
|
||||
is_admin = False
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def disable_server_public_ip_discovery(monkeypatch):
|
||||
async def no_server_location():
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
permission_monitoring,
|
||||
"resolve_monitoring_server_location",
|
||||
no_server_location,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_monitoring_scope_rejects_project_pm(db_session):
|
||||
"""系统监测模块仅允许系统管理员访问,项目 PM 不再具备监测范围。"""
|
||||
@@ -103,9 +118,11 @@ async def _seed_permission_log(db_session, study_id: uuid.UUID, user_id: uuid.UU
|
||||
text(
|
||||
"""
|
||||
INSERT INTO permission_access_logs
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address, created_at)
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address,
|
||||
request_snapshot, created_at)
|
||||
VALUES
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address, CURRENT_TIMESTAMP)
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address,
|
||||
:request_snapshot, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{
|
||||
@@ -117,6 +134,7 @@ async def _seed_permission_log(db_session, study_id: uuid.UUID, user_id: uuid.UU
|
||||
"allowed": allowed,
|
||||
"elapsed_ms": elapsed_ms,
|
||||
"ip_address": "127.0.0.1",
|
||||
"request_snapshot": None,
|
||||
},
|
||||
)
|
||||
await db_session.commit()
|
||||
@@ -262,8 +280,8 @@ async def test_permission_system_health_degraded(db_session):
|
||||
assert any("权限拒绝率偏高" in issue for issue in data["issues"])
|
||||
assert any("缓存命中率偏低" in issue for issue in data["issues"])
|
||||
assert data["status"] == "unhealthy"
|
||||
assert data["health_score"] == 30
|
||||
assert data["score_breakdown"]["performance"]["deduction"] == 25
|
||||
assert data["health_score"] == 35
|
||||
assert data["score_breakdown"]["performance"]["deduction"] == 20
|
||||
assert data["score_breakdown"]["deny_rate"]["deduction"] == 20
|
||||
assert data["score_breakdown"]["cache"]["deduction"] == 25
|
||||
|
||||
@@ -279,6 +297,10 @@ async def test_permission_system_health_includes_metrics(db_session):
|
||||
assert "cache_stats" in data
|
||||
assert "score_breakdown" in data
|
||||
assert "sample_sufficient" in data
|
||||
assert "components" in data
|
||||
assert "storage" in data
|
||||
assert "runtime" in data["score_breakdown"]
|
||||
assert data["components"]["database"]["status"] == "healthy"
|
||||
assert "total_checks" in data["last_hour"]
|
||||
assert "cache_metrics" in data["cache_stats"]
|
||||
|
||||
@@ -304,6 +326,41 @@ async def test_permission_system_health_ignores_small_cache_sample(db_session):
|
||||
assert data["health_score"] == 100 - non_cache_deductions
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_permission_system_health_reports_writer_degradation(db_session, monkeypatch):
|
||||
monitor = PermissionMonitor()
|
||||
set_permission_monitor(monitor)
|
||||
writer = SimpleNamespace(
|
||||
stats=lambda: {
|
||||
"running": True,
|
||||
"queue_size": 3,
|
||||
"queue_capacity": 100,
|
||||
"failed_batches": 0,
|
||||
"dropped_entries": 2,
|
||||
}
|
||||
)
|
||||
monkeypatch.setattr(permission_monitoring, "get_log_writer", lambda: writer)
|
||||
monkeypatch.setattr(permission_monitoring, "get_security_log_writer", lambda: None)
|
||||
monkeypatch.setattr(
|
||||
permission_monitoring,
|
||||
"get_monitoring_retention_status",
|
||||
lambda: {
|
||||
"running": False,
|
||||
"last_run_started_at": None,
|
||||
"last_success_at": None,
|
||||
"last_error_at": None,
|
||||
},
|
||||
)
|
||||
|
||||
data = await permission_monitoring.permission_system_health(
|
||||
db=db_session, _=AdminUserStub()
|
||||
)
|
||||
|
||||
assert data["components"]["permission_log_writer"]["status"] == "degraded"
|
||||
assert data["score_breakdown"]["runtime"]["deduction"] == 10
|
||||
assert any("权限日志写入器" in issue for issue in data["issues"])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ip_locations_counts_unique_users_per_location(db_session, monkeypatch):
|
||||
"""IP 属地统计应按省市聚合访问次数、来源 IP 数和访问用户数。"""
|
||||
@@ -381,10 +438,26 @@ async def test_ip_locations_counts_unique_users_per_location(db_session, monkeyp
|
||||
)
|
||||
await db_session.commit()
|
||||
|
||||
resolved_ips = []
|
||||
|
||||
def fake_resolve_ip_location(ip):
|
||||
resolved_ips.append(ip)
|
||||
return FakeIpInfo("广东省", "深圳市")
|
||||
|
||||
async def fake_resolve_monitoring_server_location():
|
||||
return MonitoringServerLocation(
|
||||
name="China / 重庆 / 重庆市",
|
||||
longitude=106.5516,
|
||||
latitude=29.563,
|
||||
accuracy_level="city",
|
||||
resolution_source="public_ip_discovery",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(permission_monitoring, "resolve_ip_location", fake_resolve_ip_location)
|
||||
monkeypatch.setattr(
|
||||
permission_monitoring,
|
||||
"resolve_ip_location",
|
||||
lambda ip: FakeIpInfo("广东省", "深圳市"),
|
||||
"resolve_monitoring_server_location",
|
||||
fake_resolve_monitoring_server_location,
|
||||
)
|
||||
|
||||
result = await permission_monitoring.get_ip_locations(db=db_session, _=AdminUserStub(), days=7, limit=10)
|
||||
@@ -396,6 +469,56 @@ async def test_ip_locations_counts_unique_users_per_location(db_session, monkeyp
|
||||
assert result["summary"]["total_count"] == 3
|
||||
assert result["summary"]["unique_ip_count"] == 2
|
||||
assert result["summary"]["unique_user_count"] == 2
|
||||
assert result["items"][0]["country_code"] == "CN"
|
||||
assert result["items"][0]["region_code"] == "CN-GD"
|
||||
assert result["items"][0]["longitude"] is not None
|
||||
assert result["items"][0]["risk_level"] == "attention"
|
||||
assert result["period"]["mode"] == "rolling"
|
||||
assert result["period"]["retention_days"] >= 7
|
||||
assert result["data_quality"]["located_ip_count"] == 2
|
||||
assert result["server_location"]["longitude"] == 106.5516
|
||||
assert result["server_location"]["resolution_source"] == "public_ip_discovery"
|
||||
assert sorted(resolved_ips) == ["10.1.1.1", "10.1.1.2"]
|
||||
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO permission_access_logs
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address, created_at)
|
||||
VALUES
|
||||
(lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' ||
|
||||
substr(lower(hex(randomblob(2))),2) || '-' ||
|
||||
substr('89ab', abs(random()) % 4 + 1, 1) ||
|
||||
substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6))),
|
||||
:study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address,
|
||||
datetime('now', '-120 days'))
|
||||
"""
|
||||
),
|
||||
{
|
||||
"study_id": study_id,
|
||||
"user_id": user_a,
|
||||
"endpoint_key": "admin.permissions.read",
|
||||
"role": "PM",
|
||||
"allowed": True,
|
||||
"elapsed_ms": 2.4,
|
||||
"ip_address": "10.1.1.3",
|
||||
},
|
||||
)
|
||||
await db_session.commit()
|
||||
|
||||
resolved_ips.clear()
|
||||
all_result = await permission_monitoring.get_ip_locations(
|
||||
db=db_session,
|
||||
_=AdminUserStub(),
|
||||
days=7,
|
||||
limit=10,
|
||||
all_time=True,
|
||||
)
|
||||
|
||||
assert all_result["days"] is None
|
||||
assert all_result["summary"]["total_count"] == 4
|
||||
assert all_result["summary"]["unique_ip_count"] == 3
|
||||
assert sorted(resolved_ips) == ["10.1.1.1", "10.1.1.2", "10.1.1.3"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -530,9 +653,11 @@ async def test_ip_locations_keeps_private_network_location_label(db_session, mon
|
||||
text(
|
||||
"""
|
||||
INSERT INTO permission_access_logs
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address, created_at)
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address,
|
||||
request_snapshot, created_at)
|
||||
VALUES
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address, CURRENT_TIMESTAMP)
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address,
|
||||
:request_snapshot, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{
|
||||
@@ -544,6 +669,7 @@ async def test_ip_locations_keeps_private_network_location_label(db_session, mon
|
||||
"allowed": True,
|
||||
"elapsed_ms": 3.2,
|
||||
"ip_address": "192.168.1.10",
|
||||
"request_snapshot": None,
|
||||
},
|
||||
)
|
||||
await db_session.commit()
|
||||
@@ -556,8 +682,8 @@ async def test_ip_locations_keeps_private_network_location_label(db_session, mon
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ip_locations_includes_abnormal_security_ips(db_session, monkeypatch):
|
||||
"""来源分析应同时统计安全事件中的异常来源 IP。"""
|
||||
async def test_ip_locations_scores_security_sources_by_behavior(db_session, monkeypatch):
|
||||
"""来源分析应按安全行为而非地域评估来源风险。"""
|
||||
await db_session.execute(text("DELETE FROM permission_access_logs"))
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
await db_session.commit()
|
||||
@@ -603,9 +729,11 @@ async def test_ip_locations_includes_abnormal_security_ips(db_session, monkeypat
|
||||
text(
|
||||
"""
|
||||
INSERT INTO permission_access_logs
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address, created_at)
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address,
|
||||
request_snapshot, created_at)
|
||||
VALUES
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address, CURRENT_TIMESTAMP)
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address,
|
||||
:request_snapshot, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{
|
||||
@@ -617,9 +745,10 @@ async def test_ip_locations_includes_abnormal_security_ips(db_session, monkeypat
|
||||
"allowed": True,
|
||||
"elapsed_ms": 3.2,
|
||||
"ip_address": "10.3.1.1",
|
||||
"request_snapshot": None,
|
||||
},
|
||||
)
|
||||
for ip_address in ["8.8.8.8", "1.1.1.1"]:
|
||||
for ip_address, status_code in [("8.8.8.8", 403), ("1.1.1.1", 200)]:
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
@@ -633,7 +762,7 @@ async def test_ip_locations_includes_abnormal_security_ips(db_session, monkeypat
|
||||
"id": str(uuid.uuid4()),
|
||||
"method": "GET",
|
||||
"path": "/api/v1/admin",
|
||||
"status_code": 403,
|
||||
"status_code": status_code,
|
||||
"elapsed_ms": 8.5,
|
||||
"client_ip": ip_address,
|
||||
"user_agent": "pytest",
|
||||
@@ -658,18 +787,23 @@ async def test_ip_locations_includes_abnormal_security_ips(db_session, monkeypat
|
||||
locations = {item["country"]: item for item in result["items"]}
|
||||
|
||||
assert result["summary"]["total_count"] == 3
|
||||
assert result["summary"]["denied_count"] == 2
|
||||
assert result["summary"]["denied_count"] == 1
|
||||
assert result["summary"]["unique_ip_count"] == 3
|
||||
assert result["summary"]["unique_user_count"] == 1
|
||||
assert locations["United States"]["total_count"] == 1
|
||||
assert locations["United States"]["denied_count"] == 1
|
||||
assert locations["United States"]["risk_level"] == "high"
|
||||
assert locations["United States"]["country_code"] == "US"
|
||||
assert locations["Australia"]["total_count"] == 1
|
||||
assert locations["Australia"]["denied_count"] == 0
|
||||
assert locations["Australia"]["risk_level"] == "normal"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_access_logs_include_user_behavior_summary(db_session):
|
||||
"""访问日志应返回用户行为审计汇总和用户排行。"""
|
||||
await db_session.execute(text("DELETE FROM permission_access_logs"))
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
await db_session.commit()
|
||||
|
||||
study_id = "00000000-0000-0000-0000-000000000201"
|
||||
@@ -784,10 +918,300 @@ def test_access_logs_user_stats_query_avoids_postgresql_uuid_max():
|
||||
assert "func.max(PermissionAccessLog.user_id)" not in source
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_access_logs_filter_by_account_location_and_client_source(db_session, monkeypatch):
|
||||
"""访问审计应支持按账号、IP 属地和客户端来源排查。"""
|
||||
await db_session.execute(text("DELETE FROM permission_access_logs"))
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
await db_session.commit()
|
||||
|
||||
study_id = "00000000-0000-0000-0000-000000001401"
|
||||
user_a = "00000000-0000-0000-0000-000000001501"
|
||||
user_b = "00000000-0000-0000-0000-000000001502"
|
||||
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO studies (id, code, name, status, is_locked, visit_schedule, active_roles)
|
||||
VALUES (:id, :code, :name, :status, :is_locked, :visit_schedule, :active_roles)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": study_id,
|
||||
"code": "ACCESS-CONTEXT-STUDY",
|
||||
"name": "Access Context Study",
|
||||
"status": "ACTIVE",
|
||||
"is_locked": False,
|
||||
"visit_schedule": "[]",
|
||||
"active_roles": "[]",
|
||||
},
|
||||
)
|
||||
for user_id, email, name in [
|
||||
(user_a, "audit-shanghai@example.com", "上海审计用户"),
|
||||
(user_b, "audit-beijing@example.com", "北京审计用户"),
|
||||
]:
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO users (id, email, password_hash, full_name, clinical_department, is_admin, status)
|
||||
VALUES (:id, :email, :password_hash, :full_name, :clinical_department, :is_admin, :status)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": user_id,
|
||||
"email": email,
|
||||
"password_hash": "hash",
|
||||
"full_name": name,
|
||||
"clinical_department": "临床运营",
|
||||
"is_admin": False,
|
||||
"status": "ACTIVE",
|
||||
},
|
||||
)
|
||||
|
||||
rows = [
|
||||
(user_a, "203.0.113.10", "web", "Chrome", "macos", {"user-agent": "Chrome", "x-request-id": "req-web"}),
|
||||
(user_b, "198.51.100.20", "desktop", "CTMS Desktop", "windows", {"user-agent": "CTMS Desktop"}),
|
||||
(user_a, "10.10.10.10", None, "curl/8.0", None, {"user-agent": "curl/8.0"}),
|
||||
]
|
||||
for user, ip_address, client_type, user_agent, platform, request_headers in rows:
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO permission_access_logs
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address,
|
||||
user_agent, client_type, client_version, client_platform, build_channel, build_commit, request_headers, created_at)
|
||||
VALUES
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address,
|
||||
:user_agent, :client_type, :client_version, :client_platform, :build_channel, :build_commit, :request_headers, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"study_id": study_id,
|
||||
"user_id": user,
|
||||
"endpoint_key": "admin.permissions.read",
|
||||
"role": "PM",
|
||||
"allowed": True,
|
||||
"elapsed_ms": 4.0,
|
||||
"ip_address": ip_address,
|
||||
"user_agent": user_agent,
|
||||
"client_type": client_type,
|
||||
"client_version": "0.1.0" if client_type else None,
|
||||
"client_platform": platform,
|
||||
"build_channel": "local" if client_type else None,
|
||||
"build_commit": "abcdef1" if client_type else None,
|
||||
"request_headers": json.dumps(request_headers),
|
||||
},
|
||||
)
|
||||
await db_session.commit()
|
||||
|
||||
ip_info_by_address = {
|
||||
"203.0.113.10": FakeIpInfo("上海市", "上海市", "电信"),
|
||||
"198.51.100.20": FakeIpInfo("北京市", "北京市", "联通"),
|
||||
"10.10.10.10": FakeIpInfo("", "", "", "局域网"),
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
permission_monitoring,
|
||||
"resolve_ip_location",
|
||||
lambda ip: ip_info_by_address[ip],
|
||||
)
|
||||
|
||||
result = await permission_monitoring.get_access_logs(
|
||||
db=db_session,
|
||||
_=AdminUserStub(),
|
||||
study_id=None,
|
||||
user_id=None,
|
||||
endpoint_key=None,
|
||||
role=None,
|
||||
allowed=None,
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
client_ip=None,
|
||||
client_type="web",
|
||||
keyword="上海",
|
||||
page=1,
|
||||
page_size=50,
|
||||
)
|
||||
|
||||
assert result["total"] == 1
|
||||
assert result["items"][0]["user_email"] == "audit-shanghai@example.com"
|
||||
assert result["items"][0]["client_type"] == "web"
|
||||
assert result["items"][0]["client_platform"] == "macos"
|
||||
assert result["items"][0]["user_agent"] == "Chrome"
|
||||
assert result["items"][0]["request_headers"]["x-request-id"] == "req-web"
|
||||
assert result["items"][0]["ip_city"] == "上海市"
|
||||
|
||||
unknown_result = await permission_monitoring.get_access_logs(
|
||||
db=db_session,
|
||||
_=AdminUserStub(),
|
||||
study_id=None,
|
||||
user_id=None,
|
||||
endpoint_key=None,
|
||||
role=None,
|
||||
allowed=None,
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
client_ip=None,
|
||||
client_type="unknown",
|
||||
keyword="curl",
|
||||
page=1,
|
||||
page_size=50,
|
||||
)
|
||||
assert unknown_result["total"] == 1
|
||||
assert unknown_result["items"][0]["ip_address"] == "10.10.10.10"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_access_logs_include_security_events_for_admin(db_session, monkeypatch):
|
||||
"""访问日志应合并业务权限日志和安全访问日志,避免匿名/探测请求审计盲区。"""
|
||||
await db_session.execute(text("DELETE FROM permission_access_logs"))
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
await db_session.commit()
|
||||
|
||||
study_id = "00000000-0000-0000-0000-000000001601"
|
||||
user_id = "00000000-0000-0000-0000-000000001701"
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO studies (id, code, name, status, is_locked, visit_schedule, active_roles)
|
||||
VALUES (:id, :code, :name, :status, :is_locked, :visit_schedule, :active_roles)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": study_id,
|
||||
"code": "ACCESS-SECURITY-FEED",
|
||||
"name": "Access Security Feed",
|
||||
"status": "ACTIVE",
|
||||
"is_locked": False,
|
||||
"visit_schedule": "[]",
|
||||
"active_roles": "[]",
|
||||
},
|
||||
)
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO users (id, email, password_hash, full_name, clinical_department, is_admin, status)
|
||||
VALUES (:id, :email, :password_hash, :full_name, :clinical_department, :is_admin, :status)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": user_id,
|
||||
"email": "access-feed@example.com",
|
||||
"password_hash": "hash",
|
||||
"full_name": "统一访问用户",
|
||||
"clinical_department": "临床运营",
|
||||
"is_admin": False,
|
||||
"status": "ACTIVE",
|
||||
},
|
||||
)
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO permission_access_logs
|
||||
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address,
|
||||
request_snapshot, created_at)
|
||||
VALUES
|
||||
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address,
|
||||
:request_snapshot, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"study_id": study_id,
|
||||
"user_id": user_id,
|
||||
"endpoint_key": "project_overview:read",
|
||||
"role": "PM",
|
||||
"allowed": True,
|
||||
"elapsed_ms": 3.0,
|
||||
"ip_address": "192.168.10.2",
|
||||
"request_snapshot": json.dumps(
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/projects/overview",
|
||||
"query_string": "tab=summary",
|
||||
"headers": {"user-agent": "Chrome", "x-request-id": "perm-1"},
|
||||
"body": {"captured": False, "reason": "body_not_captured_by_audit_policy"},
|
||||
}
|
||||
),
|
||||
},
|
||||
)
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO security_access_logs
|
||||
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status,
|
||||
user_identifier, request_headers, request_snapshot, created_at)
|
||||
VALUES
|
||||
(:id, :method, :path, :status_code, :elapsed_ms, :client_ip, :user_agent, :auth_status,
|
||||
:user_identifier, :request_headers, :request_snapshot, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"method": "GET",
|
||||
"path": "/api/.git/config",
|
||||
"status_code": 404,
|
||||
"elapsed_ms": 1.5,
|
||||
"client_ip": "45.148.10.95",
|
||||
"user_agent": "probe-bot/1.0",
|
||||
"auth_status": "ANONYMOUS",
|
||||
"user_identifier": None,
|
||||
"request_headers": json.dumps({"user-agent": "probe-bot/1.0", "x-request-id": "sec-1"}),
|
||||
"request_snapshot": json.dumps(
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/.git/config",
|
||||
"query_string": "token=[redacted]",
|
||||
"headers": {"user-agent": "probe-bot/1.0", "authorization": "[redacted]"},
|
||||
"body": {"captured": False, "reason": "body_not_captured_by_audit_policy"},
|
||||
}
|
||||
),
|
||||
},
|
||||
)
|
||||
await db_session.commit()
|
||||
|
||||
ip_info_by_address = {
|
||||
"192.168.10.2": FakeIpInfo("", "", "", "局域网"),
|
||||
"45.148.10.95": FakeIpInfo("South Holland", "", "", "Netherlands"),
|
||||
}
|
||||
monkeypatch.setattr(permission_monitoring, "resolve_ip_location", lambda ip: ip_info_by_address[ip])
|
||||
|
||||
result = await permission_monitoring.get_access_logs(
|
||||
db=db_session,
|
||||
_=AdminUserStub(),
|
||||
study_id=None,
|
||||
user_id=None,
|
||||
endpoint_key=None,
|
||||
role=None,
|
||||
allowed=None,
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
client_ip=None,
|
||||
client_type=None,
|
||||
keyword=None,
|
||||
page=1,
|
||||
page_size=50,
|
||||
)
|
||||
|
||||
assert result["total"] == 2
|
||||
assert result["summary"]["total_count"] == 2
|
||||
assert result["summary"]["denied_count"] == 1
|
||||
items_by_type = {item["event_type"]: item for item in result["items"]}
|
||||
assert items_by_type["permission"]["endpoint_key"] == "project_overview:read"
|
||||
assert items_by_type["security"]["status_code"] == 404
|
||||
assert items_by_type["security"]["allowed"] is False
|
||||
assert items_by_type["security"]["category"] == "PROBE"
|
||||
assert items_by_type["security"]["severity"] == "CRITICAL"
|
||||
assert items_by_type["security"]["request_headers"]["x-request-id"] == "sec-1"
|
||||
assert items_by_type["permission"]["request_snapshot"]["path"] == "/api/v1/projects/overview"
|
||||
assert items_by_type["security"]["request_snapshot"]["headers"]["authorization"] == "[redacted]"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_access_logs_ip_ranking_aggregates_by_ip_total(db_session):
|
||||
"""IP 访问排行应按同一 IP 的整体访问量聚合排序,而不是按用户/IP/角色拆分。"""
|
||||
await db_session.execute(text("DELETE FROM permission_access_logs"))
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
await db_session.commit()
|
||||
|
||||
study_id = "00000000-0000-0000-0000-000000001201"
|
||||
@@ -896,10 +1320,13 @@ async def test_security_access_logs_include_anonymous_ip_attempts(db_session, mo
|
||||
text(
|
||||
"""
|
||||
INSERT INTO security_access_logs
|
||||
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status, user_identifier, created_at)
|
||||
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status,
|
||||
user_identifier, request_snapshot, created_at)
|
||||
VALUES
|
||||
('00000000-0000-4000-8000-000000000501', 'POST', '/api/v1/auth/login', 401, 12.5,
|
||||
'203.0.113.10', 'attack-bot/1.0', 'ANONYMOUS', NULL, CURRENT_TIMESTAMP)
|
||||
'203.0.113.10', 'attack-bot/1.0', 'ANONYMOUS', NULL,
|
||||
'{"method":"POST","path":"/api/v1/auth/login","headers":{"authorization":"[redacted]"}}',
|
||||
CURRENT_TIMESTAMP)
|
||||
"""
|
||||
)
|
||||
)
|
||||
@@ -925,6 +1352,7 @@ async def test_security_access_logs_include_anonymous_ip_attempts(db_session, mo
|
||||
assert result["items"][0]["ip_isp"] == "联通"
|
||||
assert result["items"][0]["account_label"] == "未知账号"
|
||||
assert result["items"][0]["status_code"] == 401
|
||||
assert result["items"][0]["request_snapshot"]["headers"]["authorization"] == "[redacted]"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -993,8 +1421,8 @@ async def test_security_access_logs_prioritize_sensitive_probe_over_foreign_ip(d
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_security_access_logs_classify_non_china_ip_as_abnormal(db_session, monkeypatch):
|
||||
"""安全事件明细应把非中国公网 IP 归类为异常 IP。"""
|
||||
async def test_security_access_logs_classify_foreign_invalid_token_by_behavior(db_session, monkeypatch):
|
||||
"""海外来源不应单凭地域升级风险,仍按认证行为分类。"""
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
monkeypatch.setattr(
|
||||
permission_monitoring,
|
||||
@@ -1024,8 +1452,8 @@ async def test_security_access_logs_classify_non_china_ip_as_abnormal(db_session
|
||||
)
|
||||
|
||||
item = result["items"][0]
|
||||
assert item["category"] == "ABNORMAL_IP"
|
||||
assert item["severity"] == "HIGH"
|
||||
assert item["category"] == "INVALID_TOKEN"
|
||||
assert item["severity"] == "MEDIUM"
|
||||
assert item["ip_location"] == "United States / California / San Jose / Google"
|
||||
assert item["ip_country"] == "United States"
|
||||
|
||||
@@ -1062,4 +1490,108 @@ async def test_security_access_logs_resolve_public_ip_with_packaged_ip2region(db
|
||||
assert item["ip_country"] == "United States"
|
||||
assert item["ip_province"] == "California"
|
||||
assert item["ip_city"] == "San Jose"
|
||||
assert item["category"] == "ABNORMAL_IP"
|
||||
assert item["category"] == "NOT_FOUND_NOISE"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_access_logs_deduplicate_permission_and_security_rows_by_request_id(db_session):
|
||||
await db_session.execute(text("DELETE FROM permission_access_logs"))
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
request_id = str(uuid.uuid4())
|
||||
study_id = str(uuid.uuid4())
|
||||
user_id = str(uuid.uuid4())
|
||||
await _seed_permission_log(db_session, uuid.UUID(study_id), uuid.UUID(user_id), allowed=False, elapsed_ms=4.0)
|
||||
await db_session.execute(
|
||||
text("UPDATE permission_access_logs SET request_id = :request_id"),
|
||||
{"request_id": request_id},
|
||||
)
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO security_access_logs
|
||||
(id, method, path, status_code, elapsed_ms, client_ip, auth_status, user_identifier,
|
||||
request_id, category, severity, created_at)
|
||||
VALUES
|
||||
(:id, 'GET', '/api/v1/studies/denied', 403, 5.0, '203.0.113.20', 'AUTHENTICATED', :user_id,
|
||||
:request_id, 'OTHER', 'LOW', CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{"id": str(uuid.uuid4()), "user_id": user_id, "request_id": request_id},
|
||||
)
|
||||
await db_session.commit()
|
||||
|
||||
result = await permission_monitoring.get_access_logs(
|
||||
db=db_session,
|
||||
_=AdminUserStub(),
|
||||
study_id=None,
|
||||
user_id=None,
|
||||
endpoint_key=None,
|
||||
role=None,
|
||||
allowed=None,
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
client_ip=None,
|
||||
client_type=None,
|
||||
keyword=None,
|
||||
page=1,
|
||||
page_size=50,
|
||||
)
|
||||
|
||||
assert result["total"] == 1
|
||||
assert result["items"][0]["event_type"] == "security"
|
||||
assert result["items"][0]["request_id"] == request_id
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_security_events_only_includes_persisted_probe(db_session):
|
||||
await db_session.execute(text("DELETE FROM security_access_logs"))
|
||||
for category, ip in [("PROBE", "52.53.218.145"), ("OTHER", "203.0.113.20")]:
|
||||
await db_session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO security_access_logs
|
||||
(id, method, path, status_code, elapsed_ms, client_ip, auth_status,
|
||||
category, severity, created_at)
|
||||
VALUES
|
||||
(:id, 'GET', '/api/v1/studies/', 200, 3.0, :ip, 'AUTHENTICATED',
|
||||
:category, :severity, CURRENT_TIMESTAMP)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"ip": ip,
|
||||
"category": category,
|
||||
"severity": "CRITICAL" if category == "PROBE" else "LOW",
|
||||
},
|
||||
)
|
||||
await db_session.commit()
|
||||
|
||||
result = await permission_monitoring.get_security_access_logs(
|
||||
db=db_session,
|
||||
_=AdminUserStub(),
|
||||
status_min=None,
|
||||
auth_status=None,
|
||||
events_only=True,
|
||||
page=1,
|
||||
page_size=20,
|
||||
)
|
||||
|
||||
assert result["total"] == 1
|
||||
assert result["items"][0]["category"] == "PROBE"
|
||||
assert result["summary"]["high_risk_count"] == 1
|
||||
|
||||
|
||||
def test_server_error_classification_takes_priority_over_foreign_ip():
|
||||
log = SimpleNamespace(
|
||||
path="/api/v1/studies/",
|
||||
status_code=503,
|
||||
auth_status="AUTHENTICATED",
|
||||
category=None,
|
||||
severity=None,
|
||||
)
|
||||
location = FakeIpInfo("California", "San Jose", "Google", "United States")
|
||||
|
||||
assert permission_monitoring._classify_security_access_log(log, location) == {
|
||||
"category": "SERVER_ERROR",
|
||||
"severity": "HIGH",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user