-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathVersion13000Date20251031165700.php
More file actions
377 lines (318 loc) · 11.3 KB
/
Version13000Date20251031165700.php
File metadata and controls
377 lines (318 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Migration;
use Closure;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Service\CaIdentifierService;
use OCA\Libresign\Service\Install\InstallService;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\Types;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\IAppData;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Override;
use Psr\Log\LoggerInterface;
class Version13000Date20251031165700 extends SimpleMigrationStep {
protected IAppData $appData;
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private CertificateEngineFactory $certificateEngineFactory,
private CaIdentifierService $caIdentifierService,
private InstallService $installService,
private IDBConnection $connection,
private IAppDataFactory $appDataFactory,
private LoggerInterface $logger,
) {
$this->appData = $appDataFactory->get('libresign');
}
/**
* Prepare operations before schema changes
*
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
#[Override]
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$this->convertRootCertOuStringToArray();
$this->addConfigPathToOpenSsl();
$this->backupCrlDataToDisk();
}
/**
* Apply schema changes to the database
*
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
#[Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$engineName = $this->appConfig->getValueString(Application::APP_ID, 'certificate_engine', '');
if ($schema->hasTable('libresign_crl')) {
$crlTable = $schema->getTable('libresign_crl');
if ($crlTable->hasColumn('serial_number')) {
$crlTable->dropColumn('serial_number');
}
$crlTable->addColumn('serial_number', Types::STRING, [
'length' => 64,
]);
if (!$crlTable->hasColumn('engine')) {
$crlTable->addColumn('engine', Types::STRING, ['default' => $engineName]);
}
if (!$crlTable->hasColumn('instance_id')) {
$crlTable->addColumn('instance_id', Types::STRING, ['notnull' => false]);
}
if (!$crlTable->hasColumn('generation')) {
$crlTable->addColumn('generation', Types::INTEGER, ['notnull' => false]);
}
if (!$crlTable->hasColumn('issuer')) {
$crlTable->addColumn('issuer', Types::TEXT, ['notnull' => false]);
}
if (!$crlTable->hasColumn('subject')) {
$crlTable->addColumn('subject', Types::TEXT, ['notnull' => false]);
}
if (!$crlTable->hasColumn('certificate_type')) {
$crlTable->addColumn('certificate_type', Types::STRING, [
'length' => 20,
'default' => 'leaf',
]);
}
if ($crlTable->hasIndex('libresign_crl_serial_uk')) {
$crlTable->dropIndex('libresign_crl_serial_uk');
}
$crlTable->addUniqueIndex(['serial_number'], 'libresign_crl_serial_uk');
}
return $schema;
}
/**
* Execute operations that depend on the new schema
*
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
#[Override]
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$this->migrateToNewestConfigFormat();
$this->restoreCrlDataFromDisk();
$this->populateCrlInstanceAndGeneration();
}
private function addConfigPathToOpenSsl(): void {
$engineName = $this->appConfig->getValueString(Application::APP_ID, 'certificate_engine', '');
if ($engineName !== 'openssl') {
return;
}
$engine = $this->certificateEngineFactory->getEngine();
$configPath = $this->appConfig->getValueString(Application::APP_ID, 'config_path', '');
if (empty($configPath)) {
$engine->setConfigPath($engine->getCurrentConfigPath());
}
}
private function migrateToNewestConfigFormat(): void {
$dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
$rootPath = $dataDir . '/appdata_' . $this->config->getSystemValue('instanceid') . '/libresign/';
if (!is_dir($rootPath)) {
return;
}
$originalCaId = $this->appConfig->getValueString(Application::APP_ID, 'ca_id');
if (empty($originalCaId)) {
$engineName = $this->appConfig->getValueString(Application::APP_ID, 'certificate_engine');
if ($engineName) {
$originalCaId = $this->caIdentifierService->generateCaId($engineName);
}
}
$generatedNewCaId = false;
$engines = ['o' => 'openssl', 'c' => 'cfssl'];
foreach ($engines as $engineType => $engineName) {
if (!is_dir($rootPath . $engineName . '_config')) {
continue;
}
$engine = $this->certificateEngineFactory->getEngine($engineName);
if (empty($originalCaId) || !str_ends_with($originalCaId, '-e:' . $engineType)) {
$generatedNewCaId = true;
$this->caIdentifierService->generateCaId($engineName);
}
$this->appConfig->deleteKey(Application::APP_ID, 'config_path');
$configPath = $engine->getCurrentConfigPath();
$configFiles = glob($rootPath . $engineName . '_config/*');
if (!empty($configFiles) && empty(glob($configPath . '/*'))) {
foreach ($configFiles as $file) {
if (is_file($file)) {
copy($file, $configPath . '/' . basename($file));
}
}
}
if (!empty($configFiles)) {
foreach ($configFiles as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
if (is_dir($rootPath . $engineName . '_config')) {
rmdir($rootPath . $engineName . '_config');
}
}
if ($generatedNewCaId && $originalCaId) {
$this->appConfig->setValueString(Application::APP_ID, 'ca_id', $originalCaId);
}
}
private function convertRootCertOuStringToArray(): void {
$rootCert = $this->appConfig->getValueArray(Application::APP_ID, 'rootCert');
if (!$rootCert || !isset($rootCert['names']['OU']['value'])) {
return;
}
$ouValue = $rootCert['names']['OU']['value'];
if (is_string($ouValue)) {
$rootCert['names']['OU']['value'] = [$ouValue];
$this->appConfig->setValueArray(Application::APP_ID, 'rootCert', $rootCert);
}
}
private function populateCrlInstanceAndGeneration(): void {
$currentCaId = $this->appConfig->getValueString(Application::APP_ID, 'ca_id');
if (empty($currentCaId)) {
return;
}
try {
$pattern = '/^libresign-ca-id:(?P<instanceId>[a-z0-9]+)_g:(?P<generation>\d+)_e:(?P<engineType>[oc])$/';
if (!preg_match($pattern, $currentCaId, $matches)) {
return;
}
$instanceId = $matches['instanceId'];
$generation = (int)$matches['generation'];
$engineType = $matches['engineType'];
$engineName = $engineType === 'o' ? 'openssl' : 'cfssl';
$rootCertCreationDate = $this->getRootCertificateCreationDate();
if ($rootCertCreationDate === null) {
return;
}
$qb = $this->connection->getQueryBuilder();
$qb->update('libresign_crl')
->set('instance_id', $qb->createNamedParameter($instanceId))
->set('generation', $qb->createNamedParameter($generation, IQueryBuilder::PARAM_INT))
->set('engine', $qb->createNamedParameter($engineName))
->where($qb->expr()->gte('issued_at', $qb->createNamedParameter($rootCertCreationDate->getTimestamp(), IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->isNull('instance_id'));
$qb->executeStatement();
} catch (\Exception $e) {
$this->logger->error('Error creating backup folder for CRL data during migration: ' . $e->getMessage(), ['exception' => $e]);
return;
}
}
private function getRootCertificateCreationDate(): ?\DateTime {
try {
$currentCaId = $this->appConfig->getValueString(Application::APP_ID, 'ca_id');
if (empty($currentCaId)) {
return null;
}
$pattern = '/^libresign-ca-id:(?P<instanceId>[a-z0-9]+)_g:(?P<generation>\d+)_e:(?P<engineType>[oc])$/';
if (!preg_match($pattern, $currentCaId, $matches)) {
return null;
}
$instanceId = $matches['instanceId'];
$generation = (int)$matches['generation'];
$engineType = $matches['engineType'];
$engineName = $engineType === 'o' ? 'openssl' : 'cfssl';
$engine = $this->certificateEngineFactory->getEngine($engineName);
$configPath = $engine->getConfigPathByParams($instanceId, $generation);
$caCertPath = $configPath . DIRECTORY_SEPARATOR . 'ca.pem';
if (!file_exists($caCertPath)) {
return null;
}
$certContent = file_get_contents($caCertPath);
if (!$certContent) {
return null;
}
$x509Resource = openssl_x509_read($certContent);
if (!$x509Resource) {
return null;
}
$parsed = openssl_x509_parse($x509Resource);
if (!$parsed || !isset($parsed['validFrom_time_t'])) {
return null;
}
return new \DateTime('@' . $parsed['validFrom_time_t']);
} catch (\Exception $e) {
$this->logger->error('Error parsing certificate for creation date during migration: ' . $e->getMessage(), ['exception' => $e]);
return null;
}
}
private function backupCrlDataToDisk(): void {
try {
$qb = $this->connection->getQueryBuilder();
$qb->select('id', 'serial_number')
->from('libresign_crl');
$this->persistData($qb, 'backup-table-libresign_crl_Version13000Date20251031165700.csv');
} catch (\Exception $e) {
$this->logger->error('Error backing up CRL data to disk during migration: ' . $e->getMessage(), ['exception' => $e]);
}
}
private function persistData(IQueryBuilder $query, string $filename): void {
$cursor = $query->executeQuery();
$row = $cursor->fetch();
if ($row) {
$folder = $this->appData->getFolder('/');
$file = $folder->newFile($filename);
$file->putContent('');
$handle = $file->write();
fputcsv($handle, array_keys($row));
fputcsv($handle, $row);
while ($row = $cursor->fetch()) {
fputcsv($handle, $row);
}
fclose($handle);
}
$cursor->closeCursor();
}
private function restoreCrlDataFromDisk(): void {
$filename = 'backup-table-libresign_crl_Version13000Date20251031165700.csv';
try {
$folder = $this->appData->getFolder('/');
if (!$folder->fileExists($filename)) {
return;
}
$file = $folder->getFile($filename);
$handle = $file->read();
if (!$handle) {
return;
}
$headers = fgetcsv($handle);
if (!$headers || !in_array('id', $headers) || !in_array('serial_number', $headers)) {
fclose($handle);
return;
}
$idIndex = array_search('id', $headers);
$serialIndex = array_search('serial_number', $headers);
while (($row = fgetcsv($handle)) !== false) {
if (!isset($row[$idIndex]) || !isset($row[$serialIndex])) {
continue;
}
$id = (int)$row[$idIndex];
$serialNumber = $row[$serialIndex];
$qb = $this->connection->getQueryBuilder();
$qb->update('libresign_crl')
->set('serial_number', $qb->createNamedParameter($serialNumber))
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}
fclose($handle);
$file->delete();
} catch (\Exception $e) {
$this->logger->error('Error restoring CRL data from disk during migration: ' . $e->getMessage(), ['exception' => $e]);
}
}
}