|
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
|
|
|
/** |
|
79
|
|
|
* Validate the inputfile. |
|
80
|
|
|
* |
|
81
|
|
|
* @param $certificateFile |
|
82
|
|
|
* |
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function guardAgainstInvalidInput($certificateFile) |
|
86
|
|
|
{ |
|
87
|
|
|
if (! file_exists($certificateFile)) { |
|
88
|
|
|
throw CouldNotRunCommand::inputFileDoesNotExist($certificateFile); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check if outputfile already exists, |
|
94
|
|
|
* if so, ask the user confirmation to overwrite. |
|
95
|
|
|
* |
|
96
|
|
|
* @param $input |
|
97
|
|
|
* @param $output |
|
98
|
|
|
* @param $outputFile |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function confirmOverwrite($input, $output, $outputFile) |
|
103
|
|
|
{ |
|
104
|
|
|
$output->writeln(''); |
|
105
|
|
|
|
|
106
|
|
|
$helper = $this->getHelper('question'); |
|
107
|
|
|
$question = new ConfirmationQuestion('<comment>Outputfile '.$outputFile.' already exists. Do you want to overwrite it? (y/n) </comment>', false); |
|
108
|
|
|
|
|
109
|
|
|
if (! $helper->ask($input, $output, $question)) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|