forked from intel/cve-bin-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: John Andersen <[email protected]>
- Loading branch information
Showing
16 changed files
with
166 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,45 @@ | ||
# Copyright (C) 2022 Intel Corporation | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
import sys | ||
|
||
from cve_bin_tool.parsers.dart import DartParser | ||
from cve_bin_tool.parsers.go import GoParser | ||
from cve_bin_tool.parsers.java import JavaParser | ||
from cve_bin_tool.parsers.javascript import JavascriptParser | ||
from cve_bin_tool.parsers.perl import PerlParser | ||
from cve_bin_tool.parsers.php import PhpParser | ||
from cve_bin_tool.parsers.python import PythonParser, PythonRequirementsParser | ||
from cve_bin_tool.parsers.r import RParser | ||
from cve_bin_tool.parsers.ruby import RubyParser | ||
from cve_bin_tool.parsers.rust import RustParser | ||
from cve_bin_tool.parsers.swift import SwiftParser | ||
|
||
valid_files = { | ||
"pom.xml": JavaParser, | ||
"package-lock.json": JavascriptParser, | ||
"Cargo.lock": RustParser, | ||
"renv.lock": RParser, | ||
"requirements.txt": PythonRequirementsParser, | ||
"go.mod": GoParser, | ||
"PKG-INFO: ": PythonParser, | ||
"METADATA: ": PythonParser, | ||
"Gemfile.lock": RubyParser, | ||
"Package.resolved": SwiftParser, | ||
"composer.lock": PhpParser, | ||
"cpanfile": PerlParser, | ||
"pubspec.lock": DartParser, | ||
} | ||
|
||
if sys.version_info >= (3, 10): | ||
from importlib import metadata as importlib_metadata | ||
else: | ||
import importlib_metadata | ||
|
||
from cve_bin_tool.parsers import Parser | ||
|
||
|
||
PARSERS_ENTRYPOINT = "cve_bin_tool.parsers" | ||
|
||
|
||
def load_valid_files() -> dict[str, list[type[Parser]]]: | ||
"""Loads file parsers""" | ||
valid_files = {} | ||
for entrypoint in importlib_metadata.entry_points().select( | ||
group=PARSERS_ENTRYPOINT | ||
): | ||
parser_cls = entrypoint.load() | ||
for match_filename in getattr(parser_cls, "PARSER_MATCH_FILENAMES", []): | ||
valid_files.setdefault(match_filename, []) | ||
valid_files[match_filename].append(parser_cls) | ||
for match_filename in valid_files: | ||
valid_files[match_filename] = list(set(valid_files[match_filename])) | ||
return valid_files | ||
|
||
|
||
valid_files = load_valid_files() | ||
|
||
|
||
def parse(filename, output, cve_db, logger): | ||
""" | ||
Parses the given filename using the appropriate parser. | ||
""" | ||
parsers = [] | ||
for file in list(valid_files.keys()): | ||
if file in output: | ||
parser = valid_files[file](cve_db, logger) | ||
yield from parser.run_checker(filename) | ||
for valid_file_parser in valid_files[file]: | ||
parsers.append(valid_file_parser(cve_db, logger)) | ||
for parser in parsers: | ||
yield from parser.run_checker(filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
import unittest | ||
|
||
from cve_bin_tool.parsers.parse import valid_files as actual_valid_files | ||
from cve_bin_tool.parsers.dart import DartParser | ||
from cve_bin_tool.parsers.go import GoParser | ||
from cve_bin_tool.parsers.java import JavaParser | ||
from cve_bin_tool.parsers.javascript import JavascriptParser | ||
from cve_bin_tool.parsers.perl import PerlParser | ||
from cve_bin_tool.parsers.php import PhpParser | ||
from cve_bin_tool.parsers.python import PythonParser, PythonRequirementsParser | ||
from cve_bin_tool.parsers.r import RParser | ||
from cve_bin_tool.parsers.ruby import RubyParser | ||
from cve_bin_tool.parsers.rust import RustParser | ||
from cve_bin_tool.parsers.swift import SwiftParser | ||
|
||
|
||
EXPECTED_VALID_FILES = { | ||
"pom.xml": [JavaParser], | ||
"package-lock.json": [JavascriptParser], | ||
"Cargo.lock": [RustParser], | ||
"renv.lock": [RParser], | ||
"requirements.txt": [PythonRequirementsParser], | ||
"go.mod": [GoParser], | ||
"PKG-INFO: ": [PythonParser], | ||
"METADATA: ": [PythonParser], | ||
"Gemfile.lock": [RubyParser], | ||
"Package.resolved": [SwiftParser], | ||
"composer.lock": [PhpParser], | ||
"cpanfile": [PerlParser], | ||
"pubspec.lock": [DartParser], | ||
} | ||
|
||
|
||
class TestParsers: | ||
@pytest.mark.asyncio | ||
async def test_parser_match_filenames_results_in_correct_valid_files(self): | ||
unittest.TestCase().assertDictEqual( | ||
EXPECTED_VALID_FILES, | ||
actual_valid_files, | ||
"Expected registered file types not the same as loaded file types, second dict is actual file types loaded, first is expected", | ||
) |