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
1818import pathlib
1919from typing import Optional
2020
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
9999
100100
101101def _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+
105120class 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)
199214def _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
240255def _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
283298def _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
321336def _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:
389403def _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(
433447def _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(
474488def _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(
509523def _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