From 6db46b50e98d3489a6e5bed5490bb6b27704a42f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 21 Sep 2023 08:58:05 +0200 Subject: [PATCH] Roll back change to allow creating API keys with custom value --- config/autoload/installer.global.php | 2 +- docker/docker-entrypoint.sh | 7 ++++- module/CLI/config/cli.config.php | 3 +- module/CLI/config/dependencies.config.php | 4 +-- ...eKeyCommand.php => GenerateKeyCommand.php} | 15 ++------- .../CLI/test-cli/Command/CreateApiKeyTest.php | 31 ------------------- .../test-cli/Command/GenerateApiKeyTest.php | 22 +++++++++++++ ...andTest.php => GenerateKeyCommandTest.php} | 16 ++-------- 8 files changed, 38 insertions(+), 62 deletions(-) rename module/CLI/src/Command/Api/{CreateKeyCommand.php => GenerateKeyCommand.php} (88%) delete mode 100644 module/CLI/test-cli/Command/CreateApiKeyTest.php create mode 100644 module/CLI/test-cli/Command/GenerateApiKeyTest.php rename module/CLI/test/Command/Api/{CreateKeyCommandTest.php => GenerateKeyCommandTest.php} (81%) diff --git a/config/autoload/installer.global.php b/config/autoload/installer.global.php index de6a69a3..029a50d6 100644 --- a/config/autoload/installer.global.php +++ b/config/autoload/installer.global.php @@ -84,7 +84,7 @@ return [ 'command' => 'bin/cli ' . Command\Visit\DownloadGeoLiteDbCommand::NAME, ], InstallationCommand::API_KEY_GENERATE->value => [ - 'command' => 'bin/cli ' . Command\Api\CreateKeyCommand::NAME, + 'command' => 'bin/cli ' . Command\Api\GenerateKeyCommand::NAME, ], ], ], diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 2058c44c..1fcdfa99 100644 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -12,8 +12,13 @@ fi php vendor/bin/shlink-installer init ${flags} +# TODO If INIT_API_KEY was provided, create an initial API key +#if [ -n "${INIT_API_KEY}" ]; then +# php bin/cli api-key:initial "${INIT_API_KEY}" +#fi + # Periodically run visit:locate every hour, if ENABLE_PERIODIC_VISIT_LOCATE=true was provided and running as root -# ENABLE_PERIODIC_VISIT_LOCATE is deprecated. Remove cron support in Shlink 4.0.0 +# FIXME: ENABLE_PERIODIC_VISIT_LOCATE is deprecated. Remove cron support in Shlink 4.0.0 if [ "${ENABLE_PERIODIC_VISIT_LOCATE}" = "true" ] && [ "${SHLINK_USER_ID}" = "root" ]; then echo "Configuring periodic visit location..." echo "0 * * * * php /etc/shlink/bin/cli visit:locate -q" > /etc/crontabs/root diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 1857bec7..9feeee7b 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -21,8 +21,7 @@ return [ Command\Visit\DeleteOrphanVisitsCommand::NAME => Command\Visit\DeleteOrphanVisitsCommand::class, Command\Visit\GetNonOrphanVisitsCommand::NAME => Command\Visit\GetNonOrphanVisitsCommand::class, - Command\Api\CreateKeyCommand::NAME => Command\Api\CreateKeyCommand::class, - Command\Api\CreateKeyCommand::ALIAS => Command\Api\CreateKeyCommand::class, + Command\Api\GenerateKeyCommand::NAME => Command\Api\GenerateKeyCommand::class, Command\Api\DisableKeyCommand::NAME => Command\Api\DisableKeyCommand::class, Command\Api\ListKeysCommand::NAME => Command\Api\ListKeysCommand::class, diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index c96b13c1..6b7fc552 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -50,7 +50,7 @@ return [ Command\Visit\DeleteOrphanVisitsCommand::class => ConfigAbstractFactory::class, Command\Visit\GetNonOrphanVisitsCommand::class => ConfigAbstractFactory::class, - Command\Api\CreateKeyCommand::class => ConfigAbstractFactory::class, + Command\Api\GenerateKeyCommand::class => ConfigAbstractFactory::class, Command\Api\DisableKeyCommand::class => ConfigAbstractFactory::class, Command\Api\ListKeysCommand::class => ConfigAbstractFactory::class, @@ -102,7 +102,7 @@ return [ Command\Visit\DeleteOrphanVisitsCommand::class => [Visit\VisitsDeleter::class], Command\Visit\GetNonOrphanVisitsCommand::class => [Visit\VisitsStatsHelper::class, ShortUrlStringifier::class], - Command\Api\CreateKeyCommand::class => [ApiKeyService::class, ApiKey\RoleResolver::class], + Command\Api\GenerateKeyCommand::class => [ApiKeyService::class, ApiKey\RoleResolver::class], Command\Api\DisableKeyCommand::class => [ApiKeyService::class], Command\Api\ListKeysCommand::class => [ApiKeyService::class], diff --git a/module/CLI/src/Command/Api/CreateKeyCommand.php b/module/CLI/src/Command/Api/GenerateKeyCommand.php similarity index 88% rename from module/CLI/src/Command/Api/CreateKeyCommand.php rename to module/CLI/src/Command/Api/GenerateKeyCommand.php index 30d4ca58..85f709f8 100644 --- a/module/CLI/src/Command/Api/CreateKeyCommand.php +++ b/module/CLI/src/Command/Api/GenerateKeyCommand.php @@ -13,7 +13,6 @@ use Shlinkio\Shlink\Rest\ApiKey\Role; use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -22,10 +21,9 @@ use Symfony\Component\Console\Style\SymfonyStyle; use function Shlinkio\Shlink\Core\arrayToString; use function sprintf; -class CreateKeyCommand extends Command +class GenerateKeyCommand extends Command { - public const NAME = 'api-key:create'; - public const ALIAS = 'api-key:generate'; + public const NAME = 'api-key:generate'; public function __construct( private readonly ApiKeyServiceInterface $apiKeyService, @@ -60,13 +58,7 @@ class CreateKeyCommand extends Command $this ->setName(self::NAME) - ->setDescription('Creates a new valid API key.') - ->setAliases([self::ALIAS]) - ->addArgument( - 'key', - InputArgument::OPTIONAL, - 'The API key to create. A random one will be generated if not provided', - ) + ->setDescription('Generate a new valid API key.') ->addOption( 'name', 'm', @@ -102,7 +94,6 @@ class CreateKeyCommand extends Command $expirationDate = $input->getOption('expiration-date'); $apiKey = $this->apiKeyService->create(ApiKeyMeta::fromParams( - key: $input->getArgument('key'), name: $input->getOption('name'), expirationDate: isset($expirationDate) ? Chronos::parse($expirationDate) : null, roleDefinitions: $this->roleResolver->determineRoles($input), diff --git a/module/CLI/test-cli/Command/CreateApiKeyTest.php b/module/CLI/test-cli/Command/CreateApiKeyTest.php deleted file mode 100644 index 1b2b9c0d..00000000 --- a/module/CLI/test-cli/Command/CreateApiKeyTest.php +++ /dev/null @@ -1,31 +0,0 @@ -exec([CreateKeyCommand::NAME]); - - self::assertStringContainsString('[OK] Generated API key', $output); - self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); - } - - #[Test] - public function allowsCustomKeyToBeProvided(): void - { - [$output, $exitCode] = $this->exec([CreateKeyCommand::NAME, 'custom_api_key']); - - self::assertStringContainsString('[OK] Generated API key: "custom_api_key"', $output); - self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); - } -} diff --git a/module/CLI/test-cli/Command/GenerateApiKeyTest.php b/module/CLI/test-cli/Command/GenerateApiKeyTest.php new file mode 100644 index 00000000..7d90c336 --- /dev/null +++ b/module/CLI/test-cli/Command/GenerateApiKeyTest.php @@ -0,0 +1,22 @@ +exec([GenerateKeyCommand::NAME]); + + self::assertStringContainsString('[OK] Generated API key', $output); + self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); + } +} diff --git a/module/CLI/test/Command/Api/CreateKeyCommandTest.php b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php similarity index 81% rename from module/CLI/test/Command/Api/CreateKeyCommandTest.php rename to module/CLI/test/Command/Api/GenerateKeyCommandTest.php index 44481114..aedda4ca 100644 --- a/module/CLI/test/Command/Api/CreateKeyCommandTest.php +++ b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php @@ -9,7 +9,7 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface; -use Shlinkio\Shlink\CLI\Command\Api\CreateKeyCommand; +use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; @@ -17,7 +17,7 @@ use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Tester\CommandTester; -class CreateKeyCommandTest extends TestCase +class GenerateKeyCommandTest extends TestCase { use CliTestUtilsTrait; @@ -30,7 +30,7 @@ class CreateKeyCommandTest extends TestCase $roleResolver = $this->createMock(RoleResolverInterface::class); $roleResolver->method('determineRoles')->with($this->isInstanceOf(InputInterface::class))->willReturn([]); - $command = new CreateKeyCommand($this->apiKeyService, $roleResolver); + $command = new GenerateKeyCommand($this->apiKeyService, $roleResolver); $this->commandTester = $this->testerForCommand($command); } @@ -70,14 +70,4 @@ class CreateKeyCommandTest extends TestCase '--name' => 'Alice', ]); } - - #[Test] - public function createsCustomApiKeyWhenProvided(): void - { - $this->apiKeyService->expects($this->once())->method('create')->with( - $this->callback(fn (ApiKeyMeta $meta) => $meta->key === 'my_custom_key'), - )->willReturn(ApiKey::create()); - - $this->commandTester->execute(['key' => 'my_custom_key']); - } }