|
| 1 | +import logging |
| 2 | +import subprocess |
| 3 | +import time |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +import requests |
| 8 | +from pypi_attestations import Attestation, GitHubPublisher |
| 9 | +from sigstore import oidc |
| 10 | + |
| 11 | +import action |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="session") |
| 17 | +def id_token() -> oidc.IdentityToken: |
| 18 | + def _id_token() -> oidc.IdentityToken | None: |
| 19 | + # GitHub loves to cache things it has no business caching. |
| 20 | + result = subprocess.run( |
| 21 | + [ |
| 22 | + "git", |
| 23 | + "ls-remote", |
| 24 | + "https://github.com/sigstore-conformance/extremely-dangerous-public-oidc-beacon", |
| 25 | + "refs/heads/current-token", |
| 26 | + ], |
| 27 | + capture_output=True, |
| 28 | + text=True, |
| 29 | + check=True, |
| 30 | + ) |
| 31 | + ref = result.stdout.split()[0] |
| 32 | + |
| 33 | + resp = requests.get( |
| 34 | + f"https://raw.githubusercontent.com/sigstore-conformance/extremely-dangerous-public-oidc-beacon/{ref}/oidc-token.txt", |
| 35 | + ) |
| 36 | + resp.raise_for_status() |
| 37 | + id_token = resp.text.strip() |
| 38 | + try: |
| 39 | + return oidc.IdentityToken(id_token) |
| 40 | + except Exception: |
| 41 | + return None |
| 42 | + |
| 43 | + # Try up to 10 times to get a valid token, waiting 3 seconds between attempts. |
| 44 | + for n in range(10): |
| 45 | + token = _id_token() |
| 46 | + if token is not None: |
| 47 | + return token |
| 48 | + else: |
| 49 | + logger.warning(f"Waiting for valid OIDC identity token, try {n}...") |
| 50 | + time.sleep(3) |
| 51 | + |
| 52 | + raise RuntimeError("Failed to obtain OIDC identity token for tests") |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def sampleproject(tmp_path: Path) -> Path: |
| 57 | + """ |
| 58 | + Create a sample Python project with a distribution file. |
| 59 | + """ |
| 60 | + |
| 61 | + project_dir = tmp_path / "sampleproject" |
| 62 | + project_dir.mkdir() |
| 63 | + |
| 64 | + pyproject = project_dir / "pyproject.toml" |
| 65 | + pyproject.write_text(""" |
| 66 | +name = "astral-sh-attest-action-test-sampleproject" |
| 67 | +version = "0.1.0" |
| 68 | +description = "Who's wants to know?" |
| 69 | +requires-python = ">=3.10" |
| 70 | +""") |
| 71 | + |
| 72 | + hello_py = project_dir / "hello.py" |
| 73 | + hello_py.write_text(""" |
| 74 | +def main(): |
| 75 | + print("Hello, world!") |
| 76 | +""") |
| 77 | + |
| 78 | + return project_dir |
| 79 | + |
| 80 | + |
| 81 | +def test_get_input(monkeypatch: pytest.MonkeyPatch) -> None: |
| 82 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_FOO", "expected") |
| 83 | + |
| 84 | + assert action._get_input("foo") == "expected" |
| 85 | + |
| 86 | + |
| 87 | +def test_get_path_patterns(monkeypatch: pytest.MonkeyPatch) -> None: |
| 88 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_PATHS", "dist/* another/** third/") |
| 89 | + |
| 90 | + patterns = action._get_path_patterns() |
| 91 | + assert patterns == {"dist/*", "another/**", "third/*"} |
| 92 | + |
| 93 | + # Deduplicates patterns / files. |
| 94 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_PATHS", "dist/* dist/* another/") |
| 95 | + patterns = action._get_path_patterns() |
| 96 | + assert patterns == {"dist/*", "another/*"} |
| 97 | + |
| 98 | + monkeypatch.setenv("ATTEST_ACTION_INPUT_PATHS", "a a b b c") |
| 99 | + patterns = action._get_path_patterns() |
| 100 | + assert patterns == {"a", "b", "c"} |
| 101 | + |
| 102 | + |
| 103 | +def test_attest(sampleproject: Path, id_token: oidc.IdentityToken) -> None: |
| 104 | + subprocess.run(["uv", "build"], cwd=sampleproject, check=True) |
| 105 | + dist_dir = sampleproject / "dist" |
| 106 | + |
| 107 | + patterns = {str(dist_dir / "*")} |
| 108 | + |
| 109 | + dists = action._collect_dists(patterns) |
| 110 | + assert len(dists) == 2 # sdist and wheel |
| 111 | + |
| 112 | + action._attest( |
| 113 | + dists, |
| 114 | + id_token, |
| 115 | + overwrite=False, |
| 116 | + ) |
| 117 | + |
| 118 | + for dist_path, _ in dists: |
| 119 | + attestation_path = dist_path.with_name(f"{dist_path.name}.publish.attestation") |
| 120 | + assert attestation_path.exists() |
| 121 | + |
| 122 | + |
| 123 | +def test_attest_overwrite_fails( |
| 124 | + sampleproject: Path, |
| 125 | + id_token: oidc.IdentityToken, |
| 126 | +) -> None: |
| 127 | + subprocess.run(["uv", "build"], cwd=sampleproject, check=True) |
| 128 | + dist_dir = sampleproject / "dist" |
| 129 | + |
| 130 | + patterns = {str(dist_dir / "*")} |
| 131 | + |
| 132 | + dists = action._collect_dists(patterns) |
| 133 | + assert len(dists) == 2 # sdist and wheel |
| 134 | + |
| 135 | + action._attest( |
| 136 | + dists, |
| 137 | + id_token, |
| 138 | + overwrite=False, |
| 139 | + ) |
| 140 | + |
| 141 | + with pytest.raises(RuntimeError, match="Attestation file already exists"): |
| 142 | + action._attest( |
| 143 | + dists, |
| 144 | + id_token, |
| 145 | + overwrite=False, |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def test_attest_overwrite_succeeds( |
| 150 | + sampleproject: Path, |
| 151 | + id_token: oidc.IdentityToken, |
| 152 | +) -> None: |
| 153 | + subprocess.run(["uv", "build"], cwd=sampleproject, check=True) |
| 154 | + dist_dir = sampleproject / "dist" |
| 155 | + |
| 156 | + patterns = {str(dist_dir / "*")} |
| 157 | + |
| 158 | + dists = action._collect_dists(patterns) |
| 159 | + assert len(dists) == 2 # sdist and wheel |
| 160 | + |
| 161 | + action._attest( |
| 162 | + dists, |
| 163 | + id_token, |
| 164 | + overwrite=False, |
| 165 | + ) |
| 166 | + |
| 167 | + # This should succeed without error. |
| 168 | + action._attest( |
| 169 | + dists, |
| 170 | + id_token, |
| 171 | + overwrite=True, |
| 172 | + ) |
| 173 | + |
| 174 | + |
| 175 | +def test_attest_verify( |
| 176 | + sampleproject: Path, |
| 177 | + id_token: oidc.IdentityToken, |
| 178 | +) -> None: |
| 179 | + subprocess.run(["uv", "build"], cwd=sampleproject, check=True) |
| 180 | + dist_dir = sampleproject / "dist" |
| 181 | + |
| 182 | + patterns = {str(dist_dir / "*")} |
| 183 | + |
| 184 | + dists = action._collect_dists(patterns) |
| 185 | + assert len(dists) == 2 # sdist and wheel |
| 186 | + |
| 187 | + action._attest( |
| 188 | + dists, |
| 189 | + id_token, |
| 190 | + overwrite=False, |
| 191 | + ) |
| 192 | + |
| 193 | + for dist_path, dist in dists: |
| 194 | + attestation_path = dist_path.with_name(f"{dist_path.name}.publish.attestation") |
| 195 | + assert attestation_path.exists() |
| 196 | + |
| 197 | + attestation = Attestation.model_validate_json(attestation_path.read_bytes()) |
| 198 | + identity = GitHubPublisher( |
| 199 | + repository="sigstore-conformance/extremely-dangerous-public-oidc-beacon", |
| 200 | + workflow="extremely-dangerous-oidc-beacon.yml", |
| 201 | + ) |
| 202 | + |
| 203 | + attestation.verify( |
| 204 | + identity=identity, |
| 205 | + dist=dist, |
| 206 | + offline=True, |
| 207 | + ) |
0 commit comments