Skip to content

Commit cb80c6b

Browse files
author
Marcel Coetzee
committed
Add HMAC credentials and update Clickhouse configuration
Signed-off-by: Marcel Coetzee <[email protected]>
1 parent 4c6f928 commit cb80c6b

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

dlt/common/configuration/specs/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .api_credentials import OAuth2Credentials
2222
from .aws_credentials import AwsCredentials, AwsCredentialsWithoutDefaults
2323
from .azure_credentials import AzureCredentials, AzureCredentialsWithoutDefaults
24+
from .hmac_credentials import HMACCredentials
2425

2526

2627
# backward compatibility for service account credentials
@@ -53,4 +54,5 @@
5354
"AzureCredentialsWithoutDefaults",
5455
"GcpClientCredentials",
5556
"GcpClientCredentialsWithDefault",
56-
]
57+
"HMACCredentials",
58+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Optional, Dict
2+
3+
from dlt.common.configuration.specs import (
4+
CredentialsConfiguration,
5+
configspec,
6+
)
7+
from dlt.common.typing import TSecretStrValue, DictStrAny
8+
9+
10+
@configspec
11+
class HMACCredentials(CredentialsConfiguration):
12+
aws_access_key_id: str = None
13+
aws_secret_access_key: TSecretStrValue = None
14+
aws_session_token: Optional[TSecretStrValue] = None
15+
profile_name: Optional[str] = None
16+
region_name: Optional[str] = None
17+
endpoint_url: Optional[str] = None
18+
19+
def to_s3fs_credentials(self) -> Dict[str, Optional[str]]:
20+
"""Dict of keyword arguments that can be passed to s3fs"""
21+
credentials: DictStrAny = dict(
22+
key=self.aws_access_key_id,
23+
secret=self.aws_secret_access_key,
24+
token=self.aws_session_token,
25+
profile=self.profile_name,
26+
endpoint_url=self.endpoint_url,
27+
)
28+
if self.region_name:
29+
credentials["client_kwargs"] = {"region_name": self.region_name}
30+
return credentials
31+
32+
def to_native_representation(self) -> Dict[str, Optional[str]]:
33+
"""Return a dict that can be passed as kwargs to boto3 session"""
34+
return dict(self)
35+
36+
def to_session_credentials(self) -> Dict[str, str]:
37+
return dict(
38+
aws_access_key_id=self.aws_access_key_id,
39+
aws_secret_access_key=self.aws_secret_access_key,
40+
aws_session_token=self.aws_session_token,
41+
)

dlt/destinations/impl/clickhouse/clickhouse.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
import clickhouse_connect
88
from clickhouse_connect.driver.tools import insert_file
99

10-
import dlt
1110
from dlt import config
1211
from dlt.common.configuration.specs import (
1312
CredentialsConfiguration,
1413
AzureCredentialsWithoutDefaults,
1514
GcpCredentials,
1615
AwsCredentialsWithoutDefaults,
16+
HMACCredentials,
1717
)
18-
from dlt.destinations.exceptions import DestinationTransientException
1918
from dlt.common.destination import DestinationCapabilitiesContext
2019
from dlt.common.destination.reference import (
2120
SupportsStagingDestination,
@@ -34,7 +33,7 @@
3433
TColumnSchemaBase,
3534
)
3635
from dlt.common.storages import FileStorage
37-
from dlt.destinations.exceptions import LoadJobTerminalException
36+
from dlt.destinations.exceptions import DestinationTransientException, LoadJobTerminalException
3837
from dlt.destinations.impl.clickhouse import capabilities
3938
from dlt.destinations.impl.clickhouse.clickhouse_adapter import (
4039
TTableEngineType,
@@ -209,13 +208,15 @@ def __init__(
209208
access_key_id = staging_credentials.aws_access_key_id
210209
secret_access_key = staging_credentials.aws_secret_access_key
211210
elif isinstance(staging_credentials, GcpCredentials):
212-
access_key_id = client.credentials.gcp_access_key_id
213-
secret_access_key = client.credentials.gcp_secret_access_key
211+
# Defer to the provided HMAC Credentials.
212+
access_key_id = client.credentials.gcp_access_key_id # type: ignore
213+
secret_access_key = client.credentials.gcp_secret_access_key # type: ignore
214214
if not access_key_id or not secret_access_key:
215215
raise DestinationTransientException(
216-
"You have tried loading from gcs with clickhouse. Please provide valid"
217-
" 'gcp_access_key_id' and 'gcp_secret_access_key' to connect to gcs as"
218-
" outlined in the dlthub docs."
216+
"You have tried loading from gcs with clickhouse. "
217+
"Please provide valid HMAC credentials as outlined in "
218+
"the dlthub docs:\n"
219+
"https://dlthub.com/devel/dlt-ecosystem/destinations/clickhouse#using-google-cloud-storage-as-a-staging-area."
219220
)
220221

221222
auth = "NOSIGN"

dlt/destinations/impl/clickhouse/configuration.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ class ClickHouseCredentials(ConnectionStringCredentials):
3838
"""Separator for dataset table names, defaults to '___', i.e. 'database.dataset___table'."""
3939
dataset_sentinel_table_name: str = "dlt_sentinel_table"
4040
"""Special table to mark dataset as existing"""
41-
gcp_access_key_id: Optional[str] = None
42-
"""When loading from a gcp bucket, you need to provide gcp interoperable keys"""
43-
gcp_secret_access_key: Optional[str] = None
44-
"""When loading from a gcp bucket, you need to provide gcp interoperable keys"""
4541

4642
__config_gen_annotations__: ClassVar[List[str]] = [
4743
"host",

0 commit comments

Comments
 (0)