Пример #1
0
    def test_simple_request_serializer(self):
        def request_matcher(r1, r2):
            soap_msg = ('<soapenv:Body>'
                        '<RetrieveServiceContent xmlns="urn:vim25">'
                        '<_this type="ServiceInstance">'
                        'ServiceInstance'
                        '</_this>'
                        '</RetrieveServiceContent>'
                        '</soapenv:Body>')
            if soap_msg in r1.body:
                return True
            raise SystemError('serialization error occurred')

        my_vcr = vcr.VCR()
        my_vcr.register_matcher('request_matcher', request_matcher)

        with my_vcr.use_cassette(
                'test_simple_request_serializer.yaml',
                cassette_library_dir=tests.fixtures_path,
                record_mode='none',
                match_on=['request_matcher']) as cass:
            host = 'vcsa'
            port = 443
            stub = SoapStubAdapter(host, port)
            si = vim.ServiceInstance("ServiceInstance", stub)
            content = si.RetrieveContent()
            self.assertTrue(content is not None)
            self.assertTrue(
                '<_this type="ServiceInstance">ServiceInstance</_this>'
                in cass.requests[0].body)
Пример #2
0
    def __init__(self, server, username, password):
        self.server = server
        self.username = username
        self.password = password
        self.SI = None

        requests.packages.urllib3.disable_warnings()
        self.context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        self.context.verify_mode = ssl.CERT_NONE
        try:
            smart_stub = connect.SmartStubAdapter(host=self.server,
                                                  sslContext=self.context,
                                                  connectionPoolTimeout=0)
            session_stub = connect.VimSessionOrientedStub(smart_stub,
                                                          connect.VimSessionOrientedStub.makeUserLoginMethod(self.username,
                                                                                                             self.password))
            self.SI = vim.ServiceInstance('ServiceInstance', session_stub)

        except Exception as exc:
            raise vCenterException(exc)

        else:
            atexit.register(connect.Disconnect, self.SI)

        if not self.SI:
            vCenterException("Unable to connect to host with supplied info.")
Пример #3
0
    def _get_service_instance(self):
        """
        Gets the service instance

        Returns:
            vim.ServiceInstance: Service Instance for Host

        """
        try:
            smart_stub = SmartStubAdapter(
                host=self._host,
                port=int(self._port),
                sslContext=self.sslContext,
                connectionPoolTimeout=0,
            )
            session_stub = VimSessionOrientedStub(
                smart_stub,
                VimSessionOrientedStub.makeUserLoginMethod(
                    self._user, self._password),
            )
            service_instance = vim.ServiceInstance("ServiceInstance",
                                                   session_stub)

            # Ensure connection to server is closed on program exit
            atexit.register(Disconnect, service_instance)
            return service_instance
        except vmodl.MethodFault as error:
            logger.error(f"Caught vmodl fault : {error.msg}")
            raise
Пример #4
0
 def _get_file_manager(self):
     if not self._file_manager:
         soap_stub = self._datacenter_mo._stub
         service_instance = vim.ServiceInstance('ServiceInstance',
                                                soap_stub)
         self._file_manager = service_instance.content.fileManager
     return self._file_manager
Пример #5
0
 def test_find_a_host(self, mock_si):
     """
         Verify that find a host calls the right sub functions
     """
     test_si = vim.ServiceInstance()
     test_si.content.searchIndex.FindAllByDnsName.return_value = ['thehost']
     self.assertEqual(find_host(test_si, 'thehost'), 'thehost')
     test_si.content.searchIndex.FindAllByDnsName.assert_called_once()
Пример #6
0
def WaitForTask(task,
                raiseOnError=True,
                si=None,
                pc=None,
                onProgressUpdate=None):
    """
    Wait for task to complete.

    @type  raiseOnError      : bool
    @param raiseOnError      : Any exception thrown is thrown up to the caller
                               if raiseOnError is set to true.
    @type  si                : ManagedObjectReference to a ServiceInstance.
    @param si                : ServiceInstance to use. If None, use the
                               information from the task.
    @type  pc                : ManagedObjectReference to a PropertyCollector.
    @param pc                : Property collector to use. If None, get it from
                               the ServiceInstance.
    @type  onProgressUpdate  : callable
    @param onProgressUpdate  : Callable to call with task progress updates.

        For example::

            def OnTaskProgressUpdate(task, percentDone):
                print 'Task %s is %d%% complete.' % (task, percentDone)
    """

    if si is None:
        si = vim.ServiceInstance("ServiceInstance", task._stub)
    if pc is None:
        pc = si.content.propertyCollector

    progressUpdater = ProgressUpdater(task, onProgressUpdate)
    progressUpdater.Update('created')

    filter = CreateFilter(pc, task)

    version, state = None, None
    # Loop looking for updates till the state moves to a completed state.
    while state not in (vim.TaskInfo.State.success, vim.TaskInfo.State.error):
        try:
            version, state = GetTaskStatus(task, version, pc)
            progressUpdater.UpdateIfNeeded()
        except vmodl.fault.ManagedObjectNotFound as e:
            print("Task object has been deleted: %s" % e.obj)
            break

    filter.Destroy()

    if state == "error":
        progressUpdater.Update('error: %s' % str(task.info.error))
        if raiseOnError:
            raise task.info.error
        else:
            print("Task reported error: " + str(task.info.error))
    else:
        progressUpdater.Update('completed')

    return state
Пример #7
0
    def __init__(self,
                 hostname: str,
                 username: str,
                 password: str,
                 datacenter: str = None,
                 cluster: str = None,
                 port: int = 443):
        """ Create a VcCluster instance """

        # Instance variables
        self.hostname = hostname
        self.username = username
        self.password = password
        self.port = port

        # Instance parameters
        self.ssl_context = unverified_ssl_context()
        self.timeout = 0

        try:
            # Connect using a session oriented connection
            # Ref. https://github.com/vmware/pyvmomi/issues/347
            self.si = None
            credentials = VimSessionOrientedStub.makeUserLoginMethod(self.username, self.password)
            smart_stub = SmartStubAdapter(host=self.hostname,
                                          port=self.port,
                                          sslContext=self.ssl_context,
                                          connectionPoolTimeout=self.timeout)
            self.session_stub = VimSessionOrientedStub(smart_stub, credentials)
            self.si = vim.ServiceInstance('ServiceInstance', self.session_stub)

            if not self.si:
                msg = f'Could not connect to the specified host using the specified username and password'
                raise ValueError(msg)

        except socket.gaierror as e:
            msg = f'Connection: failed ({e.strerror})'
            raise ValueError(msg)

        except IOError as e:
            raise e

        except Exception as e:
            raise e

        self.host_type = self.si.content.about.apiType
        if self.host_type != 'VirtualCenter':
            raise ValueError(f'Host is not a vCenter: {self.host_type}')

        self.api_version = self.si.content.about.apiVersion
        if int(self.api_version.split('.')[0]) < 6:
            raise RuntimeError(f'API version less than 6.0.0 is not supported: {self.api_version}')

        # Get objects
        self.datacenter = self.__get_datacenter(name=datacenter)
        self.cluster = self.__get_cluster(name=cluster)
        self.hosts = self.get_cluster_hosts()
Пример #8
0
def __RetrieveContent(host, port, adapter, version, path, keyFile, certFile,
                      thumbprint, sslContext, connectionPoolTimeout=CONNECTION_POOL_IDLE_TIMEOUT_SEC):
   """
   Retrieve service instance for connection.
   @param host: Which host to connect to.
   @type  host: string
   @param port: Port
   @type  port: int
   @param adapter: Adapter
   @type  adapter: string
   @param version: Version
   @type  version: string
   @param path: Path
   @type  path: string
   @param keyFile: ssl key file path
   @type  keyFile: string
   @param certFile: ssl cert file path
   @type  certFile: string
   @param connectionPoolTimeout: Timeout in secs for idle connections to close, specify negative numbers for never
                                 closing the connections
   @type  connectionPoolTimeout: int
   """

   # XXX remove the adapter and service arguments once dependent code is fixed
   if adapter != "SOAP":
      raise ValueError(adapter)

   # Create the SOAP stub adapter
   stub = SoapStubAdapter(host, port, version=version, path=path,
                          certKeyFile=keyFile, certFile=certFile,
                          thumbprint=thumbprint, sslContext=sslContext,
                          connectionPoolTimeout=connectionPoolTimeout)

   # Get Service instance
   si = vim.ServiceInstance("ServiceInstance", stub)
   content = None
   try:
      content = si.RetrieveContent()
   except vmodl.MethodFault:
      raise
   except ParserError:
      raise
   except Exception as e:
      # NOTE (hartsock): preserve the traceback for diagnostics
      # pulling and preserving the traceback makes diagnosing connection
      # failures easier since the fault will also include where inside the
      # library the fault occurred. Without the traceback we have no idea
      # why the connection failed beyond the message string.
      (type, value, traceback) = sys.exc_info()
      if traceback:
         fault = vim.fault.HostConnectFault(msg=str(e))
         reraise(vim.fault.HostConnectFault, fault, traceback)
      else:
          raise vim.fault.HostConnectFault(msg=str(e))

   return content, si, stub
Пример #9
0
 def connect_ticket(self, host, ticket):
     if ticket:
         try:
             stub = connect.SoapStubAdapter(host, HOSTD_PORT, VIM_NAMESPACE)
             self._si = vim.ServiceInstance("ServiceInstance", stub)
             self._si.RetrieveContent().sessionManager.CloneSession(ticket)
             self._post_connect()
         except httplib.HTTPException as http_exception:
             self._logger.info("Failed to login hostd with ticket: %s" % http_exception)
             raise AcquireCredentialsException(http_exception)
Пример #10
0
 def test_fail_find_a_host(self, mock_si):
     """
         Verify that find a host calls the right sub functions,
         and fails with an exception
     """
     test_si = vim.ServiceInstance()
     test_si.content.searchIndex.FindAllByDnsName.return_value = []
     with self.assertRaises(Exception,
                            msg="Exception not raised \
                                on failure to find host"):
         find_host(test_si, 'thehost')
     test_si.content.searchIndex.FindAllByDnsName.assert_called_once()
def ConnectToVC(vchost, vcport, vcuser, vcpassword):
    si = SmartConnectNoSSL(host=vchost,
                           user=vcuser,
                           pwd=vcpassword,
                           port=vcport)

    stub = VimSessionOrientedStub(
        si._stub,
        VimSessionOrientedStub.makeUserLoginMethod(vcuser, vcpassword))
    si = vim.ServiceInstance("ServiceInstance", stub)
    atexit.register(Disconnect, si)
    return si
Пример #12
0
def connect(host, user, password):
    stub = pyVmomi.SoapStubAdapter(
        host=host,
        port=443,
        version='vim.version.version6',
        path='/sdk',
        certKeyFile=None,
        certFile=None)

    si = vim.ServiceInstance("ServiceInstance", stub)
    content = si.RetrieveContent()
    content.sessionManager.Login(user, password, None)
    return si
    def get_service_instance(self):
        # create a connection to hostd.
        request = ServiceTicketRequest(ServiceType.VIM)
        response = self.host_client.get_service_ticket(request)
        self.assertEqual(response.result, ServiceTicketResultCode.OK)

        hostd_port = 443
        vim_namespace = "vim25/5.0"
        stub = SoapStubAdapter(self.server, hostd_port, vim_namespace)
        si = vim.ServiceInstance("ServiceInstance", stub)
        si.RetrieveContent().sessionManager.CloneSession(response.vim_ticket)
        connect.SetSi(si)
        return si
Пример #14
0
def connectToVimEndpoint(host,
                         username,
                         password,
                         version='vim.version.version6'):
    """
      Get a service instance
   """
    context = None
    ssl._create_default_https_context = ssl._create_unverified_context
    stub = SoapStubAdapter(host=host, path="/sdk", poolSize=0, version=version)
    si = vim.ServiceInstance('ServiceInstance', stub)
    content = si.content
    logging.info(str([username, password]))
    content.sessionManager.Login(userName=username, password=password)
    return si, content
Пример #15
0
 def test_canary_test(self, mock_sleep, mock_vmotion,
                      mock_fh, mock_go, mock_vm, mock_si, mock_hs):
     """
         Verify that canary calls cause vmotions to be called for
     """
     test_si = vim.ServiceInstance()
     test_vm = vim.VirtualMachine()
     mock_go.return_value = test_vm
     host = vim.HostSystem()
     host.name = 'Foo'
     mock_fh.return_value = host
     canary_test(test_si, ['host1', 'host2'], 'vmname', False)
     test_si.RetrieveContent.assert_called_once()
     self.assertEqual(mock_vmotion.call_count, 2,
                      "Two vmotions should occur")
Пример #16
0
def __Login(host, port, user, pwd, service, adapter, version, path, keyFile,
            certFile):
    """
   Private method that performs the actual Connect and returns a
   connected service instance object.

   @param host: Which host to connect to.
   @type  host: string
   @param port: Port
   @type  port: int
   @param user: User
   @type  user: string
   @param pwd: Password
   @type  pwd: string
   @param service: Service
   @type  service: string
   @param adapter: Adapter
   @type  adapter: string
   @param version: Version
   @type  version: string
   @param path: Path
   @type  path: string
   @param keyFile: ssl key file path
   @type  keyFile: string
   @param certFile: ssl cert file path
   @type  certFile: string
   """

    # XXX remove the adapter and service arguments once dependent code is fixed
    if adapter != "SOAP":
        raise ValueError(adapter)

    # Create the SOAP stub adapter
    stub = SoapStubAdapter(host,
                           port,
                           version=version,
                           path=path,
                           certKeyFile=keyFile,
                           certFile=certFile)

    # Get Service instance
    si = vim.ServiceInstance("ServiceInstance", stub)
    try:
        content = si.RetrieveContent()
    except vmodl.MethodFault:
        raise
    except Exception, e:
        raise vim.fault.HostConnectFault(msg=str(e))
Пример #17
0
 def _doLogin(soapStub):
     import sso
     cert = soapStub.schemeArgs['cert_file']
     key = soapStub.schemeArgs['key_file']
     authenticator = sso.SsoAuthenticator(sts_url=stsUrl,
                                          sts_cert=stsCert)
     samlAssertion = authenticator.get_bearer_saml_assertion(
         username, password, cert, key)
     si = vim.ServiceInstance("ServiceInstance", soapStub)
     sm = si.content.sessionManager
     if not sm.currentSession:
         try:
             soapStub.samlToken = samlAssertion
             si.content.sessionManager.LoginByToken()
         finally:
             soapStub.samlToken = None
Пример #18
0
    def _base_serialize_test(self, soap_creator, request_matcher):
        my_vcr = vcr.VCR()
        my_vcr.register_matcher('request_matcher', request_matcher)

        with my_vcr.use_cassette(
                'test_simple_request_serializer.yaml',
                cassette_library_dir=tests.fixtures_path,
                record_mode='none',
                match_on=['request_matcher']) as cass:
            stub = soap_creator()
            si = vim.ServiceInstance("ServiceInstance", stub)
            content = si.RetrieveContent()
            self.assertTrue(content is not None)
            self.assertTrue(
                '<_this type="ServiceInstance">ServiceInstance</_this>'
                in cass.requests[0].body.decode("utf-8"))
Пример #19
0
def get_service_instance_from_managed_object(mo_ref, name='<unnamed>'):
    '''
    Retrieves the service instance from a managed object.

    me_ref
        Reference to a managed object (of type vim.ManagedEntity).

    name
        Name of managed object. This field is optional.
    '''
    if not name:
        name = mo_ref.name
    log.trace('[{0}] Retrieving service instance from managed object'
              ''.format(name))
    si = vim.ServiceInstance('ServiceInstance')
    si._stub = mo_ref._stub
    return si
Пример #20
0
 def test_canary_test_vmname(self, mock_sleep, mock_vmotion, mock_fh,
                             mock_go, mock_vm, mock_si, mock_hs):
     """
         Given a canary VM name, vmotions get called for.
     """
     test_si = vim.ServiceInstance()
     test_vm = vim.VirtualMachine()
     test_vm.guest.ipAddress = None
     test_vm.name = "Bob"
     mock_go.return_value = test_vm
     host = vim.HostSystem()
     host.name = 'Foo'
     mock_fh.return_value = host
     canary_test(test_si, ['host1', 'host2'], test_vm.name, False)
     test_si.RetrieveContent.assert_called_once()
     self.assertEqual(mock_vmotion.call_count, 2,
                      "Two vmotions should occur")
     mock_vmotion.assert_called_with(test_vm, host, test_vm.name, False)
Пример #21
0
def connect(host, user, password, verify=True):
    if verify:
        context = None
    else:
        context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        context.verify_mode = ssl.CERT_NONE

    stub = pyVmomi.SoapStubAdapter(
        host=host,
        port=443,
        version='vim.version.version6',
        path='/sdk',
        sslContext=context)

    si = vim.ServiceInstance("ServiceInstance", stub)
    content = si.RetrieveContent()
    content.sessionManager.Login(user, password, None)
    return si
Пример #22
0
 def test_canary_test_failure(self, mock_sleep, mock_vmotion, mock_fh,
                              mock_go, mock_vm, mock_si, mock_hs):
     """
         Verify that if an exception occurs, that
         a) the vmotion was asked for, and
         b) an exception happens
     """
     test_si = vim.ServiceInstance()
     test_vm = vim.VirtualMachine()
     mock_go.return_value = test_vm
     host = vim.HostSystem()
     host.name = 'Foo'
     mock_fh.return_value = host
     canarytest.vsphere_tools.do_a_vmotion.side_effect = Exception(
         'vmotion raised an exception Failed')
     with self.assertRaises(Exception):
         canary_test(test_si, ['host1', 'host2'], 'vmname', False)
     test_si.RetrieveContent.assert_called_once()
     self.assertEqual(mock_vmotion.call_count, 1,
                      "One vmotions should occur, raising the exception")
Пример #23
0
    def _connect(self):
        print("Connecting to %s as %s" %
              (self._vm_host["name"],
               self._request["host_authentication"]["username"]))
        smart_stub = SmartStubAdapter(
            host=self._vm_host["name"],
            port=443,
            sslContext=ssl._create_unverified_context(),
            connectionPoolTimeout=0)
        self._session_stub = VimSessionOrientedStub(
            smart_stub,
            VimSessionOrientedStub.makeUserLoginMethod(
                self._request["host_authentication"]["username"],
                self._request["host_authentication"]["password"]))
        si = vim.ServiceInstance('ServiceInstance', self._session_stub)

        if not si:
            raise Exception("Could not connect to %s" % self._vm_host["name"])

        return si
Пример #24
0
        def _doLogin(soapStub):
            import sso
            cert = soapStub.schemeArgs['cert_file']
            key = soapStub.schemeArgs['key_file']
            authenticator = sso.SsoAuthenticator(sts_url=stsUrl,
                                                 sts_cert=stsCert)

            samlAssertion = authenticator.get_hok_saml_assertion(cert, key)

            def _requestModifier(request):
                return sso.add_saml_context(request, samlAssertion, key)

            si = vim.ServiceInstance("ServiceInstance", soapStub)
            sm = si.content.sessionManager
            if not sm.currentSession:
                with soapStub.requestModifier(_requestModifier):
                    try:
                        soapStub.samlToken = samlAssertion
                        si.content.sessionManager.LoginByToken()
                    finally:
                        soapStub.samlToken = None
Пример #25
0
def _SmartSessionOrientedConnect(host, username, password):
    """Create a new session oriented stub that will
   automatically login when used.
   """

    # create a smart stub that will connect using the latest known
    # VIM version
    stub = SmartStubAdapter(host, sslContext=ssl._create_unverified_context())

    # login with username and password
    loginMethod = VimSessionOrientedStub.makeUserLoginMethod(
        username, password)

    # and make the stub session oriented
    sessOrientedStub = VimSessionOrientedStub(stub,
                                              loginMethod,
                                              retryDelay=0.5,
                                              retryCount=20)

    si = vim.ServiceInstance("ServiceInstance", sessOrientedStub)
    return si
Пример #26
0
    def _connect(self):
        # https://github.com/vmware/pyvmomi/issues/347#issuecomment-297591340
        smart_stub = SmartStubAdapter(
            host=self._request[
                self._side]["authentication"]["host"]["hostname"],
            port=443,
            sslContext=ssl._create_unverified_context(),
            connectionPoolTimeout=0)
        session_stub = VimSessionOrientedStub(
            smart_stub,
            VimSessionOrientedStub.makeUserLoginMethod(
                self._request[
                    self._side]["authentication"]["host"]["username"],
                self._request[self._side]["authentication"]["host"]
                ["password"],
            ))
        conn = vim.ServiceInstance('ServiceInstance', session_stub)

        if not conn:
            raise Exception("Could not connect to vCenter")

        return conn
Пример #27
0
 def GetManagedPropertyValuesPC(self, obj):
     PropertyCollector = vmodl.query.PropertyCollector
     filterSpec = PropertyCollector.FilterSpec(
         objectSet=[PropertyCollector.ObjectSpec(obj=obj, skip=False)],
         propSet=[
             PropertyCollector.PropertySpec(all=True, type=obj.__class__)
         ],
     )
     si = vim.ServiceInstance(self.serviceInstanceId, obj._stub)
     pc = si.RetrieveContent().GetPropertyCollector()
     props = []
     for content in pc.RetrieveContents([filterSpec]):
         props.extend([(dp.name, dp.val) for dp in content.propSet])
         # Raise an exception if any of the missing properties
         # were caused by authentication failures
         authFailures = [
             mp.fault for mp in content.missingSet
             if self.auth.IsNotAuthenticatedFault(mp.fault)
         ]
         if authFailures:
             raise authFailures[0]
         props.extend([(mp.path, '<i>%s</i>' % str(mp.fault))
                       for mp in content.missingSet])
     return props
Пример #28
0
 def _service_instance(self):
     stub = SoapStubAdapter("localhost", HOSTD_PORT)
     return vim.ServiceInstance("ServiceInstance", stub)
Пример #29
0
    print "Enter HostIP RootPassword CertPath KeyPath"
    print "Incorrect set of arguments passed."
    sys.exit()
host_ip = sys.argv[1]
root_password = sys.argv[2]
cert_path = sys.argv[3]
key_path = sys.argv[4]

print "Apply Certificate: %s, %s, %s" % (host_ip, cert_path, key_path)

print "Connecting to hostd CertificateManager"
ssl._create_default_https_context = ssl._create_unverified_context
stub = pyVmomi.SoapStubAdapter(host=host_ip,
                               version="vim.version.version10",
                               path="/sdk")
si = vim.ServiceInstance("ServiceInstance", stub)
content = si.RetrieveServiceContent()
content.sessionManager.Login("root", root_password)
cert_manager = vim.host.CertificateManager('ha-certificate-manager', stub)

print "Loading certificate from file"
cert = open(cert_path, 'r').read()
print "Installing certificate"
dName = '/CN=%s' % host_ip
csr = cert_manager.GenerateCertificateSigningRequestByDn(dName)
cert_manager.InstallServerCertificate(cert=cert)

print "Replacing private key /etc/vmware/ssl/rui.key"
shutil.copyfile(key_path, "/etc/vmware/ssl/rui.key")

print "Notifying affected services"
Пример #30
0
 def _doLogin(soapStub):
    si = vim.ServiceInstance("ServiceInstance", soapStub)
    sm = si.content.sessionManager
    if not sm.currentSession:
       si.content.sessionManager.LoginExtensionByCertificate(extensionKey)