"""Parse keyfiles for snowflake ssh access.""" from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization def get_private_key(key: str, passphrase: str) -> bytes: """Get the private key from the specified path.""" p_key = serialization.load_pem_private_key( key.encode(), password=passphrase.encode(), backend=default_backend() ) return p_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption(), ) def get_private_key_from_file(key_path: str, passphrase: str) -> bytes: """Get the private key from the specified path.""" with open(key_path, "rb") as key_file: key_file_contents = key_file.read().decode() return get_private_key(key_file_contents, passphrase)