Skip to content

Commit b279745

Browse files
committed
examples: support dsse in example client
Add `--use-dsse` flag to `download` subcommand of example client, which can be used to indicate that all metadata is expected to come in a DSSE envelope. Signed-off-by: Lukas Puehringer <[email protected]>
1 parent 5fab635 commit b279745

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

examples/client/client

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ from pathlib import Path
1414
from urllib import request
1515

1616
from tuf.api.exceptions import DownloadError, RepositoryError
17-
from tuf.ngclient import Updater
17+
from tuf.ngclient import Updater, UpdaterConfig
1818

1919
# constants
2020
DOWNLOAD_DIR = "./downloads"
2121
CLIENT_EXAMPLE_DIR = os.path.dirname(os.path.abspath(__file__))
2222

23+
2324
def build_metadata_dir(base_url: str) -> str:
2425
"""build a unique and reproducible directory name for the repository url"""
2526
name = sha256(base_url.encode()).hexdigest()[:8]
@@ -46,7 +47,7 @@ def init_tofu(base_url: str) -> bool:
4647
return True
4748

4849

49-
def download(base_url: str, target: str) -> bool:
50+
def download(base_url: str, target: str, use_dsse: bool) -> bool:
5051
"""
5152
Download the target file using ``ngclient`` Updater.
5253
@@ -72,12 +73,16 @@ def download(base_url: str, target: str) -> bool:
7273
if not os.path.isdir(DOWNLOAD_DIR):
7374
os.mkdir(DOWNLOAD_DIR)
7475

76+
config = UpdaterConfig()
77+
config.use_dsse = use_dsse
78+
7579
try:
7680
updater = Updater(
7781
metadata_dir=metadata_dir,
7882
metadata_base_url=f"{base_url}/metadata/",
7983
target_base_url=f"{base_url}/targets/",
8084
target_dir=DOWNLOAD_DIR,
85+
config=config,
8186
)
8287
updater.refresh()
8388

@@ -146,6 +151,13 @@ def main() -> None:
146151
help="Target file",
147152
)
148153

154+
download_parser.add_argument(
155+
"--use-dsse",
156+
help="Parse TUF metadata as DSSE",
157+
default=False,
158+
action="store_true",
159+
)
160+
149161
command_args = client_args.parse_args()
150162

151163
if command_args.verbose == 0:
@@ -164,7 +176,9 @@ def main() -> None:
164176
if not init_tofu(command_args.url):
165177
return "Failed to initialize local repository"
166178
elif command_args.sub_command == "download":
167-
if not download(command_args.url, command_args.target):
179+
if not download(
180+
command_args.url, command_args.target, command_args.use_dsse
181+
):
168182
return f"Failed to download {command_args.target}"
169183
else:
170184
client_args.print_help()

0 commit comments

Comments
 (0)