Skip to content

Commit

Permalink
Merge branch 'main' into 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Mar 22, 2024
2 parents 63fb5f7 + cc3087d commit 9bfc18f
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 32 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"require-dev": {
"sebastianfeldmann/git": "^3.2",
"sebastianfeldmann/ftp": "^0.9",
"sebastianfeldmann/ftp": "^0.9.2",
"guzzlehttp/guzzle": "^5.3.4|^6.5.8|^7.5.0",
"aws/aws-sdk-php": "^3.10",
"kunalvarma05/dropbox-php-sdk": "^0.4",
Expand All @@ -68,7 +68,7 @@
"phpmailer/phpmailer": "^6.0"
},
"suggest": {
"sebastianfeldmann/ftp": "Require ^0.9 to sync to an FTP server",
"sebastianfeldmann/ftp": "Require ^0.9.2 to sync to an FTP server",
"guzzlehttp/guzzle": "Require ^5.3.3|^6.2.1 to write logs to Telegram",
"aws/aws-sdk-php": "Require '^3.10' to sync to Amazon S3",
"kunalvarma05/dropbox-php-sdk": "Require '^0.2' to sync to Dropbox",
Expand Down
3 changes: 2 additions & 1 deletion doc/config/sync/ftp.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"user": "user.name",
"password": "mySecret",
"path": "someDir/someSubDir",
"passive": "false"
"passive": "false",
"secure": "false"
}
}

3 changes: 3 additions & 0 deletions doc/config/sync/ftp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@

<!-- optional, default=false -->
<option name="passive" value="true" />

<!-- optional, default=false -->
<option name="secure" value="true" />
</sync>
4 changes: 2 additions & 2 deletions src/Backup/Crypter/Gpg.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function setup(array $options = [])
throw new Exception('gpg expects \'user\'');
}

$this->pathToGpg = Util\Arr::getValue($options, 'pathToOpenSSL', '');
$this->pathToGpg = Util\Arr::getValue($options, 'pathToGPG', '');
$this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false);
$this->user = $this->toAbsolutePath(Util\Arr::getValue($options, 'user', ''));
$this->user = Util\Arr::getValue($options, 'user', '');
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Backup/Crypter/OpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class OpenSSL extends Abstraction implements Simulator, Restorable
*/
private $password;

/**
* Use password-based key derivation
*
* @var bool
*/
private bool $keyDerivation;

/**
* Keep the not encrypted file
*
Expand Down Expand Up @@ -157,6 +164,7 @@ public function setup(array $options = [])
$this->certFile = $this->toAbsolutePath(Util\Arr::getValue($options, 'certFile', ''));
$this->algorithm = Util\Arr::getValue($options, 'algorithm', '');
$this->password = Util\Arr::getValue($options, 'password', '');
$this->keyDerivation = Util\Str::toBoolean(Util\Arr::getValue($options, 'keyDerivation', ''), false);
}

/**
Expand Down Expand Up @@ -242,6 +250,7 @@ private function createOpenSSL(Target $target): Executable\OpenSSL
$executable->useSSLCert($this->certFile);
} else {
$executable->usePassword($this->password)
->usePasswordBasedKeyDerivation($this->keyDerivation)
->encodeBase64(true);
}
$executable->useAlgorithm($this->algorithm);
Expand Down
2 changes: 1 addition & 1 deletion src/Backup/File/OpenStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OpenStack extends Remote
public function __construct($container, $object)
{
$this->container = $container;
$this->filename = basename($object->name);
$this->filename = empty($object->name) ? '' : basename($object->name);
$this->pathname = $object->name;
$this->size = (int) $object->contentLength;
$this->lastModified = $object->lastModified->getTimestamp();
Expand Down
10 changes: 9 additions & 1 deletion src/Backup/Sync/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class Ftp extends Xtp
*/
protected $passive;

/**
* If set to `true` connection is made via FTPS (explicit SSL).
*
* @var bool
*/
protected $secure;

/**
* Setup the Ftp sync
*
Expand All @@ -56,6 +63,7 @@ public function setup(array $config)
parent::setup($config);

$this->passive = Util\Str::toBoolean(Util\Arr::getValue($config, 'passive', ''), false);
$this->secure = Util\Str::toBoolean(Util\Arr::getValue($config, 'secure', ''), false);
$this->setUpCleanable($config);
}

Expand Down Expand Up @@ -113,7 +121,7 @@ protected function createClient()
{
if (!$this->ftpClient) {
$login = $this->user . ($this->password ? ':' . $this->password : '');
$this->ftpClient = new Client('ftp://' . $login . '@' . $this->host, $this->passive);
$this->ftpClient = new Client('ftp://' . $login . '@' . $this->host, $this->passive, $this->secure);
}
return $this->ftpClient;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Cli/Executable/Gpg.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,26 @@ protected function createCommandLine(): CommandLine
$cmd = new Cmd($this->binary);

$process->addCommand($cmd);
$this->setOptions($cmd);

$this->addDeleteCommand($process);

return $process;
}

/**
* Set the gpg command line options
*
* @param \SebastianFeldmann\Cli\Command\Executable $cmd
*/
protected function setOptions(Cmd $cmd): void
{
$cmd->addOption('--' . ($this->action === 'e' ? 'encrypt' : 'decrypt'));
$cmd->addOption('-r', $this->user, ' ');
$cmd->addOption('-o', $this->targetFile, ' ');
$cmd->addArgument($this->sourceFile);
}

/**
* Add the 'rm' command to remove the uncrypted file
*
Expand Down
14 changes: 14 additions & 0 deletions src/Cli/Executable/OpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class OpenSSL extends Abstraction implements Executable
*/
private $password;

/**
* Use password-based key derivation
*
* @var bool
*/
private $passwordBasedKeyDerivation;

/**
* Algorithm to use
*
Expand Down Expand Up @@ -237,6 +244,12 @@ public function usePassword(string $password): OpenSSL
return $this;
}

public function usePasswordBasedKeyDerivation(bool $bool): OpenSSL
{
$this->passwordBasedKeyDerivation = $bool;
return $this;
}

/**
* Set algorithm to use
*
Expand Down Expand Up @@ -359,6 +372,7 @@ protected function setPasswordOptions(Cmd $cmd): void
$cmd->addOptionIfNotEmpty('-a', $this->base64, false);
$cmd->addOption('-' . $this->algorithm);
$cmd->addOption('-pass', $password, ' ');
$cmd->addOptionIfNotEmpty('-pbkdf2', $this->passwordBasedKeyDerivation, false);
$cmd->addOption('-in', $this->sourceFile, ' ');
$cmd->addOption('-out', $this->targetFile, ' ');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Executable/Xtrabackup8.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function createCommandLine() : CommandLine
$cmdDump->addOption('--databases', implode(' ', $this->databases));
}

$cmdDump->addArgument($this->dumpDir);
$cmdDump->addOption('--target-dir', $this->dumpDir);

return $process;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Log/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function setup(array $options)
$this->sendSimulating = Str::toBoolean(Arr::getValue($options, 'sendOnSimulation'), true);
$this->subject = Arr::getValue($options, 'subject', 'PHPBU backup report from ' . $server);
$this->senderMail = Arr::getValue($options, 'sender.mail', 'phpbu@' . $server);
$this->senderName = Arr::getValue($options, 'sender.name');
$this->senderName = Arr::getValue($options, 'sender.name', '');
$this->transportType = Arr::getValue($options, 'transport', 'mail');
$this->recipients = array_map('trim', explode(';', $mails));
$this->isSimulation = Arr::getValue($options, '__simulate__', false);
Expand Down
7 changes: 6 additions & 1 deletion src/Log/Webhook.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace phpbu\App\Log;

use phpbu\App\Configuration;
use phpbu\App\Exception;
use phpbu\App\Event;
use phpbu\App\Listener;
use phpbu\App\Result;
use phpbu\App\Util\Arr;
use phpbu\App\Util\Path;
use Throwable;

/**
Expand Down Expand Up @@ -185,7 +187,10 @@ private function buildGetUri(array $data) : string
private function getBodyFormatter() : ResultFormatter
{
if (!empty($this->template)) {
return new ResultFormatter\Template($this->template);
$tpl = Path::isAbsolutePath($this->template)
? $this->template
: Path::withTrailingSlash(Configuration::getWorkingDirectory()) . $this->template;
return new ResultFormatter\Template($tpl);
}

if (!isset($this->availableFormatter[$this->contentType])) {
Expand Down
9 changes: 7 additions & 2 deletions tests/phpbu/Backup/Crypter/OpenSSLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ public function testPasswordAndAlgorithm()
{
$target = $this->createTargetMock('/foo/bar.txt');
$openSSL = new OpenSSL();
$openSSL->setup(['pathToOpenSSL' => PHPBU_TEST_BIN, 'password' => 'fooBarBaz', 'algorithm' => 'aes-256-cbc']);
$openSSL->setup([
'pathToOpenSSL' => PHPBU_TEST_BIN,
'password' => 'fooBarBaz',
'keyDerivation' => true,
'algorithm' => 'aes-256-cbc'
]);

$executable = $openSSL->getExecutable($target);
$expected = '("' . PHPBU_TEST_BIN . '/openssl" enc -e -a -aes-256-cbc -pass \'pass:fooBarBaz\' '
$expected = '("' . PHPBU_TEST_BIN . '/openssl" enc -e -a -aes-256-cbc -pass \'pass:fooBarBaz\' -pbkdf2 '
. '-in \'/foo/bar.txt\' -out \'/foo/bar.txt.enc\' '
. '&& "rm" \'/foo/bar.txt\')';

Expand Down
23 changes: 14 additions & 9 deletions tests/phpbu/Backup/File/OpenStackTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php
namespace phpbu\App\Backup\File;

use DateTimeImmutable;
use Exception;
use GuzzleHttp\ClientInterface;
use OpenStack\Common\Api\ApiInterface;
use OpenStack\ObjectStore\v1\Models\Container;
use OpenStack\ObjectStore\v1\Models\StorageObject;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -19,16 +25,16 @@ class OpenStackTest extends TestCase
{
public function testCreateFileWithCorrectProperties()
{
$storageObjectLastModified = new \DateTimeImmutable('2018-05-08 14:14:54.0 +00:00');
$storageObject = $this->createMock(\OpenStack\ObjectStore\v1\Models\StorageObject::class);
$storageObjectLastModified = new DateTimeImmutable('2018-05-08 14:14:54.0 +00:00');
$storageObject = $this->createMock(StorageObject::class);

$storageObject->name = 'path/dump.tar.gz';
$storageObject->filename = 'dump.tar.gz';
$storageObject->contentLength = 102102;
$storageObject->lastModified = $storageObjectLastModified;
$storageObject->expects($this->once())
->method('delete');

$container = $this->createMock(\OpenStack\ObjectStore\v1\Models\Container::class);
$container = $this->createMock(Container::class);
$container->expects($this->once())
->method('getObject')
->with('path/dump.tar.gz')
Expand All @@ -49,17 +55,16 @@ public function testCreateFileWithCorrectProperties()
public function testOpenStackDeleteFailure()
{
$this->expectException('phpbu\App\Exception');
$storageObjectLastModified = new \DateTimeImmutable('2018-05-08 14:14:54.0 +00:00');
$storageObject = $this->createMock(\OpenStack\ObjectStore\v1\Models\StorageObject::class);
$storageObjectLastModified = new DateTimeImmutable('2018-05-08 14:14:54.0 +00:00');
$storageObject = $this->createMock(StorageObject::class);
$storageObject->name = 'path/dump.tar.gz';
$storageObject->filename = 'dump.tar.gz';
$storageObject->contentLength = 102102;
$storageObject->lastModified = $storageObjectLastModified;
$storageObject->expects($this->once())
->method('delete')
->will($this->throwException(new \Exception()));
->will($this->throwException(new Exception()));

$container = $this->createMock(\OpenStack\ObjectStore\v1\Models\Container::class);
$container = $this->createMock(Container::class);
$container->expects($this->once())
->method('getObject')
->with('path/dump.tar.gz')
Expand Down
6 changes: 3 additions & 3 deletions tests/phpbu/Backup/Source/XtraBackup8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testDefault()
$xtrabackup->setup(['pathToXtraBackup' => PHPBU_TEST_BIN]);

$executable = $xtrabackup->getExecutable($target);
$expectedDump = 'xtrabackup" --backup \'./dump\'';
$expectedDump = 'xtrabackup" --backup --target-dir=\'./dump\'';
$expected = '"' . PHPBU_TEST_BIN . '/' . $expectedDump;

$this->assertEquals($expected, $executable->getCommand());
Expand All @@ -50,7 +50,7 @@ public function testDataDir()
$xtrabackup->setup(['pathToXtraBackup' => PHPBU_TEST_BIN, 'dataDir' => '/x/mysql']);

$executable = $xtrabackup->getExecutable($target);
$expectedDump = 'xtrabackup" --backup --datadir=\'/x/mysql\' \'./dump\'';
$expectedDump = 'xtrabackup" --backup --datadir=\'/x/mysql\' --target-dir=\'./dump\'';
$expected = '"' . PHPBU_TEST_BIN . '/' . $expectedDump;

$this->assertEquals($expected, $executable->getCommand());
Expand All @@ -67,7 +67,7 @@ public function testDatabases()
$xtrabackup->setup(['pathToXtraBackup' => PHPBU_TEST_BIN, 'databases' => 'db1,db2,db3.table1']);

$executable = $xtrabackup->getExecutable($target);
$expectedDump = 'xtrabackup" --backup --databases=\'db1 db2 db3.table1\' \'./dump\'';
$expectedDump = 'xtrabackup" --backup --databases=\'db1 db2 db3.table1\' --target-dir=\'./dump\'';
$expected = '"' . PHPBU_TEST_BIN . '/' . $expectedDump;

$this->assertEquals($expected, $executable->getCommand());
Expand Down
6 changes: 4 additions & 2 deletions tests/phpbu/Cli/Executable/OpenSSLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ public function testPassword()
*/
public function testDecrypt()
{
$expected = 'openssl" enc -d -aes-256-cbc -pass \'pass:fooBarBaz\' '
$expected = 'openssl" enc -d -aes-256-cbc -pass \'pass:fooBarBaz\' -pbkdf2 '
. '-in \'/foo/bar.txt.enc\' -out \'/foo/bar.txt\'';
$openSSL = new OpenSSL(PHPBU_TEST_BIN);
$openSSL->decryptFile('/foo/bar.txt')
->usePassword('fooBarBaz')
->usePasswordBasedKeyDerivation(true)
->useAlgorithm('aes-256-cbc')
->deleteSource(false);

Expand All @@ -85,11 +86,12 @@ public function testDecrypt()
*/
public function testDoNotDeleteUncrypted()
{
$expected = 'openssl" enc -e -aes-256-cbc -pass \'pass:fooBarBaz\' '
$expected = 'openssl" enc -e -aes-256-cbc -pass \'pass:fooBarBaz\' -pbkdf2 '
. '-in \'/foo/bar.txt\' -out \'/foo/bar.txt.enc\'';
$openSSL = new OpenSSL(PHPBU_TEST_BIN);
$openSSL->encryptFile('/foo/bar.txt')
->usePassword('fooBarBaz')
->usePasswordBasedKeyDerivation(true)
->useAlgorithm('aes-256-cbc')
->deleteSource(false);

Expand Down
Loading

0 comments on commit 9bfc18f

Please sign in to comment.