Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set necessary headers when authenticating via Azure CLI #290

Merged
merged 5 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions databricks/sdk/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ class AzureEnvironment:
resource_manager_endpoint="https://management.chinacloudapi.cn/",
active_directory_endpoint="https://login.chinacloudapi.cn/"),
)


def add_workspace_id_header(cfg: 'Config', headers: dict[str, str]) -> dict[str, str]:
if cfg.azure_workspace_resource_id:
headers["X-Databricks-Azure-Workspace-Resource-Id"] = cfg.azure_workspace_resource_id
return headers
9 changes: 4 additions & 5 deletions databricks/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from .azure import ARM_DATABRICKS_RESOURCE_ID, ENVIRONMENTS, AzureEnvironment
from .azure import ARM_DATABRICKS_RESOURCE_ID, ENVIRONMENTS, AzureEnvironment, add_workspace_id_header
from .oauth import (ClientCredentials, OAuthClient, OidcEndpoints, Refreshable,
Token, TokenCache, TokenSource)
from .version import __version__
Expand Down Expand Up @@ -210,9 +210,7 @@ def refreshed_headers() -> Dict[str, str]:
'Authorization': f"Bearer {inner.token().access_token}",
'X-Databricks-Azure-SP-Management-Token': cloud.token().access_token,
}
if cfg.azure_workspace_resource_id:
headers["X-Databricks-Azure-Workspace-Resource-Id"] = cfg.azure_workspace_resource_id
return headers
return add_workspace_id_header(cfg, headers)

return refreshed_headers

Expand Down Expand Up @@ -281,7 +279,8 @@ def azure_cli(cfg: 'Config') -> Optional[HeaderFactory]:

def inner() -> Dict[str, str]:
token = token_source.token()
return {'Authorization': f'{token.token_type} {token.access_token}'}
headers = {'Authorization': f'{token.token_type} {token.access_token}'}
return add_workspace_id_header(cfg, headers)

return inner

Expand Down
12 changes: 12 additions & 0 deletions tests/test_auth_manual_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from databricks.sdk.core import Config

from .conftest import __tests__

def test_azure_cli_workspace_header_present(monkeypatch):
monkeypatch.setenv('HOME', __tests__ + '/testdata/azure')
monkeypatch.setenv('PATH', __tests__ + '/testdata:/bin')
resource_id = '/subscriptions/123/resourceGroups/abc/providers/Microsoft.Databricks/workspaces/abc123'
cfg = Config(auth_type='azure-cli', host='x', azure_workspace_resource_id=resource_id)
assert 'X-Databricks-Azure-Workspace-Resource-Id' in cfg.authenticate()
assert cfg.authenticate()['X-Databricks-Azure-Workspace-Resource-Id'] == resource_id

Loading