|
| 1 | +# Copyright New York University and the TUF contributors |
| 2 | +# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | +""" |
| 4 | +A TUF succinct hash bin delegation example using the low-level TUF Metadata API. |
| 5 | +
|
| 6 | +The example code in this file demonstrates how to perform succinct hash bin |
| 7 | +delegation using the low-level Metadata API. |
| 8 | +Succinct hash bin delegation achieves a similar result as using a standard hash |
| 9 | +bin delegation, but the delegating metadata is smaller, resulting in fewer bytes |
| 10 | +to transfer and parse. |
| 11 | +
|
| 12 | +See 'basic_repo.py' for a more comprehensive TUF metadata API example. |
| 13 | +
|
| 14 | +For a comprehensive explanation of succinct hash bin delegation and the |
| 15 | +difference between succinct and standard hash bin delegation read: |
| 16 | +https://github.com/theupdateframework/taps/blob/master/tap15.md |
| 17 | +
|
| 18 | +NOTE: Metadata files will be written to a 'tmp*'-directory in CWD. |
| 19 | +""" |
| 20 | +import math |
| 21 | +import os |
| 22 | +import tempfile |
| 23 | +from datetime import datetime, timedelta |
| 24 | +from pathlib import Path |
| 25 | +from typing import Dict, Tuple |
| 26 | + |
| 27 | +from securesystemslib.keys import generate_ed25519_key |
| 28 | +from securesystemslib.signer import SSlibSigner |
| 29 | + |
| 30 | +from tuf.api.metadata import ( |
| 31 | + Delegations, |
| 32 | + Key, |
| 33 | + Metadata, |
| 34 | + SuccinctRoles, |
| 35 | + TargetFile, |
| 36 | + Targets, |
| 37 | +) |
| 38 | +from tuf.api.serialization.json import JSONSerializer |
| 39 | + |
| 40 | +# Succinct hash bin delegation |
| 41 | +# ============================ |
| 42 | +# Succinct hash bin delegation aims to distribute a large number of target files |
| 43 | +# over multiple delegated targets metadata roles (bins). The consequence is |
| 44 | +# smaller metadata files and thus a lower network overhead for repository-client |
| 45 | +# communication. |
| 46 | +# |
| 47 | +# The assignment of target files to a target's metadata is done automatically, |
| 48 | +# based on the byte digest of the target file name. |
| 49 | +# |
| 50 | +# The number of bins, name prefix for all bins and key threshold are all |
| 51 | +# attributes that need to be configured. |
| 52 | + |
| 53 | +# Number of bins, bit length and bin number computation |
| 54 | +# ----------------------------------------------------- |
| 55 | +# Determining the correct number of bins is dependent on the expected number of |
| 56 | +# target files in a repository. For the purpose of this example we choose: |
| 57 | +NUMBER_OF_BINS = 32 |
| 58 | +# |
| 59 | +# The number of bins will determine the number of bits in a target path |
| 60 | +# considered in assigning the target to a bin. |
| 61 | +BIT_LENGTH = int(math.log2(NUMBER_OF_BINS)) |
| 62 | + |
| 63 | +# Delegated role (bin) name format |
| 64 | +# -------------------------------- |
| 65 | +# Each bin has a name in the format of f"{NAME_PREFIX}-{bin_number}". |
| 66 | +# |
| 67 | +# Name prefix is the common prefix of all delegated target roles (bins). |
| 68 | +# For our example it will be: |
| 69 | +NAME_PREFIX = "delegated_bin" |
| 70 | +# |
| 71 | +# The suffix "bin_number" is a zero-padded hexadecimal number of that |
| 72 | +# particular bin. |
| 73 | + |
| 74 | +# Keys and threshold |
| 75 | +# ------------------ |
| 76 | +# Succinct hash bin delegation uses the same key(s) to sign all bins. This is |
| 77 | +# acceptable because the primary concern of this type of delegation is to reduce |
| 78 | +# network overhead. For the purpose of this example only one key is required. |
| 79 | +THRESHOLD = 1 |
| 80 | + |
| 81 | + |
| 82 | +def create_key() -> Tuple[Key, SSlibSigner]: |
| 83 | + """Generates a new Key and Signer.""" |
| 84 | + sslib_key = generate_ed25519_key() |
| 85 | + return Key.from_securesystemslib_key(sslib_key), SSlibSigner(sslib_key) |
| 86 | + |
| 87 | + |
| 88 | +# Create one signing key for all bins, and one for the delegating targets role. |
| 89 | +bins_key, bins_signer = create_key() |
| 90 | +_, targets_signer = create_key() |
| 91 | + |
| 92 | +# Delegating targets role |
| 93 | +# ----------------------- |
| 94 | +# Akin to regular targets delegation, the delegating role ships the public keys |
| 95 | +# of the delegated roles. However, instead of providing individual delegation |
| 96 | +# information about each role, one single `SuccinctRoles` object is used to |
| 97 | +# provide the information for all delegated roles (bins). |
| 98 | + |
| 99 | +# NOTE: See "Targets" and "Targets delegation" paragraphs in 'basic_repo.py' |
| 100 | +# example for more details about the Targets object. |
| 101 | + |
| 102 | +expiration_date = datetime.utcnow().replace(microsecond=0) + timedelta(days=7) |
| 103 | +targets = Metadata(Targets(expires=expiration_date)) |
| 104 | + |
| 105 | +succinct_roles = SuccinctRoles( |
| 106 | + keyids=[bins_key.keyid], |
| 107 | + threshold=THRESHOLD, |
| 108 | + bit_length=BIT_LENGTH, |
| 109 | + name_prefix=NAME_PREFIX, |
| 110 | +) |
| 111 | +delegations_keys_info: Dict[str, Key] = {} |
| 112 | +delegations_keys_info[bins_key.keyid] = bins_key |
| 113 | + |
| 114 | +targets.signed.delegations = Delegations( |
| 115 | + delegations_keys_info, roles=None, succinct_roles=succinct_roles |
| 116 | +) |
| 117 | + |
| 118 | +# Delegated targets roles (bins) |
| 119 | +# ------------------------------ |
| 120 | +# We can use the SuccinctRoles object from the delegating role above to iterate |
| 121 | +# over all bin names in the delegation and create the corresponding metadata. |
| 122 | + |
| 123 | +assert targets.signed.delegations.succinct_roles is not None # make mypy happy |
| 124 | + |
| 125 | +delegated_bins: Dict[str, Metadata[Targets]] = {} |
| 126 | +for delegated_bin_name in targets.signed.delegations.succinct_roles.get_roles(): |
| 127 | + delegated_bins[delegated_bin_name] = Metadata( |
| 128 | + Targets(expires=expiration_date) |
| 129 | + ) |
| 130 | + |
| 131 | +# Add target file inside a delegated role (bin) |
| 132 | +# --------------------------------------------- |
| 133 | +# For the purpose of this example we will protect the integrity of this |
| 134 | +# example script by adding its file info to the corresponding bin metadata. |
| 135 | + |
| 136 | +# NOTE: See "Targets" paragraph in 'basic_repo.py' example for more details |
| 137 | +# about adding target file infos to targets metadata. |
| 138 | +local_path = Path(__file__).resolve() |
| 139 | +target_path = f"{local_path.parts[-2]}/{local_path.parts[-1]}" |
| 140 | +target_file_info = TargetFile.from_file(target_path, str(local_path)) |
| 141 | + |
| 142 | +# We don't know yet in which delegated role (bin) our target belongs. |
| 143 | +# With SuccinctRoles.get_role_for_target() we can get the name of the delegated |
| 144 | +# role (bin) responsible for that target_path. |
| 145 | +target_bin = targets.signed.delegations.succinct_roles.get_role_for_target( |
| 146 | + target_path |
| 147 | +) |
| 148 | + |
| 149 | +# In our example with NUMBER_OF_BINS = 32 and the current file as target_path |
| 150 | +# the target_bin is "delegated_bin-0d" |
| 151 | + |
| 152 | +# Now we can add the current target to the bin responsible for it. |
| 153 | +delegated_bins[target_bin].signed.targets[target_path] = target_file_info |
| 154 | + |
| 155 | +# Sign and persist |
| 156 | +# ---------------- |
| 157 | +# Sign all metadata and write to a temporary directory at CWD for review using |
| 158 | +# versioned file names. Most notably see '1.targets.json' and |
| 159 | +# '1.delegated_bin-0d.json'. |
| 160 | + |
| 161 | +# NOTE: See "Persist metadata" paragraph in 'basic_repo.py' example for more |
| 162 | +# details about serialization formats and metadata file name convention. |
| 163 | +PRETTY = JSONSerializer(compact=False) |
| 164 | +TMP_DIR = tempfile.mkdtemp(dir=os.getcwd()) |
| 165 | + |
| 166 | + |
| 167 | +targets.sign(targets_signer) |
| 168 | +targets.to_file(os.path.join(TMP_DIR, "1.targets.json"), serializer=PRETTY) |
| 169 | + |
| 170 | +for bin_name, bin_target_role in delegated_bins.items(): |
| 171 | + file_name = f"1.{bin_name}.json" |
| 172 | + file_path = os.path.join(TMP_DIR, file_name) |
| 173 | + |
| 174 | + bin_target_role.sign(bins_signer, append=True) |
| 175 | + |
| 176 | + bin_target_role.to_file(file_path, serializer=PRETTY) |
0 commit comments