def get_session_auth_stub_config(self): """ Create a stub configuration that uses session-based authentication. Session authentication is more efficient, since the server only needs to perform authentication of the username/password one time. """ session = requests.session() # Since the NSX manager default certificate is self-signed, # we disable verification. This is dangerous and real code # should verify that it is talking to a valid server. session.verify = False requests.packages.urllib3.disable_warnings() nsx_url = 'https://%s:%s' % (self.ip, self.tcp_port) resp = session.post(nsx_url + "/api/session/create", data={"j_username": self.username, "j_password": self.password}) if resp.status_code != requests.codes.ok: resp.raise_for_status() # Set the Cookie and X-XSRF-TOKEN headers session.headers["Cookie"] = resp.headers.get("Set-Cookie") session.headers["X-XSRF-TOKEN"] = resp.headers.get("X-XSRF-TOKEN") connector = connect.get_requests_connector( session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_runtime_configuration( connector, response_extractor=True) return stub_config
def get_basic_auth_stub_config(self): """ Create a stub configuration that uses HTTP basic authentication. """ session = requests.session() # Since the NSX manager default certificate is self-signed, # we disable verification. This is dangerous and real code # should verify that it is talking to a valid server. session.verify = False requests.packages.urllib3.disable_warnings() nsx_url = 'https://%s:%s' % (self.ip, self.tcp_port) connector = connect.get_requests_connector( session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_runtime_configuration( connector, response_extractor=True) security_context = create_user_password_security_context( self.username, self.password) connector.set_security_context(security_context) return stub_config