Skip to content

Commit 82d8a3f

Browse files
committed
Refactor zsh configs and api config handling
1 parent 81308b2 commit 82d8a3f

File tree

6 files changed

+73
-47
lines changed

6 files changed

+73
-47
lines changed

apis/rest/api.py

+12-21
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,20 @@ def stopServer():
4242
_http_server.stop()
4343

4444

45-
def startAPIs(plugin_manager, model_controller, mapper_manager, hostname=None, port=None):
45+
def startAPIs(plugin_manager, model_controller, mapper_manager, hostname, port):
4646
global _rest_controllers
4747
global _http_server
4848
_rest_controllers = [PluginControllerAPI(plugin_manager, mapper_manager), ModelControllerAPI(model_controller)]
4949

50-
#TODO: some way to get defaults.. from config?
51-
if str(hostname) == "None":
52-
hostname = "localhost"
53-
if str(port) == "None":
54-
port = 9977
55-
56-
if CONF.getApiRestfulConInfo() is None:
57-
CONF.setApiRestfulConInfo(hostname, port)
58-
5950
app = Flask('APISController')
6051

6152
_http_server = HTTPServer(WSGIContainer(app))
62-
_http_server.listen(port,address=hostname)
53+
_http_server.listen(port, address=hostname)
6354

6455
routes = [r for c in _rest_controllers for r in c.getRoutes()]
6556

66-
for route in routes:
67-
app.add_url_rule(route.path, view_func=route.view_func, methods=route.methods)
57+
for route in routes:
58+
app.add_url_rule(route.path, view_func=route.view_func, methods=route.methods)
6859

6960
logging.getLogger("tornado.access").addHandler(logger.getLogger(app))
7061
logging.getLogger("tornado.access").propagate = False
@@ -169,16 +160,16 @@ def deleteVuln(self):
169160
hostid = json_data['hostid']
170161

171162
host = self.controller.getHost(hostid)
172-
if not host:
173-
return self.badRequest("no plugin available for cmd")
163+
if not host:
164+
return self.badRequest("no plugin available for cmd")
174165

175166
visitor = VulnsLookupVisitor(vulnid)
176167
host.accept(visitor)
177168

178169
if not visitor.vulns:
179170
return self.noContent('No vuls matched criteria')
180171

181-
# forward to controller
172+
# forward to controller
182173
for vuln, parents in zip(visitor.vulns, visitor.parents):
183174
last_parent = parents[0]
184175
self.controller.delVulnSYNC(last_parent, vuln.getID())
@@ -198,8 +189,8 @@ def postEditVulns(self):
198189
hostid = json_data['hostid']
199190

200191
host = self.controller.getHost(hostid)
201-
if not host:
202-
return self.badRequest("no plugin available for cmd")
192+
if not host:
193+
return self.badRequest("no plugin available for cmd")
203194

204195
visitor = VulnsLookupVisitor(vulnid)
205196
host.accept(visitor)
@@ -213,9 +204,9 @@ def postEditVulns(self):
213204
resolution = json_data.get('resolution', None)
214205
refs = json_data.get('refs', None)
215206

216-
# forward to controller
217-
for vuln in visitor.vulns:
218-
self.controller.editVulnSYNC(vuln, name, desc, severity, resolution, refs)
207+
# forward to controller
208+
for vuln in visitor.vulns:
209+
self.controller.editVulnSYNC(vuln, name, desc, severity, resolution, refs)
219210

220211
return self.ok("output successfully sent to plugin")
221212

config/configuration.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ def getApiParams(self):
280280
def getTktTemplate(self):
281281
return self._tkt_template
282282

283-
284-
285283
def setLastWorkspace(self, workspaceName):
286284
self._last_workspace = workspaceName
287285

@@ -411,16 +409,21 @@ def saveConfig(self, xml_file="~/.faraday/config/user.xml"):
411409

412410
ROOT = Element("faraday")
413411

412+
tree = self._getTree()
413+
414414
API_CON_INFO_HOST = Element(CONST_API_CON_INFO_HOST)
415-
API_CON_INFO_HOST.text = self.getApiConInfoHost()
415+
API_CON_INFO_HOST.text = self._getValue(tree, CONST_API_CON_INFO_HOST)
416+
# API_CON_INFO_HOST.text = self.getApiConInfoHost()
416417
ROOT.append(API_CON_INFO_HOST)
417418

418419
API_CON_INFO_PORT = Element(CONST_API_CON_INFO_PORT)
419-
API_CON_INFO_PORT.text = str(self.getApiConInfoPort())
420+
API_CON_INFO_PORT.text = self._getValue(tree, CONST_API_CON_INFO_PORT)
421+
# API_CON_INFO_PORT.text = str(self.getApiConInfoPort())
420422
ROOT.append(API_CON_INFO_PORT)
421423

422424
API_RESTFUL_CON_INFO_PORT = Element(CONST_API_RESTFUL_CON_INFO_PORT)
423-
API_RESTFUL_CON_INFO_PORT.text = str(self.getApiRestfulConInfoPort())
425+
API_RESTFUL_CON_INFO_PORT.text = self._getValue(tree, CONST_API_RESTFUL_CON_INFO_PORT)
426+
# API_RESTFUL_CON_INFO_PORT.text = str(self.getApiRestfulConInfoPort())
424427
ROOT.append(API_RESTFUL_CON_INFO_PORT)
425428

426429
APPNAME = Element(CONST_APPNAME)

faraday-terminal.zsh

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,25 @@
77
###
88

99
#ZDOTDIR="~/.faraday/zsh/" /bin/zsh
10+
FARADAY_ZSH_RPORT="9977"
11+
FARADAY_ZSH_HOST="127.0.0.1"
12+
if [ $# -eq 2 ]; then
13+
FARADAY_ZSH_HOST=$1
14+
FARADAY_ZSH_RPORT=$2
15+
else
16+
if [ $# -gt 2 ] || [ $# -eq 1 ]; then
17+
echo "[*] Usage $0 host port"
18+
echo "[*] Usage $0 127.0.0.1 9977"
19+
exit
20+
else
21+
echo "[!] Using default configuration" $FARADAY_ZSH_HOST:$FARADAY_ZSH_RPORT
22+
fi
23+
fi
24+
25+
export FARADAY_ZSH_RPORT
26+
export FARADAY_ZSH_HOST
1027
FARADAYZDOTDIR="$HOME/.faraday/zsh/"
1128
OLDZDOTDIR=$ZDOTDIR
1229
ZDOTDIR=$FARADAYZDOTDIR /bin/zsh
13-
#source ~/.faraday/zsh/.zshrc
30+
31+
#source ~/.faraday/zsh/.zshrc

faraday.py

+26-10
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ def getParserArgs():
8787

8888
parser_connection.add_argument('-n', '--hostname', action="store",
8989
dest="host",
90-
default="localhost",
90+
default=None,
9191
help="The hostname where both server APIs will listen (XMLRPC and RESTful). \
9292
Default = localhost")
9393

94-
parser_connection.add_argument('-px', '--port-xmlrpc', action="store", dest="port_xmlrpc", default=9876, type=int,
94+
parser_connection.add_argument('-px', '--port-xmlrpc', action="store", dest="port_xmlrpc", default=None, type=int,
9595
help="Sets the port where the api XMLRPCServer will listen. Default = 9876")
9696
parser_connection.add_argument('-pr', '--port-rest', action="store", dest="port_rest",
97-
default=9977, type=int,
97+
default=None, type=int,
9898
help="Sets the port where the api RESTful server will listen. Default = 9977")
9999

100100
parser.add_argument('-d', '--debug', action="store_true", dest="debug",
@@ -267,12 +267,28 @@ def setConf():
267267

268268
CONF = getInstanceConfiguration()
269269
CONF.setDebugStatus(args.debug)
270-
# if args.host != FARADAY_DEFAULT_HOST:
271-
CONF.setApiConInfoHost(args.host)
272-
# if args.port_xmlrpc != FARADAY_DEFAULT_PORT_XMLRPC:
273-
CONF.setApiConInfoPort(args.port_xmlrpc)
274-
# if args.port_rest != FARADAY_DEFAULT_PORT_REST:
275-
CONF.setApiRestfulConInfoPort(args.port_rest)
270+
271+
host = CONF.getApiConInfoHost()
272+
port_xmlrpc = CONF.getApiConInfoPort()
273+
port_rest = CONF.getApiRestfulConInfoPort()
274+
275+
print "[CONF]", host, port_rest, port_xmlrpc
276+
277+
host = host if host else FARADAY_DEFAULT_HOST
278+
port_xmlrpc = port_xmlrpc if port_xmlrpc else FARADAY_DEFAULT_PORT_XMLRPC
279+
port_rest = port_rest if port_rest else FARADAY_DEFAULT_PORT_REST
280+
281+
print "[DEFAULTS]", host, port_rest, port_xmlrpc
282+
283+
host = args.host if args.host else host
284+
port_xmlrpc = args.port_xmlrpc if args.port_xmlrpc else port_xmlrpc
285+
port_rest = args.port_rest if args.port_rest else port_rest
286+
287+
print "[ARGS]", host, port_rest, port_xmlrpc
288+
289+
CONF.setApiConInfoHost(host)
290+
CONF.setApiConInfoPort(port_xmlrpc)
291+
CONF.setApiRestfulConInfoPort(port_rest)
276292

277293
CONF.setAuth(args.disable_login)
278294

@@ -600,6 +616,7 @@ def init():
600616
global logger
601617

602618
args = getParserArgs()
619+
setUpLogger(args.debug)
603620
logger = getLogger("launcher")
604621

605622

@@ -620,7 +637,6 @@ def main():
620637
setConf()
621638
checkCouchUrl()
622639
checkVersion()
623-
setUpLogger()
624640
update()
625641
checkUpdates()
626642
startFaraday()

zsh/faraday.zsh

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
#
66
#'''
77

8-
98
WORKSPACE=`cat $HOME/.faraday/config/user.xml | grep '<last_workspace' | cut -d '>' -f 2 | cut -d '<' -f 1`
10-
STATUS=`curl -s 127.0.0.1:9977/status/check | sed "s/[^0-9]//g" | grep -v '^[[:space:]]*$'`
9+
STATUS=`curl -s $FARADAY_ZSH_HOST:$FARADAY_ZSH_RPORT/status/check | sed "s/[^0-9]//g" | grep -v '^[[:space:]]*$'`
1110
PS1="%{${fg_bold[red]}%}[faraday]($WORKSPACE)%{${reset_color}%} $PS1"
1211

1312
echo ">>> WELCOME TO FARADAY"
1413
echo "[+] Current Workspace: $WORKSPACE"
1514
if [[ -z $STATUS ]]; then
1615
echo "[-] API: Warning API unreachable"
17-
18-
elif [[ $STATUS == "200" ]]; then
16+
17+
elif [[ $STATUS == "200" ]]; then
1918
echo "[+] API: OK"
20-
else
21-
echo "[!] API: $STATUS"
22-
19+
else
20+
echo "[!] API: $STATUS"
21+
2322
fi
2423

2524
setopt multios

zsh/plugin_controller_client.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
if not os.path.exists(output_folder):
2121
os.mkdir(output_folder)
2222

23-
#TODO: Load this from faraday config
24-
host = "127.0.0.1"
25-
port = 9977
23+
host = os.environ["FARADAY_ZSH_HOST"]
24+
port = int(os.environ["FARADAY_ZSH_RPORT"])
2625

2726
url_input = "http://%s:%d/cmd/input" % (host, port)
2827
url_output = "http://%s:%d/cmd/output" % (host, port)

0 commit comments

Comments
 (0)