68 lines
4.0 KiB
Python
68 lines
4.0 KiB
Python
|
|
"""Read-only deployment preflight; never approves, backfills or grants access."""
|
||
|
|
from django.db import connections
|
||
|
|
from django.db.models import Count, F, Q
|
||
|
|
|
||
|
|
from control.models import PlanApplication, Subscription
|
||
|
|
|
||
|
|
|
||
|
|
def audit_plan_approvals(*, using="default", sample_limit=50):
|
||
|
|
if not 1 <= sample_limit <= 1000:
|
||
|
|
raise ValueError("sample_limit must be between 1 and 1000")
|
||
|
|
connection = connections[using]
|
||
|
|
with connection.cursor() as cursor:
|
||
|
|
columns = connection.introspection.get_table_description(cursor, PlanApplication._meta.db_table)
|
||
|
|
has_link = "subscription_id" in {column.name for column in columns}
|
||
|
|
applications = PlanApplication.objects.using(using).order_by()
|
||
|
|
subscriptions = Subscription.objects.using(using).order_by()
|
||
|
|
findings = []
|
||
|
|
|
||
|
|
def record(code, rows, fields, *, level="attention"):
|
||
|
|
count = rows.count()
|
||
|
|
if count:
|
||
|
|
findings.append({"code": code, "level": level, "count": count,
|
||
|
|
"samples": list(rows.order_by(*fields).values(*fields)[:sample_limit]),
|
||
|
|
"samples_truncated": count > sample_limit})
|
||
|
|
|
||
|
|
open_statuses = [Subscription.Status.PENDING, Subscription.Status.ACTIVE, Subscription.Status.SUSPENDED]
|
||
|
|
duplicate_open = subscriptions.filter(status__in=open_statuses).values("user_id").annotate(total=Count("pk")).filter(total__gt=1)
|
||
|
|
duplicate_pending = applications.filter(status=PlanApplication.Status.PENDING).values("user_id").annotate(total=Count("pk")).filter(total__gt=1)
|
||
|
|
record("multiple_open_subscriptions", duplicate_open, ["user_id"], level="blocker")
|
||
|
|
record("multiple_pending_applications", duplicate_pending, ["user_id"])
|
||
|
|
pending = applications.filter(status=PlanApplication.Status.PENDING)
|
||
|
|
stopped_accounts = pending.filter(
|
||
|
|
Q(user__is_active=False) | ~Q(user__account_status__in=["pending", "active"])
|
||
|
|
)
|
||
|
|
record("pending_account_no_longer_eligible", stopped_accounts, ["id", "user_id", "plan_id"])
|
||
|
|
ineligible = pending.filter(
|
||
|
|
Q(plan__active=False)
|
||
|
|
| (~Q(plan__audience="all") & ~Q(plan__audience=F("user__identity_type")))
|
||
|
|
| (Q(plan__employee_benefit=True) & ~Q(user__identity_type="employee"))
|
||
|
|
)
|
||
|
|
record("pending_plan_no_longer_eligible", ineligible, ["id", "user_id", "plan_id"])
|
||
|
|
approved = applications.filter(status=PlanApplication.Status.APPROVED)
|
||
|
|
# Explicit values/count queries work on both pre-0008 and post-0008 schemas:
|
||
|
|
# never instantiate the current model when the new column is absent.
|
||
|
|
unlinked = approved.filter(subscription_id__isnull=True) if has_link else approved
|
||
|
|
record("legacy_approved_application_without_link", unlinked, ["id", "user_id", "plan_id"])
|
||
|
|
linked_count = 0
|
||
|
|
if has_link:
|
||
|
|
linked = applications.filter(subscription_id__isnull=False)
|
||
|
|
linked_count = linked.count()
|
||
|
|
record("subscription_belongs_to_another_user", linked.exclude(user_id=F("subscription__user_id")),
|
||
|
|
["id", "user_id", "subscription_id"], level="blocker")
|
||
|
|
record("unapproved_application_has_subscription_link", linked.exclude(status=PlanApplication.Status.APPROVED),
|
||
|
|
["id", "user_id", "subscription_id"], level="blocker")
|
||
|
|
return {
|
||
|
|
"kind": "read_only_plan_approval_preflight", "database_modified": False,
|
||
|
|
"data_scope": "IDs and counts only; no names, email, credentials or subscription tokens",
|
||
|
|
"consistency": "Point-in-time query observations; rerun with approval writes paused before deployment",
|
||
|
|
"subscription_link_column_present": has_link, "migration_0008_required": not has_link,
|
||
|
|
"counts": {"applications": applications.count(), "pending": pending.count(),
|
||
|
|
"subscriptions": subscriptions.count(), "linked_applications": linked_count},
|
||
|
|
"schema_rollback_would_drop_link_count": linked_count,
|
||
|
|
"schema_rollback_requires_link_backup": linked_count > 0,
|
||
|
|
"findings": findings, "review_required": bool(findings),
|
||
|
|
"automatic_repair_performed": False,
|
||
|
|
"deployment_approved": False,
|
||
|
|
}
|