|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2025 The Sigstore Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""python-conformance — model-signing conformance adapter. |
| 17 | +
|
| 18 | +Translates the OMS conformance protocol to the model_signing Python CLI. |
| 19 | +
|
| 20 | +Usage: |
| 21 | + python-conformance sign-model --method key|certificate|sigstore \ |
| 22 | + --model-path DIR --output-bundle FILE [FLAGS...] |
| 23 | +
|
| 24 | + python-conformance verify-model --method key|certificate|sigstore \ |
| 25 | + --model-path DIR --bundle FILE [FLAGS...] |
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import argparse |
| 31 | +import os |
| 32 | +import subprocess |
| 33 | +import sys |
| 34 | + |
| 35 | + |
| 36 | +def _python() -> str: |
| 37 | + return os.environ.get("PYTHON_BIN", sys.executable) |
| 38 | + |
| 39 | + |
| 40 | +def cmd_sign(args: argparse.Namespace) -> int: |
| 41 | + method = args.method |
| 42 | + cmd = [_python(), "-m", "model_signing", "sign", method, |
| 43 | + "--signature", args.output_bundle] |
| 44 | + |
| 45 | + if method == "key": |
| 46 | + if not args.private_key: |
| 47 | + print("ERROR: --private-key required for method=key", file=sys.stderr) |
| 48 | + return 2 |
| 49 | + cmd += ["--private_key", args.private_key] |
| 50 | + |
| 51 | + elif method == "certificate": |
| 52 | + if not args.private_key: |
| 53 | + print("ERROR: --private-key required for method=certificate", file=sys.stderr) |
| 54 | + return 2 |
| 55 | + if not args.signing_cert: |
| 56 | + print("ERROR: --signing-cert required for method=certificate", file=sys.stderr) |
| 57 | + return 2 |
| 58 | + cmd += ["--private_key", args.private_key] |
| 59 | + cmd += ["--signing_certificate", args.signing_cert] |
| 60 | + for chain_cert in (args.cert_chain or []): |
| 61 | + cmd += ["--certificate_chain", chain_cert] |
| 62 | + |
| 63 | + elif method == "sigstore": |
| 64 | + token = args.identity_token or os.environ.get("SIGSTORE_ID_TOKEN", "") |
| 65 | + if token: |
| 66 | + cmd += ["--identity_token", token] |
| 67 | + if args.use_staging: |
| 68 | + cmd += ["--use_staging"] |
| 69 | + |
| 70 | + else: |
| 71 | + print(f"ERROR: Unknown method: {method}", file=sys.stderr) |
| 72 | + return 2 |
| 73 | + |
| 74 | + for ignore_path in (args.ignore_paths or []): |
| 75 | + cmd += ["--ignore-paths", ignore_path] |
| 76 | + |
| 77 | + cmd += [args.model_path] |
| 78 | + |
| 79 | + result = subprocess.run(cmd, timeout=300) |
| 80 | + return result.returncode |
| 81 | + |
| 82 | + |
| 83 | +def cmd_verify(args: argparse.Namespace) -> int: |
| 84 | + method = args.method |
| 85 | + cmd = [_python(), "-m", "model_signing", "verify", method, |
| 86 | + "--signature", args.bundle] |
| 87 | + |
| 88 | + if method == "key": |
| 89 | + if not args.public_key: |
| 90 | + print("ERROR: --public-key required for method=key", file=sys.stderr) |
| 91 | + return 2 |
| 92 | + cmd += ["--public_key", args.public_key] |
| 93 | + |
| 94 | + elif method == "certificate": |
| 95 | + for chain_cert in (args.cert_chain or []): |
| 96 | + cmd += ["--certificate_chain", chain_cert] |
| 97 | + |
| 98 | + elif method == "sigstore": |
| 99 | + if args.identity: |
| 100 | + cmd += ["--identity", args.identity] |
| 101 | + if args.identity_provider: |
| 102 | + cmd += ["--identity_provider", args.identity_provider] |
| 103 | + if args.use_staging: |
| 104 | + cmd += ["--use_staging"] |
| 105 | + |
| 106 | + else: |
| 107 | + print(f"ERROR: Unknown method: {method}", file=sys.stderr) |
| 108 | + return 2 |
| 109 | + |
| 110 | + for ignore_path in (args.ignore_paths or []): |
| 111 | + cmd += ["--ignore-paths", ignore_path] |
| 112 | + |
| 113 | + if args.ignore_unsigned_files: |
| 114 | + cmd += ["--ignore_unsigned_files"] |
| 115 | + |
| 116 | + cmd += [args.model_path] |
| 117 | + |
| 118 | + result = subprocess.run(cmd, timeout=300) |
| 119 | + return result.returncode |
| 120 | + |
| 121 | + |
| 122 | +def main() -> int: |
| 123 | + parser = argparse.ArgumentParser( |
| 124 | + prog="python-conformance", |
| 125 | + description="model-signing conformance adapter (Python)", |
| 126 | + ) |
| 127 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 128 | + |
| 129 | + sign_parser = subparsers.add_parser("sign-model") |
| 130 | + sign_parser.add_argument("--method", required=True, |
| 131 | + choices=["key", "certificate", "sigstore"]) |
| 132 | + sign_parser.add_argument("--model-path", required=True) |
| 133 | + sign_parser.add_argument("--output-bundle", required=True) |
| 134 | + sign_parser.add_argument("--private-key") |
| 135 | + sign_parser.add_argument("--signing-cert") |
| 136 | + sign_parser.add_argument("--cert-chain", action="append", default=[]) |
| 137 | + sign_parser.add_argument("--ignore-paths", action="append", |
| 138 | + dest="ignore_paths", default=[]) |
| 139 | + sign_parser.add_argument("--identity-token") |
| 140 | + sign_parser.add_argument("--use-staging", action="store_true") |
| 141 | + |
| 142 | + verify_parser = subparsers.add_parser("verify-model") |
| 143 | + verify_parser.add_argument("--method", required=True, |
| 144 | + choices=["key", "certificate", "sigstore"]) |
| 145 | + verify_parser.add_argument("--model-path", required=True) |
| 146 | + verify_parser.add_argument("--bundle", required=True) |
| 147 | + verify_parser.add_argument("--public-key") |
| 148 | + verify_parser.add_argument("--cert-chain", action="append", default=[]) |
| 149 | + verify_parser.add_argument("--ignore-paths", action="append", |
| 150 | + dest="ignore_paths", default=[]) |
| 151 | + verify_parser.add_argument("--ignore-unsigned-files", action="store_true") |
| 152 | + verify_parser.add_argument("--identity") |
| 153 | + verify_parser.add_argument("--identity-provider") |
| 154 | + verify_parser.add_argument("--use-staging", action="store_true") |
| 155 | + |
| 156 | + args = parser.parse_args() |
| 157 | + |
| 158 | + if args.command == "sign-model": |
| 159 | + return cmd_sign(args) |
| 160 | + elif args.command == "verify-model": |
| 161 | + return cmd_verify(args) |
| 162 | + return 1 |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + sys.exit(main()) |
0 commit comments