Skip to content

Commit 455a14c

Browse files
authored
Fixed Interactive OAuth on Azure & updated documentations (#669)
## Changes - Removed Azure check in interactive OAuth - Updated Flask with OAuth example - Updated OAuth documentation ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [x] `make fmt` applied - [ ] relevant integration tests applied
1 parent a6e3710 commit 455a14c

File tree

3 files changed

+10
-27
lines changed

3 files changed

+10
-27
lines changed

databricks/sdk/oauth.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,6 @@ def noop_credentials(_: any):
371371
config = Config(host=host, credentials_strategy=noop_credentials)
372372
if not scopes:
373373
scopes = ['all-apis']
374-
if config.is_azure:
375-
# Azure AD only supports full access to Azure Databricks.
376-
scopes = [f'{config.effective_azure_login_app_id}/user_impersonation', 'offline_access']
377374
oidc = config.oidc_endpoints
378375
if not oidc:
379376
raise ValueError(f'{host} does not support OAuth')
@@ -385,6 +382,7 @@ def noop_credentials(_: any):
385382
self.token_url = oidc.token_endpoint
386383
self.is_aws = config.is_aws
387384
self.is_azure = config.is_azure
385+
self.is_gcp = config.is_gcp
388386

389387
self._auth_url = oidc.authorization_endpoint
390388
self._scopes = scopes

docs/oauth.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ Databricks SDK for Python exposes the `oauth_client.initiate_consent()` helper t
245245
PKCE state verification. Application developers are expected to persist `RefreshableCredentials` in the webapp session
246246
and restore it via `RefreshableCredentials.from_dict(oauth_client, session['creds'])` helpers.
247247

248-
Works for both AWS and Azure. Not supported for GCP at the moment.
249-
250248
```python
251249
from databricks.sdk.oauth import OAuthClient
252250
oauth_client = OAuthClient(host='<workspace-url>',
@@ -305,10 +303,6 @@ account_client = AccountClient(host='https://accounts.cloud.databricks.com',
305303
account_id=input('Databricks Account ID: '),
306304
username=input('Username: '),
307305
password=getpass.getpass('Password: '))
308-
logging.info('Enrolling all published apps...')
309-
account_client.o_auth_enrollment.create(enable_all_published_apps=True)
310-
status = account_client.o_auth_enrollment.get()
311-
logging.info(f'Enrolled all published apps: {status}')
312306
custom_app = account_client.custom_app_integration.create(
313307
name='awesome-app',
314308
redirect_urls=[f'https://host.domain/path/to/callback'],

examples/flask_app_with_oauth.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
99
If you have already Custom App:
1010
11-
./flask_app_with_oauth.py <databricks workspace url> \
11+
./flask_app_with_oauth.py --host <databricks workspace url> \
1212
--client_id <app-client-id> \
1313
--client_secret <app-secret> \
1414
--port 5001
1515
1616
If you want this script to register Custom App and redirect URL for you:
1717
18-
./flask_app_with_oauth.py <databricks workspace url>
18+
./flask_app_with_oauth.py --port 5001 --profile <databricks account profile>
1919
2020
You'll get prompted for Databricks Account username and password for
2121
script to enroll your account into OAuth and create a custom app with
@@ -44,7 +44,7 @@
4444
</ul>"""
4545

4646

47-
def create_flask_app(oauth_client: OAuthClient, port: int):
47+
def create_flask_app(oauth_client: OAuthClient):
4848
"""The create_flask_app function creates a Flask app that is enabled with OAuth.
4949
5050
It initializes the app and web session secret keys with a randomly generated token. It defines two routes for
@@ -91,23 +91,13 @@ def index():
9191
return app
9292

9393

94-
def register_custom_app(oauth_client: OAuthClient, args: argparse.Namespace) -> tuple[str, str]:
94+
def register_custom_app(args: argparse.Namespace) -> tuple[str, str]:
9595
"""Creates new Custom OAuth App in Databricks Account"""
96-
if not oauth_client.is_aws:
97-
logging.error("Not supported for other clouds than AWS")
98-
sys.exit(2)
99-
10096
logging.info("No OAuth custom app client/secret provided, creating new app")
10197

102-
import getpass
103-
10498
from databricks.sdk import AccountClient
10599

106-
account_client = AccountClient(host="https://accounts.cloud.databricks.com",
107-
account_id=input("Databricks Account ID: "),
108-
username=input("Username: "),
109-
password=getpass.getpass("Password: "),
110-
)
100+
account_client = AccountClient(profile=args.profile)
111101

112102
custom_app = account_client.custom_app_integration.create(
113103
name=APP_NAME, redirect_urls=[f"http://localhost:{args.port}/callback"], confidential=True,
@@ -129,7 +119,7 @@ def init_oauth_config(args) -> OAuthClient:
129119
scopes=["all-apis"],
130120
)
131121
if not oauth_client.client_id:
132-
client_id, client_secret = register_custom_app(oauth_client, args)
122+
client_id, client_secret = register_custom_app(args)
133123
oauth_client.client_id = client_id
134124
oauth_client.client_secret = client_secret
135125

@@ -139,10 +129,11 @@ def init_oauth_config(args) -> OAuthClient:
139129
def parse_arguments() -> argparse.Namespace:
140130
"""Parses arguments for this demo"""
141131
parser = argparse.ArgumentParser(prog=APP_NAME, description=__doc__.strip())
142-
parser.add_argument("host")
132+
parser.add_argument("--host")
143133
for flag in ["client_id", "client_secret"]:
144134
parser.add_argument(f"--{flag}")
145135
parser.add_argument("--port", default=5001, type=int)
136+
parser.add_argument("--profile", default="DEFAULT", help="Databricks account profile to use for authentication.")
146137
return parser.parse_args()
147138

148139

@@ -155,7 +146,7 @@ def parse_arguments() -> argparse.Namespace:
155146

156147
args = parse_arguments()
157148
oauth_cfg = init_oauth_config(args)
158-
app = create_flask_app(oauth_cfg, args.port)
149+
app = create_flask_app(oauth_cfg)
159150

160151
app.run(
161152
host="localhost",

0 commit comments

Comments
 (0)