Skip to content

feat: [OCISDEV-1032] ldap config and retry policy - #12672

Merged
mklos-kw merged 3 commits into
stable-8.0from
feat/ocisdev-1032
Jul 30, 2026
Merged

mklos-kw merged 3 commits into
stable-8.0from
feat/ocisdev-1032

Conversation

@mklos-kw

@mklos-kw mklos-kw commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

Description

Related to PRs
owncloud/reva#678
owncloud/reva#673

Related Issue

  • Fixes <issue_link>

Motivation and Context

How Has This Been Tested?

  • test environment:
  • test case 1:
  • test case 2:
  • ...

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation ticket raised:

@update-docs

update-docs Bot commented Jul 29, 2026

Copy link
Copy Markdown

Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a changelog item based on your changes.

@kw-security

kw-security commented Jul 29, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@mklos-kw
mklos-kw force-pushed the feat/ocisdev-1032 branch 2 times, most recently from bae8f85 to 36f1371 Compare July 30, 2026 09:40
# Conflicts:
#	go.mod
#	go.sum
#	services/graph/pkg/service/v0/service.go
#	vendor/github.com/owncloud/reva/v2/pkg/utils/ldap.go
#	vendor/github.com/owncloud/reva/v2/pkg/utils/ldap/config.go
#	vendor/github.com/owncloud/reva/v2/pkg/utils/ldap/pool.go
#	vendor/github.com/owncloud/reva/v2/pkg/utils/ldap/reconnect.go
#	vendor/modules.txt
@mklos-kw
mklos-kw force-pushed the feat/ocisdev-1032 branch from 36f1371 to 54d8b76 Compare July 30, 2026 09:55
@mklos-kw
mklos-kw marked this pull request as ready for review July 30, 2026 09:55
@2403905

2403905 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Code review

Found 3 issues:

  1. NewWritePolicy's retryable set is unreachable in practice, so writes now never retry — a regression versus the retry() it replaces. It matches only LDAPResultServerDown (81) and LDAPResultConnectError (91), but the go-ldap client never constructs those codes: grep -rn "LDAPResultServerDown\|LDAPResultConnectError" vendor/github.com/go-ldap/ldap/v3/ returns only the error.go definitions, and every connection failure it raises is ErrorNetwork (200) — including DialURL failures and sendMessageWithFlags's "ldap: connection closed", which returns before the request packet is transmitted and is therefore safe to retry. Since ConnWithReconnect exists precisely because the long-lived connection goes stale (idle timeout, LB/firewall reaping — as the ConnPool.do comment describes), a write on a reaped connection previously reconnected and succeeded and now fails outright. This also contradicts OCISDEV-1032's acceptance criteria, which list ErrorNetwork (200) among the write-retryable codes. Consider distinguishing pre-transmit ErrorNetwork from mid-operation ErrorNetwork rather than dropping 200 entirely.

MaxDelay: maxDelay,
isRetryable: func(code uint16) bool {
switch code {
case ldap.LDAPResultServerDown,
ldap.LDAPResultConnectError:
return true
}
return false
},

  1. Unrelated change: UpdateUserLastSignInDate's introductionVersion is changed from 8.0.6 to 8.2.0 while the line is being re-aligned. On stable-8.0 this shipped in 8.0.6 (changelog/8.0.6_2026-07-15/), and the backport in [backport stable-8.0] feat(graph): allow disabling the last sign-in timestamp update #12532 deliberately adjusted it from 8.2.0 (master) to 8.0.6 for this release line — this silently reverts that. 8.2.0 does not exist on this branch, and the PR's own new fields correctly use 8.0.7. These tags generate the user-facing env-var docs.

WriteEnabled bool `yaml:"write_enabled" env:"OCIS_LDAP_SERVER_WRITE_ENABLED;GRAPH_LDAP_SERVER_WRITE_ENABLED" desc:"Allow creating, modifying and deleting LDAP users via the GRAPH API. This can only be set to 'true' when keeping default settings for the LDAP user and group attribute types (the 'OCIS_LDAP_USER_SCHEMA_* and 'OCIS_LDAP_GROUP_SCHEMA_* variables)." introductionVersion:"pre5.0"`
UpdateUserLastSignInDate bool `yaml:"update_user_last_sign_in_date" env:"OCIS_LDAP_UPDATE_LAST_SIGNIN_DATE;GRAPH_LDAP_UPDATE_LAST_SIGNIN_DATE" desc:"Update the 'oCLastSignInTimestamp' LDAP attribute of a user when the user signs in. The attribute is only persisted when 'OCIS_LDAP_SERVER_WRITE_ENABLED' is also enabled. Set this to 'false' to avoid an LDAP write on every sign-in, e.g. when the last sign-in timestamp is not needed or the LDAP write load should be reduced." introductionVersion:"8.2.0"`
RetryMaxCount int `yaml:"retry_max_count" env:"OCIS_LDAP_RETRY_MAX_COUNT;GRAPH_LDAP_RETRY_MAX_COUNT" desc:"Maximum number of retries for failed LDAP operations. Applies to both read and write operations (with different retryable error sets). Default 1." introductionVersion:"8.0.7"`

  1. The three new options are silently ignored when OCIS_LDAP_POOL_ENABLED=true. GetLDAPClientWithPool does not forward RetryMaxCount/RetryBaseDelay/RetryMaxDelay to NewLDAPPool, and ConnPool.do still loops on the hardcoded defaultRetries with no backoff at all (ConnPool dials via dialLDAP rather than wrapping ConnWithReconnect, so it cannot inherit the policy). An operator setting RETRY_MAX_COUNT=5 and RETRY_BASE_DELAY=200ms in pool mode gets one retry and zero delay, while the shipped desc text claims unconditionally that it "Applies to both read and write operations". Either wire the policy into ConnPool or document the pool-mode caveat.

func (p *ConnPool) do(retryOnNetworkErr bool, fn func(conn ldap.Client) error) error {
var opErr error
for try := 0; try <= defaultRetries; try++ {
conn, err := p.checkout()

Verified as non-issues: the vendored reva matches upstream commit 0c6a26f48bb4 (owncloud/reva#673) with no hand-editing; readBackAfterWrite is additive to the useServerUUID branch that OCISDEV-1033 deliberately kept, not a revert of it; retry counts are consistent (maxRetries+1 total attempts) in both retry loops; BaseDelay=0 correctly skips sleeping; and jitter is applied via backoff/v5's default RandomizationFactor.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

@2403905 2403905 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claim

NewWritePolicy's retryable set can never match a real error, so no write is ever retried.
This is a behavioural regression against the retry() function the PR replaces, and it
contradicts the ticket's own acceptance criteria.

Where

vendor/github.com/owncloud/reva/v2/pkg/utils/ldap/reconnect.go:104-112
(upstream: pkg/utils/ldap/reconnect.go in owncloud/reva)

isRetryable: func(code uint16) bool {
    switch code {
    case ldap.LDAPResultServerDown,   // 81
        ldap.LDAPResultConnectError:  // 91
        return true
    }
    return false
},

ErrorNetwork (200) is deliberately excluded, with this rationale in the doc comment:

ErrorNetwork is deliberately NOT retryable for writes: go-ldap's Add/Modify/Del send the
request packet (doRequest) and then read the response (readPacket), so a connection drop
during the response read surfaces as ErrorNetwork AFTER the mutation was already
transmitted. Retrying such a write would double-apply it.

Why it's wrong

  1. Codes 81 and 91 are never produced by the go-ldap client. They are declared in
    error.go and never passed to NewError() anywhere in the library. Every connection
    failure go-ldap raises is ErrorNetwork (200) — 27 construction sites.

  2. So the write policy matches nothing. isRetryable returns false for every error a
    write can actually observe. The write branch of RetryOp is unreachable code.

  3. This is a regression, not a no-op. The removed retry() gated on
    ldap.IsErrorWithCode(err, ldap.ErrorNetwork) and applied to all operations, including
    Add/Del/Modify/ModifyDN. ConnWithReconnect exists precisely because the single
    long-lived connection goes stale (idle timeout, LB/firewall reaping) — the comment on
    ConnPool.do in pool.go says exactly that. Before this PR a write on a reaped
    connection reconnected and succeeded; now it fails to the caller.

  4. The double-apply rationale is real but over-broad. Not all ErrorNetwork is
    post-transmit. sendMessageWithFlags (conn.go:462, returning at conn.go:464 and
    conn.go:497) returns NewError(ErrorNetwork, "ldap: connection closed") before the
    request packet is ever handed to the write loop, and DialURL (conn.go:266,
    conn.go:279) wraps dial/TLS failures the same way. Those provably never reached the
    server and are safe to retry. (Line numbers are for go-ldap v3.4.14, the version
    vendored on stable-8.0.)

  5. It contradicts the acceptance criteria. OCISDEV-1032 states for writes:

    Retry only on connection-establishment failures where the request provably never
    reached the server: ErrorNetwork (200), LDAPResultServerDown (81),
    LDAPResultConnectError (91).

    The implementation drops the only one of those three that actually occurs.

Failure scenario

  1. oCIS runs with the default ConnWithReconnect path (OCIS_LDAP_POOL_ENABLED=false).
  2. The LDAP connection sits idle long enough to be reaped by an idle timeout, load
    balancer, or firewall.
  3. A user-management write arrives — e.g. PATCH /graph/v1.0/users/{id}, which reaches
    conn.Modify.
  4. go-ldap fails the operation with ErrorNetwork (200).
  5. NewWritePolicy.isRetryable(200) → false. RetryOp breaks immediately, without
    reconnecting.
  6. The write fails and the error surfaces to the API caller. Pre-PR, step 5 reconnected and
    the retry succeeded.

Note the read path is unaffected — NewReadPolicy includes ErrorNetwork, so Search
still recovers from a stale connection. The failure is write-only, which makes it easy to
miss in smoke tests that mostly read.

Suggested fix

Distinguish pre-transmit from mid-operation ErrorNetwork rather than excluding 200
wholesale. Options, roughly in order of preference:

  • Retry writes when the connection was known-unusable before the request was sent. The
    cheap version: check conn.IsClosing() / attempt a fresh connection and only retry the
    write if the previous attempt never got as far as sending. go-ldap distinguishes these
    cases by error message but not by code, so this needs care.
  • Treat the first write attempt on a checked-out-but-idle connection as retryable by
    proactively validating the connection before use (an ldap.Compare or abandoned no-op),
    keeping ErrorNetwork non-retryable after a real send.
  • At minimum, drop 81/91 from the write set (they are dead) and document plainly that
    writes are never retried, so the config's "Applies to both read and write operations"
    description stops over-promising. This keeps the safety property but makes the regression
    explicit and reviewable.

Also worth adding a test that asserts the write policy matches at least one code the
client can actually emit — the existing "write does not retry ErrorNetwork" test locks
in the current behaviour without checking that anything remains retryable.

Reproduce

./confirm-issue-1.sh

See confirm-issue-1.sh in this directory. It performs two independent checks:

  • Static: enumerates every NewError(<Code> site in the vendored go-ldap and shows
    that 81/91 appear only as declarations in error.go, never as constructed errors.
  • Dynamic: builds a small program against the real github.com/go-ldap/ldap/v3 that
    (a) replays the PR's exact isRetryable predicates over both policies, and (b) forces a
    genuine failure by writing to a closed connection and to an unreachable port, printing
    the actual result code and whether the write policy would retry it.

The script exits 0 only when both checks confirm the issue. No LDAP server is required — it
builds a real TCP pair and closes the client side to model an idle-reaped connection.

Recorded output (go-ldap v3.4.14, run 2026-07-30)

[static] Every result code the client passes to NewError(), by frequency:
  27 ErrorNetwork
  14 ErrorFilterCompile
   3 ErrorEmptyPassword
   2 ErrorDebugging
   1 resultCode
   1 ErrorUnexpectedResponse
   1 ErrorFilterDecompile

Codes: ErrorNetwork=200  ServerDown=81  ConnectError=91  Timeout=85  LocalError=82  Other=80

=== A. Write on a CLOSED connection (models an idle-reaped conn) ===
  conn.Modify (write)   err=LDAP Result Code 200 "Network Error": ldap: connection closed
                        code=200 (Network Error)
                        write-retry(new)=false  read-retry(new)=true   retry(old, pre-PR)=true
  conn.Add (write)      ... write-retry(new)=false  read-retry(new)=true  retry(old, pre-PR)=true
  conn.Del (write)      ... write-retry(new)=false  read-retry(new)=true  retry(old, pre-PR)=true

=== B. Dial an unreachable port (connection-establishment failure) ===
  ldap.DialURL          err=LDAP Result Code 200 "Network Error": dial tcp 127.0.0.1:1: connect: connection refused
                        code=200 (Network Error)
                        write-retry(new)=false  read-retry(new)=true   retry(old, pre-PR)=true

CONFIRMED: NewWritePolicy.isRetryable is unreachable for client-observed errors
=> writes are never retried. Pre-PR retry() returned true for the same errors => regression.

The retry(old, pre-PR)=true column is the regression: every one of these errors was
retried before this PR and is not retried after it. The read-retry(new)=true column shows
why the failure is write-only and easy to miss in read-heavy smoke tests.

2403905
2403905 previously approved these changes Jul 30, 2026
deyankiteworks
deyankiteworks previously approved these changes Jul 30, 2026
@2403905
2403905 dismissed stale reviews from deyankiteworks and themself via 1f2ab2a July 30, 2026 16:02
@mzner
mzner self-requested a review July 30, 2026 16:25
@mklos-kw
mklos-kw merged commit cdb3ba2 into stable-8.0 Jul 30, 2026
109 of 111 checks passed
@mklos-kw
mklos-kw deleted the feat/ocisdev-1032 branch July 30, 2026 16:49
mklos-kw added a commit that referenced this pull request Aug 4, 2026
* feat: ldap config and retry policy

Co-authored-by: Roman Perekhod <[email protected]>

^ Conflicts:
^	go.mod
^	go.sum
^	services/graph/pkg/identity/ldap.go
^	services/graph/pkg/identity/ldap_education_class.go
^	services/graph/pkg/identity/ldap_education_class_test.go
^	services/graph/pkg/identity/ldap_education_school.go
^	services/graph/pkg/identity/ldap_education_school_test.go
^	services/graph/pkg/identity/ldap_education_user.go
^	services/graph/pkg/identity/ldap_education_user_test.go
^	services/graph/pkg/identity/ldap_group.go
^	services/graph/pkg/identity/ldap_writeback.go
^	services/graph/pkg/identity/ldap_writeback_test.go
^	services/graph/pkg/service/v0/service.go
^	vendor/github.com/owncloud/reva/v2/pkg/utils/ldap/config.go
^	vendor/modules.txt
2403905 pushed a commit that referenced this pull request Aug 4, 2026
#12710)

feat: [OCISDEV-1032] ldap config and retry policy (#12672)

* feat: ldap config and retry policy

Co-authored-by: Roman Perekhod <[email protected]>

^ Conflicts:
^	go.mod
^	go.sum
^	services/graph/pkg/identity/ldap.go
^	services/graph/pkg/identity/ldap_education_class.go
^	services/graph/pkg/identity/ldap_education_class_test.go
^	services/graph/pkg/identity/ldap_education_school.go
^	services/graph/pkg/identity/ldap_education_school_test.go
^	services/graph/pkg/identity/ldap_education_user.go
^	services/graph/pkg/identity/ldap_education_user_test.go
^	services/graph/pkg/identity/ldap_group.go
^	services/graph/pkg/identity/ldap_writeback.go
^	services/graph/pkg/identity/ldap_writeback_test.go
^	services/graph/pkg/service/v0/service.go
^	vendor/github.com/owncloud/reva/v2/pkg/utils/ldap/config.go
^	vendor/modules.txt
mklos-kw added a commit that referenced this pull request Aug 5, 2026
* feat: ldap config and retry policy

Co-authored-by: Roman Perekhod <[email protected]>
# Conflicts:
#	go.mod
#	go.sum
#	services/graph/pkg/config/config.go
#	services/graph/pkg/service/v0/service.go
#	vendor/modules.txt

# Conflicts:
#	services/graph/pkg/service/v0/service.go
mklos-kw added a commit that referenced this pull request Aug 5, 2026
* feat: ldap config and retry policy

Co-authored-by: Roman Perekhod <[email protected]>
# Conflicts:
#	go.mod
#	go.sum
#	services/graph/pkg/config/config.go
#	services/graph/pkg/service/v0/service.go
#	vendor/modules.txt

# Conflicts:
#	services/graph/pkg/service/v0/service.go
mklos-kw added a commit that referenced this pull request Aug 6, 2026
* feat: [OCISDEV-1032] ldap config and retry policy (#12672)

* feat: ldap config and retry policy

Co-authored-by: Roman Perekhod <[email protected]>
# Conflicts:
#	go.mod
#	go.sum
#	services/graph/pkg/config/config.go
#	services/graph/pkg/service/v0/service.go
#	vendor/modules.txt

# Conflicts:
#	services/graph/pkg/service/v0/service.go

* test: e2e ldap

# Conflicts:
#	.github/workflows/acceptance-tests.yml

# Conflicts:
#	.github/workflows/acceptance-tests.yml

* test: e2e ldap distributed

# Conflicts:
#	.github/workflows/acceptance-tests.yml

* fix: post rebases build error
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.

5 participants