fix(config): import replace and rollback configs through a private temporary file (#45) #46
No files matched your search
@@ -73,6 +73,12 @@ recorded here; their history is the git log.
|
||||
- `set_hostname()` hands the name to `uci` as one shell word (the `uci()` helper of
|
||||
`uci_steps`, #43, NetOrk/netork#2456). It stood between single quotes of its own, so a name
|
||||
with a single quote ended them and the rest ran as root (`x';reboot;'`).
|
||||
- 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).
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user