Skip to content

Commit 74f6694

Browse files
committed
Adds warning method to the CLI printer
1 parent cde4a1d commit 74f6694

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/Backup/Crypter/OpenSSL.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use phpbu\App\Backup\Restore\Plan;
55
use phpbu\App\Backup\Target;
66
use phpbu\App\Cli\Executable;
7+
use phpbu\App\Result;
78
use phpbu\App\Util;
89

910
/**
@@ -54,6 +55,36 @@ class OpenSSL extends Abstraction implements Simulator, Restorable
5455
*/
5556
private $keepUncrypted;
5657

58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function crypt(Target $target, Result $result)
63+
{
64+
if ($this->getExecutable($target)->isUsingWeakAlgorithm()) {
65+
$name = strtolower(get_class($this));
66+
67+
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
68+
}
69+
70+
return parent::crypt($target, $result);
71+
}
72+
73+
74+
/**
75+
* @inheritDoc
76+
*/
77+
public function simulate(Target $target, Result $result)
78+
{
79+
if ($this->getExecutable($target)->isUsingWeakAlgorithm()) {
80+
$name = strtolower(get_class($this));
81+
82+
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
83+
}
84+
85+
return parent::simulate($target, $result);
86+
}
87+
5788
/**
5889
* Setup
5990
*

src/Cli/Executable/OpenSSL.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class OpenSSL extends Abstraction implements Executable
162162
]
163163
];
164164

165-
private $weakCiphers = [
165+
private $weakAlgorithms = [
166166
'rc2' => true,
167167
'rc2-40' => true,
168168
'rc2-64' => true,
@@ -291,6 +291,15 @@ public function useAlgorithm(string $algorithm): OpenSSL
291291
return $this;
292292
}
293293

294+
public function isUsingWeakAlgorithm(): bool
295+
{
296+
if (null === $this->algorithm) {
297+
throw new Exception('algorithm is not set');
298+
}
299+
300+
return isset($this->weakAlgorithms[$this->algorithm]);
301+
}
302+
294303
/**
295304
* Use base64 encoding
296305
*

src/Result/PrinterCli.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public static function getSubscribedEvents(): array
110110
{
111111
return [
112112
'phpbu.debug' => 'onDebug',
113+
'phpbu.warning' => 'onWarning',
113114
'phpbu.app_start' => 'onPhpbuStart',
114115
'phpbu.backup_start' => 'onBackupStart',
115116
'phpbu.backup_failed' => 'onBackupFailed',
@@ -406,6 +407,16 @@ public function onDebug(Event\Debug $event)
406407
}
407408
}
408409

410+
/**
411+
* Warnings.
412+
*
413+
* @param \phpbu\App\Event\Warning $event
414+
*/
415+
public function onWarning(Event\Warning $event)
416+
{
417+
$this->writeWithColor('fg-black, bg-yellow', $event->getMessage() . PHP_EOL);
418+
}
419+
409420
/**
410421
* phpbu end event.
411422
*

tests/phpbu/Backup/Crypter/OpenSSLTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,23 @@ public function testGetSuffix()
171171
$suffix = $openSSL->getSuffix();
172172
$this->assertEquals('enc', $suffix);
173173
}
174+
175+
/**
176+
* Tests that a warning is emitted for weak algorithms
177+
*/
178+
public function testWeakAlgorithmsCauseWarnings()
179+
{
180+
$runner = $this->getRunnerMock();
181+
$runner->expects($this->once())
182+
->method('run')
183+
->willReturn($this->getRunnerResultMock(0, 'openssl'));
184+
185+
$target = $this->createTargetMock(__FILE__);
186+
$appResult = $this->getAppResultMock();
187+
$appResult->expects($this->once())->method('warn');
188+
189+
$openSSL = new OpenSSL($runner);
190+
$openSSL->setup(['pathToOpenSSL' => PHPBU_TEST_BIN, 'password' => 'fooBarBaz', 'algorithm' => 'des']);
191+
$openSSL->crypt($target, $appResult);
192+
}
174193
}

0 commit comments

Comments
 (0)