From 67cde9e0a9a7fc6b597881faf659e0699ff92878 Mon Sep 17 00:00:00 2001 From: Bartek Grzybowski Date: Thu, 2 Jul 2020 11:53:47 +0200 Subject: Fix --conf option validation Native Argparse library facility is used to validate the --conf option value. In case of invalid value error msg is printed together with usage info. Change-Id: I43aee892699c8238176b19a5148d217b3d16102c Issue-ID: INT-1643 Signed-off-by: Bartek Grzybowski --- test/security/check_for_ingress_and_nodeports.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/security/check_for_ingress_and_nodeports.py b/test/security/check_for_ingress_and_nodeports.py index 7429afce9..f2b7be09e 100755 --- a/test/security/check_for_ingress_and_nodeports.py +++ b/test/security/check_for_ingress_and_nodeports.py @@ -284,7 +284,13 @@ def compare_nodeports_and_ingress(): console_compare_visualisation(scan_key,s1,s2) return num_failures - +def kube_config_exists(conf): + try: + assert path.exists(conf) + except AssertionError: + raise argparse.ArgumentTypeError(f'Fatal! K8S config {conf} does not exist') + else: + return conf if __name__ == "__main__": colorama.init() @@ -312,7 +318,8 @@ if __name__ == "__main__": ) parser.add_argument( "--conf", default='~/.kube/config', action='store', - help = 'kubernetes config file' + help = 'kubernetes config file', + type = kube_config_exists ) parser.add_argument("--verbose", default=False, action='store_true', @@ -322,11 +329,6 @@ if __name__ == "__main__": K8S_NAMESPACE = args.namespace K8S_INGRESS_NS = args.ingress_namespace VERBOSE = args.verbose - try: - assert path.exists(args.conf) - except AssertionError: - print('Fatal! K8S config',args.conf, 'does not exist',file=sys.stderr) - sys.exit(-1) config.load_kube_config(config_file=args.conf) v1 = client.CoreV1Api() v1b = client.ExtensionsV1beta1Api() -- cgit 1.2.3-korg