People → Sign-in shows single sign-on, Active Directory and SCIM, and the directory's last sync. E sets up Entra ID from the tenant and client IDs; D sets up Active Directory, tried from the control plane first: each server bound, people who may sign in, members of each mapped group. The secrets go into the store as store:auth/..., sealed by the master key, and the CA into the settings, so no control plane needs a file. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
109 lines
5.0 KiB
Go
109 lines
5.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
|
|
)
|
|
|
|
// adForm is the directory form's values, in its order.
|
|
func adForm(password, ca, allowed, teams string) []string {
|
|
return []string{"ldaps://dc1.corp.example, ldaps://dc2.corp.example", "OU=Staff,DC=corp,DC=example",
|
|
"CN=svc,OU=Service,DC=corp,DC=example", password, ca, allowed, "llmgw-admins", "", teams, "Acme", "yes"}
|
|
}
|
|
|
|
func TestTheDirectoryFormMakesTheBlockTheSettingsKeep(t *testing.T) {
|
|
pem := "-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----\n"
|
|
caFile := filepath.Join(t.TempDir(), "ca.pem")
|
|
if err := os.WriteFile(caFile, []byte(pem), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err := directoryFromForm(config.LDAP{}, adForm("pw", caFile, "CN=llmgw-users,OU=Groups,DC=corp,DC=example",
|
|
"ml-team=team:Acme/ml; sales=org:Sales"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The group, nested ones counting; the CA written into the settings;
|
|
// the password named in the store, not a file on one machine.
|
|
if l.UserFilter != "(memberOf:1.2.840.113556.1.4.1941:=CN=llmgw-users,OU=Groups,DC=corp,DC=example)" ||
|
|
l.CA != pem || l.BindPasswordFile != "store:auth/ldap-bind" || len(l.URLs) != 2 ||
|
|
l.Memberships["sales"] != "org:Sales" || l.Roles["admin"][0] != "llmgw-admins" || l.Roles["operator"] != nil {
|
|
t.Errorf("%+v", l)
|
|
}
|
|
// What the settings keep reads back the same.
|
|
back, err := config.ParseLDAP([]byte(ldapYAML(l)))
|
|
if err != nil || back.UserFilter != l.UserFilter || back.CA != pem || back.Memberships["ml-team"] != "team:Acme/ml" {
|
|
t.Errorf("read back %+v %v", back, err)
|
|
}
|
|
|
|
// Changed again with nothing typed for the password or the CA: both kept.
|
|
again, err := directoryFromForm(l, adForm("", "", "(objectClass=user)", ""))
|
|
if err != nil || again.CA != pem || again.BindPasswordFile != l.BindPasswordFile || again.UserFilter != "(objectClass=user)" ||
|
|
again.Memberships != nil {
|
|
t.Errorf("kept: %+v %v", again, err)
|
|
}
|
|
// A group by its bare name cannot be a filter, and a plaintext server is refused.
|
|
if _, err := directoryFromForm(config.LDAP{}, adForm("pw", "", "llmgw-users", "")); err == nil {
|
|
t.Error("a bare group name was taken")
|
|
}
|
|
plain := adForm("pw", "", "", "")
|
|
plain[0] = "ldap://dc1"
|
|
if _, err := directoryFromForm(config.LDAP{}, plain); err == nil || !strings.Contains(err.Error(), "plaintext") {
|
|
t.Errorf("plaintext: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTheEntraFormMakesTheIssuerFromTheTenant(t *testing.T) {
|
|
o, err := oidcFromForm(config.OIDC{}, []string{"0f2c-tenant", "app-id", "s3cret", "g-admins", "", "Microsoft", "yes", "no"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if o.Issuer != "https://login.microsoftonline.com/0f2c-tenant/v2.0" || o.ClientSecretFile != "store:auth/oidc-client-secret" ||
|
|
o.Roles["admin"][0] != "g-admins" || !o.CreateAccounts || o.NoPasswords {
|
|
t.Errorf("%+v", o)
|
|
}
|
|
// Another provider by its issuer; the secret kept when none is typed.
|
|
k, err := oidcFromForm(o, []string{"https://sso.example/realms/acme", "llmgw", "", "", "", "Acme SSO", "no", "yes"})
|
|
if err != nil || k.Issuer != "https://sso.example/realms/acme" || k.ClientSecretFile != o.ClientSecretFile || !k.NoPasswords {
|
|
t.Errorf("%+v %v", k, err)
|
|
}
|
|
if _, err := oidcFromForm(config.OIDC{}, []string{"t", "app", "", "", "", "", "yes", "no"}); err == nil {
|
|
t.Error("a first setup without a secret was taken")
|
|
}
|
|
}
|
|
|
|
func TestTheSignInPanelSaysWhatIsOnAndHowTheDirectoryLastWent(t *testing.T) {
|
|
m := consoleModel{menu: cSignIn, loaded: true, width: 120, height: 40}
|
|
m.signIn = signInState{loaded: true, view: adminapi.AuthView{
|
|
BaseURL: "https://llm.example",
|
|
OIDC: &adminapi.OIDCView{Issuer: "https://login.microsoftonline.com/t/v2.0", Label: "Microsoft", CreateAccounts: true},
|
|
LDAP: &adminapi.LDAPView{URLs: []string{"ldaps://dc1"}, BaseDN: "OU=Staff,DC=corp",
|
|
LastSync: &adminapi.DirectorySyncView{At: time.Now().Add(-time.Minute), People: 40, Accounts: 12, Suspended: 1}},
|
|
}}
|
|
_, body, _ := m.signInPanel(110)
|
|
for _, want := range []string{"Single sign-on (Entra ID, OIDC)", "Microsoft", "https://llm.example/login/oidc/callback",
|
|
"Active Directory (LDAP)", "ldaps://dc1", "40 people there, 12 accounts kept in step, 1 suspended", "Provisioning (SCIM)"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("the panel lacks %q:\n%s", want, body)
|
|
}
|
|
}
|
|
m.signIn.view.LDAP.Off = "auth.ldap.ca: holds no certificate"
|
|
if _, body, _ := m.signInPanel(110); !strings.Contains(body, "broken here") || !strings.Contains(body, "holds no certificate") {
|
|
t.Errorf("a directory off here is not said:\n%s", body)
|
|
}
|
|
}
|
|
|
|
func TestAuthBlocksAreReadFromTheSettingsDocument(t *testing.T) {
|
|
body := "auth:\n ldap:\n urls: [ldaps://dc1]\n base_dn: DC=corp\n bind_dn: CN=svc\n bind_password_file: store:auth/ldap-bind\n" +
|
|
" sync_interval: 1m\n oidc:\n issuer: https://sso.example\n client_id: llmgw\n"
|
|
l, o := authBlocks(body)
|
|
if l.BaseDN != "DC=corp" || l.SyncInterval != time.Minute || o.ClientID != "llmgw" {
|
|
t.Errorf("%+v %+v", l, o)
|
|
}
|
|
}
|