def create_pv_nodes(self):
     """Create Package and Version nodes, if needed."""
     nodes = [
     ]  # return (e, p, v) tuples of created/existing nodes; for easier testing
     affected_pkgs = {}
     all_epvs_created = True
     for pv_dict in self._cve_dict.get('affected'):
         epv_dict = pv_dict.copy()
         epv_dict['ecosystem'] = self._cve_dict.get('ecosystem')
         query = GraphPopulator.construct_graph_nodes(epv_dict)
         latest_version = "-1"
         # Fetch the value of the latest_version from the query created
         if "latest_version" in query:
             data = query.split("\'latest_version\'")[1].split(");")[0]
             latest_version = data.replace(",", "").strip().replace("'", "")
         success, json_response = BayesianGraph.execute(query)
         e = epv_dict.get('ecosystem')
         p = epv_dict.get('name')
         v = epv_dict.get('version')
         if p not in affected_pkgs:
             tmp = {"ecosystem": e, "latest_version": latest_version}
             affected_pkgs[p] = tmp
         if not success:
             logger.error(
                 'CVEIngestionError - Error creating nodes for {e}/{p}/{v}: {r}'
                 .format(e=e, p=p, v=v, r=str(json_response)))
             all_epvs_created = False
         else:
             nodes.append((e, p, v))
     return nodes, all_epvs_created, affected_pkgs
示例#2
0
 def create_pv_nodes(self):
     """Create Package and Version nodes, if needed."""
     nodes = []  # return (e, p, v) tuples of created/existing nodes; for easier testing
     affected_pkgs = {}
     all_epvs_created = True
     for pv_dict in self._cve_dict.get('affected'):
         epv_dict = pv_dict.copy()
         epv_dict['ecosystem'] = self._cve_dict.get('ecosystem')
         query, bindings = GraphPopulator.construct_graph_nodes(epv_dict)
         # Fetch the value of the latest_version from the query created
         latest_version = -1
         if "latest" in bindings:
             latest_version = bindings['latest']
         success, json_response = BayesianGraph.execute(self.prepare_payload(query, bindings))
         e = epv_dict.get('ecosystem')
         p = epv_dict.get('name')
         v = epv_dict.get('version')
         if p not in affected_pkgs:
             tmp = {
                 "ecosystem": e,
                 "latest_version": latest_version
             }
             affected_pkgs[p] = tmp
         if not success:
             logger.error('CVEIngestionError - Error creating nodes for {e}/{p}/{v}: {r}'.format(
                 e=e, p=p, v=v, r=str(json_response))
             )
             all_epvs_created = False
         else:
             nodes.append((e, p, v))
     return nodes, all_epvs_created, affected_pkgs
    def create_pv_nodes(self):
        """Create Package and Version nodes, if needed."""
        nodes = [
        ]  # return (e, p, v) tuples of created/existing nodes; for easier testing
        affected_pkgs = {}
        all_epvs_created = True
        p = self._cve_dict.get('package')
        e = self._cve_dict.get('ecosystem')
        epv_dict = {
            "ecosystem": self._cve_dict.get('ecosystem'),
            "name": self._cve_dict.get('package')
        }
        latest_version = ""
        for ver in self._cve_dict.get('affected'):
            epv_dict['version'] = ver
            query = GraphPopulator.construct_graph_nodes(epv_dict)
            success, json_response = BayesianGraph.execute(query)
            # Fetch the value of the latest_version from the query create
            if not latest_version and "latest_version" in query:
                data = query.split("\'latest_version\'")[1].split(");")[0]
                latest_version = data.replace(",", "").strip().replace("'", "")

            if not success:
                logger.error(
                    'CVEIngestionError - Error creating nodes for {e}/{p}/{v}: {r}'
                    .format(e=e, p=p, v=ver, r=str(json_response)))
                all_epvs_created = False
            else:
                nodes.append((e, p, ver))

        if p not in affected_pkgs:
            affected_pkg = {"ecosystem": e, "latest_version": latest_version}
            affected_pkgs[p] = affected_pkg

        # To create the latest version node if not present
        if latest_version and latest_version != "-1":
            epv_dict['version'] = latest_version
            query = GraphPopulator.construct_graph_nodes(epv_dict)
            BayesianGraph.execute(query)
        return nodes, all_epvs_created, affected_pkgs
示例#4
0
 def create_pv_nodes(self):
     """Create Package and Version nodes, if needed."""
     nodes = [
     ]  # return (e, p, v) tuples of created/existing nodes; for easier testing
     all_epvs_created = True
     for pv_dict in self._cve_dict.get('affected'):
         epv_dict = pv_dict.copy()
         epv_dict['ecosystem'] = self._cve_dict.get('ecosystem')
         query = GraphPopulator.construct_graph_nodes(epv_dict)
         success, json_response = BayesianGraph.execute(query)
         e = epv_dict.get('ecosystem')
         p = epv_dict.get('name')
         v = epv_dict.get('version')
         if not success:
             logger.error(
                 'CVEIngestionError - Error creating nodes for {e}/{p}/{v}: {r}'
                 .format(e=e, p=p, v=v, r=str(json_response)))
             all_epvs_created = False
         else:
             nodes.append((e, p, v))
     return nodes, all_epvs_created
示例#5
0
def create_graph_nodes(list_epv):
    """Create blank graph nodes given an EPV."""
    count_blank_epvs_created = 0
    success_epvs = []
    failure_epvs = []

    for item in list_epv:
        str_gremlin, bindings = GraphPopulator.construct_graph_nodes(item)
        epv = item.get('ecosystem') + ":" + item.get('name') + ":" + item.get(
            'version')

        if str_gremlin:
            payload = {'gremlin': str_gremlin, "bindings": bindings}
            print(json.dumps(payload))
            try:
                result = requests.post(config.GREMLIN_SERVER_URL_REST,
                                       data=json.dumps(payload),
                                       timeout=30)
                resp = result.json()
                print(json.dumps(resp))

                if resp['status']['code'] == 200:
                    count_blank_epvs_created += 1
                    success_epvs.append(epv)
            except Exception as e:  # pragma: no cover
                logger.error(e)
                failure_json = {epv: e}
                failure_epvs.append(failure_json)

    status = "Success"
    if count_blank_epvs_created == 0:
        status = "Failure"

    response = {
        "epv_nodes_created": count_blank_epvs_created,
        "success_list": success_epvs,
        "failure_list": failure_epvs,
        "status": status
    }
    return response
示例#6
0
    def create_pv_nodes(self):
        """Create Package and Version nodes, if needed."""
        nodes = []  # return (e, p, v) tuples of created/existing nodes; for easier testing
        affected_pkgs = {}
        all_epvs_created = True
        p = self._snyk_pkg_data.get('package')
        e = self._snyk_pkg_data.get('ecosystem')
        latest_version = self._snyk_pkg_data.get('latest_version')
        latest_non_cve_version = ''
        epv_dict = {
            "ecosystem": e,
            "name": p,
            "latest_version": latest_version
        }
        if latest_version not in self._snyk_pkg_data.get('affected'):
            logger.info("Latest version is not affected {}".format(p))
            latest_non_cve_version = latest_version
        else:
            logger.info("Latest version is affected {p} {v}".format(p=p, v=latest_version))

        if e == 'golang':
            itr_list = self._snyk_pkg_data.get('all_ver')
            epv_dict['gh_link'] = self._snyk_pkg_data.get('gh_link')
            epv_dict['license'] = self._snyk_pkg_data.get('license')
        else:
            itr_list = self._snyk_pkg_data.get('affected')

        for ver in itr_list:
            epv_dict['version'] = ver
            query = GraphPopulator.construct_graph_nodes(epv_dict)
            success, json_response = BayesianGraph.execute(query)
            # Fetch the value of the latest_version from the query create
            if not latest_version and "latest_version" in query:
                data = query.split("\'latest_version\'")[1].split(");")[0]
                latest_version = data.replace(",", "").strip().replace("'", "")

            if not success:
                logger.error('CVEIngestionError - Error creating nodes for {e}/{p}/{v}: {r}'.format(
                    e=e, p=p, v=ver, r=str(json_response))
                )
                all_epvs_created = False
            else:
                nodes.append((e, p, ver))

        # To create the latest version node if not present
        if latest_version and latest_version != "-1" and e != "golang":
            epv_dict['version'] = latest_version
            logger.info("Creating latest version node {e} {p} {v}".format(e=epv_dict['ecosystem'],
                                                                          p=epv_dict['name'],
                                                                          v=epv_dict['version']))
            query = GraphPopulator.construct_graph_nodes(epv_dict)
            BayesianGraph.execute(query)

        res = ""
        if latest_non_cve_version:
            res = update_non_cve_on_pkg(e, p, latest_non_cve_version)

        if p not in affected_pkgs and res != "Success":
            affected_pkg = {
                "ecosystem": e,
                "latest_version": latest_version
            }
            affected_pkgs[p] = affected_pkg
        return nodes, all_epvs_created, affected_pkgs