Skip to content

Commit

Permalink
fix: Show errors in encryption:migrate-key-storage-format and continu…
Browse files Browse the repository at this point in the history
…e to other files

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Mar 28, 2024
1 parent 310c8cb commit 669775f
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions core/Command/Encryption/MigrateKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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;
Expand All @@ -81,15 +81,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) {
Expand All @@ -105,6 +105,11 @@ private function traverseKeys(string $folder, ?string $uid): void {

$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;
Expand All @@ -123,12 +128,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);
Expand All @@ -147,6 +152,11 @@ private function traverseFileKeys(string $folder): void {

$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;
Expand Down Expand Up @@ -190,7 +200,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);
Expand All @@ -203,16 +213,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);
}
}
}
Expand Down

0 comments on commit 669775f

Please sign in to comment.