feat(oidc): set the default group from the environment, by name

Without it every account auto-created through the env provider fell back to
Viewers (routes/mfa.py), and because the provider is locked the UI could not
correct it either -- a real limitation for a declarative deployment running
BAMBUDDY_OIDC_AUTO_CREATE_USERS=true.

BAMBUDDY_OIDC_DEFAULT_GROUP names a group rather than an id: ids are handed out
per installation, so the same compose file would point at a different group on
the next deployment. The name is matched exactly, resolved against the database
before anything is written, and default_group_id joins _APPLIED_FIELDS so
dropping the variable clears the group again -- the environment is the whole
truth for this row.

A name that matches no group is refused rather than defaulted: silently landing
users in Viewers is the failure this variable exists to remove, and the API
already answers 422 for a default_group_id that does not exist. The refusal is
logged and survivable, and it says which of the two cases happened, because
they differ sharply -- an existing provider keeps running on its last good
config, while on a first boot nothing is created and no SSO button appears.

Raised by maziggy in review of #2625 as a scope decision; documented in
.env.example and in the companion wiki PR.
This commit is contained in:
Marian
2026-07-30 14:43:23 +00:00
parent ac2f4d058e
commit 3c679459e1
5 changed files with 182 additions and 0 deletions
@@ -28,6 +28,7 @@ OPTIONAL = (
"BAMBUDDY_OIDC_REQUIRE_EMAIL_VERIFIED",
"BAMBUDDY_OIDC_ICON_URL",
"BAMBUDDY_OIDC_AUTOLOGIN",
"BAMBUDDY_OIDC_DEFAULT_GROUP",
)
@@ -118,6 +119,23 @@ def test_optional_strings_override_their_defaults(monkeypatch):
assert cfg["icon_url"] == "https://sso.example.com/logo.png"
def test_the_default_group_is_read_as_a_name(monkeypatch):
"""A name, not an id: group ids differ per install, so an id in a compose
file would point at whatever group happened to be created third."""
_set_required(monkeypatch)
monkeypatch.setenv("BAMBUDDY_OIDC_DEFAULT_GROUP", "Operators")
cfg = read_env_oidc_config()
assert cfg["default_group"] == "Operators"
assert "default_group_id" not in cfg, "resolution needs the database, not the reader"
@pytest.mark.parametrize("raw", ["", " "])
def test_a_blank_default_group_is_unset(monkeypatch, raw):
_set_required(monkeypatch)
monkeypatch.setenv("BAMBUDDY_OIDC_DEFAULT_GROUP", raw)
assert read_env_oidc_config()["default_group"] is None
def test_every_var_the_reader_knows_is_registered_in_the_typo_guard():
"""An unregistered BAMBUDDY_* var logs "possible typo" at every boot, which
would tell operators their correct config is wrong. Asserted against the