| 1 |
# Copyright 2021, New York University and the TUF contributors |
| 2 |
# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 |
|
| 4 |
"""Test ngclient Updater key rotation handling""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import os |
| 9 |
import sys |
| 10 |
import tempfile |
| 11 |
import unittest |
| 12 |
from dataclasses import dataclass |
| 13 |
from typing import ClassVar |
| 14 |
|
| 15 |
from securesystemslib.signer import CryptoSigner, Signer |
| 16 |
|
| 17 |
from tests import utils |
| 18 |
from tests.repository_simulator import RepositorySimulator |
| 19 |
from tests.utils import run_sub_tests_with_dataset |
| 20 |
from tuf.api.exceptions import UnsignedMetadataError |
| 21 |
from tuf.api.metadata import Key, Root |
| 22 |
from tuf.ngclient import Updater |
| 23 |
|
| 24 |
|
| 25 |
@dataclass |
| 26 |
class MdVersion: |
| 27 |
keys: list[int] |
| 28 |
threshold: int |
| 29 |
sigs: list[int] |
| 30 |
res: type[Exception] | None = None |
| 31 |
|
| 32 |
|
| 33 |
class TestUpdaterKeyRotations(unittest.TestCase): |
| 34 |
"""Test ngclient root rotation handling""" |
| 35 |
|
| 36 |
# set dump_dir to trigger repository state dumps |
| 37 |
dump_dir: str | None = None |
| 38 |
temp_dir: ClassVar[tempfile.TemporaryDirectory] |
| 39 |
keys: ClassVar[list[Key]] |
| 40 |
signers: ClassVar[list[Signer]] |
| 41 |
|
| 42 |
@classmethod |
| 43 |
def setUpClass(cls) -> None: |
| 44 |
cls.temp_dir = tempfile.TemporaryDirectory() |
| 45 |
|
| 46 |
# Pre-create a bunch of keys and signers |
| 47 |
cls.signers = [] |
| 48 |
for _ in range(10): |
| 49 |
signer = CryptoSigner.generate_ed25519() |
| 50 |
cls.signers.append(signer) |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def tearDownClass(cls) -> None: |
| 54 |
cls.temp_dir.cleanup() |
| 55 |
|
| 56 |
def setup_subtest(self) -> None: |
| 57 |
# Setup repository for subtest: make sure no roots have been published |
| 58 |
self.sim = RepositorySimulator() |
| 59 |
self.sim.signed_roots.clear() |
| 60 |
self.sim.root.version = 0 |
| 61 |
|
| 62 |
if self.dump_dir is not None: |
| 63 |
# create subtest dumpdir |
| 64 |
name = f"{self.id().split('.')[-1]}-{self.case_name}" |
| 65 |
self.sim.dump_dir = os.path.join(self.dump_dir, name) |
| 66 |
os.mkdir(self.sim.dump_dir) |
| 67 |
|
| 68 |
def _run_refresh(self) -> None: |
| 69 |
"""Create new updater, run refresh""" |
| 70 |
if self.sim.dump_dir is not None: |
| 71 |
self.sim.write() |
| 72 |
|
| 73 |
# bootstrap with initial root |
| 74 |
self.metadata_dir = tempfile.mkdtemp(dir=self.temp_dir.name) |
| 75 |
|
| 76 |
updater = Updater( |
| 77 |
self.metadata_dir, |
| 78 |
"https://example.com/metadata/", |
| 79 |
fetcher=self.sim, |
| 80 |
bootstrap=self.sim.signed_roots[0], |
| 81 |
) |
| 82 |
updater.refresh() |
| 83 |
|
| 84 |
# fmt: off |
| 85 |
root_rotation_cases = { |
| 86 |
"1-of-1 key rotation": [ |
| 87 |
MdVersion(keys=[1], threshold=1, sigs=[1]), |
| 88 |
MdVersion(keys=[2], threshold=1, sigs=[2, 1]), |
| 89 |
MdVersion(keys=[2], threshold=1, sigs=[2]), |
| 90 |
], |
| 91 |
"1-of-1 key rotation, unused signatures": [ |
| 92 |
MdVersion(keys=[1], threshold=1, sigs=[3, 1, 4]), |
| 93 |
MdVersion(keys=[2], threshold=1, sigs=[3, 2, 1, 4]), |
| 94 |
MdVersion(keys=[2], threshold=1, sigs=[3, 2, 4]), |
| 95 |
], |
| 96 |
"1-of-1 key rotation fail: not signed with old key": [ |
| 97 |
MdVersion(keys=[1], threshold=1, sigs=[1]), |
| 98 |
MdVersion(keys=[2], threshold=1, sigs=[2, 3, 4], res=UnsignedMetadataError), |
| 99 |
], |
| 100 |
"1-of-1 key rotation fail: not signed with new key": [ |
| 101 |
MdVersion(keys=[1], threshold=1, sigs=[1]), |
| 102 |
MdVersion(keys=[2], threshold=1, sigs=[1, 3, 4], res=UnsignedMetadataError), |
| 103 |
], |
| 104 |
"3-of-5, sign with different keycombos": [ |
| 105 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 2, 4]), |
| 106 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 4, 1]), |
| 107 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 1, 3]), |
| 108 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 1, 3]), |
| 109 |
], |
| 110 |
"3-of-5, one key rotated": [ |
| 111 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 2, 4]), |
| 112 |
MdVersion(keys=[0, 1, 3, 4, 5], threshold=3, sigs=[0, 4, 1]), |
| 113 |
], |
| 114 |
"3-of-5, one key rotate fails: not signed with 3 new keys": [ |
| 115 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 2, 4]), |
| 116 |
MdVersion(keys=[0, 1, 3, 4, 5], threshold=3, sigs=[0, 2, 4], res=UnsignedMetadataError), |
| 117 |
], |
| 118 |
"3-of-5, one key rotate fails: not signed with 3 old keys": [ |
| 119 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 2, 4]), |
| 120 |
MdVersion(keys=[0, 1, 3, 4, 5], threshold=3, sigs=[0, 4, 5], res=UnsignedMetadataError), |
| 121 |
], |
| 122 |
"3-of-5, one key rotated, with intermediate step": [ |
| 123 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 2, 4]), |
| 124 |
MdVersion(keys=[0, 1, 3, 4, 5], threshold=3, sigs=[0, 2, 4, 5]), |
| 125 |
MdVersion(keys=[0, 1, 3, 4, 5], threshold=3, sigs=[0, 4, 5]), |
| 126 |
], |
| 127 |
"3-of-5, all keys rotated, with intermediate step": [ |
| 128 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 2, 4]), |
| 129 |
MdVersion(keys=[5, 6, 7, 8, 9], threshold=3, sigs=[0, 2, 4, 5, 6, 7]), |
| 130 |
MdVersion(keys=[5, 6, 7, 8, 9], threshold=3, sigs=[5, 6, 7]), |
| 131 |
], |
| 132 |
"1-of-3 threshold increase to 2-of-3": [ |
| 133 |
MdVersion(keys=[1, 2, 3], threshold=1, sigs=[1]), |
| 134 |
MdVersion(keys=[1, 2, 3], threshold=2, sigs=[1, 2]), |
| 135 |
], |
| 136 |
"1-of-3 threshold bump to 2-of-3 fails: new threshold not reached": [ |
| 137 |
MdVersion(keys=[1, 2, 3], threshold=1, sigs=[1]), |
| 138 |
MdVersion(keys=[1, 2, 3], threshold=2, sigs=[2], res=UnsignedMetadataError), |
| 139 |
], |
| 140 |
"2-of-3 threshold decrease to 1-of-3": [ |
| 141 |
MdVersion(keys=[1, 2, 3], threshold=2, sigs=[1, 2]), |
| 142 |
MdVersion(keys=[1, 2, 3], threshold=1, sigs=[1, 2]), |
| 143 |
MdVersion(keys=[1, 2, 3], threshold=1, sigs=[1]), |
| 144 |
], |
| 145 |
"2-of-3 threshold decr. to 1-of-3 fails: old threshold not reached": [ |
| 146 |
MdVersion(keys=[1, 2, 3], threshold=2, sigs=[1, 2]), |
| 147 |
MdVersion(keys=[1, 2, 3], threshold=1, sigs=[1], res=UnsignedMetadataError), |
| 148 |
], |
| 149 |
"1-of-2 threshold increase to 2-of-2": [ |
| 150 |
MdVersion(keys=[1], threshold=1, sigs=[1]), |
| 151 |
MdVersion(keys=[1, 2], threshold=2, sigs=[1, 2]), |
| 152 |
], |
| 153 |
} |
| 154 |
# fmt: on |
| 155 |
|
| 156 |
@run_sub_tests_with_dataset(root_rotation_cases) |
| 157 |
def test_root_rotation(self, root_versions: list[MdVersion]) -> None: |
| 158 |
"""Test Updater.refresh() with various sequences of root updates |
| 159 |
|
| 160 |
Each MdVersion in the list describes root keys and signatures of a |
| 161 |
remote root metadata version. As an example: |
| 162 |
MdVersion([1,2,3], 2, [1,2]) |
| 163 |
defines a root that contains keys 1, 2 and 3 with threshold 2. The |
| 164 |
metadata is signed with keys 1 and 2. |
| 165 |
|
| 166 |
Assert that refresh() result is expected and that local root on disk is |
| 167 |
the expected one after all roots have been loaded from remote using the |
| 168 |
standard client update workflow. |
| 169 |
""" |
| 170 |
self.setup_subtest() |
| 171 |
|
| 172 |
# Publish all remote root versions defined in root_versions |
| 173 |
for rootver in root_versions: |
| 174 |
# clear root keys, signers |
| 175 |
self.sim.root.roles[Root.type].keyids.clear() |
| 176 |
self.sim.signers[Root.type].clear() |
| 177 |
|
| 178 |
self.sim.root.roles[Root.type].threshold = rootver.threshold |
| 179 |
for i in rootver.keys: |
| 180 |
self.sim.root.add_key(self.signers[i].public_key, Root.type) |
| 181 |
for i in rootver.sigs: |
| 182 |
self.sim.add_signer(Root.type, self.signers[i]) |
| 183 |
self.sim.root.version += 1 |
| 184 |
self.sim.publish_root() |
| 185 |
|
| 186 |
# run client workflow, assert success/failure |
| 187 |
expected_error = root_versions[-1].res |
| 188 |
if expected_error is None: |
| 189 |
self._run_refresh() |
| 190 |
expected_local_root = self.sim.signed_roots[-1] |
| 191 |
else: |
| 192 |
# failure expected: local root should be the root before last |
| 193 |
with self.assertRaises(expected_error): |
| 194 |
self._run_refresh() |
| 195 |
expected_local_root = self.sim.signed_roots[-2] |
| 196 |
|
| 197 |
# assert local root on disk is expected |
| 198 |
with open(os.path.join(self.metadata_dir, "root.json"), "rb") as f: |
| 199 |
self.assertEqual(f.read(), expected_local_root) |
| 200 |
|
| 201 |
# fmt: off |
| 202 |
non_root_rotation_cases: dict[str, MdVersion] = { |
| 203 |
"1-of-1 key rotation": |
| 204 |
MdVersion(keys=[2], threshold=1, sigs=[2]), |
| 205 |
"1-of-1 key rotation, unused signatures": |
| 206 |
MdVersion(keys=[1], threshold=1, sigs=[3, 1, 4]), |
| 207 |
"1-of-1 key rotation fail: not signed with new key": |
| 208 |
MdVersion(keys=[2], threshold=1, sigs=[1, 3, 4], res=UnsignedMetadataError), |
| 209 |
"3-of-5, one key signature wrong: not signed with 3 expected keys": |
| 210 |
MdVersion(keys=[0, 1, 3, 4, 5], threshold=3, sigs=[0, 2, 4], res=UnsignedMetadataError), |
| 211 |
"2-of-5, one key signature missing: threshold not reached": |
| 212 |
MdVersion(keys=[0, 1, 3, 4, 5], threshold=3, sigs=[0, 4], res=UnsignedMetadataError), |
| 213 |
"3-of-5, sign first combo": |
| 214 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 2, 4]), |
| 215 |
"3-of-5, sign second combo": |
| 216 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 4, 1]), |
| 217 |
"3-of-5, sign third combo": |
| 218 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[0, 1, 3]), |
| 219 |
"3-of-5, sign fourth combo": |
| 220 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[1, 2, 3]), |
| 221 |
"3-of-5, sign fifth combo": |
| 222 |
MdVersion(keys=[0, 1, 2, 3, 4], threshold=3, sigs=[2, 3, 4]), |
| 223 |
} |
| 224 |
# fmt: on |
| 225 |
|
| 226 |
@run_sub_tests_with_dataset(non_root_rotation_cases) |
| 227 |
def test_non_root_rotations(self, md_version: MdVersion) -> None: |
| 228 |
"""Test Updater.refresh() with various sequences of metadata updates |
| 229 |
|
| 230 |
Each MdVersion in the list describes metadata keys and signatures |
| 231 |
of a remote metadata version. As an example: |
| 232 |
MdVersion([1,2,3], 2, [1,2]) |
| 233 |
defines a metadata that contains keys 1, 2 and 3 with threshold 2. The |
| 234 |
metadata is signed with keys 1 and 2. |
| 235 |
|
| 236 |
Assert that refresh() result is expected and that local metadata on disk |
| 237 |
is the expected one after all roots have been loaded from remote using |
| 238 |
the standard client update workflow. |
| 239 |
""" |
| 240 |
self.setup_subtest() |
| 241 |
roles = ["timestamp", "snapshot", "targets"] |
| 242 |
for role in roles: |
| 243 |
# clear role keys, signers |
| 244 |
self.sim.root.roles[role].keyids.clear() |
| 245 |
self.sim.signers[role].clear() |
| 246 |
|
| 247 |
self.sim.root.roles[role].threshold = md_version.threshold |
| 248 |
for i in md_version.keys: |
| 249 |
self.sim.root.add_key(self.signers[i].public_key, role) |
| 250 |
|
| 251 |
for i in md_version.sigs: |
| 252 |
self.sim.add_signer(role, self.signers[i]) |
| 253 |
|
| 254 |
self.sim.root.version += 1 |
| 255 |
self.sim.publish_root() |
| 256 |
|
| 257 |
# run client workflow, assert success/failure |
| 258 |
expected_error = md_version.res |
| 259 |
if expected_error is None: |
| 260 |
self._run_refresh() |
| 261 |
|
| 262 |
# Call fetch_metadata to sign metadata with new keys |
| 263 |
expected_local_md: bytes = self.sim.fetch_metadata(role) |
| 264 |
# assert local metadata role is on disk as expected |
| 265 |
md_path = os.path.join(self.metadata_dir, f"{role}.json") |
| 266 |
with open(md_path, "rb") as f: |
| 267 |
data = f.read() |
| 268 |
self.assertEqual(data, expected_local_md) |
| 269 |
else: |
| 270 |
# failure expected |
| 271 |
with self.assertRaises(expected_error): |
| 272 |
self._run_refresh() |
| 273 |
|
| 274 |
|
| 275 |
if __name__ == "__main__": |
| 276 |
if "--dump" in sys.argv: |
| 277 |
TestUpdaterKeyRotations.dump_dir = tempfile.mkdtemp() |
| 278 |
print(f"Repository dumps in {TestUpdaterKeyRotations.dump_dir}") |
| 279 |
sys.argv.remove("--dump") |
| 280 |
|
| 281 |
utils.configure_test_logging(sys.argv) |
| 282 |
unittest.main() |