Skip to content

Commit

Permalink
Setup GitHub Actions (#7)
Browse files Browse the repository at this point in the history
* ci: utilizes GitHub Actions

Uses GitHub actions for Pylint, CodeQL analysis, and automated PyPI uploads
  • Loading branch information
jaredhendrickson13 committed Aug 14, 2022
1 parent 8b63571 commit dd8955a
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: jaredhendrickson13
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "pip" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
reviewers:
- "jaredhendrickson13"
72 changes: 72 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
schedule:
- cron: '37 5 * * 4'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
23 changes: 23 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Pylint

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
39 changes: 39 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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: PyPI

on:
release:
types: [published]

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: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
42 changes: 42 additions & 0 deletions .github/workflows/test-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build*
*__pycache__*
*pfsense_vshell.egg*
dist*
venv*
venv*
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ persistent=yes

# Minimum Python version to use for version dependent checks. Will default to
# the version used to run pylint.
py-version=3.5
py-version=3.10

# Discover python modules and packages in the file system subtree.
recursive=no
Expand Down Expand Up @@ -335,7 +335,7 @@ indent-after-paren=4
indent-string=' '

# Maximum number of characters on a single line.
max-line-length=128
max-line-length=120

# Maximum number of lines in a module.
max-module-lines=1000
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![PyPI](https://github.com/jaredhendrickson13/pfsense-vshell/actions/workflows/pypi.yml/badge.svg)](https://github.com/jaredhendrickson13/pfsense-vshell/actions/workflows/pypi.yml)
[![PyLint](https://github.com/jaredhendrickson13/pfsense-vshell/actions/workflows/pylint.yml/badge.svg)](https://github.com/jaredhendrickson13/pfsense-vshell/actions/workflows/pylint.yml)
[![CodeQL](https://github.com/jaredhendrickson13/pfsense-vshell/actions/workflows/codeql.yml/badge.svg)](https://github.com/jaredhendrickson13/pfsense-vshell/actions/workflows/codeql.yml)

# Introduction
pfSense vShell is a command line tool and Python module that enables users to remotely enter shell commands on a pfSense
host without enabling `sshd`. This allows administrators to automate installation of packages, enable `sshd`, and make other backend
Expand Down
38 changes: 20 additions & 18 deletions pfsense_vshell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
# 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.
"""Defines the client object used to establish virtual pfSense shell connections."""
__version__ = "2.0.4"


# IMPORT MODULES #
import datetime
import html

import requests
import urllib3


class PFClient:
"""Client object that facilitates controlling the virtual shell."""
# Allow current number of instance attributes, they are needed to allow configurable connections
# pylint: disable=too-many-instance-attributes

def __init__(self, host, username, password, port=443, scheme="https", timeout=30, verify=True):
"""
Expand All @@ -34,6 +37,8 @@ def __init__(self, host, username, password, port=443, scheme="https", timeout=3
:param timeout: (int) the timeout value in seconds for HTTP requests. Defaults to 30.
:param verify: (bool) true to enable certificate verification, false to disable. Defaults to true.
"""
# Allow current number of arguments, it does not affect readability
# pylint: disable=too-many-arguments

# Set properties using parameters
self.session = requests.Session()
Expand All @@ -57,7 +62,7 @@ def version():
Provides the current version of pfsense vShell
:return: (string) the current pfSense vShell version
"""
return "2.0.3"
return __version__

def url(self):
"""
Expand Down Expand Up @@ -146,14 +151,16 @@ def authenticate(self):
if "username or Password incorrect" not in req.text and "class=\"fa fa-sign-out\"" in req.text:
self.__log__("authenticate", "success")
return True
elif "<p>One moment while the initial setup wizard starts." in req.text:
# Support first time logings where wizard is triggered
if "<p>One moment while the initial setup wizard starts." in req.text:
self.__log__("authenticate", "success")
return True
else:
self.__log__("authenticate", "failed")
return False
else:
return True
# Otherwise, assume authentication failed
self.__log__("authenticate", "failed")
return False

# Don't re-authenticate if we're already authenticated
return True

def get_csrf_token(self, uri):
"""
Expand Down Expand Up @@ -183,7 +190,7 @@ def has_dns_rebind_error(self, req=None):
"""
# Make a preliminary request to check if a DNS Rebind error was detected by pfSense.
resp = req.text if req else self.request("/").text
return True if "Potential DNS Rebind attack detected" in resp else False
return "Potential DNS Rebind attack detected" in resp

def is_host_pfsense(self, req=None):
"""
Expand All @@ -208,7 +215,7 @@ def is_host_pfsense(self, req=None):
for item in check_items:
platform_confidence = platform_confidence + 10 if item in resp else platform_confidence

return True if platform_confidence > 50 else False
return platform_confidence > 50

def __has_host_errors__(self):
"""
Expand Down Expand Up @@ -259,10 +266,5 @@ def __log__(self, event, msg):
self.log.append(",".join([str(datetime.datetime.utcnow()), self.url(), self.username, event, msg]))


class PFError(Exception):
"""
Error object used by the PFVShell class
"""
def __init__(self, code, message):
self.code = code
self.message = message
class PFError(BaseException):
"""Error object used by the PFVShell class"""
1 change: 1 addition & 0 deletions scripts/pfsense-vshell
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import argparse
import sys
import time
import pfsense_vshell
import pkg_resources


class PFCLI:
Expand Down
Loading

0 comments on commit dd8955a

Please sign in to comment.