diff --git a/docs/Server/General-Usage.md b/docs/Server/General-Usage.md index 84b82f61..9c45d957 100644 --- a/docs/Server/General-Usage.md +++ b/docs/Server/General-Usage.md @@ -663,6 +663,9 @@ the full request pipeline. Supported changetypes: `add`, `delete`, `modify` (`ad Operational attributes (`entryUUID`, `createTimestamp`, etc.) are preserved. So `dump()` then `seed()` restores the entries exactly as they were, since seeding keeps the ones its source supplies rather than stamping new. +**Note**: Running dump on a directory that is not being written to produces the best results. A move during the dump can +skip or repeat part of a subtree. + ```php use FreeDSx\Ldap\LdapServer; use FreeDSx\Ldap\Ldif\Output\FileLdifOutput; diff --git a/docs/Server/Schema.md b/docs/Server/Schema.md index c17db724..b7b43cd2 100644 --- a/docs/Server/Schema.md +++ b/docs/Server/Schema.md @@ -52,7 +52,7 @@ Every `add`, `modify` and `modify DN` is checked before reaching storage: - Values conform to their attribute's syntax, and so do RDN values. - No attribute is given twice, and no value is repeated under an equivalent spelling. - The structural object class of an existing entry does not change. -- Naming attributes have an equality matching rule, and none of them is operational. +- Naming attributes have an equality matching rule, and none of them is operational or linked. Failures return one of these codes, with a diagnostic naming the attribute or class: @@ -65,7 +65,7 @@ Failures return one of these codes, with a diagnostic naming the attribute or cl | `constraintViolation` (19) | Several values on a single-valued attribute, a `NO-USER-MODIFICATION` attribute | | `attributeOrValueExists` (20) | An attribute given twice, a value repeated under an equivalent spelling | | `objectClassModsProhibited` (69) | Changing the structural object class | -| `namingViolation` (64) | A naming attribute with no equality matching rule, or an operational one | +| `namingViolation` (64) | A naming attribute with no equality matching rule, an operational or linked one | | `adminLimitExceeded` (11) | More values than the attribute's limit | ## Entry Requirements @@ -163,14 +163,14 @@ existing entry. | Writing `createTimestamp`, `modifyTimestamp`, `creatorsName` or `modifiersName` | Waived | Waived | | Writing `entryUUID` on Add | Waived | Waived | | Changing `entryUUID`, writing a derived attribute (`entryDN`, `hasSubordinates`, `subschemaSubentry`) or a back-link such as `memberOf` | Refused | Refused | -| Naming an entry by an operational attribute | Refused | Refused | +| Naming an entry by an operational or linked attribute | Refused | Refused | | Writing any other `NO-USER-MODIFICATION` attribute | Refused | Waived | | Required and allowed attributes, a structural object class, undefined types, undefined object classes, single-valued types, naming attributes | Refused | Waived | | Attribute syntax, the value limit, equivalent values, an attribute description given twice | Refused | Refused | Each violation is logged and the first that is not waived decides the result code. An entry's identity and the values the server derives for it are refused with `constraintViolation` in every mode, `Off` included: an entry holds exactly -one `entryUUID`, and a rename may not drop it. An entry named by an operational attribute is refused with +one `entryUUID`, and a rename may not drop it. An entry named by an operational or linked attribute is refused with `namingViolation` in every mode as well. A value a relaxed write supplies for one of these attributes is stored as given. A timestamp may not be in the future, @@ -359,6 +359,7 @@ leave such a class without an attribute it requires, so the server refuses to st | Reading a value back | The target's current stored DN, not the spelling that was written. | | Renaming the target | The new DN appears, with no write to the referring entry and no change to its `modifyTimestamp`. | | Deleting the target | The reference goes with it, again with no write to the referring entry. | +| Naming an entry by it | Refused with `namingViolation`. The value follows its target, and an RDN value may not change on its own. | | Value order | The order the references were created in, which RFC 4511 leaves unconstrained. | | An option-bearing form such as `member;lang-en` | Not linked, and kept on the entry as an ordinary value. | diff --git a/src/FreeDSx/Ldap/Container/Provider/DirectoryServerContainerProvider.php b/src/FreeDSx/Ldap/Container/Provider/DirectoryServerContainerProvider.php index c0ec02f5..8456a08c 100644 --- a/src/FreeDSx/Ldap/Container/Provider/DirectoryServerContainerProvider.php +++ b/src/FreeDSx/Ldap/Container/Provider/DirectoryServerContainerProvider.php @@ -387,9 +387,10 @@ private function makeWriteRequestReplayer(Container $container): WriteRequestRep private function makeDirectoryDumper(Container $container): DirectoryDumper { return new DirectoryDumper( - $container->get(ListEntryInterface::class), + $container->get(SubtreeEnumerator::class), $container->get(ReadEntryInterface::class)->namingContexts(), $container->get(FilterEvaluatorInterface::class), + $container->get(LinkedLeafWitness::class), ); } diff --git a/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Contract/PdoLinkWriteDialectInterface.php b/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Contract/PdoLinkWriteDialectInterface.php index 9c942b41..c4041383 100644 --- a/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Contract/PdoLinkWriteDialectInterface.php +++ b/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Contract/PdoLinkWriteDialectInterface.php @@ -34,6 +34,13 @@ public function queryLinkIdsForOwner(): string; */ public function queryResolveDns(int $count): string; + /** + * Which of $count resolved targets one owner links under an attribute. + * + * Parameters: [owner lc_dn, attr_name_lower, target_entry_id, ...] + */ + public function queryHeldLinkTargets(int $count): string; + /** * INSERT of $count links. * diff --git a/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Sql/PdoLinkWriteDialectTrait.php b/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Sql/PdoLinkWriteDialectTrait.php index e5d534f1..826847e6 100644 --- a/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Sql/PdoLinkWriteDialectTrait.php +++ b/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Dialect/Sql/PdoLinkWriteDialectTrait.php @@ -42,6 +42,20 @@ public function queryResolveDns(int $count): string SQL; } + public function queryHeldLinkTargets(int $count): string + { + $markers = SqlFilterUtility::markers($count); + + return <<resolve([$attribute => array_values($asked)]); - if ($asked === []) { + if ($resolved === []) { return []; } - $held = []; - $chunks = array_chunk( - array_keys($asked), - self::DNS_PER_STATEMENT, - ); - - foreach ($chunks as $chunk) { - $rows = $this->rowsOf( - $this->dialect->queryHeldLinkValues(count($chunk)), - [ - $owner->normalizedString(), - Attribute::normalizeName($attribute), - ...$chunk, - ], - ); - - foreach ($rows as $row) { - $held[] = $asked[$this->stringColumn($row['lc_dn'] ?? null)] ?? null; - } - } - - return array_values(array_filter($held)); + return array_values(array_intersect_key( + $asked, + $this->heldTargets( + $owner, + $attribute, + $resolved, + ), + )); } /** @@ -294,6 +284,45 @@ private function resolve(array $values): array return $ids; } + /** + * The resolved targets the owner links under the attribute. + * + * @param array $resolved + * + * @return array + */ + private function heldTargets( + Dn $owner, + string $attribute, + array $resolved, + ): array { + $held = []; + $chunks = array_chunk( + array_values($resolved), + self::DNS_PER_STATEMENT, + ); + + foreach ($chunks as $chunk) { + $rows = $this->rowsOf( + $this->dialect->queryHeldLinkTargets(count($chunk)), + [ + $owner->normalizedString(), + Attribute::normalizeName($attribute), + ...$chunk, + ], + ); + + foreach ($rows as $row) { + $held[$this->intColumn($row['target_entry_id'] ?? null)] = true; + } + } + + return array_filter( + $resolved, + static fn(int $id): bool => isset($held[$id]), + ); + } + /** * The links the entry now stands for, keyed so that two spellings of one target collapse to one row. * diff --git a/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Pdo/Writer/EntryWriter.php b/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Pdo/Writer/EntryWriter.php index 9f593308..8688fa5b 100644 --- a/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Pdo/Writer/EntryWriter.php +++ b/src/FreeDSx/Ldap/Server/Backend/Storage/Adapter/Pdo/Writer/EntryWriter.php @@ -180,24 +180,32 @@ public function renameSubtree( $to, $base->getDn()->toString(), ); + // Either clash names the new base, since a descendant's name can only be taken where the base exists. + $landsOn = $to->normalize(); - $this->translatingRefusal(function () use ($rename): void { - $this->connection->execute( - $this->dialect->queryRenameDescendants(), - $this->renameDescendantParams($rename), - ); - }); + $this->translatingRefusal( + function () use ($rename): void { + $this->connection->execute( + $this->dialect->queryRenameDescendants(), + $this->renameDescendantParams($rename), + ); + }, + $landsOn, + ); $this->translatingRefusal( - function () use ($rename, $to): void { - $this->connection->execute($this->dialect->queryRenameEntry(), [ - $rename->toDisplay, - $rename->lcTo, - $to->normalize()->getParent()?->toString() ?? '', - $rename->lcFrom, - ]); + function () use ($rename, $landsOn): void { + $this->connection->execute( + $this->dialect->queryRenameEntry(), + [ + $rename->toDisplay, + $rename->lcTo, + $landsOn->getParent()?->toString() ?? '', + $rename->lcFrom, + ], + ); }, - $to->normalize(), + $landsOn, ); // A moved subtree can put many DNs in place at once, so nothing narrows which parked values may settle. @@ -329,7 +337,7 @@ private function updateRow( * Runs a write, turning the driver failures the dialect recognises into the directory conditions they mean. * * @param Closure(): void $write - * @param ?Dn $landsOn The DN written, where the write lands on exactly one. + * @param ?Dn $landsOn The DN the write lands on, or under. * @throws EntryAlreadyExistsException * @throws EntryUuidTakenException * @throws DnTooLongException diff --git a/src/FreeDSx/Ldap/Server/Backend/Storage/Directory/SubtreeEnumerator.php b/src/FreeDSx/Ldap/Server/Backend/Storage/Directory/SubtreeEnumerator.php index b773ac21..b0b99620 100644 --- a/src/FreeDSx/Ldap/Server/Backend/Storage/Directory/SubtreeEnumerator.php +++ b/src/FreeDSx/Ldap/Server/Backend/Storage/Directory/SubtreeEnumerator.php @@ -50,6 +50,24 @@ public function leavesFirst(Dn $base): Generator yield from $this->childrenFirst($base->normalizedString()); } + /** + * The base and everything beneath it, each entry before its children, streamed with every value it holds. + * + * @return Generator + */ + public function parentsFirst(Dn $base): Generator + { + $bases = $this->lister->entriesAt( + [$base], + EntryProjection::unbounded(), + ); + + foreach ($bases as $entry) { + yield $entry; + yield from $this->childrenAfter($entry->getDn()); + } + } + /** * Children of the entries that are not themselves among them, which removing the entries would strand. * @@ -121,6 +139,34 @@ private function childrenFirst(string $dn): Generator yield $dn; } + /** + * Streamed one level at a time, which holds about one batch per level, as nothing here writes against the result. + * + * @return Generator + */ + private function childrenAfter(Dn $parent): Generator + { + $children = $this->lister->list(StorageListOptions::matchAll( + $parent, + subtree: false, + projection: new EntryProjection( + linkCap: null, + withHasSubordinates: true, + ), + )); + + foreach ($children->entries() as $child) { + $hasChildren = $child->get(AttributeTypeOid::NAME_HAS_SUBORDINATES)?->getValues() === ['TRUE']; + $child->reset(AttributeTypeOid::NAME_HAS_SUBORDINATES); + + yield $child; + + if ($hasChildren) { + yield from $this->childrenAfter($child->getDn()); + } + } + } + /** * @return Generator each child with whether it has children of its own */ diff --git a/src/FreeDSx/Ldap/Server/Backend/Storage/Export/DirectoryDumper.php b/src/FreeDSx/Ldap/Server/Backend/Storage/Export/DirectoryDumper.php index 454cb0f2..9989d238 100644 --- a/src/FreeDSx/Ldap/Server/Backend/Storage/Export/DirectoryDumper.php +++ b/src/FreeDSx/Ldap/Server/Backend/Storage/Export/DirectoryDumper.php @@ -14,15 +14,13 @@ namespace FreeDSx\Ldap\Server\Backend\Storage\Export; use FreeDSx\Ldap\Entry\Dn; +use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Ldif\LdifWriter; use FreeDSx\Ldap\Operations; -use FreeDSx\Ldap\Search\Filter\AndFilter; use FreeDSx\Ldap\Search\Filter\FilterInterface; -use FreeDSx\Ldap\Server\Backend\Storage\Contract\ListEntryInterface; +use FreeDSx\Ldap\Server\Backend\Storage\Directory\SubtreeEnumerator; use FreeDSx\Ldap\Server\Backend\Storage\Filter\FilterEvaluatorInterface; -use FreeDSx\Ldap\Server\Backend\Storage\Search\EntryProjection; -use FreeDSx\Ldap\Server\Backend\Storage\Search\Options\ListScope; -use FreeDSx\Ldap\Server\Backend\Storage\StorageListOptions; +use FreeDSx\Ldap\Server\Backend\Storage\Filter\LinkedLeafWitness; use Generator; /** @@ -36,9 +34,10 @@ * @param list $namingContexts dump roots when DumpOptions::baseDn is not set */ public function __construct( - private ListEntryInterface $storage, + private SubtreeEnumerator $subtree, private array $namingContexts, private FilterEvaluatorInterface $filterEvaluator, + private LinkedLeafWitness $linkedLeaves, private LdifWriter $writer = new LdifWriter(), ) {} @@ -82,7 +81,7 @@ private function resolveBases(DumpOptions $options): array } /** - * Stream the entries from a naming context to LDIF. + * Stream the entries from a naming context to LDIF, each parent before its children so the output seeds back. * * @return Generator */ @@ -91,24 +90,28 @@ private function streamNamingContext( ?FilterInterface $filter, ): Generator { // @todo An export needs every value, but each is held at once; stream linked values in chunks as they are written. - $listOptions = new StorageListOptions( - scope: new ListScope( - baseDn: $base, - subtree: true, - ), - filter: $filter ?? new AndFilter(), - projection: EntryProjection::unbounded(), - ); - $stream = $this->storage->list($listOptions); - - foreach ($stream->entries() as $entry) { - if (!$stream->isPreFiltered && $filter !== null) { - if (!$this->filterEvaluator->evaluate($entry, $filter)) { - continue; - } + foreach ($this->subtree->parentsFirst($base) as $entry) { + if ($filter !== null && !$this->matches($entry, $filter)) { + continue; } yield $this->writer->writeOne(Operations::add($entry)); } } + + /** + * Judged with the back-links the filter names, which the walk does not read. + */ + private function matches( + Entry $entry, + FilterInterface $filter, + ): bool { + return $this->filterEvaluator->evaluate( + $this->linkedLeaves->witness( + $entry, + $filter, + ), + $filter, + ); + } } diff --git a/src/FreeDSx/Ldap/Server/Backend/Write/Schema/SchemaViolationGate.php b/src/FreeDSx/Ldap/Server/Backend/Write/Schema/SchemaViolationGate.php index e27a9efb..fbe9df44 100644 --- a/src/FreeDSx/Ldap/Server/Backend/Write/Schema/SchemaViolationGate.php +++ b/src/FreeDSx/Ldap/Server/Backend/Write/Schema/SchemaViolationGate.php @@ -158,7 +158,7 @@ private function assertTypeStorable(string $type): void } /** - * Only a user attribute can name an entry, whatever the mode allows. + * Only a user attribute held as written can name an entry, whatever the mode allows. * * @throws OperationException */ @@ -171,18 +171,52 @@ private function assertNamedByUserAttributes(SchemaWrite $write): void } foreach ($dn->getRdn()->getAll() as $component) { - if (!$this->schema->isOperational($component->getName())) { - continue; - } + $this->assertCanName($component->getName()); + } + } - throw new OperationException( - sprintf( - 'The operational attribute "%s" cannot name an entry.', - $component->getName(), - ), - ResultCode::NAMING_VIOLATION, + /** + * @throws OperationException + */ + private function assertCanName(string $name): void + { + if ($this->schema->isOperational($name)) { + self::refuseNaming( + 'operational', + $name, ); } + if ($this->isLinkedType($name)) { + self::refuseNaming( + 'linked', + $name, + ); + } + } + + /** + * A linked value follows the entry it names, which would change the RDN without a rename. + */ + private function isLinkedType(string $name): bool + { + return $this->schema->getAttributeType(Attribute::normalizeName($name))?->isLinked() === true; + } + + /** + * @throws OperationException + */ + private static function refuseNaming( + string $kind, + string $name, + ): never { + throw new OperationException( + sprintf( + 'The %s attribute "%s" cannot name an entry.', + $kind, + $name, + ), + ResultCode::NAMING_VIOLATION, + ); } /** diff --git a/tests/integration/Ldif/LdapDumpServerTest.php b/tests/integration/Ldif/LdapDumpServerTest.php index 6ce4d354..a460b482 100644 --- a/tests/integration/Ldif/LdapDumpServerTest.php +++ b/tests/integration/Ldif/LdapDumpServerTest.php @@ -28,6 +28,8 @@ final class LdapDumpServerTest extends ServerTestCase { private const SEED_LDIF = __DIR__ . '/../../resources/seed/seed-test.ldif'; + private const CHANGES_LDIF = __DIR__ . '/../../resources/changes/dump-move-test.ldif'; + private static string $dumpPath; public static function setUpBeforeClass(): void @@ -50,6 +52,7 @@ public static function setUpBeforeClass(): void [ '--storage=sqlite', '--seed=' . self::SEED_LDIF, + '--changes=' . self::CHANGES_LDIF, '--dump=' . self::$dumpPath, ], ); @@ -134,6 +137,24 @@ public function test_a_fresh_server_seeded_from_the_dump_reconstructs_the_direct ); } + public function test_an_entry_moved_under_a_newer_parent_seeds_back_under_it(): void + { + $options = TestServerOptions::defaults(); + $container = Container::forServer($options); + (new LdapServer( + $options, + $container, + ))->seed(new StringLdifLoader((string) file_get_contents(self::$dumpPath))); + + self::assertSame( + ['mover'], + $container->get(ReadEntryInterface::class) + ->find(new Dn('cn=mover,ou=later,dc=foo,dc=bar')) + ?->get('cn') + ?->getValues(), + ); + } + public function test_the_dump_preserves_operational_attributes_for_round_trip(): void { $parsed = LdifChanges::fromLoader(new FileLdifLoader(self::$dumpPath)); diff --git a/tests/integration/Storage/Concern/LinkedAttributeTestsTrait.php b/tests/integration/Storage/Concern/LinkedAttributeTestsTrait.php index 794e5757..9fb27099 100644 --- a/tests/integration/Storage/Concern/LinkedAttributeTestsTrait.php +++ b/tests/integration/Storage/Concern/LinkedAttributeTestsTrait.php @@ -577,43 +577,34 @@ public function testARangeThatCannotBeServedIsRefused(): void $this->ldapClient()->read($group, ['member;range=9-2']); } - public function testARenameWhoseNewRdnIsALinkedValueLinksIt(): void + public function testAnAddNamedByALinkedAttributeIsRefused(): void { - $person = $this->seedPerson('link-rdn-new'); + $this->authenticateAdmin(); - $this->asAdmin(fn(LdapClient $admin) => $admin->rename( - $person, - 'seeAlso=cn=admin\,dc=foo\,dc=bar', - false, - )); + $this->expectOperationCode(ResultCode::NAMING_VIOLATION); - self::assertSame( - [self::LINKED_ADMIN], - $this->seeAlsoOf('seeAlso=cn=admin\,dc=foo\,dc=bar,dc=foo,dc=bar'), - ); + $this->ldapClient()->create(Entry::fromArray( + 'seeAlso=cn=user\,dc=foo\,dc=bar,dc=foo,dc=bar', + [ + 'cn' => 'link-rdn-add', + 'objectClass' => 'person', + 'seeAlso' => self::LINKED_USER, + 'sn' => 'link-rdn-add', + ], + )); } - public function testARenameDroppingAnRdnThatWasALinkedValueUnlinksIt(): void + public function testARenameOntoALinkedAttributeIsRefused(): void { - $this->asAdmin(function (LdapClient $admin): void { - $admin->create(Entry::fromArray( - 'seeAlso=cn=user\,dc=foo\,dc=bar,dc=foo,dc=bar', - [ - 'cn' => 'link-rdn-old', - 'objectClass' => 'person', - 'seeAlso' => self::LINKED_USER, - 'sn' => 'link-rdn-old', - ], - )); - $admin->rename( - 'seeAlso=cn=user\,dc=foo\,dc=bar,dc=foo,dc=bar', - 'cn=link-rdn-old', - ); - }); + $person = $this->seedPerson('link-rdn-new'); + $this->authenticateAdmin(); - self::assertSame( - [], - $this->seeAlsoOf('cn=link-rdn-old,dc=foo,dc=bar'), + $this->expectOperationCode(ResultCode::NAMING_VIOLATION); + + $this->ldapClient()->rename( + $person, + 'seeAlso=cn=admin\,dc=foo\,dc=bar', + false, ); } diff --git a/tests/integration/Storage/Pdo/EntryLinkWriterTest.php b/tests/integration/Storage/Pdo/EntryLinkWriterTest.php index 10a67596..ba3f4b38 100644 --- a/tests/integration/Storage/Pdo/EntryLinkWriterTest.php +++ b/tests/integration/Storage/Pdo/EntryLinkWriterTest.php @@ -17,6 +17,7 @@ use FreeDSx\Ldap\Entry\Dn; use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Server\Backend\Storage\Adapter\Pdo\Writer\EntryLinkWriter; +use FreeDSx\Ldap\Server\Backend\Storage\Contract\WriteEntryInterface; use FreeDSx\Ldap\Server\Backend\Storage\Import\LdapImporter; use PHPUnit\Framework\TestCase; use Tests\Support\FreeDSx\Ldap\Server\Configuration\TestServerOptions; @@ -31,10 +32,13 @@ final class EntryLinkWriterTest extends TestCase private EntryLinkWriter $subject; + private WriteEntryInterface $storage; + protected function setUp(): void { $container = Container::forServer(TestServerOptions::sqlite()); $this->subject = $container->get(EntryLinkWriter::class); + $this->storage = $container->get(WriteEntryInterface::class); $container->get(LdapImporter::class)->importEntries([ Entry::fromArray( @@ -80,6 +84,30 @@ public function test_a_held_value_is_found_past_the_first_statement_of_values(): ); } + public function test_a_renamed_target_is_held_under_its_new_dn_and_not_its_old_one(): void + { + $this->storage->renameSubtree( + new Dn(self::BOB), + new Dn('cn=Robert,' . self::BASE), + ); + + self::assertSame( + [['cn=Robert,' . self::BASE], []], + [ + $this->subject->heldLinkValues( + new Dn(self::ADMINS), + 'member', + ['cn=Robert,' . self::BASE], + ), + $this->subject->heldLinkValues( + new Dn(self::ADMINS), + 'member', + [self::BOB], + ), + ], + ); + } + public function test_a_value_the_owner_does_not_link_is_not_held(): void { self::assertSame( diff --git a/tests/resources/changes/dump-move-test.ldif b/tests/resources/changes/dump-move-test.ldif new file mode 100644 index 00000000..a2f3963b --- /dev/null +++ b/tests/resources/changes/dump-move-test.ldif @@ -0,0 +1,19 @@ +version: 1 + +# An entry created before the container it is then moved under, which leaves it stored ahead of its new parent. +dn: cn=mover,dc=foo,dc=bar +changetype: add +objectClass: inetOrgPerson +cn: mover +sn: Mover + +dn: ou=later,dc=foo,dc=bar +changetype: add +objectClass: organizationalUnit +ou: later + +dn: cn=mover,dc=foo,dc=bar +changetype: modrdn +newrdn: cn=mover +deleteoldrdn: 0 +newsuperior: ou=later,dc=foo,dc=bar diff --git a/tests/support/Storage/SubtreeRenameStorageContractTests.php b/tests/support/Storage/SubtreeRenameStorageContractTests.php index 4bfa5945..cdaba1c2 100644 --- a/tests/support/Storage/SubtreeRenameStorageContractTests.php +++ b/tests/support/Storage/SubtreeRenameStorageContractTests.php @@ -182,6 +182,39 @@ public function test_renaming_a_subtree_onto_an_occupied_dn_is_refused_and_moves self::assertNotNull($this->storedEntry($container, 'cn=carol,ou=groups,dc=foo,dc=bar')); } + public function test_renaming_a_subtree_onto_one_holding_the_same_child_is_refused_and_moves_nothing(): void + { + $container = $this->makeStorageContainer( + new Entry(new Dn('dc=foo,dc=bar')), + new Entry(new Dn('ou=src,dc=foo,dc=bar')), + new Entry(new Dn('cn=x,ou=src,dc=foo,dc=bar')), + new Entry(new Dn('ou=dest,dc=foo,dc=bar')), + new Entry(new Dn('cn=x,ou=dest,dc=foo,dc=bar')), + ); + + try { + $container->get(WriteEntryInterface::class)->renameSubtree( + new Dn('ou=src,dc=foo,dc=bar'), + new Dn('ou=dest,dc=foo,dc=bar'), + ); + self::fail('Renaming onto a subtree holding the same child should have been refused.'); + } catch (EntryAlreadyExistsException $e) { + self::assertSame( + [ResultCode::ENTRY_ALREADY_EXISTS, 'Entry already exists: ou=dest,dc=foo,dc=bar'], + [$e->getCode(), $e->getMessage()], + ); + } + + self::assertNotNull($this->storedEntry( + $container, + 'cn=x,ou=src,dc=foo,dc=bar', + )); + self::assertNotNull($this->storedEntry( + $container, + 'cn=x,ou=dest,dc=foo,dc=bar', + )); + } + /** * @param Entry ...$entries Seeded, each given an entryUUID, before the contract runs. * @return Container the graph holding the seeded entries, which every storage contract resolves from diff --git a/tests/support/Storage/SubtreeWalkStorageContractTests.php b/tests/support/Storage/SubtreeWalkStorageContractTests.php index 9b6a77b1..cc48b4b6 100644 --- a/tests/support/Storage/SubtreeWalkStorageContractTests.php +++ b/tests/support/Storage/SubtreeWalkStorageContractTests.php @@ -17,8 +17,10 @@ use FreeDSx\Ldap\Entry\Attribute; use FreeDSx\Ldap\Entry\Dn; use FreeDSx\Ldap\Entry\Entry; +use FreeDSx\Ldap\Schema\Definition\AttributeTypeOid; use FreeDSx\Ldap\Server\Backend\Storage\Directory\SubtreeEnumerator; +use function array_filter; use function array_map; use function array_search; use function count; @@ -138,6 +140,129 @@ public function test_the_walk_does_not_reach_an_entry_stored_under_a_missing_par ); } + public function test_the_parents_first_walk_yields_the_base_first_and_each_entry_after_its_parent(): void + { + $walked = $this->dnsOf($this->walkParentsFirst( + $this->walkContainer(), + 'ou=people,dc=foo,dc=bar', + )); + + self::assertSame( + 'ou=people,dc=foo,dc=bar', + $walked[0], + ); + self::assertLessThan( + $this->positionOf( + $walked, + 'cn=laptop,cn=alice,ou=people,dc=foo,dc=bar', + ), + $this->positionOf( + $walked, + 'cn=alice,ou=people,dc=foo,dc=bar', + ), + ); + } + + public function test_the_parents_first_walk_yields_each_entry_beneath_the_base_once(): void + { + $walked = $this->dnsOf($this->walkParentsFirst( + $this->walkContainer(), + 'ou=people,dc=foo,dc=bar', + )); + sort($walked); + + self::assertSame( + [ + 'cn=alice,ou=people,dc=foo,dc=bar', + 'cn=bob,ou=people,dc=foo,dc=bar', + 'cn=laptop,cn=alice,ou=people,dc=foo,dc=bar', + 'cn=link,ou=people,dc=foo,dc=bar', + 'cn=policy,ou=people,dc=foo,dc=bar', + 'ou=people,dc=foo,dc=bar', + ], + $walked, + ); + } + + public function test_the_parents_first_walk_finishes_a_container_after_descending_into_its_first_child(): void + { + $children = array_map( + static fn(int $i): Entry => new Entry(new Dn("cn=c$i,ou=wide,dc=foo,dc=bar")), + range( + 0, + 1000, + ), + ); + $container = $this->makeStorageContainer(...[ + new Entry(new Dn('dc=foo,dc=bar')), + new Entry(new Dn('ou=Wide,dc=foo,dc=bar')), + ...$children, + new Entry(new Dn('cn=Below,cn=c0,ou=wide,dc=foo,dc=bar')), + ]); + $walked = $this->dnsOf($this->walkParentsFirst( + $container, + 'ou=wide,dc=foo,dc=bar', + )); + + self::assertCount( + 1003, + $walked, + ); + self::assertLessThan( + $this->positionOf( + $walked, + 'cn=below,cn=c0,ou=wide,dc=foo,dc=bar', + ), + $this->positionOf( + $walked, + 'cn=c0,ou=wide,dc=foo,dc=bar', + ), + ); + } + + public function test_the_parents_first_walk_does_not_reach_an_entry_stored_under_a_missing_parent(): void + { + $container = $this->makeStorageContainer( + new Entry(new Dn('dc=foo,dc=bar')), + new Entry(new Dn('cn=Orphan,ou=Missing,dc=foo,dc=bar')), + ); + + self::assertSame( + ['dc=foo,dc=bar'], + $this->dnsOf($this->walkParentsFirst( + $container, + 'dc=foo,dc=bar', + )), + ); + } + + public function test_the_parents_first_walk_of_a_missing_base_yields_nothing(): void + { + self::assertSame( + [], + $this->walkParentsFirst( + $this->walkContainer(), + 'ou=missing,dc=foo,dc=bar', + ), + ); + } + + public function test_no_entry_the_parents_first_walk_yields_carries_the_child_flag(): void + { + $flagged = array_filter( + $this->walkParentsFirst( + $this->walkContainer(), + 'dc=foo,dc=bar', + ), + static fn(Entry $entry): bool => $entry->has(AttributeTypeOid::NAME_HAS_SUBORDINATES), + ); + + self::assertSame( + [], + $flagged, + ); + } + public function test_children_outside_are_the_children_not_among_the_entries_given(): void { $subtree = $this->walkContainer()->get(SubtreeEnumerator::class); @@ -214,6 +339,32 @@ private function walk( ); } + /** + * @return list + */ + private function walkParentsFirst( + Container $container, + string $base, + ): array { + return iterator_to_array( + $container->get(SubtreeEnumerator::class)->parentsFirst(new Dn($base)), + false, + ); + } + + /** + * @param list $entries + * + * @return list + */ + private function dnsOf(array $entries): array + { + return array_map( + static fn(Entry $entry): string => $entry->getDn()->normalizedString(), + $entries, + ); + } + /** * @param list $walked */ diff --git a/tests/unit/LdapServerTest.php b/tests/unit/LdapServerTest.php index 698ce000..4e1c15ee 100644 --- a/tests/unit/LdapServerTest.php +++ b/tests/unit/LdapServerTest.php @@ -68,6 +68,20 @@ class LdapServerTest extends TestCase sn: Bar LDIF; + private const MOVE_UNDER_NEWER_PARENT_LDIF = <<<'LDIF' + dn: ou=later,dc=example,dc=com + changetype: add + objectClass: top + objectClass: organizationalUnit + ou: later + + dn: cn=foo,dc=example,dc=com + changetype: modrdn + newrdn: cn=foo + deleteoldrdn: 0 + newsuperior: ou=later,dc=example,dc=com + LDIF; + private const SUBTREE_LDIF = <<<'LDIF' dn: ou=people,dc=example,dc=com objectClass: top @@ -632,6 +646,30 @@ public function test_dump_seed_round_trip_preserves_entryUUID_and_create_timesta ); } + public function test_a_dump_seeds_back_an_entry_moved_under_a_newer_parent(): void + { + $this->subject->seed(new StringLdifLoader(self::SEED_LDIF)); + $this->subject->applyChanges(new StringLdifLoader(self::MOVE_UNDER_NEWER_PARENT_LDIF)); + + $output = new StringLdifOutput(); + $this->subject->dump( + $output, + (new DumpOptions())->setBaseDn(new Dn('dc=example,dc=com')), + ); + $restoreOptions = TestServerOptions::defaults(); + $restoreContainer = Container::forServer($restoreOptions); + (new LdapServer( + $restoreOptions, + $restoreContainer, + ))->seed(new StringLdifLoader($output->getLdif())); + + self::assertNotNull( + $restoreContainer + ->get(ReadEntryInterface::class) + ->find(new Dn('cn=foo,ou=later,dc=example,dc=com')), + ); + } + public function test_it_should_apply_the_dump_options_filter(): void { $this->subject->seed(new StringLdifLoader(self::SEED_LDIF)); diff --git a/tests/unit/Server/Backend/Storage/Export/DirectoryDumperTest.php b/tests/unit/Server/Backend/Storage/Export/DirectoryDumperTest.php index 3495807a..7c004f9c 100644 --- a/tests/unit/Server/Backend/Storage/Export/DirectoryDumperTest.php +++ b/tests/unit/Server/Backend/Storage/Export/DirectoryDumperTest.php @@ -18,16 +18,15 @@ use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Ldif\LdifOutputOptions; use FreeDSx\Ldap\Ldif\LdifWriter; -use FreeDSx\Ldap\Search\Filter\FilterInterface; use FreeDSx\Ldap\Search\Filters; use FreeDSx\Ldap\Server\Backend\Storage\Adapter\InMemoryStorage; -use FreeDSx\Ldap\Server\Backend\Storage\Contract\ListEntryInterface; -use FreeDSx\Ldap\Server\Backend\Storage\EntryStream; +use FreeDSx\Ldap\Server\Backend\Storage\Capability\LinkedValueLookupInterface; +use FreeDSx\Ldap\Server\Backend\Storage\Directory\SubtreeEnumerator; use FreeDSx\Ldap\Server\Backend\Storage\Export\DirectoryDumper; use FreeDSx\Ldap\Server\Backend\Storage\Export\DumpOptions; use FreeDSx\Ldap\Server\Backend\Storage\Filter\FilterEvaluatorInterface; -use FreeDSx\Ldap\Server\Backend\Storage\StorageListOptions; -use Generator; +use FreeDSx\Ldap\Server\Backend\Storage\Filter\LinkedLeafWitness; +use FreeDSx\Ldap\Server\Backend\Storage\Schema\LinkedAttributes; use PHPUnit\Framework\TestCase; use Tests\Support\FreeDSx\Ldap\ServerContainerTrait; use Tests\Support\FreeDSx\Ldap\Storage\EntryFixture; @@ -144,102 +143,67 @@ public function test_it_restricts_to_the_options_base_dn_when_set(): void ); } - public function test_it_re_evaluates_the_filter_when_the_stream_is_not_preFiltered(): void + public function test_a_filter_writes_only_the_entries_it_matches(): void { - $alice = Entry::create( - 'cn=alice,dc=foo,dc=bar', - ['cn' => 'alice'], - ); - $bob = Entry::create( - 'cn=bob,dc=foo,dc=bar', - ['cn' => 'bob'], + $dumper = $this->makeDumper( + writer: new LdifWriter((new LdifOutputOptions())->setIncludeVersion(false)), ); - $storage = $this->createMock(ListEntryInterface::class); - $storage->method('list')->willReturn(EntryStream::of( - entries: (function () use ($alice, $bob): Generator { - yield $alice; - yield $bob; - - return null; - })(), - isPreFiltered: false, - )); - $evaluator = $this->createMock(FilterEvaluatorInterface::class); - $evaluator->method('evaluate')->willReturnCallback( - fn(Entry $entry, FilterInterface $filter): bool - => $entry->getDn()->toString() === 'cn=alice,dc=foo,dc=bar', + + self::assertSame( + ['cn=alice,dc=foo,dc=bar'], + $this->dnsIn($dumper->dump((new DumpOptions())->setFilter(Filters::equal( + 'cn', + 'alice', + )))), ); + } + public function test_it_writes_a_parent_before_a_child_stored_ahead_of_it(): void + { $dumper = $this->makeDumper( - $storage, - $evaluator, + $this->storageWithAMovedEntry(), new LdifWriter((new LdifOutputOptions())->setIncludeVersion(false)), ); - $ldif = implode('', iterator_to_array( - $dumper->dump((new DumpOptions())->setFilter(Filters::equal('cn', 'alice'))), - false, - )); - - self::assertStringContainsString( - 'cn=alice,dc=foo,dc=bar', - $ldif, - ); - self::assertStringNotContainsString( - 'cn=bob,dc=foo,dc=bar', - $ldif, + self::assertSame( + [ + 'dc=foo,dc=bar', + 'ou=later,dc=foo,dc=bar', + 'cn=mover,ou=later,dc=foo,dc=bar', + ], + $this->dnsIn($dumper->dump(new DumpOptions())), ); } - public function test_it_does_not_re_evaluate_the_filter_when_the_stream_is_preFiltered(): void + public function test_a_filter_still_reaches_a_match_below_a_container_it_does_not_match(): void { - $alice = Entry::create('cn=alice,dc=foo,dc=bar', ['cn' => 'alice']); - $storage = $this->createMock(ListEntryInterface::class); - $storage->method('list')->willReturn(EntryStream::of( - entries: (function () use ($alice): Generator { - yield $alice; - - return null; - })(), - isPreFiltered: true, - )); - $evaluator = $this->createMock(FilterEvaluatorInterface::class); - $evaluator->expects(self::never())->method('evaluate'); - $dumper = $this->makeDumper( - $storage, - $evaluator, + $this->storageWithAMovedEntry(), new LdifWriter((new LdifOutputOptions())->setIncludeVersion(false)), ); - iterator_to_array( - $dumper->dump((new DumpOptions())->setFilter(Filters::equal('cn', 'alice'))), - false, + self::assertSame( + ['cn=mover,ou=later,dc=foo,dc=bar'], + $this->dnsIn($dumper->dump((new DumpOptions())->setFilter(Filters::equal( + 'cn', + 'mover', + )))), ); } - public function test_it_passes_match_all_to_storage_when_no_filter_is_set(): void + public function test_a_filter_on_a_back_link_writes_the_entries_it_names(): void { - $storage = $this->createMock(ListEntryInterface::class); - $storage->expects(self::once()) - ->method('list') - ->with(self::callback( - fn(StorageListOptions $opts): bool - => $opts->scope->subtree === true && $opts->scope->baseDn->toString() === 'dc=foo,dc=bar', - )) - ->willReturn(EntryStream::of( - entries: (function (): Generator { - yield from []; - - return null; - })(), - )); - - $dumper = $this->makeDumper($storage); - - iterator_to_array( - $dumper->dump(new DumpOptions()), - false, + $dumper = $this->makeDumper( + $this->storageWithAGroup(), + new LdifWriter((new LdifOutputOptions())->setIncludeVersion(false)), + ); + + self::assertSame( + ['cn=alice,dc=foo,dc=bar'], + $this->dnsIn($dumper->dump((new DumpOptions())->setFilter(Filters::equal( + 'memberOf', + 'cn=staff,dc=foo,dc=bar', + )))), ); } @@ -247,18 +211,119 @@ public function test_it_passes_match_all_to_storage_when_no_filter_is_set(): voi * The subject, with only the collaborator a test cares about supplied. */ private function makeDumper( - ?ListEntryInterface $storage = null, - ?FilterEvaluatorInterface $filterEvaluator = null, + ?InMemoryStorage $storage = null, ?LdifWriter $writer = null, ): DirectoryDumper { + $storage ??= $this->storageWithEntries(); + return new DirectoryDumper( - $storage ?? $this->storageWithEntries(), + new SubtreeEnumerator($storage), [new Dn('dc=foo,dc=bar')], - $filterEvaluator ?? $this->fromContainer(FilterEvaluatorInterface::class), + $this->fromContainer(FilterEvaluatorInterface::class), + $this->fromContainer( + LinkedLeafWitness::class, + [LinkedValueLookupInterface::class => $storage], + ), $writer ?? new LdifWriter(), ); } + /** + * Holds the child ahead of the parent it was moved under, as storage order leaves a move. + */ + private function storageWithAMovedEntry(): InMemoryStorage + { + return EntryFixture::inMemoryStorage( + new Entry( + new Dn('dc=foo,dc=bar'), + new Attribute( + 'dc', + 'foo', + ), + ), + new Entry( + new Dn('cn=mover,ou=later,dc=foo,dc=bar'), + new Attribute( + 'cn', + 'mover', + ), + ), + new Entry( + new Dn('ou=later,dc=foo,dc=bar'), + new Attribute( + 'ou', + 'later', + ), + ), + ); + } + + /** + * Keeps member values as links, which is where a back-link is read from. + */ + private function storageWithAGroup(): InMemoryStorage + { + return new InMemoryStorage( + EntryFixture::allWithUuid( + new Entry( + new Dn('dc=foo,dc=bar'), + new Attribute( + 'dc', + 'foo', + ), + ), + new Entry( + new Dn('cn=alice,dc=foo,dc=bar'), + new Attribute( + 'cn', + 'alice', + ), + ), + new Entry( + new Dn('cn=bob,dc=foo,dc=bar'), + new Attribute( + 'cn', + 'bob', + ), + ), + new Entry( + new Dn('cn=staff,dc=foo,dc=bar'), + new Attribute( + 'cn', + 'staff', + ), + new Attribute( + 'member', + 'cn=alice,dc=foo,dc=bar', + ), + ), + ), + linkedAttributes: $this->fromContainer(LinkedAttributes::class), + ); + } + + /** + * @param iterable $chunks + * + * @return list + */ + private function dnsIn(iterable $chunks): array + { + preg_match_all( + '/^dn: (.+)$/m', + implode( + '', + iterator_to_array( + $chunks, + false, + ), + ), + $matches, + ); + + return $matches[1]; + } + private function storageWithEntries(): InMemoryStorage { return EntryFixture::inMemoryStorage( diff --git a/tests/unit/Server/Backend/Storage/Import/LdapImporterTest.php b/tests/unit/Server/Backend/Storage/Import/LdapImporterTest.php index 22c49504..15651e3d 100644 --- a/tests/unit/Server/Backend/Storage/Import/LdapImporterTest.php +++ b/tests/unit/Server/Backend/Storage/Import/LdapImporterTest.php @@ -390,6 +390,38 @@ public function test_importEntries_ignoreValidation_still_refuses_an_entry_named ); } + public function test_importEntries_ignoreValidation_still_refuses_an_entry_named_by_a_linked_attribute(): void + { + self::expectException(OperationException::class); + self::expectExceptionCode(ResultCode::NAMING_VIOLATION); + + $this->importerFor( + $this->storage, + TestServerOptions::validatedCore(SchemaValidationMode::Strict), + )->importEntries( + entries: [ + $this->domain(), + new Entry( + new Dn('seeAlso=dc=example\,dc=com,dc=example,dc=com'), + new Attribute( + 'objectClass', + 'device', + 'extensibleObject', + ), + new Attribute( + 'cn', + 'Named', + ), + new Attribute( + 'seeAlso', + 'dc=example,dc=com', + ), + ), + ], + ignoreValidation: true, + ); + } + public function test_importEntries_ignoreValidation_still_refuses_an_entry_holding_two_entry_uuids(): void { self::expectException(OperationException::class); diff --git a/tests/unit/Server/Backend/Write/Operation/AddEntryHandlerTest.php b/tests/unit/Server/Backend/Write/Operation/AddEntryHandlerTest.php index ddc96d36..e0f26e23 100644 --- a/tests/unit/Server/Backend/Write/Operation/AddEntryHandlerTest.php +++ b/tests/unit/Server/Backend/Write/Operation/AddEntryHandlerTest.php @@ -543,6 +543,46 @@ public static function operationalRdnProvider(): iterable } } + #[DataProvider('linkedRdnProvider')] + public function test_an_add_named_by_a_linked_attribute_is_refused_in_every_validation_mode( + SchemaValidationMode $mode, + string $rdn, + ): void { + $this->validatedGraph($mode); + + self::expectException(OperationException::class); + self::expectExceptionCode(ResultCode::NAMING_VIOLATION); + + $this->adds()->handle( + new AddCommand(Entry::fromArray( + $rdn . ',dc=example,dc=com', + [ + 'objectClass' => ['device', 'extensibleObject'], + 'cn' => ['Named'], + 'seeAlso' => ['dc=example,dc=com'], + ], + )), + $this->context(), + ); + } + + /** + * @return iterable + */ + public static function linkedRdnProvider(): iterable + { + foreach (SchemaValidationMode::cases() as $mode) { + yield $mode->name . ' seeAlso' => [ + $mode, + 'seeAlso=dc=example\,dc=com', + ]; + yield $mode->name . ' seeAlso by OID' => [ + $mode, + '2.5.4.34=dc=example\,dc=com', + ]; + } + } + #[DataProvider('validationModeProvider')] public function test_an_add_holding_two_entry_uuids_is_refused_in_every_validation_mode( SchemaValidationMode $mode, diff --git a/tests/unit/Server/Backend/Write/Operation/MoveEntryHandlerTest.php b/tests/unit/Server/Backend/Write/Operation/MoveEntryHandlerTest.php index 701db6a5..00413d43 100644 --- a/tests/unit/Server/Backend/Write/Operation/MoveEntryHandlerTest.php +++ b/tests/unit/Server/Backend/Write/Operation/MoveEntryHandlerTest.php @@ -522,20 +522,23 @@ public function test_a_rename_keeps_the_linked_values_its_rdn_does_not_name(): v ); } - public function test_a_new_rdn_naming_a_linked_value_links_it(): void - { - $this->linkedGraph(...$this->fixture()); + #[DataProvider('validationModeProvider')] + public function test_a_new_rdn_naming_a_linked_attribute_is_refused_in_every_validation_mode( + SchemaValidationMode $mode, + ): void { + $this->writeGraph( + EntryFixture::inMemoryStorage(...$this->fixture()), + TestServerOptions::validatedCore($mode), + ); + + self::expectException(OperationException::class); + self::expectExceptionCode(ResultCode::NAMING_VIOLATION); $this->rename( self::ALICE, 'seeAlso=cn=Bob\,ou=People\,dc=example\,dc=com', deleteOldRdn: false, ); - - self::assertSame( - ['cn=Bob,ou=People,dc=example,dc=com'], - $this->find(self::LINKED_RDN_DN)?->get('seeAlso')?->getValues(), - ); } public function test_dropping_an_rdn_that_named_a_linked_value_unlinks_it(): void