* feat(graph): eliminate LDAP read-after-write on create/update Every graph LDAP create/update wrote the entry, then immediately re-read it back by DN solely to recover the entry ID for the response model. Against a replicated directory reached through a proxy, that read-back can hit a lagging replica and fail or return stale data. When oCIS generates the UUID itself (GRAPH_LDAP_SERVER_UUID disabled, the default), the read-back returns nothing new: create responses are now synthesized from the data already sent, and update responses are built by folding the applied modifications onto the entry read before the write. When the directory assigns the ID (GRAPH_LDAP_SERVER_UUID enabled), creates keep the existing read-back since the generated ID cannot otherwise be recovered. New ldap_writeback.go with two pure helpers: - attrsFromAddRequest: flatten an AddRequest for ldap.NewEntry (create paths) - applyModifyToEntry: fold a ModifyRequest (Replace/Add/Delete, case-insensitive, non-mutating, ByteValues populated for octet-string id parity) onto a copy of the pre-read entry (update paths) All 9 write sites updated: - creates (user, group, education user/class/school): synthesize when the ID is client-generated, keep the read-back when the server assigns it - updates (user, education user): fold the ModifyRequest onto the pre-read entry; the AccountEnabled trailing override and the rename read-back inside changeUserName are preserved (rename is out of scope) - education school update: updateSchoolProperties now returns its ModifyRequest so the caller can fold it; the displayName ModifyDN rename is folded as a displayName attribute replace - education class update: pre-read widened from name/id only to the full education-class attribute set so classification/externalID are not dropped from the response; the externalID ModifyDN rename is folded as an attribute replace Model builders are untouched, preserving the response shape. Existing update tests were reconciled: read-back Search mocks that are no longer issued were removed and the pre-read fixtures now carry the full baseline entry, with all assertions unchanged. OCISDEV-1033 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix: hardcoded groupAttributeMap.id and wrong getLDAPGroup filter * fix(graph): reject octet-string IDs without server-assigned UUIDs Octet-string ID attributes (e.g. Active Directory's objectGUID) are assigned by the directory server and require GRAPH_LDAP_SERVER_UUID=true. With GRAPH_LDAP_SERVER_UUID=false oCIS generates the ID and writes it as a string UUID, so an octet-string ID attribute would be decoded from raw string bytes and produce a corrupt ID on both the read-back and the synthesize path. NewLDAPBackend now rejects this incompatible combination at startup instead of silently returning bad IDs. Also adds regression tests for the id-attribute and group-name lookup fixes: - CreateGroup / CreateEducationClass synthesize the response id under the configured GroupIDAttribute (not a hardcoded attribute name) - getLDAPGroupByNameOrID filters by the group name attribute, not the user one Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix: update the staled DN * test(graph): assert no read-back by search scope, not hard-coded DN The synthesize-on-create test matched the read-back on a hard-coded createdDN whose correctness relied on the school base DN being empty. Match on ScopeBaseObject instead: DN-independent and precise about the read-back being eliminated. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix: fixed calling it with a nil argumets. prevent the slice mutations * chore: tests --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
package identity
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
)
|
|
|
|
// attrsFromAddRequest flattens the attributes of an *ldap.AddRequest into the
|
|
// map[string][]string shape that ldap.NewEntry expects. It is used to synthesize
|
|
// the response entry from data already in hand, avoiding a read-after-write when
|
|
// oCIS generated the entry's ID itself (useServerUUID=false).
|
|
func attrsFromAddRequest(ar *ldap.AddRequest) map[string][]string {
|
|
attrs := make(map[string][]string, len(ar.Attributes))
|
|
for _, a := range ar.Attributes {
|
|
vals := make([]string, len(a.Vals))
|
|
copy(vals, a.Vals)
|
|
attrs[a.Type] = vals
|
|
}
|
|
return attrs
|
|
}
|
|
|
|
// applyModifyToEntry returns a copy of base with the changes from mr folded on
|
|
// (Replace / Add / Delete). It never mutates base. Attribute names are matched
|
|
// case-insensitively, consistent with ldap.Entry's GetEqualFold* accessors, so
|
|
// folding never produces a duplicate attribute entry that differs only in case.
|
|
//
|
|
// It is used to synthesize the response entry after an update from the pre-read
|
|
// entry plus the ModifyRequest being persisted, avoiding a read-after-write.
|
|
func applyModifyToEntry(base *ldap.Entry, mr *ldap.ModifyRequest) *ldap.Entry {
|
|
if base == nil {
|
|
return nil
|
|
}
|
|
if mr == nil {
|
|
mr = &ldap.ModifyRequest{DN: base.DN}
|
|
}
|
|
// Deep-copy base into a name->values map so we never touch the original.
|
|
attrs := make(map[string][]string, len(base.Attributes))
|
|
// order preserves the original attribute order, with new attributes appended.
|
|
order := make([]string, 0, len(base.Attributes))
|
|
index := make(map[string]string, len(base.Attributes)) // fold-key -> stored name
|
|
for _, a := range base.Attributes {
|
|
vals := make([]string, len(a.Values))
|
|
copy(vals, a.Values)
|
|
attrs[a.Name] = vals
|
|
order = append(order, a.Name)
|
|
index[strings.ToLower(a.Name)] = a.Name
|
|
}
|
|
|
|
// name resolves the stored attribute name for attrType case-insensitively,
|
|
// registering a new attribute (preserving the request's casing) if none matches.
|
|
name := func(attrType string) string {
|
|
if n, ok := index[strings.ToLower(attrType)]; ok {
|
|
return n
|
|
}
|
|
index[strings.ToLower(attrType)] = attrType
|
|
order = append(order, attrType)
|
|
return attrType
|
|
}
|
|
|
|
for _, change := range mr.Changes {
|
|
attrType := change.Modification.Type
|
|
vals := change.Modification.Vals
|
|
switch change.Operation {
|
|
case ldap.ReplaceAttribute:
|
|
n := name(attrType)
|
|
cp := make([]string, len(vals))
|
|
copy(cp, vals)
|
|
attrs[n] = cp
|
|
case ldap.AddAttribute:
|
|
n := name(attrType)
|
|
attrs[n] = append(attrs[n], vals...)
|
|
case ldap.DeleteAttribute:
|
|
if n, ok := index[strings.ToLower(attrType)]; ok {
|
|
if len(vals) == 0 {
|
|
// whole-attribute delete
|
|
delete(attrs, n)
|
|
} else {
|
|
kept := removeValues(attrs[n], vals)
|
|
if len(kept) == 0 {
|
|
// the last value was removed; a real server drops the attribute
|
|
delete(attrs, n)
|
|
} else {
|
|
attrs[n] = kept
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
dn := base.DN
|
|
if mr.DN != "" {
|
|
dn = mr.DN
|
|
}
|
|
result := ldap.NewEntry(dn, nil)
|
|
result.Attributes = make([]*ldap.EntryAttribute, 0, len(order))
|
|
for _, n := range order {
|
|
v, ok := attrs[n]
|
|
if !ok {
|
|
// deleted attribute
|
|
continue
|
|
}
|
|
result.Attributes = append(result.Attributes, ldap.NewEntryAttribute(n, v))
|
|
}
|
|
return result
|
|
}
|
|
|
|
// removeValues returns have with every element of remove stripped out.
|
|
func removeValues(have, remove []string) []string {
|
|
if len(have) == 0 {
|
|
return have
|
|
}
|
|
drop := make(map[string]struct{}, len(remove))
|
|
for _, r := range remove {
|
|
drop[r] = struct{}{}
|
|
}
|
|
kept := make([]string, 0, len(have))
|
|
for _, v := range have {
|
|
if _, ok := drop[v]; ok {
|
|
continue
|
|
}
|
|
kept = append(kept, v)
|
|
}
|
|
return kept
|
|
}
|