Skip to content

Commit

Permalink
Merge pull request #44555 from nextcloud/fix/handle-errors-in-migrate…
Browse files Browse the repository at this point in the history
…-key-format
  • Loading branch information
Altahrim authored Dec 4, 2024
2 parents 416c8ea + 1d80351 commit 811f10e
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions core/Command/Encryption/MigrateKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -50,15 +50,12 @@ 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 {
$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 @@ -67,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) {
private function traverseKeys(string $folder, ?string $uid, OutputInterface $output): void {
$listing = $this->rootView->getDirectoryContent($folder);

foreach ($listing as $node) {
Expand All @@ -91,6 +88,11 @@ private function traverseKeys(string $folder, ?string $uid) {

$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 @@ -109,14 +111,14 @@ private function traverseKeys(string $folder, ?string $uid) {
}
}

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;
Expand All @@ -133,6 +135,11 @@ private function traverseFileKeys(string $folder) {

$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 @@ -154,22 +161,17 @@ 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);
}


/**
* 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();

Expand All @@ -181,7 +183,7 @@ protected function updateUsersKeys(string $root, OutputInterface $output) {
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 @@ -192,20 +194,18 @@ 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, 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 811f10e

Please sign in to comment.