-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add an occ command to scan files for legacy file key in use and get rid of those #38080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5663f9b
Add an occ command to scan files for legacy file key in use and get r…
come-nc 146284f
Fix fopen mode
come-nc c9c49bf
Log failures to delete legacy file key
come-nc 725403c
Copy and move files to migrate them to the new key
come-nc 36fc5dc
Copy data back instead of renaming to avoid changing the fileid
come-nc a92028f
Rename command to drop-legacy-filekey and remove comment about legacy…
come-nc 4910888
Add fclose on opened resources
come-nc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2023, Côme Chilliet <[email protected]> | ||
| * | ||
| * @author Côme Chilliet <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
| * 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. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * 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. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| namespace OCA\Encryption\Command; | ||
|
|
||
| use OC\Encryption\Exceptions\DecryptionFailedException; | ||
| use OC\Files\FileInfo; | ||
| use OC\Files\View; | ||
| use OCA\Encryption\KeyManager; | ||
| use OCP\Encryption\Exceptions\GenericEncryptionException; | ||
| use OCP\IUserManager; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class DropLegacyFileKey extends Command { | ||
| private View $rootView; | ||
|
|
||
| public function __construct( | ||
| private IUserManager $userManager, | ||
| private KeyManager $keyManager, | ||
| ) { | ||
| parent::__construct(); | ||
|
|
||
| $this->rootView = new View(); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| $this | ||
| ->setName('encryption:drop-legacy-filekey') | ||
| ->setDescription('Scan the files for the legacy filekey format using RC4 and get rid of it (if master key is enabled)'); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| $result = true; | ||
|
|
||
| $output->writeln('<info>Scanning all files for legacy filekey</info>'); | ||
|
|
||
| foreach ($this->userManager->getBackends() as $backend) { | ||
| $limit = 500; | ||
| $offset = 0; | ||
| do { | ||
| $users = $backend->getUsers('', $limit, $offset); | ||
| foreach ($users as $user) { | ||
| $output->writeln('Scanning all files for ' . $user); | ||
| $this->setupUserFS($user); | ||
| $result = $result && $this->scanFolder($output, '/' . $user); | ||
| } | ||
| $offset += $limit; | ||
| } while (count($users) >= $limit); | ||
| } | ||
|
|
||
| if ($result) { | ||
| $output->writeln('All scanned files are properly encrypted.'); | ||
| return 0; | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| private function scanFolder(OutputInterface $output, string $folder): bool { | ||
| $clean = true; | ||
|
|
||
| foreach ($this->rootView->getDirectoryContent($folder) as $item) { | ||
| $path = $folder . '/' . $item['name']; | ||
| if ($this->rootView->is_dir($path)) { | ||
| if ($this->scanFolder($output, $path) === false) { | ||
| $clean = false; | ||
| } | ||
| } else { | ||
| if (!$item->isEncrypted()) { | ||
| // ignore | ||
| continue; | ||
| } | ||
|
|
||
| $stats = $this->rootView->stat($path); | ||
| if (!isset($stats['hasHeader']) || $stats['hasHeader'] === false) { | ||
| $clean = false; | ||
| $output->writeln('<error>' . $path . ' does not have a proper header</error>'); | ||
| } else { | ||
| try { | ||
| $legacyFileKey = $this->keyManager->getFileKey($path, null, true); | ||
| if ($legacyFileKey === '') { | ||
| $output->writeln('Got an empty legacy filekey for ' . $path . ', continuing', OutputInterface::VERBOSITY_VERBOSE); | ||
| continue; | ||
| } | ||
| } catch (GenericEncryptionException $e) { | ||
| $output->writeln('Got a decryption error for legacy filekey for ' . $path . ', continuing', OutputInterface::VERBOSITY_VERBOSE); | ||
| continue; | ||
| } | ||
| /* If that did not throw and filekey is not empty, a legacy filekey is used */ | ||
| $clean = false; | ||
| $output->writeln($path . ' is using a legacy filekey, migrating'); | ||
| $this->migrateSinglefile($path, $item, $output); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $clean; | ||
| } | ||
|
|
||
| private function migrateSinglefile(string $path, FileInfo $fileInfo, OutputInterface $output): void { | ||
| $source = $path; | ||
| $target = $path . '.reencrypted.' . time(); | ||
|
|
||
| try { | ||
| $this->rootView->copy($source, $target); | ||
| $copyResource = $this->rootView->fopen($target, 'r'); | ||
| $sourceResource = $this->rootView->fopen($source, 'w'); | ||
| if ($copyResource === false || $sourceResource === false) { | ||
| throw new DecryptionFailedException('Failed to open '.$source.' or '.$target); | ||
| } | ||
| if (stream_copy_to_stream($copyResource, $sourceResource) === false) { | ||
| $output->writeln('<error>Failed to copy '.$target.' data into '.$source.'</error>'); | ||
| $output->writeln('<error>Leaving both files in there to avoid data loss</error>'); | ||
| return; | ||
| } | ||
| $this->rootView->touch($source, $fileInfo->getMTime()); | ||
| $this->rootView->unlink($target); | ||
| $output->writeln('<info>Migrated ' . $source . '</info>', OutputInterface::VERBOSITY_VERBOSE); | ||
| } catch (DecryptionFailedException $e) { | ||
| if ($this->rootView->file_exists($target)) { | ||
| $this->rootView->unlink($target); | ||
| } | ||
| $output->writeln('<error>Failed to migrate ' . $path . '</error>'); | ||
| $output->writeln('<error>' . $e . '</error>', OutputInterface::VERBOSITY_VERBOSE); | ||
| } finally { | ||
| if (is_resource($copyResource)) { | ||
Check noticeCode scanning / Psalm RedundantConditionGivenDocblockType
Docblock-defined type resource for $copyResource is always resource
Check noticeCode scanning / Psalm PossiblyUndefinedVariable
Possibly undefined variable $copyResource defined in try block
|
||
| fclose($copyResource); | ||
| } | ||
| if (is_resource($sourceResource)) { | ||
Check noticeCode scanning / Psalm RedundantConditionGivenDocblockType
Docblock-defined type resource for $sourceResource is always resource
Check noticeCode scanning / Psalm PossiblyUndefinedVariable
Possibly undefined variable $sourceResource defined in try block
|
||
| fclose($sourceResource); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * setup user file system | ||
| */ | ||
| protected function setupUserFS(string $uid): void { | ||
| \OC_Util::tearDownFS(); | ||
| \OC_Util::setupFS($uid); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.