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

Document how to authenticate with a private key #72

Closed
fhoffa opened this issue May 16, 2023 · 1 comment
Closed

Document how to authenticate with a private key #72

fhoffa opened this issue May 16, 2023 · 1 comment

Comments

@fhoffa
Copy link

fhoffa commented May 16, 2023

I see @sfc-gh-hachouraria is adding some examples to OPTIONS for authentication:

#66

As we find the best way to include this code, I wanted to document a hack to authenticate with a private key in the meantime.

In settings.py I just add an ugly block to decrypt the key:

#### --Fh
import os
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import serialization

with open("./rsa_key.p8", "rb") as key:
    p_key= serialization.load_pem_private_key(
        key.read(),
        password=None,
        backend=default_backend()
    )

pkb = p_key.private_bytes(
    encoding=serialization.Encoding.DER,
    format=serialization.PrivateFormat.PKCS8,
    encryption_algorithm=serialization.NoEncryption())
### --Fh

So then in DATABASES in settings.py I can use that pkb value:

DATABASES = {
    'default': {
        'ENGINE': 'django_snowflake',
        'NAME': 'DJANGO',
        'SCHEMA': 'PUBLIC',
        'WAREHOUSE': 'S',
        'USER': 'DJANGO',
        'PASSWORD': '#',
        'ACCOUNT': 'my_account',
        # Include 'OPTIONS' if you need to specify any other
        # snowflake.connector.connect() parameters.
        # https://docs.snowflake.com/en/user-guide/python-connector-api.html#connect
        'OPTIONS': {
            'private_key': pkb,
        }
    }
}
@sfc-gh-hachouraria
Copy link
Contributor

sfc-gh-hachouraria commented May 16, 2023

There's not a simpler way (other than reading the private key bytes (pkb) yourself) because the python connector does not accept a file path as an input yet. See also a related issue discussion on: #67

Feature request raised at: snowflakedb/snowflake-connector-python#1565

@timgraham timgraham changed the title Authentication with private keys? Document how to authenticate with a private key May 16, 2023
timgraham pushed a commit to sfc-gh-hachouraria/django-snowflake that referenced this issue May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants