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

+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