diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..40383a2 --- /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 diff --git a/cs.py b/cs.py index 46acd62..a3e2d42 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,8 @@ 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..cb11e28 --- /dev/null +++ b/modules/argspopulator.py @@ -0,0 +1,69 @@ +#!/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: + + 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 _: + return args_dict + +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