Fix typo and add name to not_implemented and mock key_manager
Fixes a typo, and adds the name parameter to the functions in the not_imlemented_key_manager and mock_key_manager. Change-Id: Ic0954325750782e830f597969278ce1c257263b1
This commit is contained in:
parent
1a54b468bf
commit
e80a6010ee
@ -29,10 +29,11 @@ class NotImplementedKeyManager(key_manager.KeyManager):
|
||||
super(NotImplementedKeyManager, self).__init__(configuration)
|
||||
|
||||
def create_key(self, context, algorithm='AES', length=256,
|
||||
expiration=None, **kwargs):
|
||||
expiration=None, name=None, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
def create_key_pair(self, context, algorithm, lengthm, expiration=None):
|
||||
def create_key_pair(self, context, algorithm, length,
|
||||
expiration=None, name=None):
|
||||
raise NotImplementedError()
|
||||
|
||||
def store(self, context, managed_object, expiration=None, **kwargs):
|
||||
|
@ -64,12 +64,14 @@ class MockKeyManager(key_manager.KeyManager):
|
||||
return hex_encoded
|
||||
|
||||
def _generate_key(self, **kwargs):
|
||||
name = kwargs.get('name', None)
|
||||
key_length = kwargs.get('key_length', 256)
|
||||
_hex = self._generate_hex_key(key_length)
|
||||
return sym_key.SymmetricKey(
|
||||
'AES',
|
||||
key_length,
|
||||
bytes(binascii.unhexlify(_hex)))
|
||||
bytes(binascii.unhexlify(_hex)),
|
||||
name)
|
||||
|
||||
def create_key(self, context, **kwargs):
|
||||
"""Creates a symmetric key.
|
||||
@ -84,7 +86,7 @@ class MockKeyManager(key_manager.KeyManager):
|
||||
key = self._generate_key(**kwargs)
|
||||
return self.store(context, key)
|
||||
|
||||
def _generate_public_and_private_key(self, length):
|
||||
def _generate_public_and_private_key(self, length, name):
|
||||
crypto_private_key = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=length,
|
||||
@ -104,16 +106,19 @@ class MockKeyManager(key_manager.KeyManager):
|
||||
private_key = pri_key.PrivateKey(
|
||||
algorithm='RSA',
|
||||
bit_length=length,
|
||||
key=bytearray(private_der))
|
||||
key=bytearray(private_der),
|
||||
name=name)
|
||||
|
||||
public_key = pub_key.PublicKey(
|
||||
algorithm='RSA',
|
||||
bit_length=length,
|
||||
key=bytearray(public_der))
|
||||
key=bytearray(public_der),
|
||||
name=name)
|
||||
|
||||
return private_key, public_key
|
||||
|
||||
def create_key_pair(self, context, algorithm, length, expiration=None):
|
||||
def create_key_pair(self, context, algorithm, length,
|
||||
expiration=None, name=None):
|
||||
"""Creates an asymmetric key pair.
|
||||
|
||||
This implementation returns UUIDs for the created keys in the order:
|
||||
@ -134,7 +139,8 @@ class MockKeyManager(key_manager.KeyManager):
|
||||
length, valid_lengths)
|
||||
raise ValueError(msg)
|
||||
|
||||
private_key, public_key = self._generate_public_and_private_key(length)
|
||||
private_key, public_key = self._generate_public_and_private_key(length,
|
||||
name)
|
||||
|
||||
private_key_uuid = self.store(context, private_key)
|
||||
public_key_uuid = self.store(context, public_key)
|
||||
|
@ -66,14 +66,21 @@ class MockKeyManagerTestCase(test_key_mgr.KeyManagerTestCase):
|
||||
key = self.key_mgr.get(self.context, key_id)
|
||||
self.assertEqual(length / 8, len(key.get_encoded()))
|
||||
|
||||
def test_create_key_with_name(self):
|
||||
name = 'my key'
|
||||
key_id = self.key_mgr.create_key(self.context, name=name)
|
||||
key = self.key_mgr.get(self.context, key_id)
|
||||
self.assertEqual(name, key.name)
|
||||
|
||||
def test_create_key_null_context(self):
|
||||
self.assertRaises(exception.Forbidden,
|
||||
self.key_mgr.create_key, None)
|
||||
|
||||
def test_create_key_pair(self):
|
||||
for length in [2048, 3072, 4096]:
|
||||
name = str(length) + ' key'
|
||||
private_key_uuid, public_key_uuid = self.key_mgr.create_key_pair(
|
||||
self.context, 'RSA', length)
|
||||
self.context, 'RSA', length, name=name)
|
||||
|
||||
private_key = self.key_mgr.get(self.context, private_key_uuid)
|
||||
public_key = self.key_mgr.get(self.context, public_key_uuid)
|
||||
@ -81,6 +88,9 @@ class MockKeyManagerTestCase(test_key_mgr.KeyManagerTestCase):
|
||||
crypto_private_key = get_cryptography_private_key(private_key)
|
||||
crypto_public_key = get_cryptography_public_key(public_key)
|
||||
|
||||
self.assertEqual(name, private_key.name)
|
||||
self.assertEqual(name, public_key.name)
|
||||
|
||||
self.assertEqual(length, crypto_private_key.key_size)
|
||||
self.assertEqual(length, crypto_public_key.key_size)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user