|
| 1 | +# Copyright 2024 The Sigstore Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for the top level API.""" |
| 16 | + |
| 17 | +from base64 import b64decode |
| 18 | +from datetime import datetime |
| 19 | +from datetime import timedelta |
| 20 | +import json |
| 21 | +import os |
| 22 | +import subprocess |
| 23 | +from tempfile import TemporaryDirectory |
| 24 | +import time |
| 25 | + |
| 26 | +import pytest |
| 27 | + |
| 28 | +from model_signing import api as model_signing_api |
| 29 | + |
| 30 | + |
| 31 | +_MIN_VALIDITY = timedelta(minutes=1) |
| 32 | +_MAX_RETRY_TIME = timedelta(minutes=5) |
| 33 | +_RETRY_SLEEP_SECS = 30 |
| 34 | + |
| 35 | + |
| 36 | +class DangerousPublicOIDCBeacon: |
| 37 | + """Fetches and validates tokens from Sigstore's testing beacon repo.""" |
| 38 | + |
| 39 | + def __init__(self): |
| 40 | + self._token = "" |
| 41 | + |
| 42 | + def _fetch(self) -> None: |
| 43 | + # the git approach is apparently fresher than https://raw.githubusercontent.com |
| 44 | + # https://github.com/sigstore-conformance/extremely-dangerous-public-oidc-beacon/issues/17 |
| 45 | + git_url = "https://github.com/sigstore-conformance/extremely-dangerous-public-oidc-beacon.git" |
| 46 | + with TemporaryDirectory() as tmpdir: |
| 47 | + base_cmd = [ |
| 48 | + "git", |
| 49 | + "clone", |
| 50 | + "--quiet", |
| 51 | + "--single-branch", |
| 52 | + "--branch=current-token", |
| 53 | + "--depth=1", |
| 54 | + ] |
| 55 | + subprocess.run(base_cmd + [git_url, tmpdir], check=True) |
| 56 | + token_path = os.path.join(tmpdir, "oidc-token.txt") |
| 57 | + with open(token_path) as f: |
| 58 | + self._token = f.read().rstrip() |
| 59 | + |
| 60 | + def _expiration(self) -> datetime: |
| 61 | + payload = self._token.split(".")[1] |
| 62 | + payload += "=" * (4 - len(payload) % 4) |
| 63 | + payload_json = json.loads(b64decode(payload)) |
| 64 | + return datetime.fromtimestamp(payload_json["exp"]) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def sigstore_oidc_beacon_token(): |
| 69 | + beacon = DangerousPublicOIDCBeacon() |
| 70 | + start = datetime.now() |
| 71 | + while True: |
| 72 | + now = datetime.now() |
| 73 | + deadline = now + _MIN_VALIDITY |
| 74 | + beacon._fetch() |
| 75 | + exp = beacon._expiration() |
| 76 | + if deadline < exp: |
| 77 | + return beacon._token |
| 78 | + if now > start + _MAX_RETRY_TIME: |
| 79 | + break |
| 80 | + time.sleep(_RETRY_SLEEP_SECS) |
| 81 | + pytest.fail("unable to fetch token within time limit") |
| 82 | + |
| 83 | + |
| 84 | +class TestSigstoreSigning: |
| 85 | + @pytest.mark.integration |
| 86 | + def test_sign_and_verify( |
| 87 | + self, sigstore_oidc_beacon_token, sample_model_folder, tmp_path |
| 88 | + ): |
| 89 | + sc = model_signing_api.SigningConfig() |
| 90 | + sc.set_sigstore_signer( |
| 91 | + use_staging=True, identity_token=sigstore_oidc_beacon_token |
| 92 | + ) |
| 93 | + signature_path = tmp_path / "model.sig" |
| 94 | + sc.sign(sample_model_folder, signature_path) |
| 95 | + expected_identity = "https://github.com/sigstore-conformance/extremely-dangerous-public-oidc-beacon/.github/workflows/extremely-dangerous-oidc-beacon.yml@refs/heads/main" |
| 96 | + model_signing_api.verify( |
| 97 | + sample_model_folder, |
| 98 | + signature_path, |
| 99 | + identity=expected_identity, |
| 100 | + use_staging=True, |
| 101 | + ) |
0 commit comments