|
| 1 | +#!/usr/bin/env python |
| 2 | +"""TUF Client Example""" |
| 3 | + |
| 4 | +# Copyright 2012 - 2017, New York University and the TUF contributors |
| 5 | +# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 6 | + |
| 7 | +import argparse |
| 8 | +import logging |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +from tuf.exceptions import RepositoryError |
| 14 | +from tuf.ngclient import Updater |
| 15 | + |
| 16 | +# constants |
| 17 | +BASE_URL = "http://127.0.0.1:8000" |
| 18 | +DOWNLOAD_DIR = "./downloads" |
| 19 | +METADATA_DIR = f"{Path.home()}/.local/share/python-tuf-client-example" |
| 20 | +CLIENT_EXAMPLE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 21 | + |
| 22 | + |
| 23 | +def init() -> None: |
| 24 | + """Initialize local trusted metadata and create a directory for downloads""" |
| 25 | + |
| 26 | + if not os.path.isdir(DOWNLOAD_DIR): |
| 27 | + os.mkdir(DOWNLOAD_DIR) |
| 28 | + |
| 29 | + if not os.path.isdir(METADATA_DIR): |
| 30 | + os.makedirs(METADATA_DIR) |
| 31 | + |
| 32 | + if not os.path.isfile(f"{METADATA_DIR}/root.json"): |
| 33 | + shutil.copy( |
| 34 | + f"{CLIENT_EXAMPLE_DIR}/1.root.json", f"{METADATA_DIR}/root.json" |
| 35 | + ) |
| 36 | + print(f"Added trusted root in {METADATA_DIR}") |
| 37 | + |
| 38 | + else: |
| 39 | + print(f"Found trusted root in {METADATA_DIR}") |
| 40 | + |
| 41 | + |
| 42 | +def download(target: str) -> bool: |
| 43 | + """ |
| 44 | + Download the target file using ``ngclient`` Updater. |
| 45 | +
|
| 46 | + The Updater refreshes the top-level metadata, get the target information, |
| 47 | + verifies if the target is already cached, and in case it is not cached, |
| 48 | + downloads the target file. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + A boolean indicating if process was successful |
| 52 | + """ |
| 53 | + try: |
| 54 | + updater = Updater( |
| 55 | + repository_dir=METADATA_DIR, |
| 56 | + metadata_base_url=f"{BASE_URL}/metadata/", |
| 57 | + target_base_url=f"{BASE_URL}/targets/", |
| 58 | + target_dir=DOWNLOAD_DIR, |
| 59 | + ) |
| 60 | + updater.refresh() |
| 61 | + |
| 62 | + info = updater.get_targetinfo(target) |
| 63 | + |
| 64 | + if info is None: |
| 65 | + print(f"Target {target} not found") |
| 66 | + return True |
| 67 | + |
| 68 | + path = updater.find_cached_target(info) |
| 69 | + if path: |
| 70 | + print(f"Target is available in {path}") |
| 71 | + return True |
| 72 | + |
| 73 | + path = updater.download_target(info) |
| 74 | + print(f"Target downloaded and available in {path}") |
| 75 | + |
| 76 | + except (OSError, RepositoryError) as e: |
| 77 | + print(str(e)) |
| 78 | + return False |
| 79 | + |
| 80 | + return True |
| 81 | + |
| 82 | + |
| 83 | +def main() -> None: |
| 84 | + """Main TUF Client Example function""" |
| 85 | + |
| 86 | + client_args = argparse.ArgumentParser(description="TUF Client Example") |
| 87 | + |
| 88 | + # Global arguments |
| 89 | + client_args.add_argument( |
| 90 | + "-v", |
| 91 | + "--verbose", |
| 92 | + help="Output verbosity level (-v, -vv, ...)", |
| 93 | + action="count", |
| 94 | + default=0, |
| 95 | + ) |
| 96 | + |
| 97 | + # Sub commands |
| 98 | + sub_command = client_args.add_subparsers(dest="sub_command") |
| 99 | + |
| 100 | + # Download |
| 101 | + download_parser = sub_command.add_parser( |
| 102 | + "download", |
| 103 | + help="Download a target file", |
| 104 | + ) |
| 105 | + |
| 106 | + download_parser.add_argument( |
| 107 | + "target", |
| 108 | + metavar="TARGET", |
| 109 | + help="Target file", |
| 110 | + ) |
| 111 | + |
| 112 | + command_args = client_args.parse_args() |
| 113 | + |
| 114 | + if command_args.verbose == 0: |
| 115 | + loglevel = logging.ERROR |
| 116 | + elif command_args.verbose == 1: |
| 117 | + loglevel = logging.WARNING |
| 118 | + elif command_args.verbose == 2: |
| 119 | + loglevel = logging.INFO |
| 120 | + else: |
| 121 | + loglevel = logging.DEBUG |
| 122 | + |
| 123 | + logging.basicConfig(level=loglevel) |
| 124 | + |
| 125 | + # initialize the TUF Client Example infrastructure |
| 126 | + init() |
| 127 | + |
| 128 | + if command_args.sub_command == "download": |
| 129 | + download(command_args.target) |
| 130 | + |
| 131 | + else: |
| 132 | + client_args.print_help() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments