Skip to content

Commit cb7bd6a

Browse files
authored
Merge pull request #1734 from MVrachev/securesystemslib-exceptions
Metadata API: Avoid raising securesystemslib exceptions
2 parents 4de5617 + 9533c3f commit cb7bd6a

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

tests/test_api.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import sys
1414
import tempfile
1515
import unittest
16+
from copy import copy
1617
from datetime import datetime, timedelta
17-
from typing import ClassVar, Dict
18+
from typing import Any, ClassVar, Dict
1819

1920
from securesystemslib import hash as sslib_hash
2021
from securesystemslib.interface import (
@@ -51,7 +52,7 @@ class TestMetadata(unittest.TestCase):
5152
temporary_directory: ClassVar[str]
5253
repo_dir: ClassVar[str]
5354
keystore_dir: ClassVar[str]
54-
keystore: ClassVar[Dict[str, str]]
55+
keystore: ClassVar[Dict[str, Dict[str, Any]]]
5556

5657
@classmethod
5758
def setUpClass(cls) -> None:
@@ -126,6 +127,16 @@ def test_generic_read(self) -> None:
126127

127128
os.remove(bad_metadata_path)
128129

130+
def test_md_read_write_file_exceptions(self) -> None:
131+
# Test writing to a file with bad filename
132+
with self.assertRaises(exceptions.StorageError):
133+
Metadata.from_file("bad-metadata.json")
134+
135+
# Test serializing to a file with bad filename
136+
with self.assertRaises(exceptions.StorageError):
137+
md = Metadata.from_file(f"{self.repo_dir}/metadata/root.json")
138+
md.to_file("")
139+
129140
def test_compact_json(self) -> None:
130141
path = os.path.join(self.repo_dir, "metadata", "targets.json")
131142
md_obj = Metadata.from_file(path)
@@ -212,6 +223,17 @@ def test_sign_verify(self) -> None:
212223
with self.assertRaises(exceptions.UnsignedMetadataError):
213224
targets_key.verify_signature(md_obj)
214225

226+
def test_sign_failures(self) -> None:
227+
# Test throwing UnsignedMetadataError because of signing problems
228+
# related to bad information in the signer.
229+
md = Metadata.from_file(f"{self.repo_dir}/metadata/snapshot.json")
230+
key_dict = copy(self.keystore[Snapshot.type])
231+
key_dict["keytype"] = "rsa"
232+
key_dict["scheme"] = "bad_scheme"
233+
sslib_signer = SSlibSigner(key_dict)
234+
with self.assertRaises(exceptions.UnsignedMetadataError):
235+
md.sign(sslib_signer)
236+
215237
def test_verify_failures(self) -> None:
216238
root_path = os.path.join(self.repo_dir, "metadata", "root.json")
217239
root = Metadata[Root].from_file(root_path).signed

tuf/api/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#### Repository errors ####
1212

13+
# pylint: disable=unused-import
14+
from securesystemslib.exceptions import StorageError
15+
1316

1417
class RepositoryError(Exception):
1518
"""An error with a repository's state, such as a missing file.

tuf/api/metadata.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def from_file(
183183
a (local) FilesystemBackend is used.
184184
185185
Raises:
186-
securesystemslib.exceptions.StorageError: The file cannot be read.
186+
exceptions.StorageError: The file cannot be read.
187187
tuf.api.serialization.DeserializationError:
188188
The file cannot be deserialized.
189189
@@ -275,8 +275,7 @@ def to_file(
275275
Raises:
276276
tuf.api.serialization.SerializationError:
277277
The metadata object cannot be serialized.
278-
securesystemslib.exceptions.StorageError:
279-
The file cannot be written.
278+
exceptions.StorageError: The file cannot be written.
280279
"""
281280

282281
bytes_data = self.to_bytes(serializer)
@@ -305,9 +304,7 @@ def sign(
305304
Raises:
306305
tuf.api.serialization.SerializationError:
307306
'signed' cannot be serialized.
308-
securesystemslib.exceptions.CryptoError, \
309-
securesystemslib.exceptions.UnsupportedAlgorithmError:
310-
Signing errors.
307+
exceptions.UnsignedMetadataError: Signing errors.
311308
312309
Returns:
313310
Securesystemslib Signature object that was added into signatures.
@@ -320,7 +317,14 @@ def sign(
320317

321318
signed_serializer = CanonicalJSONSerializer()
322319

323-
signature = signer.sign(signed_serializer.serialize(self.signed))
320+
bytes_data = signed_serializer.serialize(self.signed)
321+
322+
try:
323+
signature = signer.sign(bytes_data)
324+
except Exception as e:
325+
raise exceptions.UnsignedMetadataError(
326+
"Problem signing the metadata"
327+
) from e
324328

325329
if not append:
326330
self.signatures.clear()

0 commit comments

Comments
 (0)