-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scanner.py
78 lines (74 loc) · 2.75 KB
/
Scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import subprocess
import os
import asyncio
import json
from json2html import *
# common scanner class for scanning repositories
class CodeScanner:
def __init__(self,repoLink,path,repoName,scanResultPath):
# Adding checks to variables
if repoLink is None:
print("repoLink cannot be none")
else:
self.repoLink = repoLink
if path is None:
print("scan path cannot be none")
else:
# remove new line from the string
path = path.strip()
self.path = path
if repoName is None:
print("repo name cannot be none")
else:
# remove new lines from the string
repoName = repoName.strip()
self.repoName = repoName
if scanResultPath is None:
print("scan result path cannot be none")
else:
self.scanResultPath = scanResultPath
# self.scanCode()
# self.generateReport()
# clone code from source repository
async def getCode(self):
try:
await os.system('git clone '+self.repoLink+" "+self.path+"/"+self.repoName+"/ --depth 1")
return "success"
except:
print("error")
return "failed"
# scan code using SemGrep
def scanCode(self):
try:
#p = os.system('docker run --rm -v "'+self.path+'/'+self.repoName+':/src" returntocorp/semgrep semgrep --config=auto --output=output.json --json --verbose --max-target-bytes 15MB')
command = 'docker run --rm -v "'+self.path+'/'+self.repoName+':/src" returntocorp/semgrep semgrep --config=auto --output=output.json --json --verbose --max-target-bytes 15MB'
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
return "success"
except:
print("error")
return "failed"
# move the generated file
async def moveReport(self):
try:
await os.system('mv '+self.path+'/'+self.repoName+'/output.json '+self.scanResultPath+'/'+self.repoName+'.json')
return "success"
except:
print("error")
return "failed"
# generate HTML report
async def generateReport(self):
f = open(str(self.scanResultPath+'/'+self.repoName+'.json'))
data = json.loads(f.read())
output = json2html.convert(json = data)
file = open(self.scanResultPath+'/'+self.repoName+'.html',"w")
await file.write(output)
file.close()
# delete the code to conserve memory
def cleanUp(self):
try:
os.system('rm -rf '+self.path+'/'+self.repoName)
print("success")
return "success"
except:
return "failed"