Skip to content

Commit

Permalink
convert requires auth
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Nov 28, 2023
1 parent 9b20369 commit c31353b
Show file tree
Hide file tree
Showing 71 changed files with 112 additions and 113 deletions.
13 changes: 13 additions & 0 deletions src/Attribute/RequireAuth.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 RequireAuth {

}
4 changes: 3 additions & 1 deletion src/Command/Acsf/AcsfApiBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acquia\Cli\Command\Acsf;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\Api\ApiBaseCommand;
use Acquia\Cli\Exception\AcquiaCliException;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -13,7 +14,8 @@
class AcsfApiBaseCommand extends ApiBaseCommand {

protected function checkAuthentication(): void {
if ($this->commandRequiresAuthentication() && !$this->cloudApiClientService->isMachineAuthenticated()) {
$reflectionClass = new \ReflectionClass($this);
if ($reflectionClass->getAttributes(RequireAuth::class) && !$this->cloudApiClientService->isMachineAuthenticated()) {

Check warning on line 18 in src/Command/Acsf/AcsfApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalNot": --- Original +++ New @@ @@ protected function checkAuthentication() : void { $reflectionClass = new \ReflectionClass($this); - if ($reflectionClass->getAttributes(RequireAuth::class) && !$this->cloudApiClientService->isMachineAuthenticated()) { + if ($reflectionClass->getAttributes(RequireAuth::class) && $this->cloudApiClientService->isMachineAuthenticated()) { throw new AcquiaCliException('This machine is not yet authenticated with the Acquia Cloud Site Factory. Run `acli auth:acsf-login`'); } }

Check warning on line 18 in src/Command/Acsf/AcsfApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ protected function checkAuthentication() : void { $reflectionClass = new \ReflectionClass($this); - if ($reflectionClass->getAttributes(RequireAuth::class) && !$this->cloudApiClientService->isMachineAuthenticated()) { + if ($reflectionClass->getAttributes(RequireAuth::class) || !$this->cloudApiClientService->isMachineAuthenticated()) { throw new AcquiaCliException('This machine is not yet authenticated with the Acquia Cloud Site Factory. Run `acli auth:acsf-login`'); } }

Check warning on line 18 in src/Command/Acsf/AcsfApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ protected function checkAuthentication() : void { $reflectionClass = new \ReflectionClass($this); - if ($reflectionClass->getAttributes(RequireAuth::class) && !$this->cloudApiClientService->isMachineAuthenticated()) { + if (!$reflectionClass->getAttributes(RequireAuth::class) && !$this->cloudApiClientService->isMachineAuthenticated()) { throw new AcquiaCliException('This machine is not yet authenticated with the Acquia Cloud Site Factory. Run `acli auth:acsf-login`'); } }
throw new AcquiaCliException('This machine is not yet authenticated with the Acquia Cloud Site Factory. Run `acli auth:acsf-login`');
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Command/Acsf/AcsfListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Acquia\Cli\Command\Acsf;

use Acquia\Cli\Attribute\RequireAuth;
use Symfony\Component\Console\Attribute\AsCommand;

#[RequireAuth]
#[AsCommand(name: 'acsf:list', description: 'List all Acquia Cloud Site Factory commands', aliases: ['acsf'])]
class AcsfListCommand extends AcsfListCommandBase {

Expand Down
8 changes: 0 additions & 8 deletions src/Command/Acsf/AcsfListCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ public function setNamespace(string $namespace): void {
$this->namespace = $namespace;
}

/**
* Indicates whether the command requires the machine to be authenticated with the Cloud Platform.
*/
protected function commandRequiresAuthentication(): bool {
// Assume commands require authentication unless they opt out by overriding this method.
return FALSE;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$commands = $this->getApplication()->all();
foreach ($commands as $command) {
Expand Down
2 changes: 2 additions & 0 deletions src/Command/Api/ApiBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acquia\Cli\Command\Api;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use AcquiaCloudApi\Connector\Client;
use AcquiaCloudApi\Exception\ApiErrorException;
Expand All @@ -21,6 +22,7 @@
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Validation;

#[RequireAuth]
#[AsCommand(name: 'api:base')]
class ApiBaseCommand extends CommandBase {

Expand Down
2 changes: 2 additions & 0 deletions src/Command/Api/ApiListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Acquia\Cli\Command\Api;

use Acquia\Cli\Attribute\RequireAuth;
use Symfony\Component\Console\Attribute\AsCommand;

#[RequireAuth]
#[AsCommand(name: 'api:list', description: 'List all API commands', aliases: ['api'])]
class ApiListCommand extends ApiListCommandBase {

Expand Down
2 changes: 2 additions & 0 deletions src/Command/App/AppOpenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Acquia\Cli\Command\App;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Exception\AcquiaCliException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'app:open', description: 'Opens your browser to view a given Cloud application', aliases: ['open', 'o'])]
class AppOpenCommand extends CommandBase {

Expand Down
2 changes: 2 additions & 0 deletions src/Command/App/AppVcsInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acquia\Cli\Command\App;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Exception\AcquiaCliException;
use AcquiaCloudApi\Endpoints\Code;
Expand All @@ -14,6 +15,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'app:vcs:info', description: 'Get all branches and tags of the application with the deployment status')]
class AppVcsInfo extends CommandBase {

Expand Down
2 changes: 2 additions & 0 deletions src/Command/App/LinkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Acquia\Cli\Command\App;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'app:link', description: 'Associate your project with a Cloud Platform application', aliases: ['link'])]
class LinkCommand extends CommandBase {

Expand Down
2 changes: 2 additions & 0 deletions src/Command/App/LogTailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Acquia\Cli\Command\App;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use AcquiaCloudApi\Endpoints\Logs;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'app:log:tail', description: 'Tail the logs from your environments', aliases: ['tail', 'log:tail'])]
class LogTailCommand extends CommandBase {

Expand Down
4 changes: 0 additions & 4 deletions src/Command/App/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

private function createNextJsProject(string $dir): void {
$process = $this->localMachineHelper->execute([
'npx',
Expand Down
4 changes: 0 additions & 4 deletions src/Command/App/NewFromDrupal7Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,6 @@ function (mixed $path): string {
return Command::SUCCESS;
}

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

private function initializeGitRepository(string $dir): void {
if ($this->localMachineHelper->getFilesystem()->exists(Path::join($dir, '.git'))) {
$this->logger->debug('.git directory detected, skipping Git repo initialization');
Expand Down
2 changes: 2 additions & 0 deletions src/Command/App/TaskWaitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Acquia\Cli\Command\App;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'app:task-wait', description: 'Wait for a task to complete')]
class TaskWaitCommand extends CommandBase {

Expand Down
4 changes: 0 additions & 4 deletions src/Command/App/UnlinkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#[AsCommand(name: 'app:unlink', description: 'Remove local association between your project and a Cloud Platform application', aliases: ['unlink'])]
class UnlinkCommand extends CommandBase {

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$this->validateCwdIsValidDrupalProject();

Expand Down
2 changes: 2 additions & 0 deletions src/Command/Archive/ArchiveExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acquia\Cli\Command\Archive;

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

#[RequireAuth]
#[AsCommand(name: 'archive:export', description: 'Export an archive of the Drupal application including code, files, and database')]
class ArchiveExportCommand extends CommandBase {

Expand Down
4 changes: 0 additions & 4 deletions src/Command/Auth/AuthAcsfLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ protected function configure(): void {
->addOption('factory-url', 'f', InputOption::VALUE_REQUIRED, "Your Site Factory URL");
}

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
if ($input->getOption('factory-url')) {
$factoryUrl = $input->getOption('factory-url');
Expand Down
4 changes: 0 additions & 4 deletions src/Command/Auth/AuthAcsfLogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#[AsCommand(name: 'auth:acsf-logout', description: 'Remove your Site Factory key and secret from your local machine.')]
class AuthAcsfLogoutCommand extends CommandBase {

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$factories = $this->datastoreCloud->get('acsf_factories');
if (empty($factories)) {
Expand Down
4 changes: 0 additions & 4 deletions src/Command/Auth/AuthLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected function configure(): void {
->addOption('secret', 's', InputOption::VALUE_REQUIRED, 'Your Cloud API secret');
}

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
if ($this->cloudApiClientService->isMachineAuthenticated()) {
$answer = $this->io->confirm('Your machine has already been authenticated with the Cloud Platform API, would you like to re-authenticate?');
Expand Down
4 changes: 0 additions & 4 deletions src/Command/Auth/AuthLogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#[AsCommand(name: 'auth:logout', description: 'Remove Cloud API key and secret from local machine.', aliases: ['logout'])]
class AuthLogoutCommand extends CommandBase {

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
if ($this->cloudApiClientService->isMachineAuthenticated()) {
$answer = $this->io->confirm('Are you sure you\'d like to unset the Acquia Cloud API key for Acquia CLI?');
Expand Down
2 changes: 2 additions & 0 deletions src/Command/CodeStudio/CodeStudioPhpVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Acquia\Cli\Command\CodeStudio;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Gitlab\Exception\RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'codestudio:php-version', description: 'Change the PHP version in Code Studio')]
class CodeStudioPhpVersionCommand extends CommandBase {

Expand Down
4 changes: 0 additions & 4 deletions src/Command/CodeStudio/CodeStudioPipelinesMigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

/**
* Check whether wizard command is executed by checking the env variable of codestudio project.
*
Expand Down
4 changes: 0 additions & 4 deletions src/Command/CodeStudio/CodeStudioWizardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

/**
* @param array $project
* @return array<mixed>|null
Expand Down
15 changes: 5 additions & 10 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Acquia\Cli\Command;

use Acquia\Cli\ApiCredentialsInterface;
use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\CloudApi\ClientService;
use Acquia\Cli\Command\Ssh\SshKeyCommandBase;
use Acquia\Cli\DataStore\AcquiaCliDatastore;
Expand Down Expand Up @@ -112,7 +113,8 @@ public function __construct(
$this->setLocalDbName();
$this->setLocalDbHost();
parent::__construct();
if ($this->commandRequiresAuthentication()) {
$reflectionClass = new \ReflectionClass($this);
if ($reflectionClass->getAttributes(RequireAuth::class)) {
$this->appendHelp('This command requires authentication via the Cloud Platform API.');
}
if ($this->commandRequiresDatabase()) {
Expand Down Expand Up @@ -301,14 +303,6 @@ protected function acceptSite(): self {
return $this;
}

/**
* Indicates whether the command requires the machine to be authenticated with the Cloud Platform.
*/
protected function commandRequiresAuthentication(): bool {
// Assume commands require authentication unless they opt out by overriding this method.
return TRUE;
}

protected function commandRequiresDatabase(): bool {
return FALSE;
}
Expand Down Expand Up @@ -1477,7 +1471,8 @@ protected function validateEnvironmentUuid(mixed $envUuidArgument, mixed $argume
}

protected function checkAuthentication(): void {
if ($this->commandRequiresAuthentication() && !$this->cloudApiClientService->isMachineAuthenticated()) {
$reflectionClass = new \ReflectionClass($this);
if ($reflectionClass->getAttributes(RequireAuth::class) && !$this->cloudApiClientService->isMachineAuthenticated()) {
throw new AcquiaCliException('This machine is not yet authenticated with the Cloud Platform. Run `acli auth:login`');
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/Command/DocsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected function configure(): void {
->addUsage('acli');
}

protected function commandRequiresAuthentication(): bool {
return FALSE;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$acquiaProducts = [
'Acquia CLI' => [
Expand Down
2 changes: 2 additions & 0 deletions src/Command/Email/ConfigurePlatformEmailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acquia\Cli\Command\Email;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Exception\AcquiaCliException;
use Acquia\Cli\Output\Checklist;
Expand All @@ -25,6 +26,7 @@
use Symfony\Component\Validator\Validation;
use Symfony\Component\Yaml\Yaml;

#[RequireAuth]
#[AsCommand(name: 'email:configure', description: 'Configure Platform email for one or more applications', aliases: ['ec'])]
class ConfigurePlatformEmailCommand extends CommandBase {

Expand Down
2 changes: 2 additions & 0 deletions src/Command/Email/EmailInfoForSubscriptionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acquia\Cli\Command\Email;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use AcquiaCloudApi\Connector\Client;
use AcquiaCloudApi\Response\SubscriptionResponse;
Expand All @@ -18,6 +19,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'email:info', description: 'Print information related to Platform Email set up in a subscription.')]
class EmailInfoForSubscriptionCommand extends CommandBase {

Expand Down
2 changes: 2 additions & 0 deletions src/Command/Env/EnvCertCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acquia\Cli\Command\Env;

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use AcquiaCloudApi\Endpoints\SslCertificates;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -13,6 +14,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[RequireAuth]
#[AsCommand(name: 'env:certificate-create', description: 'Install an SSL certificate.')]
class EnvCertCreateCommand extends CommandBase {

Expand Down
Loading

0 comments on commit c31353b

Please sign in to comment.