shaikshakeel commented on a change in pull request #6469: [AIRFLOW-5816] S3 to snowflake operator URL: https://github.com/apache/airflow/pull/6469#discussion_r364125960
########## File path: airflow/providers/snowflake/hooks/snowflake.py ########## @@ -0,0 +1,134 @@ +# -*- coding: utf-8 -*- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import snowflake.connector +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization + +from airflow.hooks.dbapi_hook import DbApiHook + + +class SnowflakeHook(DbApiHook): + """ + Interact with Snowflake. + + get_sqlalchemy_engine() depends on snowflake-sqlalchemy + + """ + + conn_name_attr = 'snowflake_conn_id' + default_conn_name = 'snowflake_default' + supports_autocommit = True + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.account = kwargs.pop("account", None) + self.warehouse = kwargs.pop("warehouse", None) + self.database = kwargs.pop("database", None) + self.region = kwargs.pop("region", None) + self.role = kwargs.pop("role", None) + self.schema = kwargs.pop("schema", None) + + def _get_conn_params(self): + """ + one method to fetch connection params as a dict + used in get_uri() and get_connection() + """ + conn = self.get_connection(self.snowflake_conn_id) + account = conn.extra_dejson.get('account', None) + warehouse = conn.extra_dejson.get('warehouse', None) + database = conn.extra_dejson.get('database', None) + region = conn.extra_dejson.get("region", None) + role = conn.extra_dejson.get('role', None) + + conn_config = { + "user": conn.login, + "password": conn.password or '', + "schema": self.schema or conn.schema or '', + "database": self.database or database or '', + "account": self.account or account or '', + "warehouse": self.warehouse or warehouse or '', + "region": self.region or region or '', + "role": self.role or role or '' + + } + + """ + If private_key_file is specified in the extra json, load the contents of the file as a private + key and specify that in the connection configuration. The connection password then becomes the + passphrase for the private key. If your private key file is not encrypted (not recommended), then + leave the password empty. + """ + private_key_file = conn.extra_dejson.get('private_key_file', None) + if private_key_file: + with open(private_key_file, "rb") as key: + passphrase = None + if conn.password: + passphrase = conn.password.strip().encode() + + p_key = serialization.load_pem_private_key( + key.read(), + password=passphrase, + backend=default_backend() + ) + + pkb = p_key.private_bytes(encoding=serialization.Encoding.DER, + format=serialization.PrivateFormat.PKCS8, + encryption_algorithm=serialization.NoEncryption()) + + conn_config['private_key'] = pkb + conn_config.pop('password', None) + + return conn_config + + def get_uri(self): + """ + override DbApiHook get_uri method for get_sqlalchemy_engine() + """ + conn_config = self._get_conn_params() + uri = 'snowflake://{user}:{password}@{account}/{database}/' + uri += '{schema}?warehouse={warehouse}&role={role}' Review comment: Done ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
