A lightweight, adapter-based LDAP client for Go. LDAPper wraps
go-ldap/ldap with:
- Context-aware operations — every networked call takes a
context.Contextas its first argument; cancellation kills the underlying connection deterministically. - Multi-host failover and transparent reconnection.
- Pluggable catalog adapters so the same API works against Microsoft Active Directory, Samba 4 AD DC, OpenLDAP, 389 Directory Server, OpenDJ, Oracle Unified Directory, ALD Pro, FreeIPA, and any other directory you write an adapter for.
- Connection-level search limits (
SizeLimit,TimeLimit,MaxPages,MaxEntries,RequirePaging) injected once at construction time. - Client-side operation timeouts applied around individual
Bind/Searchcalls, independent of server-sideTimeLimit. - Efficient catalog tree discovery — a single paged subtree search
assembled into an
OUtree on the client, with optional depth trimming. - Sentinel errors (
ErrNotFoundOrNotVisible,ErrUnavailable,ErrInvalidCredentials,ErrInsufficientAccess, …) designed forerrors.Is/errors.As. - End-to-end integration tests for every shipped adapter, running against real directory servers in Docker.
| Layer | Where it runs | Build tag | Notes |
|---|---|---|---|
| Unit tests | go test -race ./... (GitHub Actions, every push) |
— | No external dependencies; includes the race detector. |
| Integration — OpenLDAP | go test -tags=integration_openldap ./... |
integration_openldap |
Spins up osixia/openldap:1.5.0 via testcontainers. |
| Integration — MS AD | go test -tags=integration_msad ./... |
integration_msad |
Spins up a Samba 4 AD DC; gracefully skips if Docker is unavailable. |
| Integration — Samba AD DC | go test -tags=integration_samba ./... |
integration_samba |
Same image as MS AD; exercises SAMBAAdapter. |
| Integration — 389-DS | go test -tags=integration_389ds ./... |
integration_389ds |
389ds/dirsrv (LDAP on :3389). |
| Integration — OpenDJ | go test -tags=integration_opendj ./... |
integration_opendj |
openidentityplatform/opendj (:1389). |
| Integration — OUD | go test -tags=integration_oud ./... |
integration_oud |
Same image as OpenDJ (compat proxy; see below). |
| Integration — FreeIPA | go test -tags=integration_freeipa ./... |
integration_freeipa |
Spins up a FreeIPA server; heavy image, gracefully skips on failure. |
| Integration — ALDPro | go test -tags=integration_aldpro ./... |
integration_aldpro |
OpenLDAP + custom ALDPro schema. |
Coverage is measured with go test -coverprofile=coverage.out ./... and
uploaded to Codecov on every push to main and on pull requests. The
current coverage report is available at
codecov.io/gh/NGRsoftlab/ldapper.
The default go test ./... is intentionally fast and Docker-free: all
integration suites live behind build tags so they do not compile into
the default test binary. Go allows only one TestMain per package, so
each tag also isolates a single suite in the compiled test binary — and
that means tags must be run one at a time, not combined. In
particular, integration_opendj and integration_oud cannot be passed
together: both define TestMain in package ldapper_test, so combining
them is a compile: duplicate symbol error.
- Installation
- Quick start
- Architecture
- Core concepts
- Shipped adapters
- API overview
- Errors
- Writing your own adapter
- Configuration & tuning
- Testing
- License
go get github.com/NGRsoftlab/ldapperRequires Go 1.26 or newer (see go.mod). Depends on
github.com/go-ldap/ldap/v3. Integration tests additionally require
github.com/testcontainers/testcontainers-go and a running Docker daemon.
package main
import (
"context"
"crypto/tls"
"log"
"time"
"github.com/NGRsoftlab/ldapper"
"github.com/NGRsoftlab/ldapper/adapters"
)
func main() {
ctx := context.Background()
adapter := adapters.MSADAdapter{}
limits := ldapper.DefaultSearchLimits()
limits.TimeLimit = 30
conn, err := ldapper.NewAuthenticatedLDAPper(
ctx,
"svc-reader@corp.example.com", "secret", // bind credentials
"DC=corp,DC=example,DC=com", // base DN for probe and user lookups
[]string{"dc1.corp.example.com", "dc2.corp.example.com"},
636, // port
true, // useLDAPS
false, // skipStartTLS (must be false when useLDAPS is true)
&tls.Config{MinVersion: tls.VersionTLS12},
adapter,
ldapper.WithSearchLimits(limits),
ldapper.WithDialTimeout(5*time.Second),
)
if err != nil {
log.Fatalf("connect: %v", err)
}
defer conn.Close()
u, err := conn.GetUser(ctx, "jdoe", "DC=corp,DC=example,DC=com")
if err != nil {
log.Fatalf("get user: %v", err)
}
info := adapter.GetNormalizedUserInfo(*u)
log.Printf("%s <%s>", info.CommonName, info.Mail)
}LDAPper is intentionally small and layered. Only two things are pluggable
(interfaces.LDAPConn and interfaces.CatalogAdapter); everything else is
plain structs and functions.
┌────────────────────────────────────────────────────────────────────┐
│ User code │
│ conn, _ := ldapper.NewAuthenticatedLDAPper(ctx, ...) │
│ conn.GetUser(ctx, ...) / conn.GetCatalogTree(ctx, ...) / ... │
└──────────────────────────────────┬─────────────────────────────────┘
│
┌──────────────────────────────────▼─────────────────────────────────┐
│ LDAPper — public API │
│ │
│ ldapper.go constructors + LDAPper struct │
│ ldapper_connection.go dial / failover / IsAlive / │
│ Reconnect / EnsureAlive / Close │
│ + runWithWatchdog (per-op │
│ timeout & ctx watchdog) │
│ ldapper_auth.go BindConn, │
│ CheckUserCredentials │
│ ldapper_search.go searchPaged, │
│ searchSingleEntryLocked, │
│ newSearchRequest, │
│ renderFilterTemplate, │
│ CustomSearch, CustomSearchPaged │
│ ldapper_user_ops.go GetUser, GetUserGroups, │
│ GetOUUsers │
│ ldapper_group_ops.go GetGroup │
│ ldapper_ou_ops.go GetOU │
│ ldapper_catalog_tree_discovery.go GetCatalogTree, GetSubOUs, │
│ ListSingleLevelOUs, │
│ WalkCatalogTree │
│ ldapper_options.go Option, SearchLimits, │
│ OperationTimeouts, With/Set/Get │
│ ldapper_errors.go sentinel errors + classifiers │
│ helpers.go attrsToMap, escapeFilterArgs, │
│ parentDNOf, canonicalDN, │
│ escapeCanonicalValue │
│ validators.go validateDN / validateBaseDN / │
│ validateHost / formatHost / │
│ isIPLiteral │
└──────────────┬────────────────────────────────────┬────────────────┘
│ │
uses │ │ uses
▼ ▼
┌──────────────────────────────┐ ┌───────────────────────────────┐
│ interfaces.LDAPConn │ │ interfaces.CatalogAdapter │
│ (thin subset of go-ldap) │ │ (schema-specific glue) │
│ │ │ │
│ StartTLS / Bind / Search / │ │ Filters / Attrs / Casts / │
│ SetTimeout / IsClosing / │ │ BindIdentity │
│ Close │ │ │
└──────────────┬───────────────┘ └──────────────┬────────────────┘
│ │
prod: *ldap.Conn prod: adapters.{MSAD, SAMBA, OPENLDAP,
test: mocks.MockLDAPConn DS389, OpenDJ, OUD, ALDPRO, FREEIPA}
test: fakeAdapter
(ldapper_auth_test.go)
│
┌──────────────────────────────▼─────────────────────────────────────┐
│ go-ldap/ldap/v3 │
└──────────────────────────────┬─────────────────────────────────────┘
│
LDAP server
The two interfaces are the only seams:
interfaces.LDAPConnis a 6-method subset of*ldap.Conn. Production uses the real*ldap.Conn; tests injectmocks.MockLDAPConn.interfaces.CatalogAdapteris the only place LDAPper knows anything about directory schema. All filters, attribute lists, value casts and bind-principal translation go through it.
go-ldap's Bind / StartTLS are not context-aware. LDAPper bridges the
gap with a small watchdog primitive in ldapper_connection.go:
withCtx/withCtxResult— the two operation wrappers. They compute the effective timeout asmin(opTimeout, ctx.Deadline - now), apply it viaLDAPConn.SetTimeout, run the operation, and reset the timeout to zero afterwards.runWithWatchdog— the shared primitive. If the caller supplies a cancellable context, a goroutine is spawned that closes the underlying connection whenctxis done. The stop/join and the timeout reset happen inside adefer, so a panic inside the operation cannot leak the goroutine or leave a stale timeout behind.ctx.Err()is treated as the authoritative signal of "was the operation aborted" (a goroutine-local flag would race with the operation returning). When it fires, the connection is published unusable (connection = nil,invalidateAuthLocked) and the caller receives a wrappedcontext.Canceled/context.DeadlineExceeded.
The same watchdog is reused inside dialHost (for StartTLS) and
reconnectLocked (for Bind), so all four cancellation-aware paths
share one implementation.
LDAPper
├── opMu sync.Mutex serializes all ops
│
├── catalogAdapter interfaces.CatalogAdapter schema-specific behavior
├── connection interfaces.LDAPConn active network connection
│
├── isAuthenticated bool set by BindConn
├── bindUser string cached for re-bind on
├── bindPassword string Reconnect /
├── bindDN string CheckUserCredentials
│ (base DN used by
│ CheckUserCredentials)
│
├── hosts []string failover list (≤ 50)
├── currentHost string host of the active conn
├── port uint
│
├── useLDAPS bool LDAPS vs plain LDAP
├── skipStartTLS bool disable StartTLS upgrade
├── tlsCfg *tls.Config
│
├── searchLimits SearchLimits injected via newSearchRequest
├── dialTimeout time.Duration used by dialHost
└── opTimeouts OperationTimeouts applied by withCtx
Every search method funnels through newSearchRequest(...), which injects
searchLimits. Changing limits at runtime (SetSearchLimits) is
therefore a single assignment with no plumbing.
bindDN is a subtle but useful field: BindConn(ctx, username, password, baseDN) records baseDN so that later calls to
CheckUserCredentials(ctx, username, password) can look users up under
the same base DN without the caller repeating it.
dialTimeout is initialised to constants.DefaultDialTimeout (10s) by
the constructors. WithDialTimeout(0) (or SetDialTimeout(0)) disables
the explicit timeout and falls back to Go's net.Dialer default.
opTimeouts starts at zero (Bind: 0, Search: 0) which means "no
client-side timeout" — the operations rely on SearchRequest.TimeLimit
(server-side) and the caller's context deadline. Enabling
WithOperationTimeout adds a client-side deadline around each individual
Bind / Search call. This zero value also means that Reconnect
applies no client-side Bind timeout when the cached credentials are
replayed — the dial itself is still bounded by dialTimeout, so a dead
peer cannot stall the reconnect indefinitely, but a slow Bind is not
capped unless ctx has a deadline or opTimeouts.Bind > 0.
conn.GetUser(ctx, "jdoe", baseDN)
│
├── catalogAdapter.SearchUserFilterTemplate()
│ → "(uid=%[1]s)" → renderFilterTemplate
│ (escapes, Sprintf, ldap.CompileFilter validation)
│ → "(uid=jdoe)"
├── catalogAdapter.UserAttrs()
│ → []string{"cn", "mail", ...}
│
▼
searchSingleEntryLocked(ctx, kindUser, "jdoe", baseDN, template, attrs)
│
│ SizeLimit is hardcoded to 2 (ambiguity probe);
│ TimeLimit comes from conn.searchLimits.
▼
withCtxResult(ctx, opTimeouts.Search, …)
│
│ watchdog goroutine closes the connection
│ if ctx is cancelled mid-flight
▼
conn.connection.Search(req) ─────────▶ LDAP server
│
▼
0 entries → ErrNotFoundOrNotVisible
("user \"jdoe\" not found or not visible")
1 entry → types.LDAPUser{DN, Attributes}
>1 entries → ErrAmbiguousResult
("too many entries for user ...")
Filter templates are validated before the round-trip:
renderFilterTemplate rejects arity mismatches (the %!s(MISSING)
marker) and structural problems (via ldap.CompileFilter) with
ErrInvalidArgument. A broken adapter is surfaced at the call site,
not as a mysterious "Bad search filter" from the server.
Note on ambiguity: single-object lookups use
ScopeWholeSubtree + SizeLimit=2. On MSAD and Samba AD DC, a user
and a contact/computer object with the same sAMAccountName in
different OUs will trigger ErrAmbiguousResult even though the
caller only cared about the user. See
Adapter-specific gotchas.
conn.GetCatalogTree(ctx, baseDN, maxDepth)
│
▼
loop:
Search(baseDN, WholeSubtree, paging=1000)
│
├── server echoes ControlPaging → read cookie
│
▼
next request with cookie … until empty cookie
│
▼
allEntries []*ldap.Entry
│
▼
for each entry:
catalogAdapter.GetNormalizedOUInfoFromEntry(entry)
drop if !baseDN.AncestorOfFold(entry.DN) (caseIgnoreMatch)
byDN[canonicalDN(info.DN)] = info
│
▼
attach each node to parentDNOf(info.DN) → tree roots
│
▼
DFS walk respects maxDepth (0 = no limit)
│
▼
types.CatalogTree{Tree: [...]}
Two important details:
canonicalDNnormalises attribute types and values to lowercase, sorts multi-valued RDN attributes bytype=value, trims whitespace, preserves RDN order, and escapes,,+,=, and\inside attribute values. Without the last step, two structurally different DNs would collide at a join point (cn=a,ou=b— two RDNs — vscn=a\,ou=b— one RDN whose value isa,ou=b). The output is an opaque equivalence key, never re-parsed as a DN.AncestorOfFold(notAncestorOf) is used so case-differing ancestors still match — otherwise subtree entries with different attribute-type case would be silently dropped.
This is the whole reason GetCatalogTree is O(1) round-trips instead of
O(N): the server streams pages of the subtree, and the parent-child
wiring is done locally in O(N) with a single map[string]*OUNormalizedInfo.
Partial results are not returned from the tree functions. If the
server returns SizeLimitExceeded, budget exhaustion, or any other
error after at least one page, searchPaged preserves the entries
collected so far and returns ErrPartialResult together with those
entries — but WalkCatalogTree / GetCatalogTree currently discard
the partial data and return an empty types.CatalogTree (non-nil, empty
slice) plus the error. Callers who need the partial data must drive
searchPaged themselves (via CustomSearchPaged) and assemble the tree
on their side. GetCatalogTree's contract is: "non-nil tree, empty on
failure, complete on success".
LDAPper is created with one of two constructors. Both take a
context.Context as their first argument; that context governs the
initial dial (and, for the authenticated variant, the initial Bind).
// Unauthenticated: dials a host but performs no bind. Suitable for
// directories that allow anonymous reads.
func NewLDAPper(
ctx context.Context,
hosts []string,
port uint,
useLDAPS, skipStartTLS bool,
tlsCfg *tls.Config,
catalogAdapter interfaces.CatalogAdapter,
opts ...Option,
) (*LDAPper, error)
// Authenticated: dials, then binds as `username` and probes `baseDN`
// with a base-object search to confirm the tree is reachable under the
// bound identity. No post-bind user lookup is performed, so service
// principals that do not match UserObjectFilter are accepted. No user
// entry is returned — call GetUser to fetch it. `username` and `baseDN`
// are cached for later CheckUserCredentials calls.
func NewAuthenticatedLDAPper(
ctx context.Context,
username, password, baseDN string,
hosts []string,
port uint,
useLDAPS, skipStartTLS bool,
tlsCfg *tls.Config,
catalogAdapter interfaces.CatalogAdapter,
opts ...Option,
) (*LDAPper, error)TLS matrix — useLDAPS and skipStartTLS together select the transport:
useLDAPS |
skipStartTLS |
tlsCfg |
Result |
|---|---|---|---|
true |
false |
required | LDAPS — TLS from the first byte |
true |
true |
— | rejected (ErrInvalidArgument) |
false |
false |
required | Plain dial followed by StartTLS |
false |
true |
ignored | Plaintext LDAP (no TLS) |
Other constructor rules:
- Host failover — hosts are tried in order until one accepts the
connection. The list is capped at
constants.DefaultMaxHostsLength(50) to guard against accidentally passing a million hosts. - Port — must be in
[1, 65535]. - Host strings — validated against URL-special characters. IPv4
literals (
1.2.3.4) and IPv6 literals (2001:db8::1, unbracketed) are accepted; brackets are added byformatHostbefore the URI is built. IPv6 scoped literals (fe80::1%eth0) are rejected —net.ParseIPdoes not parse the%zonesuffix, and scope IDs are link-local-only. Use a globally routable IPv6 address instead. dialConnis a package-level variable (of typeDialFunc) so tests can swap it out viawithDialConn(t, fn)without touching production code.
Mutual TLS works with no LDAPper-specific API: the *tls.Config passed
to NewLDAPper / NewAuthenticatedLDAPper is Clone()d and then used
verbatim by both transports —
- LDAPS (
useLDAPS=true) —tls.Dialer{Config: tlsCfg}indialHost. - StartTLS (
useLDAPS=false, skipStartTLS=false) —conn.StartTLS(tlsCfg)immediately after the plain dial.
Client authentication is configured exactly as in the standard library:
put the client certificate into tls.Config.Certificates (or
tls.Config.GetClientCertificate), and the CA that signed the server
certificate into tls.Config.RootCAs.
NewLDAPper clones the provided *tls.Config and, if
tlsCfg.ServerName == "", sets it to hosts[0]. This matches the
common HA deployment where every host resolves to a certificate carrying
the same SAN (name). The following two cases need explicit attention:
- Same SAN across hosts — the default is correct; no action needed.
- Different SANs across hosts (e.g. internal vs external names in a
mixed failover list) — set
tlsCfg.ServerNameexplicitly before calling the constructor. If you leave it empty, failover tohosts[1..]will fail TLS verification with a name-mismatch error, because the client will still expect a certificate forhosts[0].
cert, err := tls.LoadX509KeyPair("client.crt", "client.key")
if err != nil {
log.Fatalf("load client cert: %v", err)
}
caPool := x509.NewCertPool()
caPEM, err := os.ReadFile("ca.crt")
if err != nil {
log.Fatalf("read CA: %v", err)
}
if !caPool.AppendCertsFromPEM(caPEM) {
log.Fatal("CA bundle contains no valid certificates")
}
tlsCfg := &tls.Config{
// Optional for LDAPS: ServerName is inferred from the dialed host.
// Set it explicitly when `hosts` holds an IP or a name that does not
// appear in the server certificate's SAN.
ServerName: "ldap.example.com",
RootCAs: caPool,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}
conn, err := ldapper.NewAuthenticatedLDAPper(
ctx,
"svc-reader@corp.example.com", "secret", "DC=corp,DC=example,DC=com",
[]string{"ldap.example.com"}, 636,
true, // useLDAPS
false, // skipStartTLS (must be false when useLDAPS is true)
tlsCfg,
adapters.MSADAdapter{},
)go-ldap calls tls.Client(conn, cfg) without a ServerName hint, and
the standard library rejects the handshake with
tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config
unless one of the two fields is set. LDAPper's auto-fill (see
ServerName and multi-host failover)
covers the common case, but you should still set ServerName
explicitly if your failover list spans multiple certificates.
InsecureSkipVerify: true defeats the point of TLS — prefer
ServerName.
tlsCfg := &tls.Config{
ServerName: "ldap.example.com", // required for StartTLS
RootCAs: caPool,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}
conn, err := ldapper.NewLDAPper(
ctx, []string{"ldap.example.com"}, 389,
false, // useLDAPS
false, // skipStartTLS → StartTLS upgrade
tlsCfg,
adapters.OPENLDAPAdapter{},
)NewLDAPper stores a Clone() of the provided tls.Config. The clone
is shallow — the Certificates slice and the DER bytes inside each
tls.Certificate are shared with the caller — so do not mutate the
certificate bytes after passing them in. For hot rotation (common with
short-lived mTLS certificates issued by Vault / SPIRE / a custom CA),
use tls.Config.GetClientCertificate instead of Certificates; the
callback is invoked on every handshake, and Clone() copies it by
reference:
type certRotator struct {
mu sync.RWMutex
cur *tls.Certificate
}
func (r *certRotator) load(crtPath, keyPath string) error {
c, err := tls.LoadX509KeyPair(crtPath, keyPath)
if err != nil {
return err
}
r.mu.Lock()
r.cur = &c
r.mu.Unlock()
return nil
}
func (r *certRotator) get(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
r.mu.RLock()
defer r.mu.RUnlock()
return r.cur, nil
}
tlsCfg := &tls.Config{
ServerName: "ldap.example.com",
RootCAs: caPool,
GetClientCertificate: rotator.get,
MinVersion: tls.VersionTLS12,
}To pick up new material, reload it in the rotator and call
conn.Reconnect(ctx): reconnect performs a fresh TLS handshake against
ldapper.tlsCfg, so the callback serves the new certificate
immediately. Cached bind credentials (see Connections)
survive the reconnect — Reconnect replays the bind before publishing
the connection, and a failed rebind still invalidates auth state and
returns ErrInvalidCredentials, exactly as for non-mTLS connections.
TLS handshake failures — including "server rejected client certificate"
— flow through classifyLDAPError and surface as ErrUnavailable
(transport class). This is the case in both directions:
*tls.CertificateVerificationError— the client rejected the server certificate (unknown CA, wrong SAN, expired, …).tls.AlertError— the server rejected the client certificate (e.g.bad_certificateduring an mTLS handshake).
Both are recognised by isTransportError (see
ldapper_errors.go) and therefore reachable via
errors.Is(err, ErrUnavailable). For diagnostics of the underlying
cause, use errors.As:
var cvErr *tls.CertificateVerificationError
if errors.As(err, &cvErr) {
log.Printf("TLS verification failed: %v", cvErr)
}
var alertErr tls.AlertError
if errors.As(err, &alertErr) {
log.Printf("TLS alert: %v", alertErr)
}Note:
tls.Config.ClientAuthis a server-side field. Setting it on the client has no effect — the certificate presented by the client is selected fromCertificates/GetClientCertificate, not byClientAuth.
All directory-specific behavior lives in an adapter — a value that
implements interfaces.CatalogAdapter:
type CatalogAdapter interface {
// Cast / convert
GetNormalizedUserInfo(ldapUser types.LDAPUser) *types.UserNormalizedInfo
GetNormalizedOUInfoFromEntry(entry ldap.Entry) *types.OUNormalizedInfo
// Object-class filters
UserObjectFilter() string
GroupObjectFilter() string
OUObjectFilter() string
// Search filter templates
//
// Contract: templates use fmt-style positional verbs (%[N]s). All
// arguments are escaped with ldap.EscapeFilter by LDAPper before
// substitution, so implementers must return raw (unescaped) attribute
// values. LDAPper additionally validates the substituted filter via
// ldap.CompileFilter and rejects arity mismatches with
// ErrInvalidArgument before the round-trip.
SearchUserFilterTemplate() string // 1 arg
SearchUserGroupsFilterTemplate() string // N args, use with the args helper
SearchUserGroupsFilterTemplateArgs(ldapUser types.LDAPUser) []any
SearchOUFilterTemplate() string // 1 arg
SearchGroupFilterTemplate() string // 1 arg
// Attribute lists
UserAttrs() []string
OUAttrs() []string
GroupAttrs() []string
// BindIdentity converts a caller-supplied login into the principal
// that is passed verbatim to LDAPConn.Bind. See "Writing your own
// adapter" below for the contract.
BindIdentity(login, baseDN string) (string, error)
}Some adapters additionally implement interfaces.PrimaryGroupResolver:
type PrimaryGroupResolver interface {
ResolvePrimaryGroupDN(ctx context.Context, user types.LDAPUser, ldapConn LDAPConn) (string, error)
}GetUserGroups checks for this interface via a type assertion. Only
MSADAdapter and SAMBAAdapter implement it (for AD's primaryGroupID
resolution); the others deliberately do not, because their schemas have
no such concept and the presence of the interface would cause
GetUserGroups to try to resolve a non-existent attribute.
PrimaryGroupResolver implementations MUST use only the provided
LDAPConn and MUST NOT call LDAPper methods — the resolver is
invoked while opMu is already held, so any re-entry into LDAPper
would deadlock.
Timeout semantics. The resolver is invoked inside a single
withCtx(ctx, opTimeouts.Search, …) call. The timeout therefore applies
per-request (each LDAPConn.Bind / LDAPConn.Search on the
supplied connection) — it does not cap the resolver's total
wall-clock time. A resolver doing N sequential round-trips can take up
to N × opTimeouts.Search. For a hard end-to-end bound, attach a
context.WithTimeout to the GetUserGroups call.
The adapter is the only place LDAPper assumes anything about schema. See Shipped adapters for the adapters that ship with the library.
Every search request is built by a single helper that applies
connection-level SearchLimits:
type SearchLimits struct {
SizeLimit int // 0 = server default
TimeLimit int // seconds; 0 = server default
MaxPages int // 0 = no page budget
MaxEntries int // 0 = no entry budget
RequirePaging bool // true = reject paged results without paging control
}conn, _ := ldapper.NewLDAPper(
ctx, hosts, 636, true, false, tlsCfg, adapter,
ldapper.WithSearchLimits(ldapper.SearchLimits{
SizeLimit: 500,
TimeLimit: 30,
RequirePaging: true,
}),
)
// Later, temporarily raise limits for one heavy pass:
old := conn.GetSearchLimits()
conn.SetSearchLimits(ldapper.SearchLimits{TimeLimit: 120})
tree, _ := conn.GetCatalogTree(ctx, "DC=corp,DC=example,DC=com", 0)
conn.SetSearchLimits(old)Field semantics:
SizeLimit— server-enforced total across the entire paged operation (RFC 2696 semantics).0means "no client-side size limit; use the server default".TimeLimit— server-side time limit in seconds.0means "no client-side time limit; use the server default".MaxPages— client-side page budget. When exceeded,searchPagedreturnsErrPartialResulttogether with the entries collected so far.MaxEntries— client-side entry budget. ComplementsSizeLimit: it works even against servers that ignoreSizeLimit. Truncates the slice and returnsErrPartialResult.RequirePaging— whentrue, a server that ignores the paging control is treated as a protocol violation andErrPartialResultis returned. Defaults totrueinDefaultSearchLimits(). Enable this when completeness matters (e.g. security decisions based on "user belongs to ALL groups").
Negative values are clamped to 0. SetSearchLimits takes opMu, so
it is safe to call concurrently with searches — but the value change
takes effect on the next request.
NB: WithSearchLimits replaces the entire struct. Fields
omitted from the argument get Go zero values, not the values from
DefaultSearchLimits(). In particular:
TimeLimit: 0means "no server-side time limit" (not 30 seconds);RequirePaging: falsemeans "silently accept missing paging control" (nottrue).
Use the read-modify-write pattern with DefaultSearchLimits() if you
want to keep the defaults for a subset of fields:
limits := ldapper.DefaultSearchLimits()
limits.SizeLimit = 100
conn, _ := ldapper.NewLDAPper(..., ldapper.WithSearchLimits(limits))MaxPages is also the primary defence against a misbehaving paging
server. searchPaged tracks every distinct paging cookie it has seen
(to catch A→B→A cycles) in an in-memory set. When MaxPages == 0 that
set has no upper bound: a hostile or buggy server that returns an
endless sequence of fresh cookies will inflate it indefinitely. Callers
that must resist such a server should set MaxPages (or MaxEntries)
to a finite budget; searchPaged will then abort with ErrPartialResult
before the set can grow.
conn, _ := ldapper.NewLDAPper(
ctx, hosts, 389, false, true, nil, adapter,
ldapper.WithDialTimeout(5*time.Second),
)
// At runtime:
conn.SetDialTimeout(10 * time.Second)
fmt.Println(conn.GetDialTimeout()) // 10s- After construction:
dialTimeout == constants.DefaultDialTimeout(10s). This is the actual default, not 0. - Explicitly setting it to
0(viaWithDialTimeout(0)orSetDialTimeout(0)) means "no explicit timeout; use Go'snet.Dialerdefault". - Negative values are clamped to
0. - The timeout is applied as
net.Dialer.Timeoutduring the initial dial and re-applied during theStartTLShandshake (so the handshake cannot block indefinitely). After the handshake,SetTimeout(0)resets the connection so subsequent operations are not capped by the dial timeout. ReconnectandEnsureAliveuse the same dial timeout.
OperationTimeouts are client-side timeouts applied around
individual Bind and Search calls. They complement — but do not
replace — SearchLimits.TimeLimit, which is a server-side limit.
type OperationTimeouts struct {
Bind time.Duration
Search time.Duration
}conn, _ := ldapper.NewLDAPper(
ctx, hosts, 389, false, true, nil, adapter,
ldapper.WithOperationTimeout(ldapper.OperationTimeouts{
Bind: 5 * time.Second,
Search: 30 * time.Second,
}),
)- Zero value disables the corresponding timeout (
Bind: 0, Search: 0is the default — backwards compatible). In particular,Reconnectdoes not force a hidden default on the Bind step whenBind == 0: onlyctx(and, for the dial phase,dialTimeout) bounds the call. - Negative durations are clamped to zero.
- The timeout is applied via
LDAPConn.SetTimeout(which is part of theLDAPConninterface contract — every implementation, including mocks, must support it). After the operation, the timeout is reset to zero so it does not cap subsequent operations. - In addition,
withCtx/withCtxResultrespect the caller's context deadline: the effective timeout ismin(opTimeout, ctx.Deadline - now). - For
PrimaryGroupResolveradapters,opTimeouts.Searchis a per-request budget on eachLDAPConn.Searchthe resolver issues, not a total. See Adapters for details.
The eight adapters group into three schema families. Members of the same family share almost all behaviour — the new adapters are implemented by embedding the family's "base" adapter and overriding only the pieces that genuinely differ. This keeps the code (and its tests) small and avoids drift between family members.
| Adapter | Directory | Family | Notes |
|---|---|---|---|
adapters.MSADAdapter |
Microsoft Active Directory | AD | Baseline for the AD family. objectCategory-based filters; recursive group expansion via LDAP_MATCHING_RULE_IN_CHAIN (1.2.840.113556.1.4.1941); prefers thumbnailPhoto over jpegPhoto; implements PrimaryGroupResolver for primaryGroupID. |
adapters.SAMBAAdapter |
Samba 4 AD DC | AD (embeds MSAD) | Samba 4 presents an AD-compatible schema — sAMAccountName, userPrincipalName, objectSid, primaryGroupID, LDAP_MATCHING_RULE_IN_CHAIN (since Samba 4.11). Embeds MSADAdapter; inherits PrimaryGroupResolver. |
adapters.OPENLDAPAdapter |
OpenLDAP (and most RFC-4519 dialects) | OpenLDAP | Baseline for the OpenLDAP family. Three group models — groupOfNames (member), groupOfUniqueNames (uniqueMember), posixGroup (memberUid). User search keyed by uid or entryDN. Short-form bind expands to uid=<login>,<baseDN>. |
adapters.DS389Adapter |
389 Directory Server (and forks) | OpenLDAP (embeds OPENLDAP) | Netscape-derived. Adds nsAccount / nsOrgPerson to the user object filter (they often appear on top of inetOrgPerson / posixAccount), and requests nsAccountLock in UserAttrs. Group model and bind expansion inherited. |
adapters.OpenDJAdapter |
OpenDJ (ForgeRock / Open Identity Platform) | OpenLDAP (embeds OPENLDAP) | Same group model; adds the virtual isMemberOf attribute to UserAttrs — OpenDJ computes it on user entries so callers can read group membership without a separate search. |
adapters.OUDAdapter |
Oracle Unified Directory | OpenLDAP (embeds OpenDJ) | OUD is Oracle's fork of OpenDJ: identical group model, user schema, and virtual isMemberOf. Embeds OpenDJAdapter; exists so call sites read naturally and OUD-specific quirks can be added later. |
adapters.ALDPROAdapter |
ALD Pro (Astra Linux Directory Pro) | FreeIPA-style | Recognizes rbta-org-unit alongside organizationalUnit; department from rbtadp; two-branch group filter (member/memberIndirect DN + memberUid uid). Bind is pass-through (UPN/DN). |
adapters.FREEIPAAdapter |
FreeIPA | FreeIPA | Recognizes nsContainer alongside organizationalUnit; department is intentionally not mapped; two-branch group filter. Short-form bind expanded to uid=<login>,cn=users,cn=accounts,<baseDN>, with structural RDN detection (case-insensitive; substring matching was a bug we fixed). |
All eight adapters are stateless and safe to reuse across connections.
-
MSADAdapter/SAMBAAdapter—GetUsermatches onsAMAccountName/userPrincipalName/distinguishedName. The value passed toNewAuthenticatedLDAPpermust be a valid AD principal (UPN, sAMAccountName, or DN). Group search usesLDAP_MATCHING_RULE_IN_CHAIN, which Samba 4 AD DC and Microsoft AD both support — OpenLDAP, 389-DS, OpenDJ, OUD and FreeIPA do not.Ambiguity risk:
GetUser/GetGroup/GetOUuseScopeWholeSubtree+SizeLimit=2to detect duplicates. On AD and Samba, a user and acontact/computerobject with the samesAMAccountName(across different OUs) both match the user filter, so the lookup fails withErrAmbiguousResult. Pass a fully qualified principal (UPN or DN) or narrow thebaseDNto the user's OU when this happens.Samba container gotcha (integration tests only): the Samba 4 AD DC container needs
Privileged: trueand a proper FQDN inHostname; the suite gracefully skips if the container cannot start. -
OPENLDAPAdapter/DS389Adapter/OpenDJAdapter/OUDAdapter— the user filter uses the indexed placeholder%[1]sso the same argument is substituted into bothuid=andentryDN=, allowing lookup by either short name or full DN. The user-groups filter is a union of the group models:(|(&(objectClass=groupOfNames)(member=%[1]s)) (&(objectClass=groupOfUniqueNames)(uniqueMember=%[1]s)) (&(objectClass=posixGroup)(memberUid=%[2]s)))with args
[user.DN, user.uid]. DS389 overridesUserObjectFilterandUserAttrs; OpenDJ and OUD override onlyUserAttrs. The group filter is inherited. -
DS389Adapter— 389-DS exposes account state viansAccountLock: true. Unlike OpenLDAP, the attribute is on the user entry, so it is included inUserAttrs. The user filter additionally includesnsAccount/nsOrgPerson— they commonly appear alongsideinetOrgPersonon 389-DS entries. A container gotcha for the integration suite: 389-DS listens on:3389, not:389. -
OpenDJAdapter/OUDAdapter— OpenDJ computes the virtualisMemberOfattribute on user entries. Requesting it inUserAttrslets callers read group membership directly from the user entry. It is harmless on servers that do not implement it (the attribute is simply absent), which keeps the adapter usable against plain OpenLDAP too. -
ALDPROAdapter— the user-groups filter:(|(&(|(objectClass=groupOfNames)(objectClass=ipaUserGroup)(objectClass=posixGroup)) (|(member=%[1]s)(memberIndirect=%[1]s))) (&(objectClass=posixGroup)(memberUid=%[2]s)))with args
[user.DN, user.uid]. -
FREEIPAAdapter— the user-groups filter uses the same shape as ALDPro (two positional args, DN and uid). -
BindIdentity:MSAD/SAMBA/ALDPro— pass-through. The server performs the mapping.OPENLDAP/389-DS/OpenDJ/OUD— short-form login is expanded touid=<login>,<baseDN>.FreeIPA— short-form login is expanded touid=<login>,cn=users,cn=accounts,<baseDN>(the chain is built deepest-first:accounts→users→uid). Whether eachcn=...node is added is decided by structural RDN comparison (case- insensitive, single-valued RDN), not by substring matching the DN string.
-
GetNormalizedOUInfoFromEntry.Name— all shipped adapters deriveNamefrom the first RDN ofentry.DNviaouNameFromDN. The helper does not check the RDN attribute type: it returns the first attribute's value whatever it is (ou=sales→"sales",cn=users→"users"). It is meant for display (tree labels, log lines), not identification. Malformed DNs fall back to the raw string. -
GetNormalizedOUInfoFromEntry.OU—MSADandSAMBAsetOUto the full entry DN; the other six adapters read theouattribute, withcnfallback for ALDPro'srbta-org-unitand FreeIPA'snsContainer.
DistinguishedName is always taken from the LDAPUser.DN struct field,
never from an attribute. Every adapter reads the remaining fields from LDAP
attributes; — means "hardcoded to empty" (attribute not read even if
present).
UserNormalizedInfo field |
MSAD / SAMBA | OpenLDAP family (OPENLDAP, 389-DS, OpenDJ, OUD) | ALD Pro | FreeIPA |
|---|---|---|---|---|
DistinguishedName |
struct DN |
struct DN |
struct DN |
struct DN |
CommonName |
cn |
cn |
cn |
cn |
Mobile |
mobile |
mobile |
mobile |
mobile |
Phone |
telephoneNumber |
telephoneNumber |
telephoneNumber |
telephoneNumber |
Mail |
mail |
mail |
mail |
mail |
Department |
department |
departmentNumber |
rbtadp |
— |
Title |
title |
title |
title |
title |
Manager |
manager |
manager |
manager |
manager |
Photo |
thumbnailPhoto → jpegPhoto |
jpegPhoto |
jpegPhoto |
jpegPhoto |
Company |
company |
o |
o |
o |
Country |
co |
— | co |
co |
Address |
streetAddress |
postalAddress |
streetAddress |
street |
City |
l |
— | l |
l |
Index |
postalCode |
— | postalCode |
postalCode |
Key differences to keep in mind:
Department— four different sources (department,departmentNumber,rbtadp, or none). FreeIPA simply does not map it.Address— MSAD/Samba/ALDPro usestreetAddress, OpenLDAP family usespostalAddress, FreeIPA usesstreet.Company— MSAD/Samba use thecompanyattribute, the others useo.Photo— only MSAD/Samba fall back fromthumbnailPhototojpegPhoto; the others readjpegPhotoonly and do not consultthumbnailPhoto.Country/City/Index— OpenLDAP family hardcodes these to empty regardless of the entry's attributes.
// Bind as `username` and store the credentials + base DN for later use.
err := conn.BindConn(ctx, username, password, baseDN)
// Verify a user's password against an already-authenticated connection,
// then rebind back to the service account.
user, err := conn.CheckUserCredentials(ctx, username, password)BindConn:
- Stores username, password and baseDN on the connection.
- Asks the adapter's
BindIdentityfor the actual Simple Bind principal, then performs the bind, then probesbaseDNwith a base-object search to confirm the tree is reachable under the bound identity. No post-bind user lookup is performed, and no*types.LDAPUseris returned — callGetUserafterwards if you need the user entry. - If
BindIdentityfails, the adapter's error is wrapped with%w, so callers canerrors.Is/errors.Asinto it. If the subsequent bind fails, the error isErrInvalidCredentials(or another bind-path sentinel; testable witherrors.Is). - On success, caches
isAuthenticated=true,bindUser,bindPassword,bindDN.bindUseris the value returned byBindIdentity, not the raw login — that's whatReconnectreplays. - On any failure, closes the connection and invalidates the cached credentials (RFC 4513 §4 — a failed Bind transitions the session to anonymous; a successful Bind whose base-DN probe failed leaves the session identified as an unverified principal).
CheckUserCredentials:
- Requires
isAuthenticated == true(i.e.BindConnorNewAuthenticatedLDAPperwas called) and a live connection. Otherwise returnsErrNotAuthenticatedorErrClosedrespectively. - Uses the stored
bindDNas the base DN for the internal user lookup — callers do not repeat it. - User enumeration protection: a missing user is returned as
ErrInvalidCredentials, indistinguishable from a wrong password. - Returns
niluser on any failure — a returned*LDAPUseralways implies a successful verification. Do not checkuser != nilalone as an auth signal. - Restores the service bind via a deferred rebind. If the rebind itself
fails,
isAuthenticatedis cleared and the error surfaces asErrNotAuthenticated(or is appended to the primary error). If the rebind was aborted byctxcancellation, however, the rawcontext.Canceled/context.DeadlineExceededis surfaced instead —ErrNotAuthenticatedwould mislead a caller who already decided to tear down.
All bind-path errors go through classifySensitiveBindError, which never
echoes raw server text — passwords cannot leak into logs.
// Escape hatch: build any *ldap.SearchRequest yourself. No DN validation,
// no paging, no search-limit overrides — the caller owns semantics.
res, err := conn.CustomSearch(ctx, &ldap.SearchRequest{...})
// Same, with automatic paging applied on top of the request's controls.
// Base DN is validated; result is the concatenation of all pages.
entries, err := conn.CustomSearchPaged(ctx, &ldap.SearchRequest{...})
// Fetch a single user by the adapter's user filter template.
user, err := conn.GetUser(ctx, "jdoe", baseDN)
// Fetch DNs of all groups the user belongs to.
groups, err := conn.GetUserGroups(ctx, "jdoe", baseDN)
// Fetch a single group by its cn.
group, err := conn.GetGroup(ctx, "admins", baseDN)
// Fetch a single OU by its ou attribute value (e.g. "Sales").
ou, err := conn.GetOU(ctx, "Sales", baseDN)
// Fetch every user under an OU DN (subtree search, adapter user filter).
users, err := conn.GetOUUsers(ctx, "OU=Staff,DC=corp,DC=example,DC=com")-
GetUser,GetGroup,GetOU— single-object lookups. Each usesSizeLimit=2internally to detect duplicates and returns:ErrNotFoundOrNotVisibleif zero entries match,ErrAmbiguousResultif more than one entry matches.
ErrNotFoundOrNotVisibleis returned (notErrNotFound) because LDAP cannot distinguish "the object does not exist" from "the object is hidden by ACL". Never infer "does not exist" from this error in security-sensitive paths. -
GetUserGroups— callsGetUserfirst, then applies the adapter'sSearchUserGroupsFilterTemplate/SearchUserGroupsFilterTemplateArgswith[]string{"1.1"}(OID for "no attributes"), so only DNs come back from the server. Returns group DNs (notcnvalues). If the adapter implementsPrimaryGroupResolver(MSAD and Samba), the primary group DN is appended to the result. See Adapters for the per-request timeout semantics of the resolver. -
GetOUUsers— takes a DN, not a shortouname. Uses the adapter'sUserObjectFilterandUserAttrsto list all users under the subtree, subject to paging. -
GetOU— takes the shortouvalue (e.g."Sales") plus a base DN. -
GetGroup— takes the shortcnvalue.
Filter templates are validated before the search is sent: an arity
mismatch (%!s(MISSING)) or a structurally broken filter surfaces as
ErrInvalidArgument with the adapter template quoted in the message. See
Writing your own adapter for the
adapter-side obligations this depends on.
Asymmetry between CustomSearch and CustomSearchPaged: the two
methods check their inputs in different orders.
CustomSearchPagedrejects anil*ldap.SearchRequestbefore acquiringopMuand before runningrequireConnLocked. The nil request is treated as a programming error and reported asErrInvalidArgumentregardless of connection state.CustomSearchacquiresopMuand callsrequireConnLockedfirst, then rejectsnil. An uninitialized or closedLDAPperreportsErrClosed, even when the caller also passed a nil request.
Both behaviours are pinned by tests (TestCustomSearchPaged_NilRequest,
TestCustomSearch_NilRequest, TestCustomSearch_ConnRequired) and are
intentional — do not "unify" them without adjusting those tests.
All of these are safe to call concurrently: opMu serializes the
underlying LDAP operations.
tree, err := conn.GetCatalogTree(ctx, "DC=corp,DC=example,DC=com", 2)- Performs one paged subtree search (
ControlPaging, page sizeconstants.DefaultCatalogSearchPageSize = 1000) and builds theOUtree on the client. - Filters out entries that are not strict descendants of
baseDN(vialdap.DN.AncestorOfFold) — misbehaving servers and cross-tree referrals cannot leak foreign entries into the result. - Skips malformed DNs returned by the server rather than aborting.
- Nodes whose parent is not in the fetched set (e.g. server-side
filtering) are promoted to roots of
tree.Tree. maxDepthtrims the resulting tree:0= no limit,1= roots only,2= roots + first level, etc. FormaxDepth == 1, the tree is fetched withScopeSingleLevel— a single round-trip, no full subtree read.- On failure the returned
types.CatalogTree.Treeis non-nil but empty — the tree builder deliberately discards any partial state, so a caller trustingtree.Treenever sees half a hierarchy. If you need partial data onErrPartialResult(e.g.SizeLimitExceededafter the first page), driveCustomSearchPagedyourself:searchPagedreturns the entries collected so far alongsideErrPartialResult, and the wiring above is just the samemap[string]*OUNormalizedInfodance.
// Root-level OUs only (single-level scope, one round-trip):
roots, err := conn.ListSingleLevelOUs(ctx, "DC=corp,DC=example,DC=com")
// Direct children of an OU (same as ListSingleLevelOUs, but explicit):
children, err := conn.GetSubOUs(
ctx,
"OU=Staff,DC=corp,DC=example,DC=com",
ldap.ScopeSingleLevel,
)
// All descendants of an OU (subtree scope):
descendants, err := conn.GetSubOUs(
ctx,
"OU=Staff,DC=corp,DC=example,DC=com",
ldap.ScopeWholeSubtree,
)
// Depth-first walk without building a tree:
err := conn.WalkCatalogTree(ctx, baseDN, 0,
func(ctx context.Context, depth int, node *types.OUNormalizedInfo) error {
log.Printf("%*s%s", depth*2, "", node.Name)
return nil
})GetSubOUs accepts only ldap.ScopeSingleLevel and
ldap.ScopeWholeSubtree; ldap.ScopeBaseObject is rejected with
ErrInvalidArgument (it would always return an empty list). The base DN
itself is always excluded — "sub" means strict descendants.
if !conn.IsAlive(ctx) {
_ = conn.Reconnect(ctx)
}
// Or the combined form, suitable as a pre-flight check:
if err := conn.EnsureAlive(ctx); err != nil {
return err
}IsAlive(ctx)— checks the local connection state (IsClosing) and issues a cheap root-DSE search with hard-codedSizeLimit=1, TimeLimit=1. Intentionally independent of the user-configured search limits.Reconnect(ctx)— closes the current connection, dials any host inhostsagain, and — if the connection was authenticated — re-binds with the stored credentials. On rebind failure, the new connection is closed,isAuthenticatedis cleared, and subsequent operations returnErrCloseduntilBindConnis called again.bindDNis preserved, soCheckUserCredentialscontinues to work after a successful reconnect. IfopTimeouts.Bind == 0, the rebind applies no client-side Bind timeout (same contract aswithCtx); the dial itself is still bounded bydialTimeout.EnsureAlive(ctx)—IsAlive()first; reconnect only when stale. AfterClose()the client is terminal:EnsureAlivereturnsErrClosedand does not attempt to reconnect. That is the documented contract — a closed client cannot be revived.Close()— idempotent, best-effort (swallowsCloseerrors). Setsclosed=truewhich is terminal.
All public functions return sentinel errors that can be tested with
errors.Is. Where sensible, the original *ldap.Error / net.Error is
preserved in the wrapped chain (errors.As still works).
| Sentinel | Meaning |
|---|---|
ErrInvalidArgument |
Config or argument validation failed (empty/invalid DN, empty username, invalid host, invalid port, useLDAPS && skipStartTLS, nil TLS config, nil adapter, adapter-supplied filter template arity mismatch / malformed filter, …). |
ErrInvalidCredentials |
Bind rejected as bad credentials. Also returned by CheckUserCredentials when the user is missing (enumeration protection). |
ErrInvalidCredentialsOrUnknown |
Bind-path classifier could not map the LDAP result code to a specific sentinel. Treat as inconclusive, not as a rejection. |
ErrNotAuthenticated |
CheckUserCredentials called on a connection without a successful prior BindConn/NewAuthenticatedLDAPper, or the service rebind was lost (non-cancellation failure). |
ErrUnavailable |
Directory unreachable (dial failure, LDAPResultUnavailable/Busy/ServerDown, transport errors — including TLS handshake failures from tls.CertificateVerificationError and tls.AlertError). |
ErrTimeout |
Timeout / deadline exceeded (LDAPResultTimeout, LDAPResultTimeLimitExceeded, net.Error.Timeout, context.DeadlineExceeded). |
ErrClosed |
The connection is nil (never initialized, or Close() was called, or a failed reconnect left it nil). |
ErrNotFound |
Search produced a definitive "no such object" (LDAPResultNoSuchObject). Rare — subtree searches usually return zero entries instead. |
ErrNotFoundOrNotVisible |
Subtree search returned zero entries. The object may not exist, or may be hidden by ACL. Do not infer "does not exist". |
ErrAmbiguousResult |
Single-object lookup matched more than one entry. |
ErrPartialResult |
Paged search was interrupted by SizeLimitExceeded, budget exhaustion, or a missing paging control (when RequirePaging is true). The returned slice/tree is partial. |
ErrInsufficientAccess |
LDAP returned LDAPResultInsufficientAccessRights. Surfaced by both classifyLDAPError (search path) and classifySensitiveBindError (bind path). |
Bind-path failures never wrap the raw LDAP text — this prevents
credential leakage into logs when the server echoes back the submitted
password. All bind errors are mapped to bare sentinels by
classifySensitiveBindError.
BindConn can return ErrNotFoundOrNotVisible if baseDN is unreachable
under the bound identity (for example, when the bind principal has no
read access to the tree root). This is produced by the base-DN probe,
not by a user lookup.
CheckUserCredentials can return context.Canceled /
context.DeadlineExceeded when the caller aborts during the service
rebind — the raw ctx error is surfaced rather than masked as
ErrNotAuthenticated. See Authentication.
Non-bind failures use classifyLDAPError, which wraps both the sentinel
and the original error:
if errors.Is(err, ldapper.ErrNotFoundOrNotVisible) { /* ... */ }
var ldapErr *ldap.Error
if errors.As(err, &ldapErr) {
log.Printf("LDAP result code: %d", ldapErr.ResultCode)
}Implement interfaces.CatalogAdapter for your directory. The adapter is
stateless and safe to reuse across connections. Skeleton:
type MyAdapter struct{}
func (MyAdapter) GetNormalizedUserInfo(u types.LDAPUser) *types.UserNormalizedInfo {
return &types.UserNormalizedInfo{
DistinguishedName: u.DN,
CommonName: u.GetAttributeValue("cn"),
Mail: u.GetAttributeValue("mail"),
}
}
func (MyAdapter) GetNormalizedOUInfoFromEntry(e ldap.Entry) *types.OUNormalizedInfo {
return &types.OUNormalizedInfo{
DN: e.DN,
Name: ouNameFromDN(e.DN), // first RDN value
OU: e.GetAttributeValue("ou"),
Children: make([]*types.OUNormalizedInfo, 0),
}
}
func (MyAdapter) UserObjectFilter() string { return "(&(objectClass=inetOrgPerson))" }
func (MyAdapter) GroupObjectFilter() string { return "(objectClass=groupOfNames)" }
func (MyAdapter) OUObjectFilter() string { return "(objectClass=organizationalUnit)" }
func (MyAdapter) SearchUserFilterTemplate() string { return "(&(objectClass=inetOrgPerson)(uid=%[1]s))" }
func (MyAdapter) SearchGroupFilterTemplate() string { return "(&(objectClass=groupOfNames)(cn=%[1]s))" }
func (MyAdapter) SearchOUFilterTemplate() string { return "(&(objectClass=organizationalUnit)(ou=%[1]s))" }
func (MyAdapter) SearchUserGroupsFilterTemplate() string {
return "(&(objectClass=groupOfNames)(member=%[1]s))"
}
func (MyAdapter) SearchUserGroupsFilterTemplateArgs(u types.LDAPUser) []any {
return []any{u.DN}
}
func (MyAdapter) UserAttrs() []string { return []string{"uid", "cn", "mail"} }
func (MyAdapter) OUAttrs() []string { return []string{"ou", "description"} }
func (MyAdapter) GroupAttrs() []string { return []string{"cn", "member"} }
// BindIdentity returns the principal passed verbatim to LDAPConn.Bind.
func (MyAdapter) BindIdentity(login, baseDN string) (string, error) {
if login == "" {
return "", fmt.Errorf("empty login")
}
// Pass-through: server decides whether `login` is a valid principal.
return login, nil
}Rules of thumb:
- Return raw, unescaped values from
SearchUserGroupsFilterTemplateArgs. LDAPper runs each value throughldap.EscapeFilterbefore substitution. Same for the single argument that is passed intoSearchUserFilterTemplate,SearchGroupFilterTemplateandSearchOUFilterTemplate. - Count placeholders carefully.
SearchUserFilterTemplate,SearchGroupFilterTemplateandSearchOUFilterTemplatetake exactly one argument.SearchUserGroupsFilterTemplatemay take any number, butSearchUserGroupsFilterTemplateArgsmust return exactly that many values in the same order. LDAPper validates arity and the compiled filter, returningErrInvalidArgumenton mismatch — this surfaces adapter bugs at the call site rather than as a "Bad search filter" from the server. - Group membership format:
- Schema stores DN-valued
member→ use(member=%[1]s)and return[user.DN]. - Schema stores uid-valued
memberUid→ use(memberUid=%[1]s)and return[user.GetAttributeValue("uid")]. - Both (like
OPENLDAPAdapter/ALDPROAdapter/FREEIPAAdapter) → use a union with two placeholders and return both values in order.
GetNormalizedOUInfoFromEntrymust return a non-nilChildrenslice —GetCatalogTreemay append to it.DistinguishedNameinGetNormalizedUserInfomust come fromldapUser.DN(the struct field), not from an LDAP attribute.BindIdentitymust return something that the server accepts as a Simple Bind principal. If the adapter accepts both DN and short-form logins,SearchUserFilterTemplatemust match both (typically by unioning the schema attribute withentryDN=). SeeOPENLDAPAdapter/FREEIPAAdapterfor examples. Errors returned byBindIdentityare wrapped with%wbyBindConn, so they remain reachable througherrors.Is/errors.As. Do not put credentials in the error message — it will be surfaced to the caller.PrimaryGroupResolver— implement only if your schema has a real concept of "primary group" that is not covered by ordinary group membership (like AD'sprimaryGroupID). The resolver MUST use only the providedLDAPConnand MUST NOT callLDAPpermethods — the lock is already held.opTimeouts.Searchapplies per-request, not as a total — see Adapters.appendRDN(internal helper, used by FreeIPA/OpenLDAP adapters) validatesattrTypeagainst RFC 4514'sdescr/numericoidgrammar before returning. If you build DNs from caller-controlled attribute types in a custom adapter, callappendRDNrather thanldap.DN.String()directly — the latter happily rendersfoo bar=xwhichldap.ParseDNthen rejects.
See adapters/msad_adapter.go, adapters/openldap_adapter.go,
adapters/aldpro_adapter.go and adapters/freeipa_adapter.go for
complete, production-quality examples. For examples of embedding a
family base adapter (SAMBAAdapter embeds MSADAdapter; DS389Adapter
and OpenDJAdapter embed OPENLDAPAdapter; OUDAdapter embeds
OpenDJAdapter), see the corresponding files in adapters/.
Defaults live in constants/constants.go:
| Constant | Default | Meaning |
|---|---|---|
DefaultSearchSizeLimit |
0 |
0 = no client-side size limit |
DefaultSearchTimeLimit |
30 |
Seconds |
DefaultMaxHostsLength |
50 |
Upper bound on the hosts slice |
DefaultCatalogSearchPageSize |
1000 |
Paging control size for paged searches |
DefaultDialTimeout |
10s |
Initial dial / StartTLS-handshake timeout |
DefaultStartTLSTimeout |
30s |
Used when no explicit dial timeout is set |
Options (type Option func(*LDAPper)):
| Option | Effect |
|---|---|
WithSearchLimits(SearchLimits) |
Replace connection-level search limits (whole struct) |
WithDialTimeout(d) |
Override dial / StartTLS-handshake timeout |
WithOperationTimeout(OperationTimeouts) |
Override client-side Bind/Search timeouts |
SetSearchLimits(SearchLimits) |
Runtime replacement of search limits |
GetSearchLimits() |
Read current search limits |
SetDialTimeout(d) |
Runtime override of dial timeout |
GetDialTimeout() |
Read current dial timeout |
SetOperationTimeout(OperationTimeouts) |
Runtime override of operation timeouts |
GetOperationTimeout() |
Read current operation timeouts |
Negative values are clamped to 0 ("no limit"/"Go default"). The zero
value of OperationTimeouts disables the client-side timeout, so it is
backwards compatible with code that does not configure it. In
particular, opTimeouts.Bind == 0 disables the client-side Bind
timeout applied by withCtx and by reconnectLocked — reconnect
does not force constants.DefaultDialTimeout on the Bind step.
With* options are applied by NewLDAPper before the object is
returned to the caller; there is no concurrent access at that point.
Set*/Get* take opMu, so they are safe to call concurrently with
search operations.
MaxPages and MaxEntries are complementary memory guards for paged
searches: MaxPages bounds the number of round-trips (and, indirectly,
the size of the internal seenCookies set), MaxEntries bounds the
total entries accumulated locally. Setting either is recommended for
long-running services that page through large subtrees — leaving both at
0 means "no limit", per the same convention as SizeLimit.
The package is designed for testability at every layer.
Unit tests run without any network access and cover:
- Input validation —
validators_test.go: DN / base DN / host / port / TLS-config / option validation,formatHostIPv4-vs-IPv6,isIPLiteralIP-vs-DNS detection, and the IPv6-scoped-literal limitation. - Core helpers —
helpers_test.go:attrsToMap(including duplicate-name deduplication),escapeFilterArgs,parentDNOf(including multi-valued RDN preservation),canonicalDN(case folding, RDN-order significance, escaped-comma round-trip, structural ambiguity betweencn=a,ou=bandcn=a\,ou=b, malformed fallback),escapeCanonicalValue. - Errors —
ldapper_errors_test.go:classifyLDAPError,classifySensitiveBindError(including the "no secret leak" regression test and the TLS-error classification),isTimeoutError,isTransportError. - Options & limits —
ldapper_options_test.go:With/Set/GetSearchLimits,With/Set/GetDialTimeout,With/Set/GetOperationTimeout, clamping,-raceguards. - Search primitives —
ldapper_search_test.go:searchSingleEntryLocked(including arity and syntax validation viarenderFilterTemplate),searchPaged(single page, multiple pages,MaxPages/MaxEntriesbudgets,RequirePagingon/off, repeated-cookie runaway,SizeLimitExceeded-preserves-entries, context-cancelled during/between pages, raw error propagation),CustomSearch,CustomSearchPaged. - Connection —
ldapper_connection_test.go:runWithWatchdog(happy path, panic path with defer-based cleanup, timeout reset),withCtx/withCtxResult(deadline merging, watchdog, cancellation invalidating the connection),Close,IsAlive,EnsureAlive,Reconnect,dialAnyLocked(including ctx-cancellation between hosts). Tests that swap the package-leveldialConnseam are not parallel — the swap is process-wide. - Auth —
ldapper_auth_test.go:invalidateAuthLocked,restoreServiceBindLocked,bindConnLocked/verifyBaseDNLocked(success path, 0-entry base probe, transport failure),BindConn(includingerrors.Is-reachability ofBindIdentityerrors),CheckUserCredentials(user-not-found masking, restore-failure path, restore-cancellation surfacingcontext.Canceled, context-cancelled user-bind path). - User / group / OU ops —
ldapper_user_ops_test.go,ldapper_group_ops_test.go,ldapper_ou_ops_test.go. - Catalog tree —
ldapper_catalog_tree_discovery_test.go:WalkCatalogTree(DFS order, depth truncation, deduplication, callback errors, context cancellation),GetCatalogTree,GetSubOUs,ListSingleLevelOUs. - Constructors —
ldapper_test.go:NewLDAPper/NewAuthenticatedLDAPpervalidation and defaults, options, TLS clone, hosts copy, dial failure, auth-failure propagation. Note that the success path ofNewAuthenticatedLDAPperrequires a live connection and is covered by integration tests only. - Shipped adapters —
adapters/*_adapter_test.go: user-info mapping, OU mapping, filter templates compiled byldap.CompileFilter, args ordering, attribute lists, distinguishing-attribute guards, MSADResolvePrimaryGroupDNchild-domain regression, FreeIPABindIdentitystructural-RDN detection (case-insensitive, substring-vs-RDN regression),appendRDNattrTypevalidation,binarySidToStringboundary (subAuthorityCount == 255). Family-base delegation is pinned explicitly:SAMBAAdapteris verified to matchMSADAdapteron every method;DS389Adapter/OpenDJAdapterare verified to keep the inheritedOPENLDAPAdapterbehaviour and only extend it where documented;OUDAdapteris verified to matchOpenDJAdapter.
go test ./...
go test -race -count=20 ./...
go vet ./...go test -run=^$ -fuzz=FuzzXxx ./... drives the load-bearing invariants:
FuzzCanonicalDN_NoPanic/FuzzCanonicalDN_CaseEquivalence(helpers_fuzz_test.go) — case-folding and no-panic contracts of the tree-linking key.FuzzParentDNOf(helpers_fuzz_test.go) — valid input yields parent =RDNs[1:], malformed / single-RDN input yields"".FuzzEscapeFilterArgs,FuzzValidateDN_SentinelContract,FuzzValidateHost_SentinelContract(helpers_fuzz_test.go) — filter escaping and sentinel-error contracts.FuzzAppendRDN(adapters/builtin_adapter_helpers_test.go) — DN built byappendRDNmust parse, and its first RDN must carry the suppliedattrType/ value.FuzzAdapterFilterTemplates(adapters/filters_fuzz_test.go) — for every shipped adapter (all eight, including the family extensions) and every user-controlled input, the rendered filter must compile; template arity is checked independently, catching both under-supply (%!s(MISSING)) and over-supply (silently dropped args).
utils_test.go(packageldapper):newLDAPEntry(dn, attrs) *ldap.Entry— builds a*ldap.Entrywith bothValuesandByteValuespopulated soGetRawAttributeValueworks in tests.ldapper_connection_test.go:withDialConn(t, fn)— swaps the package-leveldialConnseam for the duration of a test and restores it viat.Cleanup.ldapper_auth_test.go:fakeAdapter— a configurableCatalogAdapterstub used by core tests; each field defaults to an empty value.newAuthTestLDAPper,markAuthenticatedhelpers.ldapper_search_test.go:newTestLDAPper,searchCookie,pagedResponse— paging helpers.ldapper_test.go:stubTLSCfg,connectStub.adapters/helpers_test.go(packageadapters):newLDAPUser(dn, attrs)— builds atypes.LDAPUserfixture with a non-nil attribute map.attrMap(map[string]string) map[string][]string— shorthand for single-valued attributes.newLDAPEntry(dn, attrs) ldap.Entry— value form, matches the signature ofGetNormalizedOUInfoFromEntry. Note that theldapperpackage has a pointer-returningnewLDAPEntryinutils_test.go— same name, different package, different form.requireValidFilter(t, filter)— fails the test ifldap.CompileFilterrejects the filter, with the raw text included in the diagnostic.
integration_helpers_test.go(packageldapper_test, no build tag) — shared helpers available to all eight tagged integration suites. Currently holdscontainsDN(case-insensitive substring match for group DNs). Keep it dependency-free: only the standard library. If a helper needs testify /ldap/v3, move it into the specific tagged file that uses it.
Tip: tests that swap
dialConnmust not callt.Parallel(). The swap is process-wide; parallel tests would stomp on each other.withDialConnprovides the pattern.
End-to-end tests spin up real directories via
testcontainers-go and require a
running Docker daemon. Each suite lives in a separate file behind its own
build tag, because Go allows only one TestMain per package — the
tags keep the suites from compiling together. For the same reason,
tags must be run one at a time: combining any two suites (e.g.
integration_opendj + integration_oud) into a single go test call
compiles two TestMain functions into package ldapper_test, which is
a duplicate-symbol error.
| File | Build tag | Container |
|---|---|---|
openldap_integration_test.go |
integration_openldap |
osixia/openldap:1.5.0 |
msad_integration_test.go |
integration_msad |
nowszy/samba-ad-dc (Samba 4 AD) |
sambadc_integration_test.go |
integration_samba |
nowszy/samba-ad-dc (Samba 4 AD, SAMBAAdapter) |
ds389_integration_test.go |
integration_389ds |
389ds/dirsrv (LDAP on :3389) |
opendj_integration_test.go |
integration_opendj |
openidentityplatform/opendj (:1389) |
oracleud_integration_test.go |
integration_oud |
openidentityplatform/opendj (OUD-compat proxy) |
freeipa_integration_test.go |
integration_freeipa |
freeipa/freeipa-server:rocky-9 |
aldpro_integration_test.go |
integration_aldpro |
osixia/openldap + ALDPro schema |
Run one suite at a time:
go test -tags=integration_openldap ./...
go test -tags=integration_msad ./...
go test -tags=integration_samba ./...
go test -tags=integration_389ds ./...
go test -tags=integration_opendj ./...
go test -tags=integration_oud ./...
go test -tags=integration_freeipa ./...
go test -tags=integration_aldpro ./...In CI (.github/workflows/integration.yml) every suite is its own job.
Light-weight suites (openldap, aldpro) run on every PR that touches
adapters or integration files; the heavier ones (msad, samba,
389ds, opendj, oud, freeipa) run only on schedule / manual
triggers, so PR feedback stays fast.
What each suite exercises:
- OpenLDAP — full end-to-end coverage of the public API against
OPENLDAPAdapter; both branches of themember/memberUidgroup filter are positively asserted, plusentryDN-fallback lookups and duplicate detection (ErrAmbiguousResult). Breaks a user's password via direct LDAP Modify and verifies thatReconnecton the stale cached password fails and leaves the client unusable (ErrClosed). - MS AD — real Samba 4 AD DC, exercising the AD-only
LDAP_MATCHING_RULE_IN_CHAINgroup expansion and thePrimaryGroupResolverpath. The primary-group test resolves the expected DN viaprimaryGroupToken(the well-known RID 513), so it is locale-independent — "Domain Users" only works in English AD deployments, and the suite avoids that assumption. Gracefully skips if the container cannot be started. - Samba AD DC — same container image as MS AD, exercised through
SAMBAAdapter. BecauseSAMBAAdapterembedsMSADAdapter, the suite is the Samba-flavoured mirror of the MS AD tests: same filters, sameLDAP_MATCHING_RULE_IN_CHAINgroup expansion, samePrimaryGroupResolverpath withprimaryGroupToken=513. This is the end-to-end proof that the embedding introduced no drift. - 389-DS —
DS389Adapteragainst389ds/dirsrv(listening on:3389). Exercises the extendedUserObjectFilter(which now includesnsAccount/nsOrgPerson) and the extransAccountLockattribute; group model (via the inheritedgroupOfNames/posixGroupunion) and short-form bind expansion are verified end-to-end. - OpenDJ —
OpenDJAdapteragainstopenidentityplatform/opendj(:1389). Same group model as OpenLDAP, plus the virtualisMemberOfattribute — the suite exercises both branches of the group filter and both short-form and full-DN binds. - OUD —
OUDAdapteragainst the same OpenDJ image, used as an OUD-compatible proxy: a real Oracle OUD image requires an Oracle licence and is not redistributable, but OUD is Oracle's fork of OpenDJ and shares the same LDAP engine, group model, user schema and virtualisMemberOfattribute. The suite therefore serves as an OUD contract test even though the running binary is OpenDJ. As noted above, it cannot be combined withintegration_opendjin the samego testinvocation (both defineTestMain). - FreeIPA — real FreeIPA container, verifying that
ipa group-add-memberpopulates DN-valuedmember/memberIndirect, and cross-checkingGetUserGroupsagainst an independentCustomSearchwith the same filter. Gracefully skips if the container cannot be started.Reconnect-rebind-failure is intentionallyt.Skip-ed here (FreeIPA password rotation is not scriptable non-interactively); the equivalent path is covered by the OpenLDAP suite. - ALDPro — stock OpenLDAP augmented with a custom schema mirroring
the ALDPro-specific objectClass
rbta-org-unitandrbtadpattribute. Therbta-org-unitclass is cn-named (SUP top MUST cn, it does not inherit fromorganizationalUnit), which is what exercises theou → cnfallback inGetNormalizedOUInfoFromEntryand thecn=arm ofSearchOUFilterTemplate. The suite also rewrites the defaultolcAccessthroughcn=configright after startup:attrs=userPassword by anonymous auth(Simple Bind compares the hash as an anonymous identity — without this every non-root bind fails withLDAP Result Code 49) andto * by users read(without this every authenticated search returnsLDAP Result Code 32 "No Such Object"even though the data is present). Exercises both branches of the dual-branch group filter, and confirms thatOUObjectFiltermatches both standardorganizationalUnitand ALDPro-specificrbta-org-unitentries. Uses a dedicatedsvc-readerinetOrgPerson for authenticated binds because the osixia admin is asimpleSecurityObjectthatUserFilterdoes not match.
All integration tests assert error conditions with errors.Is(err, ldapper.Err*) against the sentinel contract — not against error text —
so message rewording does not break them.
Guards for the PrimaryGroupResolver contract are installed per suite,
in both directions:
- Positive (the adapter must implement the interface):
msad_integration_test.go—var _ interfaces.PrimaryGroupResolver = adapters.MSADAdapter{}sambadc_integration_test.go—var _ interfaces.PrimaryGroupResolver = adapters.SAMBAAdapter{}
- Negative (the adapter must not implement it — presence of the
interface would make
GetUserGroupstry to resolve a non-existent attribute). Go has no static "does not implement" assertion, so each file does aninit()-time panic if the interface is unexpectedly satisfied:openldap_integration_test.go,freeipa_integration_test.go,aldpro_integration_test.go,ds389_integration_test.go,opendj_integration_test.go,oracleud_integration_test.go.
Distributed under the terms specified in the LICENSE file
at the repository root. The file is the authoritative source — this
section is only a pointer.
Copyright © 2020–2026 NGR Softlab. All rights reserved.
Before adding LDAPper as a dependency, check the LICENSE file for the
exact terms (permission to use, redistribute, modify, sublicense,
commercial use, warranty disclaimers, etc.). If your use case is not
covered by the file (e.g. redistribution in a closed-source product,
SaaS deployment with non-standard terms), contact NGR Softlab for a
commercial agreement.
LDAPper itself is small; the bulk of the dependency tree comes from
testcontainers-go, and only the go-ldap/ldap/v3 dependency is pulled
in at build time for production code. Test-only dependencies do not
ship in binaries that import LDAPper.
| Dependency | Role | License |
|---|---|---|
github.com/go-ldap/ldap/v3 |
runtime | MIT |
github.com/stretchr/testify |
tests only | MIT |
github.com/testcontainers/testcontainers-go + indirect |
tests only | MIT (various) |
Transitive dependencies are listed in go.mod; their licenses are
declared in their respective LICENSE files. Run
go run github.com/google/go-licenses@latest report ./... for an
up-to-date, machine-generated inventory.