django-sec-audit 0.1.0a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_sec_audit-0.1.0a1/.gitignore +216 -0
- django_sec_audit-0.1.0a1/LICENSE +21 -0
- django_sec_audit-0.1.0a1/PKG-INFO +238 -0
- django_sec_audit-0.1.0a1/README.md +192 -0
- django_sec_audit-0.1.0a1/docs/how-to-use.md +362 -0
- django_sec_audit-0.1.0a1/docs/loki-setup.md +205 -0
- django_sec_audit-0.1.0a1/pyproject.toml +69 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/__init__.py +1 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/apps.py +29 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/checks.py +150 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/config.py +224 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/events.py +249 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/__init__.py +25 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/auth.py +98 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/body.py +205 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/drf.py +94 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/formatters.py +30 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/identity.py +103 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/model.py +87 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/request_info.py +43 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/routes.py +35 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/logging/sessions.py +29 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/middleware.py +195 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/runtime.py +119 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/utils/__init__.py +0 -0
- django_sec_audit-0.1.0a1/src/sec_audit/django/utils/request.py +12 -0
- django_sec_audit-0.1.0a1/tests/test_checks.py +129 -0
- django_sec_audit-0.1.0a1/tests/test_context_lifecycle.py +174 -0
- django_sec_audit-0.1.0a1/tests/test_events.py +743 -0
- django_sec_audit-0.1.0a1/tests/test_fail_open.py +110 -0
- django_sec_audit-0.1.0a1/tests/test_formatter_factory.py +74 -0
- django_sec_audit-0.1.0a1/tests/test_integration_opt_in.py +107 -0
- django_sec_audit-0.1.0a1/tests/test_middleware_defaults.py +168 -0
- django_sec_audit-0.1.0a1/tests/test_request_info.py +94 -0
- django_sec_audit-0.1.0a1/tests/test_sessions.py +167 -0
- django_sec_audit-0.1.0a1/tests/test_username_gating.py +56 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
logs/*
|
|
61
|
+
!logs/.gitkeep
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
89
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
90
|
+
# .python-version
|
|
91
|
+
|
|
92
|
+
# pipenv
|
|
93
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
94
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
95
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
96
|
+
# install all needed dependencies.
|
|
97
|
+
#Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# UV
|
|
100
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
101
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
102
|
+
# commonly ignored for libraries.
|
|
103
|
+
#uv.lock
|
|
104
|
+
|
|
105
|
+
# poetry
|
|
106
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
107
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
108
|
+
# commonly ignored for libraries.
|
|
109
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
110
|
+
#poetry.lock
|
|
111
|
+
#poetry.toml
|
|
112
|
+
|
|
113
|
+
# pdm
|
|
114
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
115
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
116
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
117
|
+
#pdm.lock
|
|
118
|
+
#pdm.toml
|
|
119
|
+
.pdm-python
|
|
120
|
+
.pdm-build/
|
|
121
|
+
|
|
122
|
+
# pixi
|
|
123
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
124
|
+
#pixi.lock
|
|
125
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
126
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
127
|
+
.pixi
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# SageMath parsed files
|
|
137
|
+
*.sage.py
|
|
138
|
+
|
|
139
|
+
# Environments
|
|
140
|
+
.env
|
|
141
|
+
.envrc
|
|
142
|
+
.venv
|
|
143
|
+
env/
|
|
144
|
+
venv/
|
|
145
|
+
ENV/
|
|
146
|
+
env.bak/
|
|
147
|
+
venv.bak/
|
|
148
|
+
|
|
149
|
+
# Spyder project settings
|
|
150
|
+
.spyderproject
|
|
151
|
+
.spyproject
|
|
152
|
+
|
|
153
|
+
# Rope project settings
|
|
154
|
+
.ropeproject
|
|
155
|
+
|
|
156
|
+
# mkdocs documentation
|
|
157
|
+
/site
|
|
158
|
+
|
|
159
|
+
# mypy
|
|
160
|
+
.mypy_cache/
|
|
161
|
+
.dmypy.json
|
|
162
|
+
dmypy.json
|
|
163
|
+
|
|
164
|
+
# Pyre type checker
|
|
165
|
+
.pyre/
|
|
166
|
+
|
|
167
|
+
# pytype static type analyzer
|
|
168
|
+
.pytype/
|
|
169
|
+
|
|
170
|
+
# Cython debug symbols
|
|
171
|
+
cython_debug/
|
|
172
|
+
|
|
173
|
+
# Celery beat schedule files
|
|
174
|
+
celerybeat-schedule
|
|
175
|
+
celerybeat.pid
|
|
176
|
+
celerybeat-schedule.db
|
|
177
|
+
|
|
178
|
+
# PyCharm
|
|
179
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
180
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
181
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
182
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
183
|
+
#.idea/
|
|
184
|
+
|
|
185
|
+
# Abstra
|
|
186
|
+
# Abstra is an AI-powered process automation framework.
|
|
187
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
188
|
+
# Learn more at https://abstra.io/docs
|
|
189
|
+
.abstra/
|
|
190
|
+
|
|
191
|
+
# Visual Studio Code
|
|
192
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
193
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
194
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
195
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
196
|
+
# .vscode/
|
|
197
|
+
|
|
198
|
+
# Ruff stuff:
|
|
199
|
+
.ruff_cache/
|
|
200
|
+
|
|
201
|
+
# PyPI configuration file
|
|
202
|
+
.pypirc
|
|
203
|
+
|
|
204
|
+
# Cursor
|
|
205
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
206
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
207
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
208
|
+
.cursorignore
|
|
209
|
+
.cursorindexingignore
|
|
210
|
+
|
|
211
|
+
# Marimo
|
|
212
|
+
marimo/_static/
|
|
213
|
+
marimo/_lsp/
|
|
214
|
+
__marimo__/
|
|
215
|
+
|
|
216
|
+
.DS_Store
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ammar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-sec-audit
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Structured security and audit logging for Django with OpenTelemetry LogRecord-shaped JSONL output
|
|
5
|
+
Project-URL: Homepage, https://github.com/ammar39/sec-audit
|
|
6
|
+
Project-URL: Repository, https://github.com/ammar39/sec-audit
|
|
7
|
+
Project-URL: Documentation, https://github.com/ammar39/sec-audit/tree/main/packages/django-sec-audit/docs
|
|
8
|
+
Project-URL: Changelog, https://github.com/ammar39/sec-audit/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Issues, https://github.com/ammar39/sec-audit/issues
|
|
10
|
+
Author-email: Ammar <ammarwaleed@proton.me>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: audit,django,drf,logging,loki,opentelemetry,security,siem
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Framework :: Django
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Security
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: django<6.1,>=5.2
|
|
27
|
+
Requires-Dist: sec-audit-logging<0.2,>=0.1.0a1
|
|
28
|
+
Requires-Dist: sec-audit<0.2,>=0.1.0a1
|
|
29
|
+
Provides-Extra: demo
|
|
30
|
+
Requires-Dist: django-auditlog>=2.0.0; extra == 'demo'
|
|
31
|
+
Requires-Dist: djangorestframework>=3.15; extra == 'demo'
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
34
|
+
Requires-Dist: playwright>=1.44; extra == 'dev'
|
|
35
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
36
|
+
Requires-Dist: ruff>=0.8; extra == 'dev'
|
|
37
|
+
Requires-Dist: twine>=5.0; extra == 'dev'
|
|
38
|
+
Provides-Extra: drf
|
|
39
|
+
Requires-Dist: djangorestframework>=3.15; extra == 'drf'
|
|
40
|
+
Provides-Extra: full
|
|
41
|
+
Requires-Dist: django-auditlog>=2.0.0; extra == 'full'
|
|
42
|
+
Requires-Dist: djangorestframework>=3.15; extra == 'full'
|
|
43
|
+
Provides-Extra: model
|
|
44
|
+
Requires-Dist: django-auditlog>=2.0.0; extra == 'model'
|
|
45
|
+
Description-Content-Type: text/markdown
|
|
46
|
+
|
|
47
|
+
# django-sec-audit
|
|
48
|
+
|
|
49
|
+
Structured security and audit logging for Django, emitting [OpenTelemetry LogRecord](https://opentelemetry.io/docs/specs/otel/logs/data-model/)-shaped JSONL events.
|
|
50
|
+
|
|
51
|
+
Captures HTTP request/response metadata, auth events (login, logout, failures), model changes (via django-auditlog), and DRF view metadata out of the box.
|
|
52
|
+
|
|
53
|
+
## Features
|
|
54
|
+
|
|
55
|
+
- **HTTP middleware** — automatic capture of requests, responses, status codes, timing, client IP, routes, DRF metadata
|
|
56
|
+
- **Auth signals** — automatic logging of `user_logged_in`, `user_logged_out`, `user_login_failed`
|
|
57
|
+
- **Model forwarding** — forward django-auditlog entries as structured audit events (optional `[model]` extra)
|
|
58
|
+
- **DRF integration** — auto-detects `drf_action`, `drf_view_class`, serializer, auth/permission classes (optional `[drf]` extra)
|
|
59
|
+
- **Request body capture** — opt-in JSON body capture with scrubbing and size limits
|
|
60
|
+
- **OTel JSONL output** — every event is a single JSON line following the OTel LogRecord envelope, ready for Loki/Grafana
|
|
61
|
+
- **Pluggable pipeline** — custom filters and enrichers run before emission
|
|
62
|
+
|
|
63
|
+
## Dependencies
|
|
64
|
+
|
|
65
|
+
| Package | Role |
|
|
66
|
+
|---------|------|
|
|
67
|
+
| `django-sec-audit` (this) | Django integration |
|
|
68
|
+
| `sec-audit` | Core (events, context, IP resolution, scrubbing) |
|
|
69
|
+
| `sec-audit-logging` | Logging runtime, formatters, SIEM handlers |
|
|
70
|
+
|
|
71
|
+
## Quick Start
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
# settings.py
|
|
75
|
+
|
|
76
|
+
INSTALLED_APPS = [
|
|
77
|
+
'sec_audit.django.apps.SecAuditConfig', # early, before your apps
|
|
78
|
+
# ...
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
MIDDLEWARE = [
|
|
82
|
+
# ... Django security middleware
|
|
83
|
+
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
84
|
+
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
85
|
+
'sec_audit.django.middleware.AuditMiddleware', # last or near-last
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
SEC_AUDIT = {
|
|
89
|
+
'core': {
|
|
90
|
+
'source': 'myapp', # appears as resource.service.name
|
|
91
|
+
'log_request_bodies': False, # opt-in
|
|
92
|
+
'log_ok_responses': False, # only 4xx/5xx by default
|
|
93
|
+
},
|
|
94
|
+
'logging': {
|
|
95
|
+
'schema_version': '1.0',
|
|
96
|
+
},
|
|
97
|
+
'django': {
|
|
98
|
+
'include_usernames': False, # opt-in for GDPR considerations
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
LOGGING = {
|
|
103
|
+
'version': 1,
|
|
104
|
+
'disable_existing_loggers': False,
|
|
105
|
+
'formatters': {
|
|
106
|
+
# Injects the resolved SEC_AUDIT config at construction.
|
|
107
|
+
'audit_jsonl': {
|
|
108
|
+
'()': 'sec_audit.django.logging.formatters.audit_jsonl_formatter',
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
'handlers': {
|
|
112
|
+
'audit_stdout': {
|
|
113
|
+
'class': 'logging.StreamHandler',
|
|
114
|
+
'formatter': 'audit_jsonl',
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
'loggers': {
|
|
118
|
+
'sec_audit.audit': {
|
|
119
|
+
'handlers': ['audit_stdout'],
|
|
120
|
+
'level': 'INFO',
|
|
121
|
+
'propagate': False,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Stdout is the supported delivery path (stdout JSONL -> Grafana Alloy -> Loki).
|
|
128
|
+
For local file output, swap the handler for a stdlib
|
|
129
|
+
`logging.handlers.RotatingFileHandler` using the same `audit_jsonl` formatter.
|
|
130
|
+
|
|
131
|
+
## Usage Guide
|
|
132
|
+
|
|
133
|
+
For a step-by-step setup and configuration walkthrough, see
|
|
134
|
+
[docs/how-to-use.md](https://github.com/ammar39/sec-audit/blob/main/packages/django-sec-audit/docs/how-to-use.md).
|
|
135
|
+
|
|
136
|
+
## Monitoring with Loki/Grafana
|
|
137
|
+
|
|
138
|
+
Audit events are OTel JSONL, ready to ship to Loki (`sec_audit.audit` JSONL → Grafana
|
|
139
|
+
Alloy → Loki → Grafana). The bundled `sec-audit-loki-init` command generates the whole
|
|
140
|
+
local stack — see
|
|
141
|
+
[docs/loki-setup.md](https://github.com/ammar39/sec-audit/blob/main/packages/django-sec-audit/docs/loki-setup.md).
|
|
142
|
+
|
|
143
|
+
## Configuration
|
|
144
|
+
|
|
145
|
+
All settings live under the `SEC_AUDIT` dict in `settings.py`. Three sections are recognised:
|
|
146
|
+
|
|
147
|
+
### `core` (CoreAuditConfig)
|
|
148
|
+
|
|
149
|
+
| Setting | Default | Description |
|
|
150
|
+
|---------|---------|-------------|
|
|
151
|
+
| `source` | `'sec-audit'` | Service name in `resource.service.name` |
|
|
152
|
+
| `ignore_paths` | `()` | Regex patterns; matching paths are skipped |
|
|
153
|
+
| `ignore_status_codes` | `frozenset()` | Status code values to skip (e.g. `{301, 302}`) |
|
|
154
|
+
| `sample_rate` | `1.0` | Sampling rate for successful (2xx) responses |
|
|
155
|
+
| `log_ok_responses` | `False` | Enable logging for 2xx responses |
|
|
156
|
+
| `log_request_bodies` | `False` | Enable request body capture |
|
|
157
|
+
| `log_body_paths` | `()` | Regex patterns; only matching paths capture bodies |
|
|
158
|
+
| `body_methods` | `{'POST', 'PUT', 'PATCH'}` | HTTP methods eligible for body capture |
|
|
159
|
+
| `max_body_bytes` | `4096` | Maximum JSON body size in bytes |
|
|
160
|
+
| `sensitive_keys` | `DEFAULT_SENSITIVE_KEYS` | Built-in key patterns to scrub (incl. `password`, `secret`, `token`, `apikey`, etc.) |
|
|
161
|
+
| `sensitive_key_allowlist` | `()` | Exact (compacted) field names never redacted, even when a `sensitive_keys` substring matches — e.g. `credit_card_last4`, `token_expiry` |
|
|
162
|
+
| `sensitive_value_patterns` | `()` | Regex patterns matching sensitive values to scrub |
|
|
163
|
+
|
|
164
|
+
### `logging` (LoggingAuditConfig)
|
|
165
|
+
|
|
166
|
+
| Setting | Default | Description |
|
|
167
|
+
|---------|---------|-------------|
|
|
168
|
+
| `schema_version` | `'1.0'` | Schema version string in every event |
|
|
169
|
+
| `projection_limits` | `ProjectionLimits()` | Bounds for dict/list nesting and string sizes (accepts a dict or `ProjectionLimits`) |
|
|
170
|
+
|
|
171
|
+
File-rotation parameters (`maxBytes`/`backupCount`/`filename`) are configured on
|
|
172
|
+
the handler in Django's `LOGGING` dict, not here.
|
|
173
|
+
|
|
174
|
+
### `django` (DjangoAuditConfig)
|
|
175
|
+
|
|
176
|
+
| Setting | Default | Description |
|
|
177
|
+
|---------|---------|-------------|
|
|
178
|
+
| `include_usernames` | `False` | Include `user.name` in events (opt-in for GDPR) |
|
|
179
|
+
| `trusted_proxy_cidrs` | `()` | CIDR ranges considered trusted proxies |
|
|
180
|
+
| `trusted_proxy_count` | `None` | Number of trusted proxies (leftmost N IPs are strip) |
|
|
181
|
+
| `emit_session_id` | `False` | Emit correlated `session.id` in events (opt-in; writes to `request.session`) |
|
|
182
|
+
| `filters` | `()` | Dotted paths to filter callable/classes |
|
|
183
|
+
| `enrichers` | `()` | Dotted paths to enricher callable/classes |
|
|
184
|
+
|
|
185
|
+
### Event Types
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
auth.login.success auth.login.failed
|
|
189
|
+
auth.logout.success auth.logout.unknown
|
|
190
|
+
http.response.success http.response.client_error http.response.server_error
|
|
191
|
+
model.create model.update model.delete model.access
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Output Format
|
|
195
|
+
|
|
196
|
+
Each event is a single JSON line:
|
|
197
|
+
|
|
198
|
+
```jsonc
|
|
199
|
+
{
|
|
200
|
+
"timestamp": 1712345678000000000,
|
|
201
|
+
"observed_timestamp": 1712345678000000000,
|
|
202
|
+
"severity_text": "WARNING",
|
|
203
|
+
"severity_number": 13,
|
|
204
|
+
"body": "http.response",
|
|
205
|
+
"resource": { "service.name": "sec-audit" },
|
|
206
|
+
"instrumentation_scope": { "name": "sec_audit.django.middleware", "version": "1.0.0" },
|
|
207
|
+
"attributes": {
|
|
208
|
+
"event_type": "http.response.client_error",
|
|
209
|
+
"source.address": "203.0.113.10",
|
|
210
|
+
"http.request.method": "POST",
|
|
211
|
+
"http.response.status_code": 404,
|
|
212
|
+
"url.full": "https://example.test/api/transfer",
|
|
213
|
+
"url.path": "/api/transfer",
|
|
214
|
+
"user.id": "42"
|
|
215
|
+
},
|
|
216
|
+
"event_name": "http.response.client_error"
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Optional Extras
|
|
221
|
+
|
|
222
|
+
| Extra | Dependencies |
|
|
223
|
+
|-------|-------------|
|
|
224
|
+
| `pip install django-sec-audit[model]` | django-auditlog |
|
|
225
|
+
| `pip install django-sec-audit[drf]` | djangorestframework |
|
|
226
|
+
| `pip install django-sec-audit[full]` | Both |
|
|
227
|
+
|
|
228
|
+
## Development
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
pip install -e "packages/django-sec-audit[dev]"
|
|
232
|
+
pytest
|
|
233
|
+
ruff check .
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## License
|
|
237
|
+
|
|
238
|
+
MIT
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# django-sec-audit
|
|
2
|
+
|
|
3
|
+
Structured security and audit logging for Django, emitting [OpenTelemetry LogRecord](https://opentelemetry.io/docs/specs/otel/logs/data-model/)-shaped JSONL events.
|
|
4
|
+
|
|
5
|
+
Captures HTTP request/response metadata, auth events (login, logout, failures), model changes (via django-auditlog), and DRF view metadata out of the box.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **HTTP middleware** — automatic capture of requests, responses, status codes, timing, client IP, routes, DRF metadata
|
|
10
|
+
- **Auth signals** — automatic logging of `user_logged_in`, `user_logged_out`, `user_login_failed`
|
|
11
|
+
- **Model forwarding** — forward django-auditlog entries as structured audit events (optional `[model]` extra)
|
|
12
|
+
- **DRF integration** — auto-detects `drf_action`, `drf_view_class`, serializer, auth/permission classes (optional `[drf]` extra)
|
|
13
|
+
- **Request body capture** — opt-in JSON body capture with scrubbing and size limits
|
|
14
|
+
- **OTel JSONL output** — every event is a single JSON line following the OTel LogRecord envelope, ready for Loki/Grafana
|
|
15
|
+
- **Pluggable pipeline** — custom filters and enrichers run before emission
|
|
16
|
+
|
|
17
|
+
## Dependencies
|
|
18
|
+
|
|
19
|
+
| Package | Role |
|
|
20
|
+
|---------|------|
|
|
21
|
+
| `django-sec-audit` (this) | Django integration |
|
|
22
|
+
| `sec-audit` | Core (events, context, IP resolution, scrubbing) |
|
|
23
|
+
| `sec-audit-logging` | Logging runtime, formatters, SIEM handlers |
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
# settings.py
|
|
29
|
+
|
|
30
|
+
INSTALLED_APPS = [
|
|
31
|
+
'sec_audit.django.apps.SecAuditConfig', # early, before your apps
|
|
32
|
+
# ...
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
MIDDLEWARE = [
|
|
36
|
+
# ... Django security middleware
|
|
37
|
+
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
38
|
+
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
39
|
+
'sec_audit.django.middleware.AuditMiddleware', # last or near-last
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
SEC_AUDIT = {
|
|
43
|
+
'core': {
|
|
44
|
+
'source': 'myapp', # appears as resource.service.name
|
|
45
|
+
'log_request_bodies': False, # opt-in
|
|
46
|
+
'log_ok_responses': False, # only 4xx/5xx by default
|
|
47
|
+
},
|
|
48
|
+
'logging': {
|
|
49
|
+
'schema_version': '1.0',
|
|
50
|
+
},
|
|
51
|
+
'django': {
|
|
52
|
+
'include_usernames': False, # opt-in for GDPR considerations
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
LOGGING = {
|
|
57
|
+
'version': 1,
|
|
58
|
+
'disable_existing_loggers': False,
|
|
59
|
+
'formatters': {
|
|
60
|
+
# Injects the resolved SEC_AUDIT config at construction.
|
|
61
|
+
'audit_jsonl': {
|
|
62
|
+
'()': 'sec_audit.django.logging.formatters.audit_jsonl_formatter',
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
'handlers': {
|
|
66
|
+
'audit_stdout': {
|
|
67
|
+
'class': 'logging.StreamHandler',
|
|
68
|
+
'formatter': 'audit_jsonl',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
'loggers': {
|
|
72
|
+
'sec_audit.audit': {
|
|
73
|
+
'handlers': ['audit_stdout'],
|
|
74
|
+
'level': 'INFO',
|
|
75
|
+
'propagate': False,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Stdout is the supported delivery path (stdout JSONL -> Grafana Alloy -> Loki).
|
|
82
|
+
For local file output, swap the handler for a stdlib
|
|
83
|
+
`logging.handlers.RotatingFileHandler` using the same `audit_jsonl` formatter.
|
|
84
|
+
|
|
85
|
+
## Usage Guide
|
|
86
|
+
|
|
87
|
+
For a step-by-step setup and configuration walkthrough, see
|
|
88
|
+
[docs/how-to-use.md](https://github.com/ammar39/sec-audit/blob/main/packages/django-sec-audit/docs/how-to-use.md).
|
|
89
|
+
|
|
90
|
+
## Monitoring with Loki/Grafana
|
|
91
|
+
|
|
92
|
+
Audit events are OTel JSONL, ready to ship to Loki (`sec_audit.audit` JSONL → Grafana
|
|
93
|
+
Alloy → Loki → Grafana). The bundled `sec-audit-loki-init` command generates the whole
|
|
94
|
+
local stack — see
|
|
95
|
+
[docs/loki-setup.md](https://github.com/ammar39/sec-audit/blob/main/packages/django-sec-audit/docs/loki-setup.md).
|
|
96
|
+
|
|
97
|
+
## Configuration
|
|
98
|
+
|
|
99
|
+
All settings live under the `SEC_AUDIT` dict in `settings.py`. Three sections are recognised:
|
|
100
|
+
|
|
101
|
+
### `core` (CoreAuditConfig)
|
|
102
|
+
|
|
103
|
+
| Setting | Default | Description |
|
|
104
|
+
|---------|---------|-------------|
|
|
105
|
+
| `source` | `'sec-audit'` | Service name in `resource.service.name` |
|
|
106
|
+
| `ignore_paths` | `()` | Regex patterns; matching paths are skipped |
|
|
107
|
+
| `ignore_status_codes` | `frozenset()` | Status code values to skip (e.g. `{301, 302}`) |
|
|
108
|
+
| `sample_rate` | `1.0` | Sampling rate for successful (2xx) responses |
|
|
109
|
+
| `log_ok_responses` | `False` | Enable logging for 2xx responses |
|
|
110
|
+
| `log_request_bodies` | `False` | Enable request body capture |
|
|
111
|
+
| `log_body_paths` | `()` | Regex patterns; only matching paths capture bodies |
|
|
112
|
+
| `body_methods` | `{'POST', 'PUT', 'PATCH'}` | HTTP methods eligible for body capture |
|
|
113
|
+
| `max_body_bytes` | `4096` | Maximum JSON body size in bytes |
|
|
114
|
+
| `sensitive_keys` | `DEFAULT_SENSITIVE_KEYS` | Built-in key patterns to scrub (incl. `password`, `secret`, `token`, `apikey`, etc.) |
|
|
115
|
+
| `sensitive_key_allowlist` | `()` | Exact (compacted) field names never redacted, even when a `sensitive_keys` substring matches — e.g. `credit_card_last4`, `token_expiry` |
|
|
116
|
+
| `sensitive_value_patterns` | `()` | Regex patterns matching sensitive values to scrub |
|
|
117
|
+
|
|
118
|
+
### `logging` (LoggingAuditConfig)
|
|
119
|
+
|
|
120
|
+
| Setting | Default | Description |
|
|
121
|
+
|---------|---------|-------------|
|
|
122
|
+
| `schema_version` | `'1.0'` | Schema version string in every event |
|
|
123
|
+
| `projection_limits` | `ProjectionLimits()` | Bounds for dict/list nesting and string sizes (accepts a dict or `ProjectionLimits`) |
|
|
124
|
+
|
|
125
|
+
File-rotation parameters (`maxBytes`/`backupCount`/`filename`) are configured on
|
|
126
|
+
the handler in Django's `LOGGING` dict, not here.
|
|
127
|
+
|
|
128
|
+
### `django` (DjangoAuditConfig)
|
|
129
|
+
|
|
130
|
+
| Setting | Default | Description |
|
|
131
|
+
|---------|---------|-------------|
|
|
132
|
+
| `include_usernames` | `False` | Include `user.name` in events (opt-in for GDPR) |
|
|
133
|
+
| `trusted_proxy_cidrs` | `()` | CIDR ranges considered trusted proxies |
|
|
134
|
+
| `trusted_proxy_count` | `None` | Number of trusted proxies (leftmost N IPs are strip) |
|
|
135
|
+
| `emit_session_id` | `False` | Emit correlated `session.id` in events (opt-in; writes to `request.session`) |
|
|
136
|
+
| `filters` | `()` | Dotted paths to filter callable/classes |
|
|
137
|
+
| `enrichers` | `()` | Dotted paths to enricher callable/classes |
|
|
138
|
+
|
|
139
|
+
### Event Types
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
auth.login.success auth.login.failed
|
|
143
|
+
auth.logout.success auth.logout.unknown
|
|
144
|
+
http.response.success http.response.client_error http.response.server_error
|
|
145
|
+
model.create model.update model.delete model.access
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Output Format
|
|
149
|
+
|
|
150
|
+
Each event is a single JSON line:
|
|
151
|
+
|
|
152
|
+
```jsonc
|
|
153
|
+
{
|
|
154
|
+
"timestamp": 1712345678000000000,
|
|
155
|
+
"observed_timestamp": 1712345678000000000,
|
|
156
|
+
"severity_text": "WARNING",
|
|
157
|
+
"severity_number": 13,
|
|
158
|
+
"body": "http.response",
|
|
159
|
+
"resource": { "service.name": "sec-audit" },
|
|
160
|
+
"instrumentation_scope": { "name": "sec_audit.django.middleware", "version": "1.0.0" },
|
|
161
|
+
"attributes": {
|
|
162
|
+
"event_type": "http.response.client_error",
|
|
163
|
+
"source.address": "203.0.113.10",
|
|
164
|
+
"http.request.method": "POST",
|
|
165
|
+
"http.response.status_code": 404,
|
|
166
|
+
"url.full": "https://example.test/api/transfer",
|
|
167
|
+
"url.path": "/api/transfer",
|
|
168
|
+
"user.id": "42"
|
|
169
|
+
},
|
|
170
|
+
"event_name": "http.response.client_error"
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Optional Extras
|
|
175
|
+
|
|
176
|
+
| Extra | Dependencies |
|
|
177
|
+
|-------|-------------|
|
|
178
|
+
| `pip install django-sec-audit[model]` | django-auditlog |
|
|
179
|
+
| `pip install django-sec-audit[drf]` | djangorestframework |
|
|
180
|
+
| `pip install django-sec-audit[full]` | Both |
|
|
181
|
+
|
|
182
|
+
## Development
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
pip install -e "packages/django-sec-audit[dev]"
|
|
186
|
+
pytest
|
|
187
|
+
ruff check .
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
MIT
|