Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key.from_securesystemslib_key() raise ValueError #1840

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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