Skip to content

Commit

Permalink
Fix: clean up key-loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Skynet committed Oct 9, 2023
1 parent 4bb541b commit 1f12bd6
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions dlt/destinations/snowflake/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,21 @@ def _read_private_key(private_key: str, password: Optional[str] = None) -> bytes
except ModuleNotFoundError as e:
raise MissingDependencyException("SnowflakeCredentials with private key", dependencies=[f"{version.DLT_PKG_NAME}[snowflake]"]) from e

load_key_func = serialization.load_pem_private_key
private_key_bytes_decoded = private_key.encode()
try:
# decode base64 encoded private key
private_key_bytes_decoded = base64.b64decode(private_key)
load_key_func = serialization.load_der_private_key
except binascii.Error:
# cannot base64-decode private key -> assume it's been provided as plain text
pass
# load key from base64-encoded DER key
pkey = serialization.load_der_private_key(
base64.b64decode(private_key),
password=password.encode() if password is not None else None,
backend=default_backend(),
)
except Exception:
# loading base64-encoded DER key failed -> assume it's a plain-text PEM key
serialization.load_pem_private_key(
private_key.encode(),
password=password.encode() if password is not None else None,
backend=default_backend(),
)

pkey: PrivateKeyTypes = load_key_func(
private_key_bytes_decoded,
password=password.encode() if password is not None else None,
backend=default_backend(),
)

return pkey.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
Expand Down

0 comments on commit 1f12bd6

Please sign in to comment.