snowflakedb / snowflake-connector-python

Snowflake Connector for Python
https://pypi.python.org/pypi/snowflake-connector-python/
Apache License 2.0
601 stars 473 forks source link

SNOW-1648412: Unable to connect to my warehouse inside snowpark container service #2042

Closed punitchauhan771 closed 2 months ago

punitchauhan771 commented 2 months ago

Hi, I am trying to connect to my warehouse inside my spcs container but it is giving me this error:


Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/connection.py", line 1414, in _authenticate
    auth.authenticate(
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/auth/_auth.py", line 250, in authenticate
    ret = self._rest._post_request(
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/network.py", line 739, in _post_request
    ret = self.fetch(
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/network.py", line 854, in fetch
    ret = self._request_exec_wrapper(
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/network.py", line 971, in _request_exec_wrapper
    raise e
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/network.py", line 896, in _request_exec_wrapper
    return_object = self._request_exec(
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/network.py", line 1156, in _request_exec
    raise err
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/network.py", line 1100, in _request_exec
    raise OperationalError(

The above exception (251012: 251012: Login request is retryable. Will be handled by authenticator) was the direct cause of the following exception:
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 263, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.11/site-packages/django_snowflake/base.py", line 154, in get_new_connection
    return Database.connect(**conn_params)
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/__init__.py", line 55, in Connect
    return SnowflakeConnection(**kwargs)
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/connection.py", line 456, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/connection.py", line 771, in connect
    self.__open_connection()
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/connection.py", line 1099, in __open_connection
    self.authenticate_with_retry(self.auth_class)
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/connection.py", line 1386, in authenticate_with_retry
    self._authenticate(auth_instance)
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/connection.py", line 1458, in _authenticate
    raise auth_op from e
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/connection.py", line 1435, in _authenticate
    auth_instance.handle_timeout(
  File "/usr/local/lib/python3.11/site-packages/snowflake/connector/auth/by_plugin.py", line 212, in handle_timeout
    raise error
sfc-gh-dszmolka commented 2 months ago

hello - unfortunately the actual issue is not included in the log snippet you shared. It might be even due to a simple misconfiguration (incorrect account name/format used, etc) or something else.

Could you please

  1. enable debug level logging and try to connect again
  2. collect the logs, sanitize it to make sure no personal information is in it, and share for analysis ?

I also recommend perhaps giving https://github.com/Snowflake-Labs/sf-samples/tree/main/samples/spcs/sf-connection/python a quick look; it should demonstrate how to connect with PythonConnector in SPCS, perhaps it gives some idea for the implementation.

punitchauhan771 commented 2 months ago

Hi, Thank you for your suggestions, after enabling logging, I tried to connect to snowflake warehouse both in my local environment and spcs, logs for each of them are below: SPCS logs: image

and my local environment logs: image

In my local environment I am able to connect to my snowflake db but in spcs it's failing to connect, also I haven't cleaned any logs in SPCS, only this much log was generated,

this is the code that I am using to connect (db config)

  import snowflake.connector as Database

  def get_connection_params(self):
        settings_dict = self.settings_dict
        conn_params = {
            'interpolate_empty_sequences':  True,
            **settings_dict['OPTIONS'],
        }
        if os.environ.get('RUNNING_DJANGOS_TEST_SUITE') != 'true':
            conn_params['application'] = 'Django_SnowflakeConnector_%s' % __version__

        if use_token := os.path.exists("/snowflake/session/token"):
            conn_params["token"] = self.get_login_token()

        if settings_dict['NAME']:
            conn_params['database'] = settings_dict['NAME']

         # Omit USER/PASSWORD if using token.
        if not use_token:
            if settings_dict['USER']:
                conn_params['user'] = settings_dict['USER']
            else:
                raise ImproperlyConfigured(self.settings_is_missing % 'USER')

        if settings_dict['PASSWORD']:
            conn_params['password'] = settings_dict['PASSWORD']
        elif 'authenticator' not in conn_params:
            raise ImproperlyConfigured(self.settings_is_missing % 'PASSWORD')

        if settings_dict.get('ACCOUNT'):
            conn_params['account'] = settings_dict['ACCOUNT']
        else:
            raise ImproperlyConfigured(self.settings_is_missing % 'ACCOUNT')

        if settings_dict.get('WAREHOUSE'):
            conn_params['warehouse'] = settings_dict['WAREHOUSE']
        else:
            raise ImproperlyConfigured(self.settings_is_missing % 'WAREHOUSE')

        if settings_dict.get('SCHEMA'):
            conn_params['schema'] = settings_dict['SCHEMA']
        else:
            raise ImproperlyConfigured(self.settings_is_missing % 'SCHEMA')

        return conn_params

    @async_unsafe
    def get_new_connection(self, conn_params):
        try:
           Database.connect(**conn_params)
        except Exception as e:
            print(conn_params)
            print(e)
            raise Exception(e)

##SPCS connectivity
DATABASES = {
'default': {
    'ENGINE': '**',
    'ACCOUNT': os.getenv("SNOWFLAKE_ACCOUNT"),
        'SCHEMA': 'DBO',
        'WAREHOUSE':os.getenv("SNOWFLAKE_WAREHOUSE","RANDOM_WAREHOUSE_NAME"),
        'NAME': os.getenv("SNOWFLAKE_DATABASE"),
        'PORT': '',
        'CONN_MAX_AGE': None,
        'HOST' : os.getenv('SNOWFLAKE_HOST'),
        'OPTIONS': {
            "token" : open('/snowflake/session/token', 'r').read(),
            "authenticator" : "oauth",
            'client_session_keep_alive': True,
            'protocol': "https"
        },
    },
}

##Local Env Connectivity
DATABASES = {
'default': {
    'ENGINE': '**',
    'ACCOUNT': '<AccountName>',
        'SCHEMA': 'DBO',
        'WAREHOUSE': str(config['APP']['warehouse']),
        'USER': str(config['APP']['username']),
        'PASSWORD': str(config['APP']['password']),
        'NAME': str(config['APP']['database_name']),
        'PORT': '',
        'CONN_MAX_AGE': None,
        'HOST': str(config['APP']['server']),
        'OPTIONS': {
            "role" : "ACCOUNTADMIN",
            'client_session_keep_alive': True
        },
    },
}

additional error that I see is : It's trying to connect to: "HTTPSConnectionPool(host='<url>.snowflakecomputing.com', port=443)"

punitchauhan771 commented 2 months ago

Hi,

After further debugging, I discovered that the issue was due to a typo in my code, which caused the hostname to be skipped during the connection process. I’ve corrected the mistake. Apologies for any inconvenience this may have caused.

sfc-gh-dszmolka commented 2 months ago

no problem at all, glad to hear you discovered this typo and are now unblocked 👍