def testFilterInstallCommands(self):
        commands, _ = common.filter_install_commands("yum install")
        self.assertEqual(len(commands), 1)
        commands, _ = common.filter_install_commands("yum remove")
        self.assertEqual(len(commands), 1)

        # Negative Scenarios
        commands, _ = common.filter_install_commands("yum clean")
        self.assertEqual(len(commands), 0)
        commands, _ = common.filter_install_commands("yum")
        self.assertEqual(len(commands), 0)
Esempio n. 2
0
File: run.py Progetto: xaleeks/tern
def get_dockerfile_packages():
    '''Given a Dockerfile return an approximate image object. This is mosty
    guess work and shouldn't be relied on for accurate information. Add
    Notice messages indicating as such:
        1. Create an image with a placeholder repotag
        2. For each RUN command, create a package list
        3. Create layer objects with incremental integers and add the package
        list to that layer with a Notice about parsing
        4. Return stub image'''
    stub_image = Image('easteregg:cookie')
    layer_count = 0
    for cmd in dhelper.docker_commands:
        if cmd['instruction'] == 'RUN':
            layer_count = layer_count + 1
            layer = ImageLayer(layer_count)
            install_commands, msg = \
                common.filter_install_commands(cmd['value'])
            if msg:
                layer.origins.add_notice_to_origins(cmd['value'],
                                                    Notice(msg, 'info'))
            pkg_names = []
            for command in install_commands:
                pkg_names.append(common.get_installed_package_names(command))
            for pkg_name in pkg_names:
                pkg = Package(pkg_name)
                # shell parser does not parse version pins yet
                # when that is enabled, Notices for no versions need to be
                # added here
                layer.add_package(pkg)
    return stub_image
Esempio n. 3
0
def get_commands_from_history(image_layer):
    '''Given the image layer object and the shell, get the list of command
    objects that created the layer'''
    # set up notice origin for the layer
    origin_layer = 'Layer {}'.format(image_layer.layer_index)
    if image_layer.created_by:
        instruction = created_to_instruction(image_layer.created_by)
        image_layer.origins.add_notice_to_origins(
            origin_layer,
            Notice(
                formats.dockerfile_line.format(
                    dockerfile_instruction=instruction), 'info'))
        command_line = instruction.split(' ', 1)[1]
    else:
        instruction = ''
        image_layer.origins.add_notice_to_origins(
            origin_layer, Notice(formats.no_created_by, 'warning'))
        command_line = instruction
    # Image layers are created with the directives RUN, ADD and COPY
    # For ADD and COPY instructions, there is no information about the
    # packages added
    if 'ADD' in instruction or 'COPY' in instruction:
        image_layer.origins.add_notice_to_origins(
            origin_layer,
            Notice(errors.unknown_content.format(files=command_line),
                   'warning'))
        # return an empty list as we cannot find any commands
        return []
    # for RUN instructions we can return a list of commands
    command_list, msg = common.filter_install_commands(command_line)
    if msg:
        image_layer.origins.add_notice_to_origins(origin_layer,
                                                  Notice(msg, 'warning'))
    return command_list
Esempio n. 4
0
def package_in_dockerfile(command_dict, pkg_name):
    '''Return True if pkg_name is a package specified in the command_dict
    RUN line provided, otherwise return False.'''
    command_words, _ = common.filter_install_commands(command_dict['value'])
    for command in command_words:
        if pkg_name in command.words:
            return True
    return False
Esempio n. 5
0
def get_install_packages(command_dict):
    '''Given a dockerfile RUN line, return a list of packages to be
    installed from that line.'''
    command_words, _ = common.filter_install_commands(command_dict['value'])
    install_packages = []
    for command in command_words:
        for word in command.words:
            install_packages.append(word)
    return install_packages