From 8fde13993d906301df3f109334268b80a311fe5e Mon Sep 17 00:00:00 2001 From: Dale Date: Tue, 3 Dec 2024 02:00:56 -0800 Subject: [PATCH 1/6] Added support for showing secrets in connection.show. This is to allow getting getting the secrets, such as a WiFi PSK. --- README.md | 2 +- nmcli/_connection.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a70c022..9b5c2e9 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ nmcli.connection.down(name: str, wait: int = None) -> None Show details for specified connections. ``` -nmcli.connection.show(name: str) -> ConnectionDetails +nmcli.connection.show(name: str, show_secrets: bool = False) -> ConnectionDetails ``` #### nmcli.connection.reload diff --git a/nmcli/_connection.py b/nmcli/_connection.py index fc82a7f..a55a540 100644 --- a/nmcli/_connection.py +++ b/nmcli/_connection.py @@ -88,8 +88,11 @@ def down(self, name: str, wait: int = None) -> None: wait) + ['connection', 'down', name] self._syscmd.nmcli(cmd) - def show(self, name: str) -> ConnectionDetails: - r = self._syscmd.nmcli(['connection', 'show', name]) + def show(self, name: str, show_secrets: bool = False) -> ConnectionDetails: + cmd = ['connection', 'show', name] + if show_secrets: + cmd += ["--show-secrets"] + r = self._syscmd.nmcli(cmd) results = {} for row in r.split('\n'): m = re.search(r'^(\S+):\s*([\S\s]+)\s*', row) From d5fa382f25d38b9082d08a473606339a115d5f58 Mon Sep 17 00:00:00 2001 From: ushiboy Date: Sat, 7 Dec 2024 14:28:16 +0900 Subject: [PATCH 2/6] fix github actions --- .github/workflows/action.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index eee97b4..7f71185 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -1,6 +1,10 @@ name: develop action -on: [push] +on: + push: + branches: [main] + pull_request: + workflow_dispatch: jobs: build: From 1d16a0e4eed9d180f58d0725f9e27a5854a436fb Mon Sep 17 00:00:00 2001 From: ushiboy Date: Sat, 7 Dec 2024 14:47:43 +0900 Subject: [PATCH 3/6] change the interface spec of the show method --- nmcli/_connection.py | 2 +- nmcli/dummy/_connection.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/nmcli/_connection.py b/nmcli/_connection.py index a55a540..93fb45d 100644 --- a/nmcli/_connection.py +++ b/nmcli/_connection.py @@ -31,7 +31,7 @@ def up(self, name: str, wait: int = None) -> None: def down(self, name: str, wait: int = None) -> None: raise NotImplementedError - def show(self, name: str) -> ConnectionDetails: + def show(self, name: str, show_secrets: bool = False) -> ConnectionDetails: raise NotImplementedError def reload(self) -> None: diff --git a/nmcli/dummy/_connection.py b/nmcli/dummy/_connection.py index 68dd8ad..e8e0ab4 100644 --- a/nmcli/dummy/_connection.py +++ b/nmcli/dummy/_connection.py @@ -43,6 +43,7 @@ def __init__(self, self._delete_args: List[Tuple] = [] self._up_args: List[Tuple] = [] self._down_args: List[Tuple] = [] + self._show_args: List[Tuple] = [] self._called_reload = 0 def __call__(self) -> List[Connection]: @@ -74,8 +75,9 @@ def down(self, name: str, wait: int = None) -> None: self._raise_error_if_needed() self._down_args.append((name, wait)) - def show(self, name: str) -> ConnectionDetails: + def show(self, name: str, show_secrets: bool = False) -> ConnectionDetails: self._raise_error_if_needed() + self._show_args.append((name, show_secrets)) if not self._result_show is None: return self._result_show raise ValueError("'result_show' is not properly initialized") From 5227fb2001fade7e18f538e53b862f34d11b02fe Mon Sep 17 00:00:00 2001 From: ushiboy Date: Sat, 7 Dec 2024 14:51:14 +0900 Subject: [PATCH 4/6] add test case --- nmcli/dummy/_connection.py | 4 ++++ tests/dummy/test_connection.py | 5 +++++ tests/test_connection.py | 9 ++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/nmcli/dummy/_connection.py b/nmcli/dummy/_connection.py index e8e0ab4..b6b36bc 100644 --- a/nmcli/dummy/_connection.py +++ b/nmcli/dummy/_connection.py @@ -27,6 +27,10 @@ def up_args(self): def down_args(self): return self._down_args + @property + def show_args(self): + return self._show_args + @property def called_reload(self) -> int: return self._called_reload diff --git a/tests/dummy/test_connection.py b/tests/dummy/test_connection.py index 6dbccdb..9d7bbee 100644 --- a/tests/dummy/test_connection.py +++ b/tests/dummy/test_connection.py @@ -110,8 +110,13 @@ def test_show(): 'key': 'value' } c = DummyConnectionControl(result_show=result_show) + name = 'MyHome' assert c.show(name) == result_show + assert c.show_args[0] == (name, False) + + c.show(name, show_secrets=True) + assert c.show_args[1] == (name, True) def test_show_when_raise_error(): diff --git a/tests/test_connection.py b/tests/test_connection.py index dc4b367..7b0a512 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -112,7 +112,10 @@ def test_show(): buf = f.read() s = DummySystemCommand(buf) connection = ConnectionControl(s) - r = connection.show('Wired connection 1') + name = 'Wired connection 1' + + r = connection.show(name) + assert s.passed_parameters == ['connection', 'show', name] assert len(r.keys()) == 114 assert r['connection.id'] == 'Wired connection 1' assert r['connection.stable-id'] is None @@ -120,6 +123,10 @@ def test_show(): assert r['IP4.ADDRESS[1]'] == '192.168.1.10/24' assert r['DHCP6.OPTION[8]'] == 'requested_dhcp6_name_servers = 1' + connection.show(name, show_secrets=True) + assert s.passed_parameters == [ + 'connection', 'show', name, "--show-secrets"] + def test_reload(): s = DummySystemCommand() From aa1439ae7b6dccf43256e715b9c9ac2b07d303fc Mon Sep 17 00:00:00 2001 From: ushiboy Date: Sat, 7 Dec 2024 14:57:26 +0900 Subject: [PATCH 5/6] adjusted README --- README.md | 111 +++++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 9b5c2e9..5b1eef0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -nmcli -===== +# nmcli nmcli is a python wrapper library for the network-manager cli client. @@ -31,62 +30,61 @@ except Exception as e: ## Dependency -* NetworkManager - * `sudo apt install network-manager` (Debian) -* User who can execute nmcli with sudo with NOPASSWD - * If sudo is not needed (like root user), use `disable_use_sudo` at the beginning of the process. +- NetworkManager + - `sudo apt install network-manager` (Debian) +- User who can execute nmcli with sudo with NOPASSWD + - If sudo is not needed (like root user), use `disable_use_sudo` at the beginning of the process. ## Compatibility table -| Object | Command | Status | -|--------|---------|--------| -| general | | supported | -| general | status | supported | -| general | hostname | supported | -| general | permissions | not supported | -| general | logging | not supported | -| networking | | supported | -| networking | on | supported | -| networking | off | supported | -| networking | connectivity | supported | -| radio | | supported | -| radio | all | supported | -| radio | wifi | supported | -| radio | wwan | supported | -| connection | | supported | -| connection | show | supported | -| connection | up | supported | -| connection | down | supported | -| connection | add | supported | -| connection | modify | supported | -| connection | clone | not supported | -| connection | edit | not supported | -| connection | delete | supported | -| connection | reload | supported | -| connection | load | not supported | -| connection | import | not supported | -| connection | export | not supported | -| device | | supported | -| device | status | supported | -| device | show | supported | -| device | set | not supported | -| device | connect | supported | -| device | reapply | supported | -| device | modify | not supported | -| device | disconnect | supported | -| device | delete | supported | -| device | monitor | not supported | -| device | wifi | supported | -| device | wifi connect | supported | -| device | wifi rescan | supported | -| device | wifi hotspot | supported | -| device | lldp | not supported | -| agent | | not supported | -| agent | secret | not supported | -| agent | polkit | not supported | -| agent | all | not supported | -| monitor | | not supported | - +| Object | Command | Status | +| ---------- | ------------ | ------------- | +| general | | supported | +| general | status | supported | +| general | hostname | supported | +| general | permissions | not supported | +| general | logging | not supported | +| networking | | supported | +| networking | on | supported | +| networking | off | supported | +| networking | connectivity | supported | +| radio | | supported | +| radio | all | supported | +| radio | wifi | supported | +| radio | wwan | supported | +| connection | | supported | +| connection | show | supported | +| connection | up | supported | +| connection | down | supported | +| connection | add | supported | +| connection | modify | supported | +| connection | clone | not supported | +| connection | edit | not supported | +| connection | delete | supported | +| connection | reload | supported | +| connection | load | not supported | +| connection | import | not supported | +| connection | export | not supported | +| device | | supported | +| device | status | supported | +| device | show | supported | +| device | set | not supported | +| device | connect | supported | +| device | reapply | supported | +| device | modify | not supported | +| device | disconnect | supported | +| device | delete | supported | +| device | monitor | not supported | +| device | wifi | supported | +| device | wifi connect | supported | +| device | wifi rescan | supported | +| device | wifi hotspot | supported | +| device | lldp | not supported | +| agent | | not supported | +| agent | secret | not supported | +| agent | polkit | not supported | +| agent | all | not supported | +| monitor | | not supported | ## API @@ -155,6 +153,8 @@ nmcli.connection.down(name: str, wait: int = None) -> None Show details for specified connections. +Use `show_secrets` argument to reveal associated secrets as well. + ``` nmcli.connection.show(name: str, show_secrets: bool = False) -> ConnectionDetails ``` @@ -239,7 +239,6 @@ Delete the software devices. The `wait` argument applies the same effect to the command as the `--wait` option. If it is omitted, the default behavior is followed. - ``` nmcli.device.delete(ifname: str, wait: int = None) -> None ``` From 6fa84c7d47faa4777e87babef9932c75844faa9a Mon Sep 17 00:00:00 2001 From: ushiboy Date: Sat, 7 Dec 2024 15:20:19 +0900 Subject: [PATCH 6/6] version up 1.5.0 --- README.md | 4 ++++ setup.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5b1eef0..2ade102 100644 --- a/README.md +++ b/README.md @@ -458,6 +458,10 @@ nmcli.set_lang(lang: str) -> None ## Change Log +### 1.5.0 + +- Added show_secrets option to `nmcli.connection.show` + ### 1.4.0 - Supported unsupported cases of `DeviceWifi.parse`. diff --git a/setup.py b/setup.py index accf817..7b34b26 100755 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ def run_tests(self): setup( name='nmcli', - version='1.4.0', + version='1.5.0', author='ushiboy', license='MIT', license_files = ('LICENSE.txt',),