Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-wang-1990 committed Oct 31, 2024
1 parent 5b8d938 commit 403e496
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 25 deletions.
94 changes: 71 additions & 23 deletions dbt/adapters/databricks/credentials.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from http import client
import itertools
import json
import os
Expand Down Expand Up @@ -142,10 +143,11 @@ def validate_creds(self) -> None:
raise DbtConfigError(
"The config '{}' is required to connect to Databricks".format(key)
)
if not self.token and self.auth_type != "external-browser":
raise DbtConfigError(
("The config `auth_type: oauth` is required when not using access token")
)

# if not self.token and self.auth_type != "external-browser":
# raise DbtConfigError(
# ("The config `auth_type: oauth` is required when not using access token")
# )

if not self.client_id and self.client_secret:
raise DbtConfigError(
Expand Down Expand Up @@ -289,32 +291,78 @@ def create_from(cls, credentials: DatabricksCredentials) -> "DatabricksCredentia
oauth_scopes=credentials.oauth_scopes or SCOPES,
auth_type=credentials.auth_type,
)

def authenticate_with_oauth_m2m(self):
return Config(
host=self.host,
client_id=self.client_id,
client_secret=self.client_secret,
auth_type="oauth-m2m"
)

def authenticate_with_external_browser(self):
return Config(
host=self.host,
client_id=self.client_id,
client_secret=self.client_secret,
auth_type="external-browser"
)

def authenticate_with_azure_client_secret(self):
return Config(
host=self.host,
azure_client_id=self.client_id,
azure_client_secret=self.client_secret,
auth_type="azure-client-secret"
)

def __post_init__(self) -> None:
if self.token:
self._config = Config(
host=self.host,
token=self.token,
)
else:
try:
self._config = Config(
host=self.host,
client_id=self.client_id,
client_secret=self.client_secret,
auth_type = self.auth_type
)
self.config.authenticate()
except Exception:
logger.warning(
"Failed to auth with client id and secret, trying azure_client_id, azure_client_secret"
)
# self._config = Config(
# host=self.host,
# azure_client_id=self.client_id,
# azure_client_secret=self.client_secret,
# )
# self.config.authenticate()
auth_methods = {
"oauth-m2m": self.authenticate_with_oauth_m2m,
"azure-client-secret": self.authenticate_with_azure_client_secret,
"external-browser": self.authenticate_with_external_browser
}

auth_type = (
"external-browser" if not self.client_secret
# if the client_secret starts with "dose" then it's likely using oauth-m2m
else "oauth-m2m" if self.client_secret.startswith("dose")
else "azure-client-secret"
)

if not self.client_secret:
auth_sequence = ["external-browser"]
elif self.client_secret.startswith("dose"):
auth_sequence = ["oauth-m2m", "azure-client-secret"]
else:
auth_sequence = ["azure-client-secret", "oauth-m2m"]

exceptions = []
for i, auth_type in enumerate(auth_sequence):
try:
self._config = auth_methods[auth_type]()
self._config.authenticate()
break # Exit loop if authentication is successful
except Exception as e:
exceptions.append((auth_type, e))
next_auth_type = auth_sequence[i + 1] if i + 1 < len(auth_sequence) else None
if next_auth_type:
logger.warning(
f"Failed to authenticate with {auth_type}, trying {next_auth_type} next. Error: {e}"
)
else:
logger.error(
f"Failed to authenticate with {auth_type}. No more authentication methods to try. Error: {e}"
)
raise Exception(
f"All authentication methods failed. Details: {exceptions}"
)


@property
def api_client(self) -> WorkspaceClient:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ dbt-spark~=1.8.0
dbt-core>=1.9.0b1, <2.0
dbt-common>=1.10.0, <2.0
dbt-adapters>=1.7.0, <2.0
databricks-sdk==0.29.0
databricks-sdk==0.36.0
keyring>=23.13.0
protobuf<5.0.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _get_plugin_version() -> str:
"dbt-adapters>=1.7.0, <2.0",
"dbt-common>=1.10.0, <2.0",
"databricks-sql-connector>=3.4.0, <3.5.0",
"databricks-sdk==0.29.0",
"databricks-sdk==0.36.0",
"keyring>=23.13.0",
"pandas<2.2.0",
"protobuf<5.0.0",
Expand Down

0 comments on commit 403e496

Please sign in to comment.