Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 800aa1b

Browse files
authored
Merge pull request #715 from ivan-toriya/bigquery-dbt-impersonation
Bigquery dbt impersonation
2 parents 43d241f + 8055000 commit 800aa1b

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

data_diff/databases/bigquery.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def import_bigquery_service_account():
5353
return service_account
5454

5555

56+
def import_bigquery_service_account_impersonation():
57+
from google.auth import impersonated_credentials
58+
59+
return impersonated_credentials
60+
61+
5662
@attrs.define(frozen=False)
5763
class Mixin_MD5(AbstractMixin_MD5):
5864
def md5_as_int(self, s: str) -> str:
@@ -248,6 +254,13 @@ def __init__(self, project, *, dataset, bigquery_credentials=None, **kw):
248254
keyfile,
249255
scopes=["https://www.googleapis.com/auth/cloud-platform"],
250256
)
257+
elif kw.get("impersonate_service_account"):
258+
bigquery_service_account_impersonation = import_bigquery_service_account_impersonation()
259+
credentials = bigquery_service_account_impersonation.Credentials(
260+
source_credentials=credentials,
261+
target_principal=kw["impersonate_service_account"],
262+
target_scopes=["https://www.googleapis.com/auth/cloud-platform"],
263+
)
251264

252265
self._client = bigquery.Client(project=project, credentials=credentials, **kw)
253266
self.project = project

data_diff/dbt_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def set_connection(self):
378378
"driver": conn_type,
379379
"project": credentials.get("project") or credentials.get("database"),
380380
"dataset": credentials.get("dataset") or credentials.get("schema"),
381+
"impersonate_service_account": credentials.get("impersonate_service_account"),
381382
}
382383

383384
self.threads = credentials.get("threads")

tests/test_dbt_parser.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,28 @@ def test_set_connection_bigquery_oauth(self):
269269
self.assertEqual(mock_self.connection.get("project"), expected_credentials["project"])
270270
self.assertEqual(mock_self.connection.get("dataset"), expected_credentials["dataset"])
271271

272+
def test_set_connection_bigquery_oauth_sa_impersonation(self):
273+
expected_driver = "bigquery"
274+
expected_credentials = {
275+
"method": "oauth",
276+
"project": "a_project",
277+
"dataset": "a_dataset",
278+
"impersonate_service_account": "a_service_account@yourproject.iam.gserviceaccount.com",
279+
}
280+
mock_self = Mock()
281+
mock_self.get_connection_creds.return_value = (expected_credentials, expected_driver)
282+
283+
DbtParser.set_connection(mock_self)
284+
285+
self.assertIsInstance(mock_self.connection, dict)
286+
self.assertEqual(mock_self.connection.get("driver"), expected_driver)
287+
self.assertEqual(mock_self.connection.get("project"), expected_credentials["project"])
288+
self.assertEqual(mock_self.connection.get("dataset"), expected_credentials["dataset"])
289+
self.assertEqual(
290+
mock_self.connection.get("impersonate_service_account"),
291+
expected_credentials["impersonate_service_account"],
292+
)
293+
272294
def test_set_connection_bigquery_svc_account(self):
273295
expected_driver = "bigquery"
274296
expected_credentials = {

0 commit comments

Comments
 (0)