"""Snoflake helper functions.""" from typing import Dict from typing import Optional from typing import Union from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from dbdeploy.base import config from dbdeploy.base import constants def get_sf_config(snowflake_account: str) -> Dict[str, Optional[Union[str, bytes]]]: """Return Snowflake config with credentials. Returns: dict: SF credentials """ snowflake_private_key = constants.PRIVATE_KEY_TEMPLATE.format(config.SNOWFLAKE_ACCOUNTS[snowflake_account].SNOWFLAKE_PRIVATE_KEY_STRING) # noqa: E501 p_key = serialization.load_pem_private_key( snowflake_private_key.encode(), password=str(config.SNOWFLAKE_ACCOUNTS[snowflake_account].SNOWFLAKE_KEY_PASSPHRASE).encode(), # noqa: E501 backend=default_backend(), ) private_key = p_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()) # Return Snowflake connection parameters including credentials return { 'role': config.SNOWFLAKE_ROLE, 'warehouse': config.SNOWFLAKE_WAREHOUSE, 'db': config.SNOWFLAKE_DATABASE, 'schema': config.SNOWFLAKE_SCHEMA, 'user': config.SNOWFLAKE_ACCOUNTS[snowflake_account].SNOWFLAKE_USER, 'account': config.SNOWFLAKE_ACCOUNTS[snowflake_account].SNOWFLAKE_ACCOUNT, 'private_key': private_key }