Skip to content

Commit df5f49f

Browse files
committed
Add networking settings to client config
Extend the UpdaterConfig class with the settings needed for RequestsFetcher. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 5f9944d commit df5f49f

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

tuf/ngclient/_internal/requests_fetcher.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import urllib3.exceptions
1515

1616
import tuf
17-
from tuf import exceptions, settings
17+
from tuf import exceptions
1818
from tuf.ngclient.fetcher import FetcherInterface
1919

2020
# Globals
@@ -30,7 +30,7 @@ class RequestsFetcher(FetcherInterface):
3030
session per scheme+hostname combination.
3131
"""
3232

33-
def __init__(self):
33+
def __init__(self, config: "UpdaterConfig"):
3434
# http://docs.python-requests.org/en/master/user/advanced/#session-objects:
3535
#
3636
# "The Session object allows you to persist certain parameters across
@@ -46,6 +46,7 @@ def __init__(self):
4646
# hosts-scheme combinations to minimize subtle security issues.
4747
# Some cookies may not be HTTP-safe.
4848
self._sessions = {}
49+
self.config = config
4950

5051
def fetch(self, url, required_length):
5152
"""Fetches the contents of HTTP/HTTPS url from a remote server.
@@ -76,7 +77,7 @@ def fetch(self, url, required_length):
7677
# - connect timeout (max delay before first byte is received)
7778
# - read (gap) timeout (max delay between bytes received)
7879
response = session.get(
79-
url, stream=True, timeout=settings.SOCKET_TIMEOUT
80+
url, stream=True, timeout=self.config.SOCKET_TIMEOUT
8081
)
8182
# Check response status.
8283
try:
@@ -99,11 +100,11 @@ def chunks():
99100
# large file in one shot. Before beginning the round, sleep
100101
# (if set) for a short amount of time so that the CPU is not
101102
# hogged in the while loop.
102-
if settings.SLEEP_BEFORE_ROUND:
103-
time.sleep(settings.SLEEP_BEFORE_ROUND)
103+
if self.config.SLEEP_BEFORE_ROUND:
104+
time.sleep(self.config.SLEEP_BEFORE_ROUND)
104105

105106
read_amount = min(
106-
settings.CHUNK_SIZE,
107+
self.config.CHUNK_SIZE,
107108
required_length - bytes_received,
108109
)
109110

tuf/ngclient/config.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
# Copyright 2021, New York University and the TUF contributors
22
# SPDX-License-Identifier: MIT OR Apache-2.0
33

4-
"""Configuration options for Updater class
4+
"""Client configuration options
55
"""
66

77
from dataclasses import dataclass
8+
from typing import Optional
89

910

1011
@dataclass
1112
class UpdaterConfig:
13+
"""Client workflow configuration"""
14+
15+
# Updater settings
1216
MAX_ROOT_ROTATIONS: int = 32
1317
MAX_DELEGATIONS: int = 32
1418
DEFAULT_ROOT_MAX_LENGTH: int = 512000 # bytes
1519
DEFAULT_TIMESTAMP_MAX_LENGTH: int = 16384 # bytes
1620
DEFAULT_SNAPSHOT_MAX_LENGTH: int = 2000000 # bytes
1721
DEFAULT_TARGETS_MAX_LENGTH: int = 5000000 # bytes
22+
23+
# RequestsFetcher settings
24+
SOCKET_TIMEOUT: int = 4 # seconds
25+
CHUNK_SIZE: int = 400000 # bytes
26+
SLEEP_BEFORE_ROUND: Optional[int] = None

tuf/ngclient/updater.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ def __init__(
6565
data = self._load_local_metadata("root")
6666
self._trusted_set = trusted_metadata_set.TrustedMetadataSet(data)
6767

68-
if fetcher is None:
69-
self._fetcher = requests_fetcher.RequestsFetcher()
70-
else:
71-
self._fetcher = fetcher
72-
7368
if config is None:
7469
self.config = UpdaterConfig()
7570
else:
7671
self.config = config
7772

73+
if fetcher is None:
74+
self._fetcher = requests_fetcher.RequestsFetcher(self.config)
75+
else:
76+
self._fetcher = fetcher
77+
7878
def refresh(self) -> None:
7979
"""
8080
This method downloads, verifies, and loads metadata for the top-level

0 commit comments

Comments
 (0)