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

Increase security #3

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions target_snowflake/db_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from target_snowflake.upload_clients.s3_upload_client import S3UploadClient
from target_snowflake.upload_clients.snowflake_upload_client import SnowflakeUploadClient

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


def validate_config(config):
"""Validate configuration"""
Expand All @@ -33,7 +36,6 @@ def validate_config(config):
'account',
'dbname',
'user',
'password',
'warehouse',
'file_format'
]
Expand Down Expand Up @@ -285,6 +287,25 @@ def __init__(self, connection_config, stream_schema_message=None, table_cache=No
else:
self.upload_client = SnowflakeUploadClient(connection_config, self)


def load_private_key(self):
if 'private_key' not in self.connection_config:
return

private_key = serialization.load_pem_private_key(
self.connection_config['private_key'].encode(),
password=self.connection_config.get('private_key_password'),
backend=default_backend()
)

bytes = private_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)

return bytes

def open_connection(self):
"""Open snowflake connection"""
stream = None
Expand All @@ -293,13 +314,13 @@ def open_connection(self):

return snowflake.connector.connect(
user=self.connection_config['user'],
password=self.connection_config['password'],
private_key=self.connection_config.get('private_key', None),
private_key_file_pwd=self.connection_config.get('private_key_password', None),
password=self.connection_config.get('password'),
private_key=self.load_private_key(),
authenticator=self.connection_config.get('authenticator'),
account=self.connection_config['account'],
database=self.connection_config['dbname'],
warehouse=self.connection_config['warehouse'],
role=self.connection_config.get('role', None),
role=self.connection_config.get('role'),
autocommit=True,
session_parameters={
# Quoted identifiers should be case sensitive
Expand Down
Loading