Skip to content

Commit 506979e

Browse files
committed
adding in global setting of aws access/secret keys
1 parent 1e9dac8 commit 506979e

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

dbt/adapters/clickhouse/impl.py

+10
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def s3source_clause(
251251
) -> str:
252252
s3config = self.config.vars.vars.get(config_name, {})
253253
s3config.update(s3_model_config)
254+
254255
structure = structure or s3config.get('structure', '')
255256
struct = ''
256257
if structure:
@@ -261,7 +262,9 @@ def s3source_clause(
261262
struct = f", '{','.join(structure)}'"
262263
else:
263264
struct = f",'{structure}'"
265+
264266
fmt = fmt or s3config.get('fmt')
267+
265268
bucket = bucket or s3config.get('bucket', '')
266269
path = path or s3config.get('path', '')
267270
url = bucket.replace('https://', '')
@@ -270,22 +273,29 @@ def s3source_clause(
270273
path = f'/{path}'
271274
url = f'{url}{path}'.replace('//', '/')
272275
url = f'https://{url}'
276+
273277
access = ''
278+
aws_access_key_id = aws_access_key_id or s3config.get('aws_access_key_id')
279+
aws_secret_access_key = aws_secret_access_key or s3config.get('aws_secret_access_key')
274280
if aws_access_key_id and not aws_secret_access_key:
275281
raise DbtRuntimeError('S3 aws_access_key_id specified without aws_secret_access_key')
276282
if aws_secret_access_key and not aws_access_key_id:
277283
raise DbtRuntimeError('S3 aws_secret_access_key specified without aws_access_key_id')
278284
if aws_access_key_id:
279285
access = f", '{aws_access_key_id}', '{aws_secret_access_key}'"
286+
280287
comp = compression or s3config.get('compression', '')
281288
if comp:
282289
comp = f"', {comp}'"
290+
283291
extra_credentials = ''
284292
role_arn = role_arn or s3config.get('role_arn')
285293
if role_arn:
286294
extra_credentials = f", extra_credentials(role_arn='{role_arn}')"
295+
287296
return f"s3('{url}'{access}, '{fmt}'{struct}{comp}{extra_credentials})"
288297

298+
289299
def check_schema_exists(self, database, schema):
290300
results = self.execute_macro(LIST_SCHEMAS_MACRO_NAME, kwargs={'database': database})
291301
return schema in (row[0] for row in results)

tests/integration/adapter/clickhouse/test_clickhouse_s3.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,38 @@ def test_read(self, project):
115115
assert result[0] == 1000
116116

117117

118+
class TestS3AwsAccessGlobal:
119+
@pytest.fixture(scope="class")
120+
def project_config_update(self):
121+
return {
122+
'vars': {
123+
'taxi_s3': {
124+
'bucket': 'https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/',
125+
'fmt': 'TabSeparatedWithNames',
126+
'aws_access_key_id': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
127+
'aws_secret_access_key': '1234567890123456789012345678901234567890',
128+
}
129+
}
130+
}
131+
132+
@pytest.fixture(scope="class")
133+
def models(self):
134+
return {
135+
"s3_taxis_source.sql": s3_taxis_full_source,
136+
"schema.yml": schema_yaml,
137+
}
138+
139+
def test_role_arn_in_compiled_sql(self, project):
140+
# Only compile, don't run
141+
result = run_dbt(["compile", "--select", "s3_taxis_source.sql"], expect_pass=True)
142+
143+
# Assert the SQL contains the expected role_arn function call
144+
assert (
145+
", 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '1234567890123456789012345678901234567890'"
146+
in result.results[0].node.compiled_code
147+
)
148+
149+
118150
class TestS3RoleArnGlobal:
119151
@pytest.fixture(scope="class")
120152
def project_config_update(self):
@@ -123,7 +155,7 @@ def project_config_update(self):
123155
'taxi_s3': {
124156
'bucket': 'https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/',
125157
'fmt': 'TabSeparatedWithNames',
126-
'role_arn': 'arn:aws:iam::123456789012:role/my-role'
158+
'role_arn': 'arn:aws:iam::123456789012:role/my-role',
127159
}
128160
}
129161
}
@@ -140,4 +172,7 @@ def test_role_arn_in_compiled_sql(self, project):
140172
result = run_dbt(["compile", "--select", "s3_taxis_source.sql"], expect_pass=True)
141173

142174
# Assert the SQL contains the expected role_arn function call
143-
assert "extra_credentials(role_arn='arn:aws:iam::123456789012:role/my-role')" in result.results[0].node.compiled_code
175+
assert (
176+
"extra_credentials(role_arn='arn:aws:iam::123456789012:role/my-role')"
177+
in result.results[0].node.compiled_code
178+
)

0 commit comments

Comments
 (0)