Repository navigation
fix(release): verify archives before signing #1096
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
Open
gty404
wants to merge
2
commits into
apache:main
Choose a base branch
from
gty404:fix/verify-release-archive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+337
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| name: Release Script Tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - 'dev/release/**' | ||
| - '.github/workflows/rc.yml' | ||
| - '.github/workflows/release_test.yml' | ||
| pull_request: | ||
| types: [opened, synchronize, reopened, ready_for_review] | ||
| paths: | ||
| - 'dev/release/**' | ||
| - '.github/workflows/rc.yml' | ||
| - '.github/workflows/release_test.yml' | ||
|
|
||
| concurrency: | ||
| group: ${{ github.repository }}-${{ github.head_ref || github.sha }}-${{ github.workflow }} | ||
| cancel-in-progress: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| release-tests: | ||
| if: ${{ github.event_name != 'pull_request' || github.event.pull_request.draft == false }} | ||
| runs-on: ${{ matrix.os }} | ||
| timeout-minutes: 5 | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-26.04, macos-26] | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| - name: Set up Python | ||
| uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 | ||
| with: | ||
| python-version: '3.13' | ||
| - name: Test release script | ||
| run: python3 -B -m unittest discover -s dev/release -p 'test_release_rc.py' -v |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import gzip | ||
| import hashlib | ||
| import os | ||
| from pathlib import Path | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
|
|
||
|
|
||
| MOCK_TOOL = r''' | ||
| import os | ||
| from pathlib import Path | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
|
|
||
| tool = Path(sys.argv[0]).name | ||
| args = sys.argv[1:] | ||
| if tool in ("git", "gzip"): | ||
| result = subprocess.run([os.environ["REAL_" + tool.upper()], *args]) | ||
| if tool == "git" and "archive" in args and os.environ.get("FAIL_ARCHIVE"): | ||
| sys.exit(1) | ||
| sys.exit(result.returncode) | ||
| elif tool == "gh" and args[:2] == ["run", "watch"]: | ||
| pass | ||
| elif tool == "gh" and args[:2] == ["release", "download"]: | ||
| destination = Path(args[args.index("--dir") + 1]) | ||
| for index, arg in enumerate(args): | ||
| if arg == "--pattern": | ||
| filename = args[index + 1] | ||
| target = destination / filename | ||
| if target.exists() and "--skip-existing" in args: | ||
| continue | ||
| shutil.copyfile(Path(os.environ["ASSETS"]) / filename, target) | ||
| elif tool == "gpg": | ||
| Path(args[args.index("--output") + 1]).write_text("test signature") | ||
| elif tool == "svn" and args[0] == "import": | ||
| pass | ||
| elif tool != "gh" or args[:2] != ["release", "upload"]: | ||
| sys.exit("Unexpected command: " + tool + " " + " ".join(args)) | ||
|
|
||
| if tool in ("gpg", "svn") or (tool == "gh" and args[:2] == ["release", "upload"]): | ||
| with open(os.environ["CALL_LOG"], "a") as log: | ||
| log.write(tool + " " + " ".join(args) + "\n") | ||
| ''' | ||
|
|
||
|
|
||
| class ReleaseRcTest(unittest.TestCase): | ||
| def setUp(self): | ||
| temporary = tempfile.TemporaryDirectory(prefix="iceberg-release-test-") | ||
| self.addCleanup(temporary.cleanup) | ||
| self.directory = Path(temporary.name) | ||
| self.repo = self.directory / "repo" | ||
| self.repo.mkdir() | ||
| self.assets = self.directory / "assets" | ||
| self.assets.mkdir() | ||
| self.calls = self.directory / "calls.log" | ||
| self.scratch = self.directory / "scratch" | ||
| self.scratch.mkdir() | ||
| self.name = "apache-iceberg-cpp-1.2.3" | ||
| self.tar_gz = self.name + ".tar.gz" | ||
| self.output = self.repo / (self.name + "-rc0") | ||
| self.real_git = shutil.which("git") | ||
| self.env = os.environ.copy() | ||
| for key in list(self.env): | ||
| if key.startswith(("RELEASE_", "GIT_")): | ||
| del self.env[key] | ||
| self.env.update({ | ||
| "GIT_CONFIG_GLOBAL": os.devnull, | ||
| "GIT_CONFIG_NOSYSTEM": "1", | ||
| "GIT_AUTHOR_NAME": "Release Test", | ||
| "GIT_AUTHOR_EMAIL": "test@example.invalid", | ||
| "GIT_COMMITTER_NAME": "Release Test", | ||
| "GIT_COMMITTER_EMAIL": "test@example.invalid", | ||
| "RELEASE_DEFAULT": "0", | ||
| "RELEASE_SIGN": "1", | ||
| "RELEASE_UPLOAD": "1", | ||
| "RELEASE_WATCH": "1", | ||
| "RELEASE_RUN_ID": "123", | ||
| "ASSETS": str(self.assets), | ||
| "CALL_LOG": str(self.calls), | ||
| "REAL_GIT": self.real_git, | ||
| "REAL_GZIP": shutil.which("gzip"), | ||
| "TMPDIR": str(self.scratch), | ||
| }) | ||
| self.git("init", "--quiet") | ||
| self.git("remote", "add", "origin", "https://github.com/example/iceberg-cpp.git") | ||
| (self.repo / "source.txt").write_text("original source\n") | ||
| (self.repo / ".gitattributes").write_text("source.txt text\n") | ||
| self.git("add", "source.txt", ".gitattributes") | ||
| self.git("commit", "--quiet", "-m", "Initial source") | ||
| self.git("tag", "-a", "v1.2.3-rc0", "-m", "Release candidate") | ||
| self.archive = self.git("archive", "HEAD", "--prefix", self.name + "/") | ||
| self.publish(gzip.compress(self.archive, mtime=123)) | ||
|
|
||
| script = Path(__file__).with_name("release_rc.sh") | ||
| self.script = self.repo / "dev" / "release" / script.name | ||
| self.script.parent.mkdir(parents=True) | ||
| shutil.copyfile(script, self.script) | ||
| commands = self.directory / "bin" | ||
| commands.mkdir() | ||
| for tool in ("gh", "gpg", "svn", "git", "gzip"): | ||
| executable = commands / tool | ||
| executable.write_text("#!" + sys.executable + "\n" + MOCK_TOOL) | ||
| executable.chmod(0o755) | ||
| self.env["PATH"] = str(commands) + os.pathsep + self.env["PATH"] | ||
|
|
||
| def git(self, *args): | ||
| return subprocess.check_output( | ||
| [self.real_git, *args], cwd=self.repo, env=self.env, | ||
| stderr=subprocess.STDOUT, | ||
| ) | ||
|
|
||
| def publish(self, archive): | ||
| (self.assets / self.tar_gz).write_bytes(archive) | ||
| checksum = hashlib.sha512(archive).hexdigest() | ||
| (self.assets / (self.tar_gz + ".sha512")).write_text( | ||
| checksum + " " + self.tar_gz + "\n" | ||
| ) | ||
|
|
||
| def release(self): | ||
| return subprocess.run( | ||
| ["bash", str(self.script), "1.2.3", "0"], | ||
| cwd=self.repo, env=self.env, capture_output=True, text=True, | ||
| timeout=30, | ||
| ) | ||
|
|
||
| def assert_rejected(self, result, message): | ||
| self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) | ||
| self.assertIn(message, result.stdout + result.stderr) | ||
| self.assertFalse(self.calls.exists(), "Signing or upload was attempted") | ||
| self.assertEqual(list(self.scratch.iterdir()), []) | ||
|
|
||
| def assert_release_succeeds(self): | ||
| result = self.release() | ||
| self.assertEqual(result.returncode, 0, result.stdout + result.stderr) | ||
| self.assertEqual( | ||
| (self.output / self.tar_gz).read_bytes(), | ||
| (self.assets / self.tar_gz).read_bytes(), | ||
| ) | ||
| self.assertEqual( | ||
| {path.name for path in self.output.iterdir()}, | ||
| {self.tar_gz, self.tar_gz + ".sha512", self.tar_gz + ".asc"}, | ||
| ) | ||
| calls = self.calls.read_text().splitlines() | ||
| self.assertEqual([line.split()[0] for line in calls], ["gpg", "gh", "svn"]) | ||
| self.assertEqual(list(self.scratch.iterdir()), []) | ||
|
|
||
| def test_valid_archive_with_different_gzip_metadata(self): | ||
| self.assert_release_succeeds() | ||
|
|
||
| def test_local_autocrlf_does_not_change_archive(self): | ||
| self.git("config", "core.autocrlf", "true") | ||
| self.assert_release_succeeds() | ||
|
|
||
| def test_local_eol_does_not_change_archive(self): | ||
| self.git("config", "core.eol", "crlf") | ||
| self.assert_release_succeeds() | ||
|
|
||
| def test_local_tar_umask_does_not_change_archive(self): | ||
| self.git("config", "tar.umask", "022") | ||
| self.assert_release_succeeds() | ||
|
|
||
| def test_replaced_archive_and_checksum(self): | ||
| (self.repo / "source.txt").write_text("modified source\n") | ||
| self.git("add", "source.txt") | ||
| self.git("commit", "--quiet", "-m", "Unreleased changes") | ||
| modified = self.git("archive", "HEAD", "--prefix", self.name + "/") | ||
| self.publish(gzip.compress(modified)) | ||
| self.assert_rejected(self.release(), "does not match RC commit") | ||
|
|
||
| def test_incorrect_checksum(self): | ||
| (self.assets / (self.tar_gz + ".sha512")).write_text( | ||
| "0" * 128 + " " + self.tar_gz + "\n" | ||
| ) | ||
| self.assert_rejected(self.release(), "failed SHA-512 verification") | ||
|
|
||
| def test_corrupt_gzip_trailer_with_matching_checksum(self): | ||
| archive = bytearray((self.assets / self.tar_gz).read_bytes()) | ||
| archive[-8] ^= 1 | ||
| self.publish(archive) | ||
| self.assert_rejected(self.release(), "Failed to decompress") | ||
|
|
||
| def test_existing_release_directory_is_preserved(self): | ||
| self.output.mkdir() | ||
| stale = self.output / self.tar_gz | ||
| stale.write_bytes(b"stale archive") | ||
| self.assert_rejected(self.release(), "already exists") | ||
| self.assertEqual(stale.read_bytes(), b"stale archive") | ||
|
|
||
| def test_failed_git_archive_after_writing_complete_output(self): | ||
| self.env["FAIL_ARCHIVE"] = "1" | ||
| self.assert_rejected(self.release(), "Failed to archive RC commit") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this test? Should we hook it into CI if yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept the tests to guard against signing invalid archives and added CI coverage on Ubuntu and macOS.