[Pkg-owncloud-commits] [owncloud] 146/215: skip update of encryption keys if file is not encrypted

David Prévot taffit at moszumanska.debian.org
Tue May 5 01:01:41 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch stable8
in repository owncloud.

commit df428b76ac498110bde0bfec1ad726cf24c21cfa
Author: Bjoern Schiessle <schiessle at owncloud.com>
Date:   Tue Apr 28 17:29:10 2015 +0200

    skip update of encryption keys if file is not encrypted
---
 apps/encryption/appinfo/application.php            |  3 +-
 apps/encryption/lib/crypto/encryption.php          | 39 ++++++++++++-----
 .../encryption/tests/lib/crypto/encryptionTest.php | 49 +++++++++++++++++++++-
 3 files changed, 78 insertions(+), 13 deletions(-)

diff --git a/apps/encryption/appinfo/application.php b/apps/encryption/appinfo/application.php
index fa62099..0c9dcb7 100644
--- a/apps/encryption/appinfo/application.php
+++ b/apps/encryption/appinfo/application.php
@@ -101,7 +101,8 @@ class Application extends \OCP\AppFramework\App {
 			return new Encryption(
 				$container->query('Crypt'),
 				$container->query('KeyManager'),
-				$container->query('Util')
+				$container->query('Util'),
+				$container->getServer()->getLogger()
 			);
 		});
 	}
diff --git a/apps/encryption/lib/crypto/encryption.php b/apps/encryption/lib/crypto/encryption.php
index 3f29848..cc61f04 100644
--- a/apps/encryption/lib/crypto/encryption.php
+++ b/apps/encryption/lib/crypto/encryption.php
@@ -28,6 +28,7 @@ namespace OCA\Encryption\Crypto;
 use OCA\Encryption\Util;
 use OCP\Encryption\IEncryptionModule;
 use OCA\Encryption\KeyManager;
+use OCP\ILogger;
 
 class Encryption implements IEncryptionModule {
 
@@ -66,16 +67,24 @@ class Encryption implements IEncryptionModule {
 	/** @var Util */
 	private $util;
 
+	/** @var  ILogger */
+	private $logger;
+
 	/**
 	 *
-	 * @param \OCA\Encryption\Crypto\Crypt $crypt
+	 * @param Crypt $crypt
 	 * @param KeyManager $keyManager
 	 * @param Util $util
+	 * @param ILogger $logger
 	 */
-	public function __construct(Crypt $crypt, KeyManager $keyManager, Util $util) {
+	public function __construct(Crypt $crypt,
+								KeyManager $keyManager,
+								Util $util,
+								ILogger $logger) {
 		$this->crypt = $crypt;
 		$this->keyManager = $keyManager;
 		$this->util = $util;
+		$this->logger = $logger;
 	}
 
 	/**
@@ -257,18 +266,28 @@ class Encryption implements IEncryptionModule {
 	 */
 	public function update($path, $uid, array $accessList) {
 		$fileKey = $this->keyManager->getFileKey($path, $uid);
-		$publicKeys = array();
-		foreach ($accessList['users'] as $user) {
-			$publicKeys[$user] = $this->keyManager->getPublicKey($user);
-		}
 
-		$publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys);
+		if (!empty($fileKey)) {
+
+			$publicKeys = array();
+			foreach ($accessList['users'] as $user) {
+				$publicKeys[$user] = $this->keyManager->getPublicKey($user);
+			}
+
+			$publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys);
 
-		$encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
+			$encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
 
-		$this->keyManager->deleteAllFileKeys($path);
+			$this->keyManager->deleteAllFileKeys($path);
 
-		$this->keyManager->setAllFileKeys($path, $encryptedFileKey);
+			$this->keyManager->setAllFileKeys($path, $encryptedFileKey);
+
+		} else {
+			$this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted',
+				array('file' => $path, 'app' => 'encryption'));
+
+			return false;
+		}
 
 		return true;
 	}
diff --git a/apps/encryption/tests/lib/crypto/encryptionTest.php b/apps/encryption/tests/lib/crypto/encryptionTest.php
index 500433c..2fbc7a1 100644
--- a/apps/encryption/tests/lib/crypto/encryptionTest.php
+++ b/apps/encryption/tests/lib/crypto/encryptionTest.php
@@ -38,6 +38,9 @@ class EncryptionTest extends TestCase {
 	/** @var \PHPUnit_Framework_MockObject_MockObject */
 	private $utilMock;
 
+	/** @var \PHPUnit_Framework_MockObject_MockObject */
+	private $loggerMock;
+
 	public function setUp() {
 		parent::setUp();
 
@@ -50,8 +53,16 @@ class EncryptionTest extends TestCase {
 		$this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
 			->disableOriginalConstructor()
 			->getMock();
+		$this->loggerMock = $this->getMockBuilder('OCP\ILogger')
+			->disableOriginalConstructor()
+			->getMock();
 
-		$this->instance = new Encryption($this->cryptMock, $this->keyManagerMock, $this->utilMock);
+		$this->instance = new Encryption(
+			$this->cryptMock,
+			$this->keyManagerMock,
+			$this->utilMock,
+			$this->loggerMock
+		);
 	}
 
 	/**
@@ -83,6 +94,9 @@ class EncryptionTest extends TestCase {
 		$this->cryptMock->expects($this->any())
 			->method('getLegacyCipher')
 			->willReturn($legacyCipher);
+		$this->cryptMock->expects($this->any())
+			->method('generateFileKey')
+			->willReturn('fileKey');
 
 		$result = $this->instance->begin('/user/files/foo.txt', 'user', $mode, $header, []);
 
@@ -99,5 +113,36 @@ class EncryptionTest extends TestCase {
 		);
 	}
 
+	/**
+	 * @dataProvider dataTestUpdate
+	 *
+	 * @param string $fileKey
+	 * @param boolean $expected
+	 */
+	public function testUpdate($fileKey, $expected) {
+		$this->keyManagerMock->expects($this->once())
+			->method('getFileKey')->willReturn($fileKey);
+
+		$this->keyManagerMock->expects($this->any())
+			->method('getPublicKey')->willReturn('publicKey');
+
+		$this->keyManagerMock->expects($this->any())
+			->method('addSystemKeys')
+			->willReturnCallback(function($accessList, $publicKeys) {
+				return $publicKeys;
+			});
+
+		$this->assertSame($expected,
+			$this->instance->update('path', 'user1', ['users' => ['user1']])
+		);
+
+	}
+
+	public function dataTestUpdate() {
+		return array(
+			array('', false),
+			array('fileKey', true)
+		);
+	}
 
-}
\ No newline at end of file
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list