From 65cd09face129c089b98226ed7fa39bf8a02614d Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 23 Mar 2022 15:28:58 +0100 Subject: [PATCH 1/6] limit ldap_dn columns to 4000 - enlarging string cols from 255 to more than 4000 fails on default Oracle installations for invalid datatype conversion Signed-off-by: Arthur Schiwon --- .../lib/Migration/Version1010Date20200630192842.php | 4 ++-- .../lib/Migration/Version1130Date20211102154716.php | 6 +++--- .../lib/Migration/Version1130Date20220110154718.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/user_ldap/lib/Migration/Version1010Date20200630192842.php b/apps/user_ldap/lib/Migration/Version1010Date20200630192842.php index 939db69a6abf4..5c7dc7db95cd1 100644 --- a/apps/user_ldap/lib/Migration/Version1010Date20200630192842.php +++ b/apps/user_ldap/lib/Migration/Version1010Date20200630192842.php @@ -47,7 +47,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table = $schema->createTable('ldap_user_mapping'); $table->addColumn('ldap_dn', Types::STRING, [ 'notnull' => true, - 'length' => 255, + 'length' => 4000, 'default' => '', ]); $table->addColumn('owncloud_name', Types::STRING, [ @@ -73,7 +73,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table = $schema->createTable('ldap_group_mapping'); $table->addColumn('ldap_dn', Types::STRING, [ 'notnull' => true, - 'length' => 255, + 'length' => 4000, 'default' => '', ]); $table->addColumn('owncloud_name', Types::STRING, [ diff --git a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php index 024c5801582fd..5c5ed44c8992c 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20211102154716.php @@ -95,8 +95,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt } $column = $table->getColumn('ldap_dn'); if ($tableName === 'ldap_user_mapping') { - if ($column->getLength() < 4096) { - $column->setLength(4096); + if ($column->getLength() < 4000) { + $column->setLength(4000); $changeSchema = true; } @@ -117,7 +117,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table2 = $schema->createTable('ldap_group_mapping_backup'); $table2->addColumn('ldap_dn', Types::STRING, [ 'notnull' => true, - 'length' => 4096, + 'length' => 4000, 'default' => '', ]); $table2->addColumn('owncloud_name', Types::STRING, [ diff --git a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php index 5f146ab06a5f2..74dd2d873bc9d 100644 --- a/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php +++ b/apps/user_ldap/lib/Migration/Version1130Date20220110154718.php @@ -54,7 +54,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table = $schema->createTable('ldap_group_mapping'); $table->addColumn('ldap_dn', Types::STRING, [ 'notnull' => true, - 'length' => 4096, + 'length' => 4000, 'default' => '', ]); $table->addColumn('owncloud_name', Types::STRING, [ From 8ce4f33e10466f802ec7117613ea75818331e78d Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 23 Mar 2022 16:16:24 +0100 Subject: [PATCH 2/6] shrink ldap_dn column to 4000 Signed-off-by: Arthur Schiwon --- .../Version1141Date20220323143801.php | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 apps/user_ldap/lib/Migration/Version1141Date20220323143801.php diff --git a/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php b/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php new file mode 100644 index 0000000000000..84663466bd7f5 --- /dev/null +++ b/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php @@ -0,0 +1,117 @@ + + * + * @author Arthur Schiwon + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\User_LDAP\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1141Date20220323143801 extends SimpleMigrationStep { + + private IDBConnection $dbc; + + public function __construct(IDBConnection $dbc) { + $this->dbc = $dbc; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { + $dnsTooLong = []; + $lengthExpr = $this->dbc->getDatabasePlatform()->getLengthExpression('ldap_dn'); + + $qb = $this->dbc->getQueryBuilder(); + $qb->select('ldap_dn') + ->from($tableName) + ->where($qb->expr()->gt($qb->createFunction($lengthExpr), '255', IQueryBuilder::PARAM_INT)); + + $result = $qb->executeQuery(); + while(($dn = $result->fetchOne()) !== false) { + if(mb_strlen($dn) > 4000) { + $dnsTooLong[] = $dn; + } + } + $result->closeCursor(); + $this->shortenDNs($dnsTooLong, $tableName); + } + } + + protected function shortenDNs(array $dns, string $table): void { + $qb = $this->dbc->getQueryBuilder(); + $qb->update($table) + ->set('ldap_dn', $qb->createParameter('shortenedDn')) + ->where($qb->expr()->eq('ldap_dn', $qb->createParameter('originalDn'))); + + $pageSize = 1000; + $page = 0; + do { + $subset = array_slice($dns, $page * $pageSize, $pageSize); + try { + $this->dbc->beginTransaction(); + foreach ($subset as $dn) { + $shortenedDN = mb_substr($dn, 0, 4000); + $qb->setParameter('shortenedDn', $shortenedDN); + $qb->setParameter('originalDn', $dn); + $qb->executeStatement(); + } + $this->dbc->commit(); + } catch (\Throwable $t) { + $this->dbc->rollBack(); + throw $t; + } + $page++; + } while (count($subset) === $pageSize); + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { + $table = $schema->getTable($tableName); + $column = $table->getColumn('ldap_dn'); + if ($column->getLength() > 4000) { + $column->setLength(4000); + } + } + + return $schema; + } +} From f9b0c85933c18a5158defa925e8c2978400ef1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 29 Mar 2022 17:00:38 +0200 Subject: [PATCH 3/6] Adapt max length limit in AbstactMapping to match database MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/Mapping/AbstractMapping.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/user_ldap/lib/Mapping/AbstractMapping.php b/apps/user_ldap/lib/Mapping/AbstractMapping.php index 1a747cc8bfd4d..15dce3e067d9d 100644 --- a/apps/user_ldap/lib/Mapping/AbstractMapping.php +++ b/apps/user_ldap/lib/Mapping/AbstractMapping.php @@ -358,9 +358,9 @@ public function getList(int $offset = 0, int $limit = null, bool $invalidatedOnl * @return bool */ public function map($fdn, $name, $uuid) { - if (mb_strlen($fdn) > 4096) { + if (mb_strlen($fdn) > 4000) { \OC::$server->getLogger()->error( - 'Cannot map, because the DN exceeds 4096 characters: {dn}', + 'Cannot map, because the DN exceeds 4000 characters: {dn}', [ 'app' => 'user_ldap', 'dn' => $fdn, From a6143ab0da4853599674a92203a23c9613df1f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 29 Mar 2022 17:01:41 +0200 Subject: [PATCH 4/6] Use the new octetLength function to filter lines to migrate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../lib/Migration/Version1141Date20220323143801.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php b/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php index 84663466bd7f5..be496d9215e35 100644 --- a/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php +++ b/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php @@ -48,19 +48,15 @@ public function __construct(IDBConnection $dbc) { */ public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { - $dnsTooLong = []; - $lengthExpr = $this->dbc->getDatabasePlatform()->getLengthExpression('ldap_dn'); - $qb = $this->dbc->getQueryBuilder(); $qb->select('ldap_dn') ->from($tableName) - ->where($qb->expr()->gt($qb->createFunction($lengthExpr), '255', IQueryBuilder::PARAM_INT)); + ->where($qb->expr()->gt($qb->func()->octetLength('ldap_dn'), '4000', IQueryBuilder::PARAM_INT)); + $dnsTooLong = []; $result = $qb->executeQuery(); - while(($dn = $result->fetchOne()) !== false) { - if(mb_strlen($dn) > 4000) { - $dnsTooLong[] = $dn; - } + while (($dn = $result->fetchOne()) !== false) { + $dnsTooLong[] = $dn; } $result->closeCursor(); $this->shortenDNs($dnsTooLong, $tableName); From a2b19f1ae946b4a8c38b22757a04bf16af6804ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Wed, 30 Mar 2022 11:04:21 +0200 Subject: [PATCH 5/6] Build autoloaders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/composer/composer/autoload_classmap.php | 1 + apps/user_ldap/composer/composer/autoload_static.php | 1 + 2 files changed, 2 insertions(+) diff --git a/apps/user_ldap/composer/composer/autoload_classmap.php b/apps/user_ldap/composer/composer/autoload_classmap.php index 34a208357fe88..b6760d2b780ed 100644 --- a/apps/user_ldap/composer/composer/autoload_classmap.php +++ b/apps/user_ldap/composer/composer/autoload_classmap.php @@ -68,6 +68,7 @@ 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => $baseDir . '/../lib/Migration/Version1130Date20220110154717.php', 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => $baseDir . '/../lib/Migration/Version1130Date20220110154718.php', 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => $baseDir . '/../lib/Migration/Version1130Date20220110154719.php', + 'OCA\\User_LDAP\\Migration\\Version1141Date20220323143801' => $baseDir . '/../lib/Migration/Version1141Date20220323143801.php', 'OCA\\User_LDAP\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php', 'OCA\\User_LDAP\\PagedResults\\IAdapter' => $baseDir . '/../lib/PagedResults/IAdapter.php', 'OCA\\User_LDAP\\PagedResults\\Php73' => $baseDir . '/../lib/PagedResults/Php73.php', diff --git a/apps/user_ldap/composer/composer/autoload_static.php b/apps/user_ldap/composer/composer/autoload_static.php index e9fa63b818524..ee6a2191cf730 100644 --- a/apps/user_ldap/composer/composer/autoload_static.php +++ b/apps/user_ldap/composer/composer/autoload_static.php @@ -83,6 +83,7 @@ class ComposerStaticInitUser_LDAP 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154717.php', 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154718.php', 'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154719.php', + 'OCA\\User_LDAP\\Migration\\Version1141Date20220323143801' => __DIR__ . '/..' . '/../lib/Migration/Version1141Date20220323143801.php', 'OCA\\User_LDAP\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php', 'OCA\\User_LDAP\\PagedResults\\IAdapter' => __DIR__ . '/..' . '/../lib/PagedResults/IAdapter.php', 'OCA\\User_LDAP\\PagedResults\\Php73' => __DIR__ . '/..' . '/../lib/PagedResults/Php73.php', From 167a54299d559dbe19e0cf87f9c878836878a29f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 20 Dec 2022 10:40:58 +0100 Subject: [PATCH 6/6] Remove property type for PHP 7.3 support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../user_ldap/lib/Migration/Version1141Date20220323143801.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php b/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php index be496d9215e35..84faee03b66c3 100644 --- a/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php +++ b/apps/user_ldap/lib/Migration/Version1141Date20220323143801.php @@ -34,8 +34,8 @@ use OCP\Migration\SimpleMigrationStep; class Version1141Date20220323143801 extends SimpleMigrationStep { - - private IDBConnection $dbc; + /** @var IDBConnection */ + private $dbc; public function __construct(IDBConnection $dbc) { $this->dbc = $dbc;