Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Add native support for authentication through Azure DevOps OIDC

### Bug Fixes
- Improving the error message that is shown when the unsupported `dbutils.credentials.getServiceCredentialsProvider` method is used. This method can only be used inside of a notebook.

### Documentation

Expand Down
17 changes: 17 additions & 0 deletions databricks/sdk/dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,17 @@ def get_local_notebook_path():
return value


def not_supported_method_err_msg(methodName):
return f"Method '{methodName}' is not supported in the SDK version of DBUtils"


class _OverrideProxyUtil:

@classmethod
def new(cls, path: str):
if path in cls.not_supported_override_paths:
raise ValueError(cls.not_supported_override_paths[path])

if len(cls.__get_matching_overrides(path)) > 0:
return _OverrideProxyUtil(path)
return None
Expand All @@ -297,6 +304,16 @@ def __init__(self, name: str):
"notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()": get_local_notebook_path,
}

# These paths work the same as 'proxy_override_paths' but instead of using a local implementation we raise an exception.
not_supported_override_paths = {
# The object returned by 'credentials.getServiceCredentialProvider()' can't be serialized to JSON.
# Without this override, the command would fail with an error 'TypeError: Object of type Session is not JSON serializable'.
# We override it to show a better error message
"credentials.getServiceCredentialsProvider": not_supported_method_err_msg(
"credentials.getServiceCredentialsProvider"
),
}

@classmethod
def __get_matching_overrides(cls, path: str):
return [x for x in cls.proxy_override_paths.keys() if x.startswith(path)]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,13 @@ def test_dbutils_proxy_overrides(dbutils, mocker, restorable_env):
return_value="test_cluster_id",
)
assert dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get() == "test_source_file"


@raises("Method 'credentials.getServiceCredentialsProvider' is not supported in the SDK version of DBUtils")
def test_dbutils_credentials_get_service_credential_provider(config, mocker):
from databricks.sdk.dbutils import RemoteDbUtils

config.cluster_id = "test_cluster_id"
dbutils = RemoteDbUtils(config)

dbutils.credentials.getServiceCredentialsProvider("creds")
Loading