Skip to content

Commit

Permalink
CLI-1415: [auth:acsf-login] validate factory URL
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Nov 14, 2024
1 parent b136a7a commit 3ad4ef5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Command/App/LogTailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$environment = $this->determineEnvironment($input, $output);
$environment = $this->determineEnvironment($input, $output, true);

Check warning on line 57 in src/Command/App/LogTailCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ } protected function execute(InputInterface $input, OutputInterface $output) : int { - $environment = $this->determineEnvironment($input, $output, true); + $environment = $this->determineEnvironment($input, $output, false); $acquiaCloudClient = $this->cloudApiClientService->getClient(); $logs = $this->promptChooseLogs(); $logTypes = array_map(static function (mixed $log) {
$acquiaCloudClient = $this->cloudApiClientService->getClient();
$logs = $this->promptChooseLogs();
$logTypes = array_map(static function (mixed $log) {
Expand Down
9 changes: 6 additions & 3 deletions src/Command/Auth/AuthAcsfLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ protected function configure(): void
->addOption('factory-url', 'f', InputOption::VALUE_REQUIRED, "Your Site Factory URL (including https://)");
}

/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('factory-url')) {
Expand All @@ -37,7 +40,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
];
$factory = $this->promptChooseFromObjectsOrArrays($factoryChoices, 'url', 'url', 'Choose a Factory to login to');
if ($factory['url'] === 'Enter a new factory URL') {
$factoryUrl = $this->io->ask('Enter the full URL of the factory');
$factoryUrl = $this->determineOption('factory-url', false, $this->validateUrl(...));

Check warning on line 43 in src/Command/Auth/AuthAcsfLoginCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/Auth/AuthAcsfLoginCommand.php#L43

Added line #L43 was not covered by tests
$factory = [
'url' => $factoryUrl,
'users' => [],
Expand All @@ -61,11 +64,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}
} else {
$factoryUrl = $this->determineOption('factory-url');
$factoryUrl = $this->determineOption('factory-url', false, $this->validateUrl(...));

Check warning on line 67 in src/Command/Auth/AuthAcsfLoginCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ return Command::SUCCESS; } } else { - $factoryUrl = $this->determineOption('factory-url', false, $this->validateUrl(...)); + $factoryUrl = $this->determineOption('factory-url', true, $this->validateUrl(...)); } $username = $this->determineOption('username'); $key = $this->determineOption('key', true, $this->validateApiKey(...));
}

$username = $this->determineOption('username');
$key = $this->determineOption('key', true);
$key = $this->determineOption('key', true, $this->validateApiKey(...));

$this->writeAcsfCredentialsToDisk($factoryUrl, $username, $key);
$output->writeln("<info>Saved credentials</info>");
Expand Down
25 changes: 24 additions & 1 deletion src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -610,6 +611,9 @@ private function promptChooseDatabases(
return [$environmentDatabases[$chosenDatabaseIndex]];
}

/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function determineEnvironment(InputInterface $input, OutputInterface $output, bool $allowProduction = false, bool $allowNode = false): array|string|EnvironmentResponse
{
if ($input->getArgument('environmentId')) {
Expand All @@ -628,6 +632,10 @@ protected function determineEnvironment(InputInterface $input, OutputInterface $
}

// Todo: obviously combine this with promptChooseEnvironment.

/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
private function promptChooseEnvironmentConsiderProd(Client $acquiaCloudClient, string $applicationUuid, bool $allowProduction, bool $allowNode): EnvironmentResponse
{
$environmentResource = new Environments($acquiaCloudClient);
Expand Down Expand Up @@ -1657,12 +1665,15 @@ protected function promptOpenBrowserToCreateToken(
}
}

/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function determineApiKey(): string
{
return $this->determineOption('key', false, $this->validateApiKey(...));
}

private function validateApiKey(mixed $key): string
protected static function validateApiKey(mixed $key): string
{
$violations = Validation::createValidator()->validate($key, [
new Length(['min' => 10]),
Expand All @@ -1678,6 +1689,18 @@ private function validateApiKey(mixed $key): string
return $key;
}

protected static function validateUrl(?string $url): string
{
$violations = Validation::createValidator()->validate($url, [

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

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ } protected static function validateUrl(?string $url) : string { - $violations = Validation::createValidator()->validate($url, [new NotBlank(), new Url()]); + $violations = Validation::createValidator()->validate($url, [new Url()]); if (count($violations)) { throw new ValidatorException($violations->get(0)->getMessage()); }
new NotBlank(),
new Url(),
]);
if (count($violations)) {
throw new ValidatorException($violations->get(0)->getMessage());

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

View check run for this annotation

Codecov / codecov/patch

src/Command/CommandBase.php#L1699

Added line #L1699 was not covered by tests
}
return $url;
}

protected function determineApiSecret(): string
{
return $this->determineOption('secret', true, $this->validateApiKey(...));
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Email/ConfigurePlatformEmailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private function addDomainToSubscriptionApplications(Client $client, Subscriptio
/**
* Validates the URL entered as the base domain name.
*/
public static function validateUrl(string $url): string
public static function validateUrl(?string $url): string

Check warning on line 228 in src/Command/Email/ConfigurePlatformEmailCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ /** * Validates the URL entered as the base domain name. */ - public static function validateUrl(?string $url) : string + protected static function validateUrl(?string $url) : string { $constraintsList = [new NotBlank()]; $urlParts = parse_url($url);
{
$constraintsList = [new NotBlank()];
$urlParts = parse_url($url);
Expand Down
5 changes: 4 additions & 1 deletion src/Command/Remote/DrushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ protected function configure(): void
->addUsage('myapp.dev -- status --fields=db-status');
}

/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$environment = $this->determineEnvironment($input, $output);
$environment = $this->determineEnvironment($input, $output, true);
$alias = self::getEnvironmentAlias($environment);
$acliArguments = $input->getArguments();
$drushArguments = (array) $acliArguments['drush_command'];
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/src/Commands/Acsf/AcsfCommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class AcsfCommandTestBase extends CommandTestBase

protected static string $acsfUsername = 'tester';

protected static string $acsfKey = 'h@x0r';
protected static string $acsfKey = 'abcdefghijklmnop';

protected string $apiCommandPrefix = 'acsf';

Expand Down

0 comments on commit 3ad4ef5

Please sign in to comment.