cmmc_audit/migrations/0006_add_cmmc_evidence_fields.py
2026-05-27 15:47:47 -06:00

108 lines
5.1 KiB
Python

# Generated by Django 5.2.14 on 2026-05-20 22:17
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models
ACTION_EVIDENCE_MAP = {
"admin_addition": ("AU-Audit-Accountability", "AU", "django_admin"),
"admin_change": ("AU-Audit-Accountability", "AU", "django_admin"),
"admin_deletion": ("AU-Audit-Accountability", "AU", "django_admin"),
"user_pi_upgraded": ("AC-Access-Control", "AC", "runtime"),
"pi_status_revoked": ("AC-Access-Control", "AC", "runtime"),
"user_admin_privileges_changed": ("AC-Access-Control", "AC", "runtime"),
"allocation_requested": ("AC-Access-Control; AU-Audit-Accountability", "AC,AU", "runtime"),
"allocation_change_requested": ("AC-Access-Control; AU-Audit-Accountability", "AC,AU", "runtime"),
"allocation_status_changed": ("AC-Access-Control; AU-Audit-Accountability", "AC,AU", "runtime"),
"allocation_disabled": ("AC-Access-Control; AU-Audit-Accountability", "AC,AU", "runtime"),
"renewal_requested": ("AC-Access-Control; AU-Audit-Accountability", "AC,AU", "runtime"),
"renewal_approved": ("AC-Access-Control; AU-Audit-Accountability", "AC,AU", "runtime"),
"allocation_renewed": ("AC-Access-Control; AU-Audit-Accountability", "AC,AU", "runtime"),
"project_user_added": ("AC-Access-Control", "AC", "runtime"),
"project_user_removed": ("AC-Access-Control", "AC", "runtime"),
"project_user_role_changed": ("AC-Access-Control", "AC", "runtime"),
"project_review_forced": ("AU-Audit-Accountability", "AU", "runtime"),
"project_review_submitted": ("AU-Audit-Accountability", "AU", "runtime"),
"project_review_completed": ("AU-Audit-Accountability", "AU", "runtime"),
"project_review_status_changed": ("AU-Audit-Accountability", "AU", "runtime"),
"resource_created": ("CM-Configuration-Management", "CM", "runtime"),
"resource_changed": ("CM-Configuration-Management", "CM", "runtime"),
"resource_deleted": ("CM-Configuration-Management", "CM", "runtime"),
}
def backfill_cmmc_fields(apps, schema_editor):
AuditEvent = apps.get_model("cmmc_audit", "AuditEvent")
for event in AuditEvent.objects.all().iterator():
evidence_category, control_family, source = ACTION_EVIDENCE_MAP.get(event.action, ("", "", "runtime"))
event.event_time = event.timestamp
event.evidence_category = evidence_category
event.control_family = control_family
event.source = source
event.save(
update_fields=[
"event_time",
"evidence_category",
"control_family",
"source",
]
)
class Migration(migrations.Migration):
dependencies = [
('cmmc_audit', '0005_alter_auditevent_action'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.RemoveIndex(
model_name='auditevent',
name='carc_audit__timesta_8969da_idx',
),
migrations.AddField(
model_name='auditevent',
name='control_family',
field=models.CharField(blank=True, max_length=16),
),
migrations.AddField(
model_name='auditevent',
name='event_time',
field=models.DateTimeField(default=django.utils.timezone.now),
),
migrations.AddField(
model_name='auditevent',
name='evidence_category',
field=models.CharField(blank=True, choices=[('AC-Access-Control', 'AC - Access Control'), ('AU-Audit-Accountability', 'AU - Audit and Accountability'), ('AC-Access-Control; AU-Audit-Accountability', 'AC/AU - Access and Audit'), ('CM-Configuration-Management', 'CM - Configuration Management'), ('IA-Identification-Authentication', 'IA - Identification and Authentication')], max_length=128),
),
migrations.AddField(
model_name='auditevent',
name='is_reconstructed',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='auditevent',
name='source',
field=models.CharField(choices=[('runtime', 'Runtime'), ('django_admin', 'Django admin'), ('coldfront_signal', 'ColdFront signal'), ('coldfront_workflow', 'ColdFront workflow'), ('reconstructed_django_admin_log', 'Reconstructed Django admin log')], default='runtime', max_length=64),
),
migrations.AddField(
model_name='auditevent',
name='source_id',
field=models.CharField(blank=True, max_length=128),
),
migrations.RunPython(backfill_cmmc_fields, migrations.RunPython.noop),
migrations.AddIndex(
model_name='auditevent',
index=models.Index(fields=['event_time'], name='carc_audit__event_t_752462_idx'),
),
migrations.AddIndex(
model_name='auditevent',
index=models.Index(fields=['evidence_category'], name='carc_audit__evidenc_404a75_idx'),
),
migrations.AddIndex(
model_name='auditevent',
index=models.Index(fields=['source'], name='carc_audit__source_21deb0_idx'),
),
]