Skip to content

Commit

Permalink
Merge pull request #1 from willyw0nka/add-dataclasses
Browse files Browse the repository at this point in the history
Multiple improvements
  • Loading branch information
willyw0nka authored Aug 16, 2023
2 parents 3d349e4 + fa452c8 commit f151a87
Show file tree
Hide file tree
Showing 27 changed files with 321 additions and 31 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ jobs:
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install -e .
- name: Install grype
run: |
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
grype version
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
38 changes: 24 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Supported commands
- [x] delete
- [ ] diff
- [ ] import
- [ ] list
- [x] list
- [ ] status
- [x] update
- [ ] ~~help~~
Expand All @@ -35,31 +35,41 @@ pip install pygrype
## Usage
Instantiate `Grype` using the default path
```python3
from pygrype.grype import Grype
from pygrype import Grype
grype = Grype()
```
or specify the binary
```python3
from pygrype.grype import Grype
from pygrype import Grype
grype = Grype(path='/opt/grype')
```

## Full example
```python3
from pygrype.grype import Grype
from pygrype import Grype

image = 'alpine:3.12'
grype = Grype('/opt/grype')
grype = Grype()

version_info = grype.version()

version = grype.version()
print('Using Grype version {version}'.format(version=version['version']))
print(f'Using grype {version_info.version}')

print('Updating DB...')
grype.db.update()
images = [
'alpine:3.12',
'ubuntu:18.04',
'debian:9'
]

scan = grype.scan(image)
print('Image {image} has {matches} vlunerabilities'.format(
image=image,
matches=len(scan['matches'])))
for image in images:
scan = grype.scan(image)
criticals = len(list(filter(lambda x: x.vulnerability.severity.lower() == 'critical', scan.matches)))
print(f'{image} has {len(scan.matches)} vulnerabilities ({criticals} critical)')
```
Example output
```
Using grype 0.62.3
alpine:3.12 has 23 vulnerabilities (3 critical)
ubuntu:18.04 has 18 vulnerabilities (0 critical)
debian:9 has 213 vulnerabilities (23 critical)
```

1 change: 1 addition & 0 deletions pygrype/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .grype import Grype
15 changes: 15 additions & 0 deletions pygrype/core/grype_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from dataclasses import dataclass


@dataclass
class GrypeVersion:
version: str
syft_version: str
git_commit: str
git_description: str
build_date: str
go_version: str
compiler: str
platform: str
application: str
supported_db_schema: int
9 changes: 9 additions & 0 deletions pygrype/core/list/db_meta_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass


@dataclass
class DBMetaData:
built: str
version: int
url: str
checksum: str
19 changes: 19 additions & 0 deletions pygrype/core/scan/artifact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.scan.location import Location
from pygrype.core.scan.upstream import Upstream


@dataclass
class Artifact:
id: str
name: str
version: str
type: str
locations: List[Location]
language: str
licenses: List[str]
cpes: List[str]
purl: str
upstreams: List[Upstream]
13 changes: 13 additions & 0 deletions pygrype/core/scan/cvss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass
from typing import Optional

from pygrype.core.scan.cvss_metrics import CVSSMetrics


@dataclass
class CVSS:
source: Optional[str]
type: Optional[str]
version: str
vector: str
metrics: CVSSMetrics
8 changes: 8 additions & 0 deletions pygrype/core/scan/cvss_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class CVSSMetrics:
baseScore: float
exploitabilityScore: float
impactScore: float
9 changes: 9 additions & 0 deletions pygrype/core/scan/distro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass
from typing import List


@dataclass
class Distro:
name: str
version: str
idLike: List
8 changes: 8 additions & 0 deletions pygrype/core/scan/layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class Layer:
mediaType: str
digest: str
size: int
7 changes: 7 additions & 0 deletions pygrype/core/scan/location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass


@dataclass
class Location:
path: str
layerID: str
14 changes: 14 additions & 0 deletions pygrype/core/scan/match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.scan.artifact import Artifact
from pygrype.core.scan.match_details import MatchDetails
from pygrype.core.scan.vulnerability import Vulnerability


@dataclass
class Match:
vulnerability: Vulnerability
relatedVulnerabilities: List[Vulnerability]
matchDetails: List[MatchDetails]
artifact: Artifact
12 changes: 12 additions & 0 deletions pygrype/core/scan/match_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from dataclasses import dataclass

from pygrype.core.scan.match_details_found import MatchDetailsFound
from pygrype.core.scan.match_details_searched_by import MatchDetailsSearchedBy


@dataclass
class MatchDetails:
type: str
matcher: str
searchedBy: MatchDetailsSearchedBy
found: MatchDetailsFound
9 changes: 9 additions & 0 deletions pygrype/core/scan/match_details_found.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass
from typing import List, Optional


@dataclass
class MatchDetailsFound:
vulnerabilityID: str
versionConstraint: str
cpes: Optional[List[str]]
13 changes: 13 additions & 0 deletions pygrype/core/scan/match_details_searched_by.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass
from typing import List, Optional

from pygrype.core.scan.package import Package
from pygrype.core.scan.searched_by_distro import Distro


@dataclass
class MatchDetailsSearchedBy:
namespace: str
cpes: Optional[List[str]]
Package: Optional[Package]
distro: Optional[Distro]
7 changes: 7 additions & 0 deletions pygrype/core/scan/package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass


@dataclass
class Package:
name: str
version: str
14 changes: 14 additions & 0 deletions pygrype/core/scan/scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.scan.distro import Distro
from pygrype.core.scan.match import Match
from pygrype.core.scan.scan_source import ScanSource


@dataclass
class Scan:
matches: List[Match]
source: ScanSource
distro: Distro
# descriptor: GrypeScanDescriptor
9 changes: 9 additions & 0 deletions pygrype/core/scan/scan_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass

from pygrype.core.scan.target import Target


@dataclass
class ScanSource:
type: str
target: Target
7 changes: 7 additions & 0 deletions pygrype/core/scan/searched_by_distro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass


@dataclass
class Distro:
type: str
version: str
20 changes: 20 additions & 0 deletions pygrype/core/scan/target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.scan.layer import Layer


@dataclass
class Target:
userInput: str
imageID: str
manifestDigest: str
mediaType: str
tags: List[str]
imageSize: int
layers: List[Layer]
manifest: str
config: str
repoDigests: List[str]
architecture: str
os: str
6 changes: 6 additions & 0 deletions pygrype/core/scan/upstream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dataclasses import dataclass


@dataclass
class Upstream:
name: str
17 changes: 17 additions & 0 deletions pygrype/core/scan/vulnerability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from dataclasses import dataclass
from typing import List, Optional

from pygrype.core.scan.cvss import CVSS
from pygrype.core.scan.vulnerability_fix import VulnerabilityFix


@dataclass
class Vulnerability:
id: str
description: Optional[str]
dataSource: str
namespace: str
severity: Optional[str]
urls: List[str]
cvss: List[CVSS]
fix: Optional[VulnerabilityFix]
8 changes: 8 additions & 0 deletions pygrype/core/scan/vulnerability_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass
from typing import List


@dataclass
class VulnerabilityFix:
versions: List[str]
state: str
Loading

0 comments on commit f151a87

Please sign in to comment.