refactor: remove db.commit() from audit_service.log_action, all callers use UoW
This commit is contained in:
@@ -30,8 +30,9 @@ from app.services.campaign_crud_service import (
|
||||
serialize_campaign,
|
||||
update_campaign as crud_update,
|
||||
)
|
||||
from app.services.notification_service import notify_role
|
||||
from app.domain.unit_of_work import UnitOfWork
|
||||
from app.services.audit_service import log_action
|
||||
from app.services.notification_service import notify_role
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -108,27 +109,27 @@ def create_campaign(
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
"""Create a new campaign."""
|
||||
result = crud_create(
|
||||
db,
|
||||
creator_id=current_user.id,
|
||||
name=payload.name,
|
||||
description=payload.description,
|
||||
type=payload.type,
|
||||
threat_actor_id=payload.threat_actor_id,
|
||||
target_platform=payload.target_platform,
|
||||
tags=payload.tags,
|
||||
scheduled_at=payload.scheduled_at,
|
||||
)
|
||||
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="create_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=result["id"],
|
||||
details={"name": payload.name, "type": payload.type},
|
||||
)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
result = crud_create(
|
||||
db,
|
||||
creator_id=current_user.id,
|
||||
name=payload.name,
|
||||
description=payload.description,
|
||||
type=payload.type,
|
||||
threat_actor_id=payload.threat_actor_id,
|
||||
target_platform=payload.target_platform,
|
||||
tags=payload.tags,
|
||||
scheduled_at=payload.scheduled_at,
|
||||
)
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="create_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=result["id"],
|
||||
details={"name": payload.name, "type": payload.type},
|
||||
)
|
||||
uow.commit()
|
||||
|
||||
return result
|
||||
|
||||
@@ -160,23 +161,23 @@ def update_campaign(
|
||||
):
|
||||
"""Update a campaign. Only allowed in draft or active state."""
|
||||
update_data = payload.model_dump(exclude_unset=True)
|
||||
result = crud_update(
|
||||
db,
|
||||
campaign_id,
|
||||
updater_id=current_user.id,
|
||||
updater_role=current_user.role,
|
||||
**update_data,
|
||||
)
|
||||
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="update_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign_id,
|
||||
details={"updated_fields": list(update_data.keys())},
|
||||
)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
result = crud_update(
|
||||
db,
|
||||
campaign_id,
|
||||
updater_id=current_user.id,
|
||||
updater_role=current_user.role,
|
||||
**update_data,
|
||||
)
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="update_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign_id,
|
||||
details={"updated_fields": list(update_data.keys())},
|
||||
)
|
||||
uow.commit()
|
||||
|
||||
return result
|
||||
|
||||
@@ -193,15 +194,16 @@ def add_test_to_campaign(
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
"""Add a test to a campaign with optional ordering and dependency."""
|
||||
result = crud_add_test(
|
||||
db,
|
||||
campaign_id,
|
||||
test_id=payload.test_id,
|
||||
order_index=payload.order_index,
|
||||
depends_on=payload.depends_on,
|
||||
phase=payload.phase,
|
||||
)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
result = crud_add_test(
|
||||
db,
|
||||
campaign_id,
|
||||
test_id=payload.test_id,
|
||||
order_index=payload.order_index,
|
||||
depends_on=payload.depends_on,
|
||||
phase=payload.phase,
|
||||
)
|
||||
uow.commit()
|
||||
return result
|
||||
|
||||
|
||||
@@ -217,8 +219,9 @@ def remove_test_from_campaign(
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
"""Remove a test from a campaign."""
|
||||
crud_remove_test(db, campaign_id, campaign_test_id)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
crud_remove_test(db, campaign_id, campaign_test_id)
|
||||
uow.commit()
|
||||
return {"detail": "Test removed from campaign"}
|
||||
|
||||
|
||||
@@ -233,29 +236,28 @@ def activate_campaign(
|
||||
current_user: User = Depends(require_any_role("red_lead", "blue_lead")),
|
||||
):
|
||||
"""Activate a campaign, moving it from draft to active."""
|
||||
campaign = crud_activate(db, campaign_id)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
campaign = crud_activate(db, campaign_id)
|
||||
notify_role(
|
||||
db,
|
||||
role="red_tech",
|
||||
type="campaign_activated",
|
||||
title="Campaign activated",
|
||||
message=f'Campaign "{campaign.name}" has been activated.',
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
)
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="activate_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={"name": campaign.name},
|
||||
)
|
||||
uow.commit()
|
||||
db.refresh(campaign)
|
||||
|
||||
notify_role(
|
||||
db,
|
||||
role="red_tech",
|
||||
type="campaign_activated",
|
||||
title="Campaign activated",
|
||||
message=f'Campaign "{campaign.name}" has been activated.',
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
)
|
||||
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="activate_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={"name": campaign.name},
|
||||
)
|
||||
|
||||
return serialize_campaign(db, campaign)
|
||||
|
||||
|
||||
@@ -270,19 +272,19 @@ def complete_campaign(
|
||||
current_user: User = Depends(require_any_role("red_lead", "admin")),
|
||||
):
|
||||
"""Mark a campaign as completed."""
|
||||
campaign = crud_complete(db, campaign_id)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
campaign = crud_complete(db, campaign_id)
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="complete_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={"name": campaign.name},
|
||||
)
|
||||
uow.commit()
|
||||
db.refresh(campaign)
|
||||
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="complete_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={"name": campaign.name},
|
||||
)
|
||||
|
||||
return serialize_campaign(db, campaign)
|
||||
|
||||
|
||||
@@ -321,14 +323,16 @@ def generate_campaign_from_actor(
|
||||
current_user,
|
||||
)
|
||||
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="generate_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={"actor_id": actor_id, "campaign_name": campaign.name},
|
||||
)
|
||||
with UnitOfWork(db) as uow:
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="generate_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={"actor_id": actor_id, "campaign_name": campaign.name},
|
||||
)
|
||||
uow.commit()
|
||||
|
||||
return serialize_campaign(db, campaign)
|
||||
|
||||
@@ -348,31 +352,31 @@ def schedule_campaign(
|
||||
|
||||
Only the campaign creator or admin can change scheduling.
|
||||
"""
|
||||
campaign = crud_schedule(
|
||||
db,
|
||||
campaign_id,
|
||||
owner_id=current_user.id,
|
||||
owner_role=current_user.role,
|
||||
is_recurring=payload.is_recurring,
|
||||
recurrence_pattern=payload.recurrence_pattern,
|
||||
next_run_at=payload.next_run_at,
|
||||
)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
campaign = crud_schedule(
|
||||
db,
|
||||
campaign_id,
|
||||
owner_id=current_user.id,
|
||||
owner_role=current_user.role,
|
||||
is_recurring=payload.is_recurring,
|
||||
recurrence_pattern=payload.recurrence_pattern,
|
||||
next_run_at=payload.next_run_at,
|
||||
)
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="schedule_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={
|
||||
"is_recurring": campaign.is_recurring,
|
||||
"recurrence_pattern": campaign.recurrence_pattern,
|
||||
"next_run_at": campaign.next_run_at.isoformat() if campaign.next_run_at else None,
|
||||
},
|
||||
)
|
||||
uow.commit()
|
||||
db.refresh(campaign)
|
||||
|
||||
log_action(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
action="schedule_campaign",
|
||||
entity_type="campaign",
|
||||
entity_id=campaign.id,
|
||||
details={
|
||||
"is_recurring": campaign.is_recurring,
|
||||
"recurrence_pattern": campaign.recurrence_pattern,
|
||||
"next_run_at": campaign.next_run_at.isoformat() if campaign.next_run_at else None,
|
||||
},
|
||||
)
|
||||
|
||||
return serialize_campaign(db, campaign)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user