Skip to content

Commit

Permalink
Adds in basic info/list commands for SSL Certificates and Log Forward…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
typhonius committed Mar 16, 2020
1 parent 88fea0c commit dd7fdfd
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Cli/AcquiaCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
92 changes: 92 additions & 0 deletions src/Commands/LogForwardCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace AcquiaCli\Commands;

use AcquiaCloudApi\Response\EnvironmentResponse;
use AcquiaCloudApi\Endpoints\LogForwardingDestinations;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class LogForwardCommand
*
* @package AcquiaCli\Commands
*/
class LogForwardCommand extends AcquiaCommand
{

/**
* Lists Log Forwards.
*
* @param string $uuid
* @param string $environment
*
* @command logforward:list
* @aliases lf:list
*/
public function logforwardList(
OutputInterface $output,
LogForwardingDestinations $logForwardAdapter,
$uuid,
$environment
) {
$environment = $this->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));
}
}
89 changes: 89 additions & 0 deletions src/Commands/SslCertificateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace AcquiaCli\Commands;

use AcquiaCloudApi\Response\EnvironmentResponse;
use AcquiaCloudApi\Endpoints\SslCertificates;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class SslCertificateCommand
*
* @package AcquiaCli\Commands
*/
class SslCertificateCommand extends AcquiaCommand
{

/**
* Lists SSL Certificates.
*
* @param string $uuid
* @param string $environment
*
* @command ssl:list
*/
public function sslCertificateList(
OutputInterface $output,
SslCertificates $certificatesAdapter,
$uuid,
$environment
) {
$environment = $this->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);
}
}
6 changes: 6 additions & 0 deletions src/Injector/AcquiaCliInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
}
24 changes: 23 additions & 1 deletion tests/AcquiaCliTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ protected function getMockLogstream()
return $logstream;
}

/**
* Callback for the mock client.
*/
public function sendRequestCallback($verb, $path)
{
$fixtureMap = self::getFixtureMap();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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'
]
];
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Commands/LogForwardCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace AcquiaCli\Tests\Commands;

use AcquiaCli\Tests\AcquiaCliTestCase;

class LogForwardCommandTest extends AcquiaCliTestCase
{

/**
* @dataProvider logForwardProvider
*/
public function testLogForwardInfo($command, $expected)
{
$actualResponse = $this->execute($command);
$this->assertSame($expected, $actualResponse);
}

public function logForwardProvider()
{

$listResponse = <<<LIST
+--------------------------------------+--------------------------+-------------------+-----------+--------+
| UUID | Label | Address | Consumer | Active |
+--------------------------------------+--------------------------+-------------------+-----------+--------+
| df4c5428-8d2e-453d-9edf-e412647449b1 | Test destination | example.com:1234 | sumologic | ✓ |
| df4c5428-8d2e-453d-9edf-e412647449b5 | Another test destination | 193.169.2.19:5678 | syslog | ✓ |
+--------------------------------------+--------------------------+-------------------+-----------+--------+
LIST;

$infoResponse = <<<INFO
Log server address: example.com:1234
> 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,
]
];
}
}
61 changes: 61 additions & 0 deletions tests/Commands/SslCertificateCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace AcquiaCli\Tests\Commands;

use AcquiaCli\Tests\AcquiaCliTestCase;

class SslCertificateCommandTest extends AcquiaCliTestCase
{

/**
* @dataProvider sslCertificateProvider
*/
public function testSslCertificateInfo($command, $expected)
{
$actualResponse = $this->execute($command);
$this->assertSame($expected, $actualResponse);
}

public function sslCertificateProvider()
{

$listResponse = <<<LIST
+----+--------------------+-----------------+--------------------------+--------+
| ID | Label | Domains | Expires | Active |
+----+--------------------+-----------------+--------------------------+--------+
| 7 | | example.com | 2022-03-28T00:12:34-0400 | ✓ |
| | | www.example.com | | |
| 3 | Test Certificate 1 | example.com | 2022-03-28T00:12:34-0400 | ✓ |
| | | www.example.com | | |
| 4 | Test Certificate 2 | example.com | 2022-03-28T00:12:34-0400 | |
| | | www.example.com | | |
+----+--------------------+-----------------+--------------------------+--------+
LIST;

$infoResponse = <<<INFO
Certificate
-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----
CA
-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----
Private Key
-----BEGIN RSA PRIVATE KEY-----...-----END RSA PRIVATE KEY-----
INFO;

return [
[
['ssl:list', 'devcloud:devcloud2', 'dev'],
$listResponse . PHP_EOL
],
[
['ssl:info', 'devcloud:devcloud2', 'dev', 1234],
$infoResponse . PHP_EOL,
]
];
}
}

0 comments on commit dd7fdfd

Please sign in to comment.