Skip to content

Commit 5f9944d

Browse files
committed
Add client config module
Add a config module containing a dataclass UpdaterConfig with all client settings. Initialize updater with default settings if no other condig is provided. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 3398bf3 commit 5f9944d

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

tuf/ngclient/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"""TUF client public API
55
"""
66

7+
from tuf.ngclient.config import UpdaterConfig
78
from tuf.ngclient.fetcher import FetcherInterface
89
from tuf.ngclient.updater import Updater

tuf/ngclient/config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2021, New York University and the TUF contributors
2+
# SPDX-License-Identifier: MIT OR Apache-2.0
3+
4+
"""Configuration options for Updater class
5+
"""
6+
7+
from dataclasses import dataclass
8+
9+
10+
@dataclass
11+
class UpdaterConfig:
12+
MAX_ROOT_ROTATIONS: int = 32
13+
MAX_DELEGATIONS: int = 32
14+
DEFAULT_ROOT_MAX_LENGTH: int = 512000 # bytes
15+
DEFAULT_TIMESTAMP_MAX_LENGTH: int = 16384 # bytes
16+
DEFAULT_SNAPSHOT_MAX_LENGTH: int = 2000000 # bytes
17+
DEFAULT_TARGETS_MAX_LENGTH: int = 5000000 # bytes

tuf/ngclient/updater.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,9 @@
2020
requests_fetcher,
2121
trusted_metadata_set,
2222
)
23+
from tuf.ngclient.config import UpdaterConfig
2324
from tuf.ngclient.fetcher import FetcherInterface
2425

25-
# Globals
26-
MAX_ROOT_ROTATIONS = 32
27-
MAX_DELEGATIONS = 32
28-
DEFAULT_ROOT_MAX_LENGTH = 512000 # bytes
29-
DEFAULT_TIMESTAMP_MAX_LENGTH = 16384 # bytes
30-
DEFAULT_SNAPSHOT_MAX_LENGTH = 2000000 # bytes
31-
DEFAULT_TARGETS_MAX_LENGTH = 5000000 # bytes
32-
3326
logger = logging.getLogger(__name__)
3427

3528
# Classes
@@ -45,6 +38,7 @@ def __init__(
4538
metadata_base_url: str,
4639
target_base_url: Optional[str] = None,
4740
fetcher: Optional[FetcherInterface] = None,
41+
config: Optional[UpdaterConfig] = None,
4842
):
4943
"""
5044
Args:
@@ -76,6 +70,11 @@ def __init__(
7670
else:
7771
self._fetcher = fetcher
7872

73+
if config is None:
74+
self.config = UpdaterConfig()
75+
else:
76+
self.config = config
77+
7978
def refresh(self) -> None:
8079
"""
8180
This method downloads, verifies, and loads metadata for the top-level
@@ -248,12 +247,12 @@ def _load_root(self) -> None:
248247

249248
# Update the root role
250249
lower_bound = self._trusted_set.root.signed.version + 1
251-
upper_bound = lower_bound + MAX_ROOT_ROTATIONS
250+
upper_bound = lower_bound + self.config.MAX_ROOT_ROTATIONS
252251

253252
for next_version in range(lower_bound, upper_bound):
254253
try:
255254
data = self._download_metadata(
256-
"root", DEFAULT_ROOT_MAX_LENGTH, next_version
255+
"root", self.config.DEFAULT_ROOT_MAX_LENGTH, next_version
257256
)
258257
self._trusted_set.update_root(data)
259258
self._persist_metadata("root", data)
@@ -278,7 +277,7 @@ def _load_timestamp(self) -> None:
278277

279278
# Load from remote (whether local load succeeded or not)
280279
data = self._download_metadata(
281-
"timestamp", DEFAULT_TIMESTAMP_MAX_LENGTH
280+
"timestamp", self.config.DEFAULT_TIMESTAMP_MAX_LENGTH
282281
)
283282
self._trusted_set.update_timestamp(data)
284283
self._persist_metadata("timestamp", data)
@@ -294,7 +293,7 @@ def _load_snapshot(self) -> None:
294293
logger.debug("Failed to load local snapshot %s", e)
295294

296295
metainfo = self._trusted_set.timestamp.signed.meta["snapshot.json"]
297-
length = metainfo.length or DEFAULT_SNAPSHOT_MAX_LENGTH
296+
length = metainfo.length or self.config.DEFAULT_SNAPSHOT_MAX_LENGTH
298297
version = None
299298
if self._trusted_set.root.signed.consistent_snapshot:
300299
version = metainfo.version
@@ -314,7 +313,7 @@ def _load_targets(self, role: str, parent_role: str) -> None:
314313
logger.debug("Failed to load local %s: %s", role, e)
315314

316315
metainfo = self._trusted_set.snapshot.signed.meta[f"{role}.json"]
317-
length = metainfo.length or DEFAULT_TARGETS_MAX_LENGTH
316+
length = metainfo.length or self.config.DEFAULT_TARGETS_MAX_LENGTH
318317
version = None
319318
if self._trusted_set.root.signed.consistent_snapshot:
320319
version = metainfo.version
@@ -333,7 +332,7 @@ def _preorder_depth_first_walk(self, target_filepath) -> Dict:
333332
target = None
334333
role_names = [("targets", "root")]
335334
visited_role_names = set()
336-
number_of_delegations = MAX_DELEGATIONS
335+
number_of_delegations = self.config.MAX_DELEGATIONS
337336

338337
# Preorder depth-first traversal of the graph of target delegations.
339338
while (
@@ -414,7 +413,7 @@ def _preorder_depth_first_walk(self, target_filepath) -> Dict:
414413
):
415414
msg = (
416415
f"{len(role_names)} roles left to visit, but allowed to ",
417-
f"visit at most {MAX_DELEGATIONS} delegations.",
416+
f"visit at most {self.config.MAX_DELEGATIONS} delegations.",
418417
)
419418
logger.debug(msg)
420419

0 commit comments

Comments
 (0)