-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* trigger k8s tests * Try adding example-user to cypress test * full path to qhub-config.yaml * display keycloak connection in adduser * remove comments [skip ci]
- Loading branch information
Showing
5 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pathlib | ||
import logging | ||
from qhub.keycloak import do_keycloak | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_keycloak_subcommand(subparser): | ||
subparser = subparser.add_parser("keycloak") | ||
subparser.add_argument("-c", "--config", help="qhub configuration", required=True) | ||
subparser.add_argument( | ||
"keycloak_action", nargs="+", help="adduser <username> [password]" | ||
) | ||
subparser.set_defaults(func=handle_keycloak) | ||
|
||
|
||
def handle_keycloak(args): | ||
config_filename = pathlib.Path(args.config) | ||
if not config_filename.is_file(): | ||
raise ValueError( | ||
f"passed in configuration filename={config_filename} must exist" | ||
) | ||
|
||
do_keycloak(config_filename, *args.keycloak_action) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import logging | ||
import os | ||
|
||
import keycloak | ||
|
||
from .schema import verify | ||
from .utils import load_yaml | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def do_keycloak(config_filename, *args): | ||
|
||
if len(args) < 2: | ||
raise ValueError("keycloak command requires extra inputs") | ||
|
||
if args[0] != "adduser": | ||
raise ValueError( | ||
"Only keycloak command is 'keycloak adduser username [password]'" | ||
) | ||
|
||
config = load_yaml(config_filename) | ||
|
||
verify(config) | ||
|
||
keycloak_server_url = os.environ.get( | ||
"KEYCLOAK_SERVER_URL", f"https://{config['domain']}/auth/" | ||
) | ||
|
||
keycloak_username = os.environ.get("KEYCLOAK_ADMIN_USERNAME", "root") | ||
keycloak_password = os.environ.get( | ||
"KEYCLOAK_ADMIN_PASSWORD", | ||
config.get("security", {}).get("keycloak", {}).get("initial_root_password", ""), | ||
) | ||
|
||
should_verify_tls = config.get("certificate", {}).get("type", "") != "self-signed" | ||
|
||
try: | ||
keycloak_admin = keycloak.KeycloakAdmin( | ||
server_url=keycloak_server_url, | ||
username=keycloak_username, | ||
password=keycloak_password, | ||
realm_name=os.environ.get("KEYCLOAK_REALM", "qhub"), | ||
user_realm_name="master", | ||
auto_refresh_token=("get", "put", "post", "delete"), | ||
verify=should_verify_tls, | ||
) | ||
except ( | ||
keycloak.exceptions.KeycloakConnectionError, | ||
keycloak.exceptions.KeycloakAuthenticationError, | ||
) as e: | ||
raise ValueError(f"Failed to connect to Keycloak server: {e}") | ||
|
||
new_user_dict = {"username": args[1], "enabled": True} | ||
if len(args) >= 3: | ||
new_user_dict["credentials"] = [ | ||
{"type": "password", "value": args[2], "temporary": False} | ||
] | ||
else: | ||
print("Not setting any password (none supplied)") | ||
|
||
print(f"Adding user {args[1]}") | ||
keycloak_admin.create_user(new_user_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters