From a88d64c62d6fd997aa5fc416797f26e9de8668a3 Mon Sep 17 00:00:00 2001 From: Chad Sikorra Date: Tue, 17 Nov 2026 21:44:30 -0500 Subject: [PATCH 1/2] Announce an entry leaving the sync content by the last DN the sync identity could see. --- docs/Server/Replication.md | 9 +- .../Ldap/Sync/Provider/ChangeWindow.php | 106 ++++++ .../Sync/Provider/SyncPersistStreamer.php | 45 ++- .../Sync/Provider/SyncResultProjector.php | 100 ++++-- tests/integration/Sync/SyncReplNativeTest.php | 314 +++++++++++++++++- .../Sync/SyncReplReplicaTestCase.php | 43 +++ tests/support/LdapReplicaCommand.php | 6 + tests/support/LdapServerCommand.php | 38 ++- .../ServerSyncHandlerTest.php | 8 +- .../Sync/Provider/SyncPersistStreamerTest.php | 239 ++++++++++++- .../Sync/Provider/SyncResultProjectorTest.php | 176 ++++++++-- 11 files changed, 983 insertions(+), 101 deletions(-) create mode 100644 src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php diff --git a/docs/Server/Replication.md b/docs/Server/Replication.md index 8523b868..db854ecd 100644 --- a/docs/Server/Replication.md +++ b/docs/Server/Replication.md @@ -107,6 +107,12 @@ A sync consumer receives full entries, including attributes that are hidden from so grant it only to identities you trust with the whole directory. Attribute read rules like the secure default's `userPassword` restriction still apply to normal searches by that identity; they do not trim what replication sends. +Entries the sync identity cannot search are not sent. An entry that becomes hidden from it, or leaves the synced +content, is sent as a delete named by the last DN where the identity could see it, and not at all if it never could. +This follows changes to the entry itself. Changing the access rules, or the groups they name, announces nothing. After +a change that hides or reveals entries for a restricted sync identity, remove the replica's checkpoint to have it +reload. A sync identity with full access is not affected. + ## Poll vs Listen The consumer chooses how it reads; the provider supports both: @@ -252,7 +258,8 @@ A replica requires PDO storage (SQLite or MySQL) on either runner. Any other sto Access control is enforced independently on each side, and FreeDSx ACL is server configuration that does not replicate: -* On the provider, the read access granted to the replica's bind identity bounds what data reaches the replica. +* On the provider, the read access granted to the replica's bind identity bounds what data reaches the replica. An + entry moved where that identity cannot search is removed from the replica. * On the replica, its own access control bounds what clients reading from it may see. A replica with a more permissive policy than the provider can expose data the provider would not. diff --git a/src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php b/src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php new file mode 100644 index 00000000..9952e039 --- /dev/null +++ b/src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php @@ -0,0 +1,106 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FreeDSx\Ldap\Sync\Provider; + +use Closure; +use FreeDSx\Ldap\Entry\Dn; +use FreeDSx\Ldap\Server\Backend\Storage\Journal\Change\ChangeRecord; +use FreeDSx\Ldap\Server\Backend\Storage\Journal\Change\ChangeType; +use FreeDSx\Ldap\Server\Backend\Storage\Journal\Read\ChangeScope; + +use function array_key_exists; + +/** + * What a window of journal changes says about each entry: its latest change, and the last DN the consumer could see. + * + * @author Chad Sikorra + */ +final class ChangeWindow +{ + /** + * @var array + */ + private array $latestSeqs = []; + + /** + * @var array the last in-scope DN the sync identity could see each entry at + */ + private array $lastVisible = []; + + /** + * @param Closure(Dn): bool $isVisible + */ + public function __construct( + private readonly ChangeScope $scope, + private readonly Closure $isVisible, + ) {} + + /** + * Read in journal order, which makes an entry's first rename the one that tells where the consumer held it. + */ + public function record(ChangeRecord $record): void + { + $change = $record->change; + $uuid = $change->entryUuid; + $isFirst = !array_key_exists( + $uuid, + $this->latestSeqs, + ); + $this->latestSeqs[$uuid] = $record->seq; + + if ($isFirst && $change->changeType === ChangeType::ModRdn && $change->previousDn !== null) { + $this->see( + $uuid, + $change->previousDn, + ); + } + + $this->see( + $uuid, + $change->dn, + ); + } + + public function isLatest(ChangeRecord $record): bool + { + return ($this->latestSeqs[$record->change->entryUuid] ?? null) === $record->seq; + } + + /** + * The last in-scope DN the sync identity could see the entry at, or null when it never could in this window. + */ + public function lastVisible(string $entryUuid): ?Dn + { + $dn = $this->lastVisible[$entryUuid] ?? null; + + return $dn === null + ? null + : new Dn($dn); + } + + private function see( + string $uuid, + Dn $dn, + ): void { + $name = $dn->toString(); + + if (($this->lastVisible[$uuid] ?? null) === $name) { + return; + } + + if ($this->scope->contains($dn) && ($this->isVisible)($dn)) { + $this->lastVisible[$uuid] = $name; + } + } +} diff --git a/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php b/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php index 04373403..36012222 100644 --- a/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php +++ b/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php @@ -70,19 +70,21 @@ public function projectSince( TokenInterface $token, ): iterable { $scope = $this->scopeFor($request); - $latestSeqs = $this->latestSeqByUuid( + $window = $this->windowOf( $sinceSeq, $throughSeq, $scope, + $token, ); foreach ($this->stream->since($sinceSeq, $scope, $throughSeq) as $record) { - if (($latestSeqs[$record->change->entryUuid] ?? null) !== $record->seq) { + if (!$window->isLatest($record)) { continue; } $result = $this->project( $record->change, + $window->lastVisible($record->change->entryUuid), $scope, $request, $token, @@ -213,28 +215,32 @@ private function pushChanges( } /** - * The seq of each entry's latest change in range. - * - * @return array - * * @throws JournalLapsedException */ - private function latestSeqByUuid( + private function windowOf( int $sinceSeq, int $throughSeq, ChangeScope $scope, - ): array { - $latestSeqs = []; + TokenInterface $token, + ): ChangeWindow { + $window = new ChangeWindow( + $scope, + fn(Dn $dn): bool => $this->projector->isVisibleAt( + $token, + $dn, + ), + ); foreach ($this->stream->since($sinceSeq, $scope, $throughSeq) as $record) { - $latestSeqs[$record->change->entryUuid] = $record->seq; + $window->record($record); } - return $latestSeqs; + return $window; } private function project( PendingChange $change, + ?Dn $lastVisible, ChangeScope $scope, SearchRequest $request, TokenInterface $token, @@ -242,11 +248,12 @@ private function project( return $change->changeType === ChangeType::Delete ? $this->projector->projectDeleted( $change, + $lastVisible, $request, - $token, ) : $this->fetchAndProject( $change, + $lastVisible, $scope, $request, $token, @@ -258,6 +265,7 @@ private function project( */ private function fetchAndProject( PendingChange $change, + ?Dn $lastVisible, ChangeScope $scope, SearchRequest $request, TokenInterface $token, @@ -272,18 +280,19 @@ private function fetchAndProject( return null; } - // Left the content, so RFC 4533 §4.1 wants it deleted rather than sent from where it went. + // RFC 4533 §4.1 wants an entry that left the content deleted rather than sent from where it went. if (!$scope->contains($entry->getDn())) { - return $this->projector->projectMovedOut( - $entry, - $change, - $token, + return $this->projector->projectLeft( + $change->entryUuid, + $lastVisible, ); } return $this->projector->projectFetched( $entry, - $change, + $change->entryUuid, + $change->changeType === ChangeType::Add, + $lastVisible, $request, $token, ); diff --git a/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php b/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php index fc5340cd..591cec12 100644 --- a/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php +++ b/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php @@ -29,7 +29,6 @@ use FreeDSx\Ldap\Server\Backend\Storage\Filter\LinkedLeafWitness; use FreeDSx\Ldap\Server\Backend\Storage\Schema\LinkedAttributes; use FreeDSx\Ldap\Server\Backend\Storage\Search\EntryProjection; -use FreeDSx\Ldap\Server\Backend\Storage\Journal\Change\ChangeType; use FreeDSx\Ldap\Server\Backend\Storage\Journal\Change\PendingChange; use FreeDSx\Ldap\Server\Logging\EventContext; use FreeDSx\Ldap\Server\Logging\EventLogger; @@ -99,21 +98,36 @@ public function projectSearched( ); } + /** + * Whether the sync identity could see an entry at the DN, which every access rule judges by the DN alone. + */ + public function isVisibleAt( + TokenInterface $token, + Dn $dn, + ): bool { + return $this->accessControl->isEntryVisible( + $token, + new Entry($dn), + ); + } + /** * A freshly fetched changed entry, or a delete once a change scopes it out (RFC 4533 §3.3.1). + * + * @param ?Dn $lastVisible The last in-scope DN the sync identity could see the entry at in the window. */ public function projectFetched( Entry $entry, - PendingChange $change, + string $entryUuid, + bool $isAdded, + ?Dn $lastVisible, SearchRequest $request, TokenInterface $token, ): ?SyncResult { - // An entry it can no longer see needs the pre-image to know whether announcing it would leak. if (!$this->accessControl->isEntryVisible($token, $entry)) { - return $this->projectDeleted( - $change, - $request, - $token, + return $this->projectLeft( + $entryUuid, + $lastVisible, ); } @@ -124,58 +138,81 @@ public function projectFetched( $filter, ); - // Still visible, so announcing leaks nothing and no pre-image is needed. + // Visible where it is now, so naming it there leaks nothing. if (!$this->filterEvaluator->evaluate($judged, $filter)) { return $this->deleteResult( - $change->dn, - $change->entryUuid, + $lastVisible ?? $entry->getDn(), + $entryUuid, ); } return $this->addResult( $entry, $request, - $change->changeType === ChangeType::Add + $isAdded ? SyncStateControl::STATE_ADD : SyncStateControl::STATE_MODIFY, ); } /** - * A delete, announced only if the consumer could have seen the entry (checked against its pre-image), so a - * gone entry never leaks a DN/UUID the read-side ACL would have hidden. + * A delete, named by the last DN the sync identity could see the entry at, if its pre-image matched the filter. + * + * @param ?Dn $lastVisible The last in-scope DN the sync identity could see the entry at in the window. */ public function projectDeleted( PendingChange $change, + ?Dn $lastVisible, SearchRequest $request, - TokenInterface $token, ): ?SyncResult { - if (!$this->wasVisible($request, $token, $change->preImage)) { + if ($lastVisible === null || $change->preImage === null) { + return null; + } + + $preImage = self::at( + $change->preImage, + $lastVisible, + ); + + if (!$this->matchedFilter($request, $preImage)) { return null; } return $this->deleteResult( - $change->dn, + $lastVisible, $change->entryUuid, ); } /** - * A move out of the consumer's content, announced as a delete at the DN it left (RFC 4533 §4.1). + * An entry gone from the consumer's view, named by the last DN the identity could see it at (RFC 4533 §7). + * + * @param ?Dn $lastVisible The last in-scope DN the sync identity could see the entry at in the window. */ - public function projectMovedOut( - Entry $entry, - PendingChange $change, - TokenInterface $token, + public function projectLeft( + string $entryUuid, + ?Dn $lastVisible, ): ?SyncResult { - // Still live, so visibility is judged from where it went rather than a pre-image a move never carries. - if ($change->previousDn === null || !$this->accessControl->isEntryVisible($token, $entry)) { + if ($lastVisible === null) { return null; } return $this->deleteResult( - $change->previousDn, - $change->entryUuid, + $lastVisible, + $entryUuid, + ); + } + + /** + * The entry as it would stand at another DN, for a filter that names the DN. + */ + private static function at( + Entry $entry, + Dn $dn, + ): Entry { + return new Entry( + $dn, + ...$entry->getAttributes(), ); } @@ -240,19 +277,10 @@ private function projectionFor(SearchRequest $request): AttributeProjection ); } - private function wasVisible( + private function matchedFilter( SearchRequest $request, - TokenInterface $token, - ?Entry $preImage, + Entry $preImage, ): bool { - if ($preImage === null) { - return false; - } - - if (!$this->accessControl->isEntryVisible($token, $preImage)) { - return false; - } - // A consumer ignores a delete for an entry it never held, where withholding one leaves it stale forever. if ($this->namesSomethingHeldApart($request->getFilter())) { return true; diff --git a/tests/integration/Sync/SyncReplNativeTest.php b/tests/integration/Sync/SyncReplNativeTest.php index 70596724..3934af14 100644 --- a/tests/integration/Sync/SyncReplNativeTest.php +++ b/tests/integration/Sync/SyncReplNativeTest.php @@ -28,6 +28,7 @@ use FreeDSx\Ldap\Search\Filter\FilterInterface; use FreeDSx\Ldap\Search\Filters; use FreeDSx\Ldap\Sync\Result\SyncEntryResult; +use FreeDSx\Ldap\Sync\Result\SyncIdSetResult; use FreeDSx\Ldap\Sync\SyncRepl; use PHPUnit\Framework\Attributes\DataProvider; use Tests\Integration\FreeDSx\Ldap\ServerTestCase; @@ -42,6 +43,13 @@ final class SyncReplNativeTest extends ServerTestCase private const SEED_LDIF = __DIR__ . '/../../resources/seed/sync-seed.ldif'; + /** + * Content the visibility tests sync, apart from what the other tests poll and count. + */ + private const AREA = 'ou=area,dc=other,dc=test'; + + private const HIDDEN = 'ou=hidden,' . self::AREA; + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); @@ -56,6 +64,7 @@ public static function setUpBeforeClass(): void [ '--seed=' . self::SEED_LDIF, '--allow-sync', + '--hide-from-sync=' . self::HIDDEN, ], ); } @@ -276,6 +285,202 @@ public function testAnIncrementalPollWithACookieReturnsOnlyChangesSinceTheCookie ); } + public function testAnEntryMovedWhereTheSyncIdentityCannotSearchIsDeletedWhereItWas(): void + { + $dn = 'cn=hidee,' . self::AREA; + $this->createHiddenArea(); + $this->asAdmin(static fn(LdapClient $admin) => $admin->create(Entry::fromArray( + $dn, + [ + 'cn' => 'hidee', + 'sn' => 'Hidden', + 'objectClass' => 'inetOrgPerson', + ], + ))); + $cookie = $this->cookieAfterAFullPollOf(self::AREA); + + $this->asAdmin(static fn(LdapClient $admin) => $admin->move( + $dn, + self::HIDDEN, + )); + $incremental = $this->syncRepl(baseDn: self::AREA); + $incremental->useCookie($cookie); + + self::assertSame( + [$dn], + $this->collectDeletedDns($incremental), + ); + } + + public function testASubtreeMovedWhereTheSyncIdentityCannotSearchIsDeletedWithItsChildren(): void + { + $ou = 'ou=movers,' . self::AREA; + $this->createHiddenArea(); + $uuids = $this->asAdmin(static function (LdapClient $admin) use ($ou): array { + $admin->create(Entry::fromArray( + $ou, + [ + 'ou' => 'movers', + 'objectClass' => 'organizationalUnit', + ], + )); + + foreach (['one', 'two'] as $name) { + $admin->create(Entry::fromArray( + 'cn=' . $name . ',' . $ou, + [ + 'cn' => $name, + 'sn' => 'Mover', + 'objectClass' => 'inetOrgPerson', + ], + )); + } + + return array_map( + static fn(string $dn): string => (string) $admin->read( + $dn, + ['entryUUID'], + )?->get('entryUUID')?->firstValue(), + [$ou, 'cn=one,' . $ou, 'cn=two,' . $ou], + ); + }); + $cookie = $this->cookieAfterAFullPollOf(self::AREA); + + $this->asAdmin(static fn(LdapClient $admin) => $admin->move( + $ou, + self::HIDDEN, + )); + $incremental = $this->syncRepl(baseDn: self::AREA); + $incremental->useCookie($cookie); + $deleted = $this->collectDeletedUuids($incremental); + sort($uuids); + sort($deleted); + + self::assertSame( + $uuids, + $deleted, + ); + } + + public function testAnEntryLeavingTheContentFromWhereTheSyncIdentityCannotSearchIsNeverNamed(): void + { + $dn = 'cn=secret-name,' . self::HIDDEN; + $this->createHiddenArea(); + $uuid = $this->asAdmin(static function (LdapClient $admin) use ($dn): string { + $admin->create(Entry::fromArray( + $dn, + [ + 'cn' => 'secret-name', + 'sn' => 'Hidden', + 'objectClass' => 'inetOrgPerson', + ], + )); + + return (string) $admin->read( + $dn, + ['entryUUID'], + )?->get('entryUUID')?->firstValue(); + }); + $cookie = $this->cookieAfterAFullPollOf(self::AREA); + + $this->asAdmin(static fn(LdapClient $admin) => $admin->move( + $dn, + 'dc=other,dc=test', + )); + $incremental = $this->syncRepl(baseDn: self::AREA); + $incremental->useCookie($cookie); + $named = $this->collectNamed($incremental); + + self::assertNotContains( + strtolower($dn), + $named, + ); + self::assertNotContains( + $uuid, + $named, + ); + } + + public function testAnEntryMovedIntoTheContentAndThenHiddenIsDeletedWhereItWasVisible(): void + { + $dn = 'cn=arrival,dc=other,dc=test'; + $this->createHiddenArea(); + $this->asAdmin(static fn(LdapClient $admin) => $admin->create(Entry::fromArray( + $dn, + [ + 'cn' => 'arrival', + 'sn' => 'Arrival', + 'objectClass' => 'inetOrgPerson', + ], + ))); + $cookie = $this->cookieAfterAFullPollOf(self::AREA); + + $this->asAdmin(static function (LdapClient $admin) use ($dn): void { + $admin->move( + $dn, + self::AREA, + ); + $admin->move( + 'cn=arrival,' . self::AREA, + self::HIDDEN, + ); + }); + $incremental = $this->syncRepl(baseDn: self::AREA); + $incremental->useCookie($cookie); + + self::assertSame( + ['cn=arrival,' . self::AREA], + $this->collectDeletedDns($incremental), + ); + } + + public function testAnEntryLeavingTheFilterAfterAMoveOutOfAHiddenPlaceNeverNamesTheHiddenDn(): void + { + $hiddenDn = 'cn=unfiltered,' . self::HIDDEN; + $filter = Filters::equal( + 'description', + 'keep', + ); + $this->createHiddenArea(); + $this->asAdmin(static fn(LdapClient $admin) => $admin->create(Entry::fromArray( + $hiddenDn, + [ + 'cn' => 'unfiltered', + 'sn' => 'Unfiltered', + 'description' => 'keep', + 'objectClass' => 'inetOrgPerson', + ], + ))); + $cookie = $this->cookieAfterAFullPollOf( + self::AREA, + $filter, + ); + + $this->asAdmin(static function (LdapClient $admin) use ($hiddenDn): void { + $admin->move( + $hiddenDn, + self::AREA, + ); + $admin->sendAndReceive(Operations::modify( + 'cn=unfiltered,' . self::AREA, + Change::replace( + 'description', + 'drop', + ), + )); + }); + $incremental = $this->syncRepl( + $filter, + self::AREA, + ); + $incremental->useCookie($cookie); + + self::assertNotContains( + strtolower($hiddenDn), + $this->collectNamed($incremental), + ); + } + public function testACookieReusedForDifferentContentFallsBackToAFullRefresh(): void { $this->authenticateAdmin(); @@ -403,11 +608,13 @@ public function testASyncWithAMalformedBaseDnIsRefused(): void } } - private function syncRepl(?FilterInterface $filter = null): SyncRepl - { + private function syncRepl( + ?FilterInterface $filter = null, + string $baseDn = 'dc=foo,dc=bar', + ): SyncRepl { $syncRepl = $this->ldapClient()->syncRepl($filter); $syncRepl->request() - ->base('dc=foo,dc=bar') + ->base($baseDn) ->useSubtreeScope(); return $syncRepl; @@ -426,6 +633,107 @@ private function collectPoll(SyncRepl $syncRepl): array return $dns; } + private function createHiddenArea(): void + { + $this->asAdmin(static function (LdapClient $admin): void { + if ($admin->read(self::HIDDEN) !== null) { + return; + } + + foreach ([self::AREA => 'area', self::HIDDEN => 'hidden'] as $dn => $ou) { + $admin->create(Entry::fromArray( + $dn, + [ + 'ou' => $ou, + 'objectClass' => 'organizationalUnit', + ], + )); + } + }); + } + + private function cookieAfterAFullPollOf( + string $baseDn, + ?FilterInterface $filter = null, + ): string { + $cookie = null; + $initial = $this->syncRepl( + $filter, + $baseDn, + ); + $initial->useCookieHandler(function (string $value) use (&$cookie): void { + $cookie = $value; + }); + $initial->poll(); + + self::assertNotNull( + $cookie, + 'The refresh phase must hand back a cookie for the next poll.', + ); + + return $cookie; + } + + /** + * @return list + */ + private function collectDeletedDns(SyncRepl $syncRepl): array + { + $dns = []; + $syncRepl->poll(function (SyncEntryResult $result) use (&$dns): void { + if ($result->isDelete()) { + $dns[] = strtolower($result->getEntry()->getDn()->toString()); + } + }); + + return $dns; + } + + /** + * @return list + */ + private function collectDeletedUuids(SyncRepl $syncRepl): array + { + $uuids = []; + $syncRepl->useIdSetHandler(function (SyncIdSetResult $result) use (&$uuids): void { + if (!$result->isDeleted()) { + return; + } + + foreach ($result->getDecodedEntryUuids() as $uuid) { + $uuids[] = $uuid; + } + }); + $syncRepl->poll(function (SyncEntryResult $result) use (&$uuids): void { + if ($result->isDelete()) { + $uuids[] = $result->getDecodedEntryUuid(); + } + }); + + return $uuids; + } + + /** + * Every DN and entryUUID a poll names, whether in an entry or in a set of UUIDs. + * + * @return list + */ + private function collectNamed(SyncRepl $syncRepl): array + { + $named = []; + $syncRepl->useIdSetHandler(function (SyncIdSetResult $result) use (&$named): void { + foreach ($result->getDecodedEntryUuids() as $uuid) { + $named[] = $uuid; + } + }); + $syncRepl->poll(function (SyncEntryResult $result) use (&$named): void { + $named[] = strtolower($result->getEntry()->getDn()->toString()); + $named[] = $result->getDecodedEntryUuid(); + }); + + return $named; + } + /** * A new entry and a group naming it, returning the entry's DN. */ diff --git a/tests/integration/Sync/SyncReplReplicaTestCase.php b/tests/integration/Sync/SyncReplReplicaTestCase.php index 4b531235..0a7bea9d 100644 --- a/tests/integration/Sync/SyncReplReplicaTestCase.php +++ b/tests/integration/Sync/SyncReplReplicaTestCase.php @@ -20,6 +20,7 @@ use FreeDSx\Ldap\LdapClient; use FreeDSx\Ldap\Operations; use Tests\Integration\FreeDSx\Ldap\ServerTestCase; +use Tests\Support\FreeDSx\Ldap\LdapReplicaCommand; use Tests\Support\FreeDSx\Ldap\TestWorker; use Throwable; @@ -229,6 +230,48 @@ public function test_a_move_out_of_the_replicated_scope_deletes_from_the_replica ); } + public function test_an_entry_moved_where_the_sync_identity_cannot_read_leaves_the_replica(): void + { + $this->writeToProvider(static function (LdapClient $provider): void { + $provider->create(Entry::fromArray( + LdapReplicaCommand::HIDDEN_FROM_SYNC, + [ + 'objectClass' => 'organizationalUnit', + 'ou' => 'hidden', + ], + )); + $provider->create(Entry::fromArray( + 'cn=ivy,ou=people,dc=foo,dc=bar', + [ + 'objectClass' => 'inetOrgPerson', + 'cn' => 'ivy', + 'sn' => 'Ivy', + 'userPassword' => 'ivypass', + ], + )); + }); + self::assertNotNull($this->waitForReplica('cn=ivy,ou=people,dc=foo,dc=bar')); + $this->assertBind( + 'cn=ivy,ou=people,dc=foo,dc=bar', + 'ivypass', + true, + ); + + $this->writeToProvider(static function (LdapClient $provider): void { + $provider->move( + 'cn=ivy,ou=people,dc=foo,dc=bar', + LdapReplicaCommand::HIDDEN_FROM_SYNC, + ); + }); + + self::assertNull($this->waitForReplicaGone('cn=ivy,ou=people,dc=foo,dc=bar')); + $this->assertBind( + 'cn=ivy,ou=people,dc=foo,dc=bar', + 'ivypass', + false, + ); + } + public function test_a_client_write_to_the_replica_is_referred_to_the_provider(): void { self::assertNotNull($this->waitForReplica('cn=alice,ou=people,dc=foo,dc=bar')); diff --git a/tests/support/LdapReplicaCommand.php b/tests/support/LdapReplicaCommand.php index 539ae343..3af162d8 100644 --- a/tests/support/LdapReplicaCommand.php +++ b/tests/support/LdapReplicaCommand.php @@ -31,6 +31,11 @@ final class LdapReplicaCommand extends Command { use ConsoleOptionsTrait; + /** + * A subtree the provider hides from the replica's sync identity. + */ + public const HIDDEN_FROM_SYNC = 'ou=hidden,ou=people,dc=foo,dc=bar'; + /** * Tests assert on forwarded bind state, so they should not wait a production drain for it. */ @@ -135,6 +140,7 @@ private function startProvider( '--storage=sqlite', '--seed=' . self::SEED, '--allow-sync', + '--hide-from-sync=' . self::HIDDEN_FROM_SYNC, ]; if ($forward) { diff --git a/tests/support/LdapServerCommand.php b/tests/support/LdapServerCommand.php index c6cd9fc2..1057fdbe 100644 --- a/tests/support/LdapServerCommand.php +++ b/tests/support/LdapServerCommand.php @@ -12,9 +12,11 @@ use FreeDSx\Ldap\Ldif\Loader\FileLdifLoader; use FreeDSx\Ldap\Control\Control; use FreeDSx\Ldap\Ldif\Output\FileLdifOutput; +use FreeDSx\Ldap\Operation\OperationType; use FreeDSx\Ldap\Operation\Request\ExtendedRequest; use FreeDSx\Ldap\Server\AccessControl\Rule\ControlRule; use FreeDSx\Ldap\Server\AccessControl\Rule\ExtendedOperationRule; +use FreeDSx\Ldap\Server\AccessControl\Rule\OperationRule; use FreeDSx\Ldap\Server\AccessControl\Subject\Subject; use FreeDSx\Ldap\Schema\Definition\PasswordPolicyOid; use FreeDSx\Ldap\Server\AccessControl\Rule\AttributeRule; @@ -280,7 +282,13 @@ protected function configure(): void 'allow-sync', null, InputOption::VALUE_NONE, - 'Grant authenticated identities the (privileged) content-sync control over dc=foo,dc=bar', + 'Grant cn=user the (privileged) content-sync control over dc=foo,dc=bar and dc=other,dc=test', + ) + ->addOption( + 'hide-from-sync', + null, + InputOption::VALUE_REQUIRED, + 'A subtree cn=user, the sync identity, may not search or compare', ) ->addOption( 'allow-ppolicy-forward', @@ -509,10 +517,32 @@ protected function execute( if ($input->getOption('allow-sync') === true) { $options->setAclRules( - $options->getAclRules()->appendControlRules(ControlRule::allow( + $options->getAclRules()->appendControlRules( + ControlRule::allow( + Subject::dn('cn=user,dc=foo,dc=bar'), + Target::subtree('dc=foo,dc=bar'), + Control::OID_SYNC_REQUEST, + ), + ControlRule::allow( + Subject::dn('cn=user,dc=foo,dc=bar'), + Target::subtree('dc=other,dc=test'), + Control::OID_SYNC_REQUEST, + ), + ), + ); + } + + $hiddenFromSync = $this->getStringOption( + $input, + 'hide-from-sync', + ); + if ($hiddenFromSync !== '') { + $options->setAclRules( + $options->getAclRules()->prependOperationRules(OperationRule::deny( Subject::dn('cn=user,dc=foo,dc=bar'), - Target::subtree('dc=foo,dc=bar'), - Control::OID_SYNC_REQUEST, + Target::subtree($hiddenFromSync), + OperationType::Search, + OperationType::Compare, )), ); } diff --git a/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php b/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php index ce03e7d4..f23da289 100644 --- a/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php +++ b/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php @@ -442,7 +442,7 @@ public function test_an_entry_vacating_a_dn_is_sent_before_one_that_later_takes_ ); } - public function test_a_changed_entry_moved_out_of_the_content_since_the_change_is_not_sent(): void + public function test_a_changed_entry_moved_out_of_the_content_since_the_change_is_deleted_at_its_last_visible_dn(): void { $this->append(ChangeType::Modify, 'cn=a,dc=example,dc=com', self::UUID_A); $this->liveEntries['cn=a,dc=elsewhere,dc=org'] = $this->entry('cn=a,dc=elsewhere,dc=org', self::UUID_A); @@ -450,9 +450,13 @@ public function test_a_changed_entry_moved_out_of_the_content_since_the_change_i $this->handle($this->cookieAt(0)); self::assertSame( - [], + [[SyncStateControl::STATE_DELETE, self::UUID_A]], $this->states(), ); + self::assertSame( + ['cn=a,dc=example,dc=com'], + $this->sentEntryDns(), + ); } public function test_another_entry_now_at_the_changed_dn_is_not_sent_in_its_place(): void diff --git a/tests/unit/Sync/Provider/SyncPersistStreamerTest.php b/tests/unit/Sync/Provider/SyncPersistStreamerTest.php index 136deaa8..cdbbdbf9 100644 --- a/tests/unit/Sync/Provider/SyncPersistStreamerTest.php +++ b/tests/unit/Sync/Provider/SyncPersistStreamerTest.php @@ -73,6 +73,8 @@ final class SyncPersistStreamerTest extends TestCase private const ENTRY_UUID = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'; + private const HIDDEN = 'ou=hidden,dc=example,dc=com'; + private ServerQueue&MockObject $queue; private ReadEntryInterface&MockObject $entries; @@ -104,6 +106,8 @@ final class SyncPersistStreamerTest extends TestCase private ?EntryProjection $readWith = null; + private int $visibilityChecks = 0; + protected function setUp(): void { $this->onSleep = static function (): void {}; @@ -142,7 +146,13 @@ protected function setUp(): void $accessControl = $this->createMock(AccessControlInterface::class); $accessControl ->method('isEntryVisible') - ->willReturn(true); + ->willReturnCallback(function (TokenInterface $token, Entry $entry): bool { + $this->visibilityChecks++; + + return !$entry + ->getDn() + ->isDescendantOf(new Dn(self::HIDDEN)); + }); $filterEvaluator = $this->createMock(FilterEvaluatorInterface::class); $filterEvaluator ->method('evaluate') @@ -216,6 +226,183 @@ public function test_project_since_sends_each_entry_once_in_the_order_of_its_lat ); } + public function test_an_entry_moved_into_a_hidden_place_is_deleted_at_its_last_visible_dn(): void + { + $this->liveEntries['cn=a,ou=hidden,dc=example,dc=com'] = $this->entryAt( + 'cn=a,ou=hidden,dc=example,dc=com', + self::ENTRY_UUID, + ); + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=a,ou=hidden,dc=example,dc=com', + ); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, 'cn=a,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_an_entry_moved_out_of_the_content_is_deleted_at_its_previous_dn(): void + { + $this->liveEntries['cn=a,dc=other,dc=com'] = $this->entryAt( + 'cn=a,dc=other,dc=com', + self::ENTRY_UUID, + ); + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=a,dc=other,dc=com', + ); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, 'cn=a,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_an_entry_added_and_hidden_in_one_window_is_deleted_where_it_was_added(): void + { + $this->liveEntries['cn=a,ou=hidden,dc=example,dc=com'] = $this->entryAt( + 'cn=a,ou=hidden,dc=example,dc=com', + self::ENTRY_UUID, + ); + $this->appendFor( + ChangeType::Add, + 'cn=a,dc=example,dc=com', + self::ENTRY_UUID, + ); + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=a,ou=hidden,dc=example,dc=com', + ); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, 'cn=a,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_an_entry_added_where_the_consumer_cannot_see_it_is_never_named(): void + { + $this->liveEntries['cn=a,dc=other,dc=com'] = $this->entryAt( + 'cn=a,dc=other,dc=com', + self::ENTRY_UUID, + ); + $this->appendFor( + ChangeType::Add, + 'cn=a,ou=hidden,dc=example,dc=com', + self::ENTRY_UUID, + ); + $this->appendMove( + 'cn=a,ou=hidden,dc=example,dc=com', + 'cn=a,dc=other,dc=com', + ); + + self::assertSame( + [], + $this->projected(), + ); + } + + public function test_a_delete_after_several_moves_is_named_by_its_dn_at_deletion(): void + { + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=b,dc=example,dc=com', + ); + $this->appendMove( + 'cn=b,dc=example,dc=com', + 'cn=c,dc=example,dc=com', + ); + $this->appendDelete('cn=c,dc=example,dc=com'); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, 'cn=c,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_a_delete_after_a_move_into_a_hidden_place_is_named_where_it_was_last_visible(): void + { + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=a,ou=hidden,dc=example,dc=com', + ); + $this->appendDelete('cn=a,ou=hidden,dc=example,dc=com'); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, 'cn=a,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_a_delete_where_the_identity_never_saw_the_entry_is_withheld(): void + { + $this->appendDelete('cn=a,ou=hidden,dc=example,dc=com'); + + self::assertSame( + [], + $this->projected(), + ); + } + + public function test_an_entry_moved_in_from_outside_then_hidden_is_deleted_where_it_was_visible(): void + { + $this->liveEntries['cn=a,ou=hidden,dc=example,dc=com'] = $this->entryAt( + 'cn=a,ou=hidden,dc=example,dc=com', + self::ENTRY_UUID, + ); + $this->appendMove( + 'cn=a,dc=other,dc=com', + 'cn=a,dc=example,dc=com', + ); + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=a,ou=hidden,dc=example,dc=com', + ); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, 'cn=a,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_an_entry_hidden_and_visible_again_in_one_window_is_sent_as_a_modify(): void + { + $this->liveEntries['cn=b,dc=example,dc=com'] = $this->entryAt( + 'cn=b,dc=example,dc=com', + self::ENTRY_UUID, + ); + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=a,ou=hidden,dc=example,dc=com', + ); + $this->appendMove( + 'cn=a,ou=hidden,dc=example,dc=com', + 'cn=b,dc=example,dc=com', + ); + + self::assertSame( + [[SyncStateControl::STATE_MODIFY, 'cn=b,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_a_run_of_modifies_checks_visibility_once_in_the_window_and_once_when_sent(): void + { + $this->liveEntries[self::ENTRY_DN] = $this->entry(); + $this->append(ChangeType::Modify); + $this->append(ChangeType::Modify); + $this->append(ChangeType::Modify); + + $this->projected(); + + self::assertSame( + 2, + $this->visibilityChecks, + ); + } + public function test_a_journal_trimmed_mid_projection_ends_the_stream_with_refresh_required(): void { foreach (['1', '2', '3'] as $n) { @@ -509,6 +696,56 @@ private function appendFor( )); } + private function appendMove( + string $from, + string $to, + ): void { + $this->journal->append(new PendingChange( + changeType: ChangeType::ModRdn, + dn: new Dn($to), + entryUuid: self::ENTRY_UUID, + authzId: AuthzId::anonymous(), + previousDn: new Dn($from), + )); + } + + private function appendDelete(string $dn): void + { + $this->journal->append(new PendingChange( + changeType: ChangeType::Delete, + dn: new Dn($dn), + entryUuid: self::ENTRY_UUID, + authzId: AuthzId::anonymous(), + preImage: $this->entryAt( + $dn, + self::ENTRY_UUID, + ), + )); + } + + /** + * @return list + */ + private function projected(): array + { + $results = $this->subject->projectSince( + 0, + $this->journal->latestSeq(), + $this->request(), + $this->token, + ); + $projected = []; + + foreach ($results as $result) { + $projected[] = [ + $result->control->getState(), + $result->entry->getEntry()->getDn()->toString(), + ]; + } + + return $projected; + } + private function liveEntryHolding(string $uuid): ?Entry { foreach ($this->liveEntries as $entry) { diff --git a/tests/unit/Sync/Provider/SyncResultProjectorTest.php b/tests/unit/Sync/Provider/SyncResultProjectorTest.php index 6cdb3b01..9cfa5cb0 100644 --- a/tests/unit/Sync/Provider/SyncResultProjectorTest.php +++ b/tests/unit/Sync/Provider/SyncResultProjectorTest.php @@ -32,6 +32,7 @@ use FreeDSx\Ldap\Server\Logging\EventLogPolicy; use FreeDSx\Ldap\Server\Logging\ServerEvent; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\Sync\Provider\SyncResult; use FreeDSx\Ldap\Sync\Provider\SyncResultProjector; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -45,6 +46,8 @@ final class SyncResultProjectorTest extends TestCase private const UUID = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'; + private const HIDDEN = 'ou=hidden,dc=example,dc=com'; + private AccessControlInterface&MockObject $accessControl; private FilterEvaluatorInterface&MockObject $filterEvaluator; @@ -67,7 +70,8 @@ protected function setUp(): void $this->accessControl ->method('isEntryVisible') - ->willReturnCallback(fn(TokenInterface $token, Entry $entry): bool => !$this->hidden); + ->willReturnCallback(fn(TokenInterface $token, Entry $entry): bool => !$this->hidden + && !$entry->getDn()->isDescendantOf(new Dn(self::HIDDEN))); $this->filterEvaluator ->method('evaluate') ->willReturnCallback(function (Entry $entry): bool { @@ -175,7 +179,9 @@ public function test_a_fetched_entry_that_leaves_the_filter_becomes_a_delete(): $result = $this->subject->projectFetched( $this->entry('cn=a,dc=example,dc=com', self::UUID), - $this->change(ChangeType::Modify), + self::UUID, + false, + new Dn('cn=a,dc=example,dc=com'), $this->request(), $this->token, ); @@ -187,6 +193,40 @@ public function test_a_fetched_entry_that_leaves_the_filter_becomes_a_delete(): ); } + public function test_a_fetched_entry_that_leaves_the_filter_is_named_by_its_last_visible_dn(): void + { + $this->filterMatches = false; + + self::assertSame( + 'cn=a,ou=people,dc=example,dc=com', + $this->deletedDnOf($this->subject->projectFetched( + $this->entry('cn=a,ou=staff,dc=example,dc=com', self::UUID), + self::UUID, + false, + new Dn('cn=a,ou=people,dc=example,dc=com'), + $this->request(), + $this->token, + )), + ); + } + + public function test_a_fetched_entry_that_leaves_the_filter_without_a_visible_dn_is_named_where_it_is_now(): void + { + $this->filterMatches = false; + + self::assertSame( + 'cn=a,ou=staff,dc=example,dc=com', + $this->deletedDnOf($this->subject->projectFetched( + $this->entry('cn=a,ou=staff,dc=example,dc=com', self::UUID), + self::UUID, + false, + null, + $this->request(), + $this->token, + )), + ); + } + public function test_a_fetched_entry_is_judged_with_the_back_links_the_filter_names(): void { $this->fromContainer(InMemoryStorage::class)->store(Entry::create( @@ -204,7 +244,9 @@ public function test_a_fetched_entry_is_judged_with_the_back_links_the_filter_na 'cn=a,dc=example,dc=com', self::UUID, ), - $this->change(ChangeType::Modify), + self::UUID, + false, + new Dn('cn=a,dc=example,dc=com'), (new SearchRequest(Filters::equal( 'memberOf', 'cn=staff,dc=example,dc=com', @@ -245,23 +287,76 @@ public function test_a_changed_entry_is_read_without_the_backlinks_the_request_l ); } - public function test_a_fetched_entry_hidden_by_acl_without_a_pre_image_is_null(): void + public function test_an_entry_is_visible_at_a_dn_the_access_rules_allow(): void { - $this->hidden = true; + self::assertSame( + [true, false], + [ + $this->subject->isVisibleAt( + $this->token, + new Dn('cn=a,dc=example,dc=com'), + ), + $this->subject->isVisibleAt( + $this->token, + new Dn('cn=a,ou=hidden,dc=example,dc=com'), + ), + ], + ); + } + public function test_an_entry_hidden_now_is_deleted_at_its_last_visible_dn(): void + { + self::assertSame( + 'cn=a,ou=people,dc=example,dc=com', + $this->deletedDnOf($this->subject->projectFetched( + $this->entry('cn=a,ou=hidden,dc=example,dc=com', self::UUID), + self::UUID, + false, + new Dn('cn=a,ou=people,dc=example,dc=com'), + $this->request(), + $this->token, + )), + ); + } + + public function test_an_entry_hidden_now_that_was_never_visible_sends_nothing(): void + { self::assertNull($this->subject->projectFetched( - $this->entry('cn=a,dc=example,dc=com', self::UUID), - $this->change(ChangeType::Modify), + $this->entry('cn=a,ou=hidden,dc=example,dc=com', self::UUID), + self::UUID, + true, + null, $this->request(), $this->token, )); } + public function test_an_entry_that_left_is_deleted_at_its_last_visible_dn(): void + { + self::assertSame( + 'cn=a,ou=people,dc=example,dc=com', + $this->deletedDnOf($this->subject->projectLeft( + self::UUID, + new Dn('cn=a,ou=people,dc=example,dc=com'), + )), + ); + } + + public function test_an_entry_that_left_without_a_visible_dn_sends_nothing(): void + { + self::assertNull($this->subject->projectLeft( + self::UUID, + null, + )); + } + public function test_a_fetched_modify_is_projected_as_a_modify(): void { $result = $this->subject->projectFetched( $this->entry('cn=a,dc=example,dc=com', self::UUID), - $this->change(ChangeType::Modify), + self::UUID, + false, + new Dn('cn=a,dc=example,dc=com'), $this->request(), $this->token, ); @@ -277,7 +372,9 @@ public function test_a_fetched_add_is_projected_as_an_add(): void { $result = $this->subject->projectFetched( $this->entry('cn=a,dc=example,dc=com', self::UUID), - $this->change(ChangeType::Add), + self::UUID, + true, + new Dn('cn=a,dc=example,dc=com'), $this->request(), $this->token, ); @@ -293,8 +390,8 @@ public function test_a_visible_delete_with_a_pre_image_becomes_a_delete(): void { $result = $this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), + new Dn('cn=a,dc=example,dc=com'), $this->request(), - $this->token, ); self::assertNotNull($result); @@ -304,16 +401,37 @@ public function test_a_visible_delete_with_a_pre_image_becomes_a_delete(): void ); } + public function test_a_delete_is_named_by_its_last_visible_dn(): void + { + self::assertSame( + 'cn=a,ou=people,dc=example,dc=com', + $this->deletedDnOf($this->subject->projectDeleted( + $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), + new Dn('cn=a,ou=people,dc=example,dc=com'), + $this->request(), + )), + ); + } + + public function test_a_delete_without_a_visible_dn_is_null(): void + { + self::assertNull($this->subject->projectDeleted( + $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), + null, + $this->request(), + )); + } + public function test_a_delete_is_announced_when_the_filter_rests_on_linked_values(): void { $this->filterMatches = false; $result = $this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), + new Dn('cn=a,dc=example,dc=com'), (new SearchRequest(Filters::equal('member', 'cn=a,dc=example,dc=com'))) ->base('dc=example,dc=com') ->useSubtreeScope(), - $this->token, ); self::assertNotNull($result); @@ -329,21 +447,8 @@ public function test_a_delete_the_filter_excludes_is_still_withheld(): void self::assertNull($this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), + new Dn('cn=a,dc=example,dc=com'), $this->request(), - $this->token, - )); - } - - public function test_a_delete_hidden_by_access_control_is_withheld_even_with_a_linked_filter(): void - { - $this->hidden = true; - - self::assertNull($this->subject->projectDeleted( - $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), - (new SearchRequest(Filters::equal('member', 'cn=a,dc=example,dc=com'))) - ->base('dc=example,dc=com') - ->useSubtreeScope(), - $this->token, )); } @@ -351,8 +456,8 @@ public function test_a_delete_without_a_pre_image_is_null(): void { self::assertNull($this->subject->projectDeleted( $this->deleteChange(null), + new Dn('cn=a,dc=example,dc=com'), $this->request(), - $this->token, )); } @@ -396,16 +501,6 @@ private function entry( ); } - private function change(ChangeType $changeType): PendingChange - { - return new PendingChange( - changeType: $changeType, - dn: new Dn('cn=a,dc=example,dc=com'), - entryUuid: self::UUID, - authzId: AuthzId::anonymous(), - ); - } - private function deleteChange(?Entry $preImage): PendingChange { return new PendingChange( @@ -417,6 +512,15 @@ private function deleteChange(?Entry $preImage): PendingChange ); } + private function deletedDnOf(?SyncResult $result): ?string + { + if ($result === null || !$result->control->isDelete()) { + return null; + } + + return $result->entry->getEntry()->getDn()->toString(); + } + private function request(string ...$attributes): SearchRequest { return (new SearchRequest(Filters::present('objectClass'), ...$attributes)) From d4bd32364368771030e7becf92f69be3e733abb1 Mon Sep 17 00:00:00 2001 From: Chad Sikorra Date: Wed, 18 Nov 2026 16:47:29 -0500 Subject: [PATCH 2/2] Send a sync delete for an entry changed out of the filter before it was deleted. --- docs/Server/Replication.md | 3 +- .../Ldap/Sync/Provider/ChangeWindow.php | 17 +++++ .../Sync/Provider/SyncPersistStreamer.php | 14 +++- .../Sync/Provider/SyncResultProjector.php | 39 ++++------ tests/integration/Sync/SyncReplNativeTest.php | 74 +++++++++++++++++++ .../ServerSyncHandlerTest.php | 11 ++- .../Sync/Provider/SyncPersistStreamerTest.php | 65 +++++++++++++++- .../Sync/Provider/SyncResultProjectorTest.php | 21 ++++++ 8 files changed, 210 insertions(+), 34 deletions(-) diff --git a/docs/Server/Replication.md b/docs/Server/Replication.md index db854ecd..e38a8fc9 100644 --- a/docs/Server/Replication.md +++ b/docs/Server/Replication.md @@ -68,7 +68,8 @@ sequence number, the configured origin, a timestamp, the entry's UUID and DN, an entry. A consumer presents a cookie, which is a marker of how far it has already read. The server then sends the changes that -came after that point. The journal is the single source behind sync and change auditing. +came after that point. The journal is the single source behind sync and change auditing. A consumer can also receive a +delete for an entry it never held, such as one added and then removed since its cookie, and should ignore it. ## Linked Attributes diff --git a/src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php b/src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php index 9952e039..e90bbd9a 100644 --- a/src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php +++ b/src/FreeDSx/Ldap/Sync/Provider/ChangeWindow.php @@ -38,6 +38,11 @@ final class ChangeWindow */ private array $lastVisible = []; + /** + * @var array + */ + private array $changedEarlier = []; + /** * @param Closure(Dn): bool $isVisible */ @@ -59,6 +64,10 @@ public function record(ChangeRecord $record): void ); $this->latestSeqs[$uuid] = $record->seq; + if (!$isFirst) { + $this->changedEarlier[$uuid] = true; + } + if ($isFirst && $change->changeType === ChangeType::ModRdn && $change->previousDn !== null) { $this->see( $uuid, @@ -77,6 +86,14 @@ public function isLatest(ChangeRecord $record): bool return ($this->latestSeqs[$record->change->entryUuid] ?? null) === $record->seq; } + /** + * Whether the window changed the entry before its latest change. + */ + public function hasEarlierChange(string $entryUuid): bool + { + return isset($this->changedEarlier[$entryUuid]); + } + /** * The last in-scope DN the sync identity could see the entry at, or null when it never could in this window. */ diff --git a/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php b/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php index 36012222..c2c5ccd0 100644 --- a/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php +++ b/src/FreeDSx/Ldap/Sync/Provider/SyncPersistStreamer.php @@ -84,7 +84,7 @@ public function projectSince( $result = $this->project( $record->change, - $window->lastVisible($record->change->entryUuid), + $window, $scope, $request, $token, @@ -240,15 +240,18 @@ private function windowOf( private function project( PendingChange $change, - ?Dn $lastVisible, + ChangeWindow $window, ChangeScope $scope, SearchRequest $request, TokenInterface $token, ): ?SyncResult { + $lastVisible = $window->lastVisible($change->entryUuid); + return $change->changeType === ChangeType::Delete ? $this->projector->projectDeleted( $change, $lastVisible, + $window->hasEarlierChange($change->entryUuid), $request, ) : $this->fetchAndProject( @@ -275,9 +278,12 @@ private function fetchAndProject( $this->projector->fetchProjectionFor($request), ); - // Deleted since, and its own journaled delete announces that. + // Deleted since, and announced now because a later delete judged by its pre-image alone could be withheld. if ($entry === null) { - return null; + return $this->projector->projectLeft( + $change->entryUuid, + $lastVisible, + ); } // RFC 4533 §4.1 wants an entry that left the content deleted rather than sent from where it went. diff --git a/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php b/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php index 591cec12..91513c27 100644 --- a/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php +++ b/src/FreeDSx/Ldap/Sync/Provider/SyncResultProjector.php @@ -156,25 +156,22 @@ public function projectFetched( } /** - * A delete, named by the last DN the sync identity could see the entry at, if its pre-image matched the filter. + * A delete named by the last DN the identity could see the entry at, unless its pre-image shows it was never held. * * @param ?Dn $lastVisible The last in-scope DN the sync identity could see the entry at in the window. */ public function projectDeleted( PendingChange $change, ?Dn $lastVisible, + bool $changedEarlier, SearchRequest $request, ): ?SyncResult { - if ($lastVisible === null || $change->preImage === null) { + if ($lastVisible === null) { return null; } - $preImage = self::at( - $change->preImage, - $lastVisible, - ); - - if (!$this->matchedFilter($request, $preImage)) { + // After an earlier change in the window, the pre-image no longer shows what the consumer holds. + if (!$changedEarlier && !$this->preImageMatched($change, $request)) { return null; } @@ -203,19 +200,6 @@ public function projectLeft( ); } - /** - * The entry as it would stand at another DN, for a filter that names the DN. - */ - private static function at( - Entry $entry, - Dn $dn, - ): Entry { - return new Entry( - $dn, - ...$entry->getAttributes(), - ); - } - private function deleteResult( Dn $dn, string $entryUuid, @@ -277,16 +261,23 @@ private function projectionFor(SearchRequest $request): AttributeProjection ); } - private function matchedFilter( + private function preImageMatched( + PendingChange $change, SearchRequest $request, - Entry $preImage, ): bool { + if ($change->preImage === null) { + return false; + } + // A consumer ignores a delete for an entry it never held, where withholding one leaves it stale forever. if ($this->namesSomethingHeldApart($request->getFilter())) { return true; } - return $this->filterEvaluator->evaluate($preImage, $request->getFilter()); + return $this->filterEvaluator->evaluate( + $change->preImage, + $request->getFilter(), + ); } /** diff --git a/tests/integration/Sync/SyncReplNativeTest.php b/tests/integration/Sync/SyncReplNativeTest.php index 3934af14..b2d25d50 100644 --- a/tests/integration/Sync/SyncReplNativeTest.php +++ b/tests/integration/Sync/SyncReplNativeTest.php @@ -481,6 +481,80 @@ public function testAnEntryLeavingTheFilterAfterAMoveOutOfAHiddenPlaceNeverNames ); } + public function testAModifyOutOfTheFilterThenADeleteSendsTheDelete(): void + { + $dn = 'cn=dropped,' . self::AREA; + $filter = Filters::equal( + 'description', + 'keep', + ); + $this->createHiddenArea(); + $this->asAdmin(static fn(LdapClient $admin) => $admin->create(Entry::fromArray( + $dn, + [ + 'cn' => 'dropped', + 'sn' => 'Dropped', + 'description' => 'keep', + 'objectClass' => 'inetOrgPerson', + ], + ))); + $cookie = $this->cookieAfterAFullPollOf( + self::AREA, + $filter, + ); + + $this->asAdmin(static function (LdapClient $admin) use ($dn): void { + $admin->sendAndReceive(Operations::modify( + $dn, + Change::replace( + 'description', + 'drop', + ), + )); + $admin->delete($dn); + }); + $incremental = $this->syncRepl( + $filter, + self::AREA, + ); + $incremental->useCookie($cookie); + + self::assertSame( + [strtolower($dn)], + $this->collectDeletedDns($incremental), + ); + } + + public function testAnEntryMovedOutOfTheContentAndDeletedThereIsDeletedWhereItWasVisible(): void + { + $dn = 'cn=leaver,' . self::AREA; + $this->createHiddenArea(); + $this->asAdmin(static fn(LdapClient $admin) => $admin->create(Entry::fromArray( + $dn, + [ + 'cn' => 'leaver', + 'sn' => 'Leaver', + 'objectClass' => 'inetOrgPerson', + ], + ))); + $cookie = $this->cookieAfterAFullPollOf(self::AREA); + + $this->asAdmin(static function (LdapClient $admin) use ($dn): void { + $admin->move( + $dn, + 'dc=other,dc=test', + ); + $admin->delete('cn=leaver,dc=other,dc=test'); + }); + $incremental = $this->syncRepl(baseDn: self::AREA); + $incremental->useCookie($cookie); + + self::assertSame( + [$dn], + $this->collectDeletedDns($incremental), + ); + } + public function testACookieReusedForDifferentContentFallsBackToAFullRefresh(): void { $this->authenticateAdmin(); diff --git a/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php b/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php index f23da289..90c7e565 100644 --- a/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php +++ b/tests/unit/Protocol/ServerProtocolHandler/ServerSyncHandlerTest.php @@ -467,22 +467,25 @@ public function test_another_entry_now_at_the_changed_dn_is_not_sent_in_its_plac $this->handle($this->cookieAt(0)); self::assertSame( - [], + [[SyncStateControl::STATE_DELETE, self::UUID_A]], $this->states(), ); } - public function test_a_changed_entry_gone_since_the_change_is_skipped(): void + public function test_a_changed_entry_gone_since_the_change_is_deleted_at_its_last_visible_dn(): void { $this->append(ChangeType::Modify, 'cn=a,dc=example,dc=com', self::UUID_A); - // No live entry registered, so nothing holds its entryUUID. $this->handle($this->cookieAt(0)); self::assertSame( - [], + [[SyncStateControl::STATE_DELETE, self::UUID_A]], $this->states(), ); + self::assertSame( + ['cn=a,dc=example,dc=com'], + $this->sentEntryDns(), + ); } public function test_a_delete_is_not_announced_when_the_consumer_could_not_see_the_entry(): void diff --git a/tests/unit/Sync/Provider/SyncPersistStreamerTest.php b/tests/unit/Sync/Provider/SyncPersistStreamerTest.php index cdbbdbf9..f260e536 100644 --- a/tests/unit/Sync/Provider/SyncPersistStreamerTest.php +++ b/tests/unit/Sync/Provider/SyncPersistStreamerTest.php @@ -108,6 +108,8 @@ final class SyncPersistStreamerTest extends TestCase private int $visibilityChecks = 0; + private bool $filterMatches = true; + protected function setUp(): void { $this->onSleep = static function (): void {}; @@ -156,7 +158,7 @@ protected function setUp(): void $filterEvaluator = $this->createMock(FilterEvaluatorInterface::class); $filterEvaluator ->method('evaluate') - ->willReturn(true); + ->willReturnCallback(fn(): bool => $this->filterMatches); $this->subject = new SyncPersistStreamer( entries: $this->entries, @@ -403,6 +405,67 @@ public function test_a_run_of_modifies_checks_visibility_once_in_the_window_and_ ); } + public function test_a_modify_out_of_the_filter_then_a_delete_sends_the_delete(): void + { + $this->filterMatches = false; + $this->append(ChangeType::Modify); + $this->appendDelete(self::ENTRY_DN); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, self::ENTRY_DN]], + $this->projected(), + ); + } + + public function test_a_lone_delete_out_of_the_filter_is_withheld(): void + { + $this->filterMatches = false; + $this->appendDelete(self::ENTRY_DN); + + self::assertSame( + [], + $this->projected(), + ); + } + + public function test_a_changed_entry_gone_when_fetched_is_deleted_at_its_last_visible_dn(): void + { + $this->append(ChangeType::Modify); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, self::ENTRY_DN]], + $this->projected(), + ); + } + + public function test_an_entry_moved_out_of_the_content_and_deleted_there_is_deleted_at_its_last_visible_dn(): void + { + $this->appendMove( + 'cn=a,dc=example,dc=com', + 'cn=a,dc=other,dc=com', + ); + $this->appendDelete('cn=a,dc=other,dc=com'); + + self::assertSame( + [[SyncStateControl::STATE_DELETE, 'cn=a,dc=example,dc=com']], + $this->projected(), + ); + } + + public function test_a_changed_entry_gone_when_fetched_that_was_never_visible_sends_nothing(): void + { + $this->appendFor( + ChangeType::Modify, + 'cn=a,ou=hidden,dc=example,dc=com', + self::ENTRY_UUID, + ); + + self::assertSame( + [], + $this->projected(), + ); + } + public function test_a_journal_trimmed_mid_projection_ends_the_stream_with_refresh_required(): void { foreach (['1', '2', '3'] as $n) { diff --git a/tests/unit/Sync/Provider/SyncResultProjectorTest.php b/tests/unit/Sync/Provider/SyncResultProjectorTest.php index 9cfa5cb0..aaad3b9a 100644 --- a/tests/unit/Sync/Provider/SyncResultProjectorTest.php +++ b/tests/unit/Sync/Provider/SyncResultProjectorTest.php @@ -391,6 +391,7 @@ public function test_a_visible_delete_with_a_pre_image_becomes_a_delete(): void $result = $this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), new Dn('cn=a,dc=example,dc=com'), + false, $this->request(), ); @@ -408,6 +409,7 @@ public function test_a_delete_is_named_by_its_last_visible_dn(): void $this->deletedDnOf($this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), new Dn('cn=a,ou=people,dc=example,dc=com'), + false, $this->request(), )), ); @@ -418,6 +420,7 @@ public function test_a_delete_without_a_visible_dn_is_null(): void self::assertNull($this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), null, + true, $this->request(), )); } @@ -429,6 +432,7 @@ public function test_a_delete_is_announced_when_the_filter_rests_on_linked_value $result = $this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), new Dn('cn=a,dc=example,dc=com'), + false, (new SearchRequest(Filters::equal('member', 'cn=a,dc=example,dc=com'))) ->base('dc=example,dc=com') ->useSubtreeScope(), @@ -448,15 +452,32 @@ public function test_a_delete_the_filter_excludes_is_still_withheld(): void self::assertNull($this->subject->projectDeleted( $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), new Dn('cn=a,dc=example,dc=com'), + false, $this->request(), )); } + public function test_a_delete_after_an_earlier_change_is_sent_whatever_its_pre_image(): void + { + $this->filterMatches = false; + + self::assertSame( + 'cn=a,dc=example,dc=com', + $this->deletedDnOf($this->subject->projectDeleted( + $this->deleteChange($this->entry('cn=a,dc=example,dc=com', self::UUID)), + new Dn('cn=a,dc=example,dc=com'), + true, + $this->request(), + )), + ); + } + public function test_a_delete_without_a_pre_image_is_null(): void { self::assertNull($this->subject->projectDeleted( $this->deleteChange(null), new Dn('cn=a,dc=example,dc=com'), + false, $this->request(), )); }