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
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:
1 parent
3dc24612f0
commit
248c87009f
3 files changed
+89
-8
No files matched your search
@@ -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