1 | <?php |
||
10 | class ComposerSecurityCheck extends Command |
||
11 | { |
||
12 | /** |
||
13 | * The name and signature of the console command. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $signature = 'composer-security:check |
||
18 | {path? : path where find composer.lock, you can use * as jolly character i.e. "/var/www/*/*/", use quotation marks} |
||
19 | {--M|mail= : If you want send result to email} |
||
20 | {--N|nomailok=false : True if you want send result to email only for alarm, false is default} |
||
21 | {--w|whitelist= : If you want exclude from alarm some paths, divide by ","}'; |
||
22 | |||
23 | /** |
||
24 | * The console command description. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description = <<<EOF |
||
29 | The <info>composer-security:check</info> command looks for every composer.lock file in the given path |
||
30 | and foreach composer.lock check for security issues in the project dependencies: |
||
31 | <info>php composer-security:check</info> |
||
32 | If you omit path argument, command look into current folder. |
||
33 | You can also pass the path as an argument: |
||
34 | <info>php composer-security:check /path/to/my/repos</info> |
||
35 | You can use <info>*</info> in path argument as jolly character i.e. <info>/var/www/*/*/</info> |
||
36 | By default, the command displays the result in console, but you can also |
||
37 | send an html email by using the <info>--mail</info> option: |
||
38 | <info>php composer-security:check /path/to/my/repos [email protected]</info> |
||
39 | EOF; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * @var Client an istance of GuzzleHttp\Client |
||
44 | */ |
||
45 | protected $guzzle; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $headersTableConsole = ['name', 'version', 'title', 'whitelist']; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $tableVulnerabilities = []; |
||
56 | |||
57 | /** |
||
58 | * Create a new command instance. |
||
59 | * |
||
60 | * @param Client $objguzzle |
||
61 | */ |
||
62 | 18 | public function __construct(Client $objguzzle) |
|
67 | |||
68 | /** |
||
69 | * Execute the console command. |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | 16 | public function handle() |
|
77 | |||
78 | /** |
||
79 | * @param $argument |
||
80 | * @param $option |
||
81 | */ |
||
82 | 16 | private function hardWork($argument, $option) |
|
83 | { |
||
84 | 16 | $path = $argument['path']; |
|
85 | 16 | $this->line('path: <info>' . $path . '</info>.\nCheck composer.lock files...'); |
|
86 | 16 | $lockFiles = $this->findFilesComposerLock($path); |
|
87 | 16 | $this->line('Find <info>' . count($lockFiles) . '</info> composer.lock files.'); |
|
88 | |||
89 | 16 | $this->tableVulnerabilities = []; |
|
90 | 16 | $tuttoOk = true; |
|
91 | 16 | $numLock = 0; |
|
92 | |||
93 | 16 | $whitelist = FileHelper::adjustPath($option['whitelist']); |
|
94 | |||
95 | 16 | foreach ($lockFiles as $fileLock) { |
|
96 | |||
97 | 16 | $this->line("Analizing <info>" . ($numLock + 1) . "</info> di <info>" . count($lockFiles) . "</info>"); |
|
98 | |||
99 | 16 | $tuttoOk = $this->checkFile($fileLock, $whitelist); |
|
100 | |||
101 | 16 | $numLock++; |
|
102 | } |
||
103 | |||
104 | 16 | $this->notifyResult($option['mail'], $option['nomailok'], $tuttoOk); |
|
105 | |||
106 | 16 | } |
|
107 | |||
108 | /** |
||
109 | * @param $mail |
||
110 | * @param $tuttoOk |
||
111 | */ |
||
112 | 16 | private function notifyResult($mail, $nomailok, $tuttoOk) |
|
113 | { |
||
114 | |||
115 | //print to console |
||
116 | 16 | $this->table($this->headersTableConsole, $this->tableVulnerabilities); |
|
117 | |||
118 | 16 | $nomailok_bool = false; |
|
119 | |||
120 | 16 | if ($nomailok!='' && strtolower($nomailok)=='true') { |
|
121 | 4 | $nomailok_bool = true; |
|
122 | } |
||
123 | |||
124 | //send email |
||
125 | 16 | if(!$nomailok_bool || !$tuttoOk) { |
|
126 | 14 | $this->sendEmail($mail, $tuttoOk); |
|
127 | } |
||
128 | |||
129 | 16 | if ($tuttoOk) { |
|
130 | 10 | return $this->notifyOK(); |
|
131 | } |
||
132 | |||
133 | 6 | $this->notifyKO(); |
|
134 | 6 | } |
|
135 | |||
136 | |||
137 | 10 | private function notifyOK() |
|
142 | |||
143 | 6 | private function notifyKO() |
|
148 | |||
149 | /** |
||
150 | * @param $mail |
||
151 | * @param $tuttoOk |
||
152 | */ |
||
153 | 14 | private function sendEmail($mail, $tuttoOk) |
|
160 | |||
161 | /** |
||
162 | * |
||
163 | * @param $path |
||
164 | * @return array of composer.lock file |
||
165 | */ |
||
166 | 18 | private function findFilesComposerLock($path) |
|
181 | |||
182 | /** |
||
183 | * @param $fileLock |
||
184 | * @param $whitelist |
||
185 | * @return bool |
||
186 | */ |
||
187 | 16 | private function checkFile($fileLock, $whitelist) |
|
219 | |||
220 | } |
||
221 | |||
222 |