Added keypair config for key file deletion. 13/38013/1
authorspisarski <s.pisarski@cablelabs.com>
Mon, 24 Jul 2017 21:15:51 +0000 (15:15 -0600)
committerspisarski <s.pisarski@cablelabs.com>
Mon, 24 Jul 2017 21:15:51 +0000 (15:15 -0600)
Added KeypairSetting attribute named 'delete_on_clean' which is used
to override the keypair creator's logic for deleting its associated
public/private key files.

JIRA: SNAPS-105

Change-Id: I337db99daa6bf7d4b42e8729a92a4baef5c73140
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
docs/how-to-use/IntegrationTests.rst
snaps/openstack/create_keypairs.py
snaps/openstack/tests/create_keypairs_tests.py
snaps/test_suite_builder.py

index ec549cf..1181776 100644 (file)
@@ -114,6 +114,29 @@ create_keypairs_tests.py - CreateKeypairsTests
 |                                       |               | existing public key file                                  |
 +---------------------------------------+---------------+-----------------------------------------------------------+
 
+create_keypairs_tests.py - CreateKeypairsCleanupTests
+-----------------------------------------------------
+
++---------------------------------------+---------------+-----------------------------------------------------------+
+| Test Name                             | Nova API      | Description                                               |
++=======================================+===============+===========================================================+
+| test_create_keypair_gen_files_delete_1| 2             | Ensures that new keypair files are deleted by default     |
+|                                       |               | by OpenStackKeypair#clean()                               |
++---------------------------------------+---------------+-----------------------------------------------------------+
+| test_create_keypair_gen_files_delete_2| 2             | Ensures that new keypair files are deleted by         |
+|                                       |               | OpenStackKeypair#clean() when the settings delete_on_clean|
+|                                       |               | attribute is set to True                                  |
++---------------------------------------+---------------+-----------------------------------------------------------+
+| test_create_keypair_gen_files_keep    | 2             | Ensures that new keypair files are not deleted by         |
+|                                       |               | OpenStackKeypair#clean()                                  |
++---------------------------------------+---------------+-----------------------------------------------------------+
+| test_create_keypair_exist_files_keep  | 2             | Ensures that existing keypair files are not deleted by    |
+|                                       |               | OpenStackKeypair#clean()                                  |
++---------------------------------------+---------------+-----------------------------------------------------------+
+| test_create_keypair_exist_files_delete| 2             | Ensures that existing keypair files are deleted by        |
+|                                       |               | OpenStackKeypair#clean()                                  |
++---------------------------------------+---------------+-----------------------------------------------------------+
+
 create_network_tests.py - CreateNetworkSuccessTests
 ---------------------------------------------------
 
index 03ff7ec..16374ad 100644 (file)
 import logging
 
 import os
+from neutronclient.common.utils import str2bool
 from novaclient.exceptions import NotFound
+
+from snaps import file_utils
 from snaps.openstack.utils import nova_utils
 
 __author__ = 'spisarski'
@@ -63,7 +66,11 @@ class OpenStackKeypair:
                 self.__keypair = nova_utils.upload_keypair_file(
                     self.__nova, self.keypair_settings.name,
                     self.keypair_settings.public_filepath)
-                self.__delete_keys_on_clean = False
+
+                if self.keypair_settings.delete_on_clean is not None:
+                    self.__delete_keys_on_clean = self.keypair_settings.delete_on_clean
+                else:
+                    self.__delete_keys_on_clean = False
             else:
                 logger.info("Creating new keypair")
                 # TODO - Make this value configurable
@@ -74,7 +81,11 @@ class OpenStackKeypair:
                 nova_utils.save_keys_to_files(
                     keys, self.keypair_settings.public_filepath,
                     self.keypair_settings.private_filepath)
-                self.__delete_keys_on_clean = True
+
+                if self.keypair_settings.delete_on_clean is not None:
+                    self.__delete_keys_on_clean = self.keypair_settings.delete_on_clean
+                else:
+                    self.__delete_keys_on_clean = True
         elif self.__keypair and not os.path.isfile(
                 self.keypair_settings.private_filepath):
             logger.warn("The public key already exist in OpenStack \
@@ -94,10 +105,14 @@ class OpenStackKeypair:
             self.__keypair = None
 
         if self.__delete_keys_on_clean:
-            if self.keypair_settings.public_filepath:
+            if (self.keypair_settings.public_filepath and
+                    file_utils.file_exists(
+                        self.keypair_settings.public_filepath)):
                 os.chmod(self.keypair_settings.public_filepath, 0o777)
                 os.remove(self.keypair_settings.public_filepath)
-            if self.keypair_settings.private_filepath:
+            if (self.keypair_settings.private_filepath and
+                    file_utils.file_exists(
+                        self.keypair_settings.private_filepath)):
                 os.chmod(self.keypair_settings.private_filepath, 0o777)
                 os.remove(self.keypair_settings.private_filepath)
 
@@ -122,6 +137,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 delete_on_clean: when True, the key files will be deleted when
+                                OpenStackKeypair#clean() is called
         :return:
         """
 
@@ -129,6 +146,14 @@ class KeypairSettings:
         self.public_filepath = kwargs.get('public_filepath')
         self.private_filepath = kwargs.get('private_filepath')
 
+        if kwargs.get('delete_on_clean') is not None:
+            if isinstance(kwargs.get('delete_on_clean'), bool):
+                self.delete_on_clean = kwargs.get('delete_on_clean')
+            else:
+                self.delete_on_clean = str2bool(kwargs.get('delete_on_clean'))
+        else:
+            self.delete_on_clean = None
+
         if not self.name:
             raise KeypairSettingsError('Name is a required attribute')
 
index aeeefaf..2824c34 100644 (file)
@@ -16,6 +16,8 @@ import unittest
 import uuid
 
 import os
+
+from snaps import file_utils
 from snaps.openstack.create_keypairs import (
     KeypairSettings, OpenStackKeypair, KeypairSettingsError)
 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
@@ -42,18 +44,21 @@ class KeypairSettingsUnitTests(unittest.TestCase):
         self.assertEqual('foo', settings.name)
         self.assertIsNone(settings.public_filepath)
         self.assertIsNone(settings.private_filepath)
+        self.assertIsNone(settings.delete_on_clean)
 
     def test_config_with_name_only(self):
         settings = KeypairSettings(**{'name': 'foo'})
         self.assertEqual('foo', settings.name)
         self.assertIsNone(settings.public_filepath)
         self.assertIsNone(settings.private_filepath)
+        self.assertIsNone(settings.delete_on_clean)
 
     def test_name_pub_only(self):
         settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub')
         self.assertEqual('foo', settings.name)
         self.assertEqual('/foo/bar.pub', settings.public_filepath)
         self.assertIsNone(settings.private_filepath)
+        self.assertIsNone(settings.delete_on_clean)
 
     def test_config_with_name_pub_only(self):
         settings = KeypairSettings(
@@ -61,12 +66,14 @@ class KeypairSettingsUnitTests(unittest.TestCase):
         self.assertEqual('foo', settings.name)
         self.assertEqual('/foo/bar.pub', settings.public_filepath)
         self.assertIsNone(settings.private_filepath)
+        self.assertIsNone(settings.delete_on_clean)
 
     def test_name_priv_only(self):
         settings = KeypairSettings(name='foo', private_filepath='/foo/bar')
         self.assertEqual('foo', settings.name)
         self.assertIsNone(settings.public_filepath)
         self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertIsNone(settings.delete_on_clean)
 
     def test_config_with_name_priv_only(self):
         settings = KeypairSettings(
@@ -74,21 +81,88 @@ class KeypairSettingsUnitTests(unittest.TestCase):
         self.assertEqual('foo', settings.name)
         self.assertIsNone(settings.public_filepath)
         self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertIsNone(settings.delete_on_clean)
+
+    def test_all_delete_bool(self):
+        settings = KeypairSettings(
+            name='foo', public_filepath='/foo/bar.pub',
+            private_filepath='/foo/bar', delete_on_clean=True)
+        self.assertEqual('foo', settings.name)
+        self.assertEqual('/foo/bar.pub', settings.public_filepath)
+        self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertTrue(settings.delete_on_clean)
+
+    def test_all_delete_str_true_cap(self):
+        settings = KeypairSettings(
+            name='foo', public_filepath='/foo/bar.pub',
+            private_filepath='/foo/bar', delete_on_clean='True')
+        self.assertEqual('foo', settings.name)
+        self.assertEqual('/foo/bar.pub', settings.public_filepath)
+        self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertTrue(settings.delete_on_clean)
+
+    def test_all_delete_str_true_lc(self):
+        settings = KeypairSettings(
+            name='foo', public_filepath='/foo/bar.pub',
+            private_filepath='/foo/bar', delete_on_clean='true')
+        self.assertEqual('foo', settings.name)
+        self.assertEqual('/foo/bar.pub', settings.public_filepath)
+        self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertTrue(settings.delete_on_clean)
+
+    def test_all_delete_str_false_cap(self):
+        settings = KeypairSettings(
+            name='foo', public_filepath='/foo/bar.pub',
+            private_filepath='/foo/bar', delete_on_clean='False')
+        self.assertEqual('foo', settings.name)
+        self.assertEqual('/foo/bar.pub', settings.public_filepath)
+        self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertFalse(settings.delete_on_clean)
+
+    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')
+        self.assertEqual('foo', settings.name)
+        self.assertEqual('/foo/bar.pub', settings.public_filepath)
+        self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertFalse(settings.delete_on_clean)
+
+    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})
+        self.assertEqual('foo', settings.name)
+        self.assertEqual('/foo/bar.pub', settings.public_filepath)
+        self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertFalse(settings.delete_on_clean)
 
-    def test_all(self):
-        settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub',
-                                   private_filepath='/foo/bar')
+    def test_config_all_delete_false_str_cap(self):
+        settings = KeypairSettings(
+            **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
+               'private_filepath': '/foo/bar', 'delete_on_clean': 'False'})
+        self.assertEqual('foo', settings.name)
+        self.assertEqual('/foo/bar.pub', settings.public_filepath)
+        self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertFalse(settings.delete_on_clean)
+
+    def test_config_all_delete_false_str_lc(self):
+        settings = KeypairSettings(
+            **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
+               'private_filepath': '/foo/bar', 'delete_on_clean': 'false'})
         self.assertEqual('foo', settings.name)
         self.assertEqual('/foo/bar.pub', settings.public_filepath)
         self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertFalse(settings.delete_on_clean)
 
-    def test_config_all(self):
+    def test_config_all_delete_false_str_foo(self):
         settings = KeypairSettings(
             **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
-               'private_filepath': '/foo/bar'})
+               'private_filepath': '/foo/bar', 'delete_on_clean': 'foo'})
         self.assertEqual('foo', settings.name)
         self.assertEqual('/foo/bar.pub', settings.public_filepath)
         self.assertEqual('/foo/bar', settings.private_filepath)
+        self.assertFalse(settings.delete_on_clean)
 
 
 class CreateKeypairsTests(OSIntegrationTestCase):
@@ -221,3 +295,128 @@ class CreateKeypairsTests(OSIntegrationTestCase):
         file_key = open(os.path.expanduser(self.pub_file_path)).read()
         self.assertEqual(self.keypair_creator.get_keypair().public_key,
                          file_key)
+
+
+class CreateKeypairsCleanupTests(OSIntegrationTestCase):
+    """
+    Tests for the OpenStackKeypair#clean method to ensure key files are deleted
+    when required
+    """
+
+    def setUp(self):
+        super(self.__class__, self).__start__()
+
+        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
+        self.priv_file_path = 'tmp/' + guid
+        self.pub_file_path = self.priv_file_path + '.pub'
+        self.nova = nova_utils.nova_client(self.os_creds)
+        self.keypair_name = guid
+
+        self.keypair_creator = None
+
+    def tearDown(self):
+        """
+        Cleanup of created keypair
+        """
+        if self.keypair_creator:
+            self.keypair_creator.clean()
+
+        try:
+            os.remove(self.pub_file_path)
+        except:
+            pass
+
+        try:
+            os.remove(self.priv_file_path)
+        except:
+            pass
+
+        super(self.__class__, self).__clean__()
+
+    def test_create_keypair_gen_files_delete_1(self):
+        """
+        Tests the creation of a generated keypair and ensures that the files
+        are deleted on clean()
+        :return:
+        """
+        self.keypair_creator = OpenStackKeypair(
+            self.os_creds, KeypairSettings(
+                name=self.keypair_name, public_filepath=self.pub_file_path,
+                private_filepath=self.priv_file_path))
+        self.keypair_creator.create()
+        self.keypair_creator.clean()
+
+        self.assertFalse(file_utils.file_exists(self.pub_file_path))
+        self.assertFalse(file_utils.file_exists(self.priv_file_path))
+
+    def test_create_keypair_gen_files_delete_2(self):
+        """
+        Tests the creation of a generated keypair and ensures that the files
+        are deleted on clean()
+        :return:
+        """
+        self.keypair_creator = OpenStackKeypair(
+            self.os_creds, KeypairSettings(
+                name=self.keypair_name, public_filepath=self.pub_file_path,
+                private_filepath=self.priv_file_path, delete_on_clean=True))
+        self.keypair_creator.create()
+        self.keypair_creator.clean()
+
+        self.assertFalse(file_utils.file_exists(self.pub_file_path))
+        self.assertFalse(file_utils.file_exists(self.priv_file_path))
+
+    def test_create_keypair_gen_files_keep(self):
+        """
+        Tests the creation of a generated keypair and ensures that the files
+        are not deleted on clean()
+        :return:
+        """
+        self.keypair_creator = OpenStackKeypair(
+            self.os_creds, KeypairSettings(
+                name=self.keypair_name, public_filepath=self.pub_file_path,
+                private_filepath=self.priv_file_path, delete_on_clean=False))
+        self.keypair_creator.create()
+        self.keypair_creator.clean()
+
+        self.assertTrue(file_utils.file_exists(self.pub_file_path))
+        self.assertTrue(file_utils.file_exists(self.priv_file_path))
+
+    def test_create_keypair_exist_files_keep(self):
+        """
+        Tests the creation of an existing public keypair and ensures the files
+        are not deleted on clean
+        :return:
+        """
+        keys = nova_utils.create_keys()
+        nova_utils.save_keys_to_files(
+            keys=keys, pub_file_path=self.pub_file_path,
+            priv_file_path=self.priv_file_path)
+        self.keypair_creator = OpenStackKeypair(
+            self.os_creds, KeypairSettings(
+                name=self.keypair_name, public_filepath=self.pub_file_path,
+                private_filepath=self.priv_file_path, delete_on_clean=False))
+        self.keypair_creator.create()
+        self.keypair_creator.clean()
+
+        self.assertTrue(file_utils.file_exists(self.pub_file_path))
+        self.assertTrue(file_utils.file_exists(self.priv_file_path))
+
+    def test_create_keypair_exist_files_delete(self):
+        """
+        Tests the creation of an existing public keypair and ensures the files
+        are deleted on clean
+        :return:
+        """
+        keys = nova_utils.create_keys()
+        nova_utils.save_keys_to_files(
+            keys=keys, pub_file_path=self.pub_file_path,
+            priv_file_path=self.priv_file_path)
+        self.keypair_creator = OpenStackKeypair(
+            self.os_creds, KeypairSettings(
+                name=self.keypair_name, public_filepath=self.pub_file_path,
+                private_filepath=self.priv_file_path, delete_on_clean=True))
+        self.keypair_creator.create()
+        self.keypair_creator.clean()
+
+        self.assertFalse(file_utils.file_exists(self.pub_file_path))
+        self.assertFalse(file_utils.file_exists(self.priv_file_path))
index e40aaa9..24c3e65 100644 (file)
@@ -44,7 +44,7 @@ from snaps.openstack.tests.create_instance_tests import (
     SimpleHealthCheck, CreateInstanceFromThreePartImage,
     CreateInstanceMockOfflineTests)
 from snaps.openstack.tests.create_keypairs_tests import (
-    CreateKeypairsTests, KeypairSettingsUnitTests)
+    CreateKeypairsTests, KeypairSettingsUnitTests, CreateKeypairsCleanupTests)
 from snaps.openstack.tests.create_network_tests import (
     CreateNetworkSuccessTests, NetworkSettingsUnitTests, PortSettingsUnitTests,
     SubnetSettingsUnitTests, CreateNetworkTypeTests)
@@ -323,6 +323,11 @@ def add_openstack_integration_tests(suite, os_creds, ext_net_name,
         use_keystone=use_keystone,
         flavor_metadata=flavor_metadata, image_metadata=image_metadata,
         log_level=log_level))
+    suite.addTest(OSIntegrationTestCase.parameterize(
+        CreateKeypairsCleanupTests, os_creds=os_creds, ext_net_name=ext_net_name,
+        use_keystone=use_keystone,
+        flavor_metadata=flavor_metadata, image_metadata=image_metadata,
+        log_level=log_level))
     suite.addTest(OSIntegrationTestCase.parameterize(
         CreateNetworkSuccessTests, os_creds=os_creds,
         ext_net_name=ext_net_name, use_keystone=use_keystone,