Skip to content

Commit b0637de

Browse files
committed
cli: Adjust paths when signing and verifying in model_path '.'
All paths have to be adjusted when signing or verifying when the model_path is '.'. This is the case when signing while the current working directory is the model's directory. Resolves: #451 Signed-off-by: Stefan Berger <[email protected]>
1 parent 1a420ba commit b0637de

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/model_signing/_cli.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from collections.abc import Iterable, Sequence
1818
import logging
19+
import os
1920
import pathlib
2021
import sys
2122
from typing import Optional
@@ -119,6 +120,27 @@
119120
)
120121

121122

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+
122144
class _PKICmdGroup(click.Group):
123145
"""A custom group to configure the supported PKI methods."""
124146

@@ -239,6 +261,9 @@ def _sign_sigstore(
239261
Passing the `--use_staging` flag would use that instance instead of the
240262
production one.
241263
"""
264+
model_path, signature, ignore_paths = adjust_paths(
265+
model_path, signature, ignore_paths
266+
)
242267
try:
243268
model_signing.signing.Config().use_sigstore_signer(
244269
use_ambient_credentials=use_ambient_credentials,
@@ -290,6 +315,9 @@ def _sign_private_key(
290315
signer, outside of pairing the keys. Also note that we don't offer key
291316
management protocols.
292317
"""
318+
model_path, signature, ignore_paths = adjust_paths(
319+
model_path, signature, ignore_paths
320+
)
293321
try:
294322
model_signing.signing.Config().use_elliptic_key_signer(
295323
private_key=private_key, password=password
@@ -332,6 +360,9 @@ def _sign_pkcs11_key(
332360
signer, outside of pairing the keys. Also note that we don't offer key
333361
management protocols.
334362
"""
363+
model_path, signature, ignore_paths = adjust_paths(
364+
model_path, signature, ignore_paths
365+
)
335366
try:
336367
model_signing.signing.Config().use_pkcs11_signer(
337368
pkcs11_uri=pkcs11_uri
@@ -380,6 +411,9 @@ def _sign_certificate(
380411
381412
Note that we don't offer certificate and key management protocols.
382413
"""
414+
model_path, signature, ignore_paths = adjust_paths(
415+
model_path, signature, ignore_paths
416+
)
383417
try:
384418
model_signing.signing.Config().use_certificate_signer(
385419
private_key=private_key,
@@ -432,6 +466,9 @@ def _sign_pkcs11_certificate(
432466
433467
Note that we don't offer certificate and key management protocols.
434468
"""
469+
model_path, signature, ignore_paths = adjust_paths(
470+
model_path, signature, ignore_paths
471+
)
435472
try:
436473
model_signing.signing.Config().use_pkcs11_certificate_signer(
437474
pkcs11_uri=pkcs11_uri,
@@ -505,6 +542,9 @@ def _verify_sigstore(
505542
provider for the signature. If these don't match what is provided in the
506543
signature, verification would fail.
507544
"""
545+
model_path, signature, ignore_paths = adjust_paths(
546+
model_path, signature, ignore_paths
547+
)
508548
try:
509549
model_signing.verifying.Config().use_sigstore_verifier(
510550
identity=identity,
@@ -555,6 +595,9 @@ def _verify_private_key(
555595
signer, outside of pairing the keys. Also note that we don't offer key
556596
management protocols.
557597
"""
598+
model_path, signature, ignore_paths = adjust_paths(
599+
model_path, signature, ignore_paths
600+
)
558601
try:
559602
model_signing.verifying.Config().use_elliptic_key_verifier(
560603
public_key=public_key
@@ -606,6 +649,9 @@ def _verify_certificate(
606649
607650
Note that we don't offer certificate and key management protocols.
608651
"""
652+
model_path, signature, ignore_paths = adjust_paths(
653+
model_path, signature, ignore_paths
654+
)
609655
try:
610656
model_signing.verifying.Config().use_certificate_verifier(
611657
certificate_chain=certificate_chain,

0 commit comments

Comments
 (0)