summaryrefslogtreecommitdiff
path: root/python-datapunt-django-audit-log.spec
blob: 8d7d850b489422c68505f696ff0ac46588ecc1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
%global _empty_manifest_terminate_build 0
Name:		python-datapunt-django-audit-log
Version:	0.4.0
Release:	1
Summary:	A simple Django app to enable audit logging
License:	Mozilla Public License 2.0
URL:		https://github.com/Amsterdam/auditlog
Source0:	https://mirrors.aliyun.com/pypi/web/packages/9e/41/add4b831f36182acb0b0909766e44474e27d25d72a776b7f39b5c158d035/datapunt-django-audit-log-0.4.0.tar.gz
BuildArch:	noarch

Requires:	python3-django
Requires:	python3-datapunt-audit-log
Requires:	python3-pytest
Requires:	python3-pytest-cov
Requires:	python3-twine
Requires:	python3-bump2version

%description

For the Python only implementation (which is used by this library) see https://github.com/Amsterdam/python-audit-log


# DataPunt Django Audit Log

DataPunt Audit Log is a simple Django app that will log all incoming requests
and their corresponding responses to a configurable endpoint. 

During the process request phase, the logger is attached to the request. Before 
returning a response the app can easily provide extra context. In the process
response phase the audit_log middleware will send the log. 


## Links
- [Quick Start](#quick-start)
- [Default Context Info](#default-context-info)
- [Custom Optional Context Info](#custom-optional-context-info)
- [Django Rest Framework](#django-rest-framework)


## Quick start

1. Install using pip

    ```bash
    pip install datapunt_django_audit_log
    ```

2. Add "django_audit_log" to your INSTALLED_APPS:

    ```python
    INSTALLED_APPS = [
        ...
        'django_audit_log',
    ]
    ```

3. Add the AuditLogMiddleware to your MIDDLEWARE:

    ```python
    MIDDLEWARE = [
        ...
       'django_audit_log.middleware.AuditLogMiddleware',
    ]
    ```

4. When using the Django Rest Framework, let your viewsets extend `AuditLogReadOnlyViewset`
or `AuditLogViewSet`. This will automatically add context to the audit log regarding
filters, results and executed actions (see - [Django Rest Framework](#django-rest-framework)).

    ```python
    class MyViewSet(AuditLogViewSet):
        queryset = SomeModel.objects.all()
    ```

5. Set the AUDIT_LOG_EXEMPT_URLS setting to make sure certain urls will not be logged 
(e.g. health check urls). 

    ```python
    # If a URL path matches a regular expression in this list, the request will not be redirected to HTTPS.
    # The AuditLogMiddleware strips leading slashes from URL paths, so patterns shouldn’t include them, e.g.
    # [r'foo/bar$']
    AUDIT_LOG_EXEMPT_URLS = []
    ```


At this point all requests/responses will be logged. For providing extra context
(which you are strongly urged to do so), see next chapters.

## Default context info

By default the audit log sends the following json structure per request:

```json
{
  "http_request": {
    "method": "get|post|head|options|etc..",
    "url": "https://datapunt.amsterdam.nl?including=querystring",
    "user_agent": "full browser user agent"
  },
  "http_response": {
    "status_code": "http status code",
    "reason": "http status reason",
    "headers": {
      "key": "value"
    }
  },
  "user": {
    "authenticated": "True/False",
    "provider": "auth backend the user authenticated with",
    "realm": "optional realm when using keycloak or another provider",
    "email": "email of logged in user",
    "roles": "roles attached to the logged in user",
    "ip": "ip address"
  }
}
```

Each json entry is set by its corresponding method. In this case, 
the middleware sets them automatically by calling
`set_http_request()` and `set_user_fom_request()` 
in the process_request method. In the process_response method the
last data is set by invoking `set_http_response()`.

After the response has been processed the middleware automatically
creates the log item by calling `send_log()`. 

## Custom optional context info

Per request it is possible to add optional context info. For a complete
audit log, you are strongly urged to add more info inside your view.

Adding extra context is quite simple. The audit_log object has been added
to the request by the middleware. Therefore every view can simply access 
it via the request object.

### Filter 
`request.audit_log.set_filter(self, object_name, fields, terms)` allows to provide
info on the requested type of object and the filters that have been used 
(a user searches for 'terms', which are matched on specific 'fields' of the 
'object').

This method will add the following details to the log:

```json
"filter": {
      "object": "Object name that is requested",
      "fields": "Fields that are being filtered on, if applicable",
      "terms": "Search terms, if applicable"
  }
```

### Results
`request.audit_log.set_results(self, results)` allows to pass a json dict
detailing exactly what results have been returned to the user. 

It is up to the developer to decide whether the amount of 
data that would be added here will become a burden instead
of a blessing.

This method will add the following details to the log:

```json
"results": {
    ...
  }
```

### Message and loglevel
At last, a log message and loglevel can be provided to indicate 
what the request is actually doing. This is done by calling 
one of the following methods:

```python
request.audit_log.debug(self, msg)
request.audit_log.info(self, msg)
request.audit_log.warning(self, msg)
request.audit_log.error(self, msg)
request.audit_log.critical(self, msg)
```

These methods will add the following details to the log:

```json
"type": "DEBUG|INFO|WARNING|ERROR|etc",
"message": "log message"
```

## Django Rest Framework
Two base-ViewSets are available if you use the Django Rest Framework.

The `AuditLogReadOnlyViewSet` extends the `ReadOnlyModelViewSet` and overrides
the `retrieve()` and `list()` methods. The `AuditLogViewSet` extends the `AuditLogReadOnlyViewSet`
and overrides the remaining (non-read-only) methods `create()`, `update()` and `destroy()`.

Our classes inspect the request and will automatically add extra context information
to the audit log. This context information provides info regarding filters, results
and the action that is being performed.

Note that by default `list()` will not add the results to the log, unless the `audit_log_list_response`
attribute is set. Only do so when the amount of data inside the list response is suitable
to store inside a log entry.

```python
class MyViewSet(AuditLogViewSet):
    audit_log_list_response = True
```




%package -n python3-datapunt-django-audit-log
Summary:	A simple Django app to enable audit logging
Provides:	python-datapunt-django-audit-log
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-datapunt-django-audit-log

For the Python only implementation (which is used by this library) see https://github.com/Amsterdam/python-audit-log


# DataPunt Django Audit Log

DataPunt Audit Log is a simple Django app that will log all incoming requests
and their corresponding responses to a configurable endpoint. 

During the process request phase, the logger is attached to the request. Before 
returning a response the app can easily provide extra context. In the process
response phase the audit_log middleware will send the log. 


## Links
- [Quick Start](#quick-start)
- [Default Context Info](#default-context-info)
- [Custom Optional Context Info](#custom-optional-context-info)
- [Django Rest Framework](#django-rest-framework)


## Quick start

1. Install using pip

    ```bash
    pip install datapunt_django_audit_log
    ```

2. Add "django_audit_log" to your INSTALLED_APPS:

    ```python
    INSTALLED_APPS = [
        ...
        'django_audit_log',
    ]
    ```

3. Add the AuditLogMiddleware to your MIDDLEWARE:

    ```python
    MIDDLEWARE = [
        ...
       'django_audit_log.middleware.AuditLogMiddleware',
    ]
    ```

4. When using the Django Rest Framework, let your viewsets extend `AuditLogReadOnlyViewset`
or `AuditLogViewSet`. This will automatically add context to the audit log regarding
filters, results and executed actions (see - [Django Rest Framework](#django-rest-framework)).

    ```python
    class MyViewSet(AuditLogViewSet):
        queryset = SomeModel.objects.all()
    ```

5. Set the AUDIT_LOG_EXEMPT_URLS setting to make sure certain urls will not be logged 
(e.g. health check urls). 

    ```python
    # If a URL path matches a regular expression in this list, the request will not be redirected to HTTPS.
    # The AuditLogMiddleware strips leading slashes from URL paths, so patterns shouldn’t include them, e.g.
    # [r'foo/bar$']
    AUDIT_LOG_EXEMPT_URLS = []
    ```


At this point all requests/responses will be logged. For providing extra context
(which you are strongly urged to do so), see next chapters.

## Default context info

By default the audit log sends the following json structure per request:

```json
{
  "http_request": {
    "method": "get|post|head|options|etc..",
    "url": "https://datapunt.amsterdam.nl?including=querystring",
    "user_agent": "full browser user agent"
  },
  "http_response": {
    "status_code": "http status code",
    "reason": "http status reason",
    "headers": {
      "key": "value"
    }
  },
  "user": {
    "authenticated": "True/False",
    "provider": "auth backend the user authenticated with",
    "realm": "optional realm when using keycloak or another provider",
    "email": "email of logged in user",
    "roles": "roles attached to the logged in user",
    "ip": "ip address"
  }
}
```

Each json entry is set by its corresponding method. In this case, 
the middleware sets them automatically by calling
`set_http_request()` and `set_user_fom_request()` 
in the process_request method. In the process_response method the
last data is set by invoking `set_http_response()`.

After the response has been processed the middleware automatically
creates the log item by calling `send_log()`. 

## Custom optional context info

Per request it is possible to add optional context info. For a complete
audit log, you are strongly urged to add more info inside your view.

Adding extra context is quite simple. The audit_log object has been added
to the request by the middleware. Therefore every view can simply access 
it via the request object.

### Filter 
`request.audit_log.set_filter(self, object_name, fields, terms)` allows to provide
info on the requested type of object and the filters that have been used 
(a user searches for 'terms', which are matched on specific 'fields' of the 
'object').

This method will add the following details to the log:

```json
"filter": {
      "object": "Object name that is requested",
      "fields": "Fields that are being filtered on, if applicable",
      "terms": "Search terms, if applicable"
  }
```

### Results
`request.audit_log.set_results(self, results)` allows to pass a json dict
detailing exactly what results have been returned to the user. 

It is up to the developer to decide whether the amount of 
data that would be added here will become a burden instead
of a blessing.

This method will add the following details to the log:

```json
"results": {
    ...
  }
```

### Message and loglevel
At last, a log message and loglevel can be provided to indicate 
what the request is actually doing. This is done by calling 
one of the following methods:

```python
request.audit_log.debug(self, msg)
request.audit_log.info(self, msg)
request.audit_log.warning(self, msg)
request.audit_log.error(self, msg)
request.audit_log.critical(self, msg)
```

These methods will add the following details to the log:

```json
"type": "DEBUG|INFO|WARNING|ERROR|etc",
"message": "log message"
```

## Django Rest Framework
Two base-ViewSets are available if you use the Django Rest Framework.

The `AuditLogReadOnlyViewSet` extends the `ReadOnlyModelViewSet` and overrides
the `retrieve()` and `list()` methods. The `AuditLogViewSet` extends the `AuditLogReadOnlyViewSet`
and overrides the remaining (non-read-only) methods `create()`, `update()` and `destroy()`.

Our classes inspect the request and will automatically add extra context information
to the audit log. This context information provides info regarding filters, results
and the action that is being performed.

Note that by default `list()` will not add the results to the log, unless the `audit_log_list_response`
attribute is set. Only do so when the amount of data inside the list response is suitable
to store inside a log entry.

```python
class MyViewSet(AuditLogViewSet):
    audit_log_list_response = True
```




%package help
Summary:	Development documents and examples for datapunt-django-audit-log
Provides:	python3-datapunt-django-audit-log-doc
%description help

For the Python only implementation (which is used by this library) see https://github.com/Amsterdam/python-audit-log


# DataPunt Django Audit Log

DataPunt Audit Log is a simple Django app that will log all incoming requests
and their corresponding responses to a configurable endpoint. 

During the process request phase, the logger is attached to the request. Before 
returning a response the app can easily provide extra context. In the process
response phase the audit_log middleware will send the log. 


## Links
- [Quick Start](#quick-start)
- [Default Context Info](#default-context-info)
- [Custom Optional Context Info](#custom-optional-context-info)
- [Django Rest Framework](#django-rest-framework)


## Quick start

1. Install using pip

    ```bash
    pip install datapunt_django_audit_log
    ```

2. Add "django_audit_log" to your INSTALLED_APPS:

    ```python
    INSTALLED_APPS = [
        ...
        'django_audit_log',
    ]
    ```

3. Add the AuditLogMiddleware to your MIDDLEWARE:

    ```python
    MIDDLEWARE = [
        ...
       'django_audit_log.middleware.AuditLogMiddleware',
    ]
    ```

4. When using the Django Rest Framework, let your viewsets extend `AuditLogReadOnlyViewset`
or `AuditLogViewSet`. This will automatically add context to the audit log regarding
filters, results and executed actions (see - [Django Rest Framework](#django-rest-framework)).

    ```python
    class MyViewSet(AuditLogViewSet):
        queryset = SomeModel.objects.all()
    ```

5. Set the AUDIT_LOG_EXEMPT_URLS setting to make sure certain urls will not be logged 
(e.g. health check urls). 

    ```python
    # If a URL path matches a regular expression in this list, the request will not be redirected to HTTPS.
    # The AuditLogMiddleware strips leading slashes from URL paths, so patterns shouldn’t include them, e.g.
    # [r'foo/bar$']
    AUDIT_LOG_EXEMPT_URLS = []
    ```


At this point all requests/responses will be logged. For providing extra context
(which you are strongly urged to do so), see next chapters.

## Default context info

By default the audit log sends the following json structure per request:

```json
{
  "http_request": {
    "method": "get|post|head|options|etc..",
    "url": "https://datapunt.amsterdam.nl?including=querystring",
    "user_agent": "full browser user agent"
  },
  "http_response": {
    "status_code": "http status code",
    "reason": "http status reason",
    "headers": {
      "key": "value"
    }
  },
  "user": {
    "authenticated": "True/False",
    "provider": "auth backend the user authenticated with",
    "realm": "optional realm when using keycloak or another provider",
    "email": "email of logged in user",
    "roles": "roles attached to the logged in user",
    "ip": "ip address"
  }
}
```

Each json entry is set by its corresponding method. In this case, 
the middleware sets them automatically by calling
`set_http_request()` and `set_user_fom_request()` 
in the process_request method. In the process_response method the
last data is set by invoking `set_http_response()`.

After the response has been processed the middleware automatically
creates the log item by calling `send_log()`. 

## Custom optional context info

Per request it is possible to add optional context info. For a complete
audit log, you are strongly urged to add more info inside your view.

Adding extra context is quite simple. The audit_log object has been added
to the request by the middleware. Therefore every view can simply access 
it via the request object.

### Filter 
`request.audit_log.set_filter(self, object_name, fields, terms)` allows to provide
info on the requested type of object and the filters that have been used 
(a user searches for 'terms', which are matched on specific 'fields' of the 
'object').

This method will add the following details to the log:

```json
"filter": {
      "object": "Object name that is requested",
      "fields": "Fields that are being filtered on, if applicable",
      "terms": "Search terms, if applicable"
  }
```

### Results
`request.audit_log.set_results(self, results)` allows to pass a json dict
detailing exactly what results have been returned to the user. 

It is up to the developer to decide whether the amount of 
data that would be added here will become a burden instead
of a blessing.

This method will add the following details to the log:

```json
"results": {
    ...
  }
```

### Message and loglevel
At last, a log message and loglevel can be provided to indicate 
what the request is actually doing. This is done by calling 
one of the following methods:

```python
request.audit_log.debug(self, msg)
request.audit_log.info(self, msg)
request.audit_log.warning(self, msg)
request.audit_log.error(self, msg)
request.audit_log.critical(self, msg)
```

These methods will add the following details to the log:

```json
"type": "DEBUG|INFO|WARNING|ERROR|etc",
"message": "log message"
```

## Django Rest Framework
Two base-ViewSets are available if you use the Django Rest Framework.

The `AuditLogReadOnlyViewSet` extends the `ReadOnlyModelViewSet` and overrides
the `retrieve()` and `list()` methods. The `AuditLogViewSet` extends the `AuditLogReadOnlyViewSet`
and overrides the remaining (non-read-only) methods `create()`, `update()` and `destroy()`.

Our classes inspect the request and will automatically add extra context information
to the audit log. This context information provides info regarding filters, results
and the action that is being performed.

Note that by default `list()` will not add the results to the log, unless the `audit_log_list_response`
attribute is set. Only do so when the amount of data inside the list response is suitable
to store inside a log entry.

```python
class MyViewSet(AuditLogViewSet):
    audit_log_list_response = True
```




%prep
%autosetup -n datapunt-django-audit-log-0.4.0

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-datapunt-django-audit-log -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Tue Jun 20 2023 Python_Bot <Python_Bot@openeuler.org> - 0.4.0-1
- Package Spec generated