feat(onprem): directory users instead of invitations #143

Closed
sharang wants to merge 3 commits from feat/onprem-directory-users into feat/onprem-mode
pull from: feat/onprem-directory-users
Owner

Stacked on #142, so the base is feat/onprem-mode; merge #142 first. This closes on-prem gap 6.

Problem. On-prem, every account comes from the customer's AD through Keycloak's federation. Keycloak finds an AD user by e-mail even before their first sign-in, so creating the workspace with an AD admin answered email_taken, and nobody could enter it. Invitations would also have created local users and sent a set-password mail.

Directory mode in the Keycloak adapter. It is on with APP_ENV=onprem:

  • First admin and invited members are existing directory users, added to the organization with their role. No local user is created and no mail is sent. They sign in with their AD password. The invite status is added; the audit actions are user.added and keycloak.admin_added.
  • Unknown address, or one outside the AD groups that may use the platform: 404 not_in_directory. For the workspace admin this is checked before the workspace is created, and the adapter looks the user up before it creates the organization. Nothing is left behind.
  • Resend: 409 no_invite_mail. Directory members are never "pending".
  • Member list: Keycloak 26.8 lists a federated member with only its LDAP attributes, so the roles are now read from the user record.

Keycloak side (platform/onprem, package/keycloak/):

  • Read-only federation refuses every attribute write on AD users ("Federated storage is not writable"), but the platform keeps workspace and role in those attributes.
  • The package therefore runs the federation UNSYNCED, which never writes to AD.
  • realm-policy.sh:
    • turns Organizations on
    • lets admins edit attributes, as in the cloud realm
    • closes every user-facing way to set a password in Keycloak (reset mail, update-password action, account console); the registry never sets one either
    • residual: a Keycloak admin can still set a local password, which then bypasses AD, even for an account disabled there. That is the same trust level as any Keycloak admin, who can already impersonate users. The detective control is check-local-passwords.sh, tested both ways.
  • removeInvalidUsersEnabled=false: an account disabled in AD, or removed from the groups, can't sign in but keeps its membership.

Checked against Keycloak 26.8.1 and a Samba AD (the test VM):

  • Admin attached without mail, member added, an account outside the groups refused.
  • Roles listed correctly, resend refused, nobody pending.
  • Sign-in works with the AD password. Wrong password, disabled account and an account outside the groups are all refused, and attributes survive every case.
  • PurgeTenant removes only the Keycloak copies; the AD accounts stay.

Tests. go test ./... passes, including Postgres. New: internal/keycloak/directory_test.go (each test has a cloud control where it matters) and on-prem server tests in onprem_test.go.

Small cloud-side changes:

  • The workspace admin's e-mail is lowercased before it goes to Keycloak and into the audit rows. Keycloak already stores e-mails lowercase, and the tenant row was already lowercase.
  • The member list makes one extra user read per member whose list entry has no org_roles.
  • The admin's tenant_status stays trial in the cloud; only on-prem passes the workspace status. A test pins this.

Follow-up from the test install (1b821ba)

On an installation the realm also holds our local staff accounts (onprem keycloak/staff-account.sh). The directory lookup took any Keycloak user with the address, so a workspace admin could add our staff account to their team (201 added, workspace attributes and membership on the staff account). Now only a user with a federation link (from the AD) joins a workspace, as first admin or member; a local account answers not_in_directory. New test TestDirectory_localAccountIsNotADirectoryUser; checked in the VM (422 not_in_directory). #144 has this merged in.

🤖 Generated with Claude Code

Stacked on #142, so the base is `feat/onprem-mode`; merge #142 first. This closes on-prem gap 6. **Problem.** On-prem, every account comes from the customer's AD through Keycloak's federation. Keycloak finds an AD user by e-mail even before their first sign-in, so creating the workspace with an AD admin answered `email_taken`, and nobody could enter it. Invitations would also have created local users and sent a set-password mail. **Directory mode in the Keycloak adapter.** It is on with `APP_ENV=onprem`: - **First admin and invited members** are existing directory users, added to the organization with their role. No local user is created and no mail is sent. They sign in with their AD password. The invite status is `added`; the audit actions are `user.added` and `keycloak.admin_added`. - **Unknown address,** or one outside the AD groups that may use the platform: 404 `not_in_directory`. For the workspace admin this is checked before the workspace is created, and the adapter looks the user up before it creates the organization. Nothing is left behind. - **Resend:** 409 `no_invite_mail`. Directory members are never "pending". - **Member list:** Keycloak 26.8 lists a federated member with only its LDAP attributes, so the roles are now read from the user record. **Keycloak side** (`platform/onprem`, `package/keycloak/`): - Read-only federation refuses every attribute write on AD users ("Federated storage is not writable"), but the platform keeps workspace and role in those attributes. - The package therefore runs the federation `UNSYNCED`, which never writes to AD. - `realm-policy.sh`: - turns Organizations on - lets admins edit attributes, as in the cloud realm - closes every user-facing way to set a password in Keycloak (reset mail, update-password action, account console); the registry never sets one either - residual: a **Keycloak admin** can still set a local password, which then bypasses AD, even for an account disabled there. That is the same trust level as any Keycloak admin, who can already impersonate users. The detective control is `check-local-passwords.sh`, tested both ways. - `removeInvalidUsersEnabled=false`: an account disabled in AD, or removed from the groups, can't sign in but keeps its membership. **Checked against Keycloak 26.8.1 and a Samba AD** (the test VM): - Admin attached without mail, member added, an account outside the groups refused. - Roles listed correctly, resend refused, nobody pending. - Sign-in works with the AD password. Wrong password, disabled account and an account outside the groups are all refused, and attributes survive every case. - `PurgeTenant` removes only the Keycloak copies; the AD accounts stay. **Tests.** `go test ./...` passes, including Postgres. New: `internal/keycloak/directory_test.go` (each test has a cloud control where it matters) and on-prem server tests in `onprem_test.go`. **Small cloud-side changes:** - The workspace admin's e-mail is lowercased before it goes to Keycloak and into the audit rows. Keycloak already stores e-mails lowercase, and the tenant row was already lowercase. - The member list makes one extra user read per member whose list entry has no `org_roles`. - The admin's `tenant_status` stays `trial` in the cloud; only on-prem passes the workspace status. A test pins this. ## Follow-up from the test install (1b821ba) On an installation the realm also holds our local staff accounts (`onprem` `keycloak/staff-account.sh`). The directory lookup took any Keycloak user with the address, so a workspace admin could add our staff account to their team (201 `added`, workspace attributes and membership on the staff account). Now only a user with a federation link (from the AD) joins a workspace, as first admin or member; a local account answers `not_in_directory`. New test `TestDirectory_localAccountIsNotADirectoryUser`; checked in the VM (422 `not_in_directory`). #144 has this merged in. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang added 1 commit 2026-11-19 17:18:39 +00:00
feat(onprem): directory users instead of invitations
ci / shared (pull_request) Skipped
b8206c553f
On-prem every account comes from the customer's AD through Keycloak's
federation, so an AD admin answered email_taken and invitations asked for a
new password. The Keycloak adapter gets a directory mode (DirectoryUsers,
on with APP_ENV=onprem):

- The first admin and invited members are existing directory users,
  attached to the organization with their role. No local user is created,
  no execute-actions mail is sent (invite status "added", audit user.added /
  keycloak.admin_added).
- An unknown address (or one outside the admitted AD groups) answers 404
  not_in_directory. For the workspace's admin it is checked before the
  workspace is created, and the adapter looks the user up before creating
  the organization, so nothing is left behind.
- Resend answers 409 no_invite_mail; directory members are never pending.
- Member lists read the roles from the user when Keycloak lists a federated
  member with its LDAP attributes only (seen with Keycloak 26.8).
- The admin address is normalized once for lookup, store and Keycloak.

Checked against Keycloak 26.8.1 with a Samba AD (the test install in
platform/onprem, realm with UNSYNCED federation and realm-policy.sh).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CODEOWNERS rules requested review from Benjamin_Boenisch 2026-11-19 17:18:39 +00:00
sharang added 1 commit 2026-11-19 17:24:56 +00:00
Only on-prem passes the workspace's status to Keycloak for its first admin;
a cloud demo workspace with an admin wrote demo instead of trial (pinned by
a test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang added 1 commit 2026-11-20 11:00:46 +00:00
fix(onprem): only directory users join a workspace
ci / shared (pull_request) Skipped
1b821baa7b
In directory mode the registry attached any Keycloak user found by
e-mail. On an installation the realm also holds our local staff accounts
(onprem keycloak/staff-account.sh): a workspace admin could add one to
their team (201 added), giving our support account workspace attributes
and membership. Found in the test install.

Now only a user with a federation link (from the AD) counts, for the
first admin and for members; a local account answers not_in_directory
like an unknown address. The cloud is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Owner

Merged as part of #146 (the same commits on today's main; migration renumbered to 0033).

Merged as part of #146 (the same commits on today's main; migration renumbered to 0033).
sharang closed this pull request 2026-11-20 16:31:13 +00:00

Pull request closed

Please reopen this pull request to perform a merge.
Sign in to join this conversation.