-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecInterface.py
248 lines (211 loc) · 8.3 KB
/
execInterface.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# -*- Mode:Python;indent-tabs-mode:nil; -*-
#
# File: execInterface.py
# Created: 27/08/2014
# Author: BSC, VTT
# Modified: 2016
# Author: VTT, jju, jk
#
# Description:
# Web service running on the PSA receiving the
# configuration for the PSA from the PSC
#
#
import falcon
import requests
import logging
import json
import sys
import subprocess
import datetime
from BroManager import BroManager
# Bro instance:
bro = None
class execInterface():
def __init__ ( self, home, configsPath, scriptsPath, psaLogLocation, psaID, pscAddr, psaAPIVersion ):
self.psaHome = home
self.confsPath = configsPath
self.scripts_path = scriptsPath
self.log_location = psaLogLocation
self.psaID = psaID
self.pscAddr = pscAddr
self.psaAPI = psaAPIVersion
def on_post( self, request, response, command ):
print "onPost"
try:
res = {}
res[ "command" ] = command
if command == "init":
# receive the configuration, or init package
script_file = self.confsPath + "/psaconf"
fp=open(script_file, 'wb')
while True:
chunk = request.stream.read(4096)
fp.write(chunk)
if not chunk:
break
fp.close()
# Make script executable for current user
# hazardous.. we're root
#st = os.stat(script_file)
#os.chmod(script_file, st.st_mode | stat.S_IEXEC)
# Run the init.sh and return it's return value
res["ret_code"] = str(self.callInitScript())
logging.info("PSA "+self.psaID+" configuration registered")
elif command == "start":
res["ret_code"] = str(self.callStartScript())
elif command == "stop":
res["ret_code"] = str(self.callStopScript())
else:
logging.info("POST: unknown command: " + command)
response.status = falcon.HTTP_404
return
response.body = json.dumps(res)
response.status = falcon.HTTP_200
response.set_header("Content-Type", "application/json")
except Exception as e:
logging.exception( sys.exc_info()[0] )
response.status = falcon.HTTP_501
def on_get(self, request, response, command):
try:
res = {}
res["command"] = command
if command == "status":
res["ret_code"] = self.callStatusScript().replace("\n", "")
elif command == "configuration":
res["ret_code"] = self.callGetConfigurationScript()
elif command == "internet":
res["ret_code"] = self.callGetInternetScript()
elif command == "log":
# Return PSA log or 501
log = self.callGetLogScript()
if log != None:
response.body = log
response.status = falcon.HTTP_200
response.set_header("Content-Type", "text/plain; charset=UTF-8")
else:
response.status = falcon.HTTP_501
return
elif command == 'brolog':
log = self.callGetBroLogScript()
if log != None:
response.body = log
response.status = falcon.HTTP_200
response.set_header("Content-Type", "text/plain; charset=UTF-8")
else:
response.status = falcon.HTTP_501
return
else:
logging.info("GET: unknown command: " + command)
response.status = falcon.HTTP_404
return
response.body = json.dumps(res)
response.status = falcon.HTTP_200
response.set_header("Content-Type", "application/json")
except Exception as e:
logging.exception(sys.exc_info()[0])
response.status = falcon.HTTP_501
def callInitScript( self ):
global bro
logging.info ("callInitScript()" )
if bro != None:
bro.stopBro()
del bro
bro = BroManager( self.psaHome, self )
bro.loadConfig( self.confsPath + "/psaconf" )
#ret = subprocess.call([ self.scripts_path + 'init.sh'])
#return ret
logging.info( 'BroManager initialized: %r' % ( bro != None ) )
return 0
def callStartScript( self ):
logging.info( "callStartScript()" )
if bro == None:
logging.critical( 'BroManager instance not found.' )
self.callInitScript()
try:
# bro.start()
try:
bro.connect()
logging.info( 'Bro is already running.' )
return 0
except IOError as e:
logging.info( 'No running instances of Bro found.' )
bro.startBro()
logging.info( 'Bro is running.' )
return 0
except Exception as e:
logging.critical( 'Fatal error while connecting to Bro' )
logging.critical( e )
# ret = subprocess.call([ self.scripts_path + 'start.sh'])
# return ret
return 1
def callStopScript( self ):
logging.info( "callStopScript()" )
if bro != None:
bro.stopBro()
logging.info( 'Bro stopped.' )
else:
logging.info( 'Bro is not running.' )
# ret = subprocess.call([ self.scripts_path + 'stop.sh'])
# return ret
return 0
def callStatusScript( self ):
proc = subprocess.Popen( [ self.scripts_path + 'status.sh' ],
stdout = subprocess.PIPE,
shell = True )
( out, err ) = proc.communicate()
return out
def callGetConfigurationScript( self ):
logging.info( "callGetConfigurationScript()" )
proc = subprocess.Popen( [ self.scripts_path + 'current_config.sh' ],
stdout = subprocess.PIPE,
shell = True )
( out, err ) = proc.communicate()
return out
def callGetInternetScript (self ):
logging.info( "callGetInternetScript()" )
proc = subprocess.Popen( [ self.scripts_path + 'ping.sh' ],
stdout = subprocess.PIPE,
shell = True )
( out, err ) = proc.communicate()
return out
def callGetLogScript( self ):
logging.info( "callGetLogScript()" )
try:
filename = self.confsPath + "/bro.log"
#filename = self.log_location
with open( filename, "r" ) as f:
return f.read()
except Exception as e:
logging.exception( sys.exc_info()[0] )
return None
def get_client_address( self, environ ):
try:
return environ[ 'HTTP_X_FORWARDED_FOR' ].split( ',' )[ -1 ].strip()
except KeyError:
return environ[ 'REMOTE_ADDR' ]
def callGetBroLogScript( self ):
logging.info( "callGetBroLogScript()" )
try:
filename = self.confsPath + "/bro.log"
with open( filename, "r") as f:
return f.read()
except Exception as e:
logging.exception( sys.exc_info()[ 0 ] )
return None
def onEvent( self, logEntry ):
filename = self.confsPath + "/bro.log"
line = str( datetime.datetime.utcnow() ) + ': ' + logEntry
with open( filename, "a" ) as logFile:
logFile.write( line )
def onNotifyEvent( self, policy, title, info):
self.sendPsaEvent(policy, title, info)
def sendPsaEvent(self, policy, title, info):
logging.info( "sendPsaEvent()" )
header = {"Content-Type": "application/json"}
ev = {"psa_id": self.psaID, "event_title": title, "event_body": info, "extra_info": policy, "hspl_id": "", "mspl_id": ""}
url = self.pscAddr + "/" + self.psaAPI + "/psaEvent/" + self.psaID
try:
requests.post(url, data=json.dumps(ev), headers=header)
except Exception as e:
logging.exception( sys.exc_info()[ 0 ] )