Files
firefly-iii/app/Console/Commands/Upgrade/RemovesDatabaseDecryption.php

187 lines
5.6 KiB
PHP
Raw Normal View History

<?php
2023-04-16 07:33:12 +02:00
/*
2019-02-09 10:36:59 +01:00
* DecryptDatabase.php
2023-04-16 07:33:12 +02:00
* Copyright (c) 2023 james@firefly-iii.org
2019-02-09 10:36:59 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-02-09 10:36:59 +01:00
*
* 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.
2019-02-09 10:36:59 +01:00
*
* This program is distributed in the hope that it will be useful,
2019-02-09 10:36:59 +01:00
* 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.
2019-02-09 10:36:59 +01:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-02-09 10:36:59 +01:00
*/
declare(strict_types=1);
2023-04-16 07:33:12 +02:00
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2019-03-05 16:55:03 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Preference;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
2024-12-27 06:48:58 +01:00
class RemovesDatabaseDecryption extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Decrypts the database.';
2024-12-27 06:48:58 +01:00
protected $signature = 'upgrade:480-decrypt-all';
/**
* Execute the console command.
*/
2019-06-10 20:14:00 +02:00
public function handle(): int
{
$tables = [
'accounts' => ['name', 'iban'],
'attachments' => ['filename', 'mime', 'title', 'description'],
'bills' => ['name', 'match'],
'budgets' => ['name'],
'categories' => ['name'],
'piggy_banks' => ['name'],
'preferences' => ['data'],
'tags' => ['tag', 'description'],
'transaction_journals' => ['description'],
'transactions' => ['description'],
'journal_links' => ['comment'],
];
2023-12-20 19:35:52 +01:00
2020-10-18 08:25:56 +02:00
/**
* @var string $table
2023-06-21 12:34:58 +02:00
* @var array $fields
2020-10-18 08:25:56 +02:00
*/
foreach ($tables as $table => $fields) {
2020-10-18 08:25:56 +02:00
$this->decryptTable($table, $fields);
}
2023-12-20 19:35:52 +01:00
return 0;
}
2023-06-21 12:34:58 +02:00
private function decryptTable(string $table, array $fields): void
2021-03-21 09:15:40 +01:00
{
2023-06-21 12:34:58 +02:00
if ($this->isDecrypted($table)) {
return;
}
foreach ($fields as $field) {
$this->decryptField($table, $field);
2021-03-21 09:15:40 +01:00
}
2023-06-21 12:34:58 +02:00
$this->friendlyPositive(sprintf('Decrypted the data in table "%s".', $table));
// mark as decrypted:
$configName = sprintf('is_decrypted_%s', $table);
app('fireflyconfig')->set($configName, true);
2021-03-21 09:15:40 +01:00
}
2023-06-21 12:34:58 +02:00
private function isDecrypted(string $table): bool
{
2023-06-21 12:34:58 +02:00
$configName = sprintf('is_decrypted_%s', $table);
$configVar = null;
2023-12-20 19:35:52 +01:00
2020-10-18 08:25:56 +02:00
try {
2023-06-21 12:34:58 +02:00
$configVar = app('fireflyconfig')->get($configName, false);
} catch (FireflyException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error($e->getMessage());
2023-06-21 12:34:58 +02:00
}
if (null !== $configVar) {
2024-12-22 08:43:12 +01:00
return (bool) $configVar->data;
2023-05-07 20:17:29 +02:00
}
2023-06-21 12:34:58 +02:00
return false;
}
private function decryptField(string $table, string $field): void
{
2023-12-20 19:35:52 +01:00
$rows = \DB::table($table)->get(['id', $field]);
/** @var \stdClass $row */
2023-06-21 12:34:58 +02:00
foreach ($rows as $row) {
$this->decryptRow($table, $field, $row);
2020-10-18 08:25:56 +02:00
}
}
2023-12-20 19:35:52 +01:00
private function decryptRow(string $table, string $field, \stdClass $row): void
2020-10-18 08:25:56 +02:00
{
2023-12-20 19:35:52 +01:00
$original = $row->{$field};
2020-10-18 08:25:56 +02:00
if (null === $original) {
return;
}
2024-12-22 08:43:12 +01:00
$id = (int) $row->id;
$value = '';
2020-10-18 08:25:56 +02:00
try {
$value = $this->tryDecrypt($original);
} catch (FireflyException $e) {
$message = sprintf('Could not decrypt field "%s" in row #%d of table "%s": %s', $field, $id, $table, $e->getMessage());
$this->friendlyError($message);
2023-10-29 06:32:00 +01:00
app('log')->error($message);
app('log')->error($e->getTraceAsString());
2020-10-18 08:25:56 +02:00
}
// A separate routine for preferences table:
if ('preferences' === $table) {
$this->decryptPreferencesRow($id, $value);
2021-03-21 09:15:40 +01:00
2020-10-18 08:25:56 +02:00
return;
}
if ($value !== $original) {
2023-12-20 19:35:52 +01:00
\DB::table($table)->where('id', $id)->update([$field => $value]);
2020-10-18 08:25:56 +02:00
}
}
2021-03-21 09:15:40 +01:00
/**
* Tries to decrypt data. Will only throw an exception when the MAC is invalid.
*
2023-06-21 12:34:58 +02:00
* @param mixed $value
2021-03-21 09:15:40 +01:00
*
* @return string
2023-12-20 19:35:52 +01:00
*
2021-03-21 09:15:40 +01:00
* @throws FireflyException
*/
private function tryDecrypt($value)
{
try {
2023-12-20 19:35:52 +01:00
$value = \Crypt::decrypt($value);
2021-03-21 09:15:40 +01:00
} catch (DecryptException $e) {
if ('The MAC is invalid.' === $e->getMessage()) {
2021-04-07 07:28:43 +02:00
throw new FireflyException($e->getMessage(), 0, $e);
2021-03-21 09:15:40 +01:00
}
}
return $value;
}
2023-06-21 12:34:58 +02:00
private function decryptPreferencesRow(int $id, string $value): void
{
// try to json_decrypt the value.
try {
$newValue = json_decode($value, true, 512, JSON_THROW_ON_ERROR) ?? $value;
2023-12-20 19:35:52 +01:00
} catch (\JsonException $e) {
2023-06-21 12:34:58 +02:00
$message = sprintf('Could not JSON decode preference row #%d: %s. This does not have to be a problem.', $id, $e->getMessage());
$this->friendlyError($message);
app('log')->warning($message);
app('log')->warning($value);
app('log')->warning($e->getTraceAsString());
return;
}
2023-12-20 19:35:52 +01:00
/** @var null|Preference $object */
2023-11-05 08:15:17 +01:00
$object = Preference::find($id);
2023-06-21 12:34:58 +02:00
if (null !== $object) {
$object->data = $newValue;
$object->save();
}
}
}