From 73b42a9a05f660a5e13d7a73e0eb96ba3012d8f4 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Thu, 3 Aug 2023 14:36:40 +0330 Subject: [PATCH] Refactors encryption app commands. To improve code readability. Signed-off-by: Faraz Samapoor --- .../lib/Command/DisableMasterKey.php | 32 ++++--------- .../lib/Command/DropLegacyFileKey.php | 4 +- .../lib/Command/EnableMasterKey.php | 32 ++++--------- .../lib/Command/FixEncryptedVersion.php | 20 ++++---- .../encryption/lib/Command/FixKeyLocation.php | 29 ++++-------- apps/encryption/lib/Command/RecoverUser.php | 39 +++++----------- .../lib/Command/ScanLegacyFormat.php | 46 +++++-------------- 7 files changed, 58 insertions(+), 144 deletions(-) diff --git a/apps/encryption/lib/Command/DisableMasterKey.php b/apps/encryption/lib/Command/DisableMasterKey.php index 6000d6021c8bc..5fc8878c6b5e3 100644 --- a/apps/encryption/lib/Command/DisableMasterKey.php +++ b/apps/encryption/lib/Command/DisableMasterKey.php @@ -33,31 +33,15 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; class DisableMasterKey extends Command { - - /** @var Util */ - protected $util; - - /** @var IConfig */ - protected $config; - - /** @var QuestionHelper */ - protected $questionHelper; - - /** - * @param Util $util - * @param IConfig $config - * @param QuestionHelper $questionHelper - */ - public function __construct(Util $util, - IConfig $config, - QuestionHelper $questionHelper) { - $this->util = $util; - $this->config = $config; - $this->questionHelper = $questionHelper; + public function __construct( + protected Util $util, + protected IConfig $config, + protected QuestionHelper $questionHelper, + ) { parent::__construct(); } - protected function configure() { + protected function configure(): void { $this ->setName('encryption:disable-master-key') ->setDescription('Disable the master key and use per-user keys instead. Only available for fresh installations with no existing encrypted data! There is no way to enable it again.'); @@ -80,9 +64,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('Master key successfully disabled.'); } else { $output->writeln('aborted.'); - return 1; + return self::FAILURE; } } - return 0; + return self::SUCCESS; } } diff --git a/apps/encryption/lib/Command/DropLegacyFileKey.php b/apps/encryption/lib/Command/DropLegacyFileKey.php index f0a5f36f30fee..449fbfa968439 100644 --- a/apps/encryption/lib/Command/DropLegacyFileKey.php +++ b/apps/encryption/lib/Command/DropLegacyFileKey.php @@ -75,10 +75,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($result) { $output->writeln('All scanned files are properly encrypted.'); - return 0; + return self::SUCCESS; } - return 1; + return self::FAILURE; } private function scanFolder(OutputInterface $output, string $folder): bool { diff --git a/apps/encryption/lib/Command/EnableMasterKey.php b/apps/encryption/lib/Command/EnableMasterKey.php index 031f6e3fa4e8d..44c3a7a72833f 100644 --- a/apps/encryption/lib/Command/EnableMasterKey.php +++ b/apps/encryption/lib/Command/EnableMasterKey.php @@ -32,31 +32,15 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; class EnableMasterKey extends Command { - - /** @var Util */ - protected $util; - - /** @var IConfig */ - protected $config; - - /** @var QuestionHelper */ - protected $questionHelper; - - /** - * @param Util $util - * @param IConfig $config - * @param QuestionHelper $questionHelper - */ - public function __construct(Util $util, - IConfig $config, - QuestionHelper $questionHelper) { - $this->util = $util; - $this->config = $config; - $this->questionHelper = $questionHelper; + public function __construct( + protected Util $util, + protected IConfig $config, + protected QuestionHelper $questionHelper, + ) { parent::__construct(); } - protected function configure() { + protected function configure(): void { $this ->setName('encryption:enable-master-key') ->setDescription('Enable the master key. Only available for fresh installations with no existing encrypted data! There is also no way to disable it again.'); @@ -76,9 +60,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('Master key successfully enabled.'); } else { $output->writeln('aborted.'); - return 1; + return self::FAILURE; } } - return 0; + return self::SUCCESS; } } diff --git a/apps/encryption/lib/Command/FixEncryptedVersion.php b/apps/encryption/lib/Command/FixEncryptedVersion.php index d0fbf1adb31b4..2a032fb9c0752 100644 --- a/apps/encryption/lib/Command/FixEncryptedVersion.php +++ b/apps/encryption/lib/Command/FixEncryptedVersion.php @@ -39,7 +39,7 @@ use Symfony\Component\Console\Output\OutputInterface; class FixEncryptedVersion extends Command { - private bool $supportLegacy; + private bool $supportLegacy = false; public function __construct( private IConfig $config, @@ -49,8 +49,6 @@ public function __construct( private Util $util, private View $view, ) { - $this->supportLegacy = false; - parent::__construct(); } @@ -83,12 +81,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($skipSignatureCheck) { $output->writeln("Repairing is not possible when \"encryption_skip_signature_check\" is set. Please disable this flag in the configuration.\n"); - return 1; + return self::FAILURE; } if (!$this->util->isMasterKeyEnabled()) { $output->writeln("Repairing only works with master key encryption.\n"); - return 1; + return self::FAILURE; } $user = $input->getArgument('user'); @@ -98,12 +96,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($user) { if ($all) { $output->writeln("Specifying a user id and --all are mutually exclusive"); - return 1; + return self::FAILURE; } if ($this->userManager->get($user) === null) { $output->writeln("User id $user does not exist. Please provide a valid user id"); - return 1; + return self::FAILURE; } return $this->runForUser($user, $pathOption, $output); @@ -117,7 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $result; } else { $output->writeln("Either a user id or --all needs to be provided"); - return 1; + return self::FAILURE; } } @@ -136,13 +134,13 @@ private function walkPathOfUser(string $user, string $path, OutputInterface $out $this->setupUserFs($user); if (!$this->view->file_exists($path)) { $output->writeln("Path \"$path\" does not exist. Please provide a valid path."); - return 1; + return self::FAILURE; } if ($this->view->is_file($path)) { $output->writeln("Verifying the content of file \"$path\""); $this->verifyFileContent($path, $output); - return 0; + return self::SUCCESS; } $directories = []; $directories[] = $path; @@ -158,7 +156,7 @@ private function walkPathOfUser(string $user, string $path, OutputInterface $out } } } - return 0; + return self::SUCCESS; } /** diff --git a/apps/encryption/lib/Command/FixKeyLocation.php b/apps/encryption/lib/Command/FixKeyLocation.php index 5339247ae1974..51b9e688c0255 100644 --- a/apps/encryption/lib/Command/FixKeyLocation.php +++ b/apps/encryption/lib/Command/FixKeyLocation.php @@ -40,18 +40,15 @@ use Symfony\Component\Console\Output\OutputInterface; class FixKeyLocation extends Command { - private IUserManager $userManager; - private IUserMountCache $userMountCache; - private Util $encryptionUtil; - private IRootFolder $rootFolder; private string $keyRootDirectory; private View $rootView; - public function __construct(IUserManager $userManager, IUserMountCache $userMountCache, Util $encryptionUtil, IRootFolder $rootFolder) { - $this->userManager = $userManager; - $this->userMountCache = $userMountCache; - $this->encryptionUtil = $encryptionUtil; - $this->rootFolder = $rootFolder; + public function __construct( + private IUserManager $userManager, + private IUserMountCache $userMountCache, + private Util $encryptionUtil, + private IRootFolder $rootFolder, + ) { $this->keyRootDirectory = rtrim($this->encryptionUtil->getKeyStorageRoot(), '/'); $this->rootView = new View(); @@ -75,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $user = $this->userManager->get($userId); if (!$user) { $output->writeln("User $userId not found"); - return 1; + return self::FAILURE; } \OC_Util::setupFS($user->getUID()); @@ -106,11 +103,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } - return 0; + return self::SUCCESS; } /** - * @param IUser $user * @return ICachedMountInfo[] */ private function getSystemMountsForUser(IUser $user): array { @@ -121,7 +117,6 @@ private function getSystemMountsForUser(IUser $user): array { } /** - * @param Folder $folder * @return \Generator */ private function getAllFiles(Folder $folder) { @@ -136,10 +131,6 @@ private function getAllFiles(Folder $folder) { /** * Check if the key for a file is stored in the user's keystore and not the system one - * - * @param IUser $user - * @param Node $node - * @return bool */ private function isKeyStoredForUser(IUser $user, Node $node): bool { $path = trim(substr($node->getPath(), strlen($user->getUID()) + 1), '/'); @@ -154,10 +145,6 @@ private function isKeyStoredForUser(IUser $user, Node $node): bool { /** * Check that the user key stored for a file can decrypt the file - * - * @param IUser $user - * @param File $node - * @return bool */ private function copyKeyAndValidate(IUser $user, File $node): bool { $path = trim(substr($node->getPath(), strlen($user->getUID()) + 1), '/'); diff --git a/apps/encryption/lib/Command/RecoverUser.php b/apps/encryption/lib/Command/RecoverUser.php index d3dd4a3612d37..01d4047f8f145 100644 --- a/apps/encryption/lib/Command/RecoverUser.php +++ b/apps/encryption/lib/Command/RecoverUser.php @@ -35,33 +35,16 @@ use Symfony\Component\Console\Question\Question; class RecoverUser extends Command { - - /** @var Util */ - protected $util; - - /** @var IUserManager */ - protected $userManager; - - /** @var QuestionHelper */ - protected $questionHelper; - - /** - * @param Util $util - * @param IConfig $config - * @param IUserManager $userManager - * @param QuestionHelper $questionHelper - */ - public function __construct(Util $util, - IConfig $config, - IUserManager $userManager, - QuestionHelper $questionHelper) { - $this->util = $util; - $this->questionHelper = $questionHelper; - $this->userManager = $userManager; + public function __construct( + protected Util $util, + IConfig $config, + protected IUserManager $userManager, + protected QuestionHelper $questionHelper, + ) { parent::__construct(); } - protected function configure() { + protected function configure(): void { $this ->setName('encryption:recover-user') ->setDescription('Recover user data in case of password lost. This only works if the user enabled the recovery key.'); @@ -78,20 +61,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($isMasterKeyEnabled) { $output->writeln('You use the master key, no individual user recovery needed.'); - return 0; + return self::SUCCESS; } $uid = $input->getArgument('user'); $userExists = $this->userManager->userExists($uid); if ($userExists === false) { $output->writeln('User "' . $uid . '" unknown.'); - return 1; + return self::FAILURE; } $recoveryKeyEnabled = $this->util->isRecoveryEnabledForUser($uid); if ($recoveryKeyEnabled === false) { $output->writeln('Recovery key is not enabled for: ' . $uid); - return 1; + return self::FAILURE; } $question = new Question('Please enter the recovery key password: '); @@ -107,6 +90,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->write('Start to recover users files... This can take some time...'); $this->userManager->get($uid)->setPassword($newLoginPassword, $recoveryPassword); $output->writeln('Done.'); - return 0; + return self::SUCCESS; } } diff --git a/apps/encryption/lib/Command/ScanLegacyFormat.php b/apps/encryption/lib/Command/ScanLegacyFormat.php index 85a99a178453c..7cbbcfcec966e 100644 --- a/apps/encryption/lib/Command/ScanLegacyFormat.php +++ b/apps/encryption/lib/Command/ScanLegacyFormat.php @@ -36,40 +36,20 @@ use Symfony\Component\Console\Output\OutputInterface; class ScanLegacyFormat extends Command { - /** @var Util */ - protected $util; - - /** @var IConfig */ - protected $config; - - /** @var QuestionHelper */ - protected $questionHelper; - - /** @var IUserManager */ - private $userManager; - - /** @var View */ - private $rootView; - - /** - * @param Util $util - * @param IConfig $config - * @param QuestionHelper $questionHelper - */ - public function __construct(Util $util, - IConfig $config, - QuestionHelper $questionHelper, - IUserManager $userManager) { + private View $rootView; + + public function __construct( + protected Util $util, + protected IConfig $config, + protected QuestionHelper $questionHelper, + private IUserManager $userManager, + ) { parent::__construct(); - $this->util = $util; - $this->config = $config; - $this->questionHelper = $questionHelper; - $this->userManager = $userManager; $this->rootView = new View(); } - protected function configure() { + protected function configure(): void { $this ->setName('encryption:scan:legacy-format') ->setDescription('Scan the files for the legacy format'); @@ -96,10 +76,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($result) { $output->writeln('All scanned files are properly encrypted. You can disable the legacy compatibility mode.'); - return 0; + return self::SUCCESS; } - return 1; + return self::FAILURE; } private function scanFolder(OutputInterface $output, string $folder): bool { @@ -130,10 +110,8 @@ private function scanFolder(OutputInterface $output, string $folder): bool { /** * setup user file system - * - * @param string $uid */ - protected function setupUserFS($uid) { + protected function setupUserFS(string $uid): void { \OC_Util::tearDownFS(); \OC_Util::setupFS($uid); }