Skip to content
Snippets Groups Projects
Commit a2b5b722 authored by penenadpi's avatar penenadpi Committed by Anze Luzar
Browse files

Added docker compose and fixed persistence enable/disable scenarios

Fixed formatting
parent 1ee965f9
No related branches found
No related tags found
No related merge requests found
version: '3.9'
services:
mongoservice:
image: mongo:5.0
ports:
- 27017:27017
iac-scan-runner:
image: xscanner/runner
expose:
- 27017
ports:
- 8080:80
depends_on:
- "mongoservice"
environment:
- MONGODB_CONNECTION_STRING=mongodb://mongoservice:27017/
- SCAN_PERSISTENCE=enabled
......@@ -167,10 +167,10 @@ async def post_scan(iac: UploadFile = File(..., description='IaC file (zip or ta
return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content=str(e))
@app.get("/results", summary="Retrieve particular scan result by given uuid", responses={200: {}, 400: {"model": str}})
async def get_scan_result(uuid: str) -> JSONResponse:
async def get_scan_result(uuid: Optional[str]) -> JSONResponse:
"""
Retrieve a particular scan result (GET method)
:param uuid: Identifier of a scan record in database
:param uuid: Identifier of a saved scan record
:return: JSONResponse object (with status code 200 or 400)
"""
try:
......@@ -188,7 +188,7 @@ async def get_scan_result(uuid: str) -> JSONResponse:
async def delete_scan_result(uuid: str) -> JSONResponse:
"""
Delete a particular scan result (GET method)
:param uuid: Identifier of a scan record in database
:param uuid: Identifier of a saved scan record
:return: JSONResponse object (with status code 200 or 400)
"""
try:
......@@ -202,22 +202,3 @@ async def delete_scan_result(uuid: str) -> JSONResponse:
return JSONResponse(status_code=status.HTTP_200_OK, content=f"No such scan result {uuid}")
except Exception as e:
return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content=str(e))
@app.put("/persistence_enabler/{enable}", summary="Delete particular scan result by given uuid", responses={200: {}, 400: {"model": str}})
async def persistence_enable(enable: str) -> JSONResponse:
"""
Delete a particular scan result (GET method)
:param uuid: Identifier of a scan record in database
:return: JSONResponse object (with status code 200 or 400)
"""
try:
if(enable == "disable"):
scan_runner.persistence_enabled = False
else:
scan_runner.persistence_enabled = True
return JSONResponse(status_code=status.HTTP_200_OK, content=f"Persistence enable: {enable}")
except Exception as e:
return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content=str(e))
......@@ -6,21 +6,32 @@ from datetime import datetime
import os
class ResultsPersistence:
def __init__(self):
"""
Initialize new scan result database, collection and client
"""
self.connect_db()
def connect_db(self):
"""
Initialize new scan result database, collection and client
"""
try:
connection_string = os.environ['MONGO_STRING']
print(connection_string)
connection_string = os.environ['MONGODB_CONNECTION_STRING']
if(connection_string):
self.myclient = pymongo.MongoClient(connection_string)
self.mydb = self.myclient["scandb"]
self.mycol = self.mydb["results"]
self.connection_problem = False
# TODO: Consider more specific exceptions
except Exception as e:
print("Scan result persistence not available")
print(str(e))
self.myclient = None
self.mydb = None
self.mycol = None
self.connection_problem = True
......@@ -32,6 +43,9 @@ class ResultsPersistence:
"""Inserts new scan result into database
:param result: Dictionary holding the scan summary
"""
if(self.connection_problem == True):
self.connect_db()
if(self.mycol != None):
self.mycol.insert_one(self.parse_json(result))
def show_result(self, uuid4: str) -> str:
......@@ -40,6 +54,10 @@ class ResultsPersistence:
:param uuid4: Identifier of a scan result
:return: String representing the scan result record
"""
if(self.connection_problem==True):
self.connect_db()
if(self.mycol != None):
myquery = { "uuid": uuid4 }
mydoc = self.mycol.find(myquery)
for x in mydoc:
......@@ -50,6 +68,10 @@ class ResultsPersistence:
"""Deletes the scan result with given id from database
:param uuid4: Identifier of a scan result which is about to be deleted
"""
if(self.connection_problem==True):
self.connect_db()
if(self.mycol != None):
myquery = { "uuid": uuid4 }
mydoc = self.mycol.delete_one(myquery)
......@@ -59,6 +81,10 @@ class ResultsPersistence:
"""Shows all the scan records from the database
:return: String of all database records concatenated
"""
if(self.connection_problem==True):
self.connect_db()
if(self.mycol != None):
cursor = self.mycol.find({})
output = ""
for doc in cursor:
......@@ -75,6 +101,7 @@ class ResultsPersistence:
time2 = datetime.now() # current date and time
delta = time2 - time1
string_delta = str(delta)
if(string_delta.find("days") > -1):
days = string_delta.split(" ")
days = days[0]
......@@ -89,6 +116,10 @@ class ResultsPersistence:
:param uuid4: Identifier of a scan result
:return: Integer denoting scan result age
"""
if(self.connection_problem == True):
self.connect_db()
if(self.mycol != None):
myquery = { "uuid": uuid4 }
mydoc = self.mycol.find(myquery)
for x in mydoc:
......@@ -101,6 +132,10 @@ class ResultsPersistence:
"""Calculates how long a scan result resides in database since its insertion
:param uuid4: Identifier of a scan result
"""
if(self.connection_problem == True):
self.connect_db()
if(self.mycol != None):
cursor = self.mycol.find({})
scan_ts = ""
for doc in cursor:
......
......@@ -55,7 +55,12 @@ class ScanRunner:
self.compatibility_matrix = Compatibility()
self.results_summary = ResultsSummary()
self.archive_name = ""
if(os.environ.get("SCAN_PERSISTENCE") == "enabled"):
self.persistence_enabled = True
else:
self.persistence_enabled = False
self.results_persistence = ResultsPersistence()
def init_checks(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment