aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Encryption
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Encryption')
-rw-r--r--core/Command/Encryption/ChangeKeyStorageRoot.php12
-rw-r--r--core/Command/Encryption/DecryptAll.php8
-rw-r--r--core/Command/Encryption/EncryptAll.php14
-rw-r--r--core/Command/Encryption/ListModules.php6
-rw-r--r--core/Command/Encryption/MigrateKeyStorage.php70
-rw-r--r--core/Command/Encryption/SetDefaultModule.php4
6 files changed, 59 insertions, 55 deletions
diff --git a/core/Command/Encryption/ChangeKeyStorageRoot.php b/core/Command/Encryption/ChangeKeyStorageRoot.php
index a4431b36f2f..3049fd2ca08 100644
--- a/core/Command/Encryption/ChangeKeyStorageRoot.php
+++ b/core/Command/Encryption/ChangeKeyStorageRoot.php
@@ -79,10 +79,10 @@ class ChangeKeyStorageRoot extends Command {
* @throws \Exception
*/
protected function moveAllKeys($oldRoot, $newRoot, OutputInterface $output) {
- $output->writeln("Start to move keys:");
+ $output->writeln('Start to move keys:');
if ($this->rootView->is_dir($oldRoot) === false) {
- $output->writeln("No old keys found: Nothing needs to be moved");
+ $output->writeln('No old keys found: Nothing needs to be moved');
return false;
}
@@ -123,8 +123,8 @@ class ChangeKeyStorageRoot extends Command {
*/
protected function moveSystemKeys($oldRoot, $newRoot) {
if (
- $this->rootView->is_dir($oldRoot . '/files_encryption') &&
- $this->targetExists($newRoot . '/files_encryption') === false
+ $this->rootView->is_dir($oldRoot . '/files_encryption')
+ && $this->targetExists($newRoot . '/files_encryption') === false
) {
$this->rootView->rename($oldRoot . '/files_encryption', $newRoot . '/files_encryption');
}
@@ -183,8 +183,8 @@ class ChangeKeyStorageRoot extends Command {
$source = $oldRoot . '/' . $user . '/files_encryption';
$target = $newRoot . '/' . $user . '/files_encryption';
if (
- $this->rootView->is_dir($source) &&
- $this->targetExists($target) === false
+ $this->rootView->is_dir($source)
+ && $this->targetExists($target) === false
) {
$this->prepareParentFolder($newRoot . '/' . $user);
$this->rootView->rename($source, $target);
diff --git a/core/Command/Encryption/DecryptAll.php b/core/Command/Encryption/DecryptAll.php
index 1de14debdec..92e2ba787e2 100644
--- a/core/Command/Encryption/DecryptAll.php
+++ b/core/Command/Encryption/DecryptAll.php
@@ -81,10 +81,10 @@ class DecryptAll extends Command {
$isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
if ($isMaintenanceModeEnabled) {
- $output->writeln("Maintenance mode must be disabled when starting decryption,");
- $output->writeln("in order to load the relevant encryption modules correctly.");
- $output->writeln("Your instance will automatically be put to maintenance mode");
- $output->writeln("during the actual decryption of the files.");
+ $output->writeln('Maintenance mode must be disabled when starting decryption,');
+ $output->writeln('in order to load the relevant encryption modules correctly.');
+ $output->writeln('Your instance will automatically be put to maintenance mode');
+ $output->writeln('during the actual decryption of the files.');
return 1;
}
diff --git a/core/Command/Encryption/EncryptAll.php b/core/Command/Encryption/EncryptAll.php
index 868c240e26d..f2c991471b6 100644
--- a/core/Command/Encryption/EncryptAll.php
+++ b/core/Command/Encryption/EncryptAll.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -17,7 +18,6 @@ use Symfony\Component\Console\Question\ConfirmationQuestion;
class EncryptAll extends Command {
protected bool $wasTrashbinEnabled = false;
- protected bool $wasMaintenanceModeEnabled = false;
public function __construct(
protected IManager $encryptionManager,
@@ -33,7 +33,6 @@ class EncryptAll extends Command {
*/
protected function forceMaintenanceAndTrashbin(): void {
$this->wasTrashbinEnabled = (bool)$this->appManager->isEnabledForUser('files_trashbin');
- $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
$this->config->setSystemValue('maintenance', true);
$this->appManager->disableApp('files_trashbin');
}
@@ -42,7 +41,7 @@ class EncryptAll extends Command {
* Reset the maintenance mode and re-enable the trashbin app
*/
protected function resetMaintenanceAndTrashbin(): void {
- $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
+ $this->config->setSystemValue('maintenance', false);
if ($this->wasTrashbinEnabled) {
$this->appManager->enableApp('files_trashbin');
}
@@ -73,6 +72,11 @@ class EncryptAll extends Command {
throw new \Exception('Server side encryption is not enabled');
}
+ if ($this->config->getSystemValueBool('maintenance')) {
+ $output->writeln('<error>This command cannot be run with maintenance mode enabled.</error>');
+ return self::FAILURE;
+ }
+
$output->writeln("\n");
$output->writeln('You are about to encrypt all files stored in your Nextcloud installation.');
$output->writeln('Depending on the number of available files, and their size, this may take quite some time.');
@@ -92,9 +96,9 @@ class EncryptAll extends Command {
}
$this->resetMaintenanceAndTrashbin();
- return 0;
+ return self::SUCCESS;
}
$output->writeln('aborted');
- return 1;
+ return self::FAILURE;
}
}
diff --git a/core/Command/Encryption/ListModules.php b/core/Command/Encryption/ListModules.php
index a5cd403d3a7..bf02c29f432 100644
--- a/core/Command/Encryption/ListModules.php
+++ b/core/Command/Encryption/ListModules.php
@@ -33,8 +33,8 @@ class ListModules extends Base {
protected function execute(InputInterface $input, OutputInterface $output): int {
$isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
if ($isMaintenanceModeEnabled) {
- $output->writeln("Maintenance mode must be disabled when listing modules");
- $output->writeln("in order to list the relevant encryption modules correctly.");
+ $output->writeln('Maintenance mode must be disabled when listing modules');
+ $output->writeln('in order to list the relevant encryption modules correctly.');
return 1;
}
@@ -57,7 +57,7 @@ class ListModules extends Base {
*/
protected function writeModuleList(InputInterface $input, OutputInterface $output, $items) {
if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
- array_walk($items, function (&$item) {
+ array_walk($items, function (&$item): void {
if (!$item['default']) {
$item = $item['displayName'];
} else {
diff --git a/core/Command/Encryption/MigrateKeyStorage.php b/core/Command/Encryption/MigrateKeyStorage.php
index 486a6149622..937b17cde5f 100644
--- a/core/Command/Encryption/MigrateKeyStorage.php
+++ b/core/Command/Encryption/MigrateKeyStorage.php
@@ -30,7 +30,7 @@ class MigrateKeyStorage extends Command {
parent::__construct();
}
- protected function configure() {
+ protected function configure(): void {
parent::configure();
$this
->setName('encryption:migrate-key-storage-format')
@@ -40,9 +40,9 @@ class MigrateKeyStorage extends Command {
protected function execute(InputInterface $input, OutputInterface $output): int {
$root = $this->util->getKeyStorageRoot();
- $output->writeln("Updating key storage format");
+ $output->writeln('Updating key storage format');
$this->updateKeys($root, $output);
- $output->writeln("Key storage format successfully updated");
+ $output->writeln('Key storage format successfully updated');
return 0;
}
@@ -50,15 +50,12 @@ class MigrateKeyStorage extends Command {
/**
* Move keys to new key storage root
*
- * @param string $root
- * @param OutputInterface $output
- * @return bool
* @throws \Exception
*/
protected function updateKeys(string $root, OutputInterface $output): bool {
- $output->writeln("Start to update the keys:");
+ $output->writeln('Start to update the keys:');
- $this->updateSystemKeys($root);
+ $this->updateSystemKeys($root, $output);
$this->updateUsersKeys($root, $output);
$this->config->deleteSystemValue('encryption.key_storage_migrated');
return true;
@@ -67,15 +64,15 @@ class MigrateKeyStorage extends Command {
/**
* Move system key folder
*/
- protected function updateSystemKeys(string $root): void {
+ protected function updateSystemKeys(string $root, OutputInterface $output): void {
if (!$this->rootView->is_dir($root . '/files_encryption')) {
return;
}
- $this->traverseKeys($root . '/files_encryption', null);
+ $this->traverseKeys($root . '/files_encryption', null, $output);
}
- private function traverseKeys(string $folder, ?string $uid) {
+ private function traverseKeys(string $folder, ?string $uid, OutputInterface $output): void {
$listing = $this->rootView->getDirectoryContent($folder);
foreach ($listing as $node) {
@@ -83,14 +80,19 @@ class MigrateKeyStorage extends Command {
continue;
}
- if ($node['name'] === 'fileKey' ||
- str_ends_with($node['name'], '.privateKey') ||
- str_ends_with($node['name'], '.publicKey') ||
- str_ends_with($node['name'], '.shareKey')) {
+ if ($node['name'] === 'fileKey'
+ || str_ends_with($node['name'], '.privateKey')
+ || str_ends_with($node['name'], '.publicKey')
+ || str_ends_with($node['name'], '.shareKey')) {
$path = $folder . '/' . $node['name'];
$content = $this->rootView->file_get_contents($path);
+ if ($content === false) {
+ $output->writeln("<error>Failed to open path $path</error>");
+ continue;
+ }
+
try {
$this->crypto->decrypt($content);
continue;
@@ -109,14 +111,14 @@ class MigrateKeyStorage extends Command {
}
}
- private function traverseFileKeys(string $folder) {
+ private function traverseFileKeys(string $folder, OutputInterface $output): void {
$listing = $this->rootView->getDirectoryContent($folder);
foreach ($listing as $node) {
if ($node['mimetype'] === 'httpd/unix-directory') {
- $this->traverseFileKeys($folder . '/' . $node['name']);
+ $this->traverseFileKeys($folder . '/' . $node['name'], $output);
} else {
- $endsWith = function ($haystack, $needle) {
+ $endsWith = function (string $haystack, string $needle): bool {
$length = strlen($needle);
if ($length === 0) {
return true;
@@ -125,14 +127,19 @@ class MigrateKeyStorage extends Command {
return (substr($haystack, -$length) === $needle);
};
- if ($node['name'] === 'fileKey' ||
- $endsWith($node['name'], '.privateKey') ||
- $endsWith($node['name'], '.publicKey') ||
- $endsWith($node['name'], '.shareKey')) {
+ if ($node['name'] === 'fileKey'
+ || $endsWith($node['name'], '.privateKey')
+ || $endsWith($node['name'], '.publicKey')
+ || $endsWith($node['name'], '.shareKey')) {
$path = $folder . '/' . $node['name'];
$content = $this->rootView->file_get_contents($path);
+ if ($content === false) {
+ $output->writeln("<error>Failed to open path $path</error>");
+ continue;
+ }
+
try {
$this->crypto->decrypt($content);
continue;
@@ -154,10 +161,8 @@ class MigrateKeyStorage extends Command {
/**
* setup file system for the given user
- *
- * @param string $uid
*/
- protected function setupUserFS($uid) {
+ protected function setupUserFS(string $uid): void {
\OC_Util::tearDownFS();
\OC_Util::setupFS($uid);
}
@@ -165,11 +170,8 @@ class MigrateKeyStorage extends Command {
/**
* iterate over each user and move the keys to the new storage
- *
- * @param string $root
- * @param OutputInterface $output
*/
- protected function updateUsersKeys(string $root, OutputInterface $output) {
+ protected function updateUsersKeys(string $root, OutputInterface $output): void {
$progress = new ProgressBar($output);
$progress->start();
@@ -181,7 +183,7 @@ class MigrateKeyStorage extends Command {
foreach ($users as $user) {
$progress->advance();
$this->setupUserFS($user);
- $this->updateUserKeys($root, $user);
+ $this->updateUserKeys($root, $user, $output);
}
$offset += $limit;
} while (count($users) >= $limit);
@@ -192,20 +194,18 @@ class MigrateKeyStorage extends Command {
/**
* move user encryption folder to new root folder
*
- * @param string $root
- * @param string $user
* @throws \Exception
*/
- protected function updateUserKeys(string $root, string $user) {
+ protected function updateUserKeys(string $root, string $user, OutputInterface $output): void {
if ($this->userManager->userExists($user)) {
$source = $root . '/' . $user . '/files_encryption/OC_DEFAULT_MODULE';
if ($this->rootView->is_dir($source)) {
- $this->traverseKeys($source, $user);
+ $this->traverseKeys($source, $user, $output);
}
$source = $root . '/' . $user . '/files_encryption/keys';
if ($this->rootView->is_dir($source)) {
- $this->traverseFileKeys($source);
+ $this->traverseFileKeys($source, $output);
}
}
}
diff --git a/core/Command/Encryption/SetDefaultModule.php b/core/Command/Encryption/SetDefaultModule.php
index 789138fac95..d10872afd38 100644
--- a/core/Command/Encryption/SetDefaultModule.php
+++ b/core/Command/Encryption/SetDefaultModule.php
@@ -39,8 +39,8 @@ class SetDefaultModule extends Command {
protected function execute(InputInterface $input, OutputInterface $output): int {
$isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
if ($isMaintenanceModeEnabled) {
- $output->writeln("Maintenance mode must be disabled when setting default module,");
- $output->writeln("in order to load the relevant encryption modules correctly.");
+ $output->writeln('Maintenance mode must be disabled when setting default module,');
+ $output->writeln('in order to load the relevant encryption modules correctly.');
return 1;
}