fix(config): import replace and rollback configs through a private temporary file (#45)
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 19s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 51s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 36s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 2m50s
driver / publish (pull_request) Skipped

A replace commit_config() and rollback() wrote the config as root to a
fixed name in /tmp (/tmp/napalm_candidate.uci, /tmp/napalm_rollback.uci)
and imported it with a second command. A local account could place that
file beforehand, or swap it in between, and have its own config imported
and committed (NetOrk/netork#2446).

Each import is now one command: a mktemp file made under umask 077, the
config written to it, `uci import < "$f" && uci commit`, then the file
removed.
This commit is contained in:
Christian Manivong committed 2026-11-12 22:32:46 +01:00
1 parent 3dc24612f0
commit 248c87009f
3 files changed
+89 -8

No files matched your search

+6
View File
@@ -70,6 +70,12 @@ recorded here; their history is the git log.
`manage_service("lldpd", "enable")` and `manage_service("lldpd", "start")` (#8).
### Fixed
- A replace `commit_config()` and `rollback()` import their config through a `mktemp`
file made under umask 077, written, imported and removed in one command (#45,
NetOrk/netork#2446). They wrote it as root to a fixed name in /tmp
(`/tmp/napalm_candidate.uci`, `/tmp/napalm_rollback.uci`) and imported it with a
second command, so a local account could place or swap the file and have its own
config committed.
- `push_mac_acl()` raises `ValueError` for a mode other than `"off"`, `"whitelist"` and
`"blacklist"`, before anything is sent. An unknown mode was written as a deny list
(#39).
+20 -8
View File
@@ -12,6 +12,8 @@
from __future__ import annotations
import shlex
from napalm.base.exceptions import (
CommandErrorException,
MergeConfigException,
@@ -20,6 +22,22 @@ from napalm.base.exceptions import (
from napalm_device_types import ConfigLifecycleMixin
def _uci_import_command(config: str) -> str:
"""One shell command that imports *config* with ``uci import`` and commits it.
The config goes through a ``mktemp`` file made under umask 077 and removed
afterwards, never through a fixed name in /tmp: as root, a fixed name let a
local account place or swap the file and have its own config committed
(#45, NetOrk/netork#2446).
"""
return (
"f=$(umask 077 && mktemp) && { "
f"printf '%s' {shlex.quote(config)} > \"$f\" && "
'uci import < "$f" && uci commit; '
'rm -f "$f"; }'
)
class OpenWrtConfigMixin(ConfigLifecycleMixin):
"""Mixin providing configuration-management NAPALM methods for OpenWrt.
@@ -67,10 +85,7 @@ class OpenWrtConfigMixin(ConfigLifecycleMixin):
errors.append(f" {stripped!r}: {out}")
self._send_command("uci commit")
else:
escaped = self._candidate_config.replace("'", "'\\''")
self._send_command(f"printf '%s' '{escaped}' > /tmp/napalm_candidate.uci")
out = self._send_command("uci import < /tmp/napalm_candidate.uci && uci commit")
self._send_command("rm -f /tmp/napalm_candidate.uci")
out = self._send_command(_uci_import_command(self._candidate_config))
if out and "error" in out.lower():
errors.append(out)
except Exception as exc:
@@ -95,9 +110,6 @@ class OpenWrtConfigMixin(ConfigLifecycleMixin):
"commit_config has not been called in this session."
)
escaped = self._backup_config.replace("'", "'\\''")
self._send_command(f"printf '%s' '{escaped}' > /tmp/napalm_rollback.uci")
self._send_command("uci import < /tmp/napalm_rollback.uci && uci commit")
self._send_command("rm -f /tmp/napalm_rollback.uci")
self._send_command(_uci_import_command(self._backup_config))
self._backup_config = None
+63
View File
@@ -0,0 +1,63 @@
"""A replace commit and a rollback import their config through a private temporary file
(#45, NetOrk/netork#2446).
Both wrote the config as root to a fixed name in /tmp (``/tmp/napalm_candidate.uci``,
``/tmp/napalm_rollback.uci``) and imported it with a second command. A local account
could place that file beforehand, or swap it in between, and have its own config
committed. Each import is now one command: a ``mktemp`` file made under umask 077,
written, imported and removed, named by nothing but ``"$f"``.
"""
from __future__ import annotations
import shlex
from unittest.mock import MagicMock, patch
import pytest
from napalm_openwrt.openwrt import OpenWrtDriver
CONFIG = "package network\n\nconfig interface 'lan'\n\toption proto 'static'\n"
@pytest.fixture
def driver():
with patch("napalm_openwrt.openwrt.ConnectHandler"):
drv = OpenWrtDriver(hostname="192.0.2.1", username="root", password="")
drv._send_command = MagicMock(return_value="")
return drv
def _sent(driver) -> list[str]:
return [call.args[0] for call in driver._send_command.call_args_list]
def _assert_private_import(command: str, config: str) -> None:
assert "/tmp/napalm" not in command
assert command.startswith("f=$(umask 077 && mktemp) && { ")
assert 'uci import < "$f" && uci commit' in command
assert command.rstrip().endswith('rm -f "$f"; }')
# The config reaches printf as one word, exactly as it is.
words = shlex.split(command.split("printf '%s' ", 1)[1].split(' > "$f"', 1)[0])
assert words == [config]
def test_a_replace_commit_imports_through_a_private_temporary_file(driver):
driver.load_replace_candidate(config=CONFIG)
driver.commit_config()
imports = [c for c in _sent(driver) if "uci import" in c]
assert len(imports) == 1
_assert_private_import(imports[0], CONFIG)
assert not any("napalm_candidate" in c for c in _sent(driver))
def test_a_rollback_imports_through_a_private_temporary_file(driver):
backup = "package system\n\nconfig system\n\toption hostname 'it''s'\n"
driver._backup_config = backup
driver.rollback()
[command] = _sent(driver)
_assert_private_import(command, backup)