From 4a5f05694e6d704eb7eb4feb8b945b2e5dab50b9 Mon Sep 17 00:00:00 2001 From: Ben Hayes Date: Thu, 5 Nov 2026 17:17:48 +0000 Subject: [PATCH 01/16] Read the service account from the environment The UI and setup scripts now take the service account from OASIS_SERVICE_USERNAME_OR_ID / OASIS_SERVICE_PASSWORD_OR_SECRET (exported by install.sh from .env), falling back to .streamlit/secrets.toml. A deployment's credentials only need setting in .env, and secrets.toml can't drift from the platform's admin password. quiet_login also handles failed logins instead of crashing. Co-Authored-By: Claude Opus 5.5 --- app.py | 6 ++---- docker-compose.ui.yml | 3 +++ modules/authorisation.py | 22 ++++++------------- modules/credentials.py | 46 ++++++++++++++++++++++++++++++++++++++++ scripts/utils.py | 20 +++++++---------- 5 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 modules/credentials.py diff --git a/app.py b/app.py index 0cb042b..eab3137 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ get_auth_type, exchange_session_token ) import streamlit as st +from modules.credentials import service_client_kwargs from modules.client import ClientInterface from modules.nav import SidebarNav from oasis_data_manager.errors import OasisException @@ -45,10 +46,7 @@ if ui_config.skip_login: with st.spinner("Loading platform..."): - if auth_type != 'simple': - client_interface = ClientInterface(auth_type="oidc", client_id=st.secrets["client_id"], client_secret=st.secrets["client_secret"]) - else: - client_interface = ClientInterface(auth_type="simple", username=st.secrets["user"], password=st.secrets["password"]) + client_interface = ClientInterface(**service_client_kwargs()) st.session_state["client"] = client_interface.client cols = st.columns([0.1, 0.8, 0.1]) diff --git a/docker-compose.ui.yml b/docker-compose.ui.yml index 23a5fbf..8e9f489 100644 --- a/docker-compose.ui.yml +++ b/docker-compose.ui.yml @@ -25,6 +25,9 @@ services: - API_AUTH_TYPE=${API_AUTH_TYPE} - OASIS_HOSTNAME=${OASIS_UI_HOSTNAME} - OASIS_PROTOCOL=${OASIS_PROTOCOL:-http} + # Service account (set by install.sh from .env); falls back to .streamlit/secrets.toml + - OASIS_SERVICE_USERNAME_OR_ID=${OASIS_SERVICE_USERNAME_OR_ID:-} + - OASIS_SERVICE_PASSWORD_OR_SECRET=${OASIS_SERVICE_PASSWORD_OR_SECRET:-} volumes: - ${UI_DEFAULTS:-./defaults/}:/usr/src/app/defaults/ - ${UI_CONFIG:-./ui-config.json}:/usr/src/app/ui-config.json:ro diff --git a/modules/authorisation.py b/modules/authorisation.py index 5833613..8140f15 100644 --- a/modules/authorisation.py +++ b/modules/authorisation.py @@ -1,10 +1,12 @@ """Module to handle authorisation.""" import os import requests +from oasis_data_manager.errors import OasisException from requests.exceptions import HTTPError from modules.config import retrieve_ui_config import streamlit as st from modules.client import ClientInterface +from modules.credentials import service_client_kwargs import logging logger = logging.getLogger(__name__) @@ -50,13 +52,7 @@ def handle_login(skip_login=False): if skip_login: with st.spinner("Loading platform..."): try: - auth_type = get_auth_type() - if auth_type != 'simple': - st.session_state["client_interface"] = ClientInterface( - auth_type="oidc", client_id=st.secrets["client_id"], client_secret=st.secrets["client_secret"]) - else: - st.session_state["client_interface"] = ClientInterface( - auth_type="simple", username=st.secrets["user"], password=st.secrets["password"]) + st.session_state["client_interface"] = ClientInterface(**service_client_kwargs()) except HTTPError as e: logger.error(e) st.error("Loading platform failed.") @@ -70,16 +66,10 @@ def quiet_login(): if "client_interface" in st.session_state: return - auth_type = get_auth_type() try: - if auth_type != 'simple': - if "client_id" in st.secrets and "client_secret" in st.secrets: - st.session_state["client_interface"] = ClientInterface( - auth_type="oidc", client_id=st.secrets["client_id"], client_secret=st.secrets["client_secret"]) - elif "user" in st.secrets and "password" in st.secrets: - st.session_state["client_interface"] = ClientInterface(auth_type="simple", username=st.secrets["user"], password=st.secrets["password"]) - except HTTPError as e: - logger.error(e) + st.session_state["client_interface"] = ClientInterface(**service_client_kwargs()) + except (HTTPError, OasisException) as e: + logger.error(f'Service login failed: {e}') return diff --git a/modules/credentials.py b/modules/credentials.py new file mode 100644 index 0000000..2cb4133 --- /dev/null +++ b/modules/credentials.py @@ -0,0 +1,46 @@ +"""Credentials for the UI's service account. + +Environment variables set by install.sh take precedence over +`.streamlit/secrets.toml`, so a deployment's credentials only need setting in +`.env`: + +- `API_AUTH_TYPE`: `simple`, or an OIDC provider (`keycloak`, `authentik`) +- `OASIS_SERVICE_USERNAME_OR_ID`: username (simple) or client id (OIDC) +- `OASIS_SERVICE_PASSWORD_OR_SECRET`: password (simple) or client secret (OIDC) +""" +import os + +import streamlit as st + + +def _secret(key, default=None): + try: + return st.secrets.get(key, default) + except Exception: # noqa: BLE001 - no secrets file is fine when env vars are set + return default + + +def service_auth_type(): + """'simple' or 'oidc'.""" + auth = os.environ.get('API_AUTH_TYPE') or _secret('auth_type', 'simple') + return 'simple' if auth == 'simple' else 'oidc' + + +def service_credentials(): + """`(auth_type, id, secret)` for the service account.""" + auth = service_auth_type() + env_id = os.environ.get('OASIS_SERVICE_USERNAME_OR_ID') + env_secret = os.environ.get('OASIS_SERVICE_PASSWORD_OR_SECRET') + if env_id and env_secret: + return auth, env_id, env_secret + if auth == 'simple': + return auth, _secret('user', 'admin'), _secret('password', 'password') + return auth, _secret('client_id', 'oasis-service'), _secret('client_secret', 'serviceNotSoSecret') + + +def service_client_kwargs(): + """Keyword arguments for `ClientInterface` / `APIClient` using the service account.""" + auth, ident, secret = service_credentials() + if auth == 'simple': + return {'auth_type': 'simple', 'username': ident, 'password': secret} + return {'auth_type': 'oidc', 'client_id': ident, 'client_secret': secret} diff --git a/scripts/utils.py b/scripts/utils.py index ad371e0..579c5b0 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -1,4 +1,9 @@ -import streamlit as st +import sys +from pathlib import Path + +# Allow `python scripts/