From 5bdd48d956b31bdf1bc427e047d5d86387d1fd95 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 13:59:00 -0700 Subject: [PATCH 01/11] fix: revert PFError class to v2.0.3 equivalent This fixes a regressions that resulted in any error encountered by the package to result in a traceback from the CLI. --- pfsense_vshell/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pfsense_vshell/__init__.py b/pfsense_vshell/__init__.py index 14a7c61..3b21412 100644 --- a/pfsense_vshell/__init__.py +++ b/pfsense_vshell/__init__.py @@ -266,5 +266,10 @@ def __log__(self, event, msg): self.log.append(",".join([str(datetime.datetime.utcnow()), self.url(), self.username, event, msg])) -class PFError(BaseException): - """Error object used by the PFVShell class""" +class PFError(Exception): + """ + Error object used by the PFVShell class + """ + def __init__(self, code, message): + self.code = code + self.message = message From 6bf74871a059dae3fe2f09cbdc6420e27d365f21 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 14:15:42 -0700 Subject: [PATCH 02/11] lint: fixed linting errors --- .pylintrc | 3 ++- pfsense_vshell/__init__.py | 3 +++ scripts/pfsense-vshell | 9 ++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.pylintrc b/.pylintrc index 1ce0a0b..b78afa4 100644 --- a/.pylintrc +++ b/.pylintrc @@ -151,7 +151,8 @@ disable=raw-checker-failed, suppressed-message, useless-suppression, deprecated-pragma, - use-symbolic-message-instead + use-symbolic-message-instead, + invalid-name # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/pfsense_vshell/__init__.py b/pfsense_vshell/__init__.py index 3b21412..7388e2c 100644 --- a/pfsense_vshell/__init__.py +++ b/pfsense_vshell/__init__.py @@ -270,6 +270,9 @@ class PFError(Exception): """ Error object used by the PFVShell class """ + code = 1 + message = "an unknown error has occurred" + def __init__(self, code, message): self.code = code self.message = message diff --git a/scripts/pfsense-vshell b/scripts/pfsense-vshell index bfc32a4..3929427 100644 --- a/scripts/pfsense-vshell +++ b/scripts/pfsense-vshell @@ -12,16 +12,17 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +"""The command line interface for pfsense-vshell.""" # IMPORT MODULES # import argparse import sys import time import pfsense_vshell -import pkg_resources class PFCLI: + """Class containing all methods necessary for CLI functionality.""" def __init__(self): """ Initializes the object at creation by converting arguments intto our PFClient object and running our CLI @@ -165,6 +166,8 @@ class PFCLI: Sets criteria for arguments and parses them into our args property :return: (none) args property will be populated after running """ + # Expressions can be better understood in this context as 'False if expression else True' + # pylint: disable=simplifiable-if-expression def port(value_string): """ @@ -176,7 +179,7 @@ class PFCLI: value = int(value_string) if value not in range(1, 65535): - raise argparse.ArgumentTypeError("%s is out of range, choose from [1-65535]" % value) + raise argparse.ArgumentTypeError(f"{value} is out of range, choose from [1-65535]") return value def timeout(value_string): @@ -189,7 +192,7 @@ class PFCLI: value = int(value_string) if value not in range(0, 120): - raise argparse.ArgumentTypeError("%s is out of range, choose from [0-120]" % value) + raise argparse.ArgumentTypeError(f"{value} is out of range, choose from [0-120]") return value # Setup parser and define arguments From 2dda8921b307cc1a2ddd8343ffcbe9a05d01b4c4 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 14:16:37 -0700 Subject: [PATCH 03/11] ci: lint cli script --- .github/workflows/pylint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index bbc0406..776e466 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -20,4 +20,4 @@ jobs: pip install . - name: Analysing the code with pylint run: | - pylint $(git ls-files '*.py') \ No newline at end of file + pylint $(git ls-files '*.py') scripts/pfsense-vshell \ No newline at end of file From 4eb9af1cf16326b3ee194590a00148dea685d623 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 16:34:21 -0700 Subject: [PATCH 04/11] feat: allow interactive password entry --- scripts/pfsense-vshell | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/pfsense-vshell b/scripts/pfsense-vshell index 3929427..ad32f1d 100644 --- a/scripts/pfsense-vshell +++ b/scripts/pfsense-vshell @@ -18,6 +18,7 @@ import argparse import sys import time +import getpass import pfsense_vshell @@ -225,7 +226,8 @@ class PFCLI: parser.add_argument( '--password', "-p", dest="password", - required=False if "--version" in sys.argv or "-V" in sys.argv else True, + required=False, + default=None, help='Set the password to use when authenticating', ) parser.add_argument( @@ -275,8 +277,13 @@ class PFCLI: help="Print verbose data" ) + # Parse the arguments self.args = parser.parse_args() + # Prompt for a password if one was not specified and the version flag was not used + if self.args.password is None and not self.args.version: + self.args.password = getpass.getpass(f"Enter pfSense password for '{self.args.username}': ") + # RUNTIME # Run the CLI, allow user to trigger KeyboardInterrupt (ctl + c) or EOFError (ctl + d) to safely exit the script From e3ccbdb28479dcadd16d95f268bf63fd484059b7 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 16:34:45 -0700 Subject: [PATCH 05/11] chore: bump version to v2.0.5 --- pfsense_vshell/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pfsense_vshell/__init__.py b/pfsense_vshell/__init__.py index 7388e2c..cb5e587 100644 --- a/pfsense_vshell/__init__.py +++ b/pfsense_vshell/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Defines the client object used to establish virtual pfSense shell connections.""" -__version__ = "2.0.4" +__version__ = "2.0.5" import datetime import html From c42ea0062615febe99f6eb1572b53ceb7d2db356 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 16:39:27 -0700 Subject: [PATCH 06/11] docs: remove password from required flags in docs --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 339a940..c742543 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ To uninstall:
## Usage & Syntax ``` -usage: pfsense-vshell [-h] --host HOST [--virtual_shell] [--command COMMAND] --username USERNAME --password PASSWORD [--scheme {http,https}] [--port PORT] [--timeout TIMEOUT] [--shell_timeout SHELL_TIMEOUT] [--no_verify] [--version] [--verbose] +usage: pfsense-vshell [-h] --host HOST [--virtual_shell] [--command COMMAND] --username USERNAME [--password PASSWORD] [--scheme {http,https}] [--port PORT] [--timeout TIMEOUT] [--shell_timeout SHELL_TIMEOUT] [--no_verify] [--version] [--verbose] ``` | Command | Shorthand | Required | Description | Example Usage | @@ -31,7 +31,7 @@ usage: pfsense-vshell [-h] --host HOST [--virtual_shell] [--command COMMAND] --u | --help | -h | No | Display the help page | --help | | --version | -V | No | Display the current version | --version | | --username | -u | Yes (except with --help or --version) | Set the username to login with | --username USERNAME | -| --password | -p | Yes (except with --help or --version) | Set the password to login with | --password PASSWORD | +| --password | -p | No | Set the password to login with. User will be prompted for a password if unspecified. | --password PASSWORD | | --port | -P | No | Set the TCP port of pfSense's webConfigurator | --port PORT | | --scheme | -w | No | Set the HTTP protocol scheme. `http` or `https` | --scheme SCHEME | | --no_verify | -k | No | Disable SSL certificate verification | --no_verify | From ab5fa4242100930d986eeb6d8f60953a68270214 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 16:52:46 -0700 Subject: [PATCH 07/11] lint: init parent Exception class on PFError --- pfsense_vshell/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pfsense_vshell/__init__.py b/pfsense_vshell/__init__.py index cb5e587..1acd667 100644 --- a/pfsense_vshell/__init__.py +++ b/pfsense_vshell/__init__.py @@ -276,3 +276,4 @@ class PFError(Exception): def __init__(self, code, message): self.code = code self.message = message + super().__init__() From 36c21aa260c0ae2ba71eb04b1295ddf3d73d0169 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 16:57:41 -0700 Subject: [PATCH 08/11] ci: remove test pypi workflow --- .github/workflows/test-pypi.yml | 42 --------------------------------- 1 file changed, 42 deletions(-) delete mode 100644 .github/workflows/test-pypi.yml diff --git a/.github/workflows/test-pypi.yml b/.github/workflows/test-pypi.yml deleted file mode 100644 index ce78baa..0000000 --- a/.github/workflows/test-pypi.yml +++ /dev/null @@ -1,42 +0,0 @@ -# This workflow will upload a Python Package using Twine when a release is created -# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries - -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - -name: Test PyPI - -on: - pull_request: - branches: [ "master" ] - -permissions: - contents: read - -jobs: - deploy: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Set up Python - uses: actions/setup-python@v3 - with: - python-version: '3.x' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install build - - name: Build package - run: | - export __PFSENSE_VSHELL_DEVREVISION__="${GITHUB_RUN_ID}" - python -m build - - name: Publish package - uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 - with: - user: __token__ - password: ${{ secrets.TEST_PYPI_API_TOKEN }} - repository_url: https://test.pypi.org/legacy/ From 160ce397dcf60cfa2863bc2695acf669490c0545 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 17:00:14 -0700 Subject: [PATCH 09/11] build(deps): ease install requirement versions --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 4fcf338..a26872b 100644 --- a/setup.py +++ b/setup.py @@ -66,9 +66,9 @@ def get_readme(): scripts=['scripts/pfsense-vshell'], packages=["pfsense_vshell"], install_requires=[ - "requests~=2.28.1", - "urllib3~=1.26.10", - "pylint~=2.14.5" + "requests>=2.28.1", + "urllib3>=1.26.10", + "pylint>=2.14.5" ], classifiers=[ "Programming Language :: Python :: 3", From bd94bc5f24afbfbf4594e7681a944f754ce70a33 Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 17:02:15 -0700 Subject: [PATCH 10/11] chore: bump version to v2.1.0 --- pfsense_vshell/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pfsense_vshell/__init__.py b/pfsense_vshell/__init__.py index 1acd667..75392cd 100644 --- a/pfsense_vshell/__init__.py +++ b/pfsense_vshell/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Defines the client object used to establish virtual pfSense shell connections.""" -__version__ = "2.0.5" +__version__ = "2.1.0" import datetime import html From 97096799e3e63e332dd758228e53bab4579d534a Mon Sep 17 00:00:00 2001 From: Jared Hendrickson Date: Tue, 24 Jan 2023 17:07:40 -0700 Subject: [PATCH 11/11] tests: fix PFError exception test --- tests/test_vshell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_vshell.py b/tests/test_vshell.py index 7aef83f..3bdbe92 100644 --- a/tests/test_vshell.py +++ b/tests/test_vshell.py @@ -130,7 +130,7 @@ def test_log(self): def test_pferror_class(self): """Tests the PFError exception class.""" with self.assertRaises(pfsense_vshell.PFError): - raise pfsense_vshell.PFError + raise pfsense_vshell.PFError(1, "Test error message") if __name__ == '__main__':