|  | 
| 16 | 16 | 
 | 
| 17 | 17 | from collections.abc import Iterable, Sequence | 
| 18 | 18 | import logging | 
|  | 19 | +import os | 
| 19 | 20 | import pathlib | 
| 20 | 21 | import sys | 
| 21 | 22 | from typing import Optional | 
|  | 
| 119 | 120 | ) | 
| 120 | 121 | 
 | 
| 121 | 122 | 
 | 
|  | 123 | +def adjust_paths( | 
|  | 124 | +    model_path: pathlib.Path, | 
|  | 125 | +    signature: pathlib.Path, | 
|  | 126 | +    ignore_paths: Iterable[pathlib.Path], | 
|  | 127 | +) -> tuple[pathlib.Path, pathlib.Path, Iterable[pathlib.Path]]: | 
|  | 128 | +    """Adjust paths to use 'cwd' in case dirname is empty.""" | 
|  | 129 | +    if os.path.basename(model_path) == ".": | 
|  | 130 | +        model_path = pathlib.Path(os.getcwd()) | 
|  | 131 | +    if os.path.dirname(signature) == "": | 
|  | 132 | +        signature = pathlib.Path( | 
|  | 133 | +            os.path.join(os.getcwd(), os.path.basename(signature)) | 
|  | 134 | +        ) | 
|  | 135 | +    ignore_paths = [ | 
|  | 136 | +        p | 
|  | 137 | +        if os.path.dirname(p) != "" | 
|  | 138 | +        else pathlib.Path(os.path.join(os.getcwd(), os.path.basename(p))) | 
|  | 139 | +        for p in ignore_paths | 
|  | 140 | +    ] | 
|  | 141 | +    return model_path, signature, ignore_paths | 
|  | 142 | + | 
|  | 143 | + | 
| 122 | 144 | class _PKICmdGroup(click.Group): | 
| 123 | 145 |     """A custom group to configure the supported PKI methods.""" | 
| 124 | 146 | 
 | 
| @@ -239,6 +261,9 @@ def _sign_sigstore( | 
| 239 | 261 |     Passing the `--use_staging` flag would use that instance instead of the | 
| 240 | 262 |     production one. | 
| 241 | 263 |     """ | 
|  | 264 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 265 | +        model_path, signature, ignore_paths | 
|  | 266 | +    ) | 
| 242 | 267 |     try: | 
| 243 | 268 |         model_signing.signing.Config().use_sigstore_signer( | 
| 244 | 269 |             use_ambient_credentials=use_ambient_credentials, | 
| @@ -290,6 +315,9 @@ def _sign_private_key( | 
| 290 | 315 |     signer, outside of pairing the keys. Also note that we don't offer key | 
| 291 | 316 |     management protocols. | 
| 292 | 317 |     """ | 
|  | 318 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 319 | +        model_path, signature, ignore_paths | 
|  | 320 | +    ) | 
| 293 | 321 |     try: | 
| 294 | 322 |         model_signing.signing.Config().use_elliptic_key_signer( | 
| 295 | 323 |             private_key=private_key, password=password | 
| @@ -332,6 +360,9 @@ def _sign_pkcs11_key( | 
| 332 | 360 |     signer, outside of pairing the keys. Also note that we don't offer key | 
| 333 | 361 |     management protocols. | 
| 334 | 362 |     """ | 
|  | 363 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 364 | +        model_path, signature, ignore_paths | 
|  | 365 | +    ) | 
| 335 | 366 |     try: | 
| 336 | 367 |         model_signing.signing.Config().use_pkcs11_signer( | 
| 337 | 368 |             pkcs11_uri=pkcs11_uri | 
| @@ -380,6 +411,9 @@ def _sign_certificate( | 
| 380 | 411 | 
 | 
| 381 | 412 |     Note that we don't offer certificate and key management protocols. | 
| 382 | 413 |     """ | 
|  | 414 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 415 | +        model_path, signature, ignore_paths | 
|  | 416 | +    ) | 
| 383 | 417 |     try: | 
| 384 | 418 |         model_signing.signing.Config().use_certificate_signer( | 
| 385 | 419 |             private_key=private_key, | 
| @@ -432,6 +466,9 @@ def _sign_pkcs11_certificate( | 
| 432 | 466 | 
 | 
| 433 | 467 |     Note that we don't offer certificate and key management protocols. | 
| 434 | 468 |     """ | 
|  | 469 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 470 | +        model_path, signature, ignore_paths | 
|  | 471 | +    ) | 
| 435 | 472 |     try: | 
| 436 | 473 |         model_signing.signing.Config().use_pkcs11_certificate_signer( | 
| 437 | 474 |             pkcs11_uri=pkcs11_uri, | 
| @@ -505,6 +542,9 @@ def _verify_sigstore( | 
| 505 | 542 |     provider for the signature. If these don't match what is provided in the | 
| 506 | 543 |     signature, verification would fail. | 
| 507 | 544 |     """ | 
|  | 545 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 546 | +        model_path, signature, ignore_paths | 
|  | 547 | +    ) | 
| 508 | 548 |     try: | 
| 509 | 549 |         model_signing.verifying.Config().use_sigstore_verifier( | 
| 510 | 550 |             identity=identity, | 
| @@ -555,6 +595,9 @@ def _verify_private_key( | 
| 555 | 595 |     signer, outside of pairing the keys. Also note that we don't offer key | 
| 556 | 596 |     management protocols. | 
| 557 | 597 |     """ | 
|  | 598 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 599 | +        model_path, signature, ignore_paths | 
|  | 600 | +    ) | 
| 558 | 601 |     try: | 
| 559 | 602 |         model_signing.verifying.Config().use_elliptic_key_verifier( | 
| 560 | 603 |             public_key=public_key | 
| @@ -606,6 +649,9 @@ def _verify_certificate( | 
| 606 | 649 | 
 | 
| 607 | 650 |     Note that we don't offer certificate and key management protocols. | 
| 608 | 651 |     """ | 
|  | 652 | +    model_path, signature, ignore_paths = adjust_paths( | 
|  | 653 | +        model_path, signature, ignore_paths | 
|  | 654 | +    ) | 
| 609 | 655 |     try: | 
| 610 | 656 |         model_signing.verifying.Config().use_certificate_verifier( | 
| 611 | 657 |             certificate_chain=certificate_chain, | 
|  | 
0 commit comments