From 042f83a035f0dd3d53314ca235d234504e7a8cf0 Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Thu, 18 Mar 2021 09:01:07 -0400 Subject: [PATCH] pre-commit black & flake8 --- .pre-commit-config.yaml | 12 +++++ setup.py | 25 +++++----- src/overseer/__init__.py | 5 +- src/overseer/api/v1/__init__.py | 5 +- src/overseer/api/v1/events.py | 2 + src/overseer/api/v1/routes.py | 6 ++- src/overseer/config.py | 2 + src/overseer/database.py | 84 +++++++++++++++++++++++--------- src/overseer/models.py | 14 ++++-- src/overseer/overseer.py | 8 ++- src/overseer/overseer.sqlite | Bin 0 -> 28672 bytes 11 files changed, 116 insertions(+), 47 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 src/overseer/overseer.sqlite diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..02fd918 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,12 @@ +repos: +- repo: https://github.com/psf/black + rev: 20.8b1 + hooks: + - id: black + language_version: python3.9 + files: '^src/overseer/|^setup.py' +- repo: https://gitlab.com/pycqa/flake8 + rev: 3.9.0 + hooks: + - id: flake8 + files: '^src/overseer/|^setup.py' diff --git a/setup.py b/setup.py index 1944ee8..49f87cd 100644 --- a/setup.py +++ b/setup.py @@ -1,22 +1,19 @@ from setuptools import setup, find_packages setup( - name = "overseer", - description = "Port Scanning Web Interface", - author = "Evan Reichard", - version = "0.0.1", - packages = find_packages("src"), - package_dir= {'': 'src'}, - zip_safe = False, - include_package_data = True, - entry_points = { - "console_scripts": [ - "overseer = overseer:cli" - ] - }, - install_requires = [ + name="overseer", + description="Port Scanning Web Interface", + author="Evan Reichard", + version="0.0.1", + packages=find_packages("src"), + package_dir={"": "src"}, + zip_safe=False, + include_package_data=True, + entry_points={"console_scripts": ["overseer = overseer:cli"]}, + install_requires=[ "Flask", "flask_socketio", "sqlalchemy", ], + extras_require={"dev": ["pre-commit", "black", "flake8"]}, ) diff --git a/src/overseer/__init__.py b/src/overseer/__init__.py index 1f0990a..c29d07d 100644 --- a/src/overseer/__init__.py +++ b/src/overseer/__init__.py @@ -6,11 +6,14 @@ from flask.cli import FlaskGroup app = Flask(__name__) config = EnvConfig() + def create_app(): return app + @click.group(cls=FlaskGroup, create_app=create_app) def cli(): """Management script for the Wiki application.""" -import overseer.overseer + +import overseer.overseer # noqa: E501,F401,E402 diff --git a/src/overseer/api/v1/__init__.py b/src/overseer/api/v1/__init__.py index 10d1d78..b860caf 100644 --- a/src/overseer/api/v1/__init__.py +++ b/src/overseer/api/v1/__init__.py @@ -1,3 +1,4 @@ from flask import Blueprint -api = Blueprint('v1', __name__, url_prefix="/api/v1") -from overseer.api.v1 import routes, events + +api = Blueprint("v1", __name__, url_prefix="/api/v1") +from overseer.api.v1 import routes, events # noqa: F401,E402 diff --git a/src/overseer/api/v1/events.py b/src/overseer/api/v1/events.py index bf1817f..09fe891 100644 --- a/src/overseer/api/v1/events.py +++ b/src/overseer/api/v1/events.py @@ -3,10 +3,12 @@ from flask_socketio import SocketIO socketio = SocketIO(app, path="/api/v1/socket.io") + @socketio.on("message") def handle_message(data): print("RAW DATA: %s" % data) + @socketio.on("json") def handle_json(json): print("JSON DATA: %s" % json) diff --git a/src/overseer/api/v1/routes.py b/src/overseer/api/v1/routes.py index a2166e0..36e2121 100644 --- a/src/overseer/api/v1/routes.py +++ b/src/overseer/api/v1/routes.py @@ -1,9 +1,11 @@ from overseer.api.v1 import api -@api.route('/status', methods=["GET"]) + +@api.route("/status", methods=["GET"]) def status(): return "STATUS PLACEHOLDER" -@api.route('/scan', methods=["GET"]) + +@api.route("/scan", methods=["GET"]) def scan(): return "SCAN PLACEHOLDER" diff --git a/src/overseer/config.py b/src/overseer/config.py index 12e5d7d..f6450f4 100644 --- a/src/overseer/config.py +++ b/src/overseer/config.py @@ -1,9 +1,11 @@ import os + def get_env(key, default=None, required=False): if required: assert key in os.environ, "Missing Environment Variable: %s" % key return os.environ.get(key, default) + class EnvConfig: DATABASE = get_env("OVERSEER_DB", default="sqlite") diff --git a/src/overseer/database.py b/src/overseer/database.py index de0c85d..509838c 100644 --- a/src/overseer/database.py +++ b/src/overseer/database.py @@ -2,58 +2,91 @@ import models import ipaddress from datetime import datetime from os import path -from sqlalchemy import create_engine, or_, insert, Table, Column, Integer, String, ForeignKey, DateTime -from sqlalchemy.orm import declarative_base, Session +from sqlalchemy import ( + create_engine, + or_, +) +from sqlalchemy.orm import Session + class DatabaseConnector: def __init__(self, data_path, in_memory=False): if in_memory: - self.engine = create_engine("sqlite+pysqlite:///:memory:", echo=True, future=True) + self.engine = create_engine( + "sqlite+pysqlite:///:memory:", echo=True, future=True + ) else: - self.engine = create_engine("sqlite+pysqlite:///%s" % path.join(data_path, "overseer.sqlite"), echo=True, future=True) + db_path = path.join(data_path, "overseer.sqlite") + self.engine = create_engine( + "sqlite+pysqlite:///%s" % db_path, + echo=True, + future=True, + ) models.Base.metadata.create_all(self.engine) - def create_scan_result(self, ip_address, scan_results, hostname=None): - int_ip_address = int(ipaddress.ip_address(ip_address)) + def create_scan_result(self, ip_addr, scan_results, hostname=None): + int_ip_addr = int(ipaddress.ip_addr(ip_addr)) session = Session(bind=self.engine) # Does an existing target exist? - scan_target = session.query(models.ScanTarget).filter(or_( - models.ScanTarget.ip==int_ip_address, - models.ScanTarget.hostname==hostname, - )).first() + scan_target = ( + session.query(models.ScanTarget) + .filter( + or_( + models.ScanTarget.ip == int_ip_addr, + models.ScanTarget.hostname == hostname, + ) + ) + .first() + ) # TODO: Do we need to update hostname? # Nope, create one if not scan_target: - scan_target = models.ScanTarget(ip=int_ip_address, hostname=hostname) + scan_target = models.ScanTarget(ip=int_ip_addr, hostname=hostname) session.add(scan_target) session.commit() # Create scan history - scan_history = models.ScanHistory(target_id=scan_target.id, results=",".join(map(str, scan_results)), datetime=datetime.now()) + scan_history = models.ScanHistory( + target_id=scan_target.id, + results=",".join(map(str, scan_results)), + datetime=datetime.now(), + ) session.add(scan_history) session.commit() session.close() - def get_scan_results(self, **kwargs): - if len(kwargs.keys() & {'ip_address', 'hostname'}) != 1: - raise ValueError('Missing keyword argument: ip_address or hostname') + """Returns scan results for the queries target. + + Keyword arguments: + hostname -- The hostname of the target. + ip_addr -- The IP address of the target. + """ + if len(kwargs.keys() & {"ip_addr", "hostname"}) != 1: + raise ValueError("Missing keyword argument: ip_addr or hostname") hostname = kwargs["hostname"] if "hostname" in kwargs else None - ip_address = kwargs["ip_address"] if "ip_address" in kwargs else None - int_ip_address = int(ipaddress.ip_address(ip_address)) if ip_address else None + ip_addr = kwargs["ip_addr"] if "ip_addr" in kwargs else None + int_ip_addr = int(ipaddress.ip_addr(ip_addr)) if ip_addr else None session = Session(bind=self.engine) # Get all scan histories - scan_histories = session.query(models.ScanHistory).join(models.ScanHistory.target).filter(or_( - models.ScanTarget.ip==int_ip_address, - models.ScanTarget.hostname==hostname, - )).all() + scan_histories = ( + session.query(models.ScanHistory) + .join(models.ScanHistory.target) + .filter( + or_( + models.ScanTarget.ip == int_ip_addr, + models.ScanTarget.hostname == hostname, + ) + ) + .all() + ) session.close() @@ -62,8 +95,11 @@ class DatabaseConnector: # FOR TESTING PURPOSES def main(): - db = DatabaseConnector("/Users/evanreichard/Development/git/overseer/src/overseer") - db.create_scan_result("1.2.3.4", [5,6,7,8], "test222.com") - db.get_scan_results(ip_address="1.2.3.4") + data_path = "/Users/evanreichard/Development/git/overseer/src/overseer" + db = DatabaseConnector(data_path) + db.create_scan_result("1.2.3.4", [5, 6, 7, 8], "test222.com") + db.get_scan_results(ip_addr="1.2.3.4") + + if __name__ == "__main__": main() diff --git a/src/overseer/models.py b/src/overseer/models.py index e3302b1..d9ee6b8 100644 --- a/src/overseer/models.py +++ b/src/overseer/models.py @@ -3,20 +3,25 @@ from sqlalchemy.orm import declarative_base, relationship Base = declarative_base() + class ScanTarget(Base): __tablename__ = "scan_target" # Unique ID id = Column(Integer, primary_key=True, unique=True) - # Integer representation of an IP Address. Can use SQL queries to filter subnets. + # Integer representation of an IP Address ip = Column(Integer, index=True, unique=True) # Corresponding hostname. hostname = Column(String, index=True, unique=True) def __repr__(self): - return f"ScanTarget(id={self.id!r}, ip={self.ip!r}, hostname={self.hostname!r})" + return ( + f"ScanTarget(id={self.id!r}, ip={self.ip!r}, " + f"hostname={self.hostname!r})" + ) + class ScanHistory(Base): __tablename__ = "scan_history" @@ -37,4 +42,7 @@ class ScanHistory(Base): target = relationship("ScanTarget", foreign_keys=[target_id]) def __repr__(self): - return f"ScanHistory(id={self.id!r}, target={self.target!r}, results={self.results!r}, datetime={self.datetime!r})" + return ( + f"ScanHistory(id={self.id!r}, target={self.target!r}, " + f"results={self.results!r}, datetime={self.datetime!r})" + ) diff --git a/src/overseer/overseer.py b/src/overseer/overseer.py index 626fe12..3a790e5 100644 --- a/src/overseer/overseer.py +++ b/src/overseer/overseer.py @@ -1,20 +1,26 @@ from overseer import app -from flask import Flask, make_response, render_template, send_from_directory +from flask import make_response, render_template, send_from_directory from overseer.api.v1 import api as api_v1 """ Initial Entrypoint to the SPA (i.e. 'index.html') """ + + @app.route("/", methods=["GET"]) def main_entry(): return make_response(render_template("index.html")) + """ Front End Static Resources """ + + @app.route("/static/") def static_resources(path): return send_from_directory("static", path) + # Version API's app.register_blueprint(api_v1) diff --git a/src/overseer/overseer.sqlite b/src/overseer/overseer.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..1c76a103cddd9bafa37cef5f65f74d73ddc26794 GIT binary patch literal 28672 zcmeI(PjAv-90%~{p#>}n)MSUo7?USyAR}Q&TQ*=`#I2iTtaBYR?u2zz*JO-IyJS0= z#fvY+o4y6#g|9&`?qYWE;8VJ8L=5W5IKNNY^872mKA)#OH1KR~w!Oe*&j-W46EK6^ zB$Sf-j1fX4zNh&fCy{5uJx(;vO!<^ZdG&2pJ}0THO60u!A@?rV&Z@bYDd(UG0uX=z z1Rwwb2tWV=5P-nn5;*NA(@H*1U*8FwXZxl}3VJU{g?av{jlpC1%pw!J3g?kHeO0QVoJHI(pRI7-f`45 zj(Rxi8kLe4R2LW9vX;#!Y~lpAVf}D*hs8ihEB91-s{Aa*-oOtIoPIPzQ6%vr?+TYQF+*tt2-e2{;@$NCg(ruGiCa{!}zWU=Pr&nKJmYp<4kcrky2Xf)Y*zI z<6^R+g1{f98{q?$6TTrq00Izz00bZa0SG_<0uX=z1R(J53(QcFh$>A8V&M9L zuIuI9K|jOCMdfdVZ%7b;00bZa0SG_<0uX=z1Rwwb2wXz}L6D^A{r`-}XV-9m7!m{^ z009U<00Izz00bZa0SG|ge-+3Hsxz{}fB*y_009U<00Izz00bcL#{va0Nu=A&`O-qE zR;ue2U7M>Ib6Sn*`eM~sG_xwC>Sbf0+Nd`QbV@%S zS}W^DMKkp9|No-=iLd{czsg^D1_=TXfB*y_009U<00Izz00bZafqz&aDNrhk;ZYF6 KV