Skip to content

Commit

Permalink
Allow overriding of keycloak root credentials for 2024.11.1 upgrade…
Browse files Browse the repository at this point in the history
… path (#2843)

Co-authored-by: Adam Lewis <23342526+Adam-D-Lewis@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 13, 2024
1 parent 9d8d37a commit 31f44f3
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/_nebari/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import json
import logging
import os
import re
import secrets
import string
Expand Down Expand Up @@ -1297,11 +1298,38 @@ def _version_specific_upgrade(

urllib3.disable_warnings()

keycloak_admin = get_keycloak_admin(
server_url=f"https://{config['domain']}/auth/",
username="root",
password=config["security"]["keycloak"]["initial_root_password"],
keycloak_username = os.environ.get("KEYCLOAK_ADMIN_USERNAME", "root")
keycloak_password = os.environ.get(
"KEYCLOAK_ADMIN_PASSWORD",
config["security"]["keycloak"]["initial_root_password"],
)

try:
# Quick test to connect to Keycloak
keycloak_admin = get_keycloak_admin(
server_url=f"https://{config['domain']}/auth/",
username=keycloak_username,
password=keycloak_password,
)
except ValueError as e:
if "invalid_grant" in str(e):
rich.print(
textwrap.dedent(
"""
[red bold]Failed to connect to the Keycloak server.[/red bold]\n
[yellow]Please set the [bold]KEYCLOAK_ADMIN_USERNAME[/bold] and [bold]KEYCLOAK_ADMIN_PASSWORD[/bold]
environment variables with the Keycloak root credentials and try again.[/yellow]
"""
)
)
exit()
else:
# Handle other exceptions
rich.print(
f"[red bold]An unexpected error occurred: {repr(e)}[/red bold]"
)
exit()

# Get client ID as role is bound to the JupyterHub client
client_id = keycloak_admin.get_client_id("jupyterhub")
role_name = "legacy-group-directory-creation-role"
Expand Down

0 comments on commit 31f44f3

Please sign in to comment.