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

gh-91172: Create a workflow for verifying bundled pip and setuptools #31885

Merged
merged 33 commits into from
Jun 22, 2022

Conversation

illia-v
Copy link
Contributor

@illia-v illia-v commented Mar 14, 2022

- uses: actions/checkout@v2
- name: Compare checksums of bundled pip and setuptools to ones published on PyPI
run: |
package_names=("pip" "setuptools")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could be moved to a script (say, under Tools/scripts), so it can also be run locally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, but it looks like all scripts in Tools/scripts are written in Python.
This is a Bash script, and it may not work on some platforms.

Do you know how compatible with different platforms scripts located in Tools/scripts should be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, or perhaps the Misc directory? The devguide says:

Various tools with configuration files as found in the Misc directory

I don't know what compatibility is needed, but as this script is primarily intended for Ubuntu on the CI, I'd stick with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. This is the latest run after I corrupted the pip wheel temporarily.

BTW, I added messages that GitHub has to show as annotations. But I could not see the annotations, maybe it does not show them for binary files…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They show up on the Summary page, for example clicking "🏠 Summary" at the top left:

image

👍

Co-authored-by: Hugo van Kemenade <[email protected]>
@illia-v illia-v force-pushed the bpo-47016 branch 2 times, most recently from a94b7fe to 23683cb Compare March 15, 2022 16:09
@illia-v illia-v requested a review from hugovk March 15, 2022 16:17
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🕊️🇺🇦✌️

@AA-Turner
Copy link
Member

@illia-v the below is my attempt at a Python rewrite:

"""Compares checksums for wheels in :mod:`ensurepip` against the Cheeseshop."""

import hashlib
import json
from pathlib import Path
import re
from urllib.request import urlopen

PACKAGE_NAMES = ("pip", "setuptools")
ROOT = Path(__file__).parent.parent / "Lib/ensurepip"
WHEEL_DIR = ROOT / "_bundled"
ENSURE_PIP_INIT_PY_TEXT = (ROOT / "__init__.py").read_text(encoding="utf-8")

# Contains names of successfully verified packages
verified_packages = {*()}

for package_name in PACKAGE_NAMES:
    # Find the package on disk
    package_path = next(WHEEL_DIR.glob(f"{package_name}*.whl"), None)
    if package_path is None:
        continue

    print(f"Verifying checksum for {package_path}.")

    # Find the version of the package used by ensurepip
    package_version_match = re.search(
        f'_{package_name.upper()}_VERSION = "([^"]+)',
        ENSURE_PIP_INIT_PY_TEXT
    )
    if package_version_match is None:
        continue
    package_version = package_version_match[1]

    # Get the SHA 256 digest from the Cheeseshop
    try:
        raw_text = urlopen(f"https://pypi.org/pypi/{package_name}/json").read()
    except (OSError, ValueError) as error:
        continue

    expected_digest = ""
    release_files = json.loads(raw_text)["releases"][package_version]
    for release_info in release_files:
        if package_path.name != release_info["filename"]:
            continue
        expected_digest = release_info["digests"].get("sha256", "")
    if expected_digest == "":
        continue

    # Compute the SHA 256 digest of the wheel on disk
    actual_digest = hashlib.sha256(package_path.read_bytes()).hexdigest()

    print(f"Expected digest: {expected_digest}")
    print(f"Actual digest:   {actual_digest}")

    # The messages are formatted to be parsed by GitHub Actions.
    # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message
    if actual_digest == expected_digest:
        print(f"::notice file={package_path}::"
              f"Successfully verified the checksum of the {package_name} wheel.\n")
        verified_packages.add(package_name)
    else:
        print(f"::error file={package_path}::"
              f"Failed to verify the checksum of the {package_name} wheel.\n")

# If we verified all packages, the set of package names and the set of verified
# packages will be equal.
if {*PACKAGE_NAMES} == verified_packages:
    raise SystemExit(0)

# Otherwise we failed to verify all the packages.
raise SystemExit(1)

A

@illia-v
Copy link
Contributor Author

illia-v commented Jun 6, 2022

@illia-v the below is my attempt at a Python rewrite:

@AA-Turner thanks! Could you please push it to this branch or create a pull request to it?

Also, what do you think about creating the {*()} and {*PACKAGE_NAMES} sets more explicitly using the set class? And printing an error near every continue?

BTW, isn't Warehouse the new name of what was called Cheese Shop?

@pradyunsg
Copy link
Member

BTW, isn't Warehouse the new name of what was called Cheese Shop?

Well, Python Package Index used to be called Cheese Shop (based on a Monty Python sketch about a Cheese Shop that didn't have any cheese) -- the current codebase for PyPI is called Warehouse: https://github.com/pypa/warehouse/

@ambv ambv added the needs backport to 3.9 only security fixes label Jun 22, 2022
@ambv ambv merged commit d36954b into python:main Jun 22, 2022
@miss-islington
Copy link
Contributor

Thanks @illia-v for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9, 3.10, 3.11.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 22, 2022
…tools (pythonGH-31885)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
@ambv ambv changed the title bpo-47016: Create a workflow for verifying bundled pip and setuptools gh-91172: Create a workflow for verifying bundled pip and setuptools Jun 22, 2022
@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 22, 2022
@bedevere-bot
Copy link

GH-94121 is a backport of this pull request to the 3.11 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 22, 2022
…tools (pythonGH-31885)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 22, 2022
…tools (pythonGH-31885)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Jun 22, 2022
@bedevere-bot
Copy link

GH-94123 is a backport of this pull request to the 3.9 branch.

@bedevere-bot
Copy link

GH-94124 is a backport of this pull request to the 3.8 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Jun 22, 2022
@bedevere-bot
Copy link

GH-94122 is a backport of this pull request to the 3.10 branch.

@ambv
Copy link
Contributor

ambv commented Jun 22, 2022

I added my security branches. Rationale: if there is a security-related bump of pip and/or setuptools, Python 3.7 - 3.9 security releases will be made with that bump.

ambv pushed a commit that referenced this pull request Jun 22, 2022
…H-31885) (GH-94121)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
ambv pushed a commit that referenced this pull request Jun 22, 2022
…H-31885) (GH-94123)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
ambv pushed a commit that referenced this pull request Jun 22, 2022
…H-31885) (GH-94122)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
ambv pushed a commit that referenced this pull request Jun 22, 2022
…H-31885) (GH-94124)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
@miss-islington
Copy link
Contributor

Thanks @illia-v for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 22, 2022
…tools (pythonGH-31885)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
@bedevere-bot
Copy link

GH-94126 is a backport of this pull request to the 3.7 branch.

ambv pushed a commit that referenced this pull request Jun 22, 2022
…H-31885) (GH-94126)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
(cherry picked from commit d36954b)

Co-authored-by: Illia Volochii <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants