Write cert bundle to tmp file first

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2017-05-19 22:51:26 +02:00
parent 86a496d94a
commit 04f2090698
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 45 additions and 7 deletions

View File

@ -31,6 +31,7 @@ use OC\Files\Filesystem;
use OCP\ICertificateManager; use OCP\ICertificateManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\Security\ISecureRandom;
/** /**
* Manage trusted certificates for users * Manage trusted certificates for users
@ -56,17 +57,26 @@ class CertificateManager implements ICertificateManager {
*/ */
protected $logger; protected $logger;
/** @var ISecureRandom */
protected $random;
/** /**
* @param string $uid * @param string $uid
* @param \OC\Files\View $view relative to data/ * @param \OC\Files\View $view relative to data/
* @param IConfig $config * @param IConfig $config
* @param ILogger $logger * @param ILogger $logger
* @param ISecureRandom $random
*/ */
public function __construct($uid, \OC\Files\View $view, IConfig $config, ILogger $logger) { public function __construct($uid,
\OC\Files\View $view,
IConfig $config,
ILogger $logger,
ISecureRandom $random) {
$this->uid = $uid; $this->uid = $uid;
$this->view = $view; $this->view = $view;
$this->config = $config; $this->config = $config;
$this->logger = $logger; $this->logger = $logger;
$this->random = $random;
} }
/** /**
@ -120,7 +130,8 @@ class CertificateManager implements ICertificateManager {
} }
$certPath = $path . 'rootcerts.crt'; $certPath = $path . 'rootcerts.crt';
$fhCerts = $this->view->fopen($certPath, 'w'); $tmpPath = $certPath . '.tmp' . $this->random->generate(10, ISecureRandom::CHAR_DIGITS);
$fhCerts = $this->view->fopen($tmpPath, 'w');
// Write user certificates // Write user certificates
foreach ($certs as $cert) { foreach ($certs as $cert) {
@ -143,6 +154,8 @@ class CertificateManager implements ICertificateManager {
} }
fclose($fhCerts); fclose($fhCerts);
$this->view->rename($tmpPath, $certPath);
} }
/** /**
@ -218,7 +231,7 @@ class CertificateManager implements ICertificateManager {
} }
if ($this->needsRebundling($uid)) { if ($this->needsRebundling($uid)) {
if (is_null($uid)) { if (is_null($uid)) {
$manager = new CertificateManager(null, $this->view, $this->config, $this->logger); $manager = new CertificateManager(null, $this->view, $this->config, $this->logger, $this->random);
$manager->createCertificateBundle(); $manager->createCertificateBundle();
} else { } else {
$this->createCertificateBundle(); $this->createCertificateBundle();

View File

@ -596,7 +596,13 @@ class Server extends ServerContainer implements IServerContainer {
$uid = $user ? $user : null; $uid = $user ? $user : null;
return new ClientService( return new ClientService(
$c->getConfig(), $c->getConfig(),
new \OC\Security\CertificateManager($uid, new View(), $c->getConfig(), $c->getLogger()) new \OC\Security\CertificateManager(
$uid,
new View(),
$c->getConfig(),
$c->getLogger(),
$c->getSecureRandom()
)
); );
}); });
$this->registerAlias('HttpClientService', \OCP\Http\Client\IClientService::class); $this->registerAlias('HttpClientService', \OCP\Http\Client\IClientService::class);
@ -1438,7 +1444,13 @@ class Server extends ServerContainer implements IServerContainer {
} }
$userId = $user->getUID(); $userId = $user->getUID();
} }
return new CertificateManager($userId, new View(), $this->getConfig(), $this->getLogger()); return new CertificateManager(
$userId,
new View(),
$this->getConfig(),
$this->getLogger(),
$this->getSecureRandom()
);
} }
/** /**

View File

@ -12,6 +12,7 @@ use OC\Files\Storage\Temporary;
use \OC\Security\CertificateManager; use \OC\Security\CertificateManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\Security\ISecureRandom;
/** /**
* Class CertificateManagerTest * Class CertificateManagerTest
@ -26,6 +27,8 @@ class CertificateManagerTest extends \Test\TestCase {
private $certificateManager; private $certificateManager;
/** @var String */ /** @var String */
private $username; private $username;
/** @var ISecureRandom */
private $random;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
@ -45,7 +48,17 @@ class CertificateManagerTest extends \Test\TestCase {
$config->expects($this->any())->method('getSystemValue') $config->expects($this->any())->method('getSystemValue')
->with('installed', false)->willReturn(true); ->with('installed', false)->willReturn(true);
$this->certificateManager = new CertificateManager($this->username, new \OC\Files\View(), $config, $this->createMock(ILogger::class)); $this->random = $this->createMock(ISecureRandom::class);
$this->random->method('generate')
->willReturn('random');
$this->certificateManager = new CertificateManager(
$this->username,
new \OC\Files\View(),
$config,
$this->createMock(ILogger::class),
$this->random
);
} }
protected function tearDown() { protected function tearDown() {
@ -145,7 +158,7 @@ class CertificateManagerTest extends \Test\TestCase {
/** @var CertificateManager | \PHPUnit_Framework_MockObject_MockObject $certificateManager */ /** @var CertificateManager | \PHPUnit_Framework_MockObject_MockObject $certificateManager */
$certificateManager = $this->getMockBuilder('OC\Security\CertificateManager') $certificateManager = $this->getMockBuilder('OC\Security\CertificateManager')
->setConstructorArgs([$uid, $view, $config, $this->createMock(ILogger::class)]) ->setConstructorArgs([$uid, $view, $config, $this->createMock(ILogger::class), $this->random])
->setMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle']) ->setMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle'])
->getMock(); ->getMock();