1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\CertificateChain\Console; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Spatie\CertificateChain\Certificate; |
8
|
|
|
use Spatie\CertificateChain\CertificateChain; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Spatie\CertificateChain\Exceptions\CouldNotRunCommand; |
14
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
15
|
|
|
|
16
|
|
|
class ResolveCommand extends Command |
17
|
|
|
{ |
18
|
|
|
protected $httpClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Configure the command options. |
22
|
|
|
*/ |
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this->setName('resolve') |
26
|
|
|
->setDescription('Download all intermediate certificates in the trust chain.') |
27
|
|
|
->addArgument('certificate', InputArgument::REQUIRED) |
28
|
|
|
->addArgument('outputFile', InputArgument::OPTIONAL); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
|
35
|
|
|
$this->httpClient = new Client(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the command. |
40
|
|
|
* |
41
|
|
|
* @param InputInterface $input |
42
|
|
|
* @param OutputInterface $output |
43
|
|
|
* |
44
|
|
|
* @return int|null|void |
45
|
|
|
* |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
$output->writeln('<info>Start resolving trust chain...</info>'); |
51
|
|
|
|
52
|
|
|
$certificateFile = $input->getArgument('certificate'); |
53
|
|
|
|
54
|
|
|
if (! file_exists($certificateFile)) { |
55
|
|
|
throw CouldNotRunCommand::inputFileDoesNotExist($certificateFile); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$outputFile = $input->getArgument('outputFile') ?: 'certificate-including-trust-chain.crt'; |
59
|
|
|
|
60
|
|
|
if (file_exists($outputFile)) { |
61
|
|
|
if (! $this->confirmOverwrite($input, $output, $outputFile)) { |
|
|
|
|
62
|
|
|
$output->writeln('<info>Cancelling...</info>'); |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$certificate = Certificate::loadFromFile($certificateFile); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$certificateChain = CertificateChain::fetchForCertificate($certificate); |
71
|
|
|
|
72
|
|
|
file_put_contents($outputFile, $certificateChain); |
73
|
|
|
|
74
|
|
|
$output->writeln('<info>Saved trust chain in '.$outputFile.'</info>'); |
75
|
|
|
$output->writeln('<info>All done!</info>'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function guardAgainstInvalidInput(string $certificateFile) |
79
|
|
|
{ |
80
|
|
|
if (! file_exists($certificateFile)) { |
81
|
|
|
throw CouldNotRunCommand::inputFileDoesNotExist($certificateFile); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function confirmOverwrite(InputInterface $input, OutputInterface $output, string $outputFile): bool |
86
|
|
|
{ |
87
|
|
|
$output->writeln(''); |
88
|
|
|
|
89
|
|
|
$helper = $this->getHelper('question'); |
90
|
|
|
$question = new ConfirmationQuestion('<comment>Outputfile '.$outputFile.' already exists. Do you want to overwrite it? (y/n) </comment>', false); |
91
|
|
|
|
92
|
|
|
if (! $helper->ask($input, $output, $question)) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.