Skip to content

Commit e2f1954

Browse files
Remove duplicated conversions, use generic types. (#376)
* Remove duplicated conversions, use generic types. Also reorder some arguments for consistency. No functional changes, just correctness w.r.t. typechecker. Signed-off-by: Mihai Maruseac <[email protected]> * Switch to `Iterable` Signed-off-by: Mihai Maruseac <[email protected]> --------- Signed-off-by: Mihai Maruseac <[email protected]>
1 parent 5e48121 commit e2f1954

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

src/model_signing/_cli.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""The main entry-point for the model_signing package."""
1616

17-
from collections.abc import Sequence
17+
from collections.abc import Iterable, Sequence
1818
import pathlib
1919
from typing import Optional
2020

@@ -61,18 +61,18 @@
6161
# Decorator for the commonly used option to ignore certain paths
6262
_ignore_paths_option = click.option(
6363
"--ignore-paths",
64+
type=pathlib.Path,
6465
metavar="IGNORE_PATHS",
6566
multiple=True,
66-
type=pathlib.Path,
6767
help="File paths to ignore when signing or verifying.",
6868
)
6969

7070
# Decorator for the commonly used option to ignore git-related paths
7171
_ignore_git_paths_option = click.option(
7272
"--ignore-git-paths/--no-ignore-git-paths",
7373
type=bool,
74-
is_flag=True,
7574
default=True,
75+
show_default=True,
7676
help="Ignore git-related files when signing or verifying.",
7777
)
7878

@@ -99,9 +99,24 @@
9999

100100

101101
def _collect_git_related_files(model_path: pathlib.Path) -> list[pathlib.Path]:
102+
"""Expand to all git related files in the model directory."""
102103
return [pathlib.Path(p) for p in list(model_path.glob("**/.git*"))]
103104

104105

106+
def _expand_paths_to_ignore(
107+
model_path: pathlib.Path,
108+
signature: pathlib.Path,
109+
ignore_paths: Iterable[pathlib.Path],
110+
ignore_git_paths: bool,
111+
) -> list[pathlib.Path]:
112+
"""Expand all ignore arguments to build the list of paths to exclude."""
113+
ignore_paths = [path for path in ignore_paths]
114+
ignore_paths.append(signature)
115+
if ignore_git_paths:
116+
ignore_paths.extend(_collect_git_related_files(model_path))
117+
return ignore_paths
118+
119+
105120
class PKICmdGroup(click.Group):
106121
"""A custom group to configure the supported PKI methods."""
107122

@@ -119,8 +134,8 @@ def get_command(
119134
return super().get_command(ctx, "sigstore")
120135

121136
def resolve_command(
122-
self, ctx: click.Context, args: list[str]
123-
) -> tuple[Optional[str], Optional[click.Command], list[str]]:
137+
self, ctx: click.Context, args: Sequence[str]
138+
) -> tuple[Optional[str], Optional[click.Command], Iterable[str]]:
124139
"""Resolves a command and its arguments.
125140
126141
We use this to make Sigstore signing be the default and correctly alter
@@ -198,7 +213,7 @@ def _sign() -> None:
198213
)
199214
def _sign_sigstore(
200215
model_path: pathlib.Path,
201-
ignore_paths: Sequence[pathlib.Path],
216+
ignore_paths: Iterable[pathlib.Path],
202217
ignore_git_paths: bool,
203218
signature: pathlib.Path,
204219
use_ambient_credentials: bool,
@@ -227,7 +242,7 @@ def _sign_sigstore(
227242
identity_token=identity_token,
228243
)
229244
_serialize_and_sign(
230-
model_path, list(ignore_paths), ignore_git_paths, signer, signature
245+
model_path, ignore_paths, ignore_git_paths, signer, signature
231246
)
232247

233248

@@ -239,7 +254,7 @@ def _sign_sigstore(
239254
@_private_key_option
240255
def _sign_private_key(
241256
model_path: pathlib.Path,
242-
ignore_paths: Sequence[pathlib.Path],
257+
ignore_paths: Iterable[pathlib.Path],
243258
ignore_git_paths: bool,
244259
signature: pathlib.Path,
245260
private_key: pathlib.Path,
@@ -262,7 +277,7 @@ def _sign_private_key(
262277
key.ECKeySigner.from_path(private_key_path=private_key.as_posix())
263278
)
264279
_serialize_and_sign(
265-
model_path, list(ignore_paths), ignore_git_paths, signer, signature
280+
model_path, ignore_paths, ignore_git_paths, signer, signature
266281
)
267282

268283

@@ -282,12 +297,12 @@ def _sign_private_key(
282297
@_certificate_root_of_trust_option
283298
def _sign_certificate(
284299
model_path: pathlib.Path,
285-
ignore_paths: Sequence[pathlib.Path],
300+
ignore_paths: Iterable[pathlib.Path],
286301
ignore_git_paths: bool,
287302
signature: pathlib.Path,
288303
private_key: pathlib.Path,
289304
signing_certificate: pathlib.Path,
290-
certificate_chain: list[pathlib.Path],
305+
certificate_chain: Iterable[pathlib.Path],
291306
) -> None:
292307
"""Sign using a certificate.
293308
@@ -314,13 +329,13 @@ def _sign_certificate(
314329
)
315330
)
316331
_serialize_and_sign(
317-
model_path, list(ignore_paths), ignore_git_paths, signer, signature
332+
model_path, ignore_paths, ignore_git_paths, signer, signature
318333
)
319334

320335

321336
def _serialize_and_sign(
322337
model_path: pathlib.Path,
323-
ignore_paths: list[pathlib.Path],
338+
ignore_paths: Iterable[pathlib.Path],
324339
ignore_git_paths: bool,
325340
signer: signing.Signer,
326341
signature: pathlib.Path,
@@ -336,15 +351,14 @@ def hasher_factory(file_path: pathlib.Path) -> file.FileHasher:
336351
file_hasher_factory=hasher_factory
337352
)
338353

339-
if ignore_git_paths:
340-
ignore_paths.extend(_collect_git_related_files(model_path))
341-
342354
signing_result = model.sign(
343355
model_path=model_path,
344356
signer=signer,
345357
payload_generator=in_toto.DigestsIntotoPayload.from_manifest,
346358
serializer=serializer,
347-
ignore_paths=ignore_paths + [signature],
359+
ignore_paths=_expand_paths_to_ignore(
360+
model_path, signature, ignore_paths, ignore_git_paths
361+
),
348362
)
349363

350364
signing_result.write(signature)
@@ -389,7 +403,7 @@ def _verify() -> None:
389403
def _verify_sigstore(
390404
model_path: pathlib.Path,
391405
signature: pathlib.Path,
392-
ignore_paths: Sequence[pathlib.Path],
406+
ignore_paths: Iterable[pathlib.Path],
393407
ignore_git_paths: bool,
394408
identity: str,
395409
identity_provider: str,
@@ -411,7 +425,7 @@ def _verify_sigstore(
411425
_serialize_and_verify(
412426
model_path,
413427
verifier,
414-
list(ignore_paths),
428+
ignore_paths,
415429
ignore_git_paths,
416430
signature_contents,
417431
signature,
@@ -433,7 +447,7 @@ def _verify_sigstore(
433447
def _verify_private_key(
434448
model_path: pathlib.Path,
435449
signature: pathlib.Path,
436-
ignore_paths: Sequence[pathlib.Path],
450+
ignore_paths: Iterable[pathlib.Path],
437451
ignore_git_paths: bool,
438452
public_key: pathlib.Path,
439453
) -> None:
@@ -458,7 +472,7 @@ def _verify_private_key(
458472
_serialize_and_verify(
459473
model_path,
460474
verifier,
461-
list(ignore_paths),
475+
ignore_paths,
462476
ignore_git_paths,
463477
signature_contents,
464478
signature,
@@ -474,9 +488,9 @@ def _verify_private_key(
474488
def _verify_certificate(
475489
model_path: pathlib.Path,
476490
signature: pathlib.Path,
477-
ignore_paths: Sequence[pathlib.Path],
491+
ignore_paths: Iterable[pathlib.Path],
478492
ignore_git_paths: bool,
479-
certificate_chain: list[pathlib.Path],
493+
certificate_chain: Iterable[pathlib.Path],
480494
) -> None:
481495
"""Verify using a certificate.
482496
@@ -499,7 +513,7 @@ def _verify_certificate(
499513
_serialize_and_verify(
500514
model_path,
501515
verifier,
502-
list(ignore_paths),
516+
ignore_paths,
503517
ignore_git_paths,
504518
signature_contents,
505519
signature,
@@ -509,7 +523,7 @@ def _verify_certificate(
509523
def _serialize_and_verify(
510524
model_path: pathlib.Path,
511525
verifier: signing.Verifier,
512-
ignore_paths: list[pathlib.Path],
526+
ignore_paths: Iterable[pathlib.Path],
513527
ignore_git_paths: bool,
514528
signature_content: signing.Signature,
515529
signature_file: pathlib.Path,
@@ -525,16 +539,15 @@ def hasher_factory(file_path: pathlib.Path) -> file.FileHasher:
525539
file_hasher_factory=hasher_factory
526540
)
527541

528-
if ignore_git_paths:
529-
ignore_paths.extend(_collect_git_related_files(model_path))
530-
531542
try:
532543
model.verify(
533544
sig=signature_content,
534545
verifier=verifier,
535546
model_path=model_path,
536547
serializer=serializer,
537-
ignore_paths=ignore_paths + [signature_file],
548+
ignore_paths=_expand_paths_to_ignore(
549+
model_path, signature_file, ignore_paths, ignore_git_paths
550+
),
538551
)
539552
except Exception as err:
540553
click.echo(f"Verification failed with error: {err}", err=True)

0 commit comments

Comments
 (0)