From 63ed535a37942f26ecceaa18e979730db9df29ef Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 5 Nov 2026 12:50:19 +0100 Subject: [PATCH] fix: attach the original D-Link config.bin as configuration backup Co-Authored-By: Claude Opus 5.5 --- README.md | 3 +++ netbox_sync/core/backups.py | 6 +++++- netbox_sync/core/switches.py | 23 ++++++++++++++++------- tests/test_backups.py | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2f4fb5f..0c677b4 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,9 @@ vSphere and Proxmox VE, HPE/Aruba switches and Windows clients in NetBox. port descriptions, admin state, untagged/tagged VLANs and VLAN names are taken over; the switch is matched to the NetBox device by its hostname and then synchronized like an SSH result (no LLDP neighbors, hence no cables) + The D-Link `config.bin` is attached to the device unchanged (it contains the + switch's secrets as stored by the switch); the Linksys configuration is + attached as text with secrets removed ### Sophos access points (CSV from Sophos Central) diff --git a/netbox_sync/core/backups.py b/netbox_sync/core/backups.py index 874f97f..7e4126c 100644 --- a/netbox_sync/core/backups.py +++ b/netbox_sync/core/backups.py @@ -14,6 +14,7 @@ Supported formats: A plain text ``SNCSR.conf`` is accepted as well. """ +import base64 import io import ipaddress import re @@ -44,7 +45,10 @@ def parse_backup(data, filename=""): data = data.encode("utf-8") dump = _dlink_dump(data) if dump is not None: - return _dlink_snapshot(dump, data) + switch, snapshot = _dlink_snapshot(dump, data) + # The original file is attached 1:1 so it can be restored on the switch. + snapshot["config_file"] = {"name": filename or "config.bin", "data": base64.b64encode(data).decode("ascii")} + return switch, snapshot text = _linksys_text(data) if text is not None: return _linksys_snapshot(text) diff --git a/netbox_sync/core/switches.py b/netbox_sync/core/switches.py index 63ecad0..98be372 100644 --- a/netbox_sync/core/switches.py +++ b/netbox_sync/core/switches.py @@ -1,5 +1,6 @@ """SSH discovery and NetBox synchronization for HPE and Aruba switches.""" +import base64 import hashlib import io import ipaddress @@ -1730,11 +1731,19 @@ def _backup_config(nb, device, snapshot, dry_run, report): A new attachment is only added when the configuration of this device changed since the last backup; the hash in the description identifies it. """ - config = snapshot.get("config_backup") - if not config: - report["notes"].append("Konfigurations-Backup: keine Konfiguration ausgelesen") - return - digest = hashlib.sha256(f"{device.id}\n{config}".encode("utf-8")).hexdigest()[:16] + original = snapshot.get("config_file") + if original: + # Backup file from the switch's web UI (e.g. D-Link config.bin), attached unchanged. + content = base64.b64decode(original["data"]) + extension = Path(original.get("name") or "config.bin").suffix or ".bin" + content_type = "application/octet-stream" + else: + config = snapshot.get("config_backup") + if not config: + report["notes"].append("Konfigurations-Backup: keine Konfiguration ausgelesen") + return + content, extension, content_type = config.encode("utf-8"), ".txt", "text/plain" + digest = hashlib.sha256(f"{device.id}\n".encode("utf-8") + content).hexdigest()[:16] try: app = nb.plugins.netbox_attachments known = [item for item in app.netbox_attachments.filter(q=digest) @@ -1754,13 +1763,13 @@ def _backup_config(nb, device, snapshot, dry_run, report): report["changes"].append(f"Konfigurations-Backup: Anhang „{name}“") if dry_run: return - filename = re.sub(r"[^\w.-]+", "_", f"{device.name}_{stamp}") + ".txt" + filename = re.sub(r"[^\w.-]+", "_", f"{device.name}_{stamp}") + extension try: attachment = app.netbox_attachments.create({ "name": name, "description": f"Running-Config, SHA-256 {digest}", # Older plugin versions keep the parent on the attachment itself. "object_type": "dcim.device", "object_id": device.id, - "file": (filename, io.BytesIO(config.encode("utf-8")), "text/plain"), + "file": (filename, io.BytesIO(content), content_type), }) if hasattr(attachment, "assignments"): # Newer plugin versions assign attachments to objects separately. diff --git a/tests/test_backups.py b/tests/test_backups.py index a8ac46a..4988e02 100644 --- a/tests/test_backups.py +++ b/tests/test_backups.py @@ -130,3 +130,22 @@ def test_linksys_plain_text(): def test_unknown_file(): with pytest.raises(BackupError): parse_backup(b"hello world", "x.bin") + + +def test_dlink_original_file_is_attached(): + import base64 + from types import SimpleNamespace + + from netbox_sync.core.switches import _backup_config + + data = dlink_backup() + _switch, snapshot = parse_backup(data, "config.bin") + created = [] + attachments = SimpleNamespace(filter=lambda **_kw: [], create=lambda payload: created.append(payload) + or SimpleNamespace()) + nb = SimpleNamespace(plugins=SimpleNamespace(netbox_attachments=SimpleNamespace(netbox_attachments=attachments))) + report = {"notes": [], "errors": [], "changes": []} + _backup_config(nb, SimpleNamespace(id=1, name="SW-01"), snapshot, False, report) + filename, stream, content_type = created[0]["file"] + assert filename.endswith(".bin") and content_type == "application/octet-stream" + assert stream.read() == data == base64.b64decode(snapshot["config_file"]["data"])