Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature of ConfigFile introduced #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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")
Expand Down
69 changes: 69 additions & 0 deletions modules/argspopulator.py
Original file line number Diff line number Diff line change
@@ -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

27 changes: 27 additions & 0 deletions modules/readconfigfile.py
Original file line number Diff line number Diff line change
@@ -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