Skip to content

Commit

Permalink
from_securesystemslib_key() raise ValueError
Browse files Browse the repository at this point in the history
If a securesystemslib.FormatError is raised inside
Key.from_securesystemslib_key() then reraise ValueError.
This is done so that our users don't have to import securesystemslib
in order to handle the error and because the securesystemslib error
itself is securesystemslib implementation-specific.

Signed-off-by: Martin Vrachev <[email protected]>
  • Loading branch information
MVrachev committed Feb 8, 2022
1 parent b497180 commit 8b6566a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ def test_key_class(self) -> None:
key = Key.from_securesystemslib_key(sslib_key)
self.assertFalse("private" in key.keyval.keys())

# Test raising ValueError with non-existent keytype
sslib_key["keytype"] = "bad keytype"
with self.assertRaises(ValueError):
Key.from_securesystemslib_key(sslib_key)

def test_root_add_key_and_remove_key(self) -> None:
root_path = os.path.join(self.repo_dir, "metadata", "root.json")
root = Metadata[Root].from_file(root_path)
Expand Down
20 changes: 15 additions & 5 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,22 @@ def from_securesystemslib_key(cls, key_dict: Dict[str, Any]) -> "Key":
Args:
key_dict: Key in securesystemlib dict representation.
Raises:
ValueError: ``key_dict`` value is not following the securesystemslib
format.
"""
key_meta = sslib_keys.format_keyval_to_metadata(
key_dict["keytype"],
key_dict["scheme"],
key_dict["keyval"],
)
try:
key_meta = sslib_keys.format_keyval_to_metadata(
key_dict["keytype"],
key_dict["scheme"],
key_dict["keyval"],
)
except sslib_exceptions.FormatError as e:
raise ValueError(
"key_dict value is not following the securesystemslib format"
) from e

return cls(
key_dict["keyid"],
key_meta["keytype"],
Expand Down

0 comments on commit 8b6566a

Please sign in to comment.