Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON serializer issue for Protection Rules defined in configuration.py #18369

Closed
atownson opened this issue Jan 9, 2025 · 2 comments · Fixed by #18388
Closed

JSON serializer issue for Protection Rules defined in configuration.py #18369

atownson opened this issue Jan 9, 2025 · 2 comments · Fixed by #18388
Assignees
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@atownson
Copy link
Contributor

atownson commented Jan 9, 2025

Deployment Type

Self-hosted

Triage priority

N/A

NetBox Version

v4.1.11

Python Version

3.12

Steps to Reproduce

  1. Create a custom Protection_Rule in the configuration.py. Example:
from extras.validators import CustomValidator

# Prevent IP addresses from being deleted by a cascading delete
class IpAddressProtectionRule(CustomValidator):
    def validate(self, instance, request):
        if hasattr(instance, 'primary_ip') and instance.primary_ip is not None:
            self.fail('This instance has a related IP address(es).')


PROTECTION_RULES = {
    'dcim.device': (
        IpAddressProtectionRule(),
    ),
}
  1. Restart NetBox services
  2. Navigate to Admin -> System

Expected Behavior

The system detail page is returned.

Observed Behavior

An exception is thrown:
image

@atownson atownson added status: needs triage This issue is awaiting triage by a maintainer type: bug A confirmed report of unexpected behavior in the application labels Jan 9, 2025
@atownson
Copy link
Contributor Author

atownson commented Jan 9, 2025

I believe the fix for this is to update netbox/netbox/tempates/core/inc/config_data.html as follows:
Current:

...
  <tr>
    <th scope="row" class="border-0 ps-3">{% trans "Protection rules" %}</th>
    {% if config.PROTECTION_RULES %}
      <td class="border-0"><pre>{{ config.PROTECTION_RULES|json }}</pre></td>
    {% else %}
      <td class="border-0">{{ ''|placeholder }}</td>
    {% endif %}
  </tr>
...

Proposed change:

...
  <tr>
    <th scope="row" class="border-0 ps-3">{% trans "Protection rules" %}</th>
    {% if config.PROTECTION_RULES %}
      <td class="border-0"><pre>{{ config.PROTECTION_RULES }}</pre></td>
    {% else %}
      <td class="border-0">{{ ''|placeholder }}</td>
    {% endif %}
  </tr>
...

This will match the CUSTOM_VALIDATORS block above it. I can submit a PR for this if approved.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation severity: medium Results in substantial degraded or broken functionality for specfic workflows and removed status: needs triage This issue is awaiting triage by a maintainer labels Jan 13, 2025
@jeremystretch
Copy link
Member

Thanks @atownson, I've assigned this to you.

bctiemann pushed a commit that referenced this issue Jan 28, 2025
* Remove the json filter for protection rules

* Configure PROTECTION_RULE config attribute to use ConfigJSONEncoder as serializer

* Tweak getattr()

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
bctiemann pushed a commit that referenced this issue Jan 29, 2025
* Remove the json filter for protection rules

* Configure PROTECTION_RULE config attribute to use ConfigJSONEncoder as serializer

* Tweak getattr()

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants