Python libvirt.VIR_CRED_PASSPHRASE Examples

The following are 21 code examples of libvirt.VIR_CRED_PASSPHRASE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module libvirt , or try the search function .
Example #1
Source File: utils.py    From virtualbmc with Apache License 2.0 6 votes vote down vote up
def __enter__(self):
        try:
            if self.sasl_username and self.sasl_password:

                def request_cred(credentials, user_data):
                    for credential in credentials:
                        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
                            credential[4] = self.sasl_username
                        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
                            credential[4] = self.sasl_password
                    return 0

                auth = [[libvirt.VIR_CRED_AUTHNAME,
                         libvirt.VIR_CRED_PASSPHRASE], request_cred, None]
                flags = libvirt.VIR_CONNECT_RO if self.readonly else 0
                self.conn = libvirt.openAuth(self.uri, auth, flags)
            elif self.readonly:
                self.conn = libvirt.openReadOnly(self.uri)
            else:
                self.conn = libvirt.open(self.uri)

            return self.conn

        except libvirt.libvirtError as e:
            raise exception.LibvirtConnectionOpenError(uri=self.uri, error=e) 
Example #2
Source File: LibvirtClient.py    From katprep with GNU General Public License v3.0 6 votes vote down vote up
def retrieve_credentials(self, credentials, user_data):
        """
        Retrieves the libvirt credentials in a strange format and hand it to
        the API in order to communicate with the hypervisor.
        To be honest, I have no idea why this has to be done this way. I have
        taken this function from the official libvirt documentation.

        :param credentials: libvirt credentials object
        :param user_data: some data that will never be used
        :type user_data: None
        """
        #get credentials for libvirt
        for credential in credentials:
            if credential[0] == libvirt.VIR_CRED_AUTHNAME:
                credential[4] = self.USERNAME
                if len(credential[4]) == 0:
                    credential[4] = credential[3]
            elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
                credential[4] = self.PASSWORD
            else:
                return -1
        return 0 
Example #3
Source File: multiple_thread_block_on_domain_create.py    From libvirt-test-API with GNU General Public License v2.0 6 votes vote down vote up
def request_credentials(credentials, user_data):
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            # prompt the user to input a authname. display the provided message
            credential[4] = "root"

            # if the user just hits enter raw_input() returns an empty string.
            # in this case return the default result through the last item of
            # the list
            if len(credential[4]) == 0:
                credential[4] = credential[3]
        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
            # use the getpass module to prompt the user to input a password.
            # display the provided message and return the result through the
            # last item of the list
            credential[4] = "redhat"
        else:
            return -1

    return 0 
Example #4
Source File: vMConUtils.py    From VManagePlatform with GNU General Public License v2.0 5 votes vote down vote up
def __connect_tcp(self):
        flags = [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE]
        auth = [flags, self.__libvirt_auth_credentials_callback, None]
        uri = 'qemu+tcp://%s/system' % self.host

        try:
            self.connection = libvirt.openAuth(uri, auth, 0)
            self.last_error = None

        except libvirtError as e:
            self.last_error = 'Connection Failed: ' + str(e)
            self.connection = None 
Example #5
Source File: RADclass.py    From openmano with Apache License 2.0 5 votes vote down vote up
def getCredentials(creds, data):
    """Used as a backup for libvirt.openAuth in order to provide password that came with data,
    not used by the moment
    """
    print "RADclass:getCredentials", creds, data
    for cred in creds:
        print cred[1] + ": ",
        if cred[0] == libvirt.VIR_CRED_AUTHNAME:
            cred[4] = data
        elif cred[0] == libvirt.VIR_CRED_PASSPHRASE:
            cred[4] = data
        else:
            return -1
    return 0 
Example #6
Source File: vMConUtils.py    From VManagePlatform with GNU General Public License v2.0 5 votes vote down vote up
def __libvirt_auth_credentials_callback(self, credentials, user_data):
        for credential in credentials:
            if credential[0] == libvirt.VIR_CRED_AUTHNAME:
                credential[4] = self.login
                if len(credential[4]) == 0:
                    credential[4] = credential[3]
            elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
                credential[4] = self.passwd
            else:
                return -1
        return 0 
Example #7
Source File: vMConUtils.py    From VManagePlatform with GNU General Public License v2.0 5 votes vote down vote up
def __connect_tls(self):
        flags = [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE]
        auth = [flags, self.__libvirt_auth_credentials_callback, None]
        uri = 'qemu+tls://%s@%s/system' % (self.login, self.host)

        try:
            return libvirt.openAuth(uri, auth, 0)
        except libvirtError as e:
            self.last_error = 'Connection Failed: ' + str(e)
            self.connection = None 
Example #8
Source File: vMConUtils.py    From VManagePlatform with GNU General Public License v2.0 5 votes vote down vote up
def __connect_tcp(self):
        flags = [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE]
        auth = [flags, self.__libvirt_auth_credentials_callback, None]
        uri = 'qemu+tcp://%s/system' % self.host

        try:
            return libvirt.openAuth(uri, auth, 0)
        except libvirtError as e:
            self.last_error = 'Connection Failed: ' + str(e)
            self.connection = None 
Example #9
Source File: vMConUtils.py    From VManagePlatform with GNU General Public License v2.0 5 votes vote down vote up
def __libvirt_auth_credentials_callback(self, credentials, user_data):
        for credential in credentials:
            if credential[0] == libvirt.VIR_CRED_AUTHNAME:
                credential[4] = self.login
                if len(credential[4]) == 0:
                    credential[4] = credential[3]
            elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
                credential[4] = self.passwd
            else:
                return -1
        return 0 
Example #10
Source File: libvirtd.py    From virt-who with GNU General Public License v2.0 5 votes vote down vote up
def _connect(self):
        url = self.config.get('server', "")
        self.logger.info("Using libvirt url: %s", url if url else '""')
        try:
            if self.config.get('password', None):
                auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], libvirt_cred_request, self.config]
                v = libvirt.openAuth(url, auth, libvirt.VIR_CONNECT_RO)
            else:
                v = libvirt.openReadOnly(url)
        except libvirt.libvirtError as e:
            self.logger.exception("Error in libvirt backend")
            raise VirtError(str(e))
        v.domainEventRegister(self._callback, None)
        v.setKeepAlive(5, 3)
        return v 
Example #11
Source File: libvirtd.py    From virt-who with GNU General Public License v2.0 5 votes vote down vote up
def libvirt_cred_request(credentials, config):
    """ Callback function for requesting credentials from libvirt """
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            credential[4] = config.get('username', None)
        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
            credential[4] = config.get('password', None)
        else:
            return -1
    return 0 
Example #12
Source File: tls_setup_new.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def hypervisor_connecting_test(uri, auth_tls, username,
                               password, logger, expected_result):
    """ connect remote server """
    ret = 0
    try:
        if auth_tls == 'none':
            logger.debug("call libvirt.open()")
            conn = libvirt.open(uri)
        elif auth_tls == 'sasl':
            user_data = [username, password]
            auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], domain_common.request_credentials, user_data]
            logger.debug("call libvirt.openAuth()")
            conn = libvirt.openAuth(uri, auth, 0)
        conn.close()
    except libvirtError as e:
        logger.error("API error message: %s, error code is %s"
                     % (e.get_error_message(), e.get_error_code()))
        ret = 1

    if ret == 0 and expected_result == 'success':
        logger.info("tls authentication success")
        return 0
    elif ret == 1 and expected_result == 'fail':
        logger.info("tls authentication failed, but that is expected")
        return 0
    elif ret == 0 and expected_result == 'fail':
        logger.error("tls authentication success, but we hope the reverse")
        return 1
    elif ret == 1 and expected_result == 'success':
        logger.error("tls authentication failed")
        return 1

    return 0 
Example #13
Source File: unix_perm_sasl.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def request_credentials(credentials, user_data):
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            credential[4] = user_data[0]

            if len(credential[4]) == 0:
                credential[4] = credential[3]
        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
            credential[4] = user_data[1]
        else:
            return -1

    return 0 
Example #14
Source File: tls_setup.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def request_credentials(credentials, user_data):
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            credential[4] = user_data[0]

            if len(credential[4]) == 0:
                credential[4] = credential[3]
        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
            credential[4] = user_data[1]
        else:
            return -1

    return 0 
Example #15
Source File: tcp_setup.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def hypervisor_connecting_test(uri, auth_tcp, username,
                               password, logger, expected_result):
    """ connect remote server """
    ret = 1
    try:
        if auth_tcp == 'none':
            logger.debug("call libvirt.open()")
            conn = libvirt.open(uri)
        elif auth_tcp == 'sasl':
            user_data = [username, password]
            auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data]
            logger.debug("call libvirt.openAuth()")
            conn = libvirt.openAuth(uri, auth, 0)

        ret = 0
        conn.close()
    except libvirtError as e:
        logger.error("API error message: %s, error code is %s"
                     % (e.get_error_message(), e.get_error_code()))
        ret = 1

    if ret == 0 and expected_result == 'success':
        logger.info("tcp connnection success")
        return 0
    elif ret == 1 and expected_result == 'fail':
        logger.info("tcp connection failed, but that is expected")
        return 0
    elif ret == 0 and expected_result == 'fail':
        logger.error("tcp connection success, but we hope the reverse")
        return 1
    elif ret == 1 and expected_result == 'success':
        logger.error("tcp connection failed")
        return 1

    return 0 
Example #16
Source File: tcp_setup.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def request_credentials(credentials, user_data):
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            credential[4] = user_data[0]

            if len(credential[4]) == 0:
                credential[4] = credential[3]
        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
            credential[4] = user_data[1]
        else:
            return -1

    return 0 
Example #17
Source File: utils.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def get_conn(uri='', username='', password=''):
    """ get connection object from libvirt module
    """
    user_data = [username, password]
    auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE],
            request_credentials, user_data]
    conn = libvirt.openAuth(uri, auth, 0)
    return conn 
Example #18
Source File: utils.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def request_credentials(credentials, user_data):
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            credential[4] = user_data[0]

            if len(credential[4]) == 0:
                credential[4] = credential[3]
        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
            credential[4] = user_data[1]
        else:
            return -1

    return 0 
Example #19
Source File: LibvirtClient.py    From katprep with GNU General Public License v3.0 5 votes vote down vote up
def __connect(self):
        """This function establishes a connection to the hypervisor."""
        #create weirdo auth dict
        auth = [
            [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE],
            self.retrieve_credentials, None
            ]
        #authenticate
        try:
            self.SESSION = libvirt.openAuth(self.URI, auth, 0)
            if self.SESSION == None:
                raise SessionException("Unable to establish connection to hypervisor!")
        except libvirt.libvirtError as err:
            raise InvalidCredentialsException("Invalid credentials") 
Example #20
Source File: unix_perm_sasl.py    From libvirt-test-API with GNU General Public License v2.0 4 votes vote down vote up
def hypervisor_connecting_test(uri, unix_sock_group, auth_unix_ro, auth_unix_rw, logger):
    """connect to hypervisor"""
    logger.info("connect to hypervisor")
    orginal_user = os.geteuid()
    testing_user_id = getpwnam(TESTING_USER)[2]
    logger.info("the testing_user id is %d" % testing_user_id)

    logger.info("set euid to %d" % testing_user_id)
    os.seteuid(testing_user_id)

    if utils.version_compare("libvirt", 3, 2, 0, logger):
        cmd = "klist -A | grep 'Ticket cache: FILE:' | awk '{print $3}'"
        ret, out = utils.exec_cmd(cmd, shell=True)
        if ret:
            logger.error("get ticket cache file failed.")
            logger.error("cmd: %s" % cmd)
            logger.error("out: %s" % out)
            return 1

        TICKET_CACHE = out[0].split(':')[1]
        cmd = "chown %s:%s %s" % (TESTING_USER, unix_sock_group, TICKET_CACHE)
        ret, out = utils.exec_cmd(cmd, shell=True)
        if ret:
            logger.error("change %s owner failed." % TICKET_CACHE)
            return 1

    try:
        if auth_unix_ro == 'none':
            conn = libvirt.openReadOnly(uri)
        elif auth_unix_ro == 'sasl':
            user_data = [TESTING_USER, TESTING_USER]
            auth = [[libvirt.VIR_CRED_AUTHNAME,
                     libvirt.VIR_CRED_PASSPHRASE],
                    request_credentials, user_data]
            conn = libvirt.openAuth(uri, auth, 0)

        if auth_unix_rw == 'none':
            conn = libvirt.open(uri)
        elif auth_unix_rw == 'sasl':
            user_data = [TESTING_USER, TESTING_USER]
            auth = [[libvirt.VIR_CRED_AUTHNAME,
                     libvirt.VIR_CRED_PASSPHRASE],
                    request_credentials, user_data]
            conn = libvirt.openAuth(uri, auth, 0)
        conn.close()
    except libvirtError as e:
        logger.error("API error message: %s, error code is %s"
                     % (e.get_error_message(), e.get_error_code()))
        logger.info("set euid back to %d" % orginal_user)
        os.seteuid(orginal_user)
        conn.close()
        return 1

    logger.info("set euid back to %d" % orginal_user)
    os.seteuid(orginal_user)
    return 0 
Example #21
Source File: multiple_thread_block_on_domain_create.py    From libvirt-test-API with GNU General Public License v2.0 4 votes vote down vote up
def multiple_thread_block_on_domain_create(params):
    """ spawn multiple threads to create guest simultaneously
        check the return status of calling create API
    """
    logger = params['logger']
    guestos = params.get('guestos')
    arch = params.get('guestarch')
    num = params.get('guestnum')
    xmlstr = params['xml']

    logger.info("the os of guest is %s" % guestos)
    logger.info("the arch of guest is %s" % arch)
    logger.info("the number of guest we are going to install is %s" % num)

    hypervisor = utils.get_hypervisor()
    uri = params['uri']

    auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, None]

    conn = libvirt.openAuth(uri, auth, 0)

    logger.info("the type of hypervisor is %s" % hypervisor)
    logger.debug("the uri to connect is %s" % uri)

    envfile = os.path.join(HOME_PATH, 'global.cfg')
    envparser = env_parser.Envparser(envfile)
    ostree = envparser.get_value("guest", guestos + "_" + arch)
    ks = envparser.get_value("guest", guestos + "_" + arch +
                             "_http_ks")

    # download vmlinuz and initrd.img
    vmlinuzpath = os.path.join(ostree, 'isolinux/vmlinuz')
    initrdpath = os.path.join(ostree, 'isolinux/initrd.img')

    urllib.urlretrieve(vmlinuzpath, '/var/lib/libvirt/boot/vmlinuz')
    urllib.urlretrieve(initrdpath, '/var/lib/libvirt/boot/initrd.img')

    name = "guest"
    start_num = num.split('-')[0]
    end_num = num.split('-')[1]
    thread_pid = []
    for i in range(int(start_num), int(end_num)):
        guestname = name + str(i)
        thr = guest_install(guestname, guestos, arch, ks, conn, xmlstr, logger)
        thread_pid.append(thr)

    for id in thread_pid:
        id.start()

    for id in thread_pid:
        id.join()

    conn.close()
    return 0