mirror of
https://github.com/netbox-community/netbox.git
synced 2026-03-06 22:40:05 +01:00
Compare commits
17 Commits
21440-oob-
...
feature
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a5f40338d | ||
|
|
fd6e0e9784 | ||
|
|
2a176df28a | ||
|
|
cd5d88ff8a | ||
|
|
6e3fd9d4b2 | ||
|
|
53ae164c75 | ||
|
|
c40640af81 | ||
|
|
3c6596de8f | ||
|
|
b3de0b9bee | ||
|
|
ec0fe62df5 | ||
|
|
d3a0566ee3 | ||
|
|
694e3765dd | ||
|
|
303199dc8f | ||
|
|
6eafffb497 | ||
|
|
53ea48efa9 | ||
|
|
1a404f5c0f | ||
|
|
3320e07b70 |
@@ -4,7 +4,7 @@ colorama
|
||||
|
||||
# The Python web framework on which NetBox is built
|
||||
# https://docs.djangoproject.com/en/stable/releases/
|
||||
Django==5.2.*
|
||||
Django==6.0.*
|
||||
|
||||
# Django middleware which permits cross-domain API requests
|
||||
# https://github.com/adamchainz/django-cors-headers/blob/main/CHANGELOG.rst
|
||||
@@ -35,7 +35,9 @@ django-pglocks
|
||||
|
||||
# Prometheus metrics library for Django
|
||||
# https://github.com/korfuri/django-prometheus/blob/master/CHANGELOG.md
|
||||
django-prometheus
|
||||
# TODO: 2.4.1 is incompatible with Django>=6.0, but a fixed release is expected
|
||||
# https://github.com/django-commons/django-prometheus/issues/494
|
||||
django-prometheus>=2.4.0,<2.5.0,!=2.4.1
|
||||
|
||||
# Django caching backend using Redis
|
||||
# https://github.com/jazzband/django-redis/blob/master/CHANGELOG.rst
|
||||
|
||||
@@ -21,6 +21,7 @@ Some configuration parameters are primarily controlled via NetBox's admin interf
|
||||
* [`BANNER_BOTTOM`](./miscellaneous.md#banner_bottom)
|
||||
* [`BANNER_LOGIN`](./miscellaneous.md#banner_login)
|
||||
* [`BANNER_TOP`](./miscellaneous.md#banner_top)
|
||||
* [`CHANGELOG_RETAIN_CREATE_LAST_UPDATE`](./miscellaneous.md#changelog_retain_create_last_update)
|
||||
* [`CHANGELOG_RETENTION`](./miscellaneous.md#changelog_retention)
|
||||
* [`CUSTOM_VALIDATORS`](./data-validation.md#custom_validators)
|
||||
* [`DEFAULT_USER_PREFERENCES`](./default-values.md#default_user_preferences)
|
||||
|
||||
@@ -73,6 +73,27 @@ This data enables the project maintainers to estimate how many NetBox deployment
|
||||
|
||||
---
|
||||
|
||||
## CHANGELOG_RETAIN_CREATE_LAST_UPDATE
|
||||
|
||||
!!! tip "Dynamic Configuration Parameter"
|
||||
|
||||
Default: `True`
|
||||
|
||||
When pruning expired changelog entries (per `CHANGELOG_RETENTION`), retain each non-deleted object's original `create`
|
||||
change record and its most recent `update` change record. If an object has a `delete` change record, its changelog
|
||||
entries are pruned normally according to `CHANGELOG_RETENTION`.
|
||||
|
||||
!!! note
|
||||
For objects without a `delete` change record, the original `create` record and most recent `update` record are
|
||||
exempt from pruning. All other changelog records (including intermediate `update` records and all `delete` records)
|
||||
remain subject to pruning per `CHANGELOG_RETENTION`.
|
||||
|
||||
!!! warning
|
||||
This setting is enabled by default. Upgrading deployments that rely on complete pruning of expired changelog entries
|
||||
should explicitly set `CHANGELOG_RETAIN_CREATE_LAST_UPDATE = False` to preserve the previous behavior.
|
||||
|
||||
---
|
||||
|
||||
## CHANGELOG_RETENTION
|
||||
|
||||
!!! tip "Dynamic Configuration Parameter"
|
||||
|
||||
@@ -31,6 +31,11 @@ The following data is available as context for Jinja2 templates:
|
||||
* `data` - A detailed representation of the object in its current state. This is typically equivalent to the model's representation in NetBox's REST API.
|
||||
* `snapshots` - Minimal "snapshots" of the object state both before and after the change was made; provided as a dictionary with keys named `prechange` and `postchange`. These are not as extensive as the fully serialized representation, but contain enough information to convey what has changed.
|
||||
|
||||
!!! warning "Deprecation of legacy fields"
|
||||
The "request_id" and "username" fields in the webhook payload above are deprecated and should no longer be used. Support for them will be removed in NetBox v4.7.0.
|
||||
|
||||
Use `request.user.username` and `request.request_id` from the `request` object included in the callback context instead.
|
||||
|
||||
### Default Request Body
|
||||
|
||||
If no body template is specified, the request body will be populated with a JSON object containing the context data. For example, a newly created site might appear as follows:
|
||||
|
||||
@@ -88,3 +88,8 @@ The following context variables are available in to the text and link templates.
|
||||
| `request_id` | The unique request ID |
|
||||
| `data` | A complete serialized representation of the object |
|
||||
| `snapshots` | Pre- and post-change snapshots of the object |
|
||||
|
||||
!!! warning "Deprecation of legacy fields"
|
||||
The "request_id" and "username" fields in the webhook payload above are deprecated and should no longer be used. Support for them will be removed in NetBox v4.7.0.
|
||||
|
||||
Use `request.user.username` and `request.request_id` from the `request` object included in the callback context instead.
|
||||
|
||||
@@ -43,6 +43,11 @@ The resulting webhook payload will look like the following:
|
||||
}
|
||||
```
|
||||
|
||||
!!! warning "Deprecation of legacy fields"
|
||||
The "request_id" and "username" fields in the webhook payload above are deprecated and should no longer be used. Support for them will be removed in NetBox v4.7.0.
|
||||
|
||||
Use `request.user.username` and `request.request_id` from the `request` object included in the callback context instead.
|
||||
|
||||
!!! note "Consider namespacing webhook data"
|
||||
The data returned from all webhook callbacks will be compiled into a single `context` dictionary. Any existing keys within this dictionary will be overwritten by subsequent callbacks which include those keys. To avoid collisions with webhook data provided by other plugins, consider namespacing your plugin's data within a nested dictionary as such:
|
||||
|
||||
|
||||
@@ -165,9 +165,10 @@ class ConfigRevisionForm(forms.ModelForm, metaclass=ConfigFormMetaclass):
|
||||
FieldSet('PAGINATE_COUNT', 'MAX_PAGE_SIZE', name=_('Pagination')),
|
||||
FieldSet('CUSTOM_VALIDATORS', 'PROTECTION_RULES', name=_('Validation')),
|
||||
FieldSet('DEFAULT_USER_PREFERENCES', name=_('User Preferences')),
|
||||
FieldSet('CHANGELOG_RETENTION', 'CHANGELOG_RETAIN_CREATE_LAST_UPDATE', name=_('Change Log')),
|
||||
FieldSet(
|
||||
'MAINTENANCE_MODE', 'COPILOT_ENABLED', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'JOB_RETENTION',
|
||||
'MAPS_URL', name=_('Miscellaneous'),
|
||||
'MAINTENANCE_MODE', 'COPILOT_ENABLED', 'GRAPHQL_ENABLED', 'JOB_RETENTION', 'MAPS_URL',
|
||||
name=_('Miscellaneous'),
|
||||
),
|
||||
FieldSet('comment', name=_('Config Revision'))
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ from importlib import import_module
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.db.models import Exists, OuterRef, Subquery
|
||||
from django.utils import timezone
|
||||
from packaging import version
|
||||
|
||||
@@ -14,7 +15,7 @@ from netbox.jobs import JobRunner, system_job
|
||||
from netbox.search.backends import search_backend
|
||||
from utilities.proxy import resolve_proxies
|
||||
|
||||
from .choices import DataSourceStatusChoices, JobIntervalChoices
|
||||
from .choices import DataSourceStatusChoices, JobIntervalChoices, ObjectChangeActionChoices
|
||||
from .models import DataSource
|
||||
|
||||
|
||||
@@ -126,19 +127,51 @@ class SystemHousekeepingJob(JobRunner):
|
||||
"""
|
||||
Delete any ObjectChange records older than the configured changelog retention time (if any).
|
||||
"""
|
||||
self.logger.info("Pruning old changelog entries...")
|
||||
self.logger.info('Pruning old changelog entries...')
|
||||
config = Config()
|
||||
if not config.CHANGELOG_RETENTION:
|
||||
self.logger.info("No retention period specified; skipping.")
|
||||
self.logger.info('No retention period specified; skipping.')
|
||||
return
|
||||
|
||||
cutoff = timezone.now() - timedelta(days=config.CHANGELOG_RETENTION)
|
||||
self.logger.debug(
|
||||
f"Changelog retention period: {config.CHANGELOG_RETENTION} days ({cutoff:%Y-%m-%d %H:%M:%S})"
|
||||
)
|
||||
self.logger.debug(f'Changelog retention period: {config.CHANGELOG_RETENTION} days ({cutoff:%Y-%m-%d %H:%M:%S})')
|
||||
|
||||
count = ObjectChange.objects.filter(time__lt=cutoff).delete()[0]
|
||||
self.logger.info(f"Deleted {count} expired changelog records")
|
||||
expired_qs = ObjectChange.objects.filter(time__lt=cutoff)
|
||||
|
||||
# When enabled, retain each object's original create record and most recent update record while pruning expired
|
||||
# changelog entries. This applies only to objects without a delete record.
|
||||
if config.CHANGELOG_RETAIN_CREATE_LAST_UPDATE:
|
||||
self.logger.debug('Retaining changelog create records and last update records (excluding deleted objects)')
|
||||
|
||||
deleted_exists = ObjectChange.objects.filter(
|
||||
action=ObjectChangeActionChoices.ACTION_DELETE,
|
||||
changed_object_type_id=OuterRef('changed_object_type_id'),
|
||||
changed_object_id=OuterRef('changed_object_id'),
|
||||
)
|
||||
|
||||
# Keep create records only where no delete exists for that object
|
||||
create_pks_to_keep = (
|
||||
ObjectChange.objects.filter(action=ObjectChangeActionChoices.ACTION_CREATE)
|
||||
.annotate(has_delete=Exists(deleted_exists))
|
||||
.filter(has_delete=False)
|
||||
.values('pk')
|
||||
)
|
||||
|
||||
# Keep the most recent update per object only where no delete exists for the object
|
||||
latest_update_pks_to_keep = (
|
||||
ObjectChange.objects.filter(action=ObjectChangeActionChoices.ACTION_UPDATE)
|
||||
.annotate(has_delete=Exists(deleted_exists))
|
||||
.filter(has_delete=False)
|
||||
.order_by('changed_object_type_id', 'changed_object_id', '-time', '-pk')
|
||||
.distinct('changed_object_type_id', 'changed_object_id')
|
||||
.values('pk')
|
||||
)
|
||||
|
||||
expired_qs = expired_qs.exclude(pk__in=Subquery(create_pks_to_keep))
|
||||
expired_qs = expired_qs.exclude(pk__in=Subquery(latest_update_pks_to_keep))
|
||||
|
||||
count = expired_qs.delete()[0]
|
||||
self.logger.info(f'Deleted {count} expired changelog records')
|
||||
|
||||
def delete_expired_jobs(self):
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import django_tables2 as tables
|
||||
from django.utils.html import conditional_escape
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from core.constants import JOB_LOG_ENTRY_LEVELS
|
||||
@@ -82,3 +84,9 @@ class JobLogEntryTable(BaseTable):
|
||||
class Meta(BaseTable.Meta):
|
||||
empty_text = _('No log entries')
|
||||
fields = ('timestamp', 'level', 'message')
|
||||
|
||||
def render_message(self, record, value):
|
||||
if record.get('level') == 'error' and '\n' in value:
|
||||
value = conditional_escape(value)
|
||||
return mark_safe(f'<pre class="p-0">{value}</pre>')
|
||||
return value
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
import logging
|
||||
import uuid
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from rest_framework import status
|
||||
|
||||
from core.choices import ObjectChangeActionChoices
|
||||
from core.jobs import SystemHousekeepingJob
|
||||
from core.models import ObjectChange, ObjectType
|
||||
from dcim.choices import InterfaceTypeChoices, ModuleStatusChoices, SiteStatusChoices
|
||||
from dcim.models import (
|
||||
@@ -694,3 +701,99 @@ class ChangeLogAPITest(APITestCase):
|
||||
self.assertEqual(changes[3].changed_object_type, ContentType.objects.get_for_model(Module))
|
||||
self.assertEqual(changes[3].changed_object_id, module.pk)
|
||||
self.assertEqual(changes[3].action, ObjectChangeActionChoices.ACTION_DELETE)
|
||||
|
||||
|
||||
class ChangelogPruneRetentionTest(TestCase):
|
||||
"""Test suite for Changelog pruning retention settings."""
|
||||
|
||||
@staticmethod
|
||||
def _make_oc(*, ct, obj_id, action, ts):
|
||||
oc = ObjectChange.objects.create(
|
||||
changed_object_type=ct,
|
||||
changed_object_id=obj_id,
|
||||
action=action,
|
||||
user_name='test',
|
||||
request_id=uuid.uuid4(),
|
||||
object_repr=f'Object {obj_id}',
|
||||
)
|
||||
ObjectChange.objects.filter(pk=oc.pk).update(time=ts)
|
||||
return oc.pk
|
||||
|
||||
@staticmethod
|
||||
def _run_prune(*, retention_days, retain_create_last_update):
|
||||
job = SystemHousekeepingJob.__new__(SystemHousekeepingJob)
|
||||
job.logger = logging.getLogger('netbox.tests.changelog_prune')
|
||||
|
||||
with patch('core.jobs.Config') as MockConfig:
|
||||
cfg = MockConfig.return_value
|
||||
cfg.CHANGELOG_RETENTION = retention_days
|
||||
cfg.CHANGELOG_RETAIN_CREATE_LAST_UPDATE = retain_create_last_update
|
||||
job.prune_changelog()
|
||||
|
||||
def test_prune_retain_create_last_update_excludes_deleted_objects(self):
|
||||
ct = ContentType.objects.get_for_model(Site)
|
||||
|
||||
retention_days = 90
|
||||
now = timezone.now()
|
||||
cutoff = now - timedelta(days=retention_days)
|
||||
|
||||
expired_old = cutoff - timedelta(days=10)
|
||||
expired_newer = cutoff - timedelta(days=1)
|
||||
not_expired = cutoff + timedelta(days=1)
|
||||
|
||||
# A) Not deleted: should keep CREATE + latest UPDATE, prune intermediate UPDATEs
|
||||
a_create = self._make_oc(ct=ct, obj_id=1, action=ObjectChangeActionChoices.ACTION_CREATE, ts=expired_old)
|
||||
a_update1 = self._make_oc(ct=ct, obj_id=1, action=ObjectChangeActionChoices.ACTION_UPDATE, ts=expired_old)
|
||||
a_update2 = self._make_oc(ct=ct, obj_id=1, action=ObjectChangeActionChoices.ACTION_UPDATE, ts=expired_newer)
|
||||
|
||||
# B) Deleted (all expired): should keep NOTHING
|
||||
b_create = self._make_oc(ct=ct, obj_id=2, action=ObjectChangeActionChoices.ACTION_CREATE, ts=expired_old)
|
||||
b_update = self._make_oc(ct=ct, obj_id=2, action=ObjectChangeActionChoices.ACTION_UPDATE, ts=expired_newer)
|
||||
b_delete = self._make_oc(ct=ct, obj_id=2, action=ObjectChangeActionChoices.ACTION_DELETE, ts=expired_newer)
|
||||
|
||||
# C) Deleted but delete is not expired: create/update expired should be pruned; delete remains
|
||||
c_create = self._make_oc(ct=ct, obj_id=3, action=ObjectChangeActionChoices.ACTION_CREATE, ts=expired_old)
|
||||
c_update = self._make_oc(ct=ct, obj_id=3, action=ObjectChangeActionChoices.ACTION_UPDATE, ts=expired_newer)
|
||||
c_delete = self._make_oc(ct=ct, obj_id=3, action=ObjectChangeActionChoices.ACTION_DELETE, ts=not_expired)
|
||||
|
||||
self._run_prune(retention_days=retention_days, retain_create_last_update=True)
|
||||
|
||||
remaining = set(ObjectChange.objects.values_list('pk', flat=True))
|
||||
|
||||
# A) Not deleted -> create + latest update remain
|
||||
self.assertIn(a_create, remaining)
|
||||
self.assertIn(a_update2, remaining)
|
||||
self.assertNotIn(a_update1, remaining)
|
||||
|
||||
# B) Deleted (all expired) -> nothing remains
|
||||
self.assertNotIn(b_create, remaining)
|
||||
self.assertNotIn(b_update, remaining)
|
||||
self.assertNotIn(b_delete, remaining)
|
||||
|
||||
# C) Deleted, delete not expired -> delete remains, but create/update are pruned
|
||||
self.assertNotIn(c_create, remaining)
|
||||
self.assertNotIn(c_update, remaining)
|
||||
self.assertIn(c_delete, remaining)
|
||||
|
||||
def test_prune_disabled_deletes_all_expired(self):
|
||||
ct = ContentType.objects.get_for_model(Site)
|
||||
|
||||
retention_days = 90
|
||||
now = timezone.now()
|
||||
cutoff = now - timedelta(days=retention_days)
|
||||
expired = cutoff - timedelta(days=1)
|
||||
not_expired = cutoff + timedelta(days=1)
|
||||
|
||||
# expired create/update should be deleted when feature disabled
|
||||
x_create = self._make_oc(ct=ct, obj_id=10, action=ObjectChangeActionChoices.ACTION_CREATE, ts=expired)
|
||||
x_update = self._make_oc(ct=ct, obj_id=10, action=ObjectChangeActionChoices.ACTION_UPDATE, ts=expired)
|
||||
|
||||
# non-expired delete should remain regardless
|
||||
y_delete = self._make_oc(ct=ct, obj_id=11, action=ObjectChangeActionChoices.ACTION_DELETE, ts=not_expired)
|
||||
|
||||
self._run_prune(retention_days=retention_days, retain_create_last_update=False)
|
||||
|
||||
remaining = set(ObjectChange.objects.values_list('pk', flat=True))
|
||||
self.assertNotIn(x_create, remaining)
|
||||
self.assertNotIn(x_update, remaining)
|
||||
self.assertIn(y_delete, remaining)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import warnings
|
||||
from datetime import timedelta
|
||||
from importlib import import_module
|
||||
|
||||
@@ -5,9 +6,11 @@ import requests
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Exists, OuterRef, Subquery
|
||||
from django.utils import timezone
|
||||
from packaging import version
|
||||
|
||||
from core.choices import ObjectChangeActionChoices
|
||||
from core.models import Job, ObjectChange
|
||||
from netbox.config import Config
|
||||
from utilities.proxy import resolve_proxies
|
||||
@@ -17,11 +20,12 @@ class Command(BaseCommand):
|
||||
help = "Perform nightly housekeeping tasks [DEPRECATED]"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write(
|
||||
warnings.warn(
|
||||
"\n\nDEPRECATION WARNING\n"
|
||||
"Running this command is no longer necessary: All housekeeping tasks\n"
|
||||
"are addressed automatically via NetBox's built-in job scheduler. It\n"
|
||||
"will be removed in a future release.",
|
||||
self.style.WARNING
|
||||
"will be removed in a future release.\n",
|
||||
category=FutureWarning,
|
||||
)
|
||||
|
||||
config = Config()
|
||||
@@ -45,29 +49,63 @@ class Command(BaseCommand):
|
||||
|
||||
# Delete expired ObjectChanges
|
||||
if options['verbosity']:
|
||||
self.stdout.write("[*] Checking for expired changelog records")
|
||||
self.stdout.write('[*] Checking for expired changelog records')
|
||||
if config.CHANGELOG_RETENTION:
|
||||
cutoff = timezone.now() - timedelta(days=config.CHANGELOG_RETENTION)
|
||||
if options['verbosity'] >= 2:
|
||||
self.stdout.write(f"\tRetention period: {config.CHANGELOG_RETENTION} days")
|
||||
self.stdout.write(f"\tCut-off time: {cutoff}")
|
||||
expired_records = ObjectChange.objects.filter(time__lt=cutoff).count()
|
||||
self.stdout.write(f'\tRetention period: {config.CHANGELOG_RETENTION} days')
|
||||
self.stdout.write(f'\tCut-off time: {cutoff}')
|
||||
|
||||
expired_qs = ObjectChange.objects.filter(time__lt=cutoff)
|
||||
|
||||
# When enabled, retain each object's original create and most recent update record while pruning expired
|
||||
# changelog entries. This applies only to objects without a delete record.
|
||||
if config.CHANGELOG_RETAIN_CREATE_LAST_UPDATE:
|
||||
if options['verbosity'] >= 2:
|
||||
self.stdout.write('\tRetaining create & last update records for non-deleted objects')
|
||||
|
||||
deleted_exists = ObjectChange.objects.filter(
|
||||
action=ObjectChangeActionChoices.ACTION_DELETE,
|
||||
changed_object_type_id=OuterRef('changed_object_type_id'),
|
||||
changed_object_id=OuterRef('changed_object_id'),
|
||||
)
|
||||
|
||||
# Keep create records only where no delete exists for that object
|
||||
create_pks_to_keep = (
|
||||
ObjectChange.objects.filter(action=ObjectChangeActionChoices.ACTION_CREATE)
|
||||
.annotate(has_delete=Exists(deleted_exists))
|
||||
.filter(has_delete=False)
|
||||
.values('pk')
|
||||
)
|
||||
|
||||
# Keep the most recent update per object only where no delete exists for the object
|
||||
latest_update_pks_to_keep = (
|
||||
ObjectChange.objects.filter(action=ObjectChangeActionChoices.ACTION_UPDATE)
|
||||
.annotate(has_delete=Exists(deleted_exists))
|
||||
.filter(has_delete=False)
|
||||
.order_by('changed_object_type_id', 'changed_object_id', '-time', '-pk')
|
||||
.distinct('changed_object_type_id', 'changed_object_id')
|
||||
.values('pk')
|
||||
)
|
||||
|
||||
expired_qs = expired_qs.exclude(pk__in=Subquery(create_pks_to_keep))
|
||||
expired_qs = expired_qs.exclude(pk__in=Subquery(latest_update_pks_to_keep))
|
||||
|
||||
expired_records = expired_qs.count()
|
||||
if expired_records:
|
||||
if options['verbosity']:
|
||||
self.stdout.write(
|
||||
f"\tDeleting {expired_records} expired records... ",
|
||||
self.style.WARNING,
|
||||
ending=""
|
||||
f'\tDeleting {expired_records} expired records... ', self.style.WARNING, ending=''
|
||||
)
|
||||
self.stdout.flush()
|
||||
ObjectChange.objects.filter(time__lt=cutoff).delete()
|
||||
expired_qs.delete()
|
||||
if options['verbosity']:
|
||||
self.stdout.write("Done.", self.style.SUCCESS)
|
||||
self.stdout.write('Done.', self.style.SUCCESS)
|
||||
elif options['verbosity']:
|
||||
self.stdout.write("\tNo expired records found.", self.style.SUCCESS)
|
||||
self.stdout.write('\tNo expired records found.', self.style.SUCCESS)
|
||||
elif options['verbosity']:
|
||||
self.stdout.write(
|
||||
f"\tSkipping: No retention period specified (CHANGELOG_RETENTION = {config.CHANGELOG_RETENTION})"
|
||||
f'\tSkipping: No retention period specified (CHANGELOG_RETENTION = {config.CHANGELOG_RETENTION})'
|
||||
)
|
||||
|
||||
# Delete expired Jobs
|
||||
|
||||
@@ -677,15 +677,19 @@ class ConfigContextTest(TestCase):
|
||||
if hasattr(node, 'children'):
|
||||
for child in node.children:
|
||||
try:
|
||||
if child.rhs.query.model is TaggedItem:
|
||||
subqueries.append(child.rhs.query)
|
||||
# In Django 6.0+, rhs is a Query directly; older Django wraps it in Subquery
|
||||
rhs_query = getattr(child.rhs, 'query', child.rhs)
|
||||
if rhs_query.model is TaggedItem:
|
||||
subqueries.append(rhs_query)
|
||||
except AttributeError:
|
||||
traverse(child)
|
||||
traverse(where_node)
|
||||
return subqueries
|
||||
|
||||
# In Django 6.0+, the annotation is a Query directly; older Django wraps it in Subquery
|
||||
annotation_query = getattr(config_annotation, 'query', config_annotation)
|
||||
# Find subqueries in the WHERE clause that should have DISTINCT
|
||||
tag_subqueries = find_tag_subqueries(config_annotation.query.where)
|
||||
tag_subqueries = find_tag_subqueries(annotation_query.where)
|
||||
distinct_subqueries = [sq for sq in tag_subqueries if sq.distinct]
|
||||
|
||||
# Verify we found at least one DISTINCT subquery for tags
|
||||
|
||||
@@ -94,9 +94,11 @@ class NetHost(Lookup):
|
||||
rhs, rhs_params = self.process_rhs(qn, connection)
|
||||
# Query parameters are automatically converted to IPNetwork objects, which are then turned to strings. We need
|
||||
# to omit the mask portion of the object's string representation to match PostgreSQL's HOST() function.
|
||||
# Note: params may be tuples (Django 6.0+) or lists (older Django), so convert before mutating.
|
||||
rhs_params = list(rhs_params)
|
||||
if rhs_params:
|
||||
rhs_params[0] = rhs_params[0].split('/')[0]
|
||||
params = lhs_params + rhs_params
|
||||
params = list(lhs_params) + rhs_params
|
||||
return f'HOST({lhs}) = {rhs}', params
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from rest_framework.viewsets import GenericViewSet
|
||||
from netbox.api.serializers.features import ChangeLogMessageSerializer
|
||||
from netbox.constants import ADVISORY_LOCK_KEYS
|
||||
from utilities.api import get_annotations_for_serializer, get_prefetches_for_serializer
|
||||
from utilities.exceptions import AbortRequest
|
||||
from utilities.exceptions import AbortRequest, PreconditionFailed
|
||||
from utilities.query import reapply_model_ordering
|
||||
|
||||
from . import mixins
|
||||
@@ -34,6 +34,50 @@ HTTP_ACTIONS = {
|
||||
}
|
||||
|
||||
|
||||
class ETagMixin:
|
||||
"""
|
||||
Adds ETag header support to ViewSets. Generates weak ETags (W/ prefix per
|
||||
RFC 7232 §2.1) from `last_updated` (or `created` if unavailable). Weak ETags
|
||||
are appropriate here because the tag is derived from a modification timestamp
|
||||
rather than a hash of the serialized payload.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _get_etag(obj):
|
||||
"""Return a weak ETag string for the given object, or None."""
|
||||
if ts := getattr(obj, 'last_updated', None) or getattr(obj, 'created', None):
|
||||
return f'W/"{ts.isoformat()}"'
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _get_if_match(request):
|
||||
"""Return the list of If-Match header values (if specified)."""
|
||||
if (if_match := request.META.get('HTTP_IF_MATCH')) and if_match != '*':
|
||||
return [e.strip() for e in if_match.split(',')]
|
||||
return []
|
||||
|
||||
def _validate_etag(self, request, instance):
|
||||
"""Validate the request's ETag"""
|
||||
if provided := self._get_if_match(request):
|
||||
current_etag = self._get_etag(instance)
|
||||
if current_etag and current_etag not in provided:
|
||||
raise PreconditionFailed(etag=current_etag)
|
||||
|
||||
def handle_exception(self, exc):
|
||||
response = super().handle_exception(exc)
|
||||
if isinstance(exc, PreconditionFailed) and exc.etag:
|
||||
response['ETag'] = exc.etag
|
||||
return response
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
serializer = self.get_serializer(instance)
|
||||
response = Response(serializer.data)
|
||||
if etag := self._get_etag(instance):
|
||||
response['ETag'] = etag
|
||||
return response
|
||||
|
||||
|
||||
class BaseViewSet(GenericViewSet):
|
||||
"""
|
||||
Base class for all API ViewSets. This is responsible for the enforcement of object-based permissions.
|
||||
@@ -95,6 +139,7 @@ class BaseViewSet(GenericViewSet):
|
||||
|
||||
|
||||
class NetBoxReadOnlyModelViewSet(
|
||||
ETagMixin,
|
||||
mixins.CustomFieldsMixin,
|
||||
mixins.ExportTemplatesMixin,
|
||||
drf_mixins.RetrieveModelMixin,
|
||||
@@ -105,6 +150,7 @@ class NetBoxReadOnlyModelViewSet(
|
||||
|
||||
|
||||
class NetBoxModelViewSet(
|
||||
ETagMixin,
|
||||
mixins.BulkUpdateModelMixin,
|
||||
mixins.BulkDestroyModelMixin,
|
||||
mixins.ObjectValidationMixin,
|
||||
@@ -191,7 +237,14 @@ class NetBoxModelViewSet(
|
||||
serializer = self.get_serializer(qs, many=bulk_create)
|
||||
|
||||
headers = self.get_success_headers(serializer.data)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||
response = Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||
|
||||
# Add ETag for single-object creation only (bulk returns a list, no single ETag)
|
||||
if not bulk_create:
|
||||
if etag := self._get_etag(qs):
|
||||
response['ETag'] = etag
|
||||
|
||||
return response
|
||||
|
||||
def perform_create(self, serializer):
|
||||
model = self.queryset.model
|
||||
@@ -211,6 +264,10 @@ class NetBoxModelViewSet(
|
||||
def update(self, request, *args, **kwargs):
|
||||
partial = kwargs.pop('partial', False)
|
||||
instance = self.get_object_with_snapshot()
|
||||
|
||||
# Enforce If-Match precondition (RFC 9110 §13.1.1)
|
||||
self._validate_etag(self.request, instance)
|
||||
|
||||
serializer = self.get_serializer(instance, data=request.data, partial=partial)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
self.perform_update(serializer)
|
||||
@@ -221,8 +278,12 @@ class NetBoxModelViewSet(
|
||||
|
||||
# Re-serialize the instance(s) with prefetched data
|
||||
serializer = self.get_serializer(qs)
|
||||
response = Response(serializer.data)
|
||||
|
||||
return Response(serializer.data)
|
||||
if etag := self._get_etag(qs):
|
||||
response['ETag'] = etag
|
||||
|
||||
return response
|
||||
|
||||
def perform_update(self, serializer):
|
||||
model = self.queryset.model
|
||||
@@ -232,6 +293,11 @@ class NetBoxModelViewSet(
|
||||
# Enforce object-level permissions on save()
|
||||
try:
|
||||
with transaction.atomic(using=router.db_for_write(model)):
|
||||
# Re-check the If-Match ETag under a row-level lock to close the TOCTOU window
|
||||
# between the initial check in update() and the actual write.
|
||||
if self._get_if_match(self.request):
|
||||
locked = model.objects.select_for_update().get(pk=serializer.instance.pk)
|
||||
self._validate_etag(self.request, locked)
|
||||
instance = serializer.save()
|
||||
self._validate_objects(instance)
|
||||
except ObjectDoesNotExist:
|
||||
@@ -242,6 +308,9 @@ class NetBoxModelViewSet(
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
instance = self.get_object_with_snapshot()
|
||||
|
||||
# Enforce If-Match precondition (RFC 9110 §13.1.1)
|
||||
self._validate_etag(request, instance)
|
||||
|
||||
# Attach changelog message (if any)
|
||||
serializer = ChangeLogMessageSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
@@ -256,7 +325,16 @@ class NetBoxModelViewSet(
|
||||
logger = logging.getLogger(f'netbox.api.views.{self.__class__.__name__}')
|
||||
logger.info(f"Deleting {model._meta.verbose_name} {instance} (PK: {instance.pk})")
|
||||
|
||||
return super().perform_destroy(instance)
|
||||
try:
|
||||
with transaction.atomic(using=router.db_for_write(model)):
|
||||
# Re-check the If-Match ETag under a row-level lock to close the TOCTOU window
|
||||
# between the initial check in destroy() and the actual delete.
|
||||
if self._get_if_match(self.request):
|
||||
locked = model.objects.select_for_update().get(pk=instance.pk)
|
||||
self._validate_etag(self.request, locked)
|
||||
super().perform_destroy(instance)
|
||||
except ObjectDoesNotExist:
|
||||
raise PermissionDenied()
|
||||
|
||||
|
||||
class MPTTLockedMixin:
|
||||
|
||||
@@ -10,6 +10,7 @@ from .parameters import PARAMS
|
||||
|
||||
__all__ = (
|
||||
'PARAMS',
|
||||
'Config',
|
||||
'ConfigItem',
|
||||
'clear_config',
|
||||
'get_config',
|
||||
|
||||
@@ -175,6 +175,25 @@ PARAMS = (
|
||||
field=forms.JSONField
|
||||
),
|
||||
|
||||
# Change log
|
||||
ConfigParam(
|
||||
name='CHANGELOG_RETENTION',
|
||||
label=_('Changelog retention'),
|
||||
default=90,
|
||||
description=_("Days to retain changelog history (set to zero for unlimited)"),
|
||||
field=forms.IntegerField,
|
||||
),
|
||||
ConfigParam(
|
||||
name='CHANGELOG_RETAIN_CREATE_LAST_UPDATE',
|
||||
label=_('Retain create & last update changelog records'),
|
||||
default=True,
|
||||
description=_(
|
||||
"Retain each object's create record and most recent update record when pruning expired changelog entries "
|
||||
"(excluding objects with a delete record)."
|
||||
),
|
||||
field=forms.BooleanField,
|
||||
),
|
||||
|
||||
# Miscellaneous
|
||||
ConfigParam(
|
||||
name='MAINTENANCE_MODE',
|
||||
@@ -199,13 +218,6 @@ PARAMS = (
|
||||
description=_("Enable the GraphQL API"),
|
||||
field=forms.BooleanField
|
||||
),
|
||||
ConfigParam(
|
||||
name='CHANGELOG_RETENTION',
|
||||
label=_('Changelog retention'),
|
||||
default=90,
|
||||
description=_("Days to retain changelog history (set to zero for unlimited)"),
|
||||
field=forms.IntegerField
|
||||
),
|
||||
ConfigParam(
|
||||
name='JOB_RETENTION',
|
||||
label=_('Job result retention'),
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import logging
|
||||
import os
|
||||
import traceback
|
||||
from abc import ABC, abstractmethod
|
||||
from datetime import timedelta
|
||||
from pathlib import Path
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils import timezone
|
||||
@@ -21,6 +24,11 @@ __all__ = (
|
||||
'system_job',
|
||||
)
|
||||
|
||||
# The installation root, e.g. "/opt/netbox/". Used to strip absolute path
|
||||
# prefixes from traceback file paths before recording them in the job log.
|
||||
# jobs.py lives at <root>/netbox/netbox/jobs.py, so parents[2] is the root.
|
||||
_INSTALL_ROOT = str(Path(__file__).resolve().parents[2]) + os.sep
|
||||
|
||||
|
||||
def system_job(interval):
|
||||
"""
|
||||
@@ -107,6 +115,13 @@ class JobRunner(ABC):
|
||||
job.terminate(status=JobStatusChoices.STATUS_FAILED)
|
||||
|
||||
except Exception as e:
|
||||
tb_str = traceback.format_exc().replace(_INSTALL_ROOT, '')
|
||||
tb_record = logging.makeLogRecord({
|
||||
'levelno': logging.ERROR,
|
||||
'levelname': 'ERROR',
|
||||
'msg': tb_str,
|
||||
})
|
||||
job.log(tb_record)
|
||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED, error=repr(e))
|
||||
if type(e) is JobTimeoutException:
|
||||
logger.error(e)
|
||||
|
||||
@@ -435,6 +435,7 @@ INSTALLED_APPS = [
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'django.contrib.humanize',
|
||||
'django.contrib.postgres',
|
||||
'django.forms',
|
||||
'corsheaders',
|
||||
'debug_toolbar',
|
||||
|
||||
@@ -10,6 +10,7 @@ from core.models import DataSource, Job
|
||||
from utilities.testing import disable_warnings
|
||||
|
||||
from ..jobs import *
|
||||
from ..jobs import _INSTALL_ROOT
|
||||
|
||||
|
||||
class TestJobRunner(JobRunner):
|
||||
@@ -83,6 +84,12 @@ class JobRunnerTest(JobRunnerTestCase):
|
||||
|
||||
self.assertEqual(job.status, JobStatusChoices.STATUS_ERRORED)
|
||||
self.assertEqual(job.error, repr(ErroredJobRunner.EXP))
|
||||
self.assertEqual(len(job.log_entries), 1)
|
||||
self.assertEqual(job.log_entries[0]['level'], 'error')
|
||||
tb_message = job.log_entries[0]['message']
|
||||
self.assertIn('Traceback', tb_message)
|
||||
self.assertIn('Test error', tb_message)
|
||||
self.assertNotIn(_INSTALL_ROOT, tb_message)
|
||||
|
||||
|
||||
class EnqueueTest(JobRunnerTestCase):
|
||||
|
||||
@@ -122,6 +122,19 @@
|
||||
{% endif %}
|
||||
</tr>
|
||||
|
||||
{# Changelog #}
|
||||
<tr>
|
||||
<td colspan="2" class="bg-secondary-subtle fs-5 fw-bold border-0 py-1">{% trans "Change log" %}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="ps-3">{% trans "Changelog retention" %}</th>
|
||||
<td>{{ config.CHANGELOG_RETENTION }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="ps-3">{% trans "Changelog retain create & last update records" %}</th>
|
||||
<td>{% checkmark config.CHANGELOG_RETAIN_CREATE_LAST_UPDATE %}</td>
|
||||
</tr>
|
||||
|
||||
{# Miscellaneous #}
|
||||
<tr>
|
||||
<td colspan="2" class="bg-secondary-subtle fs-5 fw-bold border-0 py-1">{% trans "Miscellaneous" %}</td>
|
||||
@@ -137,10 +150,6 @@
|
||||
<th scope="row" class="ps-3">{% trans "GraphQL enabled" %}</th>
|
||||
<td>{% checkmark config.GRAPHQL_ENABLED %}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="ps-3">{% trans "Changelog retention" %}</th>
|
||||
<td>{{ config.CHANGELOG_RETENTION }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="ps-3">{% trans "Job retention" %}</th>
|
||||
<td>{{ config.JOB_RETENTION }}</td>
|
||||
|
||||
@@ -6,6 +6,6 @@
|
||||
{% block content %}
|
||||
{{ block.super }}
|
||||
<div class="text-muted px-3">
|
||||
{% trans "Change log retention" %}: {% if config.CHANGELOG_RETENTION %}{{ config.CHANGELOG_RETENTION }} {% trans "days" %}{% else %}{% trans "Indefinite" %}{% endif %}
|
||||
{% trans "Change log retention" %}: {% if config.CHANGELOG_RETENTION %}{{ config.CHANGELOG_RETENTION }} {% trans "days" %}{% if config.CHANGELOG_RETAIN_CREATE_LAST_UPDATE %} ({% trans "retaining create & last update records for non-deleted objects" %}){% endif %}{% else %}{% trans "Indefinite" %}{% endif %}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted">
|
||||
{% trans "Change log retention" %}: {% if config.CHANGELOG_RETENTION %}{{ config.CHANGELOG_RETENTION }} {% trans "days" %}{% else %}{% trans "Indefinite" %}{% endif %}
|
||||
{% trans "Change log retention" %}: {% if config.CHANGELOG_RETENTION %}{{ config.CHANGELOG_RETENTION }} {% trans "days" %}{% if config.CHANGELOG_RETAIN_CREATE_LAST_UPDATE %} ({% trans "retaining create & last update records for non-deleted objects" %}){% endif %}{% else %}{% trans "Indefinite" %}{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -6,6 +6,7 @@ __all__ = (
|
||||
'AbortScript',
|
||||
'AbortTransaction',
|
||||
'PermissionsViolation',
|
||||
'PreconditionFailed',
|
||||
'RQWorkerNotRunningException',
|
||||
)
|
||||
|
||||
@@ -40,6 +41,20 @@ class PermissionsViolation(Exception):
|
||||
message = "Operation failed due to object-level permissions violation"
|
||||
|
||||
|
||||
class PreconditionFailed(APIException):
|
||||
"""
|
||||
Raised when an If-Match precondition is not satisfied (HTTP 412).
|
||||
Optionally carries the current ETag so it can be included in the response.
|
||||
"""
|
||||
status_code = status.HTTP_412_PRECONDITION_FAILED
|
||||
default_detail = 'Precondition failed.'
|
||||
default_code = 'precondition_failed'
|
||||
|
||||
def __init__(self, detail=None, code=None, etag=None):
|
||||
super().__init__(detail=detail, code=code)
|
||||
self.etag = etag
|
||||
|
||||
|
||||
class RQWorkerNotRunningException(APIException):
|
||||
"""
|
||||
Indicates the temporary inability to enqueue a new task (e.g. custom script execution) because no RQ worker
|
||||
|
||||
@@ -114,7 +114,12 @@ class APIViewTestCases:
|
||||
|
||||
# Try GET to permitted object
|
||||
url = self._get_detail_url(instance1)
|
||||
self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_200_OK)
|
||||
response = self.client.get(url, **self.header)
|
||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||
|
||||
# Verify ETag header is present for objects with timestamps
|
||||
if issubclass(self.model, ChangeLoggingMixin):
|
||||
self.assertIn('ETag', response, "ETag header missing from detail response")
|
||||
|
||||
# Try GET to non-permitted object
|
||||
url = self._get_detail_url(instance2)
|
||||
@@ -367,6 +372,46 @@ class APIViewTestCases:
|
||||
self.assertEqual(objectchange.action, ObjectChangeActionChoices.ACTION_UPDATE)
|
||||
self.assertEqual(objectchange.message, data['changelog_message'])
|
||||
|
||||
def test_update_object_with_etag(self):
|
||||
"""
|
||||
PATCH an object using a valid If-Match ETag → expect 200.
|
||||
PATCH again with the now-stale ETag → expect 412.
|
||||
"""
|
||||
if not issubclass(self.model, ChangeLoggingMixin):
|
||||
self.skipTest("Model does not support ETags")
|
||||
|
||||
self.add_permissions(
|
||||
f'{self.model._meta.app_label}.view_{self.model._meta.model_name}',
|
||||
f'{self.model._meta.app_label}.change_{self.model._meta.model_name}',
|
||||
)
|
||||
instance = self._get_queryset().first()
|
||||
url = self._get_detail_url(instance)
|
||||
update_data = self.update_data or getattr(self, 'create_data')[0]
|
||||
|
||||
# Fetch current ETag
|
||||
get_response = self.client.get(url, **self.header)
|
||||
self.assertHttpStatus(get_response, status.HTTP_200_OK)
|
||||
etag = get_response.get('ETag')
|
||||
self.assertIsNotNone(etag, "No ETag returned by GET")
|
||||
|
||||
# PATCH with correct ETag → 200
|
||||
response = self.client.patch(
|
||||
url, update_data, format='json',
|
||||
**{**self.header, 'HTTP_IF_MATCH': etag}
|
||||
)
|
||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||
new_etag = response.get('ETag')
|
||||
self.assertIsNotNone(new_etag)
|
||||
self.assertNotEqual(etag, new_etag) # ETag must change after update
|
||||
|
||||
# PATCH with the old (stale) ETag → 412
|
||||
with disable_warnings('django.request'):
|
||||
response = self.client.patch(
|
||||
url, update_data, format='json',
|
||||
**{**self.header, 'HTTP_IF_MATCH': etag}
|
||||
)
|
||||
self.assertHttpStatus(response, status.HTTP_412_PRECONDITION_FAILED)
|
||||
|
||||
def test_bulk_update_objects(self):
|
||||
"""
|
||||
PATCH a set of objects in a single request.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
colorama==0.4.6
|
||||
Django==5.2.11
|
||||
Django==6.0.3
|
||||
django-cors-headers==4.9.0
|
||||
django-debug-toolbar==6.2.0
|
||||
django-filter==25.2
|
||||
@@ -7,7 +7,7 @@ django-graphiql-debug-toolbar==0.2.0
|
||||
django-htmx==1.27.0
|
||||
django-mptt==0.18.0
|
||||
django-pglocks==1.0.4
|
||||
django-prometheus==2.4.1
|
||||
django-prometheus==2.4.0
|
||||
django-redis==6.0.0
|
||||
django-rich==2.2.0
|
||||
django-rq==3.2.2
|
||||
|
||||
Reference in New Issue
Block a user