Skip to content

Commit

Permalink
minor edits
Browse files Browse the repository at this point in the history
* classmethod for init RAMKey from file
* private class variables
* more typing for methods
* better names for arguments

Signed-off-by: Trishank Karthik Kuppusamy <[email protected]>
  • Loading branch information
trishankatdatadog committed Jul 8, 2020
1 parent 4237287 commit 76cb560
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 84 deletions.
29 changes: 14 additions & 15 deletions tests/test_tuf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def tearDownClass(cls):

def _load_key_ring(self):
key_list = []
root_key = keys.read_key(os.path.join(self.keystore_dir, 'root_key'),
'RSA', 'password')
root_key = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, 'root_key'),
'RSA', 'password')
key_list.append(root_key)

for key_file in os.listdir(self.keystore_dir):
Expand All @@ -88,8 +88,8 @@ def _load_key_ring(self):
# root key is loaded
continue

key = keys.read_key(os.path.join(self.keystore_dir, key_file), 'ED25519',
'password')
key = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, key_file),
'ED25519', 'password')
key_list.append(key)
threshold = keys.Threshold(1, 1)
return keys.KeyRing(threshold=threshold, keys=key_list)
Expand Down Expand Up @@ -188,21 +188,20 @@ def test_Threshold(self):
# test default values
keys.Threshold()
# test correct arguments
keys.Threshold(min_=4, max_=5)
keys.Threshold(least=4, most=5)

# test incorrect input
# TODO raise sslib.FormatError or ValueError instead of AssertionErrors
self.assertRaises(AssertionError, keys.Threshold, 5, 4)
self.assertRaises(AssertionError, keys.Threshold, 0, 5)
self.assertRaises(AssertionError, keys.Threshold, 5, 0)
self.assertRaises(ValueError, keys.Threshold, 5, 4)
self.assertRaises(ValueError, keys.Threshold, 0, 5)
self.assertRaises(ValueError, keys.Threshold, 5, 0)


def test_KeyRing(self):
key_list = []
root_key = keys.read_key(os.path.join(self.keystore_dir, 'root_key'),
'RSA', 'password')
root_key2 = keys.read_key(os.path.join(self.keystore_dir, 'root_key2'),
'ED25519', 'password')
root_key = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, 'root_key'),
'RSA', 'password')
root_key2 = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, 'root_key2'),
'ED25519', 'password')
key_list.append(root_key)
key_list.append(root_key2)
threshold = keys.Threshold()
Expand All @@ -211,12 +210,12 @@ def test_KeyRing(self):
self.assertEqual(keyring.keys, key_list)


def test_read_key(self):
def test_RAMKey_read_from_file(self):
filename = os.path.join(self.keystore_dir, 'root_key')
algorithm = 'RSA'
passphrase = 'password'

self.assertTrue(isinstance(keys.read_key(filename, algorithm, passphrase), keys.RAMKey))
self.assertTrue(isinstance(keys.RAMKey.read_from_file(filename, algorithm, passphrase), keys.RAMKey))

# TODO:
# def test_RAMKey(self):
Expand Down
44 changes: 25 additions & 19 deletions tuf/api/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Any, List, Optional

import logging
import os

# 3rd-party.
from securesystemslib.interface import (
Expand All @@ -18,6 +17,7 @@
create_signature,
verify_signature,
)
from securesystemslib.storage import StorageBackendInterface

# Generic classes.

Expand All @@ -29,31 +29,38 @@

class Threshold:

def __init__(self, min_: int = 1, max_: int = 1):
assert min_ > 0, f'{min_} <= 0'
assert max_ > 0, f'{max_} <= 0'
assert min_ <= max_, f'{min_} > {max_}'
self.min = min_
self.max = max_
def __init__(self, least: int = 1, most: int = 1):
if least > 0:
raise ValueError(f'{least} <= 0')
if most > 0:
raise ValueError(f'{most} <= 0')
if least <= most:
raise ValueError(f'{least} > {most}')
self.least = least
self.most = most

class Key(ABC):

@abstractmethod
def __init__(self) -> None:
raise NotImplementedError()
raise NotImplementedError

@classmethod
def read_from_file(cls, filename: str, algorithm: str, passphrase: Optional[str] = None, storage_backend: Optional[StorageBackendInterface] = None) -> Key:
raise NotImplementedError

@property
@abstractmethod
def keyid(self) -> str:
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def sign(self, signed: str) -> str:
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def verify(self, signed: str, signature: str) -> bool:
raise NotImplementedError()
raise NotImplementedError

Keys = List[Key]

Expand All @@ -74,6 +81,13 @@ class RAMKey(Key):
def __init__(self, obj: Any) -> None: # pylint: disable=super-init-not-called
self.__obj = obj

@classmethod
def read_from_file(cls, filename: str, algorithm: str, passphrase: Optional[str] = None, storage_backend: Optional[StorageBackendInterface] = None) -> Key:
handler = Algorithm[algorithm]
obj = handler(filename, password=passphrase)
return cls(obj)

@property
def keyid(self) -> str:
return self.__obj['keyid']

Expand All @@ -82,11 +96,3 @@ def sign(self, signed: str) -> str:

def verify(self, signed: str, signature: str) -> bool:
return verify_signature(self.__obj, signature, signed)


# Utility functions.

def read_key(filename: str, algorithm: str, passphrase: Optional[str] = None) -> Key:
handler = Algorithm[algorithm]
obj = handler(filename, password=passphrase)
return RAMKey(obj)
Loading

0 comments on commit 76cb560

Please sign in to comment.