Sign-in is set up from the console, and a directory tried before it is kept
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>
This commit is contained in:
1 parent
4b6b3eeddb
commit
ac25a9136a
24 files changed
+1753
-18
No files matched your search
+138
-11
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/binary"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -24,6 +25,26 @@ type LDAP struct {
|
||||
cfg config.LDAP
|
||||
tls *tls.Config
|
||||
dial func(url string) (ldapConn, error)
|
||||
// password, when set, is the service account's in place of the one
|
||||
// bind_password_file names: settings tried before they are kept.
|
||||
password string
|
||||
}
|
||||
|
||||
// WithPassword has the service account bind with pw rather than read it.
|
||||
func (d *LDAP) WithPassword(pw string) *LDAP {
|
||||
d.password = pw
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *LDAP) servicePassword(ctx context.Context) (string, error) {
|
||||
if d.password != "" {
|
||||
return d.password, nil
|
||||
}
|
||||
pw, err := secrets.Read(ctx, d.cfg.BindPasswordFile)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("auth.ldap.bind_password_file: %w", err)
|
||||
}
|
||||
return strings.TrimSpace(string(pw)), nil
|
||||
}
|
||||
|
||||
// ldapConn is the part of a connection used here, so tests need no server.
|
||||
@@ -49,14 +70,18 @@ var ErrBadCredentials = errors.New("the directory does not take that name and pa
|
||||
// NewLDAP prepares a directory from its configuration.
|
||||
func NewLDAP(cfg config.LDAP) (*LDAP, error) {
|
||||
tc := &tls.Config{MinVersion: tls.VersionTLS12}
|
||||
if cfg.CAFile != "" {
|
||||
pem, err := os.ReadFile(cfg.CAFile)
|
||||
if err != nil {
|
||||
ca, where := []byte(cfg.CA), "auth.ldap.ca"
|
||||
if cfg.CA == "" && cfg.CAFile != "" {
|
||||
var err error
|
||||
if ca, err = os.ReadFile(cfg.CAFile); err != nil {
|
||||
return nil, fmt.Errorf("auth.ldap.ca_file: %w", err)
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
if !pool.AppendCertsFromPEM(pem) {
|
||||
return nil, fmt.Errorf("auth.ldap.ca_file %s holds no certificate", cfg.CAFile)
|
||||
where = "auth.ldap.ca_file " + cfg.CAFile
|
||||
}
|
||||
if len(ca) > 0 {
|
||||
pool, err := certPool(ca)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", where, err)
|
||||
}
|
||||
tc.RootCAs = pool
|
||||
}
|
||||
@@ -65,6 +90,33 @@ func NewLDAP(cfg config.LDAP) (*LDAP, error) {
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// certPool is the certificates in pem, each of which must parse: a
|
||||
// certificate Go refuses (a negative serial, as Samba's own has) is said,
|
||||
// not skipped into an empty pool.
|
||||
func certPool(data []byte) (*x509.CertPool, error) {
|
||||
pool := x509.NewCertPool()
|
||||
n := 0
|
||||
for rest := data; ; {
|
||||
var b *pem.Block
|
||||
if b, rest = pem.Decode(rest); b == nil {
|
||||
break
|
||||
}
|
||||
if b.Type != "CERTIFICATE" {
|
||||
continue
|
||||
}
|
||||
c, err := x509.ParseCertificate(b.Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pool.AddCert(c)
|
||||
n++
|
||||
}
|
||||
if n == 0 {
|
||||
return nil, errors.New("holds no certificate")
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func (d *LDAP) dialURL(u string) (ldapConn, error) {
|
||||
c, err := ldap.DialURL(u, ldap.DialWithTLSConfig(d.tls))
|
||||
if err != nil {
|
||||
@@ -84,9 +136,9 @@ func (d *LDAP) dialURL(u string) (ldapConn, error) {
|
||||
// service is a connection bound as the account that finds people, to the
|
||||
// first server that answers.
|
||||
func (d *LDAP) service(ctx context.Context) (ldapConn, error) {
|
||||
pw, err := secrets.Read(ctx, d.cfg.BindPasswordFile)
|
||||
pw, err := d.servicePassword(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("auth.ldap.bind_password_file: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
var errs []error
|
||||
for _, u := range d.cfg.URLs {
|
||||
@@ -95,7 +147,7 @@ func (d *LDAP) service(ctx context.Context) (ldapConn, error) {
|
||||
errs = append(errs, fmt.Errorf("%s: %w", u, err))
|
||||
continue
|
||||
}
|
||||
if err := c.Bind(d.cfg.BindDN, strings.TrimSpace(string(pw))); err != nil {
|
||||
if err := c.Bind(d.cfg.BindDN, pw); err != nil {
|
||||
c.Close()
|
||||
// A server that refuses the service account refuses it
|
||||
// everywhere: not tried on the next.
|
||||
@@ -144,11 +196,11 @@ func (d *LDAP) Authenticate(ctx context.Context, login, password string) (Identi
|
||||
return Identity{}, fmt.Errorf("checking %s's password: %w", login, err)
|
||||
}
|
||||
// Back to the service account to read groups, which a person may not.
|
||||
pw, err := secrets.Read(ctx, d.cfg.BindPasswordFile)
|
||||
pw, err := d.servicePassword(ctx)
|
||||
if err != nil {
|
||||
return Identity{}, err
|
||||
}
|
||||
if err := c.Bind(d.cfg.BindDN, strings.TrimSpace(string(pw))); err != nil {
|
||||
if err := c.Bind(d.cfg.BindDN, pw); err != nil {
|
||||
return Identity{}, err
|
||||
}
|
||||
if id.Groups, err = d.groupsOf(c, id.DN); err != nil {
|
||||
@@ -216,6 +268,81 @@ func (d *LDAP) People(ctx context.Context, m Mapping) ([]Identity, error) {
|
||||
return people, nil
|
||||
}
|
||||
|
||||
// Check is one step of trying a directory.
|
||||
type Check struct {
|
||||
Step string `json:"step"`
|
||||
OK bool `json:"ok"`
|
||||
Detail string `json:"detail"`
|
||||
}
|
||||
|
||||
// Check tries the directory as configured, before its settings are kept:
|
||||
// each server reached and the service account bound on it, how many people
|
||||
// may sign in, and how many are in each group the mapping names.
|
||||
func (d *LDAP) Check(ctx context.Context, m Mapping) []Check {
|
||||
pw, err := d.servicePassword(ctx)
|
||||
if err != nil {
|
||||
return []Check{{Step: "password", Detail: err.Error()}}
|
||||
}
|
||||
var out []Check
|
||||
var c ldapConn
|
||||
for _, u := range d.cfg.URLs {
|
||||
conn, err := d.dial(u)
|
||||
if err == nil {
|
||||
if err = conn.Bind(d.cfg.BindDN, pw); err != nil {
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
out = append(out, Check{Step: u, Detail: err.Error()})
|
||||
continue
|
||||
}
|
||||
out = append(out, Check{Step: u, OK: true, Detail: "reached, bound as " + d.cfg.BindDN})
|
||||
if c == nil {
|
||||
c = conn
|
||||
} else {
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
if c == nil {
|
||||
return out
|
||||
}
|
||||
defer c.Close()
|
||||
res, err := c.SearchWithPaging(ldap.NewSearchRequest(d.cfg.BaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
|
||||
0, 0, false, d.people(false), []string{"objectGUID"}, nil), 500)
|
||||
switch {
|
||||
case err != nil:
|
||||
out = append(out, Check{Step: "people", Detail: err.Error()})
|
||||
case len(res.Entries) == 0:
|
||||
out = append(out, Check{Step: "people", Detail: "no enabled user under base_dn matches user_filter: no one could sign in"})
|
||||
default:
|
||||
out = append(out, Check{Step: "people", OK: true,
|
||||
Detail: fmt.Sprintf("%d enabled people under base_dn may sign in", len(res.Entries))})
|
||||
}
|
||||
for _, g := range m.Groups() {
|
||||
dns, err := d.groupDNs(c, g)
|
||||
if err != nil || len(dns) == 0 {
|
||||
detail := "no group of that name under " + d.groupBase()
|
||||
if err != nil {
|
||||
detail = err.Error()
|
||||
}
|
||||
out = append(out, Check{Step: "group " + g, Detail: detail})
|
||||
continue
|
||||
}
|
||||
n := 0
|
||||
for _, gdn := range dns {
|
||||
filter := "(&" + d.people(false) + "(memberOf:" + inChain + ":=" + ldap.EscapeFilter(gdn) + "))"
|
||||
r, err := c.SearchWithPaging(ldap.NewSearchRequest(d.cfg.BaseDN, ldap.ScopeWholeSubtree,
|
||||
ldap.NeverDerefAliases, 0, 0, false, filter, []string{"objectGUID"}, nil), 500)
|
||||
if err == nil {
|
||||
n += len(r.Entries)
|
||||
}
|
||||
}
|
||||
out = append(out, Check{Step: "group " + g, OK: true,
|
||||
Detail: fmt.Sprintf("%d of the people who may sign in are in it, nested groups included", n)})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// groupDNs are the groups a mapping's name means: a DN as it is, a CN
|
||||
// looked up.
|
||||
func (d *LDAP) groupDNs(c ldapConn, g string) ([]string, error) {
|
||||
|
||||
@@ -18,12 +18,16 @@ type fakeConn struct {
|
||||
filters []string
|
||||
password string // the person's
|
||||
people int // how many the person search finds
|
||||
refused bool // the service account's password is wrong
|
||||
}
|
||||
|
||||
func (f *fakeConn) Bind(user, password string) error {
|
||||
f.binds = append(f.binds, user)
|
||||
if f.refused && strings.HasPrefix(user, "CN=llmgw") {
|
||||
return ldap.NewError(ldap.LDAPResultInvalidCredentials, errors.New("invalid credentials"))
|
||||
}
|
||||
if strings.HasPrefix(user, "CN=Ada") && password != f.password {
|
||||
return &ldap.Error{ResultCode: ldap.LDAPResultInvalidCredentials}
|
||||
return ldap.NewError(ldap.LDAPResultInvalidCredentials, errors.New("invalid credentials"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -128,3 +132,31 @@ func slicesHas(xs []string, x string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestADirectoryIsTriedStepByStepBeforeItsSettingsAreKept(t *testing.T) {
|
||||
m := Mapping{Roles: map[string][]string{"admin": {"llmgw-admins"}}}
|
||||
checks := fakeLDAP(t, &fakeConn{people: 1}).WithPassword("typed-in").Check(t.Context(), m)
|
||||
if len(checks) != 3 || !checks[0].OK || checks[0].Step != "ldaps://dc1" || !checks[1].OK ||
|
||||
checks[2].Step != "group llmgw-admins" || !checks[2].OK {
|
||||
t.Errorf("checks %+v", checks)
|
||||
}
|
||||
// A refused service account stops at the server, and says why.
|
||||
checks = fakeLDAP(t, &fakeConn{refused: true}).Check(t.Context(), m)
|
||||
if len(checks) != 1 || checks[0].OK || !strings.Contains(checks[0].Detail, "Invalid Credentials") {
|
||||
t.Errorf("refused: %+v", checks)
|
||||
}
|
||||
}
|
||||
|
||||
func TestACertificateGoCannotReadIsSaidNotSkipped(t *testing.T) {
|
||||
if _, err := certPool([]byte("not a certificate")); err == nil {
|
||||
t.Error("an empty pool was taken")
|
||||
}
|
||||
// A CERTIFICATE block Go cannot parse, as it cannot Samba's own (its
|
||||
// serial is negative): an error, not an empty pool.
|
||||
neg := "-----BEGIN CERTIFICATE-----\nMIIBNTCB3KADAgECAgT/////MAoGCCqGSM49BAMCMA8xDTALBgNVBAMTBHRlc3Qw\n" +
|
||||
"HhcNMjYwMTAxMDAwMDAwWhcNMjgwMTAxMDAwMDAwWjAPMQ0wCwYDVQQDEwR0ZXN0\n" +
|
||||
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE\n-----END CERTIFICATE-----\n"
|
||||
if _, err := certPool([]byte(neg)); err == nil {
|
||||
t.Error("a broken certificate made a pool")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user