Skip to content

Commit

Permalink
Modified TLS and conf file features
Browse files Browse the repository at this point in the history
* TOOLS-1082: (ASADM) Fix tls connection timeout issue.

* Modified to work without toml and jsonschema installation if no need to read from any conf file.
  • Loading branch information
hbpatre committed Feb 20, 2018
1 parent 759b328 commit abe9b22
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 60 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ Admin> help
- python 2.6+ (< 3)

### Python Modules
- ply: >= 3.4
- jsonschema >= 2.5.1 (for centos6 please install jsonschema==2.5.1)
- pexpect: >= 3.0
- ply: >= 3.4
- pyOpenSSL: >= 16.2.0
- pyasn1: >= 0.3.1
- toml


### Installing Python Module Dependencies
```
Expand Down
12 changes: 9 additions & 3 deletions lib/client/assocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,33 @@ def _create_socket_for_addrinfo(self, addrinfo, tls_name=None, user=None,
sock_addr = addrinfo[4]

sock = socket.socket(addr_family, socket.SOCK_STREAM)
if not ssl_context:
sock.settimeout(self._timeout)
# timeout on wrapper might give errors
sock.settimeout(self._timeout)

sock = self._wrap_socket(sock, ssl_context)
sock.connect(sock_addr)

if ssl_context:
try:
sock.set_app_data(tls_name)

# timeout on wrapper might give errors
sock.setblocking(1)

sock.do_handshake()
except Exception as tlse:
print "TLS connection exception: " + str(tlse)
if sock:
sock.close()
sock = None
return None

if user != None:
rc = authenticate(sock, user, password)
if rc != 0:
print "Authentication failed for ", user, ": ", rc
sock.close()
return None

except Exception:
sock = None
pass
Expand Down
115 changes: 62 additions & 53 deletions lib/utils/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import toml
import collections
import json
import os
import re

import collections
try:
import toml
HAVE_TOML = True
except ImportError:
HAVE_TOML = False

from jsonschema import validate
try:
from jsonschema import validate
HAVE_JSONSCHEMA = True
except ImportError:
HAVE_JSONSCHEMA = False

from lib.utils.constants import ADMIN_HOME

Expand All @@ -29,47 +37,43 @@ def __init__(self, adict):

# Default is local host without security / tls
# with timeout value of 5ms
_confdefault = '''
[cluster]
host = "127.0.0.1:3000"
port = 3000
user = ""
password = "prompt"
tls-enable = false
tls-cafile = ""
tls-capath = ""
tls-cert-blacklist = ""
tls-certfile = ""
tls-cipher-suite = ""
tls-crl-check = false
tls-crl-check-all = false
tls-keyfile = ""
tls-protocols = ""
[asadm]
services-alumni = false
services-alternate = false
timeout = 5
line-separator = false
no-color = false
out-file = ""
profile = false
single-node = false
help = false
version = false
asinfo-mode = false
collectinfo = false
execute = false
log-analyser = false
log-path = ""
}'''
_confdefault = {
"cluster": {
"host": "127.0.0.1",
"port": 3000,
"user": None,
"password": "prompt",
"tls-enable": False,
"tls-cafile": "",
"tls-capath": "",
"tls-cert-blacklist": "",
"tls-certfile": "",
"tls-cipher-suite": "",
"tls-crl-check": False,
"tls-crl-check-all": False,
"tls-keyfile": "",
"tls-protocols": "",
},
"asadm": {
"services-alumni": False,
"services-alternate": False,
"timeout": 5,

"line-separator": False,
"no-color": False,
"out-file": "",
"profile": False,
"single-node": False,

"help": False,
"version": False,
"asinfo-mode": False,
"collectinfo": False,
"execute": False,
"log-analyser": False,
"log-path": "",
},
}

_confspec = '''{
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -141,15 +145,18 @@ def __init__(self, adict):
}'''

def _getdefault(logger):
conf_dict = toml.loads(_confdefault)
validate(conf_dict, json.loads(_confspec))
return conf_dict
import copy
return copy.deepcopy(_confdefault)

def _loadfile(fname, logger):
conf_dict = {}

if os.path.exists(fname):
# file exists
conf_dict = toml.loads(open(fname).read())
if HAVE_TOML:
conf_dict = toml.loads(open(fname).read())
else:
raise ImportError("No module named toml")

include_files = []
if "include" in conf_dict.keys():
Expand All @@ -167,7 +174,11 @@ def _loadfile(fname, logger):
except Exception as e:
logger.error("Config file parse error: " + str(f) + " " + str(e).split("\n")[0])

validate(conf_dict, json.loads(_confspec))
if HAVE_JSONSCHEMA:
validate(conf_dict, json.loads(_confspec))
else:
raise ImportError("No module named jsonschema")

return conf_dict

def decode(v):
Expand Down Expand Up @@ -307,8 +318,7 @@ def loadconfig(cli_args, logger):
_merge(conf_dict, _loadfile(f, logger))
except Exception as e:
# Bail out of the primary file has parsing error.
logger.error("Config file parse error: " + str(f) + " " + str(e).split("\n")[0])
exit(-1)
logger.critical("Config file parse error: " + str(f) + " " + str(e).split("\n")[0])

# Read config file if no-config-file is not specified
# is specified
Expand All @@ -325,8 +335,7 @@ def loadconfig(cli_args, logger):
_merge(conf_dict, _loadfile(f, logger))
except Exception as e:
# Bail out of the primary file has parsing error.
logger.error("Config file parse error: " + str(f) + " " + str(e).split("\n")[0])
exit(-1)
logger.critical("Config file parse error: " + str(f) + " " + str(e).split("\n")[0])

asadm_dict = _flatten(conf_dict, cli_args.instance)

Expand Down
6 changes: 3 additions & 3 deletions test/test_asinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
run_test(){
unknown_option_error="Do not understand"
asinfo_cmd_str="'$1' "
cmd_out=`./asadm.py --asinfo-mode -e "${asinfo_cmd_str}"`
cmd_out=`./asadm.py --asinfo-mode --no-config-file -e "${asinfo_cmd_str}"`
cmd_status="$?"
# echo ${cmd_out}
if [ "$cmd_status" -ne 0 ]; then
Expand Down Expand Up @@ -110,7 +110,7 @@ fi

asinfo_cmd_str="\"STATUS\" "

cmd_out=`./asadm.py --asinfo-mode -e "${asinfo_cmd_str}" | tr -dc '[:alnum:]\n\r'`
cmd_out=`./asadm.py --asinfo-mode --no-config-file -e "${asinfo_cmd_str}" | tr -dc '[:alnum:]\n\r'`
cmd_status="$?"

if [ "$cmd_status" -ne 0 ]; then
Expand All @@ -122,7 +122,7 @@ if [[ $cmd_out != "OK" ]];then
exit 1
fi

cmd_out=`./asadm.py --asinfo-mode -e "${asinfo_cmd_str}" | hexdump`
cmd_out=`./asadm.py --asinfo-mode --no-config-file -e "${asinfo_cmd_str}" | hexdump`
cmd_status="$?"
expected_output=`echo "OK" | hexdump`

Expand Down

0 comments on commit abe9b22

Please sign in to comment.