Skip to content

Commit

Permalink
Better examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Oct 11, 2024
1 parent ff329e7 commit d350434
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
35 changes: 34 additions & 1 deletion examples/external_browser_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,45 @@ def run(host: str, client_id: Optional[str], client_secret: Optional[str], azure
print(me)


def register_custom_app() -> tuple[str, str]:
"""Creates new Custom OAuth App in Databricks Account"""
logging.info("No OAuth custom app client/secret provided, creating new app")

from databricks.sdk import AccountClient

account_client = AccountClient()

custom_app = account_client.custom_app_integration.create(
name="external-browser-demo",
redirect_urls=[
f"http://localhost:8020",
],
confidential=True,
scopes=["all-apis"],
)
logging.info(f"Created new custom app: "
f"--client_id {custom_app.client_id} "
f"--client_secret {custom_app.client_secret}")

return custom_app.client_id, custom_app.client_secret


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--host", help="Databricks host", required=True)
parser.add_argument("--client_id", help="Databricks client_id", default=None)
parser.add_argument("--azure_client_id", help="Databricks azure_client_id", default=None)
parser.add_argument("--client_secret", help="Databricks client_secret", default=None)
parser.add_argument("--azure_client_secret", help="Databricks azure_client_secret", default=None)
parser.add_argument("--register-custom-app", action="store_true", help="Register a new custom app")
namespace = parser.parse_args()
run(namespace.host, namespace.client_id, namespace.client_secret, namespace.azure_client_id, namespace.azure_client_secret)
if namespace.register_custom_app and (namespace.client_id is not None or namespace.azure_client_id is not None):
raise ValueError("Cannot register custom app and provide --client_id/--azure_client_id at the same time")
if not namespace.register_custom_app and namespace.client_id is None and namespace.azure_client_secret is None:
raise ValueError("Must provide --client_id/--azure_client_id or register a custom app")
if namespace.register_custom_app:
client_id, client_secret = register_custom_app()
else:
client_id, client_secret = namespace.client_id, namespace.client_secret

run(namespace.host, client_id, client_secret, namespace.azure_client_id, namespace.azure_client_secret)
8 changes: 6 additions & 2 deletions examples/flask_app_with_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import logging
import sys

from databricks.sdk.oauth import OAuthClient, OidcEndpoints, get_workspace_endpoints
from databricks.sdk.oauth import OAuthClient, get_workspace_endpoints
from databricks.sdk.service.compute import ListClustersFilterBy, State

APP_NAME = "flask-demo"
Expand Down Expand Up @@ -114,7 +114,11 @@ def register_custom_app(args: argparse.Namespace) -> tuple[str, str]:
account_client = AccountClient(profile=args.profile)

custom_app = account_client.custom_app_integration.create(
name=APP_NAME, redirect_urls=[f"http://localhost:{args.port}/callback"], confidential=True,
name=APP_NAME,
redirect_urls=[
f"http://localhost:{args.port}/callback",
],
confidential=True,
scopes=["all-apis"],
)
logging.info(f"Created new custom app: "
Expand Down

0 comments on commit d350434

Please sign in to comment.