fix(onprem): only directory users join a workspace
ci / shared (pull_request) Skipped

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>
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-11-20 12:00:45 +01:00
1 parent d8cb363d5c
commit 1b821baa7b
3 files changed
+66 -9

No files matched your search

+28
View File
@@ -12,9 +12,37 @@ func newDirectoryKC(t *testing.T) (*membersKC, *HTTPAdapter) {
t.Helper()
k, a := newMembersKC(t)
a.cfg.DirectoryUsers = true
// The test users come from the directory (the AD federation), as on an installation.
for id, u := range k.users {
u.FederationLink = "ldap-ad"
k.users[id] = u
}
return k, a
}
// A local account (our staff on an installation) is no directory user: neither the first
// admin of a workspace nor a member, whatever its address.
func TestDirectory_localAccountIsNotADirectoryUser(t *testing.T) {
k, a := newDirectoryKC(t)
k.users["u-staff"] = memberRepresentation{ID: "u-staff", Username: "bp-support", Email: "support@staff.test", Enabled: true}
if _, err := a.CreateOrgAndInvite(context.Background(), InviteInput{TenantID: "t-9", Slug: "staffws", Name: "S", AdminEmail: "support@staff.test"}); !errors.Is(err, ErrNotInDirectory) {
t.Errorf("staff as workspace admin: %v, want ErrNotInDirectory", err)
}
if _, err := a.InviteMember(context.Background(), MemberInviteInput{TenantID: "t-1", Slug: "acme", Email: "support@staff.test", Role: "USER"}); !errors.Is(err, ErrNotInDirectory) {
t.Errorf("staff as member: %v, want ErrNotInDirectory", err)
}
if len(k.orgsCreated) != 0 || len(k.puts["u-staff"]) != 0 {
t.Errorf("orgs %v, attributes written %v", k.orgsCreated, k.puts["u-staff"])
}
for org, ids := range k.members {
for _, id := range ids {
if id == "u-staff" {
t.Errorf("staff joined %s", org)
}
}
}
}
func TestDirectory_adminIsAttachedWithoutMail(t *testing.T) {
k, a := newDirectoryKC(t)
res, err := a.CreateOrgAndInvite(context.Background(), InviteInput{
+4 -1
View File
@@ -24,6 +24,9 @@ type memberRepresentation struct {
Enabled bool `json:"enabled"`
CreatedTimestamp int64 `json:"createdTimestamp"` // epoch millis
Attributes map[string][]string `json:"attributes"`
// FederationLink names the user storage (the AD federation) a user comes from; empty for a
// local account.
FederationLink string `json:"federationLink,omitempty"`
}
func (m memberRepresentation) toOrgMember() OrgMember {
@@ -107,7 +110,7 @@ func (a *HTTPAdapter) InviteMember(ctx context.Context, in MemberInviteInput) (*
"tenant_status": {in.TenantStatus},
}
created := false
userID, err := a.findUserByEmail(ctx, in.Email)
userID, err := a.findMemberUser(ctx, in.Email)
switch {
case err == nil:
// Existing user (possibly created by an earlier, half-failed invite):
+34 -8
View File
@@ -193,7 +193,7 @@ func (a *HTTPAdapter) createOrg(ctx context.Context, in InviteInput) (string, er
// this person the IT admin), the user joins the organization, and no mail is
// sent: they sign in with their directory password.
func (a *HTTPAdapter) attachDirectoryAdmin(ctx context.Context, in InviteInput) (*InviteResult, error) {
userID, err := a.findUserByEmail(ctx, in.AdminEmail)
userID, err := a.findDirectoryUserByEmail(ctx, in.AdminEmail)
if errors.Is(err, errUserLookupMiss) {
return nil, fmt.Errorf("%w: %s", ErrNotInDirectory, in.AdminEmail)
}
@@ -346,28 +346,54 @@ func (a *HTTPAdapter) UserExists(ctx context.Context, email string) (bool, error
}
func (a *HTTPAdapter) findUserByEmail(ctx context.Context, email string) (string, error) {
id, _, err := a.lookupUserByEmail(ctx, email)
return id, err
}
// findDirectoryUserByEmail is findUserByEmail for directory mode: only a user from the
// directory (a federation link) counts. A local account with that address -- our staff
// accounts on an installation (onprem keycloak/staff-account.sh) -- is no workspace member:
// errUserLookupMiss, so callers answer not_in_directory.
func (a *HTTPAdapter) findDirectoryUserByEmail(ctx context.Context, email string) (string, error) {
id, federated, err := a.lookupUserByEmail(ctx, email)
if err == nil && !federated {
return "", errUserLookupMiss
}
return id, err
}
// findMemberUser looks up a member to add: any user in the cloud, a directory user on-prem.
func (a *HTTPAdapter) findMemberUser(ctx context.Context, email string) (string, error) {
if a.cfg.DirectoryUsers {
return a.findDirectoryUserByEmail(ctx, email)
}
return a.findUserByEmail(ctx, email)
}
func (a *HTTPAdapter) lookupUserByEmail(ctx context.Context, email string) (id string, federated bool, err error) {
resp, err := a.adminCall(ctx, http.MethodGet,
fmt.Sprintf("/users?email=%s&exact=true", url.QueryEscape(email)), nil, nil)
if err != nil {
return "", err
return "", false, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode/100 != 2 {
return "", fmt.Errorf("find user: %d", resp.StatusCode)
return "", false, fmt.Errorf("find user: %d", resp.StatusCode)
}
var users []struct {
ID string `json:"id"`
Email string `json:"email"`
ID string `json:"id"`
Email string `json:"email"`
FederationLink string `json:"federationLink"`
}
if err := json.NewDecoder(resp.Body).Decode(&users); err != nil {
return "", err
return "", false, err
}
for _, u := range users {
if strings.EqualFold(u.Email, email) {
return u.ID, nil
return u.ID, u.FederationLink != "", nil
}
}
return "", errUserLookupMiss
return "", false, errUserLookupMiss
}
// inviteRedirect is the configured invite redirect with "{slug}" replaced by