From aab6160d492aaa5b020761fd8e543496ba95ffdc Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Mon, 21 Oct 2019 16:20:54 -0400 Subject: [PATCH] Fix compatibility between new and old TSM firmware --- .../confluent/discovery/handlers/tsm.py | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/confluent_server/confluent/discovery/handlers/tsm.py b/confluent_server/confluent/discovery/handlers/tsm.py index 9ab4d467..e079202e 100644 --- a/confluent_server/confluent/discovery/handlers/tsm.py +++ b/confluent_server/confluent/discovery/handlers/tsm.py @@ -18,6 +18,11 @@ import confluent.netutil as netutil import confluent.util as util import eventlet.support.greendns import json +try: + from urllib import urlencode +except ImportError: + from urllib.parse import urlencode + getaddrinfo = eventlet.support.greendns.getaddrinfo webclient = eventlet.import_patched('pyghmi.util.webclient') @@ -52,8 +57,15 @@ class NodeHandler(generic.NodeHandler): } wc = webclient.SecureHTTPConnection(self.ipaddr, 443, verifycallback=self.validate_cert) wc.set_header('Content-Type', 'application/json') - if not self.trieddefault: + authmode = 0 + if not self.trieddefault: rsp, status = wc.grab_json_response_with_status('/api/session', authdata) + if status == 403: + wc.set_header('Content-Type', 'application/x-www-form-urlencoded') + authmode = 1 + rsp, status = wc.grab_json_response_with_status('/api/session', urlencode(authdata)) + else: + authmode = 2 if status > 400: rsp = util.stringify(rsp) self.trieddefault = True @@ -65,9 +77,15 @@ class NodeHandler(generic.NodeHandler): 'default_password': self.DEFAULT_PASS, 'username': self.DEFAULT_USER } - rsp, status = wc.grab_json_response_with_status('/api/reset-pass', passchange) + if authmode == 2: + rsp, status = wc.grab_json_response_with_status('/api/reset-pass', passchange) + else: + rsp, status = wc.grab_json_response_with_status('/api/reset-pass', urlencode(passchange)) authdata['password'] = self.targpass - rsp, status = wc.grab_json_response_with_status('/api/session', authdata) + if authmode == 2: + rsp, status = wc.grab_json_response_with_status('/api/session', authdata) + else: + rsp, status = wc.grab_json_response_with_status('/api/session', urlencode(authdata)) self.csrftok = rsp['CSRFToken'] self.channel = rsp['channel'] self.curruser = self.DEFAULT_USER @@ -82,7 +100,11 @@ class NodeHandler(generic.NodeHandler): if self.curruser: authdata['username'] = self.curruser authdata['password'] = self.currpass - rsp, status = wc.grab_json_response_with_status('/api/session', authdata) + if authmode != 1: + rsp, status = wc.grab_json_response_with_status('/api/session', authdata) + if authmode == 1 or rsp.status == 403: + wc.set_header('Content-Type', 'application/x-www-form-urlencoded') + rsp, status = wc.grab_json_response_with_status('/api/session', urlencode(authdata)) if rsp.status != 200: return None self.csrftok = rsp['CSRFToken'] @@ -90,7 +112,11 @@ class NodeHandler(generic.NodeHandler): return wc authdata['username'] = self.targuser authdata['password'] = self.targpass - rsp, status = wc.grab_json_response_with_status('/api/session', authdata) + if authmode != 1: + rsp, status = wc.grab_json_response_with_status('/api/session', authdata) + if authmode == 1 or rsp.status == 403: + wc.set_header('Content-Type', 'application/x-www-form-urlencoded') + rsp, status = wc.grab_json_response_with_status('/api/session', urlencode(authdata)) if status != 200: return None self.curruser = self.targuser