def test_create_keypair_from_file(self): """ Tests the creation of an existing public keypair from a file :return: """ keys = nova_utils.create_keys() file_utils.save_keys_to_files(keys=keys, pub_file_path=self.pub_file_path) self.keypair_creator = OpenStackKeypair( self.os_creds, KeypairConfig(name=self.keypair_name, public_filepath=self.pub_file_path)) self.keypair_creator.create() keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair()) self.assertEqual(self.keypair_creator.get_keypair(), keypair) pub_file = None try: pub_file = open(os.path.expanduser(self.pub_file_path)) file_key = pub_file.read() self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key) finally: if pub_file: pub_file.close() self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key)
def setUp(self): """ Instantiates the CreateImage object that is responsible for downloading and creating an OS image file within OpenStack """ guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.priv_key_file_path = 'tmp/' + guid self.pub_key_file_path = self.priv_key_file_path + '.pub' self.nova = nova_utils.nova_client(self.os_creds, self.os_session) self.keys = nova_utils.create_keys() self.public_key = nova_utils.public_key_openssh(self.keys) self.keypair_name = guid self.keypair = None
def create(self): """ Responsible for creating the keypair object. :return: The Keypair domain object or None """ self.initialize() if not self.__keypair: logger.info('Creating keypair %s...' % self.keypair_settings.name) if self.keypair_settings.public_filepath and os.path.isfile( self.keypair_settings.public_filepath): logger.info("Uploading existing keypair") self.__keypair = nova_utils.upload_keypair_file( self._nova, self.keypair_settings.name, self.keypair_settings.public_filepath) if self.keypair_settings.delete_on_clean is not None: 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") 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)) file_utils.save_keys_to_files( keys, self.keypair_settings.public_filepath, self.keypair_settings.private_filepath) if self.keypair_settings.delete_on_clean is not None: 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( self.keypair_settings.private_filepath): logger.warn("The public key already exist in OpenStack \ but the private key file is not found ..") return self.__keypair
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() file_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, KeypairConfig(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))