Skip to content

Commit

Permalink
Refactors encryption app commands.
Browse files Browse the repository at this point in the history
To improve code readability.

Signed-off-by: Faraz Samapoor <fsa@adlas.at>
  • Loading branch information
Faraz Samapoor committed Aug 3, 2023
1 parent 90bb0a2 commit dd36d5f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 176 deletions.
32 changes: 8 additions & 24 deletions apps/encryption/lib/Command/DisableMasterKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions apps/encryption/lib/Command/DropLegacyFileKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 8 additions & 24 deletions apps/encryption/lib/Command/EnableMasterKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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;
}
}
59 changes: 16 additions & 43 deletions apps/encryption/lib/Command/FixEncryptedVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,16 @@
use Symfony\Component\Console\Output\OutputInterface;

class FixEncryptedVersion extends Command {
/** @var IConfig */
private $config;

/** @var ILogger */
private $logger;

/** @var IRootFolder */
private $rootFolder;

/** @var IUserManager */
private $userManager;

/** @var Util */
private $util;

/** @var View */
private $view;

/** @var bool */
private $supportLegacy;
private bool $supportLegacy = false;

public function __construct(
IConfig $config,
ILogger $logger,
IRootFolder $rootFolder,
IUserManager $userManager,
Util $util,
View $view
private IConfig $config,
private ILogger $logger,
private IRootFolder $rootFolder,
private IUserManager $userManager,
private Util $util,
private View $view,
) {
$this->config = $config;
$this->logger = $logger;
$this->rootFolder = $rootFolder;
$this->userManager = $userManager;
$this->util = $util;
$this->view = $view;
$this->supportLegacy = false;

parent::__construct();
}

Expand Down Expand Up @@ -108,12 +81,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if ($skipSignatureCheck) {
$output->writeln("<error>Repairing is not possible when \"encryption_skip_signature_check\" is set. Please disable this flag in the configuration.</error>\n");
return 1;
return self::FAILURE;
}

if (!$this->util->isMasterKeyEnabled()) {
$output->writeln("<error>Repairing only works with master key encryption.</error>\n");
return 1;
return self::FAILURE;
}

$user = $input->getArgument('user');
Expand All @@ -123,26 +96,26 @@ 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("<error>User id $user does not exist. Please provide a valid user id</error>");
return 1;
return self::FAILURE;
}

return $this->runForUser($user, $pathOption, $output);
} elseif ($all) {
$result = 0;
$this->userManager->callForSeenUsers(function(IUser $user) use ($pathOption, $output, &$result) {
$this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) {
$output->writeln("Processing files for " . $user->getUID());
$result = $this->runForUser($user->getUID(), $pathOption, $output);
return $result === 0;
});
return $result;
} else {
$output->writeln("Either a user id or --all needs to be provided");
return 1;
return self::FAILURE;
}
}

Expand All @@ -161,13 +134,13 @@ private function walkPathOfUser(string $user, string $path, OutputInterface $out
$this->setupUserFs($user);
if (!$this->view->file_exists($path)) {
$output->writeln("<error>Path \"$path\" does not exist. Please provide a valid path.</error>");
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;
Expand All @@ -183,7 +156,7 @@ private function walkPathOfUser(string $user, string $path, OutputInterface $out
}
}
}
return 0;
return self::SUCCESS;
}

/**
Expand Down
29 changes: 8 additions & 21 deletions apps/encryption/lib/Command/FixKeyLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -75,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$user = $this->userManager->get($userId);
if (!$user) {
$output->writeln("<error>User $userId not found</error>");
return 1;
return self::FAILURE;
}

\OC_Util::setupFS($user->getUID());
Expand Down Expand Up @@ -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 {
Expand All @@ -121,7 +117,6 @@ private function getSystemMountsForUser(IUser $user): array {
}

/**
* @param Folder $folder
* @return \Generator<File>
*/
private function getAllFiles(Folder $folder) {
Expand All @@ -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), '/');
Expand All @@ -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), '/');
Expand Down
39 changes: 11 additions & 28 deletions apps/encryption/lib/Command/RecoverUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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: ');
Expand All @@ -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;
}
}
Loading

0 comments on commit dd36d5f

Please sign in to comment.