コード例 #1
0
 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 = 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 = 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
コード例 #2
0
    def update_html(self, result_name, solution_files, extension="html"):
        """
         Function updates a XML or HTML file with relevant solution
         texts
        """
        orig_file = os.path.join(self.dirname, result_name + "." + extension)
        lines = get_file_content(orig_file, "rb", method=True)
        for dir_name, files in six.iteritems(solution_files):
            section = dir_name.replace(os.path.join(self.dirname, self.scenario), "").replace("/", "_")
            solution_text = section + "_SOLUTION_MSG"

            file_name = self._return_correct_text_file(section, files)
            if not file_name or file_name is None:
                continue
            else:
                text = get_file_content(os.path.join(dir_name, file_name), "rb", method=True)
            for cnt, line in enumerate(lines):
                # If in INPLACERISK: is a [link] then update them
                # to /root/pre{migrate,upgrade}/...
                if "INPLACERISK:" in line.strip():
                    lines[cnt] = tag_formating([line], extension)[0]
                    continue
                # Find correct block
                if solution_text not in line.strip():
                    continue
                # Get updated text if it is HTML or TEXT
                lines[cnt] = self.get_updated_text(solution_text, text, line, extension)

        if extension == "xml":
            for cnt, line in enumerate(lines):
                if "SOLUTION_MSG" in line.strip():
                    lines[cnt] = re.sub(r">.*SOLUTION_MSG.*<", "><", line.strip())

        write_to_file(orig_file, "wb", lines)
コード例 #3
0
    def _update_check_description(self, filename):
        new_text = []
        lines = get_file_content(os.patch.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 index, line in enumerate(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)
コード例 #4
0
def get_script_type(dir_name, script_name=""):
    """
    The function returns type of check_script.
    If it's not any script then return just txt
    """
    mime_type = mimetypes.guess_type(get_full_path(dir_name, script_name))[0]
    if mime_type is None:
        # try get mime type with shebang
        line = get_file_content(get_full_path(dir_name, script_name), "rb",
                                True)[0]
        if line.startswith("#!"):
            if re.search(r"\bpython[0-9.-]*\b", line):
                return 'python'
            if re.search(r"\bperl[0-9.-]*\b", line):
                return 'perl'
            if re.search(r"\bcsh\b", line):
                return 'csh'
            if re.search(r"\b(bash|sh)\b", line):
                return 'sh'
    file_types = {
        'text/x-python': 'python',
        'application/x-csh': 'csh',
        'application/x-sh': 'sh',
        'application/x-perl': 'perl',
        'text/plain': 'txt',
        None: 'txt',
    }
    return file_types[mime_type]
コード例 #5
0
def check_inplace_risk(xccdf_file, verbose):
    """
    The function read the content of the file
    and finds out all INPLACERISK rows in TestResult tree.
    return value is get from function get_and_print_inplace_risk
    """
    try:
        content = utils.get_file_content(xccdf_file, 'rb', False, False)
        if not content:
            # WE NEED TO RETURN -1 FOR RED-HAT-UPGRADE-TOOL
            return -1
    except IOError:
        # WE NEED TO RETURN -1 FOR RED-HAT-UPGRADE-TOOL
        return -1

    inplace_risk = []
    target_tree = ElementTree.fromstring(content)
    for profile in target_tree.findall(XMLNS + "TestResult"):
        inplace_risk = get_check_import_inplace_risk(profile)

    result = get_and_print_inplace_risk(verbose, inplace_risk)
    # different behaviour of division between py2 & 3
    if (result == -1):
        return -1
    elif (result < 2):
        return 0
    elif (result < 4):
        return 1
    else:
        return 2
コード例 #6
0
def get_script_type(dir_name, script_name=""):
    """
    The function returns type of check_script.
    If it's not any script then return just txt
    """
    mime_type = mimetypes.guess_type(get_full_path(dir_name, script_name))[0]
    if mime_type is None:
        # try get mime type with shebang
        line = get_file_content(get_full_path(dir_name, script_name), "rb", True)[0]
        if line.startswith("#!"):
            if re.search(r"\bpython[0-9.-]*\b", line):
                return 'python'
            if re.search(r"\bperl[0-9.-]*\b", line):
                return 'perl'
            if re.search(r"\bcsh\b", line):
                return 'csh'
            if re.search(r"\b(bash|sh)\b", line):
                return 'sh'
    file_types = {'text/x-python': 'python',
                  'application/x-csh': 'csh',
                  'application/x-sh': 'sh',
                  'application/x-perl': 'perl',
                  'text/plain': 'txt',
                  None: 'txt',
                  }
    return file_types[mime_type]
コード例 #7
0
def get_hashes(filename):
    """Function gets all hashes from a filename"""
    if not os.path.exists(filename):
        return None
    hashed_file = get_file_content(filename, "rb").split()
    hashed_file = [x for x in hashed_file if "hashed_file" not in x]
    return hashed_file
コード例 #8
0
def update_check_script(dir_name, updates, script_name=None, author=""):
    """
    The function updates check script with license file
    and with API functions like check_rpm_to and check_applies_to
    """
    script_type = get_script_type(dir_name, script_name)
    if author == "":
        author = "<empty_line>"
    generated_section, functions = generate_common_stuff(settings.license.format(author),
                                                         updates,
                                                         script_type)
    full_path_script = get_full_path(dir_name, script_name)
    lines = get_file_content(full_path_script, "rb", method=True)
    if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]:
        print_error_msg("#END GENERATED SECTION is missing in check_script {0}".format(full_path_script))
        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
    write_to_file(full_path_script, "wb", output_text)
コード例 #9
0
 def get_partition_layout(self, lsblk, vgs, lvdisplay):
     """
     Returns dictionary with partition and realname and size
     :param filename:  filename with partition_layout in /root/preupgrade/kickstart directory
     :return: dictionary with layout
     """
     lsblk_filename = os.path.join(settings.KS_DIR, lsblk)
     try:
         layout = get_file_content(lsblk_filename,
                                   'rb',
                                   method=True,
                                   decode_flag=False)
     except IOError:
         log_message(
             "File %s was not generated by a content. Kickstart does not contain partitioning layout"
             % lsblk_filename)
         self.part_layout = None
         return None
     vg_info = []
     lv_info = []
     if vgs is not None:
         vg_info = KickstartGenerator.get_volume_info(vgs, 0, 5)
     if lvdisplay is not None:
         lv_info = KickstartGenerator.get_volume_info(lvdisplay, 0, 1)
     pg = PartitionGenerator(layout, vg_info, lv_info)
     pg.generate_partitioning()
     self.part_layout.extend(pg.get_partitioning())
コード例 #10
0
    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 = 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:
            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)
コード例 #11
0
 def __init__(self, conf):
     self.conf = conf
     self.cwd = ""
     self.lines = utils.get_file_content(self.conf.common_script,
                                         "rb",
                                         method=True)
     self.common_result_dir = ""
コード例 #12
0
def load_file(filename):
    try:
        lines = utils.get_file_content(filename, "rb", True)
        lines = [x.strip() for x in lines]
    except IOError:
        assert False
    return lines
コード例 #13
0
    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 = 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:
            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)
コード例 #14
0
def clean_html(report_path):
    """
    Function cleans a report
    """
    file_content = 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)

    write_to_file(report_path, 'wb', nl)
コード例 #15
0
def load_file(filename):
    try:
        lines = utils.get_file_content(filename, "rb", True)
        lines = [x.strip() for x in lines]
    except IOError:
        assert False
    return lines
コード例 #16
0
def check_inplace_risk(xccdf_file, verbose):
    """
    The function read the content of the file
    and finds out all INPLACERISK rows in TestResult tree.
    return value is get from function get_and_print_inplace_risk
    """
    try:
        content = utils.get_file_content(xccdf_file, 'rb', False, False)
        if not content:
            # WE NEED TO RETURN -1 FOR RED-HAT-UPGRADE-TOOL
            return -1
    except IOError:
        # WE NEED TO RETURN -1 FOR RED-HAT-UPGRADE-TOOL
        return -1

    inplace_risk = []
    target_tree = ElementTree.fromstring(content)
    for profile in target_tree.findall(XMLNS + "TestResult"):
        inplace_risk = get_check_import_inplace_risk(profile)

    result = get_and_print_inplace_risk(verbose, inplace_risk)
    # different behaviour of division between py2 & 3
    if(result == -1):
        return -1
    elif(result < 2):
        return 0
    elif(result < 4):
        return 1
    else:
        return 2
コード例 #17
0
    def _update_check_description(self, filename):
        new_text = []
        lines = get_file_content(os.patch.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 index, line in enumerate(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)
コード例 #18
0
def is_pkg_installed(pkg_name):
    lines = get_file_content(VALUE_RPM_QA, "rb", True)
    found = [x for x in lines if x.startswith(pkg_name)]
    if found:
        return True
    else:
        return False
コード例 #19
0
def get_hashes(filename):
    """Function gets all hashes from a filename"""
    if not os.path.exists(filename):
        return None
    hashed_file = get_file_content(filename, "rb").split()
    hashed_file = [x for x in hashed_file if "hashed_file" not in x]
    return hashed_file
コード例 #20
0
 def reload_xml(self, path):
     """Function updates self.target_tree with the new path"""
     self.path = path
     # ElementTree.fromstring can't parse safely unicode string
     content = get_file_content(self.path, 'rb', False, False)
     if not content:
         return None
     self.target_tree = ElementTree.fromstring(content)
コード例 #21
0
 def reload_xml(self, path):
     """Function updates self.target_tree with the new path"""
     self.path = path
     # ElementTree.fromstring can't parse safely unicode string
     content = get_file_content(self.path, 'rb', False, False)
     if not content:
         return None
     self.target_tree = ElementTree.fromstring(content)
コード例 #22
0
 def _copy_xccdf_file(self, update_text):
     temp_dir = tempfile.mkdtemp()
     xccdf_file = os.path.join(os.getcwd(), 'tests', 'FOOBAR6_7', 'dummy_preupg', 'all-xccdf.xml')
     temp_file = os.path.join(temp_dir, 'all_xccdf.xml')
     shutil.copyfile(xccdf_file, temp_file)
     content = utils.get_file_content(temp_file, 'rb', decode_flag=False)
     content = content.replace(b'INPLACE_TAG', update_text)
     utils.write_to_file(temp_file, 'wb', content)
     return temp_file
コード例 #23
0
    def scan_system(self):
        """The function is used for scanning system with all steps."""
        self._set_devel_mode()
        self.prepare_scan_system()
        assessment_dir = self.generate_report()
        # Update source XML file in temporary directory
        self.content = os.path.join(assessment_dir, settings.content_file)
        try:
            self.report_parser = ReportParser(self.content)
        except IOError:
            log_message("Content {0} does not exist".format(self.content))
            sys.exit(1)
        if not self.conf.contents:
            version = get_assessment_version(self.conf.scan)
            if version is None:
                log_message("Your scan have wrong format",
                            level=logging.ERROR)
                log_message("Examples format is like RHEL6_7",
                            level=logging.ERROR)
                sys.exit(1)
            self.report_parser.modify_platform_tag(version[0])
        if self.conf.mode:
            try:
                lines = [i.rstrip() for i in get_file_content(os.path.join(os.path.dirname(self.path),
                                                                           self.conf.mode),
                                                              'rb',
                                                              method=True)]
            except IOError:
                return
            self.report_parser.select_rules(lines)
        if self.conf.select_rules:
            lines = [i.strip() for i in self.conf.select_rules.split(',')]
            unknown_rules = self.report_parser.check_rules(lines)
            if unknown_rules:
                log_message(settings.unknown_rules % '\n'.join(unknown_rules))
            self.report_parser.select_rules(lines)
        self.run_scan_process()
        main_report = self.scanning_progress.get_output_data()
        # This function prepare XML and generate HTML
        self.prepare_xml_for_html()

        third_party_dir_name = self.get_third_party_dir(assessment_dir)
        if os.path.exists(third_party_dir_name):
            self.run_third_party_modules(third_party_dir_name)

        self.copy_preupgrade_scripts(assessment_dir)

        # It prints out result in table format
        format_rules_to_table(main_report, "main contents")
        for target, report in six.iteritems(self.report_data):
            format_rules_to_table(report, "3rdparty content " + target)

        tar_ball_name = tarball_result_dir(self.conf.tarball_name, self.conf.result_dir, self.conf.verbose)
        log_message("Tarball with results is stored here %s ." % tar_ball_name)
        log_message("The latest assessment is stored in directory %s ." % self.conf.result_dir)
        # pack all configuration files to tarball
        return tar_ball_name
コード例 #24
0
def service_is_enabled(service_name):
    """Returns true if given service is enabled on any runlevel"""
    return_value = False
    lines = get_file_content(VALUE_CHKCONFIG, "rb", True)
    for line in lines:
        if re.match('^%s.*:on' % service_name, line):
            return_value = True
            break
    return return_value
コード例 #25
0
 def get_package_list(filename):
     """
     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.
     """
     lines = get_file_content(os.path.join(settings.KS_DIR, filename), 'rb', method=True)
     # Remove newline character from list
     lines = [line.strip() for line in lines]
     return lines
コード例 #26
0
    def embed_script(self, tarball):
        tarball_content = 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 = get_file_content(os.path.join(settings.KS_DIR, script_path), 'rb')
        if not script_str:
            log_message("Can't open script template: {0}".format(script_path))
            return

        script_str = script_str.replace('{tar_ball}', base64.b64encode(tarball_content))
        script_str = script_str.replace('{RESULT_NAME}', tarball_name)

        script = Script(script_str, type=KS_SCRIPT_POST, inChroot=True)
        self.ks.handler.scripts.append(script)
コード例 #27
0
def copy_modified_config_files(result_dir):
    """
    Function copies all modified files to dirtyconf directory.

    (files which are not mentioned in cleanconf directory)
    """
    etc_va_log = os.path.join(settings.cache_dir, settings.common_name,
                              "rpm_etc_Va.log")
    try:
        lines = get_file_content(etc_va_log, "rb", method=True)
    except IOError:
        return
    dirty_conf = os.path.join(result_dir, settings.dirty_conf_dir)
    clean_conf = os.path.join(result_dir, settings.clean_conf_dir)
    for line in lines:
        try:
            (opts, flags, filename) = line.strip().split()
        except ValueError:
            return
        log_message("File name to copy '%s'" % filename,
                    print_output=0,
                    level=logging.INFO)
        new_filename = filename[1:]
        # Check whether config file exists in cleanconf directory
        file_name = os.path.join(clean_conf, new_filename)
        if os.path.exists(file_name):
            message = "Configuration file '%s' exists in '%s' directory"
            log_message(message % (file_name, clean_conf),
                        print_output=0,
                        level=logging.INFO)
            continue
        dirty_path = os.path.join(dirty_conf, os.path.dirname(new_filename))
        # Check whether dirtyconf directory with dirname(filename) exists
        if not os.path.exists(dirty_path):
            os.makedirs(dirty_path)
        # Copy filename to dirtyconf directory
        try:
            target_name = os.path.join(dirty_conf, new_filename)
            if os.path.islink(filename):
                filename = os.path.realpath(filename)
            if os.path.exists(target_name):
                log_message("File '%s' already exists in dirtyconf directory" %
                            target_name,
                            print_output=0,
                            level=logging.INFO)
                continue
            shutil.copyfile(filename, target_name)
        except shutil.Error:
            log_message("Copying file '%s' to '%s' failed." %
                        (filename, target_name),
                        print_output=0,
                        level=logging.INFO)
            continue
        except IOError:
            continue
コード例 #28
0
 def get_volume_info(filename, first_index, second_index):
     try:
         volume_list = get_file_content(os.path.join(settings.KS_DIR, filename), 'rb', method=True, decode_flag=False)
     except IOError:
         log_message("File %s is missing. Partitioning layout has not to be complete." % filename, level=logging.WARNING)
         return None
     volume_info = {}
     for line in volume_list:
         fields = line.strip().split(':')
         volume_info[fields[first_index]] = fields[second_index]
     return volume_info
コード例 #29
0
def get_result_tag(temp_dir):
    content = utils.get_file_content(os.path.join(temp_dir, settings.xml_result_name), 'rb')
    if not content:
        return []
    target_tree = ElementTree.fromstring(content)

    text = ""
    for test_result in target_tree.findall(".//"+xccdf.XMLNS+"rule-result"):
        for result in test_result.findall(xccdf.XMLNS+"result"):
            text = result.text
    return text
コード例 #30
0
def get_dist_native_list():
    """
    returns list of all installed native packages
    """

    native_pkgs = []
    tmp = get_file_content(VALUE_RPM_QA, "rb", True)
    pkgs = [i.split("\t")[0] for i in tmp]
    for pkg in pkgs:
        if(is_dist_native(pkg) is True):
            native_pkgs.append(pkg)
    return native_pkgs
コード例 #31
0
def check_applies_to(check_applies=""):
    not_applicable = 0
    if check_applies != "":
        rpms = check_applies.split(',')
        lines = get_file_content(VALUE_RPM_QA, "rb", True)
        for rpm in rpms:
            lst = filter(lambda x: rpm == x.split('\t')[0], lines)
            if not lst:
                log_info("Package %s is not installed" % rpm)
                not_applicable = 1
    if not_applicable:
        exit_not_applicable()
コード例 #32
0
 def get_package_list(filename):
     """
     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.
     """
     lines = get_file_content(os.path.join(settings.KS_DIR, filename),
                              'rb',
                              method=True)
     # Remove newline character from list
     lines = [line.strip() for line in lines]
     return lines
コード例 #33
0
    def test_final_compose(self):
        expected_contents = ["failed", "fixed", "needs_action", "needs_inspection", "not_applicable", "pass"]
        for content in expected_contents:
            compose_xml = ComposeXML()
            dir_name = os.path.join(self.temp_dir, FOO_DIR, "dummy")
            compose_xml.collect_group_xmls(dir_name, content=content)

        xccdf_compose = XCCDFCompose(os.path.join(self.temp_dir, FOO_DIR))
        xccdf_compose.generate_xml()
        all_xccdf = os.path.join(self.result_dir, settings.content_file)
        self.assertTrue(os.path.exists(all_xccdf))
        dummy_lines = utils.get_file_content(all_xccdf, "rb")
コード例 #34
0
    def update_html(self, result_name, solution_files, extension="html"):
        """
         Function updates a XML or HTML file with relevant solution
         texts
        """
        orig_file = os.path.join(self.dirname, result_name + "." + extension)
        lines = get_file_content(orig_file, "rb", method=True)
        for dir_name, files in six.iteritems(solution_files):
            section = dir_name.replace(
                os.path.join(self.dirname, self.scenario),
                "").replace("/", "_")
            solution_text = section + "_SOLUTION_MSG"

            file_name = self._return_correct_text_file(section, files)
            if not file_name or file_name is None:
                continue
            else:
                text = get_file_content(os.path.join(dir_name, file_name),
                                        "rb",
                                        method=True)
            for cnt, line in enumerate(lines):
                # If in INPLACERISK: is a [link] then update them
                # to /root/pre{migrate,upgrade}/...
                if 'INPLACERISK:' in line.strip():
                    lines[cnt] = tag_formating([line], extension)[0]
                    continue
                # Find correct block
                if solution_text not in line.strip():
                    continue
                # Get updated text if it is HTML or TEXT
                lines[cnt] = self.get_updated_text(solution_text, text, line,
                                                   extension)

        if extension == 'xml':
            for cnt, line in enumerate(lines):
                if 'SOLUTION_MSG' in line.strip():
                    lines[cnt] = re.sub(r'>.*SOLUTION_MSG.*<', '><',
                                        line.strip())

        write_to_file(orig_file, "wb", lines)
コード例 #35
0
def copy_modified_config_files(result_dir):
    """
    Function copies all modified files to dirtyconf directory.

    (files which are not mentioned in cleanconf directory)
    """
    etc_va_log = os.path.join(settings.cache_dir, settings.common_name, "rpm_etc_Va.log")
    try:
        lines = get_file_content(etc_va_log, "rb", method=True)
    except IOError:
        return
    dirty_conf = os.path.join(result_dir, settings.dirty_conf_dir)
    clean_conf = os.path.join(result_dir, settings.clean_conf_dir)
    for line in lines:
        try:
            (opts, flags, filename) = line.strip().split()
        except ValueError:
            return
        log_message("File name to copy '%s'" % filename,
                    print_output=0,
                    level=logging.INFO)
        new_filename = filename[1:]
        # Check whether config file exists in cleanconf directory
        file_name = os.path.join(clean_conf, new_filename)
        if os.path.exists(file_name):
            message = "Configuration file '%s' exists in '%s' directory"
            log_message(message % (file_name, clean_conf),
                        print_output=0,
                        level=logging.INFO)
            continue
        dirty_path = os.path.join(dirty_conf, os.path.dirname(new_filename))
        # Check whether dirtyconf directory with dirname(filename) exists
        if not os.path.exists(dirty_path):
            os.makedirs(dirty_path)
        # Copy filename to dirtyconf directory
        try:
            target_name = os.path.join(dirty_conf, new_filename)
            if os.path.islink(filename):
                filename = os.path.realpath(filename)
            if os.path.exists(target_name):
                log_message("File '%s' already exists in dirtyconf directory" % target_name,
                            print_output=0,
                            level=logging.INFO)
                continue
            shutil.copyfile(filename, target_name)
        except shutil.Error:
            log_message("Copying file '%s' to '%s' failed." % (filename, target_name),
                        print_output=0,
                        level=logging.INFO)
            continue
        except IOError:
            continue
コード例 #36
0
 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 = 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)
     write_to_file(file_name, 'wb', content)
コード例 #37
0
 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 = 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)
     write_to_file(file_name, 'wb', content)
コード例 #38
0
 def test_unicode_script_author(self):
     """Test processing of non-ascii characters for author section"""
     u_author = b'Petr Stod\xc5\xaflka'.decode(settings.defenc)
     script_file = os.path.join(self.result_dir, "unicode", "dummy_unicode.sh")
     settings.autocomplete = True
     self.target_tree = ComposeXML.run_compose(self.tree, self.result_dir)
     self.assertTrue(self.target_tree)
     try:
         lines = get_file_content(script_file, "rb", True)
     except IOError:
         assert False
     author = filter(lambda x: u_author in x, lines)
     self.assertTrue(author)
コード例 #39
0
 def _get_sizes(filename):
     part_sizes = {}
     lines = get_file_content(os.path.join(settings.KS_DIR, filename), 'rb', method=True, decode_flag=False)
     lines = [x for x in lines if x.startswith('/')]
     for line in lines:
         fields = line.strip().split(' ')
         part_name = fields[0]
         try:
             size = fields[2]
         except IndexError:
             size = fields[1]
         part_sizes[part_name] = size
     return part_sizes
コード例 #40
0
    def embed_script(self, tarball):
        tarball_content = 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 = get_file_content(
            os.path.join(settings.KS_DIR, script_path), 'rb')
        if not script_str:
            log_message("Can't open script template: {0}".format(script_path))
            return

        script_str = script_str.replace('{tar_ball}',
                                        base64.b64encode(tarball_content))
        script_str = script_str.replace('{RESULT_NAME}', tarball_name)

        script = Script(script_str, type=KS_SCRIPT_POST, inChroot=True)
        self.ks.handler.scripts.append(script)
コード例 #41
0
def get_result_tag(temp_dir):
    content = utils.get_file_content(
        os.path.join(temp_dir, settings.xml_result_name), 'rb')
    if not content:
        return []
    target_tree = ElementTree.fromstring(content)

    text = ""
    for test_result in target_tree.findall(".//" + xccdf.XMLNS +
                                           "rule-result"):
        for result in test_result.findall(xccdf.XMLNS + "result"):
            text = result.text
    return text
コード例 #42
0
def is_dist_native(pkg):
    """
    is_dist_native function return only True or False
    return False if package is not installed and of course information log.
    Case DEVEL_MODE is turn off then return True if package is signed or False if not.
    Case DEVEL_MODE is turn on:
    DIST_NATIVE = sign: return True if is RH_SIGNED else return False
    DIST_NATIVE = all: always return True
    DIST_NATIVE = path_to_file: return True if package is in file else return False
    """

    rpm_qa = get_file_content(VALUE_RPM_QA, "rb", True)
    found = [x for x in rpm_qa if x.startswith(pkg)]
    if not found:
        log_warning("Package %s is not installed on Red Hat Enterprise Linux system.")
        return False

    rpm_signed = get_file_content(VALUE_RPM_RHSIGNED, "rb", True)
    if int(DEVEL_MODE) == 0:
        found = [x for x in rpm_signed if x.startswith(pkg)]
        if found:
            return True
        else:
            return False
    else:
        if DIST_NATIVE == "all":
            return True
        if DIST_NATIVE == "sign":
            found = [x for x in rpm_signed if x.startswith(pkg)]
            if found:
                return True
            else:
                return False
        if os.path.exists(DIST_NATIVE):
            list_native = get_file_content(DIST_NATIVE)
            if pkg in list_native:
                return True
        return False
コード例 #43
0
    def upload_results(self, tarball_path=None):
        """upload tarball with results to frontend"""
        import xmlrpclib
        import socket
        if self.conf.upload is True:
            # lets try default configuration
            url = "http://127.0.0.1:8099/submit/"
        else:
            url = self.conf.upload \
                if self.conf.upload[-1] == '/' \
                else self.conf.upload + '/'
        try:
            proxy = xmlrpclib.ServerProxy(url)
            proxy.submit.ping()
        except Exception:
            raise Exception('Can\'t connect to preupgrade assistant WEB-UI at %s.\n\n'
                'Please ensure that package preupgrade-assistant-ui '
                'has been installed on target system and firewall is set up '
                'to allow connections on port 8099.' % url)

        tarball_results = self.conf.results or tarball_path
        file_content = get_file_content(tarball_results, 'rb', False, False)

        binary = xmlrpclib.Binary(file_content)
        host = socket.gethostname()
        response = proxy.submit.submit_new({
            'data': binary,
            'host': host,
        })
        try:
            status = response['status']
        except KeyError:
            log_message('Invalid response from server.')
            log_message("Invalid response from server: %s" % response, level=logging.ERROR)
        else:
            if status == 'OK':
                try:
                    url = response['url']
                except KeyError:
                    log_message('Report submitted successfully.')
                else:
                    log_message('Report submitted successfully. You can inspect it at %s' % url)
            else:
                try:
                    message = response['message']
                    log_message('Report not submitted. Server returned message: ', message)
                    log_message("Report submit: %s (%s)" % (status, message), level=logging.ERROR)
                except KeyError:
                    log_message('Report not submitted. Server returned status: ', status)
                    log_message("Report submit: %s" % status, level=logging.ERROR)
コード例 #44
0
 def __init__(self, report_path):
     self.path = report_path
     self.element_prefix = "{http://checklists.nist.gov/xccdf/1.2}"
     try:
         # ElementTree.fromstring can't parse safely unicode string
         content = get_file_content(report_path, 'rb', False, False)
     except IOError as ioerr:
         raise
     if not content:
         return None
     self.target_tree = ElementTree.fromstring(content)
     self.profile = "Profile"
     self.changed_results = {}
     self.output_data = []
コード例 #45
0
 def __init__(self, report_path):
     self.path = report_path
     self.element_prefix = "{http://checklists.nist.gov/xccdf/1.2}"
     try:
         # ElementTree.fromstring can't parse safely unicode string
         content = get_file_content(report_path, 'rb', False, False)
     except IOError as ioerr:
         raise
     if not content:
         return None
     self.target_tree = ElementTree.fromstring(content)
     self.profile = "Profile"
     self.changed_results = {}
     self.output_data = []
コード例 #46
0
 def write_list_rules(self):
     rule_name = '_'.join(self.dirname.split('/')[1:])
     file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules)
     lines = []
     if os.path.exists(file_list_rules):
         lines = 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')
     write_to_file(file_list_rules, "wb", lines)
コード例 #47
0
 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 = 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')
         write_to_file(path_name, 'wb', lines)
コード例 #48
0
    def test_final_compose(self):
        expected_contents = [
            'failed', 'fixed', 'needs_action', 'needs_inspection',
            'not_applicable', 'pass'
        ]
        for content in expected_contents:
            compose_xml = ComposeXML()
            dir_name = os.path.join(self.temp_dir, FOO_DIR, 'dummy')
            compose_xml.collect_group_xmls(dir_name, content=content)

        xccdf_compose = XCCDFCompose(os.path.join(self.temp_dir, FOO_DIR))
        xccdf_compose.generate_xml()
        all_xccdf = os.path.join(self.result_dir, settings.content_file)
        self.assertTrue(os.path.exists(all_xccdf))
        dummy_lines = utils.get_file_content(all_xccdf, 'rb')
コード例 #49
0
    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 = 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')
            write_to_file(path_name, 'wb', lines)
コード例 #50
0
 def _get_sizes(filename):
     part_sizes = {}
     lines = get_file_content(os.path.join(settings.KS_DIR, filename),
                              'rb',
                              method=True,
                              decode_flag=False)
     lines = [x for x in lines if x.startswith('/')]
     for line in lines:
         fields = line.strip().split(' ')
         part_name = fields[0]
         try:
             size = fields[2]
         except IndexError:
             size = fields[1]
         part_sizes[part_name] = size
     return part_sizes
コード例 #51
0
    def _read_group_info(self):
        def get_packages(s):
            # get rid of empty strings
            return [x for x in s.strip().split(',') if x]

        for fp in self.group_def_fp:
            lines = get_file_content(fp, 'r', True)
            for line in lines:
                stuff = line.split('|')
                name = stuff[0].strip()
                mandatory = get_packages(stuff[1])
                default = get_packages(stuff[2])
                optional = get_packages(stuff[3])
                # why would we want empty groups?
                if mandatory or default or optional:
                    yg = YumGroup(name, mandatory, default, optional)
                    self.gm.add(yg)
コード例 #52
0
 def kickstart_scripts():
     try:
         lines = utils.get_file_content(os.path.join(
             settings.common_dir, settings.KS_SCRIPTS),
                                        "rb",
                                        method=True)
         for counter, line in enumerate(lines):
             line = line.strip()
             if line.startswith("#"):
                 continue
             if 'is not installed' in line:
                 continue
             cmd, name = line.split("=", 2)
             kickstart_file = os.path.join(settings.KS_DIR, name)
             utils.run_subprocess(cmd, output=kickstart_file, shell=True)
     except IOError:
         pass