Skip to content

feat(ldap): trust the AD root CA (ca_cert); TLS by URL scheme; TLS settings in the console - #79

Open
alghanim wants to merge 6 commits into
bodaay:masterfrom
alghanim:feat/ldap-tls-console
Open

alghanim wants to merge 6 commits into
bodaay:masterfrom
alghanim:feat/ldap-tls-console

Conversation

@alghanim

Copy link
Copy Markdown
Collaborator

Builds on #75 and must be merged after it. This branch starts from #75's head (8b073a6), so until #75 is merged
the diff here also shows its commit.

On v2.3.0, an AD reached over ldap:// fails every StartTLS upgrade with either ServerName or InsecureSkipVerify must be specified in the tls.Config; #75 fixes that. The next wall is an AD whose domain controllers carry
certificates from the organisation's own CA: certificate signed by unknown authority.

The only way out was skip_tls_verify, and it barely worked:

  • the console doesn't offer it;
  • saving from the console reset it;
  • Import and Connect overwrote it.

What

  • ca_cert on the LDAP config (PEM, one or more CA certificates).
    • It is added to the system pool for both StartTLS and ldaps://, so verification stays on.
    • PUT, import and auto-discover refuse invalid text with 400 invalid ca_cert: …: non-PEM text, a pasted private
      key, a damaged or truncated block, a certificate that doesn't parse.
    • A stored value that doesn't parse is an error on connect, never a silent fallback to the system pool.
    • The audit log records only whether it is set.
  • TLS follows the URL scheme. ldaps:// always dials with the TLS config; ldap:// always upgrades with
    StartTLS before any bind, unless allow_insecure is set. use_tls is set from the scheme on save and is no longer
    used to connect. This fixes two bugs:
    • ldaps:// saved from the console (no use_tls) ignored skip_tls_verify (and now ca_cert).
    • ldap:// with "use_tls": true skipped StartTLS and bound in cleartext (row S12 below). go-ldap ignores the
      TLS config for ldap://, and LDAPConnect returned early on UseTLS.
  • Import and auto-discover accept skip_tls_verify, allow_insecure and ca_cert. A field left out keeps its
    stored value instead of being reset ("ca_cert": "" clears it). Auto-discover connects with the resolved
    settings. PUT stays a full replace.
  • Console: a "TLS & certificates" section in Connect, Manual Setup, Import Config and Edit.
    • The fields: an AD root CA textarea, "Skip certificate verification" (a warning when ticked), and "Allow cleartext
      (no StartTLS)" (a stronger warning).
    • Every save sends all three. The Connection card shows the encryption and whether certificates are checked.
    • A failed Connect stays on screen instead of in a 3-second toast.
    • .alert-error / .alert-success get their dark colours in the Auto theme on a dark OS.
  • Error hint: an untrusted-CA handshake error now ends with
    ; … set ca_cert to the AD root CA certificate (PEM).
  • Docs: docs/ACTIVE-DIRECTORY.md ("TLS and Certificates"), docs/API.md, and CHANGELOG.md ("Unreleased",
    with upgrade notes; rename the heading at release).

Verification

  • go vet ./... and gofmt -l are clean; go test ./... and go test -race (auth, handler) pass on Go 1.26.8.
  • New tests run against the in-process LDAP server (internal/ldaptest, which now answers bind and RootDSE
    search after TLS) with a certificate from a separate test CA:
    • TestLDAPConnectCACert: StartTLS and ldaps; fails closed without the CA; works with it, with a bundle; fails
      with the wrong CA.
    • TestLDAPConnectCACertStillChecksHost, TestLDAPConnectSkipVerifyUntrustedCA, TestLDAPConnectInvalidCACert,
      TestParseLDAPCACert, TestIsLDAPS.
    • Handler tests: invalid ca_cert refused, use_tls follows the scheme, /test trusts ca_cert,
      ldaps-without-use_tls uses the TLS settings, stored use_tls on ldap:// still uses StartTLS, import and
      auto-discover take and keep the settings.
    • On 8b073a6, all 8 new handler tests and the ldaps-with-skip test fail. The ca_cert auth tests don't compile
      there (the field doesn't exist yet).
  • End to end. A throwaway Samba AD DC (dc1.satls.test) whose LDAP certificate comes from its own OpenSSL CA,
    with no SSL_CERT_FILE anywhere. Stock 2.3.0 image vs fix(ldap): verify StartTLS certificates; keep admin binds encrypted #75 vs this branch:
# Scenario v2.3.0 #75 This branch
S1 ldap:// + ca_cert, /test ServerName error unknown authority ok; AD user login: token issued
S2 ldap://, no ca_cert ServerName error unknown authority unknown authority + "set ca_cert" hint
S3 ldap:// + skip ok ok ok
S4 ldaps:// + ca_cert, no use_tls (as the console saves it) unknown authority unknown authority ok; login ok
S5 ldaps://, no ca_cert unknown authority unknown authority unknown authority + hint
S6 ldaps:// + skip, no use_tls fails (skip ignored) fails ok
S7 wrong CA pasted ca_cert ignored ca_cert ignored unknown authority
S8 ca_cert = garbage accepted (ignored) accepted 400 invalid ca_cert
S9 Connect dc1.satls.test + ca_cert 401 Strong Auth Required (cleartext bind) 502 unknown authority 200; CA saved; /test ok
S10 Connect again, no ca_cert 401 502 200; stored CA kept
S11 Import setup-script JSON CA wiped CA wiped CA kept; /test ok
S12 ldap:// + use_tls:true + ca_cert cleartext bind (Strong Auth Required) cleartext bind StartTLS, ok; stored use_tls=false

The console was walked through in light, dark and auto-dark against the same DC:

  • Connect fails, then succeeds after pasting the CA;
  • Edit with skip, saving the attributes card, Import, manual ldaps:// with the CA, and the cleartext warning.

Behaviour changes

  • ldap:// with "use_tls": true now uses StartTLS instead of a cleartext bind. A directory without an LDAP
    certificate now fails with the StartTLS error, exactly like "use_tls": false (set allow_insecure for a
    cleartext directory).
  • ldaps:// now honours skip_tls_verify and ca_cert.
  • PUT refuses an invalid ca_cert (400).

Not in this PR

The govulncheck job fails on master already (since at least 2026-10-26). Go 1.26.8 has reachable stdlib
vulnerabilities fixed in 1.26.9 / 1.26.10, including GO-2026-6809 in crypto/x509, and golang.org/x/net v0.58.0
needs v0.60.0. That is a separate toolchain bump.

🤖 Generated with Claude Code

https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP

alghanim and others added 6 commits October 10, 2026 11:50
StartTLS wrapped the connection with a tls.Config that had no ServerName.
tls.Client does not derive it from the address (tls.Dial does, which is why
ldaps:// worked), so with certificate verification on every upgrade failed
with "either ServerName or InsecureSkipVerify must be specified" and an
ldap:// directory could only be used with allow_insecure or skip_tls_verify.
LDAPConnect now sets ServerName from the URL host for both StartTLS and
ldaps://.

The admin console's Connect (auto-discover) and the Kerberos setup, cleanup
and import paths dialled with ldaplib.DialURL and bound directly. On ldap://
that sent the service-account (or AD admin) password in cleartext, ignoring
the StartTLS policy, and auto-discover reported success for a config that then
refused every login. They now connect through auth.LDAPConnect.

Tests use a small in-process LDAP server (internal/ldaptest) that answers
StartTLS and records every operation sent before TLS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fake directory only completed the TLS handshake and then discarded
everything, so tests could not exercise anything that binds (the /test
endpoint, auto-discover). Once a connection is secured it now answers a bind
(any credentials succeed), a RootDSE search (defaultNamingContext =
NamingContext) and any other search (no entries).

Also adds StartPlainServer, a cleartext directory that refuses StartTLS (for
allow_insecure paths), and CA.PEM, the CA certificate as an operator would
paste it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
An AD whose domain controllers have certificates from an internal root CA
could only be used with skip_tls_verify (unverified) or by adding the CA to
the host's trust store; the console offered neither.

ca_cert: new LDAP config field (JSON "ca_cert") holding the PEM of one or
more CA certificates. LDAPConnect adds them to the system pool and uses that
pool for both StartTLS and ldaps://, so the certificate is verified against
the AD root CA. PUT /api/admin/ldap refuses (400) a ca_cert that is not PEM,
holds a non-certificate block (e.g. a private key), has a damaged/truncated
block or a certificate that does not parse. A stored ca_cert that cannot be
parsed makes LDAPConnect fail before dialling; it never falls back to the
system pool. Only "set / not set" goes to the audit log.

TLS by scheme: the connection is now decided by the URL scheme alone, not
by use_tls:
- ldaps:// with use_tls=false (what the console's manual form saves) was
  dialled with go-ldap's default TLS settings, ignoring skip_tls_verify
  (and now ca_cert); it now always uses the TLS config.
- ldap:// with use_tls=true skipped StartTLS entirely (go-ldap ignores a
  TLS config for ldap://) and bound in cleartext; it now uses StartTLS.
use_tls stays in the JSON and is set from the scheme on save.

A handshake that fails on an untrusted CA now says to set ca_cert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
POST /api/admin/ldap/import and /api/admin/ldap/auto-discover built a fresh
config without skip_tls_verify / allow_insecure (and now ca_cert): re-running
the console's Connect or importing the setup script's JSON silently reset
them, and Connect could not reach an AD with an internal CA at all.

Both now accept skip_tls_verify, allow_insecure and ca_cert. A field the
request omits keeps its stored value (off when nothing is stored); a field it
sends replaces it, including switching it off or clearing ca_cert with "".
Auto-discover already connects with the resolved settings, so Connect
verifies against the pasted CA. An invalid ca_cert is refused with 400 before
anything is saved or dialled.

GetLDAPConfig now returns store.ErrLDAPNotConfigured (same message) so a real
store error is reported (500) instead of being taken for "nothing stored".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
The console offered no way to trust an internal AD root CA, skip
verification or allow cleartext, and the manual form never sent them, so
saving silently reset any value set through the API.

A shared "TLS & certificates" section is now in every LDAP form: the
Connect card, Manual Setup, Import Config, and Edit on the Connection card.
It has
- "AD root CA certificate (PEM)": paste the root CA that issued the domain
  controllers' certificates; verification stays on (recommended);
- "Skip certificate verification", with a warning that anyone between
  SimpleAuth and AD could impersonate AD and capture passwords (testing only);
- "Allow cleartext (no StartTLS)", with a stronger warning (no effect on
  ldaps://).

Every save sends all three fields (PUT is a full replace), so editing the
connection or the attributes keeps them. Import Config is prefilled with the
current settings and takes them from the uploaded JSON when it has them.
The Connection card shows the encryption (StartTLS / TLS / none) and whether
certificates are checked. A failed Connect stays on screen instead of only in
a 3-second toast, and a certificate error opens the TLS section.

Also: .alert-success/.alert-error now get their dark colours in the Auto theme
on a dark OS, not only with the explicit Dark theme.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
ACTIVE-DIRECTORY.md: new "TLS and Certificates" section (how ldaps:// and
StartTLS are chosen, what the certificate must satisfy, how to export the AD
root CA and paste it as ca_cert, why skip_tls_verify and allow_insecure are
not the fix); the console's TLS & certificates switches; corrected API and
auto-discover examples (PUT, real fields and body); connection reference
table and certificate troubleshooting.

API.md: ca_cert, skip_tls_verify, allow_insecure and the derived use_tls on
PUT (full replace, 400 on an invalid ca_cert); auto-discover and import keep
stored TLS settings when the body omits them; audit fields.

CHANGELOG.md: Unreleased section with upgrade notes (ldap:// + use_tls=true
no longer binds in cleartext; console saves now send the TLS fields).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant