def _convert_server_size_needed_to_int(server_size_needed): """ converts the cli large/medium/small into the value set in the config for a [large|medium|small]_server_max_concurrent_scans by default this is large = 2, medium = 1, small = 1 but can be changed. :param server_size_needed: :return: """ # default is large. try: # if value in config is not an integer will fail. if server_size_needed.lower() in ['large']: Logger.app.info("A large server will be selected to handle this request.") large_server_max_concurrent_scans = Config().conf_get('webinspect', 'large_server_max_concurrent_scans') return int(large_server_max_concurrent_scans) # by default 2 # medium exists for some backwards compatibility. if server_size_needed.lower() in ['medium']: Logger.app.info("A medium server will be selected to handle this request.") medium_server_max_concurrent_scans = Config().conf_get('webinspect', 'medium_server_max_concurrent_scans') return int(medium_server_max_concurrent_scans) # by default 1 if server_size_needed.lower() in ['small']: Logger.app.info("A small server will be selected to handle this request.") small_server_max_concurrent_scans = Config().conf_get('webinspect', 'small_server_max_concurrent_scans') return int(small_server_max_concurrent_scans) # by default 1 else: Logger.app.error("Size doesn't exist! Valid sizes are 'small' or 'large'") sys.exit(ExitStatus.failure) except TypeError as e: Logger.app.error("value in config for [large/small]_server_max_concurrent_scans is not an integer. " "{}".format(e)) sys.exit(ExitStatus.failure)
def test_set_config_basic(set_vars_mock, conf_get_mock): set_vars_mock.return_value = None conf_get_mock.return_value = None Config() assert conf_get_mock.call_count == 55
def __init__(self): config_file = Config().config try: config.read(config_file) self.ssc_url = config.get("fortify", "ssc_url") self.project_template = config.get("fortify", "project_template") self.application_name = config.get("fortify", "application_name") self.verify_ssl = convert_verify_ssl_config( config.get("fortify", "verify_ssl")) # Bulk API Application Values self.business_risk_ranking = config.get("fortify", "business_risk_ranking") self.development_phase = config.get("fortify", "development_phase") self.development_strategy = config.get("fortify", "development_strategy") self.accessibility = config.get("fortify", "accessibility") self.custom_attribute_name = config.get("fortify", "custom_attribute_name") self.custom_attribute_value = config.get("fortify", "custom_attribute_value") except (configparser.NoOptionError, CalledProcessError) as noe: fortifyloghelper.log_error_file_incorrect_value(config_file, noe) except configparser.Error as e: fortifyloghelper.log_error_reading(config_file, e)
def test_set_path_install_success(set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None result = Config().set_path(install='/test/install') assert set_vars_mock.call_count == 1 assert set_config_mock.call_count == 1 assert result == 1
def test_set_path_dir_success(makedirs_mock, set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None result = Config().set_path(install='/test/install', dir_path='test_dir') assert makedirs_mock.call_count == 1 assert result == '/test/install/test_dir'
def _webinspect_git_clone(self, cli_settings): """ If local file exist, it will use that file. If not, it will go to github and clone the config files :return: """ try: config_helper = Config() etc_dir = config_helper.etc git_dir = os.path.join(config_helper.git, '.git') try: if cli_settings == 'Default': webinspectloghelper.log_info_default_settings() elif os.path.exists(git_dir): webinspectloghelper.log_info_updating_webinspect_configurations( etc_dir) check_output(['git', 'init', etc_dir]) check_output([ 'git', '--git-dir=' + git_dir, '--work-tree=' + str(config_helper.git), 'reset', '--hard' ]) check_output([ 'git', '--git-dir=' + git_dir, '--work-tree=' + str(config_helper.git), 'pull', '--rebase' ]) sys.stdout.flush() elif not os.path.exists(git_dir): webinspectloghelper.log_info_webinspect_git_clonning( config_helper.git) check_output([ 'git', 'clone', self.config.webinspect_git, config_helper.git ]) else: Logger.app.error( "No GIT Repo was declared in your config.ini, therefore nothing will be cloned!" ) except (CalledProcessError, AttributeError) as e: webinspectloghelper.log_webinspect_config_issue(e) raise except GitCommandError as e: webinspectloghelper.log_git_access_error( self.config.webinspect_git, e) sys.exit(ExitStatus.failure) except IndexError as e: webinspectloghelper.log_config_file_unavailable(e) sys.exit(ExitStatus.failure) Logger.app.debug("Completed webinspect config fetch") except TypeError as e: webinspectloghelper.log_error_git_cloning_error(e)
def write_secret(self, overwrite=False): secret_path = Config().secret if self.secret_exists() and overwrite: os.chmod(secret_path, 0o200) key = Fernet.generate_key() with open(secret_path, 'w') as secret_file: secret_file.write(key.decode()) os.chmod(secret_path, 0o400) print("New secret has been set.")
def __read_fernet_secret__(self): self.verify_secret() try: with open(Config().secret, 'r') as secret_file: fernet_key = secret_file.readline().strip() return fernet_key except IOError: Logger.console.error("Error retrieving Fernet key.") sys.exit(1)
def test_set_path_file_success(open_mock, makedirs_mock, set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None result = Config().set_path(install='/test/install', file_name='test.file') assert makedirs_mock.call_count == 1 assert open_mock.call_count == 1 assert result == '/test/install/test.file'
def _check_if_authenticate_required(self): config_file = Config().config try: config.read(config_file) return config.get("webinspect", "authenticate").lower() == 'true' except (configparser.NoOptionError, CalledProcessError) as noe: Logger.app.error("{} has incorrect or missing values {}".format( config_file, noe)) except configparser.Error as e: Logger.app.error("Error reading {} {}".format(config_file, e))
def test_set_path_exists(open_mock, makedirs_mock, exist_mock, set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None exist_mock.return_value = True result = Config().set_path(install='/test/install', dir_path='test_dir') assert exist_mock.call_count == 1 assert makedirs_mock.call_count == 0 assert result == '/test/install/test_dir'
def test_conf_get_success(get_mock, read_mock, set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None read_mock.return_value = True get_mock.return_value = 'new_test_value' result = Config().conf_get('test_section', 'test_option', 'test_value') assert read_mock.call_count == 1 assert get_mock.call_count == 1 assert result == 'new_test_value'
def _get_webinspect_settings(): """ Read in webinspect config.ini settings and return it as a dictionary. :return: """ Logger.app.debug("Getting webinspect settings from config file") settings_dict = {} webinspect_config = Config() config_file = webinspect_config.config try: config.read(config_file) endpoints = [] sizes = [] endpoint = re.compile('endpoint_\d*') size = re.compile('size_') for option in config.items('webinspect'): if endpoint.match(option[0]): endpoints.append([option[0], option[1]]) elif size.match(option[0]): sizes.append([option[0], option[1]]) settings_dict['git'] = webinspect_config.conf_get('webinspect', 'git_repo') settings_dict['endpoints'] = [[endpoint[1].split('|')[0], endpoint[1].split('|')[1]] for endpoint in endpoints] settings_dict['size_list'] = sizes settings_dict['mapped_policies'] = [[option, config.get('webinspect_policy', option)] for option in config.options('webinspect_policy')] settings_dict['verify_ssl'] = webinspect_config.conf_get('webinspect', 'verify_ssl') except (configparser.NoOptionError, CalledProcessError) as e: Logger.app.error("{} has incorrect or missing values {}".format(config_file, e)) except configparser.Error as e: Logger.app.error("Error reading webinspect settings {} {}".format(config_file, e)) Logger.app.debug("Initializing webinspect settings from config.ini") return settings_dict
def test_set_path_file_exception(open_mock, makedirs_mock, set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None e = IOError("Test Error") open_mock.side_effect = e result = Config().set_path(install='/test/install', file_name='test.file') assert makedirs_mock.call_count == 1 assert open_mock.call_count == 1 assert result == 1
def __init__(self, cli_overrides): # used for multi threading the _is_available API call self.config = WebInspectConfig() # handle all the overrides if 'git' not in cli_overrides: # it shouldn't be in the overrides, but here for potential future support of cli passed git paths cli_overrides['git'] = Config().git self.scan_overrides = ScanOverrides(cli_overrides) # run the scan self.scan()
def test_config_init_variables(set_vars_mock, set_config_mock): set_vars_mock.return_value = None set_config_mock.return_value = None test_obj = Config() assert os.path.exists(test_obj.home) assert test_obj.install is None assert test_obj.config is None assert test_obj.etc is None assert test_obj.git is None assert test_obj.log is None assert test_obj.agent_json is None assert test_obj.secret is None assert set_config_mock.call_count == 1
def test_conf_get_option_exec(open_mock, write_mock, set_mock, read_mock, set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None e = configparser.NoOptionError("Test Error", "Error Section") read_mock.side_effect = e result = Config().conf_get('test_section', 'test_option', 'test_value') assert read_mock.call_count == 1 assert set_mock.call_count == 1 assert open_mock.call_count == 1 assert write_mock.call_count == 1 assert result == 'test_value'
def parse_emailer_settings(self): emailer_dict = {} config.read(Config().config) try: emailer_dict['smtp_host'] = config.get('emailer', 'smtp_host') emailer_dict['smtp_port'] = config.get('emailer', 'smtp_port') emailer_dict['from_address'] = config.get('emailer', 'from_address') emailer_dict['to_address'] = config.get('emailer', 'to_address') emailer_dict['email_template'] = config.get('emailer', 'email_template') except configparser.NoOptionError: Logger.console.error("{} has incorrect or missing values!".format(self.config)) Logger.console.info("Your scan email notifier is not configured: {}".format(self.config)) return emailer_dict
def _get_proxy_certificate(self, server): path = Config().cert api = WebInspectApi(server, verify_ssl=False, username=self.username, password=self.password) response = api.cert_proxy() APIHelper().check_for_response_errors(response) try: with open(path, 'wb') as f: f.write(response.data) Logger.app.info( 'Certificate has downloaded to\t:\t{}'.format(path)) except UnboundLocalError as e: Logger.app.error('Error saving certificate locally {}'.format(e))
def __init__(self, cli_overrides): # keep track on when the scan starts self.start_time = self._get_time() # used for multi threading the _is_available API call self.config = WebInspectConfig() # handle all the overrides if 'git' not in cli_overrides: # it shouldn't be in the overrides, but here for potential future support of cli passed git paths cli_overrides['git'] = Config().git self._webinspect_git_clone(cli_overrides['settings']) self.scan_overrides = ScanOverrides(cli_overrides) # run the scan self.scan()
#!/usr/bin/env python # -*-coding:utf-8-*- import logging import datetime import sys import os from webbreaker.common.confighelper import Config LOG_PATH = Config().log FORMATTER = logging.Formatter('%(message)s') DATETIME_SUFFIX = datetime.datetime.now().strftime("%m-%d-%Y") APP_LOG = os.path.abspath(os.path.join(LOG_PATH, 'webbreaker-' + DATETIME_SUFFIX + '.log')) DEBUG_LOG = os.path.abspath(os.path.join(LOG_PATH, 'webbreaker-debug-' + DATETIME_SUFFIX + '.log')) STOUT_LOG = os.path.abspath(os.path.join(LOG_PATH, 'webbreaker-out' + DATETIME_SUFFIX + '.log')) def singleton(cls): instances = {} def get_instance(): if cls not in instances: instances[cls] = cls() return instances[cls] return get_instance() def get_console_logger(): try: console_logger = logging.getLogger()
def secret_exists(self): return os.path.isfile(Config().secret)
def __init__(self): self.fernet_key = self.__read_fernet_secret__() self.config_file = Config().config