Skip to content
Next Next commit
allow feast snowflake to read in byte string for private-key authenti…
…cation

Signed-off-by: Artur <artur.kolakowski@medely.com>
  • Loading branch information
arturkolakowski committed Aug 5, 2024
commit be3ecb2e324fe7122c75d5eb5ffb40e5bd91b878
4 changes: 2 additions & 2 deletions sdk/python/feast/infra/materialization/snowflake_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class SnowflakeMaterializationEngineConfig(FeastConfigBaseModel):
authenticator: Optional[str] = None
""" Snowflake authenticator name """

private_key: Optional[str] = None
""" Snowflake private key file path"""
private_key: Optional[str | bytes] = None
""" Snowflake private key stored as bytes or file path"""

private_key_passphrase: Optional[str] = None
""" Snowflake private key file passphrase"""
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/feast/infra/offline_stores/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class SnowflakeOfflineStoreConfig(FeastConfigBaseModel):
authenticator: Optional[str] = None
""" Snowflake authenticator name """

private_key: Optional[str] = None
""" Snowflake private key file path"""
private_key: Optional[str | bytes] = None
""" Snowflake private key stored as bytes or file path"""

private_key_passphrase: Optional[str] = None
""" Snowflake private key file passphrase"""
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/feast/infra/online_stores/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class SnowflakeOnlineStoreConfig(FeastConfigBaseModel):
authenticator: Optional[str] = None
""" Snowflake authenticator name """

private_key: Optional[str] = None
""" Snowflake private key file path"""
private_key: Optional[str | bytes] = None
""" Snowflake private key stored as bytes or file path"""

private_key_passphrase: Optional[str] = None
""" Snowflake private key file passphrase"""
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/feast/infra/registry/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class SnowflakeRegistryConfig(RegistryConfig):
authenticator: Optional[str] = None
""" Snowflake authenticator name """

private_key: Optional[str] = None
""" Snowflake private key file path"""
private_key: Optional[str | bytes] = None
""" Snowflake private key stored as bytes or file path"""

private_key_passphrase: Optional[str] = None
""" Snowflake private key file passphrase"""
Expand Down
20 changes: 16 additions & 4 deletions sdk/python/feast/infra/utils/snowflake/snowflake_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from logging import getLogger
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, Dict, Iterator, List, Optional, Tuple, cast
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union, cast

import pandas as pd
import pyarrow
Expand Down Expand Up @@ -510,13 +510,25 @@ def chunk_helper(lst: pd.DataFrame, n: int) -> Iterator[Tuple[int, pd.DataFrame]
yield int(i / n), lst[i : i + n]


def parse_private_key_path(key_path: str, private_key_passphrase: str) -> bytes:
with open(key_path, "rb") as key:
def parse_private_key_path(
private_key: Union[bytes | str], private_key_passphrase: str
) -> bytes:
"""Returns snowflake pkb by parsing and reading either from private key path or as byte string."""
if isinstance(private_key, str):
with open(private_key, "rb") as key:
p_key = serialization.load_pem_private_key(
key.read(),
password=private_key_passphrase.encode(),
backend=default_backend(),
)
elif isinstance(private_key, bytes):
p_key = serialization.load_pem_private_key(
key.read(),
private_key,
password=private_key_passphrase.encode(),
backend=default_backend(),
)
else:
raise ValueError("private_key must be either type of str or bytes.")

pkb = p_key.private_bytes(
encoding=serialization.Encoding.DER,
Expand Down