Skip to content
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
142 changes: 142 additions & 0 deletions .github/scripts/verify_provenance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env python3
"""Verify that every file of a PyPI release carries PEP 740 provenance.

The script talks to the public PyPI Integrity API:

https://pypi.org/integrity/{project}/{version}/{filename}/provenance

For each distribution file of the given release it checks that at least one
attestation bundle is present and prints the trusted publisher identity the
attestations are bound to. This is what makes the "provenance" indicator on
the PyPI project page show up, so the release workflow runs it after every
upload.

Exit codes:
0 - every file is attested (or --allow-missing was given)
1 - at least one file has no provenance
2 - the release could not be queried at all

Usage:
python .github/scripts/verify_provenance.py --project localsitemap --version 2.1.0
"""

import argparse
import json
import sys
import time
import urllib.error
import urllib.request

PYPI_JSON = "https://pypi.org/pypi/{project}/{version}/json"
PYPI_INTEGRITY = "https://pypi.org/integrity/{project}/{version}/{filename}/provenance"
USER_AGENT = "localsitemap-release-check/1.0"


def _get_json(url, timeout=30):
request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(request, timeout=timeout) as response:
return json.loads(response.read().decode("utf-8"))


def _release_files(project, version):
data = _get_json(PYPI_JSON.format(project=project, version=version))
return [item["filename"] for item in data.get("urls", [])]


def _provenance(project, version, filename):
url = PYPI_INTEGRITY.format(project=project, version=version, filename=filename)
try:
return _get_json(url)
except urllib.error.HTTPError as error:
if error.code == 404:
return None
raise


def _describe(bundles):
descriptions = []
for bundle in bundles:
publisher = bundle.get("publisher") or {}
parts = [
str(publisher.get("kind", "unknown")),
str(publisher.get("repository", "")),
str(publisher.get("workflow", "")),
]
environment = publisher.get("environment")
if environment:
parts.append("env=%s" % environment)
descriptions.append(" ".join(part for part in parts if part))
return "; ".join(descriptions)


def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--project", default="localsitemap")
parser.add_argument("--version", required=True)
parser.add_argument(
"--allow-missing",
action="store_true",
help="do not fail when a file has no provenance",
)
parser.add_argument(
"--attempts", type=int, default=5,
help="how many times to retry while PyPI propagates a new release",
)
parser.add_argument("--delay", type=float, default=6.0, help="seconds between retries")
args = parser.parse_args(argv)

filenames = []
for attempt in range(1, max(1, args.attempts) + 1):
try:
filenames = _release_files(args.project, args.version)
if filenames:
break
except (urllib.error.URLError, ValueError) as error:
print("could not query PyPI (%s), attempt %d" % (error, attempt))
if attempt < args.attempts:
time.sleep(args.delay)

if not filenames:
print(
"error: no files found for %s %s on PyPI" % (args.project, args.version),
file=sys.stderr,
)
return 2

missing = []
for filename in filenames:
data = None
for attempt in range(1, max(1, args.attempts) + 1):
try:
data = _provenance(args.project, args.version, filename)
if data:
break
except (urllib.error.URLError, ValueError) as error:
print(" %s: query failed (%s), attempt %d" % (filename, error, attempt))
if attempt < args.attempts:
time.sleep(args.delay)

bundles = (data or {}).get("attestation_bundles") or []
if bundles:
print("OK %s -> %s" % (filename, _describe(bundles)))
else:
print("MISS %s has no provenance on PyPI" % filename)
missing.append(filename)

if missing:
if args.allow_missing:
print("warning: %d file(s) without provenance" % len(missing))
return 0
print(
"error: %d file(s) were published without provenance: %s"
% (len(missing), ", ".join(missing)),
file=sys.stderr,
)
return 1

print("All %d file(s) are attested with verifiable provenance." % len(filenames))
return 0


if __name__ == "__main__":
sys.exit(main())
158 changes: 133 additions & 25 deletions .github/workflows/build_and_publish.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,142 @@
name: Publish Python 🐍 distributions 📦 to PyPI

# Publishing uses PyPI Trusted Publishing (OIDC) - no API token is stored in
# this repository. The publisher configured on PyPI must match:
# owner/repository : Infinitode/LocalSiteMap
# workflow : build_and_publish.yml
# environment : (left blank - therefore no `environment:` key below)
#
# Every upload is signed with PEP 740 attestations, which is what powers the
# "provenance" indicator on the PyPI project page.

on:
push:
tags:
- '*'
- 'v[0-9]*'
- '[0-9]*'
workflow_dispatch:

permissions:
contents: read

jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions 📦 to PyPI
build:
name: Build distribution 📦
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Check out the repository
uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install build tooling
run: >-
python -m pip install --upgrade
build
twine
setuptools
wheel

- name: Run the test suite
run: python -m unittest -v test_localsitemap

- name: Determine the release version
id: version
run: |
set -euo pipefail
if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
VERSION="${GITHUB_REF_NAME#v}"
else
VERSION="$(python -c 'from localsitemap._version import __version__; print(__version__)')"
fi
python - "${VERSION}" <<'PY'
import pathlib
import re
import sys

version = sys.argv[1].strip()
pattern = r"^[0-9]+(\.[0-9]+)*((a|b|rc)[0-9]+)?(\.post[0-9]+)?(\.dev[0-9]+)?$"
if not re.match(pattern, version):
sys.exit("Invalid release version: %r" % version)

target = pathlib.Path("localsitemap/_version.py")
target.write_text(
'"""Single source of truth for the LocalSiteMap version.\n\n'
"The release workflow rewrites this file from the pushed git tag, so the\n"
'version published to PyPI always matches the tag.\n"""\n\n'
'__version__ = "%s"\n' % version,
encoding="utf-8",
)
print("Building version", version)
PY
echo "version=${VERSION}" >> "${GITHUB_OUTPUT}"

- name: Build sdist and wheel
run: python -m build

- name: Check the distribution metadata
run: python -m twine check --strict dist/*

- name: Upload the distribution artifacts
uses: actions/upload-artifact@v4
with:
name: python-package-distributions
path: dist/
if-no-files-found: error

publish:
name: Publish distribution 📦 to PyPI
needs: build
# Only real tag pushes publish. A manual "Run workflow" therefore builds,
# tests and validates the distributions without touching PyPI, which also
# makes it safe to run before Trusted Publishing is configured.
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
# NOTE: intentionally no `environment:` key. The PyPI trusted publisher is
# configured with a blank environment name, and adding one here would make
# the OIDC token claim mismatch and the upload fail.
permissions:
id-token: write # required for Trusted Publishing and attestations
steps:
- name: Download the distribution artifacts
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/

- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
attestations: true
print-hash: true

verify-provenance:
name: Verify published provenance 🛡️
needs:
- build
- publish
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@master
- name: Set up Python 3.12
uses: actions/setup-python@v3
with:
python-version: '3.12'
- name: Install pypa/setuptools
run: >-
python -m
pip install setuptools wheel
- name: Extract tag name
id: tag
run: echo ::set-output name=TAG_NAME::$(echo $GITHUB_REF | cut -d / -f 3)
- name: Update version in setup.py
run: >-
sed -i "s/{{VERSION_PLACEHOLDER}}/${{ steps.tag.outputs.TAG_NAME }}/g" setup.py
- name: Build a binary wheel
run: >-
python setup.py sdist bdist_wheel
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.PYPI_API_TOKEN }}
- name: Check out the repository
uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Confirm every published file carries provenance
env:
VERSION: ${{ needs.build.outputs.version }}
run: >-
python .github/scripts/verify_provenance.py
--project localsitemap
--version "${VERSION}"
Loading
Loading