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

Add Key.from_securesystemslib_key #1520

Merged
merged 1 commit into from
Aug 31, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
Signature
)

from securesystemslib.keys import (
generate_ed25519_key
)

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -421,6 +425,14 @@ def test_metadata_verify_delegate(self):
root.verify_delegate('snapshot', snapshot)


def test_key_class(self):
# Test if from_securesystemslib_key removes the private key from keyval
# of a securesystemslib key dictionary.
sslib_key = generate_ed25519_key()
key = Key.from_securesystemslib_key(sslib_key)
self.assertFalse('private' in key.keyval.keys())


def test_metadata_root(self):
root_path = os.path.join(
self.repo_dir, 'metadata', 'root.json')
Expand Down
18 changes: 18 additions & 0 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,24 @@ def to_securesystemslib_key(self) -> Dict[str, Any]:
"keyval": self.keyval,
}

@classmethod
def from_securesystemslib_key(cls, key_dict: Dict[str, Any]) -> "Key":
"""
Creates a Key object from a securesystemlib key dict representation
removing the private key from keyval.
"""
key_meta = sslib_keys.format_keyval_to_metadata(
key_dict["keytype"],
key_dict["scheme"],
key_dict["keyval"],
)
return cls(
key_dict["keyid"],
key_meta["keytype"],
key_meta["scheme"],
key_meta["keyval"],
)

def verify_signature(
self,
metadata: Metadata,
Expand Down