Ejemplo n.º 1
0
    def get(ecosystem, package, version):
        decoded = decode_token()
        package = urllib.parse.unquote(package)
        if ecosystem == 'maven':
            package = MavenCoordinates.normalize_str(package)
        package = case_sensitivity_transform(ecosystem, package)
        result = get_analyses_from_graph(ecosystem, package, version)

        if result is not None:
            # Known component for Bayesian
            server_create_component_bookkeeping(ecosystem, package, version, decoded)
            return result

        if os.environ.get("INVOKE_API_WORKERS", "") == "1":
            # Enter the unknown path
            server_create_analysis(ecosystem, package, version, user_profile=decoded,
                                   api_flow=True, force=False, force_graph_sync=True)
            msg = "Package {ecosystem}/{package}/{version} is unavailable. " \
                  "The package will be available shortly," \
                  " please retry after some time.".format(ecosystem=ecosystem, package=package,
                                                          version=version)
            raise HTTPError(202, msg)
        else:
            server_create_analysis(ecosystem, package, version, user_profile=decoded,
                                   api_flow=False, force=False, force_graph_sync=True)
            msg = "No data found for {ecosystem} Package " \
                  "{package}/{version}".format(ecosystem=ecosystem,
                                               package=package, version=version)
            raise HTTPError(404, msg)
Ejemplo n.º 2
0
    def post(ecosystem, package, version):
        decoded = decode_token()
        if ecosystem == 'maven':
            package = MavenCoordinates.normalize_str(package)
        package = case_sensitivity_transform(ecosystem, package)

        server_create_analysis(ecosystem, package, version,
                               user_profile=decoded or {}, api_flow=True, force=True,
                               force_graph_sync=False)
        return {}, 202
    def get(ecosystem, package, version):
        """Handle the GET REST API call."""
        decoded = decode_token()
        package = urllib.parse.unquote(package)
        if ecosystem == 'maven':
            package = MavenCoordinates.normalize_str(package)
        package = case_sensitivity_transform(ecosystem, package)
        result = get_analyses_from_graph(ecosystem, package, version)

        if result is not None:
            return result

        msg = "No data found for {ecosystem} package " \
              "{package}/{version}".format(ecosystem=ecosystem,
                                           package=package, version=version)
        raise HTTPError(404, msg)
Ejemplo n.º 4
0
    def get(ecosystem, package, version):
        """Handle the GET REST API call.

        Component Analyses:
            - If package is Known (exists in GraphDB (Snyk Edge) returns Json formatted response.
            - If package is not Known: Call Util's function to trigger ingestion flow.

        :return:
            JSON Response
        """
        st = time.time()
        # Analytics Data
        metrics_payload = {
            "pid": os.getpid(),
            "hostname": HOSTNAME,
            "endpoint": request.endpoint,
            "request_method": "GET",
            "ecosystem": ecosystem,
            "package": package,
            "version": version
        }
        response_template = namedtuple("response_template",
                                       ["message", "status"])
        logger.info("Executed v2 API")
        package = urllib.parse.unquote(package)

        if re.findall('[!@#$%^&*()]', version):
            # Version should not contain special Characters.
            return response_template(
                {
                    'error':
                    "Package version should not have special characters."
                }, 400)

        if not check_for_accepted_ecosystem(ecosystem):
            msg = f"Ecosystem {ecosystem} is not supported for this request"
            raise HTTPError(400, msg)
        if ecosystem == 'maven':
            try:
                package = MavenCoordinates.normalize_str(package)
            except ValueError:
                msg = f"Invalid maven format - {package}"
                metrics_payload.update({
                    "status_code": 400,
                    "value": time.time() - st
                })
                _session.post(url=METRICS_SERVICE_URL + "/api/v1/prometheus",
                              json=metrics_payload)
                raise HTTPError(400, msg)
        package = case_sensitivity_transform(ecosystem, package)

        # Perform Component Analyses on Vendor specific Graph Edge.
        analyses_result = ComponentAnalyses(
            ecosystem, package, version).get_component_analyses_response()

        if analyses_result is not None:

            metrics_payload.update({
                "status_code": 200,
                "value": time.time() - st
            })
            _session.post(url=METRICS_SERVICE_URL + "/api/v1/prometheus",
                          json=metrics_payload)
            return analyses_result

        # No data has been found
        unknown_pkgs = set()
        unknown_pkgs.add(
            ingestion_utils.Package(package=package, version=version))
        unknown_package_flow(ecosystem, unknown_pkgs)

        msg = f"No data found for {ecosystem} package {package}/{version}"

        metrics_payload.update({"status_code": 404, "value": time.time() - st})
        _session.post(url=METRICS_SERVICE_URL + "/api/v1/prometheus",
                      json=metrics_payload)

        raise HTTPError(404, msg)
Ejemplo n.º 5
0
    def get(ecosystem, package, version):
        """Handle the GET REST API call.

        Component Analyses:
            - If package is Known (exists in GraphDB (Snyk Edge) returns Json formatted response.
            - If package is not Known:
                - DISABLE_UNKNOWN_PACKAGE_FLOW flag is 1: Skips the unknown package and returns 202
                - DISABLE_UNKONWN_PACKAGE_FLOW flag is 0: Than checks below condition.
                    - INVOKE_API_WORKERS flag is 1: Trigger bayesianApiFlow to fetch
                                                    Package details
                    - INVOKE_API_WORKERS flag is 0: Trigger bayesianFlow to fetch
                                                    Package details

        :return:
            JSON Response
        """
        st = time.time()
        # Analytics Data
        metrics_payload = {
            "pid": os.getpid(),
            "hostname": HOSTNAME,
            "endpoint": request.endpoint,
            "request_method": "GET",
            "ecosystem": ecosystem,
            "package": package,
            "version": version
        }
        response_template = namedtuple("response_template",
                                       ["message", "status"])
        logger.info("Executed v2 API")
        package = urllib.parse.unquote(package)

        if re.findall('[!@#$%^&*()]', version):
            # Version should not contain special Characters.
            return response_template(
                {
                    'error':
                    "Package version should not have special characters."
                }, 400)

        if not check_for_accepted_ecosystem(ecosystem):
            msg = f"Ecosystem {ecosystem} is not supported for this request"
            raise HTTPError(400, msg)
        if ecosystem == 'maven':
            try:
                package = MavenCoordinates.normalize_str(package)
            except ValueError:
                msg = f"Invalid maven format - {package}"
                metrics_payload.update({
                    "status_code": 400,
                    "value": time.time() - st
                })
                _session.post(url=METRICS_SERVICE_URL + "/api/v1/prometheus",
                              json=metrics_payload)
                raise HTTPError(400, msg)
        package = case_sensitivity_transform(ecosystem, package)

        # Perform Component Analyses on Vendor specific Graph Edge.
        analyses_result = ComponentAnalyses(
            ecosystem, package, version).get_component_analyses_response()

        if analyses_result is not None:
            # Known component for Fabric8 Analytics
            server_create_component_bookkeeping(ecosystem, package, version,
                                                g.decoded_token)

            metrics_payload.update({
                "status_code": 200,
                "value": time.time() - st
            })
            _session.post(url=METRICS_SERVICE_URL + "/api/v1/prometheus",
                          json=metrics_payload)
            return analyses_result
        elif os.environ.get("DISABLE_UNKNOWN_PACKAGE_FLOW", "") == "1":
            msg = f"No data found for {ecosystem} package {package}/{version} " \
                  "ingetion flow skipped as DISABLE_UNKNOWN_PACKAGE_FLOW is enabled"

            return response_template({'error': msg}, 202)

        if os.environ.get("INVOKE_API_WORKERS", "") == "1":
            # Trigger the unknown component ingestion.
            server_create_analysis(ecosystem,
                                   package,
                                   version,
                                   user_profile=g.decoded_token,
                                   api_flow=True,
                                   force=False,
                                   force_graph_sync=True)
            msg = f"Package {ecosystem}/{package}/{version} is unavailable. " \
                  "The package will be available shortly," \
                  " please retry after some time."

            metrics_payload.update({
                "status_code": 202,
                "value": time.time() - st
            })
            _session.post(url=METRICS_SERVICE_URL + "/api/v1/prometheus",
                          json=metrics_payload)

            return response_template({'error': msg}, 202)

        # No data has been found and INVOKE_API_WORKERS flag is down.
        server_create_analysis(ecosystem,
                               package,
                               version,
                               user_profile=g.decoded_token,
                               api_flow=False,
                               force=False,
                               force_graph_sync=True)
        msg = f"No data found for {ecosystem} package {package}/{version}"

        metrics_payload.update({"status_code": 404, "value": time.time() - st})
        _session.post(url=METRICS_SERVICE_URL + "/api/v1/prometheus",
                      json=metrics_payload)

        raise HTTPError(404, msg)