Skip to content

Commit c8a3f73

Browse files
committed
Move and increase gpg command timeout
Prior to this commit, settings.SUBPROCESS_TIMEOUT=3 (seconds) was used as default and also to configure timeouts for any subprocess spawned via the securesystemslib process interface, including running the gpg command to sign, export keys and check version. The small timeout regularly lead to test failures on slow test runners. Furthermore, configuring the timeout via global is bad practice and error-prone (see secure-systems-lab#219, secure-systems-lab#345). This patch defines a custom constant, to be used only for gpg subprocesses and increases its value to 10 seconds. To override this default, an optional timeout argument will be added to relevant gpg interfaces in a subsequent commit. Signed-off-by: Lukas Puehringer <[email protected]>
1 parent e809bf9 commit c8a3f73

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

securesystemslib/gpg/constants.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@
2424

2525
log = logging.getLogger(__name__)
2626

27+
GPG_TIMEOUT = 10
28+
2729

2830
@functools.lru_cache(maxsize=3)
2931
def is_available_gnupg(gnupg: str) -> bool:
3032
"""Returns whether gnupg points to a gpg binary."""
3133
gpg_version_cmd = gnupg + " --version"
3234
try:
33-
process.run(gpg_version_cmd, stdout=process.PIPE, stderr=process.PIPE)
35+
process.run(
36+
gpg_version_cmd,
37+
stdout=process.PIPE,
38+
stderr=process.PIPE,
39+
timeout=GPG_TIMEOUT,
40+
)
3441
return True
3542
except (OSError, subprocess.TimeoutExpired):
3643
return False

securesystemslib/gpg/functions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from securesystemslib.gpg.constants import (
2727
FULLY_SUPPORTED_MIN_VERSION,
28+
GPG_TIMEOUT,
2829
NO_GPG_MSG,
2930
SHA256,
3031
gpg_export_pubkey_command,
@@ -127,6 +128,7 @@ def create_signature(content, keyid=None, homedir=None):
127128
check=False,
128129
stdout=process.PIPE,
129130
stderr=process.PIPE,
131+
timeout=GPG_TIMEOUT,
130132
)
131133

132134
# TODO: It's suggested to take a look at `--status-fd` for proper error
@@ -307,7 +309,9 @@ def export_pubkey(keyid, homedir=None):
307309
# TODO: Consider adopting command error handling from `create_signature`
308310
# above, e.g. in a common 'run gpg command' utility function
309311
command = gpg_export_pubkey_command(keyid=keyid, homearg=homearg)
310-
gpg_process = process.run(command, stdout=process.PIPE, stderr=process.PIPE)
312+
gpg_process = process.run(
313+
command, stdout=process.PIPE, stderr=process.PIPE, timeout=GPG_TIMEOUT
314+
)
311315

312316
key_packet = gpg_process.stdout
313317
key_bundle = get_pubkey_bundle(key_packet, keyid)

securesystemslib/gpg/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ def get_version() -> Version:
380380
stdout=process.PIPE,
381381
stderr=process.PIPE,
382382
universal_newlines=True,
383+
timeout=constants.GPG_TIMEOUT,
383384
)
384385

385386
full_version_info = gpg_process.stdout

tests/test_gpg.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
parse_signature_packet,
4545
)
4646
from securesystemslib.gpg.constants import (
47+
GPG_TIMEOUT,
4748
PACKET_TYPE_PRIMARY_KEY,
4849
PACKET_TYPE_SUB_KEY,
4950
PACKET_TYPE_USER_ATTR,
@@ -234,14 +235,18 @@ def setUpClass(self): # pylint: disable=bad-classmethod-argument
234235
# erroneous gpg data in tests below.
235236
keyid = "F557D0FF451DEF45372591429EA70BD13D883381"
236237
cmd = gpg_export_pubkey_command(keyid=keyid, homearg=homearg)
237-
proc = process.run(cmd, stdout=process.PIPE, stderr=process.PIPE)
238+
proc = process.run(
239+
cmd, stdout=process.PIPE, stderr=process.PIPE, timeout=GPG_TIMEOUT
240+
)
238241
self.raw_key_data = proc.stdout
239242
self.raw_key_bundle = parse_pubkey_bundle(self.raw_key_data)
240243

241244
# Export pubkey bundle with expired key for key expiration tests
242245
keyid = "E8AC80C924116DABB51D4B987CB07D6D2C199C7C"
243246
cmd = gpg_export_pubkey_command(keyid=keyid, homearg=homearg)
244-
proc = process.run(cmd, stdout=process.PIPE, stderr=process.PIPE)
247+
proc = process.run(
248+
cmd, stdout=process.PIPE, stderr=process.PIPE, timeout=GPG_TIMEOUT
249+
)
245250
self.raw_expired_key_bundle = parse_pubkey_bundle(proc.stdout)
246251

247252
def test_parse_pubkey_payload_errors(self):

0 commit comments

Comments
 (0)