def run_stage_remediation_bash(run_type, test_env, formatting, verbose_path): """ Returns False on error, or True in case of successful bash scripts run.""" formatting['output_template'] = _BASH_TEMPLATE send_arf_to_remote_machine_and_generate_remediations_there( run_type, test_env, formatting, verbose_path) if not get_file_remote(test_env, verbose_path, LogHelper.LOG_DIR, '/' + formatting['output_file']): return False command_string = '/bin/bash -x /{output_file}'.format(** formatting) with open(verbose_path, "a") as log_file: error_msg_template = ( 'Bash remediation for {rule_id} '.format(** formatting) + 'has exited with these errors: {stderr}' ) try: test_env.execute_ssh_command( command_string, log_file, error_msg_template=error_msg_template) except Exception as exc: LogHelper.preload_log(logging.ERROR, str(exc), 'fail') return False return True
def _apply_script(rule_dir, test_env, script): """Run particular test script on VM and log it's output.""" logging.debug("Applying script {0}".format(script)) rule_name = os.path.basename(rule_dir) log_file_name = os.path.join(LogHelper.LOG_DIR, rule_name + ".prescripts.log") with open(log_file_name, 'a') as log_file: log_file.write('##### {0} / {1} #####\n'.format(rule_name, script)) shared_dir = os.path.join(common.REMOTE_TEST_SCENARIOS_DIRECTORY, "shared") command = "cd {0}; SHARED={1} bash -x {2}".format( rule_dir, shared_dir, script) try: error_msg_template = ( "Rule '{rule_name}' test setup script '{script}' " "failed with exit code {{rc}}".format(rule_name=rule_name, script=script)) test_env.execute_ssh_command(command, log_file, error_msg_template=error_msg_template) except RuntimeError as exc: logging.error(str(exc)) return False return True
def generate_fixes_remotely(test_env, formatting, verbose_path): command_base = ['oscap', 'xccdf', 'generate', 'fix'] command_options = [ '--benchmark-id', formatting['benchmark_id'], '--profile', formatting['profile'], '--template', formatting['output_template'], '--output', '/{output_file}'.format(** formatting), ] command_operands = ['/{source_arf_basename}'.format(** formatting)] if 'result_id' in formatting: command_options.extend(['--result-id', formatting['result_id']]) command_components = command_base + command_options + command_operands command_string = ' '.join([single_quote_string(c) for c in command_components]) with open(verbose_path, "a") as log_file: test_env.execute_ssh_command(command_string, log_file)
def _apply_script(rule_dir, test_env, script): """Run particular test script on VM and log it's output.""" logging.debug("Applying script {0}".format(script)) rule_name = os.path.basename(rule_dir) log_file_name = os.path.join( LogHelper.LOG_DIR, rule_name + ".prescripts.log") with open(log_file_name, 'a') as log_file: log_file.write('##### {0} / {1} #####\n'.format(rule_name, script)) shared_dir = os.path.join(common.REMOTE_TEST_SCENARIOS_DIRECTORY, "shared") command = "cd {0}; SHARED={1} bash -x {2}".format(rule_dir, shared_dir, script) try: test_env.execute_ssh_command(command, log_file) except subprocess.CalledProcessError as exc: logging.error("Rule testing script {script} failed with exit code {rc}" .format(script=script, rc=exc.returncode)) return False return True
def run_stage_remediation_bash(run_type, test_env, formatting, verbose_path): """ Returns False on error, or True in case of successful bash scripts run.""" formatting['output_template'] = _BASH_TEMPLATE send_arf_to_remote_machine_and_generate_remediations_there( run_type, test_env, formatting, verbose_path) if not get_file_remote(test_env, verbose_path, LogHelper.LOG_DIR, '/' + formatting['output_file']): return False command_string = '/bin/bash -x /{output_file}'.format(**formatting) with open(verbose_path, "a") as log_file: try: test_env.execute_ssh_command(command_string, log_file) except Exception as exc: msg = ( 'Bash script remediation run has exited with return code {} ' 'instead of expected 0'.format(exc.returncode)) LogHelper.preload_log(logging.ERROR, msg, 'fail') return False return True