Example #1
0
    def is_valid_version(self):
        """Validate that the version of self.file_path is -1."""
        file_extension = os.path.splitext(self.file_path)[1]
        version_number = -1
        reputations_valid = True
        if file_extension == '.yml':
            yaml_dict = get_json(self.file_path)
            version_number = yaml_dict.get('commonfields', {}).get('version')
            if not version_number:  # some files like playbooks do not have commonfields key
                version_number = yaml_dict.get('version')

        elif file_extension == '.json':
            if checked_type(self.file_path):
                file_name = os.path.basename(self.file_path)
                json_dict = get_json(self.file_path)
                if file_name == "reputations.json":
                    reputations_valid = self.validate_reputations_file(json_dict)
                else:
                    version_number = json_dict.get('version')

        if version_number != -1 or not reputations_valid:
            print_error("The version for our files should always be -1, "
                        "please update the file {}.".format(self.file_path))
            self._is_valid = False

        return self._is_valid
Example #2
0
def get_playbook_data(file_path):
    playbook_data = OrderedDict()
    data_dictionary = get_json(file_path)
    id = data_dictionary.get('id', '-')
    name = data_dictionary.get('name', '-')

    tests = data_dictionary.get('tests')
    toversion = data_dictionary.get('toversion')
    fromversion = data_dictionary.get('fromversion')
    implementing_scripts = get_task_ids_from_playbook('scriptName',
                                                      data_dictionary)
    implementing_playbooks = get_task_ids_from_playbook(
        'playbookName', data_dictionary)
    command_to_integration = get_commmands_from_playbook(data_dictionary)

    playbook_data['name'] = name
    if toversion:
        playbook_data['toversion'] = toversion
    if fromversion:
        playbook_data['fromversion'] = fromversion
    if implementing_scripts:
        playbook_data['implementing_scripts'] = implementing_scripts
    if implementing_playbooks:
        playbook_data['implementing_playbooks'] = implementing_playbooks
    if command_to_integration:
        playbook_data['command_to_integration'] = command_to_integration
    if tests:
        playbook_data['tests'] = tests

    return {id: playbook_data}
Example #3
0
def get_integration_commands(file_path):
    cmd_list = []
    data_dictionary = get_json(file_path)
    commands = data_dictionary.get('script', {}).get('commands', [])
    for command in commands:
        cmd_list.append(command.get('name'))

    return cmd_list
Example #4
0
def get_tests(file_path):
    """Collect tests mentioned in file_path"""
    data_dictionary = get_json(file_path)
    # inject no tests to whitelist so adding values to white list will not force all tests
    if SECRETS_WHITE_LIST in file_path:
        data_dictionary = {'tests': ["No test - whitelist"]}
    if data_dictionary:
        return data_dictionary.get('tests', [])
Example #5
0
def get_script_package_data(package_path):
    if package_path[-1] != os.sep:
        package_path = os.path.join(package_path, '')
    yml_path = glob.glob(package_path + '*.yml')[0]
    code_type = get_json(yml_path).get('type')
    code_path = get_code_file(package_path, TYPE_TO_EXTENSION[code_type])
    with open(code_path, 'r') as code_file:
        code = code_file.read()

    return yml_path, code
Example #6
0
    def __init__(self,
                 file_path,
                 check_git=True,
                 old_file_path=None,
                 old_git_branch='master'):
        self.file_path = file_path
        self.current_incident_field = {}
        self.old_incident_field = {}

        if check_git:
            self.current_incident_field = get_json(file_path)
            if old_file_path:
                self.old_incident_field = get_remote_file(
                    old_file_path, old_git_branch)
            else:
                self.old_incident_field = get_remote_file(
                    file_path, old_git_branch)
Example #7
0
    def __init__(self, file_path, check_git=True, old_file_path=None):
        self._is_valid = True

        self.file_path = file_path
        if check_git:
            self.current_integration = get_json(file_path)
            # The replace in the end is for Windows support
            if old_file_path:
                git_hub_path = os.path.join(self.CONTENT_GIT_HUB_LINK, old_file_path).replace("\\", "/")
                file_content = get(git_hub_path).content
                self.old_integration = yaml.load(file_content)
            else:
                try:
                    file_path_from_master = os.path.join(self.CONTENT_GIT_HUB_LINK, file_path).replace("\\", "/")
                    self.old_integration = yaml.load(get(file_path_from_master).content)
                except Exception:
                    print_error("Could not find the old integration please make sure that you did not break "
                                "backward compatibility")
                    self.old_integration = None
Example #8
0
def get_general_data(path):
    data = OrderedDict()
    json_data = get_json(path)

    id = json_data.get('id')
    brandname = json_data.get('brandName', '')
    name = json_data.get('name', '')
    fromversion = json_data.get('fromVersion')
    toversion = json_data.get('toVersion')
    pack = get_pack_name(path)
    if brandname:  # for classifiers
        data['name'] = brandname
    if name:  # for the rest
        data['name'] = name
    if toversion:
        data['toversion'] = toversion
    if fromversion:
        data['fromversion'] = fromversion
    if pack:
        data['pack'] = pack

    return {id: data}
Example #9
0
def get_integration_data(file_path):
    integration_data = OrderedDict()
    data_dictionary = get_json(file_path)
    id = data_dictionary.get('commonfields', {}).get('id', '-')
    name = data_dictionary.get('name', '-')

    tests = data_dictionary.get('tests')
    toversion = data_dictionary.get('toversion')
    fromversion = data_dictionary.get('fromversion')
    commands = data_dictionary.get('script', {}).get('commands', [])
    cmd_list = [command.get('name') for command in commands]

    integration_data['name'] = name
    if toversion:
        integration_data['toversion'] = toversion
    if fromversion:
        integration_data['fromversion'] = fromversion
    if cmd_list:
        integration_data['commands'] = cmd_list
    if tests:
        integration_data['tests'] = tests

    return {id: integration_data}
Example #10
0
def get_script_data(file_path, script_code=None):
    script_data = OrderedDict()
    data_dictionary = get_json(file_path)
    id = data_dictionary.get('commonfields', {}).get('id', '-')
    if script_code is None:
        script_code = data_dictionary.get('script', '')

    name = data_dictionary.get('name', '-')

    tests = data_dictionary.get('tests')
    toversion = data_dictionary.get('toversion')
    deprecated = data_dictionary.get('deprecated')
    fromversion = data_dictionary.get('fromversion')
    depends_on, command_to_integration = get_depends_on(data_dictionary)
    script_executions = sorted(
        list(
            set(
                re.findall("demisto.executeCommand\\(['\"](\w+)['\"].*",
                           script_code))))

    script_data['name'] = name
    if toversion:
        script_data['toversion'] = toversion
    if fromversion:
        script_data['fromversion'] = fromversion
    if deprecated:
        script_data['deprecated'] = deprecated
    if depends_on:
        script_data['depends_on'] = depends_on
    if script_executions:
        script_data['script_executions'] = script_executions
    if command_to_integration:
        script_data['command_to_integration'] = command_to_integration
    if tests:
        script_data['tests'] = tests

    return {id: script_data}
Example #11
0
def get_layout_data(path):
    data = OrderedDict()
    json_data = get_json(path)
    layout = json_data.get('layout')
    name = layout.get('name', '-')
    id = layout.get('id', '-')
    typeID = json_data.get('typeId')
    typeName = json_data.get('TypeName')
    fromversion = json_data.get('fromVersion')
    toversion = json_data.get('toVersion')
    pack = get_pack_name(path)
    if typeID:
        data['typeID'] = typeID
    if typeName:
        data['typename'] = typeName
    data['name'] = name
    if toversion:
        data['toversion'] = toversion
    if fromversion:
        data['fromversion'] = fromversion
    if pack:
        data['pack'] = pack

    return {id: data}
Example #12
0
def get_name(file_path):
    data_dictionary = get_json(file_path)

    if data_dictionary:
        return data_dictionary.get('name', '-')
Example #13
0
def get_tests(file_path):
    """Collect tests mentioned in file_path"""
    data_dictionary = get_json(file_path)
    # inject no tests to whitelist so adding values to white list will not force all tests
    if data_dictionary:
        return data_dictionary.get('tests', [])