Skip to content

Commit

Permalink
3888 database dump escape (#3906)
Browse files Browse the repository at this point in the history
* Add target argument to address specific db. Add escape of command line parameters where it is necessary. Replace double quotes with single quotes. Improve the code standards

* Replace translation string for restore db command
  • Loading branch information
LOBsTerr committed Apr 6, 2019
1 parent 9cff0df commit 7103912
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 66 deletions.
20 changes: 9 additions & 11 deletions src/Command/Database/ClientCommand.php
Expand Up @@ -32,6 +32,12 @@ protected function configure()
$this->trans('commands.database.client.arguments.database'),
'default'
)
->addArgument(
'target',
InputArgument::OPTIONAL,
$this->trans('commands.database.client.arguments.target'),
'default'
)
->setHelp($this->trans('commands.database.client.help'))
->setAliases(['dbc']);
}
Expand All @@ -43,18 +49,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$database = $input->getArgument('database');
$learning = $input->getOption('learning');
$target = $input->getArgument('target');

$databaseConnection = $this->resolveConnection($database);

$connection = sprintf(
'%s --database=%s --user=%s --password=%s --host=%s --port=%s',
$databaseConnection['driver'],
$databaseConnection['database'],
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port']
);
$databaseConnection = $this->resolveConnection($database, $target);
$connection = $this->getConnectionString($databaseConnection);

if ($learning) {
$this->getIo()->commentBlock(
Expand Down
12 changes: 1 addition & 11 deletions src/Command/Database/ConnectCommand.php
Expand Up @@ -50,20 +50,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$target = $input->getArgument('target');
$databaseConnection = $this->resolveConnection($key, $target);

$connection = sprintf(
'%s -A --database=%s --user=%s --password=%s --host=%s --port=%s',
$databaseConnection['driver'],
$databaseConnection['database'],
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port']
);

$this->getIo()->commentBlock(
sprintf(
$this->trans('commands.database.connect.messages.connection'),
$connection
escapeshellcmd($this->getConnectionString($databaseConnection))
)
);

Expand Down
11 changes: 9 additions & 2 deletions src/Command/Database/DumpCommand.php
Expand Up @@ -55,6 +55,12 @@ protected function configure()
$this->trans('commands.database.dump.arguments.database'),
'default'
)
->addArgument(
'target',
InputArgument::OPTIONAL,
$this->trans('commands.database.dump.arguments.target'),
'default'
)
->addOption(
'file',
null,
Expand All @@ -77,11 +83,12 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$database = $input->getArgument('database');
$target = $input->getArgument('target');
$file = $input->getOption('file');
$learning = $input->getOption('learning');
$gz = $input->getOption('gz');

$databaseConnection = $this->resolveConnection($database);
$databaseConnection = $this->escapeConnection($this->resolveConnection($database, $target));

if (!$file) {
$date = new \DateTime();
Expand Down Expand Up @@ -125,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$resultFile = $file;
if ($gz) {
if (substr($file, -3) != '.gz') {
$resultFile = $file . ".gz";
$resultFile = $file . '.gz';
}
file_put_contents(
$resultFile,
Expand Down
43 changes: 25 additions & 18 deletions src/Command/Database/QueryCommand.php
Expand Up @@ -43,6 +43,12 @@ protected function configure()
$this->trans('commands.database.query.arguments.database'),
'default'
)
->addArgument(
'target',
InputArgument::OPTIONAL,
$this->trans('commands.database.connect.arguments.target'),
'default'
)
->addOption('quick', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.quick'))
->addOption('debug', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.debug'))
->addOption('html', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.html'))
Expand All @@ -62,9 +68,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$query = $input->getArgument('query');
$database = $input->getArgument('database');
$target = $input->getArgument('target');
$learning = $input->getOption('learning');

$databaseConnection = $this->resolveConnection($database);
$databaseConnection = $this->resolveConnection($database, $target);

$connection = sprintf(
'%s -A --database=%s --user=%s --password=%s --host=%s --port=%s',
Expand All @@ -79,31 +86,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
$args = explode(' ', $connection);
$args[] = sprintf('--execute=%s', $query);

$opts = ["quick", "debug", "html", "xml", "raw", "vertical", "batch"];
$opts = ['quick', 'debug', 'html', 'xml', 'raw', 'vertical', 'batch'];
array_walk(
$opts, function ($opt) use ($input, &$args) {
if ($input->getOption($opt)) {
switch ($opt) {
case "quick":
$args[] = "--quick";
case 'quick':
$args[] = '--quick';
break;
case "debug":
$args[] = "-T";
case 'debug':
$args[] = '-T';
break;
case "html":
$args[] = "-H";
case 'html':
$args[] = '-H';
break;
case "xml":
$args[] = "-X";
case 'xml':
$args[] = '-X';
break;
case "raw":
$args[] = "--raw";
case 'raw':
$args[] = '--raw';
break;
case "vertical":
$args[] = "-E";
case 'vertical':
$args[] = '-E';
break;
case "batch":
$args[] = "--batch";
case 'batch':
$args[] = '--batch';
break;
}
}
Expand All @@ -112,11 +119,11 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($learning) {
$this->getIo()->commentBlock(
implode(" ", $args)
implode(' ', $args)
);
}

$processBuilder = new ProcessBuilder([]);
$processBuilder = new ProcessBuilder();
$processBuilder->setArguments($args);
$process = $processBuilder->getProcess();
$process->setTty('true');
Expand Down
40 changes: 24 additions & 16 deletions src/Command/Database/RestoreCommand.php
Expand Up @@ -49,6 +49,12 @@ protected function configure()
$this->trans('commands.database.restore.arguments.database'),
'default'
)
->addArgument(
'target',
InputArgument::OPTIONAL,
$this->trans('commands.database.restore.arguments.target'),
'default'
)
->addOption(
'file',
null,
Expand All @@ -66,11 +72,11 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$database = $input->getArgument('database');
$target = $input->getArgument('target');
$file = $input->getOption('file');
$learning = $input->getOption('learning');

$databaseConnection = $this->resolveConnection($database);

$databaseConnection = $this->escapeConnection($this->resolveConnection($database, $target));
if (!$file) {
$this->getIo()->error(
$this->trans('commands.database.restore.messages.no-file')
Expand All @@ -82,25 +88,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
$catCommand = 'cat %s | ';
}

$command = NULL;
if ($databaseConnection['driver'] == 'mysql') {
$command = sprintf(
$catCommand . 'mysql --user=%s --password=%s --host=%s --port=%s %s',
$file,
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database']
$catCommand . 'mysql --user=%s --password=%s --host=%s --port=%s %s',
$file,
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database']
);
} elseif ($databaseConnection['driver'] == 'pgsql') {
$command = sprintf(
$catCommand . 'PGPASSWORD="%s" psql -w -U %s -h %s -p %s -d %s',
$file,
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database']
$catCommand . 'PGPASSWORD="%s" psql -w -U %s -h %s -p %s -d %s',
$file,
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database']
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/Debug/DatabaseTableCommand.php
Expand Up @@ -77,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$databaseConnection = $this->resolveConnection($database);
if ($table) {
$result = $this->database
->query('DESCRIBE '. $table .';')
->query('DESCRIBE ' . $table . ';')
->fetchAll();
if (!$result) {
throw new \Exception(
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Module/DownloadCommand.php
Expand Up @@ -225,7 +225,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

// Register composer repository
$command = "composer config repositories.drupal composer https://packages.drupal.org/8";
$command = 'composer config repositories.drupal composer https://packages.drupal.org/8';
$this->shellProcess->exec($command, $this->root);

$command = sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Module/InstallCommand.php
Expand Up @@ -160,7 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$processBuilder = new ProcessBuilder([]);
$processBuilder->setWorkingDirectory($this->appRoot);
$processBuilder->setArguments(explode(" ", $command));
$processBuilder->setArguments(explode(' ', $command));
$process = $processBuilder->getProcess();
$process->setTty('true');
$process->run();
Expand Down
8 changes: 4 additions & 4 deletions src/Command/Module/UpdateCommand.php
Expand Up @@ -112,20 +112,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if (count($modules) > 1) {
$modules = " drupal/" . implode(" drupal/", $modules);
$modules = ' drupal/' . implode(' drupal/', $modules);
} else {
$modules = " drupal/" . current($modules);
$modules = ' drupal/' . current($modules);
}

if ($composer) {
// Register composer repository
$command = "composer config repositories.drupal composer https://packages.drupal.org/8";
$command = 'composer config repositories.drupal composer https://packages.drupal.org/8';
$this->shellProcess->exec($command, $this->root);

$command = 'composer update ' . $modules . ' --optimize-autoloader --prefer-dist --no-dev --root-reqs ';

if ($simulate) {
$command .= " --dry-run";
$command .= ' --dry-run';
}

if ($this->shellProcess->exec($command, $this->root)) {
Expand Down
26 changes: 25 additions & 1 deletion src/Command/Shared/ConnectTrait.php
Expand Up @@ -66,4 +66,28 @@ public function getRedBeanConnection($database = 'default')

return null;
}
}

public function getConnectionString($databaseConnection) {
return sprintf(
'%s -A --database=%s --user=%s --password=%s --host=%s --port=%s',
$databaseConnection['driver'],
$databaseConnection['database'],
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port']
);
}

public function escapeConnection($databaseConnection) {
$settings = [
'driver', 'database', 'username', 'password', 'host', 'port'
];

foreach ($settings as $setting) {
$databaseConnection[$setting] = escapeshellcmd($databaseConnection[$setting]);
}

return $databaseConnection;
}
}

0 comments on commit 7103912

Please sign in to comment.