def __open_connection(self): u""" Opens a new network connection """ self.converter = self._converter_class(use_sfbinaryformat=False, use_numpy=self._numpy) self._rest = SnowflakeRestful( host=self.host, port=self.port, protocol=self._protocol, inject_client_pause=self._inject_client_pause, connection=self) if self.host.endswith(u".privatelink.snowflakecomputing.com"): ocsp_cache_server = \ u'http://ocsp{}/ocsp_response_cache.json'.format( self.host[self.host.index('.'):]) os.environ[ 'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server else: if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' in os.environ: del os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'] auth_instance = AuthByWebBrowser(self.rest, self.application, protocol=self._protocol, host=self.host, port=self.port, webbrowser_pkg=WebbrowserPkg, socket_pkg=SocketPkg) if self._session_parameters is None: self._session_parameters = {} if self._autocommit is not None: self._session_parameters['AUTOCOMMIT'] = self._autocommit if self._timezone is not None: self._session_parameters['TIMEZONE'] = self._timezone if self.client_session_keep_alive: self._session_parameters['CLIENT_SESSION_KEEP_ALIVE'] = True # enable storing temporary credential in a file self._session_parameters[ 'CLIENT_STORE_TEMPORARY_CREDENTIAL'] = True auth = Auth(self.rest) if not auth.read_temporary_credential(self.account, self.user, self._session_parameters): self.__authenticate(auth_instance) else: # set the current objects as the session is derived from the id # token, and the current objects may be different. self._set_current_objects() self._password = None # ensure password won't persist if self.client_session_keep_alive: self._add_heartbeat()
def test_auth_password_change(): """Tests password change.""" global mock_cnt def _password_callback(): return "NEW_PASSWORD" application = 'testapplication' account = 'testaccount' user = '******' password = '******' # success test case mock_cnt = 0 rest = _init_rest(application, _mock_auth_password_change_rest_response) auth = Auth(rest) auth_instance = AuthByDefault(password) auth.authenticate(auth_instance, account, user, password_callback=_password_callback) assert not rest._connection.errorhandler.called # not error
def test_auth_mfa(): """ Authentication by MFA """ global mock_cnt application = 'testapplication' account = 'testaccount' user = '******' password = '******' # success test case mock_cnt = 0 rest = _init_rest(application, _mock_auth_mfa_rest_response) auth = Auth(rest) auth_instance = AuthByDefault(password) auth.authenticate(auth_instance, account, user) assert not rest._connection.errorhandler.called # not error assert rest.token == 'TOKEN' assert rest.master_token == 'MASTER_TOKEN' # failure test case mock_cnt = 0 rest = _init_rest(application, _mock_auth_mfa_rest_response_failure) auth = Auth(rest) auth_instance = AuthByDefault(password) auth.authenticate(auth_instance, account, user) assert rest._connection.errorhandler.called # error # timeout 1 second mock_cnt = 0 rest = _init_rest(application, _mock_auth_mfa_rest_response_timeout) auth = Auth(rest) auth_instance = AuthByDefault(password) auth.authenticate(auth_instance, account, user, timeout=1) assert rest._connection.errorhandler.called # error
def test_auth_mfa(next_action: str): """Authentication by MFA.""" global mock_cnt application = "testapplication" account = "testaccount" user = "******" password = "******" # success test case mock_cnt = 0 rest = _init_rest(application, _create_mock_auth_mfs_rest_response(next_action)) auth = Auth(rest) auth_instance = AuthByDefault(password) auth.authenticate(auth_instance, account, user) assert not rest._connection.errorhandler.called # not error assert rest.token == "TOKEN" assert rest.master_token == "MASTER_TOKEN" # failure test case mock_cnt = 0 rest = _init_rest(application, _mock_auth_mfa_rest_response_failure) auth = Auth(rest) auth_instance = AuthByDefault(password) auth.authenticate(auth_instance, account, user) assert rest._connection.errorhandler.called # error # timeout 1 second mock_cnt = 0 rest = _init_rest(application, _mock_auth_mfa_rest_response_timeout) auth = Auth(rest) auth_instance = AuthByDefault(password) auth.authenticate(auth_instance, account, user, timeout=1) assert rest._connection.errorhandler.called # error