From a8e1258a18ce563899d788b608f4d41362886bc8 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Mon, 18 Nov 2019 22:04:04 +0530 Subject: [PATCH 1/2] Config file done --- config.ini | 18 +++++++++++ cs.py | 5 +-- modules/argspopulator.py | 68 +++++++++++++++++++++++++++++++++++++++ modules/readconfigfile.py | 27 ++++++++++++++++ 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 config.ini create mode 100644 modules/argspopulator.py create mode 100644 modules/readconfigfile.py diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..9450915 --- /dev/null +++ b/config.ini @@ -0,0 +1,18 @@ +[default] +environment = aws +wipe = False +number = 1 +output = cs_audit.log +audit_ip = None + +[azure] +azure_user = None +azure_pass = None + +[aws] +user_name = None +pem_file = None +password = None + +[gcp] +project_id = None \ No newline at end of file diff --git a/cs.py b/cs.py index 46acd62..51b5e9a 100755 --- a/cs.py +++ b/cs.py @@ -6,12 +6,13 @@ from modules import logger import rm import subprocess +from modules import argspopulator def main(): """ main function """ parser = argparse.ArgumentParser(description='this is to get IP address for lynis audit only') - parser.add_argument('-env', '--environment', required=True, help='The cloud on which the test-suite is to be run', + parser.add_argument('-env', '--environment', required=False, help='The cloud on which the test-suite is to be run', choices=['aws', 'gcp', 'azure']) parser.add_argument('-aip', '--audit_ip', required=False, help='The IP for which lynis Audit needs to be done .... by default tries root/Administrator if username not provided') parser.add_argument('-u', '--user_name', required=False, help='The username of the user to be logged in,for a specific user') @@ -26,7 +27,7 @@ def main(): parser.add_argument('-n', '--number', required=False, help='Retain number of report to store for a particular environment and user/project.') args = parser.parse_args() - + args = argspopulator.update_args(args) # set up logging log = logger.setup_logging(args.output, "INFO") diff --git a/modules/argspopulator.py b/modules/argspopulator.py new file mode 100644 index 0000000..d8d9726 --- /dev/null +++ b/modules/argspopulator.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +import ConfigParser +import argparse +import readconfigfile +from argparse import Namespace as Namespace +import logging +import os + +def check_run_time_argument(args_dict): + list_of_run_time_keys = [] + list_of_run_time_values = [] + for key, value in args_dict.items(): + if value != None and value != False: + list_of_run_time_keys.append(key) + list_of_run_time_values.append(value) + data_dict = dict(zip(list_of_run_time_keys, list_of_run_time_values)) + return data_dict + +def get_environment(): + config = ConfigParser.ConfigParser() + config.read('config.ini') + env_value_at_config_file = config.get('default','environment') + if env_value_at_config_file == 'None': + env_value_at_config_file = None + return env_value_at_config_file + +def put_env_variables(args_dict): + try: + print(os.environ['password']) + list_of_env_variables = ['password','azure_pass'] + for env_variable in list_of_env_variables: + if os.environ[env_variable] != None and os.environ[env_variable] != 'None' : + args_dict[env_variable] = os.environ[env_variable] + return args_dict + except Exception as identifier: + logging.exception(identifier) + +def update_args(args_namespace): + args_dict = vars(args_namespace) + sections = ['default'] + if args_namespace.environment != None: + sections.append(args_namespace.environment) + elif get_environment() != None : + sections.append(get_environment()) + else: + print("No environment defined to run audit upon!") + exit(0) + data_from_cli = check_run_time_argument(args_dict) + config_file_data = {} + for section in sections: + config_file_data[section] = readconfigfile.get_section_data(section) + args_dict = put_config_file_data(sections,config_file_data,args_dict) + args_dict = put_runtime_arguments(data_from_cli,args_dict) + args_dict = put_env_variables(args_dict) + args_namespace = Namespace(**args_dict) + return args_namespace + +def put_runtime_arguments(data,args_dict): + for single_data in data: + args_dict[single_data] = data[single_data] + return args_dict + +def put_config_file_data(sections,config_file_data,args_dict): + for section in sections: + for i in config_file_data[section]: + args_dict[i] = config_file_data[section][i] + return args_dict + \ No newline at end of file diff --git a/modules/readconfigfile.py b/modules/readconfigfile.py new file mode 100644 index 0000000..b78ad7c --- /dev/null +++ b/modules/readconfigfile.py @@ -0,0 +1,27 @@ +import ConfigParser + + +def get_section_data(section): + list_of_config_file_keys = [] + list_of_config_file_values = [] + config = ConfigParser.ConfigParser() + config.read('config.ini') + raw_section_data = config.items(section) + for i in range(len(raw_section_data)): + list_of_config_file_keys.append(raw_section_data[i][0]) + list_of_config_file_values.append(raw_section_data[i][1]) + data_dict = dict(zip(list_of_config_file_keys, list_of_config_file_values)) + data_dict = correct_false_values(data_dict) + return data_dict + + + +def correct_false_values(args_dict): + for key in args_dict: + if args_dict[key] == 'None': + args_dict[key] = None + if args_dict[key] == 'False': + args_dict[key] = False + if args_dict[key] == 'True': + args_dict[key] = True + return args_dict From 707519395de82fb5e16388f2608e9c9f01904c1b Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Tue, 26 Nov 2019 00:34:03 +0530 Subject: [PATCH 2/2] tested --- config.ini | 2 +- cs.py | 1 + modules/argspopulator.py | 13 +++++++------ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/config.ini b/config.ini index 9450915..40383a2 100644 --- a/config.ini +++ b/config.ini @@ -15,4 +15,4 @@ pem_file = None password = None [gcp] -project_id = None \ No newline at end of file +project_id = None diff --git a/cs.py b/cs.py index 51b5e9a..a3e2d42 100755 --- a/cs.py +++ b/cs.py @@ -29,6 +29,7 @@ def main(): args = parser.parse_args() args = argspopulator.update_args(args) + # set up logging log = logger.setup_logging(args.output, "INFO") diff --git a/modules/argspopulator.py b/modules/argspopulator.py index d8d9726..cb11e28 100644 --- a/modules/argspopulator.py +++ b/modules/argspopulator.py @@ -26,14 +26,14 @@ def get_environment(): def put_env_variables(args_dict): try: - print(os.environ['password']) + list_of_env_variables = ['password','azure_pass'] for env_variable in list_of_env_variables: - if os.environ[env_variable] != None and os.environ[env_variable] != 'None' : + if os.environ[env_variable] != None and os.environ[env_variable] != 'None' : args_dict[env_variable] = os.environ[env_variable] - return args_dict - except Exception as identifier: - logging.exception(identifier) + return args_dict + except Exception as _: + return args_dict def update_args(args_namespace): args_dict = vars(args_namespace) @@ -51,10 +51,11 @@ def update_args(args_namespace): config_file_data[section] = readconfigfile.get_section_data(section) args_dict = put_config_file_data(sections,config_file_data,args_dict) args_dict = put_runtime_arguments(data_from_cli,args_dict) - args_dict = put_env_variables(args_dict) + args_dict = put_env_variables(args_dict) args_namespace = Namespace(**args_dict) return args_namespace + def put_runtime_arguments(data,args_dict): for single_data in data: args_dict[single_data] = data[single_data]