Skip to content

Commit

Permalink
Ensure cosign is installed before trying to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
almet committed Jan 29, 2025
1 parent 7bbd260 commit f7069a9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions dangerzone/updater/attestations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import subprocess
from tempfile import NamedTemporaryFile

from . import utils


def verify_attestation(
manifest: bytes, attestation_bundle: bytes, image_tag: str, expected_repo: str
Expand All @@ -9,6 +11,7 @@ def verify_attestation(
Look up the image attestation to see if the image has been built
on Github runners, and from a given repository.
"""
utils.ensure_cosign()

# Put the value in files and verify with cosign
with (
Expand Down
4 changes: 4 additions & 0 deletions dangerzone/updater/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ class SignatureMismatch(SignatureError):

class LocalSignatureNotFound(SignatureError):
pass


class CosignNotInstalledError(SignatureError):
pass
2 changes: 2 additions & 0 deletions dangerzone/updater/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"list_tags",
"get_manifest",
"get_attestation",
"Image",
"parse_image_location",
]

SIGSTORE_BUNDLE = "application/vnd.dev.sigstore.bundle.v0.3+json"
Expand Down
12 changes: 3 additions & 9 deletions dangerzone/updater/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Dict, List, Tuple

from ..container_utils import container_pull, load_image_hash
from . import errors, log
from . import errors, log, utils
from .registry import get_manifest_hash

try:
Expand All @@ -32,14 +32,6 @@ def get_config_dir() -> Path:
]


def is_cosign_installed() -> bool:
try:
subprocess.run(["cosign", "version"], capture_output=True, check=True)
return True
except subprocess.CalledProcessError:
return False


def signature_to_bundle(sig: Dict) -> Dict:
"""Convert a cosign-download signature to the format expected by cosign bundle."""
bundle = sig["Bundle"]
Expand All @@ -65,6 +57,7 @@ def signature_to_bundle(sig: Dict) -> Dict:
def verify_signature(signature: dict, pubkey: str) -> bool:
"""Verify a signature against a given public key"""

utils.ensure_cosign()
signature_bundle = signature_to_bundle(signature)

with (
Expand Down Expand Up @@ -221,6 +214,7 @@ def get_signatures(image: str, hash: str) -> List[Dict]:
"""
Retrieve the signatures from cosign download signature and convert each one to the "cosign bundle" format.
"""
utils.ensure_cosign()

process = subprocess.run(
["cosign", "download", "signature", f"{image}@sha256:{hash}"],
Expand Down
10 changes: 10 additions & 0 deletions dangerzone/updater/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import subprocess

from . import errors


def ensure_cosign() -> None:
try:
subprocess.run(["cosign", "version"], capture_output=True, check=True)
except subprocess.CalledProcessError:
raise errors.CosignNotInstalledError()

0 comments on commit f7069a9

Please sign in to comment.