Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper to verify release assets with sigstore #229

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions aiogithubapi/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
"""Helpers for AIOGitHubAPI."""
from __future__ import annotations

from io import BytesIO
from typing import Optional

import aiohttp
from sigstore.verify import (
VerificationMaterials,
VerificationResult,
Verifier,
models,
policy,
)

from .const import HttpMethod, Repository, RepositoryType
from .legacy.helpers import (
Expand Down Expand Up @@ -37,3 +45,40 @@ async def async_call_api(
return await legacy_async_call_api(
session, method, url, headers, params, data, jsondata, returnjson
)


def sigstore_verify_release_asset(
asset: bytes,
signature_bundle: bytes,
repository: str,
workflow: str,
tag: str,
*,
workflow_name: str | None = None,
workflow_trigger: str | None = None,
offline_verification: bool = False,
**kwargs,
) -> VerificationResult:
"""Verify release asset."""
verifier = Verifier.production()
policies = [
policy.Identity(
identity=f"https://github.com/{repository}/.github/workflows/{workflow}@refs/tags/{tag}",
issuer="https://token.actions.githubusercontent.com",
),
policy.GitHubWorkflowRepository(repository),
policy.GitHubWorkflowRef(f"refs/tags/{tag}"),
]
if workflow_trigger:
policies.append(policy.GitHubWorkflowTrigger(workflow_trigger))
if workflow_name:
policies.append(policy.GitHubWorkflowName(workflow_name))

return verifier.verify(
VerificationMaterials.from_bundle(
input_=BytesIO(asset),
bundle=models.Bundle().from_json(signature_bundle),
offline=offline_verification,
),
policy=policy.AllOf(policies),
)
Loading