| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright 2021-2022 python-tuf contributors |
| 3 |
# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 4 |
|
| 5 |
"""Simple uploader tool example |
| 6 |
|
| 7 |
Uploader is a maintainer application that communicates with the repository |
| 8 |
example. Uploader controls offline signing keys and produces signed metadata |
| 9 |
that it sends to the repository application so that the metadata can be added |
| 10 |
to the repository. |
| 11 |
""" |
| 12 |
|
| 13 |
import argparse |
| 14 |
import logging |
| 15 |
import os |
| 16 |
import sys |
| 17 |
from hashlib import sha256 |
| 18 |
from pathlib import Path |
| 19 |
from typing import List, Optional |
| 20 |
from urllib import request |
| 21 |
|
| 22 |
from _localrepo import LocalRepository |
| 23 |
|
| 24 |
logger = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
def build_metadata_dir(base_url: str) -> str: |
| 28 |
"""build a unique and reproducible metadata dirname for the repo url""" |
| 29 |
name = sha256(base_url.encode()).hexdigest()[:8] |
| 30 |
# TODO: Make this not windows hostile? |
| 31 |
return f"{Path.home()}/.local/share/tuf-upload-example/{name}" |
| 32 |
|
| 33 |
|
| 34 |
def build_key_dir(base_url: str) -> str: |
| 35 |
"""build a unique and reproducible private key dir for the repository url""" |
| 36 |
name = sha256(base_url.encode()).hexdigest()[:8] |
| 37 |
# TODO: Make this not windows hostile? |
| 38 |
return f"{Path.home()}/.config/tuf-upload-example/{name}" |
| 39 |
|
| 40 |
|
| 41 |
def init_tofu(base_url: str) -> bool: |
| 42 |
"""Initialize local trusted metadata (Trust-On-First-Use)""" |
| 43 |
metadata_dir = build_metadata_dir(base_url) |
| 44 |
|
| 45 |
if not os.path.isdir(metadata_dir): |
| 46 |
os.makedirs(metadata_dir) |
| 47 |
|
| 48 |
root_url = f"{base_url}/metadata/1.root.json" |
| 49 |
try: |
| 50 |
request.urlretrieve(root_url, f"{metadata_dir}/root.json") |
| 51 |
except OSError: |
| 52 |
print(f"Failed to download initial root from {root_url}") |
| 53 |
return False |
| 54 |
|
| 55 |
print(f"Trust-on-First-Use: Initialized new root in {metadata_dir}") |
| 56 |
return True |
| 57 |
|
| 58 |
|
| 59 |
def init(base_url: str) -> Optional[LocalRepository]: |
| 60 |
"""Initialize a LocalRepository: local root.json must already exist""" |
| 61 |
metadata_dir = build_metadata_dir(base_url) |
| 62 |
keydir = build_key_dir(base_url) |
| 63 |
|
| 64 |
if not os.path.isfile(f"{metadata_dir}/root.json"): |
| 65 |
print( |
| 66 |
"Trusted local root not found. Use 'tofu' command to " |
| 67 |
"Trust-On-First-Use or copy trusted root metadata to " |
| 68 |
f"{metadata_dir}/root.json" |
| 69 |
) |
| 70 |
return None |
| 71 |
|
| 72 |
print(f"Using trusted root in {metadata_dir}") |
| 73 |
return LocalRepository(metadata_dir, keydir, base_url) |
| 74 |
|
| 75 |
|
| 76 |
def main(argv: List[str]) -> None: |
| 77 |
"""Example uploader tool""" |
| 78 |
|
| 79 |
parser = argparse.ArgumentParser() |
| 80 |
parser.add_argument("-v", "--verbose", action="count", default=0) |
| 81 |
parser.add_argument( |
| 82 |
"-u", |
| 83 |
"--url", |
| 84 |
help="Base repository URL", |
| 85 |
default="http://127.0.0.1:8001", |
| 86 |
) |
| 87 |
|
| 88 |
subparsers = parser.add_subparsers(dest="sub_command") |
| 89 |
|
| 90 |
tofu_cmd = subparsers.add_parser( |
| 91 |
"tofu", |
| 92 |
help="Initialize client with Trust-On-First-Use", |
| 93 |
) |
| 94 |
|
| 95 |
add_delegation_cmd = subparsers.add_parser( |
| 96 |
"add-delegation", |
| 97 |
help="Create a delegation and signing key", |
| 98 |
) |
| 99 |
add_delegation_cmd.add_argument("rolename") |
| 100 |
|
| 101 |
add_target_cmd = subparsers.add_parser( |
| 102 |
"add-target", |
| 103 |
help="Add a target to a delegated role", |
| 104 |
) |
| 105 |
add_target_cmd.add_argument("rolename") |
| 106 |
add_target_cmd.add_argument("targetpath") |
| 107 |
|
| 108 |
args = parser.parse_args() |
| 109 |
|
| 110 |
if args.verbose == 0: |
| 111 |
loglevel = logging.ERROR |
| 112 |
elif args.verbose == 1: |
| 113 |
loglevel = logging.WARNING |
| 114 |
elif args.verbose == 2: |
| 115 |
loglevel = logging.INFO |
| 116 |
else: |
| 117 |
loglevel = logging.DEBUG |
| 118 |
logging.basicConfig(level=loglevel) |
| 119 |
|
| 120 |
if args.sub_command == "tofu": |
| 121 |
if not init_tofu(args.url): |
| 122 |
return "Failed to initialize local repository" |
| 123 |
elif args.sub_command == "add-delegation": |
| 124 |
repo = init(args.url) |
| 125 |
if not repo: |
| 126 |
return "Failed to initialize" |
| 127 |
if not repo.add_delegation(args.rolename): |
| 128 |
return "Failed to add delegation" |
| 129 |
elif args.sub_command == "add-target": |
| 130 |
repo = init(args.url) |
| 131 |
if not repo: |
| 132 |
return "Failed to initialize" |
| 133 |
if not repo.add_target(args.rolename, args.targetpath): |
| 134 |
return "Failed to add target" |
| 135 |
else: |
| 136 |
parser.print_help() |
| 137 |
|
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
sys.exit(main(sys.argv)) |