Skip to content

Commit

Permalink
Address logic complexity in logic.py
Browse files Browse the repository at this point in the history
- Extract remaining database related functions into respective repositories with unit test backing
- Extract Python and NMap importers out of logic.py and into `importers` directory under `app` module
- Optimize test fixtures for less repetition in test setup
- Enhancing functions to follow camel case naming convention
  • Loading branch information
ddubson committed Sep 16, 2019
1 parent b9d9612 commit 429abcf
Show file tree
Hide file tree
Showing 27 changed files with 1,252 additions and 965 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -124,3 +124,5 @@ scripts/CloudFail
docker/runLocal.sh
docker/cleanupUntagged.sh
docker/cleanupExited.sh

log/*.log
317 changes: 317 additions & 0 deletions app/importers/NmapImporter.py

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions app/importers/PythonImporter.py
@@ -0,0 +1,72 @@
"""
LEGION (https://govanguard.io)
Copyright (c) 2018 GoVanguard
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
Author(s): Dmitriy Dubson (d.dubson@gmail.com)
"""
from PyQt5 import QtCore

from db.database import hostObj
from scripts.python import pyShodan
from ui.ancillaryDialog import ProgressWidget, time


class PythonImporter(QtCore.QThread):
tick = QtCore.pyqtSignal(int, name="changed") # New style signal
done = QtCore.pyqtSignal(name="done") # New style signal
schedule = QtCore.pyqtSignal(object, bool, name="schedule") # New style signal
log = QtCore.pyqtSignal(str, name="log")

def __init__(self):
QtCore.QThread.__init__(self, parent=None)
self.output = ''
self.hostIp = ''
self.pythonScriptDispatch = {'pyShodan': pyShodan.PyShodanScript()}
self.pythonScriptObj = None
self.importProgressWidget = ProgressWidget('Importing shodan data..')

def tsLog(self, msg):
self.log.emit(str(msg))

def setDB(self, db):
self.db = db

def setHostIp(self, hostIp):
self.hostIp = hostIp

def setPythonScript(self, pythonScript):
self.pythonScriptObj = self.pythonScriptDispatch[pythonScript]

def setOutput(self, output):
self.output = output

def run(self): # it is necessary to get the qprocess because we need to send it back to the scheduler when we're done importing
try:
session = self.db.session()
startTime = time()
self.db.dbsemaphore.acquire() # ensure that while this thread is running, no one else can write to the DB
#self.setPythonScript(self.pythonScript)
db_host = session.query(hostObj).filter_by(ip = self.hostIp).first()
self.pythonScriptObj.setDbHost(db_host)
self.pythonScriptObj.setSession(session)
self.pythonScriptObj.run()
session.commit()
self.db.dbsemaphore.release() # we are done with the DB
self.tsLog('Finished in ' + str(time() - startTime) + ' seconds.')
self.done.emit()

except Exception as e:
self.tsLog(e)
raise
self.done.emit()
17 changes: 17 additions & 0 deletions app/importers/__init__.py
@@ -0,0 +1,17 @@
"""
LEGION (https://govanguard.io)
Copyright (c) 2018 GoVanguard
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
Author(s): Dmitriy Dubson (d.dubson@gmail.com)
"""

0 comments on commit 429abcf

Please sign in to comment.