From dd7fdfdc35422ab9ad60618c3c57769d3edf8e18 Mon Sep 17 00:00:00 2001 From: Adam Malone Date: Mon, 16 Mar 2020 11:49:54 +1100 Subject: [PATCH] Adds in basic info/list commands for SSL Certificates and Log Forwarding. --- src/Cli/AcquiaCli.php | 2 + src/Commands/LogForwardCommand.php | 92 ++++++++++++++++++++ src/Commands/SslCertificateCommand.php | 89 +++++++++++++++++++ src/Injector/AcquiaCliInjector.php | 6 ++ tests/AcquiaCliTestCase.php | 24 ++++- tests/Commands/LogForwardCommandTest.php | 56 ++++++++++++ tests/Commands/SslCertificateCommandTest.php | 61 +++++++++++++ 7 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 src/Commands/LogForwardCommand.php create mode 100644 src/Commands/SslCertificateCommand.php create mode 100644 tests/Commands/LogForwardCommandTest.php create mode 100644 tests/Commands/SslCertificateCommandTest.php diff --git a/src/Cli/AcquiaCli.php b/src/Cli/AcquiaCli.php index 68d53a1..cc650c1 100644 --- a/src/Cli/AcquiaCli.php +++ b/src/Cli/AcquiaCli.php @@ -156,6 +156,8 @@ public function injectParameters($container) $parameterInjection->register('AcquiaCloudApi\Endpoints\Logs', new AcquiaCliInjector); $parameterInjection->register('AcquiaCloudApi\Endpoints\Notifications', new AcquiaCliInjector); $parameterInjection->register('AcquiaCloudApi\Endpoints\Insights', new AcquiaCliInjector); + $parameterInjection->register('AcquiaCloudApi\Endpoints\LogForwardingDestinations', new AcquiaCliInjector); + $parameterInjection->register('AcquiaCloudApi\Endpoints\SslCertificates', new AcquiaCliInjector); } /** diff --git a/src/Commands/LogForwardCommand.php b/src/Commands/LogForwardCommand.php new file mode 100644 index 0000000..2ca7cce --- /dev/null +++ b/src/Commands/LogForwardCommand.php @@ -0,0 +1,92 @@ +cloudapiService->getEnvironment($uuid, $environment); + $logForwards = $logForwardAdapter->getAll($environment->uuid); + + $table = new Table($output); + $table->setHeaders(['UUID', 'Label', 'Address', 'Consumer', 'Active']); + $table->setColumnStyle(1, 'center-align'); + $table->setColumnStyle(2, 'center-align'); + $table->setColumnStyle(3, 'center-align'); + $table->setColumnStyle(4, 'center-align'); + + foreach ($logForwards as $logForward) { + /** + * @var LogForwardingDestinationResponse $logForward + */ + $table + ->addRows( + [ + [ + $logForward->uuid, + $logForward->label, + $logForward->address, + $logForward->consumer, + $logForward->status === 'active' ? '✓' : '', + ], + ] + ); + } + + $table->render(); + } + + /** + * Gets information about a Log ForwaRD. + * + * @param string $uuid + * @param string $environment + * @param int $destinationId + * + * @command logforward:info + * @aliases lf:info + */ + public function logforwardInfo( + OutputInterface $output, + LogForwardingDestinations $logForwardAdapter, + $uuid, + $environment, + $destinationId + ) { + $environment = $this->cloudapiService->getEnvironment($uuid, $environment); + $logForward = $logForwardAdapter->get($environment->uuid, $destinationId); + + $this->yell(sprintf('Log server address: %s', $logForward->address)); + $this->say(sprintf('Certificate: %s', $logForward->credentials->certificate->certificate)); + $this->say(sprintf('Expires at: %s', $logForward->credentials->certificate->expires_at)); + $this->say(sprintf('Token: %s', $logForward->credentials->token)); + $this->say(sprintf('Key: %s', $logForward->credentials->key)); + $this->say(sprintf('Sources: %s%s', "\n", implode($logForward->sources, "\n"))); + $this->say(sprintf('Health: %s', $logForward->health->summary)); + } +} diff --git a/src/Commands/SslCertificateCommand.php b/src/Commands/SslCertificateCommand.php new file mode 100644 index 0000000..738e866 --- /dev/null +++ b/src/Commands/SslCertificateCommand.php @@ -0,0 +1,89 @@ +cloudapiService->getEnvironment($uuid, $environment); + $certificates = $certificatesAdapter->getAll($environment->uuid); + + $table = new Table($output); + $table->setHeaders(['ID', 'Label', 'Domains', 'Expires', 'Active']); + $table->setColumnStyle(1, 'center-align'); + $table->setColumnStyle(2, 'center-align'); + $table->setColumnStyle(3, 'center-align'); + $table->setColumnStyle(4, 'center-align'); + + foreach ($certificates as $certificate) { + /** + * @var SslCertificateResponse $certificate + */ + $table + ->addRows( + [ + [ + $certificate->id, + $certificate->label, + implode($certificate->domains, "\n"), + $certificate->expires_at, + $certificate->flags->active ? '✓' : '', + ], + ] + ); + } + + $table->render(); + } + + /** + * Gets information about an SSL certificate. + * + * @param string $uuid + * @param string $environment + * @param int $certificateId + * + * @command ssl:info + */ + public function sslCertificateInfo( + OutputInterface $output, + SslCertificates $certificatesAdapter, + $uuid, + $environment, + $certificateId + ) { + $environment = $this->cloudapiService->getEnvironment($uuid, $environment); + $certificate = $certificatesAdapter->get($environment->uuid, $certificateId); + + $this->yell('Certificate'); + $this->writeln($certificate->certificate); + $this->yell('CA'); + $this->writeln($certificate->ca); + $this->yell('Private Key'); + $this->writeln($certificate->private_key); + } +} diff --git a/src/Injector/AcquiaCliInjector.php b/src/Injector/AcquiaCliInjector.php index 4626c2a..3d25849 100644 --- a/src/Injector/AcquiaCliInjector.php +++ b/src/Injector/AcquiaCliInjector.php @@ -21,6 +21,8 @@ use AcquiaCloudApi\Endpoints\Logs; use AcquiaCloudApi\Endpoints\Notifications; use AcquiaCloudApi\Endpoints\Insights; +use AcquiaCloudApi\Endpoints\LogForwardingDestinations; +use AcquiaCloudApi\Endpoints\SslCertificates; class AcquiaCliInjector implements ParameterInjector { @@ -81,6 +83,10 @@ public function get(CommandData $commandData, $interfaceName) return new Notifications($this->client); case 'AcquiaCloudApi\Endpoints\Insights': return new Insights($this->client); + case 'AcquiaCloudApi\Endpoints\LogForwardingDestinations': + return new LogForwardingDestinations($this->client); + case 'AcquiaCloudApi\Endpoints\SslCertificates': + return new SslCertificates($this->client); } } } diff --git a/tests/AcquiaCliTestCase.php b/tests/AcquiaCliTestCase.php index 167c6ad..cdd17c5 100644 --- a/tests/AcquiaCliTestCase.php +++ b/tests/AcquiaCliTestCase.php @@ -119,6 +119,9 @@ protected function getMockLogstream() return $logstream; } + /** + * Callback for the mock client. + */ public function sendRequestCallback($verb, $path) { $fixtureMap = self::getFixtureMap(); @@ -136,9 +139,14 @@ public function sendRequestCallback($verb, $path) } } + /** + * Run commands with a mock client. + * + * @see bin/acquia-robo.php + */ public function execute($command) { - + // Create an instance of the application and use some default parameters. $root = dirname(dirname(__DIR__)); $config = new Config($root); $loader = new YamlConfigLoader(); @@ -161,6 +169,8 @@ public function execute($command) $app->run($input, $output); + // Unset the container so we're dealing with a fresh state for each command + // This mimics the behaviour expected by users interacting with the application. Robo::unsetContainer(); return $output->fetch(); @@ -395,6 +405,18 @@ public static function getFixtureMap() ], '/notifications/42b56cff-0b55-4bdf-a949-1fd0fca61c6c' => [ 'get' => 'Notifications/getNotification.json' + ], + '/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/log-forwarding-destinations' => [ + 'get' => 'LogForwarding/getAllLogForwarding.json' + ], + '/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/log-forwarding-destinations/1234' => [ + 'get' => 'LogForwarding/getLogForwarding.json' + ], + '/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates' => [ + 'get' => 'SslCertificates/getAllSslCertificates.json' + ], + '/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates/1234' => [ + 'get' => 'SslCertificates/getSslCertificate.json' ] ]; } diff --git a/tests/Commands/LogForwardCommandTest.php b/tests/Commands/LogForwardCommandTest.php new file mode 100644 index 0000000..f2a61d9 --- /dev/null +++ b/tests/Commands/LogForwardCommandTest.php @@ -0,0 +1,56 @@ +execute($command); + $this->assertSame($expected, $actualResponse); + } + + public function logForwardProvider() + { + + $listResponse = << Certificate: -----BEGIN CERTIFICATE-----...-----END CERTIFICATE----- +> Expires at: 2018-07-16T16:15:33+00:00 +> Token: 204d892b449026f6e4ded264c8891c400df8fc8905f07beb5f70d706f6d4d5e5 +> Key: 1d0789d519c0b943cf38f401d30ffbdcd2e0c4cfb7c32ebc0c872bce62aadd4d +> Sources: +apache-access +apache-error +> Health: OK +INFO; + + return [ + [ + ['lf:list', 'devcloud:devcloud2', 'dev'], + $listResponse . PHP_EOL + ], + [ + ['lf:info', 'devcloud:devcloud2', 'dev', 1234], + $infoResponse . PHP_EOL, + ] + ]; + } +} diff --git a/tests/Commands/SslCertificateCommandTest.php b/tests/Commands/SslCertificateCommandTest.php new file mode 100644 index 0000000..159509b --- /dev/null +++ b/tests/Commands/SslCertificateCommandTest.php @@ -0,0 +1,61 @@ +execute($command); + $this->assertSame($expected, $actualResponse); + } + + public function sslCertificateProvider() + { + + $listResponse = <<