Exemplo n.º 1
0
    def test__get_connection(self):
        """Test method ``_get_connection``.

        Mock up ``paramiko.SSHClient`` (by overriding method
        ``_call_paramiko_sshclient``) before calling ``_get_connection``.
        Assert that certain parameters are passed to the (mock)
        ``paramiko.SSHClient`` object, and that certain methods on that object
        are called.

        """
        ssh._call_paramiko_sshclient = MockSSHClient  # pylint:disable=W0212
        backup = conf.properties

        key_filename = os.path.join(
            get_app_root(), 'tests', 'robottelo', 'data', 'test_dsa.key'
        )
        conf.properties['main.server.hostname'] = 'example.com'
        conf.properties['main.server.ssh.username'] = '******'
        conf.properties['main.server.ssh.key_private'] = key_filename
        with ssh._get_connection() as connection:  # pylint:disable=W0212
            self.assertEqual(connection.set_missing_host_key_policy_, 1)
            self.assertEqual(connection.connect_, 1)
            self.assertEqual(connection.close_, 0)
            self.assertEqual(connection.hostname, 'example.com')
            self.assertEqual(connection.username, 'nobody')
            self.assertEqual(connection.key_filename, key_filename)
        self.assertEqual(connection.set_missing_host_key_policy_, 1)
        self.assertEqual(connection.connect_, 1)
        self.assertEqual(connection.close_, 1)

        conf.properties = backup
Exemplo n.º 2
0
    def test_get_connection_key(self, settings):
        """Test method ``_get_connection`` using key file to connect to the
        server.

        Mock up ``paramiko.SSHClient`` (by overriding method
        ``_call_paramiko_sshclient``) before calling ``_get_connection``.
        Assert that certain parameters are passed to the (mock)
        ``paramiko.SSHClient`` object, and that certain methods on that object
        are called.
        """
        ssh._call_paramiko_sshclient = MockSSHClient  # pylint:disable=W0212

        key_filename = os.path.join(
            os.path.abspath(__name__), 'data', 'test_dsa.key'
        )
        settings.server.hostname = 'example.com'
        settings.server.ssh_username = '******'
        settings.server.ssh_key = key_filename
        with ssh._get_connection() as connection:  # pylint:disable=W0212
            self.assertEqual(connection.set_missing_host_key_policy_, 1)
            self.assertEqual(connection.connect_, 1)
            self.assertEqual(connection.close_, 0)
            self.assertEqual(connection.hostname, 'example.com')
            self.assertEqual(connection.username, 'nobody')
            self.assertEqual(connection.key_filename, key_filename)
        self.assertEqual(connection.set_missing_host_key_policy_, 1)
        self.assertEqual(connection.connect_, 1)
        self.assertEqual(connection.close_, 1)
Exemplo n.º 3
0
    def test_get_connection_pass(self, settings):
        """Test method ``_get_connection`` using password of user to connect to
        the server

        Mock up ``paramiko.SSHClient`` (by overriding method
        ``_call_paramiko_sshclient``) before calling ``_get_connection``.
        Assert that certain parameters are passed to the (mock)
        ``paramiko.SSHClient`` object, and that certain methods on that object
        are called.
        """
        ssh._call_paramiko_sshclient = MockSSHClient  # pylint:disable=W0212
        settings.server.hostname = 'example.com'
        settings.server.ssh_username = '******'
        settings.server.ssh_key = None
        settings.server.ssh_password = '******'
        with ssh._get_connection() as connection:  # pylint:disable=W0212
            self.assertEqual(connection.set_missing_host_key_policy_, 1)
            self.assertEqual(connection.connect_, 1)
            self.assertEqual(connection.close_, 0)
            self.assertEqual(connection.hostname, 'example.com')
            self.assertEqual(connection.username, 'nobody')
            self.assertEqual(connection.password, 'test_password')
        self.assertEqual(connection.set_missing_host_key_policy_, 1)
        self.assertEqual(connection.connect_, 1)
        self.assertEqual(connection.close_, 1)
Exemplo n.º 4
0
    def test_get_connection_key(self, settings):
        """Test method ``_get_connection`` using key file to connect to the
        server.

        Mock up ``paramiko.SSHClient`` (by overriding method
        ``_call_paramiko_sshclient``) before calling ``_get_connection``.
        Assert that certain parameters are passed to the (mock)
        ``paramiko.SSHClient`` object, and that certain methods on that object
        are called.
        """
        ssh._call_paramiko_sshclient = MockSSHClient  # pylint:disable=W0212

        key_filename = os.path.join(os.path.abspath(__name__), 'data',
                                    'test_dsa.key')
        settings.server.hostname = 'example.com'
        settings.server.ssh_username = '******'
        settings.server.ssh_key = key_filename
        with ssh._get_connection() as connection:  # pylint:disable=W0212
            self.assertEqual(connection.set_missing_host_key_policy_, 1)
            self.assertEqual(connection.connect_, 1)
            self.assertEqual(connection.close_, 0)
            self.assertEqual(connection.hostname, 'example.com')
            self.assertEqual(connection.username, 'nobody')
            self.assertEqual(connection.key_filename, key_filename)
        self.assertEqual(connection.set_missing_host_key_policy_, 1)
        self.assertEqual(connection.connect_, 1)
        self.assertEqual(connection.close_, 1)
Exemplo n.º 5
0
    def test_get_connection_pass(self, settings):
        """Test method ``_get_connection`` using password of user to connect to
        the server

        Mock up ``paramiko.SSHClient`` (by overriding method
        ``_call_paramiko_sshclient``) before calling ``_get_connection``.
        Assert that certain parameters are passed to the (mock)
        ``paramiko.SSHClient`` object, and that certain methods on that object
        are called.
        """
        ssh._call_paramiko_sshclient = MockSSHClient  # pylint:disable=W0212
        settings.server.hostname = 'example.com'
        settings.server.ssh_username = '******'
        settings.server.ssh_key = None
        settings.server.ssh_password = '******'
        with ssh._get_connection() as connection:  # pylint:disable=W0212
            self.assertEqual(connection.set_missing_host_key_policy_, 1)
            self.assertEqual(connection.connect_, 1)
            self.assertEqual(connection.close_, 0)
            self.assertEqual(connection.hostname, 'example.com')
            self.assertEqual(connection.username, 'nobody')
            self.assertEqual(connection.password, 'test_password')
        self.assertEqual(connection.set_missing_host_key_policy_, 1)
        self.assertEqual(connection.connect_, 1)
        self.assertEqual(connection.close_, 1)
Exemplo n.º 6
0
def default_url_on_new_port(oldport, newport):
    """Creates context where the default smart-proxy is forwarded on a new port

    :param int oldport: Port to be forwarded.
    :param int newport: New port to be used to forward `oldport`.

    :return: A string containing the new capsule URL with port.
    :rtype: str

    """
    logger = logging.getLogger('robottelo')
    domain = settings.server.hostname
    user = settings.server.ssh_username
    key = settings.server.ssh_key
    ssh.upload_file(key, '/tmp/dsa_{0}'.format(newport))
    ssh.command('chmod 700 /tmp/dsa_{0}'.format(newport))

    with ssh._get_connection() as connection:
        command = u'ssh -i {0} -L {1}:{2}:{3} {4}@{5}'.format(
            '/tmp/dsa_{0}'.format(newport),
            newport, domain, oldport, user, domain)
        logger.debug('Creating tunnel {0}'.format(command))
        # Run command and timeout in 30 seconds.
        _, _, stderr = connection.exec_command(command, 30)

        stderr = stderr.read()
        if len(stderr) > 0:
            logger.debug('Tunnel failed: {0}'.format(stderr))
            # Something failed, so raise an exception.
            raise SSHTunnelError(stderr)
        yield 'https://{0}:{1}'.format(domain, newport)
Exemplo n.º 7
0
def default_url_on_new_port(oldport, newport):
    """Creates context where the default smart-proxy is forwarded on a new port
    REQUIRES GatewayPorts yes in sshd_config

    :param int oldport: Port to be forwarded.
    :param int newport: New port to be used to forward `oldport`.

    :return: A string containing the new capsule URL with port.
    :rtype: str

    """
    logger = logging.getLogger('robottelo')
    command_timeout = 2
    domain = settings.server.hostname
    user = settings.server.ssh_username
    key = settings.server.ssh_key
    ssh.upload_file(key, '/tmp/dsa_{0}'.format(newport))
    ssh.command('chmod 700 /tmp/dsa_{0}'.format(newport))

    with ssh._get_connection() as connection:
        command = (
            u'ssh -i {0} -o StrictHostKeyChecking=no -R {1}:{2}:{3} {4}@{5}'
        ).format(
            '/tmp/dsa_{0}'.format(newport),
            newport, domain, oldport, user, domain)
        logger.debug('Creating tunnel {0}'.format(command))
        transport = connection.get_transport()
        channel = transport.open_session()
        channel.exec_command(command)
        # if exit_status appears until command_timeout, throw error
        for _ in xrange(command_timeout):
            if channel.exit_status_ready():
                if channel.recv_exit_status() != 0:
                    stderr = u''
                    while channel.recv_stderr_ready():
                        stderr += channel.recv_stderr(1)
                    logger.debug('Tunnel failed: {0}'.format(stderr))
                    # Something failed, so raise an exception.
                    raise SSHTunnelError(stderr)
            sleep(1)
        yield 'https://{0}:{1}'.format(domain, newport)
        ssh.command('rm -f /tmp/dsa_{0}'.format(newport))
Exemplo n.º 8
0
def default_url_on_new_port(oldport, newport):
    """Creates context where the default smart-proxy is forwarded on a new port
    REQUIRES GatewayPorts yes in sshd_config

    :param int oldport: Port to be forwarded.
    :param int newport: New port to be used to forward `oldport`.

    :return: A string containing the new capsule URL with port.
    :rtype: str

    """
    logger = logging.getLogger('robottelo')
    command_timeout = 2
    domain = settings.server.hostname
    user = settings.server.ssh_username
    key = settings.server.ssh_key
    ssh.upload_file(key, '/tmp/dsa_{0}'.format(newport))
    ssh.command('chmod 700 /tmp/dsa_{0}'.format(newport))

    with ssh._get_connection() as connection:
        command = (
            u'ssh -i {0} -o StrictHostKeyChecking=no -R {1}:{2}:{3} {4}@{5}'
        ).format('/tmp/dsa_{0}'.format(newport), newport, domain, oldport,
                 user, domain)
        logger.debug('Creating tunnel {0}'.format(command))
        transport = connection.get_transport()
        channel = transport.open_session()
        channel.exec_command(command)
        # if exit_status appears until command_timeout, throw error
        for _ in range(command_timeout):
            if channel.exit_status_ready():
                if channel.recv_exit_status() != 0:
                    stderr = u''
                    while channel.recv_stderr_ready():
                        stderr += channel.recv_stderr(1)
                    logger.debug('Tunnel failed: {0}'.format(stderr))
                    # Something failed, so raise an exception.
                    raise SSHTunnelError(stderr)
            sleep(1)
        yield 'https://{0}:{1}'.format(domain, newport)
        ssh.command('rm -f /tmp/dsa_{0}'.format(newport))