예제 #1
0
 def test_npm_fetcher(self, npm, package, expected, ok):
     """Test NpmReleasesFetcher."""
     f = NpmReleasesFetcher(npm)
     if ok:
         _, releases = f.fetch_releases(package)
         assert set(releases) >= expected
     else:
         with pytest.raises(ValueError):
             f.fetch_releases(package)
예제 #2
0
    def _get_package_versions(self, ecosystem, package_name):
        """Get all versions for given package name.

        :param ecosystem: f8a_worker.models.Ecosystem, ecosystem
        :param package_name: str, package name
        :return: list of all package versions
        """
        # Intentionally not checking ecosystem backend here.
        # We simply don't know about CVEs in 3rd party repositories.
        if ecosystem.name == 'maven':
            return MavenReleasesFetcher(ecosystem).fetch_releases(
                package_name)[1]
        if ecosystem.name == 'pypi':
            return PypiReleasesFetcher(ecosystem).fetch_releases(
                package_name)[1]
        if ecosystem.name == 'npm':
            return NpmReleasesFetcher(ecosystem).fetch_releases(
                package_name)[1]
        return []
예제 #3
0
 def test_npm_fetcher(self, npm, package, expected):
     """Test NpmReleasesFetcher."""
     f = NpmReleasesFetcher(npm)
     _, releases = f.fetch_releases(package)
     assert set(releases) >= expected
예제 #4
0
    def fetch_npm_artifact(ecosystem, name, version, target_dir):
        """Fetch npm artifact using system 'npm' tool."""
        git = Git.create_git(target_dir)

        npm_cmd = ['npm', '--registry', ecosystem.fetch_url]

        # $ npm config get cache
        # /root/.npm
        cache_path = TimedCommand.get_command_output(
            npm_cmd + ['config', 'get', 'cache'], graceful=False).pop()

        # add package to cache:
        # /root/.npm/express/
        # └── 4.13.4
        #      ├── package
        #      │   ├── History.md
        #      │   ├── index.js
        #      │   ├── lib
        #      │   ├── LICENSE
        #      │   ├── package.json
        #      │   └── Readme.md
        #      └── package.tgz
        # 3 directories, 6 files
        name_ver = name

        try:
            # importing here to avoid circular dependency
            from f8a_worker.solver import NpmReleasesFetcher

            version_list = NpmReleasesFetcher(ecosystem).fetch_releases(
                name_ver)[1]
            if version not in version_list:
                raise NotABugTaskError(
                    "Provided version is not supported '%s'" % name)
            else:
                name_ver = "{}@{}".format(name, version)
        except ValueError as e:
            raise NotABugTaskError(
                'No versions for package NPM package {p} ({e})'.format(
                    p=name, e=str(e)))

        # make sure the artifact is not in the cache yet
        TimedCommand.get_command_output(npm_cmd + ['cache', 'clean', name],
                                        graceful=False)
        logger.info("downloading npm module %s", name_ver)
        cmd = npm_cmd + ['cache', 'add', name_ver]
        TimedCommand.get_command_output(cmd, graceful=False)

        # copy tarball to workpath
        tarball_name = "package.tgz"
        glob_path = os.path.join(cache_path, name, "*")
        cache_abs_path = os.path.abspath(glob.glob(glob_path).pop())
        artifact_path = os.path.join(cache_abs_path, tarball_name)
        logger.debug("[cache] tarball path = %s", artifact_path)
        artifact_path = shutil.copy(artifact_path, target_dir)

        logger.debug("[workdir] tarball path = %s", artifact_path)
        # Prior to npm-2.x.x (Fedora 24)
        # npm client was repackaging modules on download. It modified file permissions inside
        # package.tgz so they matched UID/GID of a user running npm command. Therefore its
        # digest was different then of a tarball downloaded directly from registry.npmjs.org.
        digest = compute_digest(artifact_path)
        Archive.extract(artifact_path, target_dir)
        Archive.fix_permissions(os.path.join(cache_abs_path, 'package'))

        # copy package/package.json over the extracted one,
        # because it contains (since npm >= 2.x.x) more information.
        npm_package_json = os.path.join(cache_abs_path, 'package',
                                        'package.json')
        shutil.copy(npm_package_json, target_dir)
        # copy package/npm-shrinkwrap.json to target_dir
        npm_shrinkwrap_json = os.path.join(target_dir, 'package',
                                           'npm-shrinkwrap.json')
        if os.path.isfile(npm_shrinkwrap_json):
            shutil.copy(npm_shrinkwrap_json, target_dir)
        git.add_and_commit_everything()
        return digest, artifact_path