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

Managed Records do not trigger event rules for netbox_dns | record object type #246

Closed
aaronhuff opened this issue Apr 22, 2024 · 4 comments
Assignees

Comments

@aaronhuff
Copy link

Versions
NetBox Version: 3.7.5
NetBox DNS Version: 0.22.8
Python Version: 3.11.4

Describe the bug
Managed records created by the IP Address coupling feature, do not trigger the netbox_dns | record content type event rules (create, updated, delete).

To Reproduce
Steps to reproduce the behavior:

  1. Enable coupling feature.
  2. Create a netbox_dns | record content type event rule for create/update
  3. Populate DNS custom fields on ip address

Expected result
Trigger event rule and run webhook/script.

Actual result
No event rule triggers.

Screenshots
image

Code Examples

PLUGINS_CONFIG = {
    'netbox_dns': {
        'feature_ipam_coupling': True,
    }
}
 docker exec -it netbox /opt/netbox/netbox/manage.py setup_coupling
@peteeckel
Copy link
Owner

peteeckel commented Apr 22, 2024

That's interesting - I can't reproduce this at all.

(netbox) [root@dns ~]# /opt/netbox/netbox/manage.py webhook_receiver 
Listening on port http://localhost:9000. Stop with CONTROL-C.
[1] Mon, 22 Apr 2024 19:15:42 GMT 127.0.0.1 "POST / HTTP/1.1" 200 -
Host: localhost:9000
Accept-Encoding: identity
User-Agent: python-urllib3/2.1.0
Content-Type: application/json
Content-Length: 1379

{
    "event": "updated",
    "timestamp": "2024-04-22T19:15:42.165442+00:00",
    "model": "record",
    "username": "admin",
    "request_id": "6f10377d-8b66-4ee5-9983-677c398771af",
    "data": {
        "id": 180,
        "url": "/api/plugins/netbox-dns/records/180/",
        "zone": {
            "id": 7,
            "url": "/api/plugins/netbox-dns/zones/7/",
            "display": "0.0.10.in-addr.arpa",
            "name": "0.0.10.in-addr.arpa",
            "view": null,
            "status": "active",
            "active": true,
            "rfc2317_prefix": null
        },
        "display": "1.0.0.10.in-addr.arpa [CNAME]",
        "type": "CNAME",
        "name": "1",
        "fqdn": "1.0.0.10.in-addr.arpa.",
        "value": "1.0-31.0.0.10.in-addr.arpa.",
        "status": "active",
        "ttl": 86400,
        "description": "",
        "tags": [],
        "created": "2024-01-31T16:02:55.912977Z",
        "last_updated": "2024-04-02T22:40:58.013878Z",
        "managed": true,
        "disable_ptr": false,
        "ptr_record": null,
        "address_record": null,
        "active": true,
        "custom_fields": {},
        "tenant": null,
        "ipam_ip_address": null
    },
    "snapshots": {
        "prechange": null,
        "postchange": {
            "created": "2024-01-31T16:02:55.912Z",
            "last_updated": "2024-04-02T22:40:58.013Z",
            "name": "1",
            "zone": 7,
            "fqdn": "1.0.0.10.in-addr.arpa.",
            "type": "CNAME",
            "value": "1.0-31.0.0.10.in-addr.arpa.",
            "status": "active",
            "ttl": 86400,
            "managed": true,
            "ptr_record": null,
            "disable_ptr": false,
            "description": "",
            "tenant": null,
            "ip_address": null,
            "ipam_ip_address": null,
            "rfc2317_cname_record": null,
            "custom_fields": {},
            "tags": []
        }
    }
}
Completed request #1

There is a known problem with Event Rules/Webhooks, but that results in too many invocations with wrong data (see #89).

Actually it is next to impossible to avoid Webhooks being triggered, because it's internal functionality in NetBox.

What does the change logs say? Have the records actually been updated? And how do you detect the triggering of the webhook?

@peteeckel peteeckel self-assigned this Apr 22, 2024
@aaronhuff
Copy link
Author

Did you create a manual record in your test (I notice it's a CNAME which is not one the fields the coupling/managed records handles?).

I'm monitoring the jobs as well as the logs of the netbox worker.

Also, I'm triggering a script.. not a webhook. The change log do show as the record as updated but the initial creation doesn't show up in the change log (if it's supposed to).

image

@peteeckel
Copy link
Owner

Hi @aaronhuff, thanks for the update.

In fact I created an IP address with an associated DNS record, and got a much longer series of Webhook invocations - The A record itself, the PTR record, the SOA records for both the A and PTR zone and so on. So Webhook triggering definitely works.

The change log should contain separate entries for the creation and updates of all records. But for my question it's sufficient to know that there are change log entries for the record, so it was definitely written to the database.

Event rules operate within NetBox for all models inheriting from EventRulesMixin (which is in the standard NetBoxModelFeatureSet so that means basically all models - with no exception for netbox_dns.record). If any object of one of these model classes is saved, this event is stored in a queue and at the end of the request the event rules associated with the model are triggered. So there is actually no way to avoid triggering a well-defined event hook except for avoiding to save an object - which the IPAM Coupling handler did, so that error can be ruled out.

The next step would be to get your script out of the picture to make really sure the event rule has been triggered. Could you please create a webhook like this:

Screenshot 2024-04-23 at 15 14 40

and add it to your event rule:

Screenshot 2024-04-23 at 15 15 51

Then, start the webhook receiver via manage.py:

(netbox) [root@dns netbox]# /opt/netbox/netbox/manage.py webhook_receiver 
Listening on port http://localhost:9000. Stop with CONTROL-C.

Now you can create or update an IP address with an associated DNS record. The output of the test receiver should look like this (full text this time):

[1] Tue, 23 Apr 2024 13:21:49 GMT 127.0.0.1 "POST / HTTP/1.1" 200 -
Host: localhost:9000
Accept-Encoding: identity
User-Agent: python-urllib3/2.1.0
Content-Type: application/json
Content-Length: 1360

{
    "event": "created",
    "timestamp": "2024-04-23T13:21:49.096260+00:00",
    "model": "record",
    "username": "admin",
    "request_id": "f6b0a463-e2e7-4504-9153-8ee0ae97585a",
    "data": {
        "id": 218,
        "url": "/api/plugins/netbox-dns/records/218/",
        "zone": {
            "id": 23,
            "url": "/api/plugins/netbox-dns/zones/23/",
            "display": "0.1.10.in-addr.arpa",
            "name": "0.1.10.in-addr.arpa",
            "view": null,
            "status": "active",
            "active": true,
            "rfc2317_prefix": null
        },
        "display": "4.0.1.10.in-addr.arpa [PTR]",
        "type": "PTR",
        "name": "4",
        "fqdn": "4.0.1.10.in-addr.arpa.",
        "value": "name4.zone13.example.com.",
        "status": "active",
        "ttl": null,
        "description": "",
        "tags": [],
        "created": "2024-04-23T13:21:48.870269Z",
        "last_updated": "2024-04-23T13:21:48.870285Z",
        "managed": true,
        "disable_ptr": false,
        "ptr_record": null,
        "address_record": null,
        "custom_fields": {},
        "tenant": null,
        "ipam_ip_address": null
    },
    "snapshots": {
        "prechange": null,
        "postchange": {
            "created": "2024-04-23T13:21:48.870Z",
            "last_updated": "2024-04-23T13:21:48.870Z",
            "name": "4",
            "zone": 23,
            "fqdn": "4.0.1.10.in-addr.arpa.",
            "type": "PTR",
            "value": "name4.zone13.example.com.",
            "status": "active",
            "ttl": null,
            "managed": true,
            "ptr_record": null,
            "disable_ptr": false,
            "description": "",
            "tenant": null,
            "ip_address": "10.1.0.4",
            "ipam_ip_address": null,
            "rfc2317_cname_record": null,
            "custom_fields": {},
            "tags": []
        }
    }
}
Completed request #1
------------
[2] Tue, 23 Apr 2024 13:21:49 GMT 127.0.0.1 "POST / HTTP/1.1" 200 -
Host: localhost:9000
Accept-Encoding: identity
User-Agent: python-urllib3/2.1.0
Content-Type: application/json
Content-Length: 1466

{
    "event": "updated",
    "timestamp": "2024-04-23T13:21:49.108383+00:00",
    "model": "record",
    "username": "admin",
    "request_id": "f6b0a463-e2e7-4504-9153-8ee0ae97585a",
    "data": {
        "id": 202,
        "url": "/api/plugins/netbox-dns/records/202/",
        "zone": {
            "id": 23,
            "url": "/api/plugins/netbox-dns/zones/23/",
            "display": "0.1.10.in-addr.arpa",
            "name": "0.1.10.in-addr.arpa",
            "view": null,
            "status": "active",
            "active": true,
            "rfc2317_prefix": null
        },
        "display": "0.1.10.in-addr.arpa [SOA]",
        "type": "SOA",
        "name": "@",
        "fqdn": "0.1.10.in-addr.arpa.",
        "value": "ns1.example.com. hostmaster.example.com. 1713878509 43200 7200 2419200 3600",
        "status": "active",
        "ttl": 86400,
        "description": "",
        "tags": [],
        "created": "2024-03-04T13:10:24.965377Z",
        "last_updated": "2024-04-23T13:21:48.945951Z",
        "managed": true,
        "disable_ptr": false,
        "ptr_record": null,
        "address_record": null,
        "active": true,
        "custom_fields": {},
        "tenant": null,
        "ipam_ip_address": null
    },
    "snapshots": {
        "prechange": null,
        "postchange": {
            "created": "2024-03-04T13:10:24.965Z",
            "last_updated": "2024-04-23T13:21:48.945Z",
            "name": "@",
            "zone": 23,
            "fqdn": "0.1.10.in-addr.arpa.",
            "type": "SOA",
            "value": "ns1.example.com. hostmaster.example.com. 1713878509 43200 7200 2419200 3600",
            "status": "active",
            "ttl": 86400,
            "managed": true,
            "ptr_record": null,
            "disable_ptr": false,
            "description": "",
            "tenant": null,
            "ip_address": null,
            "ipam_ip_address": null,
            "rfc2317_cname_record": null,
            "custom_fields": {},
            "tags": []
        }
    }
}
Completed request #2
------------
[3] Tue, 23 Apr 2024 13:21:49 GMT 127.0.0.1 "POST / HTTP/1.1" 200 -
Host: localhost:9000
Accept-Encoding: identity
User-Agent: python-urllib3/2.1.0
Content-Type: application/json
Content-Length: 1851

{
    "event": "created",
    "timestamp": "2024-04-23T13:21:49.112351+00:00",
    "model": "record",
    "username": "admin",
    "request_id": "f6b0a463-e2e7-4504-9153-8ee0ae97585a",
    "data": {
        "id": 219,
        "url": "/api/plugins/netbox-dns/records/219/",
        "zone": {
            "id": 12,
            "url": "/api/plugins/netbox-dns/zones/12/",
            "display": "zone13.example.com",
            "name": "zone13.example.com",
            "view": null,
            "status": "active",
            "active": true,
            "rfc2317_prefix": null
        },
        "display": "name4.zone13.example.com [A]",
        "type": "A",
        "name": "name4",
        "fqdn": "name4.zone13.example.com.",
        "value": "10.1.0.4",
        "status": "active",
        "ttl": null,
        "description": "",
        "tags": [],
        "created": "2024-04-23T13:21:48.967134Z",
        "last_updated": "2024-04-23T13:21:48.967145Z",
        "managed": true,
        "disable_ptr": false,
        "ptr_record": {
            "id": 218,
            "url": "/api/plugins/netbox-dns/records/218/",
            "display": "4.0.1.10.in-addr.arpa [PTR]",
            "type": "PTR",
            "name": "4",
            "value": "name4.zone13.example.com.",
            "status": "active",
            "ttl": null,
            "zone": {
                "id": 23,
                "url": "/api/plugins/netbox-dns/zones/23/",
                "display": "0.1.10.in-addr.arpa",
                "name": "0.1.10.in-addr.arpa",
                "view": null,
                "status": "active",
                "active": true,
                "rfc2317_prefix": null
            },
            "active": null
        },
        "address_record": null,
        "custom_fields": {},
        "tenant": null,
        "ipam_ip_address": {
            "id": 5,
            "url": "/api/ipam/ip-addresses/5/",
            "display": "10.1.0.4/24",
            "family": 4,
            "address": "10.1.0.4/24"
        }
    },
    "snapshots": {
        "prechange": null,
        "postchange": {
            "created": "2024-04-23T13:21:48.967Z",
            "last_updated": "2024-04-23T13:21:48.967Z",
            "name": "name4",
            "zone": 12,
            "fqdn": "name4.zone13.example.com.",
            "type": "A",
            "value": "10.1.0.4",
            "status": "active",
            "ttl": null,
            "managed": true,
            "ptr_record": 218,
            "disable_ptr": false,
            "description": "",
            "tenant": null,
            "ip_address": "10.1.0.4",
            "ipam_ip_address": 5,
            "rfc2317_cname_record": null,
            "custom_fields": {},
            "tags": []
        }
    }
}
Completed request #3
------------
[4] Tue, 23 Apr 2024 13:21:49 GMT 127.0.0.1 "POST / HTTP/1.1" 200 -
Host: localhost:9000
Accept-Encoding: identity
User-Agent: python-urllib3/2.1.0
Content-Type: application/json
Content-Length: 1463

{
    "event": "updated",
    "timestamp": "2024-04-23T13:21:49.117557+00:00",
    "model": "record",
    "username": "admin",
    "request_id": "f6b0a463-e2e7-4504-9153-8ee0ae97585a",
    "data": {
        "id": 157,
        "url": "/api/plugins/netbox-dns/records/157/",
        "zone": {
            "id": 12,
            "url": "/api/plugins/netbox-dns/zones/12/",
            "display": "zone13.example.com",
            "name": "zone13.example.com",
            "view": null,
            "status": "active",
            "active": true,
            "rfc2317_prefix": null
        },
        "display": "zone13.example.com [SOA]",
        "type": "SOA",
        "name": "@",
        "fqdn": "zone13.example.com.",
        "value": "ns1.example.com. hostmaster.example.com. 1713878509 172800 7200 2592000 3600",
        "status": "active",
        "ttl": 86400,
        "description": "",
        "tags": [],
        "created": "2024-01-31T14:58:26.611787Z",
        "last_updated": "2024-04-23T13:21:49.039533Z",
        "managed": true,
        "disable_ptr": false,
        "ptr_record": null,
        "address_record": null,
        "active": true,
        "custom_fields": {},
        "tenant": null,
        "ipam_ip_address": null
    },
    "snapshots": {
        "prechange": null,
        "postchange": {
            "created": "2024-01-31T14:58:26.611Z",
            "last_updated": "2024-04-23T13:21:49.039Z",
            "name": "@",
            "zone": 12,
            "fqdn": "zone13.example.com.",
            "type": "SOA",
            "value": "ns1.example.com. hostmaster.example.com. 1713878509 172800 7200 2592000 3600",
            "status": "active",
            "ttl": 86400,
            "managed": true,
            "ptr_record": null,
            "disable_ptr": false,
            "description": "",
            "tenant": null,
            "ip_address": null,
            "ipam_ip_address": null,
            "rfc2317_cname_record": null,
            "custom_fields": {},
            "tags": []
        }
    }
}
Completed request #4
------------

Four webhooks were triggered:

  1. Creation of the PTR record
  2. Update of the SOA record for the reverse zone
  3. Creation of the A record
  4. Update of the SOA record for the forward zone

That's pretty much what was to be expected.

If you can confirm this, the issue is not with the event rule, but in a later stage of the processing pipeline - either a problem with the script or with NetBox being unable (or unwilling) to execute it.

@aaronhuff
Copy link
Author

Thanks, I followed your very details steps and was able to see a webhook trigger on my worker node.

Let me continue to investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants