Created
June 8, 2019 07:00
-
-
Save hsuyuming/cd6797a543f33f31972ef2e888d84caf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import snowflake connector module | |
import snowflake.connector | |
import sys | |
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 | |
# 確定輸入的參數是否給定正確 | |
# Sample: python connect_keypair.py <account> <user> <role> <warehouse> <database> <schema> | |
if len(sys.argv) < 7 : | |
print("ERROR: Please pass the following command-line parameters in order:",end='\n') | |
print("account,user,role,warehouse,database,schema.") | |
sys.exit(-1) | |
else: | |
ACCOUNT = sys.argv[1] | |
USER = sys.argv[2] | |
ROLE = sys.argv[3] | |
WAREHOUSE = sys.argv[4] | |
DATABASE = sys.argv[5] | |
SCHEMA = sys.argv[6] | |
with open("/Users/abehsu/Documents/Snowflake/Snowpipe_poc/rsa_key.p8", "rb") as key: | |
p_key = serialization.load_pem_private_key( | |
key.read(), | |
password = os.environ['PRIVATE_KEY_PASSPHRASE'].encode(), | |
backend=default_backend() | |
) | |
pkb = p_key.private_bytes( | |
encoding=serialization.Encoding.DER, | |
format=serialization.PrivateFormat.PKCS8, | |
encryption_algorithm=serialization.NoEncryption() | |
) | |
con = snowflake.connector.connect( | |
account=ACCOUNT, | |
user=USER, | |
role=ROLE, | |
warehouse=WAREHOUSE, | |
database=DATABASE, | |
schema=SCHEMA, | |
private_key=pkb | |
) | |
results = con.cursor().execute("SELECT * FROM LIBRARY LIMIT 1") | |
for result in results: | |
print(result[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment