From 6dfa09f36db0840225de2bf3845d7e2827feafcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 28 Mar 2024 17:26:38 +0100 Subject: [PATCH 1/2] chore: Improve typing in core/Command/Encryption/MigrateKeyStorage.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Encryption/MigrateKeyStorage.php | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/core/Command/Encryption/MigrateKeyStorage.php b/core/Command/Encryption/MigrateKeyStorage.php index ddd17eaa0b760..1c3cdcfa30f82 100644 --- a/core/Command/Encryption/MigrateKeyStorage.php +++ b/core/Command/Encryption/MigrateKeyStorage.php @@ -30,7 +30,7 @@ public function __construct( parent::__construct(); } - protected function configure() { + protected function configure(): void { parent::configure(); $this ->setName('encryption:migrate-key-storage-format') @@ -50,9 +50,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** * 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 { @@ -75,7 +72,7 @@ protected function updateSystemKeys(string $root): void { $this->traverseKeys($root . '/files_encryption', null); } - private function traverseKeys(string $folder, ?string $uid) { + private function traverseKeys(string $folder, ?string $uid): void { $listing = $this->rootView->getDirectoryContent($folder); foreach ($listing as $node) { @@ -109,14 +106,14 @@ private function traverseKeys(string $folder, ?string $uid) { } } - private function traverseFileKeys(string $folder) { + private function traverseFileKeys(string $folder): void { $listing = $this->rootView->getDirectoryContent($folder); foreach ($listing as $node) { if ($node['mimetype'] === 'httpd/unix-directory') { $this->traverseFileKeys($folder . '/' . $node['name']); } else { - $endsWith = function ($haystack, $needle) { + $endsWith = function (string $haystack, string $needle): bool { $length = strlen($needle); if ($length === 0) { return true; @@ -154,10 +151,8 @@ private function traverseFileKeys(string $folder) { /** * 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 +160,8 @@ protected function setupUserFS($uid) { /** * 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(); @@ -192,11 +184,9 @@ protected function updateUsersKeys(string $root, OutputInterface $output) { /** * 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): void { if ($this->userManager->userExists($user)) { $source = $root . '/' . $user . '/files_encryption/OC_DEFAULT_MODULE'; if ($this->rootView->is_dir($source)) { From 1d80351d4a8314e10f42523d303d799d64532718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 28 Mar 2024 17:30:22 +0100 Subject: [PATCH 2/2] fix: Show errors in encryption:migrate-key-storage-format and continue to other files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Encryption/MigrateKeyStorage.php | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/core/Command/Encryption/MigrateKeyStorage.php b/core/Command/Encryption/MigrateKeyStorage.php index 1c3cdcfa30f82..c2090d22d1c23 100644 --- a/core/Command/Encryption/MigrateKeyStorage.php +++ b/core/Command/Encryption/MigrateKeyStorage.php @@ -55,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int protected function updateKeys(string $root, OutputInterface $output): bool { $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; @@ -64,15 +64,15 @@ protected function updateKeys(string $root, OutputInterface $output): bool { /** * 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): void { + private function traverseKeys(string $folder, ?string $uid, OutputInterface $output): void { $listing = $this->rootView->getDirectoryContent($folder); foreach ($listing as $node) { @@ -88,6 +88,11 @@ private function traverseKeys(string $folder, ?string $uid): void { $content = $this->rootView->file_get_contents($path); + if ($content === false) { + $output->writeln("Failed to open path $path"); + continue; + } + try { $this->crypto->decrypt($content); continue; @@ -106,12 +111,12 @@ private function traverseKeys(string $folder, ?string $uid): void { } } - private function traverseFileKeys(string $folder): void { + 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 (string $haystack, string $needle): bool { $length = strlen($needle); @@ -130,6 +135,11 @@ private function traverseFileKeys(string $folder): void { $content = $this->rootView->file_get_contents($path); + if ($content === false) { + $output->writeln("Failed to open path $path"); + continue; + } + try { $this->crypto->decrypt($content); continue; @@ -173,7 +183,7 @@ protected function updateUsersKeys(string $root, OutputInterface $output): void foreach ($users as $user) { $progress->advance(); $this->setupUserFS($user); - $this->updateUserKeys($root, $user); + $this->updateUserKeys($root, $user, $output); } $offset += $limit; } while (count($users) >= $limit); @@ -186,16 +196,16 @@ protected function updateUsersKeys(string $root, OutputInterface $output): void * * @throws \Exception */ - protected function updateUserKeys(string $root, string $user): void { + 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); } } }