Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to add Snowflake connection using DVT command with RSA key. #1385

Open
shubhamTeli opened this issue Jan 3, 2025 · 5 comments
Open
Labels
type: question Request for information or clarification. Not an issue.

Comments

@shubhamTeli
Copy link

shubhamTeli commented Jan 3, 2025

i want setup a connection of Snowflake DB using RSA key as don't want to use password for security purpose.

image

Can anyone suggest how can i setup the snowflake connection?
As i did not find the RSA key option in DVT connection guide.

@helensilva14 helensilva14 added the type: question Request for information or clarification. Not an issue. label Jan 4, 2025
@shubhamTeli
Copy link
Author

@helensilva14 Any update on above question?

@sundar-mudupalli-work
Copy link
Collaborator

Shubham,

DVT uses ibis and ibis may provide a way to connect with a private key. The documentation is here. Assuming you are comfortable using Python, pandas and installing the ibis library, could you test if you can connect to snowflake using ibis. The python code would be something like this

import ibis
con=ibis.snowflake.connect( ....
     - as described in the link -
)
results=con.sql('some sql query').execute()
print(results)

If you are successful with this, we may be able some changes to DVT to allow authenticating to Snowflake with private keys. If you are a GCP customer or working with a GCP customer, can you ask your GCP account rep to reach out to me internally - this will help us prioritize your request.

Thank you.

Sundar Mudupalli

@shubhamTeli
Copy link
Author

Thanks @sundar-mudupalli-work for reply.

Tried to connect Snowflake using ibis with below option from documentation https://ibis-project.org/backends/snowflake#authenticating-with-key-pair-authentication

image

However, getting below error:

image

Please find below code which i have tried.

import ibis
from ibis import snowflake
import os

private_key_path = r'rsa_key.der'

with open(private_key_path, 'rb') as key:
    p_key = key.read()

print(type(p_key))

connection_details = {
    'user': '<my_user>',
    'account': '<my_account>',
    'database': '<my_database>/<my_schema>',
    'warehouse': '<my_warehouse>',
    'private_key': p_key,  # Directly pass the private key as bytes
    'password': ''  # Leave this empty if using private key authentication
}

try:
    con = snowflake.connect(**connection_details)
    print("Connection successful")
    table = con.table("<my_table>")
    result = table.limit(3).execute()
    print(table.schema)
    print(result)
    print(type(result))

except Exception as e:
    print("Error connecting to Snowflake:", e)

Can you please help here?

@sundar-mudupalli-work
Copy link
Collaborator

Hi,

You may need to provide the RSAPrivateKey, this example from the doc seems to create an object of the type cryptography.hazmat.bindings._rust.openssl.rsa.RSAPrivateKey.

Try that out.

Thanks.

Sundar Mudupalli

@shubhamTeli
Copy link
Author

shubhamTeli commented Jan 9, 2025

hi @sundar-mudupalli-work

i tried the option which you suggested. However still getting below error.

image

Please find below code which i have tried.

import ibis
from ibis import snowflake

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend

# Load the private key from a file
private_key_path = 'rsa_key.pem'
with open(private_key_path, 'rb') as key_file:
    private_key_data = key_file.read()

# Convert the private key data to an RSAPrivateKey object
p_key = serialization.load_pem_private_key(
    private_key_data,
    password=None,  # If your key is encrypted, provide the password here
    backend=default_backend()
)

print(type(p_key))

# Define your Snowflake connection parameters
connection_details = {
    'user': '<my_user>',
    'account': '<my_account>',
    'database': '<my_database>/<my_schema>',
    'warehouse': '<my_warehouse>',
    'private_key': p_key,  # Directly pass the private key as RSAPrivateKey
    'password': ''  # Leave this empty if using private key authentication
}

try:
    # Create a connection to Snowflake
    con = snowflake.connect(**connection_details)
    print("Connection successful")
    table = con.table("adh_com_globe_dh_ds_customerdataset_vw")
    result = table.limit(3).execute()
    print(table.schema)
    print(result)
    print(type(result))

except Exception as e:
    print("Error connecting to Snowflake:", e)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants