Skip to content

Commit

Permalink
examples: support dsse in example client
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lukpueh committed Feb 21, 2024
1 parent 5fab635 commit b279745
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions examples/client/client
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ from pathlib import Path
from urllib import request

from tuf.api.exceptions import DownloadError, RepositoryError
from tuf.ngclient import Updater
from tuf.ngclient import Updater, UpdaterConfig

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


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


def download(base_url: str, target: str) -> bool:
def download(base_url: str, target: str, use_dsse: bool) -> bool:
"""
Download the target file using ``ngclient`` Updater.
Expand All @@ -72,12 +73,16 @@ def download(base_url: str, target: str) -> bool:
if not os.path.isdir(DOWNLOAD_DIR):
os.mkdir(DOWNLOAD_DIR)

config = UpdaterConfig()
config.use_dsse = use_dsse

try:
updater = Updater(
metadata_dir=metadata_dir,
metadata_base_url=f"{base_url}/metadata/",
target_base_url=f"{base_url}/targets/",
target_dir=DOWNLOAD_DIR,
config=config,
)
updater.refresh()

Expand Down Expand Up @@ -146,6 +151,13 @@ def main() -> None:
help="Target file",
)

download_parser.add_argument(
"--use-dsse",
help="Parse TUF metadata as DSSE",
default=False,
action="store_true",
)

command_args = client_args.parse_args()

if command_args.verbose == 0:
Expand All @@ -164,7 +176,9 @@ def main() -> None:
if not init_tofu(command_args.url):
return "Failed to initialize local repository"
elif command_args.sub_command == "download":
if not download(command_args.url, command_args.target):
if not download(
command_args.url, command_args.target, command_args.use_dsse
):
return f"Failed to download {command_args.target}"
else:
client_args.print_help()
Expand Down

0 comments on commit b279745

Please sign in to comment.