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 21, 2020
1 parent 25c9790 commit 7ac8f81
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 0 deletions.
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);
}
}
}
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 7ac8f81

Please sign in to comment.