Skip to content

Commit 76cb560

Browse files
minor edits
* 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]>
1 parent 4237287 commit 76cb560

File tree

3 files changed

+93
-84
lines changed

3 files changed

+93
-84
lines changed

tests/test_tuf_api.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def tearDownClass(cls):
7575

7676
def _load_key_ring(self):
7777
key_list = []
78-
root_key = keys.read_key(os.path.join(self.keystore_dir, 'root_key'),
79-
'RSA', 'password')
78+
root_key = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, 'root_key'),
79+
'RSA', 'password')
8080
key_list.append(root_key)
8181

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

91-
key = keys.read_key(os.path.join(self.keystore_dir, key_file), 'ED25519',
92-
'password')
91+
key = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, key_file),
92+
'ED25519', 'password')
9393
key_list.append(key)
9494
threshold = keys.Threshold(1, 1)
9595
return keys.KeyRing(threshold=threshold, keys=key_list)
@@ -188,21 +188,20 @@ def test_Threshold(self):
188188
# test default values
189189
keys.Threshold()
190190
# test correct arguments
191-
keys.Threshold(min_=4, max_=5)
191+
keys.Threshold(least=4, most=5)
192192

193193
# test incorrect input
194-
# TODO raise sslib.FormatError or ValueError instead of AssertionErrors
195-
self.assertRaises(AssertionError, keys.Threshold, 5, 4)
196-
self.assertRaises(AssertionError, keys.Threshold, 0, 5)
197-
self.assertRaises(AssertionError, keys.Threshold, 5, 0)
194+
self.assertRaises(ValueError, keys.Threshold, 5, 4)
195+
self.assertRaises(ValueError, keys.Threshold, 0, 5)
196+
self.assertRaises(ValueError, keys.Threshold, 5, 0)
198197

199198

200199
def test_KeyRing(self):
201200
key_list = []
202-
root_key = keys.read_key(os.path.join(self.keystore_dir, 'root_key'),
203-
'RSA', 'password')
204-
root_key2 = keys.read_key(os.path.join(self.keystore_dir, 'root_key2'),
205-
'ED25519', 'password')
201+
root_key = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, 'root_key'),
202+
'RSA', 'password')
203+
root_key2 = keys.RAMKey.read_from_file(os.path.join(self.keystore_dir, 'root_key2'),
204+
'ED25519', 'password')
206205
key_list.append(root_key)
207206
key_list.append(root_key2)
208207
threshold = keys.Threshold()
@@ -211,12 +210,12 @@ def test_KeyRing(self):
211210
self.assertEqual(keyring.keys, key_list)
212211

213212

214-
def test_read_key(self):
213+
def test_RAMKey_read_from_file(self):
215214
filename = os.path.join(self.keystore_dir, 'root_key')
216215
algorithm = 'RSA'
217216
passphrase = 'password'
218217

219-
self.assertTrue(isinstance(keys.read_key(filename, algorithm, passphrase), keys.RAMKey))
218+
self.assertTrue(isinstance(keys.RAMKey.read_from_file(filename, algorithm, passphrase), keys.RAMKey))
220219

221220
# TODO:
222221
# def test_RAMKey(self):

tuf/api/keys.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Any, List, Optional
77

88
import logging
9-
import os
109

1110
# 3rd-party.
1211
from securesystemslib.interface import (
@@ -18,6 +17,7 @@
1817
create_signature,
1918
verify_signature,
2019
)
20+
from securesystemslib.storage import StorageBackendInterface
2121

2222
# Generic classes.
2323

@@ -29,31 +29,38 @@
2929

3030
class Threshold:
3131

32-
def __init__(self, min_: int = 1, max_: int = 1):
33-
assert min_ > 0, f'{min_} <= 0'
34-
assert max_ > 0, f'{max_} <= 0'
35-
assert min_ <= max_, f'{min_} > {max_}'
36-
self.min = min_
37-
self.max = max_
32+
def __init__(self, least: int = 1, most: int = 1):
33+
if least > 0:
34+
raise ValueError(f'{least} <= 0')
35+
if most > 0:
36+
raise ValueError(f'{most} <= 0')
37+
if least <= most:
38+
raise ValueError(f'{least} > {most}')
39+
self.least = least
40+
self.most = most
3841

3942
class Key(ABC):
4043

4144
@abstractmethod
4245
def __init__(self) -> None:
43-
raise NotImplementedError()
46+
raise NotImplementedError
47+
48+
@classmethod
49+
def read_from_file(cls, filename: str, algorithm: str, passphrase: Optional[str] = None, storage_backend: Optional[StorageBackendInterface] = None) -> Key:
50+
raise NotImplementedError
4451

4552
@property
4653
@abstractmethod
4754
def keyid(self) -> str:
48-
raise NotImplementedError()
55+
raise NotImplementedError
4956

5057
@abstractmethod
5158
def sign(self, signed: str) -> str:
52-
raise NotImplementedError()
59+
raise NotImplementedError
5360

5461
@abstractmethod
5562
def verify(self, signed: str, signature: str) -> bool:
56-
raise NotImplementedError()
63+
raise NotImplementedError
5764

5865
Keys = List[Key]
5966

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

84+
@classmethod
85+
def read_from_file(cls, filename: str, algorithm: str, passphrase: Optional[str] = None, storage_backend: Optional[StorageBackendInterface] = None) -> Key:
86+
handler = Algorithm[algorithm]
87+
obj = handler(filename, password=passphrase)
88+
return cls(obj)
89+
90+
@property
7791
def keyid(self) -> str:
7892
return self.__obj['keyid']
7993

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

8397
def verify(self, signed: str, signature: str) -> bool:
8498
return verify_signature(self.__obj, signature, signed)
85-
86-
87-
# Utility functions.
88-
89-
def read_key(filename: str, algorithm: str, passphrase: Optional[str] = None) -> Key:
90-
handler = Algorithm[algorithm]
91-
obj = handler(filename, password=passphrase)
92-
return RAMKey(obj)

0 commit comments

Comments
 (0)