Skip to content
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

Setupfs before access a users keys #26917

Merged
merged 5 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/private/Encryption/Keys/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class Storage implements IStorage {
/** @var array */
private $keyCache = [];

/** @var string */
private $currentUser = null;

/**
* @param View $view
* @param Util $util
Expand All @@ -64,6 +67,11 @@ public function __construct(View $view, Util $util) {
$this->encryption_base_dir = '/files_encryption';
$this->keys_base_dir = $this->encryption_base_dir .'/keys';
$this->root_dir = $this->util->getKeyStorageRoot();

$session = \OC::$server->getUserSession();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would be a pain to inject this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, apparently not. This class is only in one place in server.php, so I'll add it.

if (!is_null($session) && !is_null($session->getUser())) {
$this->currentUser = $session->getUser()->getUID();
}
}

/**
Expand Down Expand Up @@ -170,6 +178,7 @@ protected function constructUserKeyPath($encryptionModuleId, $keyId, $uid) {
if ($uid === null) {
$path = $this->root_dir . '/' . $this->encryption_base_dir . '/' . $encryptionModuleId . '/' . $keyId;
} else {
$this->setupUserMounts($uid);
$path = $this->root_dir . '/' . $uid . $this->encryption_base_dir . '/'
. $encryptionModuleId . '/' . $uid . '.' . $keyId;
}
Expand Down Expand Up @@ -235,6 +244,7 @@ private function getFileKeyDir($encryptionModuleId, $path) {
if ($this->util->isSystemWideMountPoint($filename, $owner)) {
$keyPath = $this->root_dir . '/' . $this->keys_base_dir . $filename . '/';
} else {
$this->setupUserMounts($owner);
$keyPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $filename . '/';
}

Expand Down Expand Up @@ -298,6 +308,7 @@ protected function getPathToKeys($path) {
if ($systemWideMountPoint) {
$systemPath = $this->root_dir . '/' . $this->keys_base_dir . $relativePath . '/';
} else {
$this->setupUserMounts($owner);
$systemPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $relativePath . '/';
}

Expand All @@ -323,4 +334,19 @@ protected function keySetPreparation($path) {
}
}

/**
* Setup the mounts of the given user if different than
* the current user.
*
* This is needed because in many cases the keys are stored
* within the user's home storage.
*
* @param string $uid user id
*/
protected function setupUserMounts($uid) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this function should return something to check the result of the initMountPoint (if any) or to check if the function is ignoring the request because the uid matches the current user or the mount point is already mounted, specially if there are plans to unittest this function.

Since it isn't public we might skip testing this.

if (!is_null($uid) && $uid !== '' && $uid !== $this->currentUser) {
\OC\Files\Filesystem::initMountPoints($uid);
}
}

}
18 changes: 16 additions & 2 deletions tests/lib/Encryption/Keys/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,29 @@
namespace Test\Encryption\Keys;

use OC\Encryption\Keys\Storage;
use OC\Encryption\Util;
use OC\Files\View;
use Test\TestCase;
use Test\Traits\UserTrait;

/**
* Class StorageTest
*
* @group DB
*
* @package Test\Encryption\Keys
*/
class StorageTest extends TestCase {

use UserTrait;

/** @var Storage */
protected $storage;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var \PHPUnit_Framework_MockObject_MockObject | Util */
protected $util;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var \PHPUnit_Framework_MockObject_MockObject | View */
protected $view;

/** @var \PHPUnit_Framework_MockObject_MockObject */
Expand All @@ -55,6 +67,8 @@ public function setUp() {
->disableOriginalConstructor()
->getMock();

$this->createUser('user1', '123456');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will create a user each time a test runs, which seems wrong.

Either you create the users in the setUpBeforeClass method (and maybe delete them in the tearDownAfterClass method) or you delete them in the tearDown method so the next tests recreate them without any problem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I forgot to re-delete the user. Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the code. Apparently createUser comes from UserTrait which itself uses the Dummy user backend which is reset after every test. So no need for additional changes here.

$this->createUser('user2', '123456');
$this->storage = new Storage($this->view, $this->util);
}

Expand Down