Completed
Push — master ( 0d138c...091bf0 )
by Lukas
07:48
created

SignCore   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 11.94 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 8
loc 67
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 8 8 1
C execute() 0 37 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\Core\Command\Integrity;
24
25
use OC\IntegrityCheck\Checker;
26
use OC\IntegrityCheck\Helpers\FileAccessHelper;
27
use phpseclib\Crypt\RSA;
28
use phpseclib\File\X509;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Input\InputOption;
32
use Symfony\Component\Console\Output\OutputInterface;
33
34
/**
35
 * Class SignCore
36
 *
37
 * @package OC\Core\Command\Integrity
38
 */
39
class SignCore extends Command {
40
	/** @var Checker */
41
	private $checker;
42
	/** @var FileAccessHelper */
43
	private $fileAccessHelper;
44
45
	/**
46
	 * @param Checker $checker
47
	 * @param FileAccessHelper $fileAccessHelper
48
	 */
49
	public function __construct(Checker $checker,
50
								FileAccessHelper $fileAccessHelper) {
51
		parent::__construct(null);
52
		$this->checker = $checker;
53
		$this->fileAccessHelper = $fileAccessHelper;
54
	}
55
56 View Code Duplication
	protected function configure() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
		$this
58
			->setName('integrity:sign-core')
59
			->setDescription('Sign core using a private key.')
60
			->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing')
61
			->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing')
62
			->addOption('path', null, InputOption::VALUE_REQUIRED, 'Path of core to sign');
63
	}
64
65
	/**
66
	 * {@inheritdoc }
67
	 */
68
	protected function execute(InputInterface $input, OutputInterface $output) {
69
		$privateKeyPath = $input->getOption('privateKey');
70
		$keyBundlePath = $input->getOption('certificate');
71
		$path = $input->getOption('path');
72
		if(is_null($privateKeyPath) || is_null($keyBundlePath) || is_null($path)) {
73
			$output->writeln('--privateKey, --certificate and --path are required.');
74
			return null;
75
		}
76
77
		$privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath);
78
		$keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath);
79
80
		if($privateKey === false) {
81
			$output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath));
82
			return null;
83
		}
84
85
		if($keyBundle === false) {
86
			$output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath));
87
			return null;
88
		}
89
90
		$rsa = new RSA();
91
		$rsa->loadKey($privateKey);
92
		$x509 = new X509();
93
		$x509->loadX509($keyBundle);
94
		$x509->setPrivateKey($rsa);
95
96
		try {
97
			$this->checker->writeCoreSignature($x509, $rsa, $path);
98
			$output->writeln('Successfully signed "core"');
99
		} catch (\Exception $e){
100
			$output->writeln('Error: ' . $e->getMessage());
101
			return 1;
102
		}
103
		return 0;
104
	}
105
}
106