Skip to content

Commit

Permalink
[BUGFIX] Properly escape options of dump database task
Browse files Browse the repository at this point in the history
  • Loading branch information
helhum committed Apr 13, 2016
1 parent 705e965 commit 799b33f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
1 change: 1 addition & 0 deletions Tests/Unit/Task/BaseTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function setUp()
}

$this->node = new \TYPO3\Surf\Domain\Model\Node('TestNode');
$this->node->setHostname('hostname');
$this->deployment = new \TYPO3\Surf\Domain\Model\Deployment('TestDeployment');
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $mockLogger */
$mockLogger = $this->getMock('Psr\Log\LoggerInterface');
Expand Down
51 changes: 51 additions & 0 deletions Tests/Unit/Task/DumpDatabaseTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace TYPO3\Surf\Tests\Unit\Task;

/* *
* This script belongs to the TYPO3 project "TYPO3 Surf". *
* *
* */

/**
* Unit test for the DumpDatabaseTaskTest
*/
class DumpDatabaseTaskTest extends BaseTaskTest
{
/**
* Set up test dependencies
*/
protected function setUp()
{
parent::setUp();

$this->application->setDeploymentPath('/home/jdoe/app');
}

/**
* @test
*/
public function executeProperlyEscapesInputOptions()
{
$options = array(
'sourceHost' => 'localhost',
'sourceUser' => 'user',
'sourcePassword' => '(pass)',
'sourceDatabase' => 'db',
'targetHost' => 'localhost',
'targetUser' => 'user',
'targetPassword' => '(pass)',
'targetDatabase' => 'db',
);
$this->task->execute($this->node, $this->application, $this->deployment, $options);

$this->assertCommandExecuted("'mysqldump' '-h' 'localhost' '-u' 'user' '-p(pass)' 'db' | 'ssh' 'hostname' ''\''mysql'\'' '\''-h'\'' '\''localhost'\'' '\''-u'\'' '\''user'\'' '\''-p(pass)'\'' '\''db'\'''");
}

/**
* @return \TYPO3\Surf\Domain\Model\Task
*/
protected function createTask()
{
return new \TYPO3\Surf\Task\DumpDatabaseTask();
}
}
45 changes: 42 additions & 3 deletions src/Task/DumpDatabaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* *
* */

use Symfony\Component\Process\ProcessBuilder;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
Expand Down Expand Up @@ -37,15 +38,53 @@ public function execute(Node $node, Application $application, Deployment $deploy
{
$this->assertRequiredOptionsExist($options);

$dumpCommand = new ProcessBuilder();
$dumpCommand->setPrefix('mysqldump');
$dumpCommand->setArguments(
array(
'-h',
$options['sourceHost'],
'-u',
$options['sourceUser'],
'-p' . $options['sourcePassword'],
$options['sourceDatabase']
)
);

$mysqlCommand = new ProcessBuilder();
$mysqlCommand->setPrefix('mysql');
$mysqlCommand->setArguments(
array(
'-h',
$options['targetHost'],
'-u',
$options['targetUser'],
'-p' . $options['targetPassword'],
$options['targetDatabase']
)
);

$arguments = array();
$username = isset($options['username']) ? $options['username'] . '@' : '';
$hostname = $node->getHostname();
$port = $node->hasOption('port') ? '-P ' . escapeshellarg($node->getOption('port')) : '';
$commands[] = "mysqldump -h {$options['sourceHost']} -u{$options['sourceUser']} -p{$options['sourcePassword']} {$options['sourceDatabase']} | ssh {$port} {$username}{$hostname} 'mysql -h {$options['targetHost']} -u{$options['targetUser']} -p{$options['targetPassword']} {$options['targetDatabase']}'";
$arguments[] = $username . $hostname;
if ($node->hasOption('port')) {
$arguments[] = '-P';
$arguments[] = $node->getOption('port');
}
$arguments[] = $mysqlCommand->getProcess()->getCommandLine();
$sshCommand = new ProcessBuilder();
$sshCommand->setPrefix('ssh');
$sshCommand->setArguments($arguments);

$command = $dumpCommand->getProcess()->getCommandLine()
. ' | '
. $sshCommand->getProcess()->getCommandLine();

$localhost = new Node('localhost');
$localhost->setHostname('localhost');

$this->shell->executeOrSimulate($commands, $localhost, $deployment);
$this->shell->executeOrSimulate($command, $localhost, $deployment);
}

/**
Expand Down

0 comments on commit 799b33f

Please sign in to comment.