Esempio n. 1
0
    def _xvp_encrypt(self, password, is_pool_password=False):
        """Call xvp to obfuscate passwords for config file.

        Args:
            - password: the password to encode, max 8 char for vm passwords,
                        and 16 chars for pool passwords. passwords will
                        be trimmed to max len before encoding.
            - is_pool_password: True if this this is the XenServer api password
                                False if it's a VM console password
              (xvp uses different keys and max lengths for pool passwords)

        Note that xvp's obfuscation should not be considered 'real' encryption.
        It simply DES encrypts the passwords with static keys plainly viewable
        in the xvp source code.

        """
        maxlen = 8
        flag = '-e'
        if is_pool_password:
            maxlen = 16
            flag = '-x'
        #xvp will blow up on passwords that are too long (mdragon)
        password = password[:maxlen]
        out, err = utils.execute('xvp', flag, process_input=password)
        if err:
            raise processutils.ProcessExecutionError(_("Failed to run xvp."))
        return out.strip()
Esempio n. 2
0
    def test_resize2fs_e2fsck_fails(self):
        imgfile = tempfile.NamedTemporaryFile()

        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('e2fsck',
                      '-fp',
                      imgfile,
                      check_exit_code=[0, 1, 2],
                      run_as_root=False).AndRaise(
                          processutils.ProcessExecutionError("fs error"))
        self.mox.ReplayAll()
        api.resize2fs(imgfile)
Esempio n. 3
0
 def test_spawn_create_lpar_fail(self):
     self.flags(powervm_img_local_path='/images/')
     self.stubs.Set(images, 'fetch', lambda *x, **y: None)
     self.stubs.Set(
         self.powervm_connection._powervm, 'get_host_stats',
         lambda *x, **y: raise_(
             (processutils.ProcessExecutionError('instance_name'))))
     fake_net_info = network_model.NetworkInfo(
         [fake_network_cache_model.new_vif()])
     self.assertRaises(exception.PowerVMLPARCreationFailed,
                       self.powervm_connection.spawn,
                       context.get_admin_context(), self.instance,
                       {'id': 'ANY_ID'}, [], 's3cr3t', fake_net_info)
Esempio n. 4
0
 def test_libvirt_nfs_driver_umount_error(self, mock_LOG_exception,
                                          mock_LOG_debug, mock_utils_exe):
     export_string = '192.168.1.1:/nfs/share1'
     connection_info = {
         'data': {
             'export': export_string,
             'name': self.name
         }
     }
     libvirt_driver = volume.LibvirtNFSVolumeDriver(self.fake_conn)
     mock_utils_exe.side_effect = processutils.ProcessExecutionError(
         None, None, None, 'umount', 'umount: device is busy.')
     libvirt_driver.disconnect_volume(connection_info, "vde")
     self.assertTrue(mock_LOG_debug.called)
     mock_utils_exe.side_effect = processutils.ProcessExecutionError(
         None, None, None, 'umount', 'umount: target is busy.')
     libvirt_driver.disconnect_volume(connection_info, "vde")
     self.assertTrue(mock_LOG_debug.called)
     mock_utils_exe.side_effect = processutils.ProcessExecutionError(
         None, None, None, 'umount', 'umount: Other error.')
     libvirt_driver.disconnect_volume(connection_info, "vde")
     self.assertTrue(mock_LOG_exception.called)
Esempio n. 5
0
def ssh_command_as_root(ssh_connection, cmd, check_exit_code=True):
    """Method to execute remote command as root.

    :param connection: an active paramiko.SSHClient connection.
    :param command: string containing the command to run.
    :returns: Tuple -- a tuple of (stdout, stderr)
    :raises: processutils.ProcessExecutionError
    """
    LOG.debug(_('Running cmd (SSH-as-root): %s') % cmd)
    chan = ssh_connection._transport.open_session()
    # This command is required to be executed
    # in order to become root.
    chan.exec_command('ioscli oem_setup_env')
    bufsize = -1
    stdin = chan.makefile('wb', bufsize)
    stdout = chan.makefile('rb', bufsize)
    stderr = chan.makefile_stderr('rb', bufsize)
    # We run the command and then call 'exit' to exit from
    # super user environment.
    stdin.write('%s\n%s\n' % (cmd, 'exit'))
    stdin.flush()
    exit_status = chan.recv_exit_status()

    # Lets handle the error just like processutils.ssh_execute does.
    if exit_status != -1:
        LOG.debug(_('Result was %s') % exit_status)
        if check_exit_code and exit_status != 0:
            # TODO(mikal): I know this is weird, but it needs to be consistent
            # with processutils.execute. I will move this method to oslo in
            # a later commit.
            raise processutils.ProcessExecutionError(exit_code=exit_status,
                                                     stdout=stdout,
                                                     stderr=stderr,
                                                     cmd=''.join(cmd))

    return (stdout, stderr)
Esempio n. 6
0
class LvmTestCase(test.NoDBTestCase):
    def test_get_volume_size(self):
        executes = []

        def fake_execute(*cmd, **kwargs):
            executes.append(cmd)
            return 123456789, None

        expected_commands = [('blockdev', '--getsize64', '/dev/foo')]
        self.stubs.Set(utils, 'execute', fake_execute)
        size = lvm.get_volume_size('/dev/foo')
        self.assertEqual(expected_commands, executes)
        self.assertEqual(size, 123456789)

    @mock.patch.object(utils,
                       'execute',
                       side_effect=processutils.ProcessExecutionError(
                           stderr=('blockdev: cannot open /dev/foo: '
                                   'No such device or address')))
    def test_get_volume_size_not_found(self, mock_execute):
        self.assertRaises(exception.VolumeBDMPathNotFound, lvm.get_volume_size,
                          '/dev/foo')

    @mock.patch.object(utils,
                       'execute',
                       side_effect=processutils.ProcessExecutionError(
                           stderr='blockdev: i am sad in other ways'))
    def test_get_volume_size_unexpectd_error(self, mock_execute):
        self.assertRaises(processutils.ProcessExecutionError,
                          lvm.get_volume_size, '/dev/foo')

    def test_lvm_clear(self):
        def fake_lvm_size(path):
            return lvm_size

        def fake_execute(*cmd, **kwargs):
            executes.append(cmd)

        self.stubs.Set(lvm, 'get_volume_size', fake_lvm_size)
        self.stubs.Set(utils, 'execute', fake_execute)

        # Test the correct dd commands are run for various sizes
        lvm_size = 1
        executes = []
        expected_commands = [('dd', 'bs=1', 'if=/dev/zero', 'of=/dev/v1',
                              'seek=0', 'count=1', 'conv=fdatasync')]
        lvm.clear_volume('/dev/v1')
        self.assertEqual(expected_commands, executes)

        lvm_size = 1024
        executes = []
        expected_commands = [('dd', 'bs=1024', 'if=/dev/zero', 'of=/dev/v2',
                              'seek=0', 'count=1', 'conv=fdatasync')]
        lvm.clear_volume('/dev/v2')
        self.assertEqual(expected_commands, executes)

        lvm_size = 1025
        executes = []
        expected_commands = [('dd', 'bs=1024', 'if=/dev/zero', 'of=/dev/v3',
                              'seek=0', 'count=1', 'conv=fdatasync')]
        expected_commands += [('dd', 'bs=1', 'if=/dev/zero', 'of=/dev/v3',
                               'seek=1024', 'count=1', 'conv=fdatasync')]
        lvm.clear_volume('/dev/v3')
        self.assertEqual(expected_commands, executes)

        lvm_size = 1048576
        executes = []
        expected_commands = [('dd', 'bs=1048576', 'if=/dev/zero', 'of=/dev/v4',
                              'seek=0', 'count=1', 'oflag=direct')]
        lvm.clear_volume('/dev/v4')
        self.assertEqual(expected_commands, executes)

        lvm_size = 1048577
        executes = []
        expected_commands = [('dd', 'bs=1048576', 'if=/dev/zero', 'of=/dev/v5',
                              'seek=0', 'count=1', 'oflag=direct')]
        expected_commands += [('dd', 'bs=1', 'if=/dev/zero', 'of=/dev/v5',
                               'seek=1048576', 'count=1', 'conv=fdatasync')]
        lvm.clear_volume('/dev/v5')
        self.assertEqual(expected_commands, executes)

        lvm_size = 1234567
        executes = []
        expected_commands = [('dd', 'bs=1048576', 'if=/dev/zero', 'of=/dev/v6',
                              'seek=0', 'count=1', 'oflag=direct')]
        expected_commands += [('dd', 'bs=1024', 'if=/dev/zero', 'of=/dev/v6',
                               'seek=1024', 'count=181', 'conv=fdatasync')]
        expected_commands += [('dd', 'bs=1', 'if=/dev/zero', 'of=/dev/v6',
                               'seek=1233920', 'count=647', 'conv=fdatasync')]
        lvm.clear_volume('/dev/v6')
        self.assertEqual(expected_commands, executes)

        # Test volume_clear_size limits the size
        lvm_size = 10485761
        CONF.set_override('volume_clear_size', '1', 'libvirt')
        executes = []
        expected_commands = [('dd', 'bs=1048576', 'if=/dev/zero', 'of=/dev/v7',
                              'seek=0', 'count=1', 'oflag=direct')]
        lvm.clear_volume('/dev/v7')
        self.assertEqual(expected_commands, executes)

        CONF.set_override('volume_clear_size', '2', 'libvirt')
        lvm_size = 1048576
        executes = []
        expected_commands = [('dd', 'bs=1048576', 'if=/dev/zero', 'of=/dev/v9',
                              'seek=0', 'count=1', 'oflag=direct')]
        lvm.clear_volume('/dev/v9')
        self.assertEqual(expected_commands, executes)

        # Test volume_clear=shred
        CONF.set_override('volume_clear', 'shred', 'libvirt')
        CONF.set_override('volume_clear_size', '0', 'libvirt')
        lvm_size = 1048576
        executes = []
        expected_commands = [('shred', '-n3', '-s1048576', '/dev/va')]
        lvm.clear_volume('/dev/va')
        self.assertEqual(expected_commands, executes)

        CONF.set_override('volume_clear', 'shred', 'libvirt')
        CONF.set_override('volume_clear_size', '1', 'libvirt')
        lvm_size = 10485761
        executes = []
        expected_commands = [('shred', '-n3', '-s1048576', '/dev/vb')]
        lvm.clear_volume('/dev/vb')
        self.assertEqual(expected_commands, executes)

        # Test volume_clear=none does nothing
        CONF.set_override('volume_clear', 'none', 'libvirt')
        executes = []
        expected_commands = []
        lvm.clear_volume('/dev/vc')
        self.assertEqual(expected_commands, executes)

        # Test volume_clear=invalid falls back to the default 'zero'
        CONF.set_override('volume_clear', 'invalid', 'libvirt')
        lvm_size = 1
        executes = []
        expected_commands = [('dd', 'bs=1', 'if=/dev/zero', 'of=/dev/vd',
                              'seek=0', 'count=1', 'conv=fdatasync')]
        lvm.clear_volume('/dev/vd')
        self.assertEqual(expected_commands, executes)

    def test_fail_remove_all_logical_volumes(self):
        def fake_execute(*args, **kwargs):
            if 'vol2' in args:
                raise processutils.ProcessExecutionError('Error')

        with contextlib.nested(
                mock.patch.object(lvm, 'clear_volume'),
                mock.patch.object(libvirt_utils,
                                  'execute',
                                  side_effect=fake_execute)) as (mock_clear,
                                                                 mock_execute):
            self.assertRaises(exception.VolumesNotRemoved, lvm.remove_volumes,
                              ['vol1', 'vol2', 'vol3'])
            self.assertEqual(3, mock_execute.call_count)
Esempio n. 7
0
 def fake_execute(*args, **kwargs):
     if 'vol2' in args:
         raise processutils.ProcessExecutionError('Error')
 def fake_execute(*args, **kwargs):
     raise processutils.ProcessExecutionError('error')
Esempio n. 9
0
 def fake_execute(*args, **kwargs):
     if args[1] == 'bad':
         raise processutils.ProcessExecutionError()
     return 'fakecontents', None
Esempio n. 10
0
 def fake_get_disk(disk_path):
     raise processutils.ProcessExecutionError()
Esempio n. 11
0
 def fake_execute(*args, **kwargs):
     if args[0] == 'rsync':
         raise processutils.ProcessExecutionError("Bad result")
     self.assertTrue(args[0] == 'scp')