Merge pull request #15616 from owncloud/enc_stream_copy
always create a new instance of the encryption module
This commit is contained in:
commit
aef6ecdbf1
|
@ -27,6 +27,7 @@ namespace OCA\Encryption\AppInfo;
|
||||||
use OC\Files\Filesystem;
|
use OC\Files\Filesystem;
|
||||||
use OC\Files\View;
|
use OC\Files\View;
|
||||||
use OCA\Encryption\Crypto\Crypt;
|
use OCA\Encryption\Crypto\Crypt;
|
||||||
|
use OCA\Encryption\Crypto\Encryption;
|
||||||
use OCA\Encryption\HookManager;
|
use OCA\Encryption\HookManager;
|
||||||
use OCA\Encryption\Hooks\UserHooks;
|
use OCA\Encryption\Hooks\UserHooks;
|
||||||
use OCA\Encryption\KeyManager;
|
use OCA\Encryption\KeyManager;
|
||||||
|
@ -90,14 +91,17 @@ class Application extends \OCP\AppFramework\App {
|
||||||
|
|
||||||
public function registerEncryptionModule() {
|
public function registerEncryptionModule() {
|
||||||
$container = $this->getContainer();
|
$container = $this->getContainer();
|
||||||
$container->registerService('EncryptionModule', function (IAppContainer $c) {
|
|
||||||
return new \OCA\Encryption\Crypto\Encryption(
|
$this->encryptionManager->registerEncryptionModule(
|
||||||
$c->query('Crypt'),
|
Encryption::ID,
|
||||||
$c->query('KeyManager'),
|
Encryption::DISPLAY_NAME,
|
||||||
$c->query('Util'));
|
function() use ($container) {
|
||||||
|
return new Encryption(
|
||||||
|
$container->query('Crypt'),
|
||||||
|
$container->query('KeyManager'),
|
||||||
|
$container->query('Util')
|
||||||
|
);
|
||||||
});
|
});
|
||||||
$module = $container->query('EncryptionModule');
|
|
||||||
$this->encryptionManager->registerEncryptionModule($module);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerServices() {
|
public function registerServices() {
|
||||||
|
|
|
@ -32,6 +32,7 @@ use OCA\Encryption\KeyManager;
|
||||||
class Encryption implements IEncryptionModule {
|
class Encryption implements IEncryptionModule {
|
||||||
|
|
||||||
const ID = 'OC_DEFAULT_MODULE';
|
const ID = 'OC_DEFAULT_MODULE';
|
||||||
|
const DISPLAY_NAME = 'ownCloud Default Encryption';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Crypt
|
* @var Crypt
|
||||||
|
@ -90,7 +91,7 @@ class Encryption implements IEncryptionModule {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getDisplayName() {
|
public function getDisplayName() {
|
||||||
return 'ownCloud Default Encryption';
|
return self::DISPLAY_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -87,17 +87,17 @@ class Manager implements IManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers an encryption module
|
* Registers an callback function which must return an encryption module instance
|
||||||
*
|
*
|
||||||
* @param IEncryptionModule $module
|
* @param string $id
|
||||||
|
* @param string $displayName
|
||||||
|
* @param callable $callback
|
||||||
* @throws Exceptions\ModuleAlreadyExistsException
|
* @throws Exceptions\ModuleAlreadyExistsException
|
||||||
*/
|
*/
|
||||||
public function registerEncryptionModule(IEncryptionModule $module) {
|
public function registerEncryptionModule($id, $displayName, callable $callback) {
|
||||||
$id = $module->getId();
|
|
||||||
$name = $module->getDisplayName();
|
|
||||||
|
|
||||||
if (isset($this->encryptionModules[$id])) {
|
if (isset($this->encryptionModules[$id])) {
|
||||||
throw new Exceptions\ModuleAlreadyExistsException($id, $name);
|
throw new Exceptions\ModuleAlreadyExistsException($id, $displayName);
|
||||||
}
|
}
|
||||||
|
|
||||||
$defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
|
$defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
|
||||||
|
@ -106,22 +106,26 @@ class Manager implements IManager {
|
||||||
$this->setDefaultEncryptionModule($id);
|
$this->setDefaultEncryptionModule($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->encryptionModules[$id] = $module;
|
$this->encryptionModules[$id] = array(
|
||||||
|
'id' => $id,
|
||||||
|
'displayName' => 'displayName',
|
||||||
|
'callback' => $callback
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters an encryption module
|
* Unregisters an encryption module
|
||||||
*
|
*
|
||||||
* @param IEncryptionModule $module
|
* @param string $moduleId
|
||||||
*/
|
*/
|
||||||
public function unregisterEncryptionModule(IEncryptionModule $module) {
|
public function unregisterEncryptionModule($moduleId) {
|
||||||
unset($this->encryptionModules[$module->getId()]);
|
unset($this->encryptionModules[$moduleId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get a list of all encryption modules
|
* get a list of all encryption modules
|
||||||
*
|
*
|
||||||
* @return IEncryptionModule[]
|
* @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]]
|
||||||
*/
|
*/
|
||||||
public function getEncryptionModules() {
|
public function getEncryptionModules() {
|
||||||
return $this->encryptionModules;
|
return $this->encryptionModules;
|
||||||
|
@ -137,21 +141,13 @@ class Manager implements IManager {
|
||||||
public function getEncryptionModule($moduleId = '') {
|
public function getEncryptionModule($moduleId = '') {
|
||||||
if (!empty($moduleId)) {
|
if (!empty($moduleId)) {
|
||||||
if (isset($this->encryptionModules[$moduleId])) {
|
if (isset($this->encryptionModules[$moduleId])) {
|
||||||
return $this->encryptionModules[$moduleId];
|
return call_user_func($this->encryptionModules[$moduleId]['callback']);
|
||||||
} else {
|
} else {
|
||||||
$message = "Module with id: $moduleId does not exists.";
|
$message = "Module with id: $moduleId does not exists.";
|
||||||
throw new Exceptions\ModuleDoesNotExistsException($message);
|
throw new Exceptions\ModuleDoesNotExistsException($message);
|
||||||
}
|
}
|
||||||
} else { // get default module and return this
|
|
||||||
// For now we simply return the first module until we have a way
|
|
||||||
// to enable multiple modules and define a default module
|
|
||||||
$module = reset($this->encryptionModules);
|
|
||||||
if ($module) {
|
|
||||||
return $module;
|
|
||||||
} else {
|
} else {
|
||||||
$message = 'No encryption module registered';
|
return $this->getDefaultEncryptionModule();
|
||||||
throw new Exceptions\ModuleDoesNotExistsException($message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +161,7 @@ class Manager implements IManager {
|
||||||
$defaultModuleId = $this->getDefaultEncryptionModuleId();
|
$defaultModuleId = $this->getDefaultEncryptionModuleId();
|
||||||
if (!empty($defaultModuleId)) {
|
if (!empty($defaultModuleId)) {
|
||||||
if (isset($this->encryptionModules[$defaultModuleId])) {
|
if (isset($this->encryptionModules[$defaultModuleId])) {
|
||||||
return $this->encryptionModules[$defaultModuleId];
|
return call_user_func($this->encryptionModules[$defaultModuleId]['callback']);
|
||||||
} else {
|
} else {
|
||||||
$message = 'Default encryption module not loaded';
|
$message = 'Default encryption module not loaded';
|
||||||
throw new Exceptions\ModuleDoesNotExistsException($message);
|
throw new Exceptions\ModuleDoesNotExistsException($message);
|
||||||
|
|
|
@ -40,26 +40,28 @@ interface IManager {
|
||||||
function isEnabled();
|
function isEnabled();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers an encryption module
|
* Registers an callback function which must return an encryption module instance
|
||||||
*
|
*
|
||||||
* @param IEncryptionModule $module
|
* @param string $id
|
||||||
|
* @param string $displayName
|
||||||
|
* @param callable $callback
|
||||||
* @throws ModuleAlreadyExistsException
|
* @throws ModuleAlreadyExistsException
|
||||||
* @since 8.1.0
|
* @since 8.1.0
|
||||||
*/
|
*/
|
||||||
function registerEncryptionModule(IEncryptionModule $module);
|
function registerEncryptionModule($id, $displayName, callable $callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters an encryption module
|
* Unregisters an encryption module
|
||||||
*
|
*
|
||||||
* @param IEncryptionModule $module
|
* @param string $moduleId
|
||||||
* @since 8.1.0
|
* @since 8.1.0
|
||||||
*/
|
*/
|
||||||
function unregisterEncryptionModule(IEncryptionModule $module);
|
function unregisterEncryptionModule($moduleId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get a list of all encryption modules
|
* get a list of all encryption modules
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]]
|
||||||
* @since 8.1.0
|
* @since 8.1.0
|
||||||
*/
|
*/
|
||||||
function getEncryptionModules();
|
function getEncryptionModules();
|
||||||
|
|
|
@ -96,10 +96,10 @@ try {
|
||||||
}
|
}
|
||||||
$encModulues = array();
|
$encModulues = array();
|
||||||
foreach ($encryptionModules as $module) {
|
foreach ($encryptionModules as $module) {
|
||||||
$encModulues[$module->getId()]['displayName'] = $module->getDisplayName();
|
$encModulues[$module['id']]['displayName'] = $module['displayName'];
|
||||||
$encModulues[$module->getId()]['default'] = false;
|
$encModulues[$module['id']]['default'] = false;
|
||||||
if ($defaultEncryptionModule && $module->getId() === $defaultEncryptionModuleId) {
|
if ($defaultEncryptionModule && $module['id'] === $defaultEncryptionModuleId) {
|
||||||
$encModulues[$module->getId()]['default'] = true;
|
$encModulues[$module['id']]['default'] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$template->assign('encryptionModules', $encModulues);
|
$template->assign('encryptionModules', $encModulues);
|
||||||
|
|
|
@ -36,9 +36,9 @@ class ManagerTest extends TestCase {
|
||||||
public function testManagerIsDisabledIfDisabledButModules() {
|
public function testManagerIsDisabledIfDisabledButModules() {
|
||||||
$this->config->expects($this->any())->method('getAppValue')->willReturn(false);
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(false);
|
||||||
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
||||||
$em->expects($this->any())->method('getId')->willReturn(0);
|
$em->expects($this->any())->method('getId')->willReturn('id');
|
||||||
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
||||||
$this->manager->registerEncryptionModule($em);
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function() use ($em) {return $em;});
|
||||||
$this->assertFalse($this->manager->isEnabled());
|
$this->assertFalse($this->manager->isEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,29 +50,32 @@ class ManagerTest extends TestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
|
* @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
|
||||||
* @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0"
|
* @expectedExceptionMessage Id "id" already used by encryption module "TestDummyModule0"
|
||||||
*/
|
*/
|
||||||
public function testModuleRegistration() {
|
public function testModuleRegistration() {
|
||||||
$this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
|
$this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
|
||||||
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
||||||
$em->expects($this->any())->method('getId')->willReturn(0);
|
$em->expects($this->any())->method('getId')->willReturn('id');
|
||||||
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
||||||
$this->manager->registerEncryptionModule($em);
|
|
||||||
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
|
||||||
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
||||||
$this->manager->registerEncryptionModule($em);
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testModuleUnRegistration() {
|
public function testModuleUnRegistration() {
|
||||||
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
||||||
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
||||||
$em->expects($this->any())->method('getId')->willReturn(0);
|
$em->expects($this->any())->method('getId')->willReturn('id');
|
||||||
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
||||||
$this->manager->registerEncryptionModule($em);
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
|
||||||
$this->assertSame(1,
|
$this->assertSame(1,
|
||||||
count($this->manager->getEncryptionModules())
|
count($this->manager->getEncryptionModules())
|
||||||
);
|
);
|
||||||
$this->manager->unregisterEncryptionModule($em);
|
|
||||||
|
$this->manager->unregisterEncryptionModule('id');
|
||||||
$this->assertEmpty($this->manager->getEncryptionModules());
|
$this->assertEmpty($this->manager->getEncryptionModules());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,9 +85,9 @@ class ManagerTest extends TestCase {
|
||||||
public function testGetEncryptionModuleUnknown() {
|
public function testGetEncryptionModuleUnknown() {
|
||||||
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
||||||
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
||||||
$em->expects($this->any())->method('getId')->willReturn(0);
|
$em->expects($this->any())->method('getId')->willReturn('id');
|
||||||
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
||||||
$this->manager->registerEncryptionModule($em);
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
|
||||||
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
||||||
$this->manager->getEncryptionModule('unknown');
|
$this->manager->getEncryptionModule('unknown');
|
||||||
}
|
}
|
||||||
|
@ -92,23 +95,23 @@ class ManagerTest extends TestCase {
|
||||||
public function testGetEncryptionModule() {
|
public function testGetEncryptionModule() {
|
||||||
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
||||||
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
||||||
$em->expects($this->any())->method('getId')->willReturn(0);
|
$em->expects($this->any())->method('getId')->willReturn('id');
|
||||||
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
||||||
$this->manager->registerEncryptionModule($em);
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
|
||||||
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
||||||
$en0 = $this->manager->getEncryptionModule(0);
|
$en0 = $this->manager->getEncryptionModule('id');
|
||||||
$this->assertEquals(0, $en0->getId());
|
$this->assertEquals('id', $en0->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetDefaultEncryptionModule() {
|
public function testGetDefaultEncryptionModule() {
|
||||||
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
||||||
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
|
||||||
$em->expects($this->any())->method('getId')->willReturn(0);
|
$em->expects($this->any())->method('getId')->willReturn('id');
|
||||||
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
||||||
$this->manager->registerEncryptionModule($em);
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
|
||||||
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
$this->assertSame(1, count($this->manager->getEncryptionModules()));
|
||||||
$en0 = $this->manager->getEncryptionModule(0);
|
$en0 = $this->manager->getEncryptionModule('id');
|
||||||
$this->assertEquals(0, $en0->getId());
|
$this->assertEquals('id', $en0->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
// /**
|
||||||
|
|
Loading…
Reference in New Issue