fix: attach the original D-Link config.bin as configuration backup

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-05 12:50:19 +01:00
1 parent 781ed7d17b
commit 63ed535a37
4 files changed
+43 -8

No files matched your search

+3
View File
@@ -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)
+5 -1
View File
@@ -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)
+16 -7
View File
@@ -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.
+19
View File
@@ -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"])