From 2ae8ade3d116c6df4ae286a375e1b2d0efab621a Mon Sep 17 00:00:00 2001 From: willyw0nka Date: Wed, 10 Apr 2024 15:23:39 +0200 Subject: [PATCH 1/3] feat: add to_json decorator to all dataclasses --- pygrype/core/decorators/to_json.py | 11 +++++++++++ pygrype/core/grype_version.py | 2 ++ pygrype/core/list/db_meta_data.py | 1 + pygrype/core/scan/artifact.py | 2 ++ pygrype/core/scan/cvss.py | 2 ++ pygrype/core/scan/cvss_metrics.py | 1 + pygrype/core/scan/distro.py | 2 ++ pygrype/core/scan/layer.py | 2 ++ pygrype/core/scan/location.py | 3 +++ pygrype/core/scan/match.py | 2 ++ pygrype/core/scan/match_details.py | 2 ++ pygrype/core/scan/match_details_found.py | 2 ++ pygrype/core/scan/match_details_searched_by.py | 3 ++- pygrype/core/scan/package.py | 2 ++ pygrype/core/scan/scan.py | 3 ++- pygrype/core/scan/scan_source.py | 2 ++ pygrype/core/scan/searched_by_distro.py | 2 ++ pygrype/core/scan/target.py | 3 ++- pygrype/core/scan/upstream.py | 2 ++ pygrype/core/scan/vulnerability.py | 2 ++ pygrype/core/scan/vulnerability_fix.py | 2 ++ 21 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 pygrype/core/decorators/to_json.py diff --git a/pygrype/core/decorators/to_json.py b/pygrype/core/decorators/to_json.py new file mode 100644 index 0000000..3997145 --- /dev/null +++ b/pygrype/core/decorators/to_json.py @@ -0,0 +1,11 @@ +import json +from dataclasses import asdict +from typing import Type, TypeVar + +T = TypeVar('T', bound='DataClass') + +def to_json(cls: Type[T]) -> Type[T]: + def to_json_func(self: T): + return json.dumps(asdict(self)) + cls.to_json = to_json_func + return cls diff --git a/pygrype/core/grype_version.py b/pygrype/core/grype_version.py index 7b73605..d08d6f4 100644 --- a/pygrype/core/grype_version.py +++ b/pygrype/core/grype_version.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class GrypeVersion: version: str syft_version: str diff --git a/pygrype/core/list/db_meta_data.py b/pygrype/core/list/db_meta_data.py index 890ebe5..f283748 100644 --- a/pygrype/core/list/db_meta_data.py +++ b/pygrype/core/list/db_meta_data.py @@ -2,6 +2,7 @@ @dataclass +@to_json class DBMetaData: built: str version: int diff --git a/pygrype/core/scan/artifact.py b/pygrype/core/scan/artifact.py index 9782ab0..992d747 100644 --- a/pygrype/core/scan/artifact.py +++ b/pygrype/core/scan/artifact.py @@ -1,11 +1,13 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.location import Location from pygrype.core.scan.upstream import Upstream @dataclass +@to_json class Artifact: id: str name: str diff --git a/pygrype/core/scan/cvss.py b/pygrype/core/scan/cvss.py index 2bf2852..84b1f4d 100644 --- a/pygrype/core/scan/cvss.py +++ b/pygrype/core/scan/cvss.py @@ -1,10 +1,12 @@ from dataclasses import dataclass from typing import Optional +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.cvss_metrics import CVSSMetrics @dataclass +@to_json class CVSS: source: Optional[str] type: Optional[str] diff --git a/pygrype/core/scan/cvss_metrics.py b/pygrype/core/scan/cvss_metrics.py index cf025e0..92bd389 100644 --- a/pygrype/core/scan/cvss_metrics.py +++ b/pygrype/core/scan/cvss_metrics.py @@ -2,6 +2,7 @@ @dataclass +@to_json class CVSSMetrics: baseScore: float exploitabilityScore: float diff --git a/pygrype/core/scan/distro.py b/pygrype/core/scan/distro.py index 907c8b6..de95770 100644 --- a/pygrype/core/scan/distro.py +++ b/pygrype/core/scan/distro.py @@ -1,8 +1,10 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Distro: name: str version: str diff --git a/pygrype/core/scan/layer.py b/pygrype/core/scan/layer.py index 62ee7df..b3ee3d3 100644 --- a/pygrype/core/scan/layer.py +++ b/pygrype/core/scan/layer.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Layer: mediaType: str digest: str diff --git a/pygrype/core/scan/location.py b/pygrype/core/scan/location.py index 75245d4..ec4b93b 100644 --- a/pygrype/core/scan/location.py +++ b/pygrype/core/scan/location.py @@ -1,7 +1,10 @@ from dataclasses import dataclass from typing import Optional +from pygrype.core.decorators.to_json import to_json + @dataclass +@to_json class Location: path: str layerID: Optional[str] = None diff --git a/pygrype/core/scan/match.py b/pygrype/core/scan/match.py index e8f4581..9584f85 100644 --- a/pygrype/core/scan/match.py +++ b/pygrype/core/scan/match.py @@ -1,12 +1,14 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.artifact import Artifact from pygrype.core.scan.match_details import MatchDetails from pygrype.core.scan.vulnerability import Vulnerability @dataclass +@to_json class Match: vulnerability: Vulnerability relatedVulnerabilities: List[Vulnerability] diff --git a/pygrype/core/scan/match_details.py b/pygrype/core/scan/match_details.py index e36c2db..386ca21 100644 --- a/pygrype/core/scan/match_details.py +++ b/pygrype/core/scan/match_details.py @@ -1,10 +1,12 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.match_details_found import MatchDetailsFound from pygrype.core.scan.match_details_searched_by import MatchDetailsSearchedBy @dataclass +@to_json class MatchDetails: type: str matcher: str diff --git a/pygrype/core/scan/match_details_found.py b/pygrype/core/scan/match_details_found.py index 93b9b12..5bff53a 100644 --- a/pygrype/core/scan/match_details_found.py +++ b/pygrype/core/scan/match_details_found.py @@ -1,8 +1,10 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class MatchDetailsFound: vulnerabilityID: str versionConstraint: str diff --git a/pygrype/core/scan/match_details_searched_by.py b/pygrype/core/scan/match_details_searched_by.py index 906c780..d8162ad 100644 --- a/pygrype/core/scan/match_details_searched_by.py +++ b/pygrype/core/scan/match_details_searched_by.py @@ -1,11 +1,12 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.package import Package from pygrype.core.scan.searched_by_distro import Distro - @dataclass +@to_json class MatchDetailsSearchedBy: namespace: str cpes: Optional[List[str]] diff --git a/pygrype/core/scan/package.py b/pygrype/core/scan/package.py index c4b92f1..cc3dc7d 100644 --- a/pygrype/core/scan/package.py +++ b/pygrype/core/scan/package.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Package: name: str version: str diff --git a/pygrype/core/scan/scan.py b/pygrype/core/scan/scan.py index 2f8ba3d..37c8a74 100644 --- a/pygrype/core/scan/scan.py +++ b/pygrype/core/scan/scan.py @@ -1,12 +1,13 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.distro import Distro from pygrype.core.scan.match import Match from pygrype.core.scan.scan_source import ScanSource - @dataclass +@to_json class Scan: matches: List[Match] source: ScanSource diff --git a/pygrype/core/scan/scan_source.py b/pygrype/core/scan/scan_source.py index 3a85e48..433fefb 100644 --- a/pygrype/core/scan/scan_source.py +++ b/pygrype/core/scan/scan_source.py @@ -1,10 +1,12 @@ from dataclasses import dataclass from typing import Union +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.target import Target @dataclass +@to_json class ScanSource: type: str target: Union[Target, str] diff --git a/pygrype/core/scan/searched_by_distro.py b/pygrype/core/scan/searched_by_distro.py index 72984d0..23f78d9 100644 --- a/pygrype/core/scan/searched_by_distro.py +++ b/pygrype/core/scan/searched_by_distro.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Distro: type: str version: str diff --git a/pygrype/core/scan/target.py b/pygrype/core/scan/target.py index b53b747..0a1fbce 100644 --- a/pygrype/core/scan/target.py +++ b/pygrype/core/scan/target.py @@ -1,10 +1,11 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.layer import Layer - @dataclass +@to_json class Target: userInput: str imageID: str diff --git a/pygrype/core/scan/upstream.py b/pygrype/core/scan/upstream.py index ca88c45..6350bd2 100644 --- a/pygrype/core/scan/upstream.py +++ b/pygrype/core/scan/upstream.py @@ -1,6 +1,8 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class Upstream: name: str diff --git a/pygrype/core/scan/vulnerability.py b/pygrype/core/scan/vulnerability.py index 6969f3e..b64a72c 100644 --- a/pygrype/core/scan/vulnerability.py +++ b/pygrype/core/scan/vulnerability.py @@ -1,11 +1,13 @@ from dataclasses import dataclass from typing import List, Optional +from pygrype.core.decorators.to_json import to_json from pygrype.core.scan.cvss import CVSS from pygrype.core.scan.vulnerability_fix import VulnerabilityFix @dataclass +@to_json class Vulnerability: id: str description: Optional[str] diff --git a/pygrype/core/scan/vulnerability_fix.py b/pygrype/core/scan/vulnerability_fix.py index 5e7acd8..d48b128 100644 --- a/pygrype/core/scan/vulnerability_fix.py +++ b/pygrype/core/scan/vulnerability_fix.py @@ -1,8 +1,10 @@ from dataclasses import dataclass from typing import List +from pygrype.core.decorators.to_json import to_json @dataclass +@to_json class VulnerabilityFix: versions: List[str] state: str From 7485709e63f9dab31cc7b480431ce2e576e8c50c Mon Sep 17 00:00:00 2001 From: willyw0nka Date: Wed, 10 Apr 2024 15:32:21 +0200 Subject: [PATCH 2/3] fix: dont bound TypeVar T --- pygrype/core/decorators/to_json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygrype/core/decorators/to_json.py b/pygrype/core/decorators/to_json.py index 3997145..36c7424 100644 --- a/pygrype/core/decorators/to_json.py +++ b/pygrype/core/decorators/to_json.py @@ -2,7 +2,7 @@ from dataclasses import asdict from typing import Type, TypeVar -T = TypeVar('T', bound='DataClass') +T = TypeVar('T') def to_json(cls: Type[T]) -> Type[T]: def to_json_func(self: T): From 56d708cee5e3a347153d976e00eb174f45738c2d Mon Sep 17 00:00:00 2001 From: willyw0nka Date: Wed, 10 Apr 2024 15:35:31 +0200 Subject: [PATCH 3/3] fix: add missing imports --- pygrype/core/list/db_meta_data.py | 1 + pygrype/core/scan/cvss_metrics.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pygrype/core/list/db_meta_data.py b/pygrype/core/list/db_meta_data.py index f283748..86123a6 100644 --- a/pygrype/core/list/db_meta_data.py +++ b/pygrype/core/list/db_meta_data.py @@ -1,5 +1,6 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass @to_json diff --git a/pygrype/core/scan/cvss_metrics.py b/pygrype/core/scan/cvss_metrics.py index 92bd389..8e1fb98 100644 --- a/pygrype/core/scan/cvss_metrics.py +++ b/pygrype/core/scan/cvss_metrics.py @@ -1,5 +1,6 @@ from dataclasses import dataclass +from pygrype.core.decorators.to_json import to_json @dataclass @to_json