def write_xml(self): """Function writes XML document to file""" self.target_tree.set('xmlns:xhtml', 'http://www.w3.org/1999/xhtml/') # we really must set encoding here! and suppress it in write_to_file data = ElementTree.tostring(self.target_tree, "utf-8") FileHelper.write_to_file(self.path, 'wb', data, False) self.target_tree = ElementTree.parse(self.path).getroot()
def _copy_xccdf_file(self, update_text=None, update_return_values=None): temp_dir = tempfile.mkdtemp() xccdf_file = os.path.join(os.getcwd(), 'tests', 'generated_results', 'inplace_combined_risk_test.xml') temp_file = os.path.join(temp_dir, 'all-xccdf.xml') shutil.copyfile(xccdf_file, temp_file) content = FileHelper.get_file_content(temp_file, 'rb', decode_flag=False) if update_text: if update_text[0] is not None: new_text = b'preupg.risk.%s: Test %s Inplace risk' % ( update_text[0], update_text[0]) if update_text is not None: content = content.replace(b'INPLACE_TAG1', new_text) else: content = content.replace(b'INPLACE_TAG1', "") if update_text[1] is not None: new_text = b'preupg.risk.%s: Test %s Inplace risk' % ( update_text[1], update_text[1]) if update_text is not None: content = content.replace(b'INPLACE_TAG2', new_text) else: content = content.replace(b'INPLACE_TAG2', "") content = content.replace(b'RESULT_VALUE1', update_return_values[0]) content = content.replace(b'RESULT_VALUE2', update_return_values[1]) FileHelper.write_to_file(temp_file, 'wb', content) return temp_file
def comment_kickstart_issues(self): """Comment out those line in kickstart that are to be reviewed by user and uncommented by themself if necessary. """ list_issues = [ ' --', 'group', 'user ', 'repo', 'url', 'rootpw', 'part', 'volgroup', 'logvol', 'raid' ] kickstart_data = [] try: kickstart_data = FileHelper.get_file_content(os.path.join( settings.KS_DIR, self.kickstart_name), 'rb', method=True, decode_flag=False) except IOError: log_message( "The %s file is missing. The partitioning layout might not be complete." % self.kickstart_name, level=logging.WARNING) return None for index, row in enumerate(kickstart_data): tag = [com for com in list_issues if row.startswith(com)] if tag: kickstart_data[index] = "#" + row FileHelper.write_to_file(self.kickstart_name, 'wb', kickstart_data)
def check_rpm_to(check_rpm="", check_bin=""): """ Function checks if relevant package is installed and if relevant binary exists on the system. Function is needed from module point of view. :param check_rpm: list of RPMs separated by comma :param check_bin: list of binaries separated by comma :return: """ not_applicable = 0 if check_rpm != "": rpms = check_rpm.split(',') lines = FileHelper.get_file_content(VALUE_RPM_QA, "rb", True) for rpm in rpms: lst = [x for x in lines if rpm == x.split('\t')[0]] if not lst: log_high_risk("Package %s is not installed." % rpm) not_applicable = 1 if check_bin != "": binaries = check_bin.split(',') lines = FileHelper.get_file_content(VALUE_EXECUTABLES, "rb", True) for binary in binaries: cmd = "which %s" % binary if ProcessHelper.run_subprocess(cmd, print_output=False, shell=True) != 0: log_high_risk("Binary %s is not installed." % binary) not_applicable = 1 if not_applicable: log_high_risk("Please, install all required packages (and binaries)" " and run preupg again to process check properly.") exit_fail() return not_applicable
def update_check_script(self, updates, author=None): """ The function updates check script with license file and with API functions like check_rpm_to and check_applies_to """ script_type = FileHelper.get_script_type(self.full_path_name) if author is None: author = "<empty_line>" generated_section, functions = ModuleHelper.generate_common_stuff(settings.license % author, updates, script_type) lines = FileHelper.get_file_content(self.full_path_name, "rb", method=True) if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]: MessageHelper.print_error_msg("#END GENERATED SECTION is missing in check_script %s" % self.full_path_name) raise MissingHeaderCheckScriptError for func in functions: lines = [x for x in lines if func not in x.strip()] output_text = "" for line in lines: if '#END GENERATED SECTION' in line: new_line = '\n'.join(generated_section) new_line = new_line.replace('<empty_line>', '').replace('<new_line>', '') output_text += new_line+'\n' if 'check_applies' in updates: component = updates['check_applies'] else: component = "distribution" if script_type == "sh": output_text += 'COMPONENT="'+component+'"\n' else: output_text += 'set_component("'+component+'")\n' output_text += line FileHelper.write_to_file(self.full_path_name, "wb", output_text)
def test_xml_upgrade_not_migrate(self): test_ini = {'content_title': 'Testing only migrate title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'applies_to': 'test', 'requires': 'bash', 'binary_req': 'sed', 'mode': 'upgrade'} ini = {} old_settings = settings.UPGRADE_PATH migrate, upgrade = self._create_temporary_dir() ini[self.filename] = test_ini xml_utils = XmlUtils(self.root_dir_name, self.dirname, ini) xml_utils.prepare_sections() upgrade_file = FileHelper.get_file_content(upgrade, 'rb', method=True) tag = [x.strip() for x in upgrade_file if 'xccdf_preupg_rule_test_check_script' in x.strip()] self.assertIsNotNone(tag) try: migrate_file = FileHelper.get_file_content(migrate, 'rb', method=True) except IOError: migrate_file = None self.assertIsNone(migrate_file) self._delete_temporary_dir(migrate, upgrade) settings.UPGRADE_PATH = old_settings
def setUp(self): self.root_dir_name = "tests/FOOBAR6_7" + settings.results_postfix self.dirname = os.path.join(self.root_dir_name, "test") if os.path.exists(self.dirname): shutil.rmtree(self.dirname) os.makedirs(self.dirname) self.filename = os.path.join(self.dirname, 'test.ini') self.rule = [] self.loaded_ini = {} self.test_ini = {'content_title': 'Testing content title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf' } self.check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ check_name = os.path.join(self.dirname, settings.check_script) FileHelper.write_to_file(check_name, "wb", self.check_sh) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.solution_text = """ A solution text for test suite" """ test_solution_name = os.path.join(self.dirname, settings.solution_txt) FileHelper.write_to_file(test_solution_name, "wb", self.solution_text) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU)
def embed_script(self, tarball): if tarball is None: return tarball_content = None if os.path.exists(tarball): tarball_content = FileHelper.get_file_content(tarball, 'rb', decode_flag=False) tarball_name = os.path.splitext( os.path.splitext(os.path.basename(tarball))[0])[0] script_str = '' try: script_path = settings.KS_TEMPLATE_POSTSCRIPT except AttributeError: log_message( 'KS_TEMPLATE_POSTSCRIPT is not defined in settings.py.') return script_str = FileHelper.get_file_content( os.path.join(settings.KS_DIR, script_path), 'rb') if not script_str: log_message( "Cannot open the script template: {0}.".format(script_path)) return if tarball_content is not None: script_str = script_str.replace('{tar_ball}', base64.b64encode(tarball_content)) script_str = script_str.replace('{RESULT_NAME}', tarball_name) script_str = script_str.replace('{TEMPORARY_PREUPG_DIR}', '/root/preupgrade') script = Script(script_str, type=KS_SCRIPT_POST, inChroot=True) self.ks.handler.scripts.append(script)
def setUp(self): self.root_dir_name = "tests/FOOBAR6_7" self.dir_name = os.path.join(self.root_dir_name, "incorrect_ini") os.makedirs(self.dir_name) self.filename = os.path.join(self.dir_name, 'test.ini') self.rule = [] self.test_ini = {'content_title': 'Testing content title', 'content_description': 'Some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'applies_to': 'test' } solution_text = """ A solution text for test suite" """ check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ FileHelper.write_to_file(os.path.join( self.dir_name, settings.solution_txt), "wb", solution_text) FileHelper.write_to_file(os.path.join( self.dir_name, settings.check_script), "wb", check_sh)
def update_check_script(self, updates, author=None): """ The function updates check script with license file and with API functions like check_rpm_to and check_applies_to """ script_type = FileHelper.get_script_type(self.check_script_path) if author is None: author = "<empty_line>" generated_section, functions = ModuleHelper.generate_common_stuff( settings.license % author, updates, script_type) lines = FileHelper.get_file_content(self.check_script_path, "rb", method=True) if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]: raise MissingHeaderCheckScriptError(self.check_script_path) for func in functions: lines = [x for x in lines if func not in x.strip()] output_text = "" for line in lines: if '#END GENERATED SECTION' in line: new_line = '\n'.join(generated_section) new_line = new_line.replace('<empty_line>', '').replace('<new_line>', '') output_text += new_line + '\n' output_text += line FileHelper.write_to_file(self.check_script_path, "wb", output_text)
def setUp(self): self.root_dir_name = "tests/FOOBAR6_7" + settings.results_postfix self.dirname = os.path.join(self.root_dir_name, "test") if os.path.exists(self.dirname): shutil.rmtree(self.dirname) os.makedirs(self.dirname) self.filename = os.path.join(self.dirname, 'test.ini') self.rule = [] self.loaded_ini = {} self.test_ini = { 'content_title': 'Testing content title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf' } self.check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ check_name = os.path.join(self.dirname, settings.check_script) FileHelper.write_to_file(check_name, "wb", self.check_sh) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.solution_text = """ A solution text for test suite" """ test_solution_name = os.path.join(self.dirname, settings.solution_txt) FileHelper.write_to_file(test_solution_name, "wb", self.solution_text) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU)
def setUp(self): self.dir_name = "tests/FOOBAR6_7/incorrect_ini" os.makedirs(self.dir_name) self.filename = os.path.join(self.dir_name, 'test.ini') self.rule = [] self.test_solution = "test_solution.sh" self.check_script = "check_script.sh" self.loaded_ini[self.filename] = [] self.test_ini = { 'content_title': 'Testing content title', 'content_description': 'Some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'check_script': self.check_script, 'solution': self.test_solution, 'applies_to': 'test' } solution_text = """ A solution text for test suite" """ check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ FileHelper.write_to_file( os.path.join(self.dir_name, self.test_solution), "wb", solution_text) FileHelper.write_to_file( os.path.join(self.dir_name, self.check_script), "wb", check_sh)
def test_xml_not_migrate_not_upgrade(self): test_ini = { 'content_title': 'Testing only migrate title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'check_script': self.check_script, 'solution': self.test_solution, 'applies_to': 'test', 'requires': 'bash', 'binary_req': 'sed' } ini = {} old_settings = settings.UPGRADE_PATH migrate, upgrade = self._create_temporary_dir() ini[self.filename] = [] ini[self.filename].append(test_ini) xml_utils = XmlUtils(self.dirname, ini) xml_utils.prepare_sections() migrate_file = FileHelper.get_file_content(migrate, 'rb', method=True) tag = [ x.strip() for x in migrate_file if 'xccdf_preupg_rule_test_check_script' in x.strip() ] self.assertIsNotNone(tag) upgrade_file = FileHelper.get_file_content(upgrade, 'rb', method=True) tag = [ x.strip() for x in upgrade_file if 'xccdf_preupg_rule_test_check_script' in x.strip() ] self.assertIsNotNone(tag) self._delete_temporary_dir(migrate, upgrade) settings.UPGRADE_PATH = old_settings
def clean_html(report_path): """ Function cleans a report """ file_content = FileHelper.get_file_content(report_path, 'rb') s_testres = ( '[\t ]*<div id="intro">[\t ]*\n[\t ]*<h2>Introduction</h2>[\t ]*\n', False) e_testres = ( '[\t ]*</table>[\t ]*\n[\t ]*</div>[\t ]*\n[\t ]*</div>[\t ]*\n', False) s_score = ('[\t ]*<div>[\t ]*\n[\t ]*<h3>Score</h3>\s*', True) e_score = ('[\t ]*</div>[\t ]*\n[\t ]*<div id="results-overview">[\t ]*\n', False) # remove test results nl = remove_lines(file_content, s_testres, e_testres) # remove score table #nl = remove_lines(nl, s_score, e_score) # sed XCCDF test results nl = re.sub('XCCDF test result', 'Preupgrade Assistant', nl) # add preupg nvr nl = re.sub('[\t ]*<h2>Introduction</h2>[\t ]*\n', add_preupg_scanner_info(), nl) FileHelper.write_to_file(report_path, 'wb', nl)
def test_add_pkg_to_kickstart(self): expected_list = ['my_foo_pkg', 'my_bar_pkg'] script_api.add_pkg_to_kickstart(['my_foo_pkg', 'my_bar_pkg']) for pkg in FileHelper.get_file_content(script_api.SPECIAL_PKG_LIST, 'rb', method=True): self.assertTrue(pkg.strip() in expected_list) script_api.add_pkg_to_kickstart('my_foo_pkg my_bar_pkg') for pkg in FileHelper.get_file_content(script_api.SPECIAL_PKG_LIST,'rb', method=True): self.assertTrue(pkg.strip() in expected_list)
def test_secret_check_script(self): """Check occurrence of secret file for check script""" self.test_ini['check_script'] = '.minicheck' text = """#!/usr/bin/sh\necho 'ahojky'\n""" FileHelper.write_to_file(os.path.join(self.dir_name, self.check_script), "wb", text) self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingFileInContentError, lambda: list(self.xml_utils.prepare_sections()))
def test_hashes(self): text_to_hash=""" This is preupgrade assistant test has string" """ self.dir_name = "tests/hashes" os.mkdir(self.dir_name) FileHelper.write_to_file(os.path.join(self.dir_name, "post_script"), 'wb', text_to_hash) PostupgradeHelper.hash_postupgrade_file(False, self.dir_name) return_value = PostupgradeHelper.hash_postupgrade_file(False, self.dir_name, check=True) self.assertTrue(return_value)
def test_secret_check_script(self): """Check occurrence of secret file for check script""" self.test_ini['check_script'] = '.minicheck' text = """#!/usr/bin/sh\necho 'ahojky'\n""" FileHelper.write_to_file( os.path.join(self.dir_name, self.check_script), "wb", text) self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingFileInContentError, lambda: list(self.xml_utils.prepare_sections()))
def write_xml(self): """The function is used for storing a group.xml file""" self.find_all_ini() self.write_list_rules() xml_utils = XmlUtils(self.dirname, self.loaded) self.rule = xml_utils.prepare_sections() file_name = os.path.join(self.dirname, "group.xml") try: FileHelper.write_to_file(file_name, "wb", ["%s" % item for item in self.rule]) except IOError as ior: print ('Problem with write data to the file ', file_name, ior.message)
def write_profile_xml(self, target_tree): """The function stores all-xccdf.xml file into content directory""" file_name = os.path.join(self.dirname, "all-xccdf.xml") print ('File which can be used by Preupgrade-Assistant is:\n', ''.join(file_name)) try: # encoding must be set! otherwise ElementTree return non-ascii characters # as html entities instead, which are unsusable for us data = ElementTree.tostring(target_tree, "utf-8") FileHelper.write_to_file(file_name, "wb", data, False) except IOError as ioe: print ('Problem with writing to file ', file_name, ioe.message)
def test_incorrect_tag(self): """ Check occurrence of incorrect tag Tests issue #30 """ text_ini = '[preupgrade]\n' text_ini += '\n'.join([key + " = " + self.test_ini[key] for key in self.test_ini]) text_ini += '\n[]\neliskk\n' FileHelper.write_to_file(self.filename, "wb", text_ini) oscap = OscapGroupXml(self.root_dir_name, self.dir_name) self.assertRaises(configparser.ParsingError, oscap.find_all_ini)
def _remove_obsolete_data(self): if os.path.exists(KickstartGenerator.get_kickstart_path( self.dir_name)): lines = FileHelper.get_file_content( KickstartGenerator.get_kickstart_path(self.dir_name), "r", method=True) lines = [x for x in lines if not x.startswith('key')] FileHelper.write_to_file( KickstartGenerator.get_kickstart_path(self.dir_name), "w", lines)
def test_incorrect_tag(self): """ Check occurrence of incorrect tag Tests issue #30 """ text_ini = '[preupgrade]\n' text_ini += '\n'.join( [key + " = " + self.test_ini[key] for key in self.test_ini]) text_ini += '\n[]\neliskk\n' FileHelper.write_to_file(self.filename, "wb", text_ini) oscap = OscapGroupXml(self.dir_name) self.assertRaises(SystemExit, oscap.find_all_ini)
def write_profile_xml(self, target_tree): """The function stores all-xccdf.xml file into content directory""" file_name = os.path.join(self.dirname, "all-xccdf.xml") print('File which can be used by Preupgrade-Assistant is:\n', ''.join(file_name)) try: # encoding must be set! otherwise ElementTree return non-ascii characters # as html entities instead, which are unsusable for us data = ElementTree.tostring(target_tree, "utf-8") FileHelper.write_to_file(file_name, "wb", data, False) except IOError as ioe: print('Problem with writing to file ', file_name, ioe.message)
def _create_check_script(self): if self.check_script: if self.script_type == "sh": content = settings.temp_bash_script else: content = settings.temp_python_script FileHelper.write_to_file( os.path.join(self.get_content_path(), self.get_check_script()), 'wb', content) os.chmod( os.path.join(self.get_content_path(), self.get_check_script()), 0755)
def test_add_pkg_to_kickstart(self): expected_list = ['my_foo_pkg', 'my_bar_pkg'] script_api.add_pkg_to_kickstart(['my_foo_pkg', 'my_bar_pkg']) for pkg in FileHelper.get_file_content(script_api.SPECIAL_PKG_LIST, 'rb', method=True): self.assertTrue(pkg.strip() in expected_list) script_api.add_pkg_to_kickstart('my_foo_pkg my_bar_pkg') for pkg in FileHelper.get_file_content(script_api.SPECIAL_PKG_LIST, 'rb', method=True): self.assertTrue(pkg.strip() in expected_list)
def update_report(self, report_path): """Update XML or HTML report with relevant solution texts.""" if not self.solution_texts: self.load_solution_texts() orig_file = os.path.join(self.assessment_result_path, report_path) report_content = FileHelper.get_file_content(orig_file, "rb") for solution_placeholer, solution_text in self.solution_texts.items(): report_content = report_content.replace(solution_placeholer, solution_text) FileHelper.write_to_file(orig_file, "wb", report_content)
def write_xccdf_version(file_name, direction=False): """ Function updates XCCDF version because of separate HTML generation and our own XSL stylesheet """ namespace_1 = 'http://checklists.nist.gov/xccdf/1.1' namespace_2 = 'http://checklists.nist.gov/xccdf/1.2' content = FileHelper.get_file_content(file_name, "rb") if direction: content = re.sub(namespace_2, namespace_1, content) else: content = re.sub(namespace_1, namespace_2, content) FileHelper.write_to_file(file_name, 'wb', content)
def _create_check_script(self): if self.check_script: if self.script_type == "sh": content = settings.temp_bash_script else: content = settings.temp_python_script FileHelper.write_to_file(os.path.join(self.get_content_path(), self.get_check_script()), 'wb', content) os.chmod(os.path.join(self.get_content_path(), self.get_check_script()), 0755)
def write_xml(self): """The function is used for storing a group.xml file""" self.find_all_ini() self.write_list_rules() xml_utils = XmlUtils(self.module_set_dir, self.dirname, self.loaded) self.rule = xml_utils.prepare_sections() file_name = os.path.join(self.dirname, "group.xml") try: FileHelper.write_to_file(file_name, "wb", ["%s" % item for item in self.rule]) except IOError as ior: raise IOError('Problem with writing to file %s.\nDetails: %s' % (file_name, ior.message))
def check_scripts(self, type_name): """ The function checks whether script exists in content directory If check_script exists then the script checks whether it is executable """ if not os.path.exists(self.full_path_name): print ("ERROR: ", self.full_path_name, "Script name does not exists") print ("List of directory (", self.dir_name, ") is:") for file_name in os.listdir(self.dir_name): print (file_name) raise MissingFileInContentError if type_name != 'solution': FileHelper.check_executable(self.full_path_name)
def solution_file(message): """ Function appends a message to solution file. solution file will be created in module directory :param message: Message - string of list of strings :return: """ if os.path.exists(SOLUTION_FILE): mod = "a+b" else: mod = "wb" FileHelper.write_to_file(SOLUTION_FILE, mod, message)
def check_scripts(self, type_name): """ The function checks whether script exists in content directory If check_script exists then the script checks whether it is executable """ if not os.path.exists(self.full_path_name): print("ERROR: ", self.full_path_name, "Script name does not exists") print("List of directory (", self.dir_name, ") is:") for file_name in os.listdir(self.dir_name): print(file_name) raise MissingFileInContentError if type_name != 'solution': FileHelper.check_executable(self.full_path_name)
def _copy_xccdf_file(self, update_text=None, update_return_value=None): temp_dir = tempfile.mkdtemp() xccdf_file = os.path.join(os.getcwd(), "tests", "generated_results", "inplace_risk_test.xml") temp_file = os.path.join(temp_dir, "all-xccdf.xml") shutil.copyfile(xccdf_file, temp_file) content = FileHelper.get_file_content(temp_file, "rb", decode_flag=False) new_text = b"preupg.risk.%s: Test %s Inplace risk" % (update_text, update_text) if update_text is not None: content = content.replace(b"INPLACE_TAG", new_text) else: content = content.replace(b"INPLACE_TAG", "") content = content.replace(b"RESULT_VALUE", update_return_value) FileHelper.write_to_file(temp_file, "wb", content) return temp_file
def write_list_rules(self): module_path = self.dirname.replace(self.module_set_dir, '') rule_name = '_'.join(module_path.split(os.sep)[1:]) file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules) lines = [] if os.path.exists(file_list_rules): lines = FileHelper.get_file_content(file_list_rules, "rb", method=True) # add rule only for modules (dir which contains module.ini) if os.path.isfile(os.path.join(self.dirname, settings.module_ini)): lines.append(settings.xccdf_tag + rule_name + '_check' + '\n') FileHelper.write_to_file(file_list_rules, "wb", lines)
def solution_file(message): """ Function appends a message to solution file. solution file will be created in module directory :param message: Message - string of list of strings :return: """ solution_filename = os.path.join(VALUE_CURRENT_DIRECTORY, SOLUTION_FILE) if os.path.exists(solution_filename): mod = "a+b" else: mod = "wb" FileHelper.write_to_file(solution_filename, mod, message)
def update_files(self, file_name, content): """Function updates file_name <migrate or update> according to INI file.""" """ :param file_name: specified in INI file like mode: upgrade, migrate :param content: name of the content like xccdf_rule_... :return: Nothing """ path_name = os.path.join(settings.UPGRADE_PATH, file_name) lines = [] if os.path.exists(path_name): lines = FileHelper.get_file_content(path_name, 'rb', method=True) test_content = [x.strip() for x in lines if content in x.strip()] if not test_content: lines.append(content + '\n') FileHelper.write_to_file(path_name, 'wb', lines)
def write_list_rules(self): end_point = self.dirname.find(SystemIdentification.get_valid_scenario(self.dirname)) rule_name = '_'.join(self.dirname[end_point:].split('/')[1:]) file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules) lines = [] if os.path.exists(file_list_rules): lines = FileHelper.get_file_content(file_list_rules, "rb", method=True) else: lines = [] for values in six.itervalues(self.loaded): check_script = [v for k, v in six.iteritems(values[0]) if k == 'check_script'] if check_script: check_script = os.path.splitext(''.join(check_script))[0] lines.append(settings.xccdf_tag + rule_name + '_' + check_script + '\n') FileHelper.write_to_file(file_list_rules, "wb", lines)
def _update_check_description(self, filename): new_text = [] lines = FileHelper.get_file_content(os.path.join(self.module_dir, filename), "rb", True) bold = '<xhtml:b>{0}</xhtml:b>' br = '<xhtml:br/>' table_begin = '<xhtml:table>' table_end = '</xhtml:table>' table_header = '<xhtml:tr><xhtml:th>Result</xhtml:th><xhtml:th>' \ 'Description</xhtml:th></xhtml:tr>' table_row = '<xhtml:tr><xhtml:td>{0}</xhtml:td><xhtml:td>{1}' \ '</xhtml:td></xhtml:tr>' new_text.append(br + br + '\n' + bold.format('Details:') + br) results = False for line in lines: if '=' in line: if not results: new_text.append(bold.format('Expected results:') + br) new_text.append(table_begin + '\n' + table_header) results = True try: exp_results = line.strip().split('=') new_text.append(table_row.format(exp_results[0], exp_results[1]) + '\n') except IndexError: pass else: new_text.append(line.rstrip() + br) if results: new_text.append(table_end + '\n') return '\n'.join(new_text)
def update_values_list(self, section, search_exp, replace_exp): """ The function replaces tags taken from INI files. Tags are mentioned in xml_tags.py """ forbidden_empty = ["{scap_name}", "{main_dir}"] if search_exp == "{content_description}": replace_exp = replace_exp.rstrip() elif search_exp == "{check_description}": replace_exp = '\n' + replace_exp + '\n' elif search_exp == "{config_file}": new_text = "" for lines in replace_exp.split(','): new_text = new_text + "<xhtml:li>" + lines.strip( ) + "</xhtml:li>" replace_exp = new_text.rstrip() elif search_exp == "{solution}": new_text = FileHelper.get_file_content( os.path.join(self.dirname, replace_exp), "rb", True) # we does not need interpreter for fix script # in XML therefore skip first line replace_exp = ''.join(new_text[1:]) elif search_exp == "{solution_text}": new_text = "_" + '_'.join(get_full_xml_tag(self.dirname))\ + "_SOLUTION_MSG_" + replace_exp.upper() replace_exp = new_text if replace_exp == '' and search_exp in forbidden_empty: MessageHelper.print_error_msg( title="Disapproved empty replacement for tag '%s'" % search_exp) raise EmptyTagIniFileError for cnt, line in enumerate(section): if search_exp in line: section[cnt] = line.replace(search_exp, replace_exp)
def get_package_list(filename, field=None): """ content packages/ReplacedPackages is taking care of packages, which were replaced/obsoleted/removed between releases. It produces a file with a list of packages which should be installed. """ full_path_name = os.path.join(settings.KS_DIR, filename) if not os.path.exists(full_path_name): return [] lines = FileHelper.get_file_content(full_path_name, 'rb', method=True, decode_flag=True) # Remove newline character from list package_list = [] for line in lines: # We have to go over all lines and remove all commented. if line.startswith('#'): continue if field is None: package_list.append(line.strip()) else: try: # Format of file is like # old-package|required-by-pkgs|replaced-by-pkgs|repoid pkg_field = line.split('|') if pkg_field[field] is not None: package_list.append(pkg_field[field]) except ValueError: # Line seems to be wrong, go to the next one pass return package_list
def update_files(self, file_name, content): """Function updates file_name <migrate or update> according to INI file. :param file_name: specified in INI file like mode: upgrade, migrate :param content: name of the content like xccdf_rule_... :return: Nothing """ path_name = os.path.join(settings.UPGRADE_PATH, file_name) lines = [] if os.path.exists(path_name): lines = FileHelper.get_file_content(path_name, 'rb', method=True) test_content = [x.strip() for x in lines if content in x.strip()] if not test_content: lines.append(content + '\n') FileHelper.write_to_file(path_name, 'wb', lines)
def _return_check(self, text): content = FileHelper.get_file_content(os.path.join( self.dirname, self.check_script), "rb", method=True) found = [x for x in content if x.startswith(text)] return found
def load_file(filename): try: lines = FileHelper.get_file_content(filename, "rb", True) lines = [x.strip() for x in lines] except IOError: assert False return lines
def _update_check_description(self, filename): new_text = [] lines = FileHelper.get_file_content( os.path.join(self.dirname, filename), "rb", True) bold = '<xhtml:b>{0}</xhtml:b>' br = '<xhtml:br/>' table_begin = '<xhtml:table>' table_end = '</xhtml:table>' table_header = '<xhtml:tr><xhtml:th>Result</xhtml:th><xhtml:th>Description</xhtml:th></xhtml:tr>' table_row = '<xhtml:tr><xhtml:td>{0}</xhtml:td><xhtml:td>{1}</xhtml:td></xhtml:tr>' new_text.append(br + br + '\n' + bold.format('Details:') + br) results = False for line in lines: if '=' in line: if not results: new_text.append(bold.format('Expected results:') + br) new_text.append(table_begin + '\n' + table_header) results = True try: exp_results = line.strip().split('=') new_text.append( table_row.format(exp_results[0], exp_results[1]) + '\n') except IndexError: pass else: new_text.append(line.rstrip() + br) if results: new_text.append(table_end + '\n') return '\n'.join(new_text)
def get_kickstart_groups(filename, splitter=":"): """ returns dictionary with names and uid, gid, etc. :param filename: filename with Users in /root/preupgrade/kickstart directory :param splitter: delimiter for parsing files :return: dictionary with users """ try: lines = FileHelper.get_file_content(os.path.join( settings.KS_DIR, filename), 'rb', method=True) except IOError: return None lines = [ x for x in lines if not x.startswith('#') and not x.startswith(' ') ] group_dict = {} for line in lines: fields = line.split(splitter) try: group_dict[fields[0]] = {} group_dict[fields[0]] = { fields[2]: fields[3].strip().split(',') } except IndexError: pass return group_dict
def update_platform(full_path): file_lines = FileHelper.get_file_content(full_path, 'rb', method=True) platform = '' platform_id = '' if not SystemIdentification.get_system(): platform = settings.CPE_RHEL else: platform = settings.CPE_FEDORA platform_id = SystemIdentification.get_assessment_version(full_path) for index, line in enumerate(file_lines): if 'PLATFORM_NAME' in line: line = line.replace('PLATFORM_NAME', platform) if 'PLATFORM_ID' in line: line = line.replace('PLATFORM_ID', platform_id[0]) file_lines[index] = line FileHelper.write_to_file(full_path, 'wb', file_lines)
def get_kickstart_users(filename, groups=None, splitter=":"): """ returns dictionary with names and uid, gid, etc. :param filename: filename with Users in /root/preupgrade/kickstart directory :param groups: dictionary with groups :param splitter: delimiter for parsing files :return: dictionary with users """ try: lines = FileHelper.get_file_content(os.path.join(settings.KS_DIR, filename), 'rb', method=True) except IOError: return None lines = [x for x in lines if not x.startswith('#') and not x.startswith(' ')] user_dict = {} for line in lines: fields = line.strip().split(splitter) try: user_group = [] if groups: for key, value in six.iteritems(groups): found = [x for x in six.itervalues(value) if fields[0] in x] if found: user_group.append(key) user_dict[fields[0]] = {} user_dict[fields[0]] = {'homedir': fields[5], 'shell': fields[6], 'uid': int(fields[2]), 'gid': int(fields[3]), 'groups': user_group} except IndexError: pass return user_dict
def output_packages(self): """ outputs %packages section """ self.packages = PackagesHandling.get_installed_packages() if self.packages is None: return None, None try: self.obsoleted = PackagesHandling.get_package_list('RHRHEL7rpmlist_obsoleted') except IOError: self.obsoleted = [] try: self.special_pkg_list = PackagesHandling.get_package_list('special_pkg_list') except IOError: self.special_pkg_list = [] self.installed_dependencies = PackagesHandling.get_installed_dependencies(self.obsoleted) self.installed_dependencies = list(set(self.installed_dependencies)) self.installed_dependencies.sort() # remove files which are replaced by another package self.replace_obsolete() removed_packages = [] remove_pkg_optional = os.path.join(settings.KS_DIR, 'RemovedPkg-optional') if os.path.exists(remove_pkg_optional): try: removed_packages = FileHelper.get_file_content(remove_pkg_optional, 'r', method=True) except IOError: return None, None # TODO We should think about if ObsoletedPkg-{required,optional} should be used if not removed_packages: return None, None abs_fps = [os.path.join(settings.KS_DIR, fp) for fp in settings.KS_FILES] ygg = YumGroupGenerator(self.packages, removed_packages, self.installed_dependencies, *abs_fps) groups, self.packages, missing_installed = ygg.get_list() self.packages = ygg.remove_packages(self.packages) return groups, missing_installed
def test_check_script_author(self): settings.autocomplete = True self.rule = self.xml_utils.prepare_sections() lines = FileHelper.get_file_content(os.path.join( self.dirname, settings.check_script), "rb", method=True) author = [x for x in lines if "test <*****@*****.**>" in x] self.assertTrue(author)