Skip to content

Commit

Permalink
Add docker based grype backend
Browse files Browse the repository at this point in the history
  • Loading branch information
robberwick committed Jul 28, 2024
1 parent ddf1b35 commit 7aa199a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions pygrype/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .grype import Grype
from .core.backends.binary import GrypeBinaryBackend
from .core.backends.docker import GrypeDockerBackend

50 changes: 50 additions & 0 deletions pygrype/core/backends/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import subprocess
from subprocess import CompletedProcess

from pygrype.core.backends.base import GrypeBackend


DOCKER_EXE = 'docker'
IMAGE_NAME = 'anchore/grype'


class GrypeDockerBackend(GrypeBackend):

def __init__(self, tag: str = "latest") -> None:
self.tag: str = tag
super().__init__()

@property
def docker_image(self) -> str:
return f"{IMAGE_NAME}:{self.tag}"

def ensure_backend(self):
try:
# Check if Docker is installed
subprocess.run(
[DOCKER_EXE, '--version'],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
except subprocess.CalledProcessError:
raise Exception("Docker is not installed or not available in the PATH.")

try:
# Ensure the required Docker image is available
subprocess.run(
[DOCKER_EXE, 'pull', self.docker_image],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
except subprocess.CalledProcessError:
raise Exception(f"Failed to pull the required Docker image '{self.docker_image}'.")

@property
def executable_string(self) -> str:
return DOCKER_EXE

def execute(self, *args) -> CompletedProcess:
docker_args = ['run', '--rm', self.docker_image] + list(args)
return super().execute(*docker_args)

0 comments on commit 7aa199a

Please sign in to comment.