|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Libresign\Migration; |
| 11 | + |
| 12 | +use Closure; |
| 13 | +use OCA\Libresign\AppInfo\Application; |
| 14 | +use OCP\DB\ISchemaWrapper; |
| 15 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 16 | +use OCP\IAppConfig; |
| 17 | +use OCP\IConfig; |
| 18 | +use OCP\IDBConnection; |
| 19 | +use OCP\Migration\IOutput; |
| 20 | +use OCP\Migration\SimpleMigrationStep; |
| 21 | + |
| 22 | +class Version17004Date20260421000000 extends SimpleMigrationStep { |
| 23 | + public function __construct( |
| 24 | + private IConfig $config, |
| 25 | + private IAppConfig $appConfig, |
| 26 | + private IDBConnection $connection, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param IOutput $output |
| 32 | + * @param Closure(): ISchemaWrapper $schemaClosure |
| 33 | + * @param array $options |
| 34 | + */ |
| 35 | + #[\Override] |
| 36 | + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
| 37 | + /** @var ISchemaWrapper $schema */ |
| 38 | + $schema = $schemaClosure(); |
| 39 | + if (!$schema->hasTable('libresign_crl')) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + $metadata = $this->resolveMetadataDefaults(); |
| 44 | + if ($metadata === null) { |
| 45 | + $output->warning('Skipped CRL metadata backfill because no deterministic metadata source was found.'); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $updatedInstance = $this->backfillInstanceId($metadata['instanceId']); |
| 50 | + $updatedGeneration = $this->backfillGeneration($metadata['generation']); |
| 51 | + $updatedEngine = $this->backfillEngine($metadata['engine']); |
| 52 | + |
| 53 | + if ($updatedInstance > 0 || $updatedGeneration > 0 || $updatedEngine > 0) { |
| 54 | + $output->warning(sprintf( |
| 55 | + 'Backfilled CRL metadata for legacy rows (instance_id=%d, generation=%d, engine=%d).', |
| 56 | + $updatedInstance, |
| 57 | + $updatedGeneration, |
| 58 | + $updatedEngine, |
| 59 | + )); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param IOutput $output |
| 65 | + * @param Closure(): ISchemaWrapper $schemaClosure |
| 66 | + * @param array $options |
| 67 | + * @return null|ISchemaWrapper |
| 68 | + */ |
| 69 | + #[\Override] |
| 70 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 71 | + /** @var ISchemaWrapper $schema */ |
| 72 | + return $schemaClosure(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array{instanceId: string, generation: int, engine: string}|null |
| 77 | + */ |
| 78 | + private function resolveMetadataDefaults(): ?array { |
| 79 | + $engine = $this->appConfig->getValueString(Application::APP_ID, 'certificate_engine', 'openssl'); |
| 80 | + if ($engine === '' || $engine === 'none') { |
| 81 | + $engine = 'openssl'; |
| 82 | + } |
| 83 | + |
| 84 | + $instanceId = $this->appConfig->getValueString(Application::APP_ID, 'instance_id', ''); |
| 85 | + $generation = max(1, $this->appConfig->getValueInt(Application::APP_ID, 'ca_generation_counter', 1)); |
| 86 | + $caId = $this->appConfig->getValueString(Application::APP_ID, 'ca_id', ''); |
| 87 | + |
| 88 | + $pattern = '/^libresign-ca-id:(?P<instanceId>[a-z0-9]+)_g:(?P<generation>\d+)_e:(?P<engineType>[oc])$/'; |
| 89 | + if ($caId !== '' && preg_match($pattern, $caId, $matches)) { |
| 90 | + $instanceId = $matches['instanceId']; |
| 91 | + $generation = max(1, (int)$matches['generation']); |
| 92 | + $engine = $matches['engineType'] === 'c' ? 'cfssl' : 'openssl'; |
| 93 | + } |
| 94 | + |
| 95 | + if ($instanceId === '') { |
| 96 | + $instanceId = $this->config->getSystemValueString('instanceid', ''); |
| 97 | + } |
| 98 | + |
| 99 | + if ($instanceId === '' || !in_array($engine, ['openssl', 'cfssl'], true)) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + return [ |
| 104 | + 'instanceId' => $instanceId, |
| 105 | + 'generation' => $generation, |
| 106 | + 'engine' => $engine, |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + private function backfillInstanceId(string $instanceId): int { |
| 111 | + $qb = $this->connection->getQueryBuilder(); |
| 112 | + |
| 113 | + return $qb->update('libresign_crl') |
| 114 | + ->set('instance_id', $qb->createNamedParameter($instanceId)) |
| 115 | + ->where($qb->expr()->eq('status', $qb->createNamedParameter('issued'))) |
| 116 | + ->andWhere( |
| 117 | + $qb->expr()->orX( |
| 118 | + $qb->expr()->isNull('generation'), |
| 119 | + $qb->expr()->isNull('engine'), |
| 120 | + $qb->expr()->eq('engine', $qb->createNamedParameter('')), |
| 121 | + ) |
| 122 | + ) |
| 123 | + ->andWhere( |
| 124 | + $qb->expr()->orX( |
| 125 | + $qb->expr()->isNull('instance_id'), |
| 126 | + $qb->expr()->eq('instance_id', $qb->createNamedParameter('')), |
| 127 | + ) |
| 128 | + ) |
| 129 | + ->executeStatement(); |
| 130 | + } |
| 131 | + |
| 132 | + private function backfillGeneration(int $generation): int { |
| 133 | + $qb = $this->connection->getQueryBuilder(); |
| 134 | + |
| 135 | + return $qb->update('libresign_crl') |
| 136 | + ->set('generation', $qb->createNamedParameter($generation, IQueryBuilder::PARAM_INT)) |
| 137 | + ->where($qb->expr()->eq('status', $qb->createNamedParameter('issued'))) |
| 138 | + ->andWhere( |
| 139 | + $qb->expr()->orX( |
| 140 | + $qb->expr()->isNull('instance_id'), |
| 141 | + $qb->expr()->eq('instance_id', $qb->createNamedParameter('')), |
| 142 | + $qb->expr()->isNull('engine'), |
| 143 | + $qb->expr()->eq('engine', $qb->createNamedParameter('')), |
| 144 | + ) |
| 145 | + ) |
| 146 | + ->andWhere($qb->expr()->isNull('generation')) |
| 147 | + ->executeStatement(); |
| 148 | + } |
| 149 | + |
| 150 | + private function backfillEngine(string $engine): int { |
| 151 | + $qb = $this->connection->getQueryBuilder(); |
| 152 | + |
| 153 | + return $qb->update('libresign_crl') |
| 154 | + ->set('engine', $qb->createNamedParameter($engine)) |
| 155 | + ->where($qb->expr()->eq('status', $qb->createNamedParameter('issued'))) |
| 156 | + ->andWhere( |
| 157 | + $qb->expr()->orX( |
| 158 | + $qb->expr()->isNull('instance_id'), |
| 159 | + $qb->expr()->eq('instance_id', $qb->createNamedParameter('')), |
| 160 | + $qb->expr()->isNull('generation'), |
| 161 | + ) |
| 162 | + ) |
| 163 | + ->andWhere( |
| 164 | + $qb->expr()->orX( |
| 165 | + $qb->expr()->isNull('engine'), |
| 166 | + $qb->expr()->eq('engine', $qb->createNamedParameter('')), |
| 167 | + ) |
| 168 | + ) |
| 169 | + ->executeStatement(); |
| 170 | + } |
| 171 | +} |
0 commit comments