Skip to content

Commit

Permalink
action: download default release assets to sign (#46)
Browse files Browse the repository at this point in the history
* action: download default release assets to sign

Signed-off-by: Andrew Pan <a@tny.town>

* README.md: doc `release-signing-artifacts` change

Signed-off-by: Andrew Pan <a@tny.town>

* action.py: use requests library for download

Signed-off-by: Andrew Pan <a@tny.town>

* Apply suggestions from code review

Signed-off-by: William Woodruff <william@yossarian.net>

---------

Signed-off-by: Andrew Pan <a@tny.town>
Signed-off-by: William Woodruff <william@yossarian.net>
Co-authored-by: William Woodruff <william@yossarian.net>
Co-authored-by: William Woodruff <william@trailofbits.com>
  • Loading branch information
3 people authored Feb 15, 2023
1 parent f3663a3 commit 7643db0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ Example:
The `release-signing-artifacts` setting controls whether or not `sigstore-python`
uploads signing artifacts to the release publishing event that triggered this run.

If enabled, this setting also re-uploads and signs GitHub's default source code artifacts,
as they are not guaranteed to be stable.

By default, no release assets are uploaded.

Requires the [`contents: write` permission](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token).
Expand Down
25 changes: 25 additions & 0 deletions action.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from glob import glob
from pathlib import Path

import requests

_HERE = Path(__file__).parent.resolve()
_TEMPLATES = _HERE / "templates"

Expand Down Expand Up @@ -53,6 +55,22 @@ def _log(msg):
print(msg, file=sys.stderr)


def _download_ref_asset(ext):
repo = os.getenv('GITHUB_REPOSITORY')
ref = os.getenv("GITHUB_REF")

artifact = Path(f"/tmp/{os.getenv('GITHUB_REF_NAME')}").with_suffix(ext)

# GitHub supports /:org/:repo/archive/:ref<.tar.gz|.zip>.
r = requests.get(f"https://github.com/{repo}/archive/{ref}{ext}", stream=True)
r.raise_for_status()
with artifact.open("wb") as io:
for chunk in r.iter_content(chunk_size=None):
io.write(chunk)

return str(artifact)


def _sigstore_sign(global_args, sign_args):
return ["python", "-m", "sigstore", *global_args, "sign", *sign_args]

Expand Down Expand Up @@ -163,6 +181,13 @@ def _fatal_help(msg):
else:
sigstore_verify_args.extend(["--cert-oidc-issuer", verify_oidc_issuer])

if os.getenv("GHA_SIGSTORE_PYTHON_RELEASE_SIGNING_ARTIFACTS") == "true":
for filetype in [".zip", ".tar.gz"]:
artifact = _download_ref_asset(filetype)
if artifact is not None:
signing_artifact_paths.append(artifact)
inputs.append(artifact)

for input_ in inputs:
# Forbid things that look like flags. This isn't a security boundary; just
# a way to prevent (less motivated) users from breaking the action on themselves.
Expand Down
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ runs:
GHA_SIGSTORE_PYTHON_VERIFY: "${{ inputs.verify }}"
GHA_SIGSTORE_PYTHON_VERIFY_CERT_IDENTITY: "${{ inputs.verify-cert-identity }}"
GHA_SIGSTORE_PYTHON_VERIFY_OIDC_ISSUER: "${{ inputs.verify-oidc-issuer }}"
GHA_SIGSTORE_PYTHON_RELEASE_SIGNING_ARTIFACTS: "${{ inputs.release-signing-artifacts }}"
GHA_SIGSTORE_PYTHON_INTERNAL_BE_CAREFUL_DEBUG: "${{ inputs.internal-be-careful-debug }}"
shell: bash

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sigstore ~= 1.1
requests ~= 2.28

0 comments on commit 7643db0

Please sign in to comment.