Skip to content

Commit 40569ba

Browse files
Auditlog: Add django-pghistory as audit log (optional for now) (#13169)
* working except data migration * update performance unit test * add management command to import scan * remove old test * restore __init__.py * fix tests * fix questionaire content type in testdata * fix delete testcases * fix tests * fix query counts * default to django-auditlog * fix query counts * update query counts and task counts * add indices * default to pghistory * ruff * migration * fix backfill * ruff * optimize diff filter * update counts * default to django-auditlog * use current user as actor for delete events * run unit tests for both auditlog-types * output more logs to verify auditlog-type * Revert "use current user as actor for delete events" This reverts commit 49986c7. * Reapply "use current user as actor for delete events" This reverts commit 4dc7312. * Revert "output more logs to verify auditlog-type" This reverts commit f5a0592. * optimize flush audit log * move helper * add flushing of pghistory entries * update counts * small reorder * fix tests
1 parent 7082611 commit 40569ba

33 files changed

+3067
-214
lines changed

.github/workflows/integration-tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ name: Integration tests
22

33
on:
44
workflow_call:
5+
inputs:
6+
auditlog_type:
7+
type: string
8+
default: "django-auditlog"
59

610
jobs:
711
integration_tests:
812
# run tests with docker compose
913
name: User Interface Tests
1014
runs-on: ubuntu-latest
15+
env:
16+
AUDITLOG_TYPE: ${{ inputs.auditlog_type }}
1117
strategy:
1218
matrix:
1319
test-case: [

.github/workflows/rest-framework-tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ on:
66
platform:
77
type: string
88
default: "linux/amd64"
9+
auditlog_type:
10+
type: string
11+
default: "django-auditlog"
912

1013
jobs:
1114
unit_tests:
1215
name: Rest Framework Unit Tests
1316
runs-on: ${{ inputs.platform == 'linux/arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }}
17+
env:
18+
AUDITLOG_TYPE: ${{ inputs.auditlog_type }}
1419

1520
strategy:
1621
matrix:

.github/workflows/unit-tests.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,26 @@ jobs:
2525
strategy:
2626
matrix:
2727
platform: ['linux/amd64', 'linux/arm64']
28+
auditlog_type: ['django-auditlog', 'django-pghistory']
2829
fail-fast: false
2930
needs: build-docker-containers
3031
uses: ./.github/workflows/rest-framework-tests.yml
3132
secrets: inherit
3233
with:
3334
platform: ${{ matrix.platform}}
35+
auditlog_type: ${{ matrix.auditlog_type }}
3436

3537
# only run integration tests for linux/amd64 (default)
3638
test-user-interface:
3739
needs: build-docker-containers
3840
uses: ./.github/workflows/integration-tests.yml
3941
secrets: inherit
42+
strategy:
43+
matrix:
44+
auditlog_type: ['django-auditlog', 'django-pghistory']
45+
fail-fast: false
46+
with:
47+
auditlog_type: ${{ matrix.auditlog_type }}
4048

4149
# only run k8s tests for linux/amd64 (default)
4250
test-k8s:

docker/entrypoint-initializer.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ python3 manage.py makemigrations --no-input --check --dry-run --verbosity 3 || {
110110
cat <<-EOF
111111
112112
********************************************************************************
113+
WARNING: Missing Database Migrations Detected
114+
********************************************************************************
113115
114116
You made changes to the models without creating a DB migration for them.
115117
@@ -119,15 +121,25 @@ If you're not familiar with migrations in Django, please read the
119121
great documentation thoroughly:
120122
https://docs.djangoproject.com/en/5.0/topics/migrations/
121123
124+
This is now a WARNING and the container will continue to start.
125+
However, you should create the necessary migrations as soon as possible using:
126+
docker compose exec uwsgi bash -c 'python manage.py makemigrations -v2'
127+
122128
********************************************************************************
123129
124130
EOF
125-
exit 1
131+
echo "WARNING: Continuing startup despite missing migrations..."
126132
}
127133

128134
echo "Migrating"
129135
python3 manage.py migrate
130136

137+
echo "Configuring pghistory triggers based on audit settings"
138+
cat <<EOD | python3 manage.py shell
139+
from dojo.auditlog import configure_pghistory_triggers
140+
configure_pghistory_triggers()
141+
EOD
142+
131143
echo "Admin user: ${DD_ADMIN_USER}"
132144
ADMIN_EXISTS=$(echo "SELECT * from auth_user;" | python manage.py dbshell | grep "${DD_ADMIN_USER}" || true)
133145
# Abort if the admin user already exists, instead of giving a new fake password that won't work

dojo/admin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from auditlog.models import LogEntry
21
from django.contrib import admin
2+
from django.contrib.admin.sites import NotRegistered
33
from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicParentModelAdmin
44

55
from dojo.models import (
@@ -14,7 +14,13 @@
1414
TextQuestion,
1515
)
1616

17-
admin.site.unregister(LogEntry)
17+
# Conditionally unregister LogEntry from auditlog if it's registered
18+
try:
19+
from auditlog.models import LogEntry
20+
admin.site.unregister(LogEntry)
21+
except (ImportError, NotRegistered):
22+
# auditlog not available or LogEntry not registered
23+
pass
1824

1925
# ==============================
2026
# Defect Dojo Engaegment Surveys

dojo/apps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.db import models
66
from watson import search as watson
77

8+
from dojo.auditlog import configure_audit_system, register_django_pghistory_models
89
from dojo.checks import check_configuration_deduplication
910

1011
logger = logging.getLogger(__name__)
@@ -87,6 +88,13 @@ def ready(self):
8788
import dojo.test.signals # noqa: PLC0415 raised: AppRegistryNotReady
8889
import dojo.tool_product.signals # noqa: F401,PLC0415 raised: AppRegistryNotReady
8990

91+
# Configure audit system after all models are loaded
92+
# This must be done in ready() to avoid "Models aren't loaded yet" errors
93+
# Note: pghistory models are registered here (no database access), but trigger
94+
# enabling is handled via management command to avoid database access warnings
95+
register_django_pghistory_models()
96+
configure_audit_system()
97+
9098

9199
def get_model_fields_with_extra(model, extra_fields=()):
92100
return get_model_fields(get_model_default_fields(model), extra_fields)

0 commit comments

Comments
 (0)