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>
(cherry picked from commit 1b821baa7b)
141 lines
5.2 KiB
Go
141 lines
5.2 KiB
Go
package keycloak
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Directory mode (on-prem): the users come from the customer's directory.
|
|
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{
|
|
TenantID: "t-2", Slug: "werk", Name: "Werk GmbH", AdminEmail: "loose@acme.test", TenantStatus: "active",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.UserID != "u-loose" || res.InviteURL != "" || res.MailError != nil {
|
|
t.Fatalf("result: %+v", res)
|
|
}
|
|
if len(k.created) != 0 || len(k.mails) != 0 {
|
|
t.Errorf("directory mode created %d users and sent %d mails", len(k.created), len(k.mails))
|
|
}
|
|
if got := k.members["org-werk"]; len(got) != 1 || got[0] != "u-loose" {
|
|
t.Errorf("membership: %v", got)
|
|
}
|
|
// The installer named this person the admin: the workspace attributes are set, also over old ones.
|
|
want := map[string]string{"tenant_id": "t-2", "tenant_slug": "werk", "org_roles": "IT_ADMIN", "tenant_status": "active"}
|
|
for key, val := range want {
|
|
if got := k.puts["u-loose"][key]; len(got) != 1 || got[0] != val {
|
|
t.Errorf("attribute %s = %v, want %s", key, got, val)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDirectory_unknownAdminLeavesNoOrganization(t *testing.T) {
|
|
k, a := newDirectoryKC(t)
|
|
_, err := a.CreateOrgAndInvite(context.Background(), InviteInput{TenantID: "t-3", Slug: "nobody", Name: "N", AdminEmail: "ghost@acme.test"})
|
|
if !errors.Is(err, ErrNotInDirectory) {
|
|
t.Fatalf("err = %v, want ErrNotInDirectory", err)
|
|
}
|
|
if len(k.orgsCreated) != 0 || len(k.created) != 0 {
|
|
t.Errorf("orgs created %v, users created %d", k.orgsCreated, len(k.created))
|
|
}
|
|
}
|
|
|
|
func TestDirectory_memberIsAddedWithoutMail(t *testing.T) {
|
|
k, a := newDirectoryKC(t)
|
|
res, err := a.InviteMember(context.Background(), MemberInviteInput{
|
|
TenantID: "t-1", Slug: "acme", TenantStatus: "active", Email: "loose@acme.test", Role: "USER",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Status != InviteStatusAdded || res.Created || len(k.mails) != 0 {
|
|
t.Fatalf("result %+v, mails %v", res, k.mails)
|
|
}
|
|
if got := k.members["org-acme"]; len(got) != 2 || got[1] != "u-loose" {
|
|
t.Errorf("membership: %v", got)
|
|
}
|
|
if _, err := a.InviteMember(context.Background(), MemberInviteInput{Slug: "acme", Email: "ghost@acme.test", Role: "USER"}); !errors.Is(err, ErrNotInDirectory) {
|
|
t.Errorf("unknown member: %v", err)
|
|
}
|
|
if len(k.created) != 0 {
|
|
t.Errorf("a local user was created in directory mode")
|
|
}
|
|
}
|
|
|
|
func TestDirectory_noResendAndNeverPending(t *testing.T) {
|
|
k, a := newDirectoryKC(t)
|
|
delete(k.creds, "u-admin") // like an AD user: no password in Keycloak
|
|
// Control: the same member without a Keycloak password is pending in the cloud.
|
|
kc, cloud := newMembersKC(t)
|
|
delete(kc.creds, "u-admin")
|
|
if ms, err := cloud.ListOrgMembers(context.Background(), "acme"); err != nil || len(ms) == 0 || !ms[0].Pending {
|
|
t.Fatalf("control: cloud member not pending: %+v %v", ms, err)
|
|
}
|
|
if err := a.ResendInvite(context.Background(), "acme", "admin@acme.test"); !errors.Is(err, ErrNoInviteMail) {
|
|
t.Errorf("resend: %v", err)
|
|
}
|
|
members, err := a.ListOrgMembers(context.Background(), "acme")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, m := range members {
|
|
if m.Pending {
|
|
t.Errorf("%s pending: a directory user has no Keycloak password and is never pending", m.Email)
|
|
}
|
|
}
|
|
}
|
|
|
|
// KC 26 lists a federated member with its LDAP attributes only; the roles are
|
|
// read from the user itself.
|
|
func TestDirectory_memberRolesWhenTheListCarriesOnlyLDAPAttributes(t *testing.T) {
|
|
k, a := newDirectoryKC(t)
|
|
k.ldapOnly = true
|
|
members, err := a.ListOrgMembers(context.Background(), "acme")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(members) != 1 || strings.Join(members[0].OrgRoles, ",") != "IT_ADMIN,OWNER" {
|
|
t.Errorf("members = %+v", members)
|
|
}
|
|
}
|