nextcloud/core/Command/Upgrade.php

308 lines
11 KiB
PHP
Raw Normal View History

2013-10-24 21:59:39 +04:00
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
2016-07-21 18:07:57 +03:00
*
2015-03-26 13:44:34 +03:00
* @author Andreas Fischer <bantu@owncloud.com>
2016-05-26 20:56:05 +03:00
* @author Björn Schießle <bjoern@schiessle.org>
2016-07-21 18:07:57 +03:00
* @author Joas Schilling <coding@schilljs.com>
2016-05-26 20:56:05 +03:00
* @author Lukas Reschke <lukas@statuscode.ch>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
* @author Owen Winkler <a_github@midnightcircus.com>
2015-06-25 12:43:55 +03:00
* @author Steffen Lindner <mail@steffen-lindner.de>
2015-03-26 13:44:34 +03:00
* @author Thomas Müller <thomas.mueller@tmit.eu>
2016-07-21 18:07:57 +03:00
* @author Thomas Pulzer <t.pulzer@kniel.de>
2015-03-26 13:44:34 +03:00
* @author Vincent Petry <pvince81@owncloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
2013-10-24 21:59:39 +04:00
*/
2013-10-24 21:59:39 +04:00
namespace OC\Core\Command;
use OC\Console\TimestampFormatter;
use OC\Updater;
use OCP\IConfig;
2015-10-09 15:08:22 +03:00
use OCP\ILogger;
2013-10-24 21:59:39 +04:00
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
2013-10-24 21:59:39 +04:00
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\GenericEvent;
2013-10-24 21:59:39 +04:00
class Upgrade extends Command {
const ERROR_SUCCESS = 0;
const ERROR_NOT_INSTALLED = 1;
const ERROR_MAINTENANCE_MODE = 2;
const ERROR_UP_TO_DATE = 0;
const ERROR_INVALID_ARGUMENTS = 4;
const ERROR_FAILURE = 5;
2015-10-09 15:08:22 +03:00
/** @var IConfig */
private $config;
2015-10-09 15:08:22 +03:00
/** @var ILogger */
private $logger;
/**
* @param IConfig $config
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
2015-11-03 22:26:06 +03:00
* @param ILogger $logger
*/
2015-10-09 15:08:22 +03:00
public function __construct(IConfig $config, ILogger $logger) {
parent::__construct();
$this->config = $config;
2015-10-09 15:08:22 +03:00
$this->logger = $logger;
}
2013-10-24 21:59:39 +04:00
protected function configure() {
$this
->setName('upgrade')
->setDescription('run upgrade routines after installation of a new release. The release has to be installed before.')
->addOption(
'--dry-run',
null,
InputOption::VALUE_NONE,
'only runs the database schema migration simulation, do not actually update'
2015-05-20 18:06:00 +03:00
)
->addOption(
'--no-app-disable',
null,
InputOption::VALUE_NONE,
'skips the disable of third party apps'
);
2013-10-24 21:59:39 +04:00
}
/**
* Execute the upgrade command
*
* @param InputInterface $input input interface
* @param OutputInterface $output output interface
*/
2013-10-24 21:59:39 +04:00
protected function execute(InputInterface $input, OutputInterface $output) {
2015-05-20 18:06:00 +03:00
$skip3rdPartyAppsDisable = false;
2015-05-20 18:06:00 +03:00
if ($input->getOption('no-app-disable')) {
$skip3rdPartyAppsDisable = true;
}
if(\OC::checkUpgrade(false)) {
if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
// Prepend each line with a little timestamp
$timestampFormatter = new TimestampFormatter($this->config, $output->getFormatter());
$output->setFormatter($timestampFormatter);
}
$self = $this;
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
2015-11-03 22:26:06 +03:00
$updater = new Updater(
$this->config,
\OC::$server->getIntegrityCodeChecker(),
$this->logger
);
2015-05-20 18:06:00 +03:00
$updater->setSkip3rdPartyAppsDisable($skip3rdPartyAppsDisable);
$dispatcher = \OC::$server->getEventDispatcher();
$progress = new ProgressBar($output);
$progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%");
$listener = function($event) use ($progress, $output) {
if ($event instanceof GenericEvent) {
$message = $event->getSubject();
if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$output->writeln(' Checking table ' . $message);
} else {
if (strlen($message) > 60) {
$message = substr($message, 0, 57) . '...';
}
$progress->setMessage($message);
if ($event[0] === 1) {
$output->writeln('');
$progress->start($event[1]);
}
$progress->setProgress($event[0]);
if ($event[0] === $event[1]) {
$progress->setMessage('Done');
$progress->finish();
$output->writeln('');
}
}
}
};
2016-04-27 14:19:00 +03:00
$repairListener = function($event) use ($progress, $output) {
if (!$event instanceof GenericEvent) {
return;
}
switch ($event->getSubject()) {
case '\OC\Repair::startProgress':
$progress->setMessage('Starting ...');
$output->writeln($event->getArgument(1));
$output->writeln('');
$progress->start($event->getArgument(0));
break;
case '\OC\Repair::advance':
$desc = $event->getArgument(1);
if (!empty($desc)) {
$progress->setMessage($desc);
}
$progress->advance($event->getArgument(0));
break;
case '\OC\Repair::finishProgress':
$progress->setMessage('Done');
$progress->finish();
$output->writeln('');
break;
case '\OC\Repair::step':
if(OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$output->writeln('<info>Repair step: ' . $event->getArgument(0) . '</info>');
}
break;
case '\OC\Repair::info':
if(OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$output->writeln('<info>Repair info: ' . $event->getArgument(0) . '</info>');
}
break;
case '\OC\Repair::warning':
$output->writeln('<error>Repair warning: ' . $event->getArgument(0) . '</error>');
break;
case '\OC\Repair::error':
$output->writeln('<error>Repair error: ' . $event->getArgument(0) . '</error>');
break;
}
};
$dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener);
$dispatcher->addListener('\OC\DB\Migrator::checkTable', $listener);
2016-04-27 14:19:00 +03:00
$dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
$dispatcher->addListener('\OC\Repair::advance', $repairListener);
$dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
$dispatcher->addListener('\OC\Repair::step', $repairListener);
$dispatcher->addListener('\OC\Repair::info', $repairListener);
$dispatcher->addListener('\OC\Repair::warning', $repairListener);
$dispatcher->addListener('\OC\Repair::error', $repairListener);
$updater->listen('\OC\Updater', 'maintenanceEnabled', function () use($output) {
$output->writeln('<info>Turned on maintenance mode</info>');
});
$updater->listen('\OC\Updater', 'maintenanceDisabled', function () use($output) {
$output->writeln('<info>Turned off maintenance mode</info>');
});
$updater->listen('\OC\Updater', 'maintenanceActive', function () use($output) {
$output->writeln('<info>Maintenance mode is kept active</info>');
});
$updater->listen('\OC\Updater', 'updateEnd',
function ($success) use($output, $updateStepEnabled, $self) {
if ($success) {
$message = "<info>Update successful</info>";
} else {
$message = "<error>Update failed</error>";
}
$output->writeln($message);
});
$updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use($output) {
$output->writeln('<info>Updating database schema</info>');
});
$updater->listen('\OC\Updater', 'dbUpgrade', function () use($output) {
$output->writeln('<info>Updated database</info>');
});
$updater->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($output) {
$output->writeln('<info>Checking whether the database schema can be updated (this can take a long time depending on the database size)</info>');
});
$updater->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($output) {
$output->writeln('<info>Checked database schema update</info>');
});
$updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($output) {
$output->writeln('<comment>Disabled incompatible app: ' . $app . '</comment>');
});
2015-05-20 18:06:00 +03:00
$updater->listen('\OC\Updater', 'thirdPartyAppDisabled', function ($app) use ($output) {
$output->writeln('<comment>Disabled 3rd-party app: ' . $app . '</comment>');
});
$updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($output) {
$output->writeln('<info>Update 3rd-party app: ' . $app . '</info>');
});
$updater->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($output) {
$output->writeln('<info>Checking updates of apps</info>');
});
$updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($output) {
$output->writeln("<info>Checking whether the database schema for <$app> can be updated (this can take a long time depending on the database size)</info>");
});
$updater->listen('\OC\Updater', 'appUpgradeCheck', function () use ($output) {
$output->writeln('<info>Checked database schema update for apps</info>');
});
$updater->listen('\OC\Updater', 'appUpgradeStarted', function ($app, $version) use ($output) {
$output->writeln("<info>Updating <$app> ...</info>");
});
$updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($output) {
$output->writeln("<info>Updated <$app> to $version</info>");
});
$updater->listen('\OC\Updater', 'failure', function ($message) use($output, $self) {
$output->writeln("<error>$message</error>");
});
$updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use($output) {
$output->writeln("<info>Set log level to debug</info>");
});
$updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($output) {
$output->writeln("<info>Reset log level</info>");
});
$updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($output) {
$output->writeln("<info>Starting code integrity check...</info>");
});
$updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($output) {
$output->writeln("<info>Finished code integrity check</info>");
});
$success = $updater->upgrade();
$this->postUpgradeCheck($input, $output);
if(!$success) {
return self::ERROR_FAILURE;
}
return self::ERROR_SUCCESS;
} else if($this->config->getSystemValue('maintenance', false)) {
//Possible scenario: Nextcloud core is updated but an app failed
$output->writeln('<warning>Nextcloud is in maintenance mode</warning>');
$output->write('<comment>Maybe an upgrade is already in process. Please check the '
. 'logfile (data/nextcloud.log). If you want to re-run the '
. 'upgrade procedure, remove the "maintenance mode" from '
. 'config.php and call this script again.</comment>'
, true);
return self::ERROR_MAINTENANCE_MODE;
} else {
$output->writeln('<info>Nextcloud is already latest version</info>');
return self::ERROR_UP_TO_DATE;
}
2013-10-24 21:59:39 +04:00
}
/**
* Perform a post upgrade check (specific to the command line tool)
*
* @param InputInterface $input input interface
* @param OutputInterface $output output interface
*/
protected function postUpgradeCheck(InputInterface $input, OutputInterface $output) {
$trustedDomains = $this->config->getSystemValue('trusted_domains', array());
if (empty($trustedDomains)) {
$output->write(
'<warning>The setting "trusted_domains" could not be ' .
'set automatically by the upgrade script, ' .
'please set it manually</warning>'
);
}
}
2013-10-24 21:59:39 +04:00
}