From 5c7c06246f5a78c3dcde13eb59d4436057825ca1 Mon Sep 17 00:00:00 2001 From: spisarski Date: Wed, 26 Jul 2017 08:50:27 -0600 Subject: Added configuration for keypair size. Removed hardcoded key byte value of 1024 and added the abiltiy for the user to define their key size. JIRA: SNAPS-20 Change-Id: I81654420e558aae8a03bed902bb8be8f025b992c Signed-off-by: spisarski --- docs/how-to-use/IntegrationTests.rst | 5 ++- snaps/openstack/create_keypairs.py | 15 ++++++--- snaps/openstack/tests/create_keypairs_tests.py | 44 +++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/docs/how-to-use/IntegrationTests.rst b/docs/how-to-use/IntegrationTests.rst index 70e51b9..7414110 100644 --- a/docs/how-to-use/IntegrationTests.rst +++ b/docs/how-to-use/IntegrationTests.rst @@ -115,6 +115,9 @@ create_keypairs_tests.py - CreateKeypairsTests | test_create_keypair_only | 2 | Ensures that a keypair object can be created simply by | | | | only configuring a name | +---------------------------------------+---------------+-----------------------------------------------------------+ +| test_create_keypair_large_key | 2 | Ensures that a keypair object can be created with a large | +| | | key of 10000 bytes | ++---------------------------------------+---------------+-----------------------------------------------------------+ | test_create_delete_keypair | 2 | Ensures that a keypair object is deleted via | | | | OpenStackKeypair.clean() and subsequent calls do not | | | | result in exceptions | @@ -243,7 +246,7 @@ create_stack_tests.py - CreateStackSuccessTests +---------------------------------------+---------------+-----------------------------------------------------------+ create_stack_tests.py - CreateStackNegativeTests --------------------------------------------------- +------------------------------------------------ +----------------------------------------+---------------+-----------------------------------------------------------+ | Test Name | Neutron API | Description | diff --git a/snaps/openstack/create_keypairs.py b/snaps/openstack/create_keypairs.py index 16374ad..c81fef5 100644 --- a/snaps/openstack/create_keypairs.py +++ b/snaps/openstack/create_keypairs.py @@ -68,13 +68,13 @@ class OpenStackKeypair: self.keypair_settings.public_filepath) if self.keypair_settings.delete_on_clean is not None: - self.__delete_keys_on_clean = self.keypair_settings.delete_on_clean + delete_on_clean = self.keypair_settings.delete_on_clean + self.__delete_keys_on_clean = delete_on_clean else: self.__delete_keys_on_clean = False else: logger.info("Creating new keypair") - # TODO - Make this value configurable - keys = nova_utils.create_keys(1024) + keys = nova_utils.create_keys(self.keypair_settings.key_size) self.__keypair = nova_utils.upload_keypair( self.__nova, self.keypair_settings.name, nova_utils.public_key_openssh(keys)) @@ -83,7 +83,8 @@ class OpenStackKeypair: self.keypair_settings.private_filepath) if self.keypair_settings.delete_on_clean is not None: - self.__delete_keys_on_clean = self.keypair_settings.delete_on_clean + delete_on_clean = self.keypair_settings.delete_on_clean + self.__delete_keys_on_clean = delete_on_clean else: self.__delete_keys_on_clean = True elif self.__keypair and not os.path.isfile( @@ -137,6 +138,8 @@ class KeypairSettings: public key file is or will be stored :param private_filepath: The path where the generated private key file will be stored + :param key_size: The number of bytes for the key size when it needs to + be generated (Must be >=512 default 1024) :param delete_on_clean: when True, the key files will be deleted when OpenStackKeypair#clean() is called :return: @@ -145,6 +148,7 @@ class KeypairSettings: self.name = kwargs.get('name') self.public_filepath = kwargs.get('public_filepath') self.private_filepath = kwargs.get('private_filepath') + self.key_size = int(kwargs.get('key_size', 1024)) if kwargs.get('delete_on_clean') is not None: if isinstance(kwargs.get('delete_on_clean'), bool): @@ -157,6 +161,9 @@ class KeypairSettings: if not self.name: raise KeypairSettingsError('Name is a required attribute') + if self.key_size < 512: + raise KeypairSettingsError('key_size must be >=512') + class KeypairSettingsError(Exception): """ diff --git a/snaps/openstack/tests/create_keypairs_tests.py b/snaps/openstack/tests/create_keypairs_tests.py index 2824c34..0b35095 100644 --- a/snaps/openstack/tests/create_keypairs_tests.py +++ b/snaps/openstack/tests/create_keypairs_tests.py @@ -39,9 +39,14 @@ class KeypairSettingsUnitTests(unittest.TestCase): with self.assertRaises(KeypairSettingsError): KeypairSettings(**dict()) + def test_small_key_size(self): + with self.assertRaises(KeypairSettingsError): + KeypairSettings(name='foo', key_size=511) + def test_name_only(self): settings = KeypairSettings(name='foo') self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertIsNone(settings.public_filepath) self.assertIsNone(settings.private_filepath) self.assertIsNone(settings.delete_on_clean) @@ -49,6 +54,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): def test_config_with_name_only(self): settings = KeypairSettings(**{'name': 'foo'}) self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertIsNone(settings.public_filepath) self.assertIsNone(settings.private_filepath) self.assertIsNone(settings.delete_on_clean) @@ -56,6 +62,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): def test_name_pub_only(self): settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub') self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertIsNone(settings.private_filepath) self.assertIsNone(settings.delete_on_clean) @@ -64,6 +71,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): settings = KeypairSettings( **{'name': 'foo', 'public_filepath': '/foo/bar.pub'}) self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertIsNone(settings.private_filepath) self.assertIsNone(settings.delete_on_clean) @@ -71,6 +79,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): def test_name_priv_only(self): settings = KeypairSettings(name='foo', private_filepath='/foo/bar') self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertIsNone(settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertIsNone(settings.delete_on_clean) @@ -79,6 +88,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): settings = KeypairSettings( **{'name': 'foo', 'private_filepath': '/foo/bar'}) self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertIsNone(settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertIsNone(settings.delete_on_clean) @@ -86,8 +96,10 @@ class KeypairSettingsUnitTests(unittest.TestCase): def test_all_delete_bool(self): settings = KeypairSettings( name='foo', public_filepath='/foo/bar.pub', - private_filepath='/foo/bar', delete_on_clean=True) + private_filepath='/foo/bar', delete_on_clean=True, + key_size=999) self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertTrue(settings.delete_on_clean) @@ -97,6 +109,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): name='foo', public_filepath='/foo/bar.pub', private_filepath='/foo/bar', delete_on_clean='True') self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertTrue(settings.delete_on_clean) @@ -106,6 +119,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): name='foo', public_filepath='/foo/bar.pub', private_filepath='/foo/bar', delete_on_clean='true') self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertTrue(settings.delete_on_clean) @@ -115,6 +129,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): name='foo', public_filepath='/foo/bar.pub', private_filepath='/foo/bar', delete_on_clean='False') self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertFalse(settings.delete_on_clean) @@ -122,8 +137,10 @@ class KeypairSettingsUnitTests(unittest.TestCase): def test_all_delete_str_false_lc(self): settings = KeypairSettings( name='foo', public_filepath='/foo/bar.pub', - private_filepath='/foo/bar', delete_on_clean='false') + private_filepath='/foo/bar', delete_on_clean='false', + key_size='999') self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertFalse(settings.delete_on_clean) @@ -131,8 +148,10 @@ class KeypairSettingsUnitTests(unittest.TestCase): def test_config_all_delete_false_bool(self): settings = KeypairSettings( **{'name': 'foo', 'public_filepath': '/foo/bar.pub', - 'private_filepath': '/foo/bar', 'delete_on_clean': False}) + 'private_filepath': '/foo/bar', 'delete_on_clean': False, + 'key_size': 999}) self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertFalse(settings.delete_on_clean) @@ -142,6 +161,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): **{'name': 'foo', 'public_filepath': '/foo/bar.pub', 'private_filepath': '/foo/bar', 'delete_on_clean': 'False'}) self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertFalse(settings.delete_on_clean) @@ -151,6 +171,7 @@ class KeypairSettingsUnitTests(unittest.TestCase): **{'name': 'foo', 'public_filepath': '/foo/bar.pub', 'private_filepath': '/foo/bar', 'delete_on_clean': 'false'}) self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertFalse(settings.delete_on_clean) @@ -158,8 +179,10 @@ class KeypairSettingsUnitTests(unittest.TestCase): def test_config_all_delete_false_str_foo(self): settings = KeypairSettings( **{'name': 'foo', 'public_filepath': '/foo/bar.pub', - 'private_filepath': '/foo/bar', 'delete_on_clean': 'foo'}) + 'private_filepath': '/foo/bar', 'delete_on_clean': 'foo', + 'key_size': '999'}) self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) self.assertEqual('/foo/bar.pub', settings.public_filepath) self.assertEqual('/foo/bar', settings.private_filepath) self.assertFalse(settings.delete_on_clean) @@ -213,6 +236,19 @@ class CreateKeypairsTests(OSIntegrationTestCase): self.keypair_creator.get_keypair()) self.assertEqual(self.keypair_creator.get_keypair(), keypair) + def test_create_keypair_large_key(self): + """ + Tests the creation of a generated keypair without saving to file + :return: + """ + self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings( + name=self.keypair_name, key_size=10000)) + self.keypair_creator.create() + + keypair = nova_utils.keypair_exists(self.nova, + self.keypair_creator.get_keypair()) + self.assertEqual(self.keypair_creator.get_keypair(), keypair) + def test_create_delete_keypair(self): """ Tests the creation then deletion of an OpenStack keypair to ensure -- cgit 1.2.3-korg