Skip to content

Commit

Permalink
CLI-1402: Require view database connection details permission
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Oct 10, 2024
1 parent 80b02e2 commit c845883
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 168 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"symfony/yaml": "^6.3",
"thecodingmachine/safe": "^2.4",
"typhonius/acquia-logstream": "^0.0.13",
"typhonius/acquia-php-sdk-v2": "^3.1.1",
"typhonius/acquia-php-sdk-v2": "dev-master as 3.3.1",
"vlucas/phpdotenv": "^5.5",
"zumba/amplitude-php": "^1.0.4"
},
Expand Down
316 changes: 162 additions & 154 deletions composer.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
* Specify that a command requires authentication.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class RequireDb
class RequireLocalDb
{
}
13 changes: 13 additions & 0 deletions src/Attribute/RequireRemoteDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Acquia\Cli\Attribute;

/**
* Specify that a command requires authentication.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class RequireRemoteDb
{
}
4 changes: 2 additions & 2 deletions src/Command/Archive/ArchiveExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Acquia\Cli\Command\Archive;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Attribute\RequireDb;
use Acquia\Cli\Attribute\RequireLocalDb;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Exception\AcquiaCliException;
use Acquia\Cli\Output\Checklist;
Expand All @@ -21,7 +21,7 @@
use Symfony\Component\Filesystem\Path;

#[RequireAuth]
#[RequireDb]
#[RequireLocalDb]
#[AsCommand(name: 'archive:export', description: 'Export an archive of the Drupal application including code, files, and database')]
final class ArchiveExportCommand extends CommandBase
{
Expand Down
15 changes: 13 additions & 2 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Acquia\Cli\AcsfApi\AcsfClientService;
use Acquia\Cli\ApiCredentialsInterface;
use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Attribute\RequireDb;
use Acquia\Cli\Attribute\RequireLocalDb;
use Acquia\Cli\Attribute\RequireRemoteDb;
use Acquia\Cli\CloudApi\ClientService;
use Acquia\Cli\Command\Ssh\SshKeyCommandBase;
use Acquia\Cli\DataStore\AcquiaCliDatastore;
Expand Down Expand Up @@ -69,6 +70,7 @@
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Yaml\Yaml;
use TypeError;
use Zumba\Amplitude\Amplitude;

abstract class CommandBase extends Command implements LoggerAwareInterface
Expand Down Expand Up @@ -119,10 +121,13 @@ public function __construct(
if ((new \ReflectionClass(static::class))->getAttributes(RequireAuth::class)) {
$this->appendHelp('This command requires authentication via the Cloud Platform API.');
}
if ((new \ReflectionClass(static::class))->getAttributes(RequireDb::class)) {
if ((new \ReflectionClass(static::class))->getAttributes(RequireLocalDb::class)) {
$this->appendHelp('This command requires an active database connection. Set the following environment variables prior to running this command: '
. 'ACLI_DB_HOST, ACLI_DB_NAME, ACLI_DB_USER, ACLI_DB_PASSWORD');
}
if ((new \ReflectionClass(static::class))->getAttributes(RequireRemoteDb::class)) {
$this->appendHelp('This command requires the \'View database connection details\' permission.');

Check warning on line 129 in src/Command/CommandBase.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $this->appendHelp('This command requires an active database connection. Set the following environment variables prior to running this command: ' . 'ACLI_DB_HOST, ACLI_DB_NAME, ACLI_DB_USER, ACLI_DB_PASSWORD'); } if ((new \ReflectionClass(static::class))->getAttributes(RequireRemoteDb::class)) { - $this->appendHelp('This command requires the \'View database connection details\' permission.'); + } } public function appendHelp(string $helpText) : void
}
}

public function appendHelp(string $helpText): void
Expand Down Expand Up @@ -521,11 +526,17 @@ protected function getLocalFilesDir(string $site): string
/**
* @param string|null $site
* @return DatabaseResponse[]
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function determineCloudDatabases(Client $acquiaCloudClient, EnvironmentResponse $chosenEnvironment, string $site = null, bool $multipleDbs = false): array
{
$databasesRequest = new Databases($acquiaCloudClient);
$databases = $databasesRequest->getAll($chosenEnvironment->uuid);
foreach ($databases as $database) {

Check warning on line 535 in src/Command/CommandBase.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ { $databasesRequest = new Databases($acquiaCloudClient); $databases = $databasesRequest->getAll($chosenEnvironment->uuid); - foreach ($databases as $database) { + foreach (array() as $database) { if ($database->user_name === null) { throw new AcquiaCliException('Database connection details missing'); }
if ($database->user_name === null) {
throw new AcquiaCliException('Database connection details missing');

Check warning on line 537 in src/Command/CommandBase.php

View check run for this annotation

Codecov / codecov/patch

src/Command/CommandBase.php#L537

Added line #L537 was not covered by tests
}
}

if (count($databases) === 1) {
$this->logger->debug('Only a single database detected on Cloud');
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Pull/PullCodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Acquia\Cli\Command\Pull;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Attribute\RequireDb;
use Acquia\Cli\Attribute\RequireLocalDb;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -14,7 +14,7 @@
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[RequireDb]
#[RequireLocalDb]
#[AsCommand(name: 'pull:code', description: 'Copy code from a Cloud Platform environment')]
final class PullCodeCommand extends PullCommandBase
{
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Pull/PullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Acquia\Cli\Command\Pull;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Attribute\RequireDb;
use Acquia\Cli\Attribute\RequireLocalDb;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -14,7 +14,7 @@
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[RequireDb]
#[RequireLocalDb]
#[AsCommand(name: 'pull:all', description: 'Copy code, database, and files from a Cloud Platform environment', aliases: [
'refresh',
'pull',
Expand Down
6 changes: 4 additions & 2 deletions src/Command/Pull/PullDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
namespace Acquia\Cli\Command\Pull;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Attribute\RequireDb;
use Acquia\Cli\Attribute\RequireLocalDb;
use Acquia\Cli\Attribute\RequireRemoteDb;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[RequireDb]
#[RequireLocalDb]
#[RequireRemoteDb]
#[AsCommand(name: 'pull:database', description: 'Import database backup from a Cloud Platform environment', aliases: ['pull:db'])]
final class PullDatabaseCommand extends PullCommandBase
{
Expand Down
6 changes: 4 additions & 2 deletions src/Command/Push/PushDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Acquia\Cli\Command\Push;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Attribute\RequireDb;
use Acquia\Cli\Attribute\RequireLocalDb;
use Acquia\Cli\Attribute\RequireRemoteDb;
use Acquia\Cli\Exception\AcquiaCliException;
use Acquia\Cli\Output\Checklist;
use AcquiaCloudApi\Response\DatabaseResponse;
Expand All @@ -16,7 +17,8 @@
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[RequireDb]
#[RequireLocalDb]
#[RequireRemoteDb]
#[AsCommand(name: 'push:database', description: 'Push a database from your local environment to a Cloud Platform environment', aliases: ['push:db'])]
final class PushDatabaseCommand extends PushCommandBase
{
Expand Down
3 changes: 3 additions & 0 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function onConsoleError(ConsoleErrorEvent $event): void
$this->helpMessages[] = 'Check for MySQL warnings above or in the server log (/var/log/mysql/error.log)';
$this->helpMessages[] = 'Frequently, `MySQL server has gone away` messages are caused by max_allowed_packet being exceeded.';
break;
case 'Database connection details missing':
$this->helpMessages[] = 'Check that you have the \'View database connection details\' permission';
break;

Check warning on line 80 in src/EventListener/ExceptionListener.php

View check run for this annotation

Codecov / codecov/patch

src/EventListener/ExceptionListener.php#L79-L80

Added lines #L79 - L80 were not covered by tests
}
}

Expand Down

0 comments on commit c845883

Please sign in to comment.