Skip to content

Commit ff329e7

Browse files
committed
tweaks and testing
1 parent 62c4c81 commit ff329e7

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

databricks/sdk/oauth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ def as_dict(self) -> dict:
368368
'client_id': self._client_id,
369369
}
370370

371+
@property
372+
def authorization_url(self) -> str:
373+
return self._authorization_url
374+
371375
@staticmethod
372376
def from_dict(raw: dict, client_secret: str = None) -> 'Consent':
373377
return Consent(raw['state'],

examples/external_browser_auth.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
from databricks.sdk import WorkspaceClient
2+
import argparse
23
import logging
4+
from typing import Optional
35

46
logging.basicConfig(level=logging.DEBUG)
57

68

7-
def run():
9+
def run(host: str, client_id: Optional[str], client_secret: Optional[str], azure_client_id: Optional[str], azure_client_secret: Optional[str]):
810
w = WorkspaceClient(
9-
host=input("Enter Databricks host: "),
11+
host=host,
12+
client_id=client_id,
13+
client_secret=client_secret,
14+
azure_client_id=azure_client_id,
15+
azure_client_secret=azure_client_secret,
1016
auth_type="external-browser",
1117
)
1218
me = w.current_user.me()
1319
print(me)
1420

1521

1622
if __name__ == "__main__":
17-
run()
23+
parser = argparse.ArgumentParser()
24+
parser.add_argument("--host", help="Databricks host", required=True)
25+
parser.add_argument("--client_id", help="Databricks client_id", default=None)
26+
parser.add_argument("--azure_client_id", help="Databricks azure_client_id", default=None)
27+
parser.add_argument("--client_secret", help="Databricks client_secret", default=None)
28+
parser.add_argument("--azure_client_secret", help="Databricks azure_client_secret", default=None)
29+
namespace = parser.parse_args()
30+
run(namespace.host, namespace.client_id, namespace.client_secret, namespace.azure_client_id, namespace.azure_client_secret)

examples/flask_app_with_oauth.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@
3232
import sys
3333

3434
from databricks.sdk.oauth import OAuthClient, OidcEndpoints, get_workspace_endpoints
35+
from databricks.sdk.service.compute import ListClustersFilterBy, State
3536

3637
APP_NAME = "flask-demo"
3738
all_clusters_template = """<ul>
38-
{% for cluster in w.clusters.list() -%}
39+
{% for cluster in clusters -%}
3940
<li><a
4041
target="_blank"
41-
href="{{ w.config.host }}/#setting/clusters/{{ cluster.cluster_id }}/configuration">
42+
href="{{ workspace_host }}/#setting/clusters/{{ cluster.cluster_id }}/configuration">
4243
{{ cluster.cluster_name }}</a> is {{ cluster.state }}</li>
4344
{% endfor %}
4445
</ul>"""
4546

4647

47-
def create_flask_app(host: str, oidc_endpoints: OidcEndpoints, client_id: str, client_secret: str, redirect_url: str):
48+
def create_flask_app(workspace_host: str, client_id: str, client_secret: str):
4849
"""The create_flask_app function creates a Flask app that is enabled with OAuth.
4950
5051
It initializes the app and web session secret keys with a randomly generated token. It defines two routes for
@@ -72,29 +73,34 @@ def callback():
7273
def index():
7374
"""The index page checks if the user has already authenticated and retrieves the user's credentials using
7475
the Databricks SDK WorkspaceClient. It then renders the template with the clusters' list."""
76+
oidc_endpoints = get_workspace_endpoints(workspace_host)
77+
port = request.environ.get("SERVER_PORT")
78+
redirect_url=f"http://localhost:{port}/callback"
7579
if "creds" not in session:
7680
oauth_client = OAuthClient(oidc_endpoints=oidc_endpoints,
7781
client_id=client_id,
7882
client_secret=client_secret,
7983
redirect_url=redirect_url)
8084
consent = oauth_client.initiate_consent()
8185
session["consent"] = consent.as_dict()
82-
return redirect(oidc_endpoints.authorization_endpoint)
86+
return redirect(consent.authorization_url)
8387

8488
from databricks.sdk import WorkspaceClient
8589
from databricks.sdk.oauth import SessionCredentials
8690

8791
credentials_strategy = SessionCredentials.from_dict(session["creds"],
88-
oidc_endpoints=oidc_endpoints,
92+
token_endpoint=oidc_endpoints.token_endpoint,
8993
client_id=client_id,
9094
client_secret=client_secret,
9195
redirect_url=redirect_url)
92-
workspace_client = WorkspaceClient(host=host,
96+
workspace_client = WorkspaceClient(host=workspace_host,
9397
product=APP_NAME,
9498
credentials_strategy=credentials_strategy,
9599
)
96-
97-
return render_template_string(all_clusters_template, w=workspace_client)
100+
clusters = workspace_client.clusters.list(
101+
filter_by=ListClustersFilterBy(cluster_states=[State.RUNNING, State.PENDING])
102+
)
103+
return render_template_string(all_clusters_template, workspace_host=workspace_host, clusters=clusters)
98104

99105
return app
100106

@@ -137,12 +143,10 @@ def parse_arguments() -> argparse.Namespace:
137143
logging.getLogger("databricks.sdk").setLevel(logging.DEBUG)
138144

139145
args = parse_arguments()
140-
oidc_endpoints = get_workspace_endpoints(args.host)
141146
client_id, client_secret = args.client_id, args.client_secret
142147
if not client_id:
143148
client_id, client_secret = register_custom_app(args)
144-
redirect_url=f"http://localhost:{args.port}/callback"
145-
app = create_flask_app(args.host, oidc_endpoints, client_id, client_secret, redirect_url)
149+
app = create_flask_app(args.host, client_id, client_secret)
146150

147151
app.run(
148152
host="localhost",

0 commit comments

Comments
 (0)