From c31c7b50b748be5359eaf94a4cc4fc28b2917455 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Mon, 3 Sep 2018 08:11:35 +0200 Subject: [PATCH 1/6] Fix #2395: Modify only when webhooks are enabled This only adds the RQ link when the webhooks setting is enabled. --- netbox/netbox/admin.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/netbox/netbox/admin.py b/netbox/netbox/admin.py index 4c7e0f81b..34faba233 100644 --- a/netbox/netbox/admin.py +++ b/netbox/netbox/admin.py @@ -23,8 +23,9 @@ admin_site.register(User, UserAdmin) admin_site.register(Tag, TagAdmin) # Modify the template to include an RQ link if django_rq is installed (see RQ_SHOW_ADMIN_LINK) -try: - import django_rq - admin_site.index_template = 'django_rq/index.html' -except ImportError: - pass +if settings.WEBHOOKS_ENABLED: + try: + import django_rq + admin_site.index_template = 'django_rq/index.html' + except ImportError: + pass From 6832df4699413f321fa4127a49902d282313df83 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 10 Oct 2018 09:49:35 -0400 Subject: [PATCH 2/6] Fixes #2508: Removed invalid link --- docs/core-functionality/devices.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core-functionality/devices.md b/docs/core-functionality/devices.md index 0d3df016c..06f78cabb 100644 --- a/docs/core-functionality/devices.md +++ b/docs/core-functionality/devices.md @@ -101,7 +101,7 @@ Device bays represent the ability of a device to house child devices. For exampl A platform defines the type of software running on a device or virtual machine. This can be helpful when it is necessary to distinguish between, for instance, different feature sets. Note that two devices of the same type may be assigned different platforms: for example, one Juniper MX240 running Junos 14 and another running Junos 15. -The platform model is also used to indicate which [NAPALM](https://napalm-automation.net/) driver NetBox should use when connecting to a remote device. The name of the driver along with optional parameters are stored with the platform. See the [API documentation](api/napalm-integration.md) for more information on NAPALM integration. +The platform model is also used to indicate which [NAPALM](https://napalm-automation.net/) driver NetBox should use when connecting to a remote device. The name of the driver along with optional parameters are stored with the platform. The assignment of platforms to devices is an optional feature, and may be disregarded if not desired. From df5d105f291182fb57acdd4b8e85de7b2b9cc3da Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 16 Oct 2018 09:42:19 -0400 Subject: [PATCH 3/6] Changelog for #2515 --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57f17a367..61cff2c30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +v2.4.7 (FUTURE) + +## Bug Fixes + +* [#2515](https://github.com/digitalocean/netbox/issues/2515) - Only use django-rq admin tmeplate if webhooks are enabled + +--- + v2.4.6 (2018-10-05) ## Enhancements From 0ae2dfbff3908c77d9125bf9763dc591021dcf49 Mon Sep 17 00:00:00 2001 From: Chris James Date: Tue, 16 Oct 2018 11:36:32 -0500 Subject: [PATCH 4/6] Fix "cusomizable" typo --- docs/core-functionality/devices.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core-functionality/devices.md b/docs/core-functionality/devices.md index 06f78cabb..5ae599c73 100644 --- a/docs/core-functionality/devices.md +++ b/docs/core-functionality/devices.md @@ -58,7 +58,7 @@ A device is said to be full depth if its installation on one rack face prevents ## Device Roles -Devices can be organized by functional roles. These roles are fully cusomizable. For example, you might create roles for core switches, distribution switches, and access switches. +Devices can be organized by functional roles. These roles are fully customizable. For example, you might create roles for core switches, distribution switches, and access switches. --- From 409a9256a188b6c679da7c4d5ccb1d1a042cfb6c Mon Sep 17 00:00:00 2001 From: mmahacek Date: Tue, 16 Oct 2018 10:19:33 -0700 Subject: [PATCH 5/6] Expand Webhook Documentation #2347 (#2524) * #2347 - Expand Webhook Documentation Move "Install Python Packages" section up one header level. Should make Napalm/Webhook sections appear in table of contents for direct linking. * #2347 - Expand Webhook Documentation Add text for installation to link to other documentation sections with instructions. --- docs/additional-features/webhooks.md | 8 ++++++++ docs/installation/2-netbox.md | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/additional-features/webhooks.md b/docs/additional-features/webhooks.md index 0e74640fa..68f342e88 100644 --- a/docs/additional-features/webhooks.md +++ b/docs/additional-features/webhooks.md @@ -4,6 +4,14 @@ A webhook defines an HTTP request that is sent to an external application when c An optional secret key can be configured for each webhook. This will append a `X-Hook-Signature` header to the request, consisting of a HMAC (SHA-512) hex digest of the request body using the secret as the key. This digest can be used by the receiver to authenticate the request's content. +## Installation + +If you are upgrading from a previous version of Netbox and want to enable the webhook feature, please follow the directions listed in the sections below. + +* [Install Redis server and djano-rq package](../installation/2-netbox/#install-python-packages) +* [Modify configuration to enable webhooks](../installation/2-netbox/#webhooks-configuration) +* [Create supervisord program to run the rqworker process](../installation/3-http-daemon/#supervisord-installation) + ## Requests The webhook POST request is structured as so (assuming `application/json` as the Content-Type): diff --git a/docs/installation/2-netbox.md b/docs/installation/2-netbox.md index 8f59adc29..ad4556383 100644 --- a/docs/installation/2-netbox.md +++ b/docs/installation/2-netbox.md @@ -71,7 +71,7 @@ Checking connectivity... done. `# chown -R netbox:netbox /opt/netbox/netbox/media/` -## Install Python Packages +# Install Python Packages Install the required Python packages using pip. (If you encounter any compilation errors during this step, ensure that you've installed all of the system dependencies listed above.) @@ -82,7 +82,7 @@ Install the required Python packages using pip. (If you encounter any compilatio !!! note If you encounter errors while installing the required packages, check that you're running a recent version of pip (v9.0.1 or higher) with the command `pip3 -V`. -### NAPALM Automation (Optional) +## NAPALM Automation (Optional) NetBox supports integration with the [NAPALM automation](https://napalm-automation.net/) library. NAPALM allows NetBox to fetch live data from devices and return it to a requester via its REST API. Installation of NAPALM is optional. To enable it, install the `napalm` package using pip or pip3: @@ -90,7 +90,7 @@ NetBox supports integration with the [NAPALM automation](https://napalm-automati # pip3 install napalm ``` -### Webhooks (Optional) +## Webhooks (Optional) [Webhooks](../data-model/extras/#webhooks) allow NetBox to integrate with external services by pushing out a notification each time a relevant object is created, updated, or deleted. Enabling the webhooks feature requires [Redis](https://redis.io/), a lightweight in-memory database. You may opt to install a Redis sevice locally (see below) or connect to an external one. From 0bb5d229e83def3d2c1c571f1cc5baaabc76c7e0 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 16 Oct 2018 16:41:12 -0400 Subject: [PATCH 6/6] Fixes #2514: Prevent new connections to already connected interfaces --- CHANGELOG.md | 1 + netbox/dcim/api/serializers.py | 59 ++++++++++++++++++++++------------ netbox/dcim/forms.py | 8 ++--- netbox/dcim/models.py | 57 +++++++++++++++++++++----------- netbox/dcim/tests/test_api.py | 10 +++--- 5 files changed, 87 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61cff2c30..eeb2749cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ v2.4.7 (FUTURE) ## Bug Fixes +* [#2514](https://github.com/digitalocean/netbox/issues/2514) - Prevent new connections to already connected interfaces * [#2515](https://github.com/digitalocean/netbox/issues/2515) - Only use django-rq admin tmeplate if webhooks are enabled --- diff --git a/netbox/dcim/api/serializers.py b/netbox/dcim/api/serializers.py index d0634e040..b0a1628de 100644 --- a/netbox/dcim/api/serializers.py +++ b/netbox/dcim/api/serializers.py @@ -472,10 +472,14 @@ class ConsoleServerPortSerializer(TaggitSerializer, ValidatedModelSerializer): class NestedConsoleServerPortSerializer(WritableNestedSerializer): url = serializers.HyperlinkedIdentityField(view_name='dcim-api:consoleserverport-detail') device = NestedDeviceSerializer(read_only=True) + is_connected = serializers.SerializerMethodField(read_only=True) class Meta: model = ConsoleServerPort - fields = ['id', 'url', 'device', 'name'] + fields = ['id', 'url', 'device', 'name', 'is_connected'] + + def get_is_connected(self, obj): + return hasattr(obj, 'connected_console') and obj.connected_console is not None # @@ -495,10 +499,14 @@ class ConsolePortSerializer(TaggitSerializer, ValidatedModelSerializer): class NestedConsolePortSerializer(TaggitSerializer, ValidatedModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='dcim-api:consoleport-detail') device = NestedDeviceSerializer(read_only=True) + is_connected = serializers.SerializerMethodField(read_only=True) class Meta: model = ConsolePort - fields = ['id', 'url', 'device', 'name'] + fields = ['id', 'url', 'device', 'name', 'is_connected'] + + def get_is_connected(self, obj): + return obj.cs_port is not None # @@ -518,10 +526,14 @@ class PowerOutletSerializer(TaggitSerializer, ValidatedModelSerializer): class NestedPowerOutletSerializer(WritableNestedSerializer): url = serializers.HyperlinkedIdentityField(view_name='dcim-api:poweroutlet-detail') device = NestedDeviceSerializer(read_only=True) + is_connected = serializers.SerializerMethodField(read_only=True) class Meta: model = PowerOutlet - fields = ['id', 'url', 'device', 'name'] + fields = ['id', 'url', 'device', 'name', 'is_connected'] + + def get_is_connected(self, obj): + return hasattr(obj, 'connected_port') and obj.connected_port is not None # @@ -541,23 +553,43 @@ class PowerPortSerializer(TaggitSerializer, ValidatedModelSerializer): class NestedPowerPortSerializer(TaggitSerializer, ValidatedModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='dcim-api:powerport-detail') device = NestedDeviceSerializer(read_only=True) + is_connected = serializers.SerializerMethodField(read_only=True) class Meta: model = PowerPort - fields = ['id', 'url', 'device', 'name'] + fields = ['id', 'url', 'device', 'name', 'is_connected'] + + def get_is_connected(self, obj): + return obj.power_outlet is not None # # Interfaces # -class NestedInterfaceSerializer(WritableNestedSerializer): +class IsConnectedMixin(object): + """ + Provide a method for setting is_connected on Interface serializers. + """ + def get_is_connected(self, obj): + """ + Return True if the interface has a connected interface or circuit. + """ + if obj.connection: + return True + if hasattr(obj, 'circuit_termination') and obj.circuit_termination is not None: + return True + return False + + +class NestedInterfaceSerializer(IsConnectedMixin, WritableNestedSerializer): device = NestedDeviceSerializer(read_only=True) url = serializers.HyperlinkedIdentityField(view_name='dcim-api:interface-detail') + is_connected = serializers.SerializerMethodField(read_only=True) class Meta: model = Interface - fields = ['id', 'url', 'device', 'name'] + fields = ['id', 'url', 'device', 'name', 'is_connected'] class InterfaceNestedCircuitSerializer(serializers.ModelSerializer): @@ -587,7 +619,7 @@ class InterfaceVLANSerializer(WritableNestedSerializer): fields = ['id', 'url', 'vid', 'name', 'display_name'] -class InterfaceSerializer(TaggitSerializer, ValidatedModelSerializer): +class InterfaceSerializer(TaggitSerializer, IsConnectedMixin, ValidatedModelSerializer): device = NestedDeviceSerializer() form_factor = ChoiceField(choices=IFACE_FF_CHOICES, required=False) lag = NestedInterfaceSerializer(required=False, allow_null=True) @@ -631,19 +663,6 @@ class InterfaceSerializer(TaggitSerializer, ValidatedModelSerializer): return super(InterfaceSerializer, self).validate(data) - def get_is_connected(self, obj): - """ - Return True if the interface has a connected interface or circuit termination. - """ - if obj.connection: - return True - try: - circuit_termination = obj.circuit_termination - return True - except CircuitTermination.DoesNotExist: - pass - return False - def get_interface_connection(self, obj): if obj.connection: context = { diff --git a/netbox/dcim/forms.py b/netbox/dcim/forms.py index e7fa15f7b..6b6ea1187 100644 --- a/netbox/dcim/forms.py +++ b/netbox/dcim/forms.py @@ -1328,7 +1328,7 @@ class ConsolePortConnectionForm(BootstrapMixin, ChainedFieldsMixin, forms.ModelF label='Port', widget=APISelect( api_url='/api/dcim/console-server-ports/?device_id={{console_server}}', - disabled_indicator='connected_console', + disabled_indicator='is_connected', ) ) @@ -1419,7 +1419,7 @@ class ConsoleServerPortConnectionForm(BootstrapMixin, ChainedFieldsMixin, forms. label='Port', widget=APISelect( api_url='/api/dcim/console-ports/?device_id={{device}}', - disabled_indicator='cs_port' + disabled_indicator='is_connected' ) ) connection_status = forms.BooleanField( @@ -1597,7 +1597,7 @@ class PowerPortConnectionForm(BootstrapMixin, ChainedFieldsMixin, forms.ModelFor label='Outlet', widget=APISelect( api_url='/api/dcim/power-outlets/?device_id={{pdu}}', - disabled_indicator='connected_port' + disabled_indicator='is_connected' ) ) @@ -1688,7 +1688,7 @@ class PowerOutletConnectionForm(BootstrapMixin, ChainedFieldsMixin, forms.Form): label='Port', widget=APISelect( api_url='/api/dcim/power-ports/?device_id={{device}}', - disabled_indicator='power_outlet' + disabled_indicator='is_connected' ) ) connection_status = forms.BooleanField( diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index d8c392838..33885e203 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -2035,25 +2035,44 @@ class InterfaceConnection(models.Model): csv_headers = ['device_a', 'interface_a', 'device_b', 'interface_b', 'connection_status'] def clean(self): - try: - if self.interface_a == self.interface_b: - raise ValidationError({ - 'interface_b': "Cannot connect an interface to itself." - }) - if self.interface_a.form_factor in NONCONNECTABLE_IFACE_TYPES: - raise ValidationError({ - 'interface_a': '{} is not a connectable interface type.'.format( - self.interface_a.get_form_factor_display() - ) - }) - if self.interface_b.form_factor in NONCONNECTABLE_IFACE_TYPES: - raise ValidationError({ - 'interface_b': '{} is not a connectable interface type.'.format( - self.interface_b.get_form_factor_display() - ) - }) - except ObjectDoesNotExist: - pass + + # An interface cannot be connected to itself + if self.interface_a == self.interface_b: + raise ValidationError({ + 'interface_b': "Cannot connect an interface to itself." + }) + + # Only connectable interface types are permitted + if self.interface_a.form_factor in NONCONNECTABLE_IFACE_TYPES: + raise ValidationError({ + 'interface_a': '{} is not a connectable interface type.'.format( + self.interface_a.get_form_factor_display() + ) + }) + if self.interface_b.form_factor in NONCONNECTABLE_IFACE_TYPES: + raise ValidationError({ + 'interface_b': '{} is not a connectable interface type.'.format( + self.interface_b.get_form_factor_display() + ) + }) + + # Prevent the A side of one connection from being the B side of another + interface_a_connections = InterfaceConnection.objects.filter( + Q(interface_a=self.interface_a) | + Q(interface_b=self.interface_a) + ).exclude(pk=self.pk) + if interface_a_connections.exists(): + raise ValidationError({ + 'interface_a': "This interface is already connected." + }) + interface_b_connections = InterfaceConnection.objects.filter( + Q(interface_a=self.interface_b) | + Q(interface_b=self.interface_b) + ).exclude(pk=self.pk) + if interface_b_connections.exists(): + raise ValidationError({ + 'interface_b': "This interface is already connected." + }) def to_csv(self): return ( diff --git a/netbox/dcim/tests/test_api.py b/netbox/dcim/tests/test_api.py index c227179f4..04952a4d4 100644 --- a/netbox/dcim/tests/test_api.py +++ b/netbox/dcim/tests/test_api.py @@ -1955,7 +1955,7 @@ class ConsolePortTest(APITestCase): self.assertEqual( sorted(response.data['results'][0]), - ['device', 'id', 'name', 'url'] + ['device', 'id', 'is_connected', 'name', 'url'] ) def test_create_consoleport(self): @@ -2070,7 +2070,7 @@ class ConsoleServerPortTest(APITestCase): self.assertEqual( sorted(response.data['results'][0]), - ['device', 'id', 'name', 'url'] + ['device', 'id', 'is_connected', 'name', 'url'] ) def test_create_consoleserverport(self): @@ -2181,7 +2181,7 @@ class PowerPortTest(APITestCase): self.assertEqual( sorted(response.data['results'][0]), - ['device', 'id', 'name', 'url'] + ['device', 'id', 'is_connected', 'name', 'url'] ) def test_create_powerport(self): @@ -2296,7 +2296,7 @@ class PowerOutletTest(APITestCase): self.assertEqual( sorted(response.data['results'][0]), - ['device', 'id', 'name', 'url'] + ['device', 'id', 'is_connected', 'name', 'url'] ) def test_create_poweroutlet(self): @@ -2432,7 +2432,7 @@ class InterfaceTest(APITestCase): self.assertEqual( sorted(response.data['results'][0]), - ['device', 'id', 'name', 'url'] + ['device', 'id', 'is_connected', 'name', 'url'] ) def test_create_interface(self):