Skip to content

Commit

Permalink
Creates the ssl:create command (#119)
Browse files Browse the repository at this point in the history
* Install, Enable and Disable SSLCertificateCommands

* enable option added to ssl:create command

* - phpcs fixes

* - certificates can now be passed as input files

* - create() expects string, string|false given fix

Co-authored-by: Thomas DENOLLE <[email protected]>
  • Loading branch information
typhonius and Thomas DENOLLE authored Oct 7, 2020
1 parent c38ab2b commit f782245
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 14 deletions.
96 changes: 85 additions & 11 deletions src/Commands/SslCertificateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace AcquiaCli\Commands;

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

Expand Down Expand Up @@ -46,13 +46,13 @@ public function sslCertificateList(
$table
->addRows(
[
[
$certificate->id,
$certificate->label,
implode("\n", $certificate->domains),
$certificate->expires_at,
$certificate->flags->active ? '' : '',
],
[
$certificate->id,
$certificate->label,
implode("\n", $certificate->domains),
$certificate->expires_at,
$certificate->flags->active ? '' : '',
],
]
);
}
Expand Down Expand Up @@ -103,8 +103,8 @@ public function sslCertificateEnable(
) {
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);

if ($this->confirm('Are you sure you want to enable this SSL certificate?')) {
$this->say(sprintf('Enabling certificate on %s environment', $environment->label));
if ($this->confirm('Are you sure you want to activate this SSL certificate? Activating this certificate will deactivate all other non-legacy certificates.')) {
$this->say(sprintf('Activating certificate on %s environment.', $environment->label));
$response = $certificatesAdapter->enable($environment->uuid, $certificateId);
$this->waitForNotification($response);
}
Expand All @@ -128,9 +128,83 @@ public function sslCertificateDisable(
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);

if ($this->confirm('Are you sure you want to disable this SSL certificate?')) {
$this->say(sprintf('Disabling certificate on %s environment', $environment->label));
$this->say(sprintf('Disabling certificate on %s environment.', $environment->label));
$response = $certificatesAdapter->disable($environment->uuid, $certificateId);
$this->waitForNotification($response);
}
}

/**
* Install an SSL certificate
*
* @param string $uuid
* @param string $environment
* @param string $label
* @param string $certificate The path to the certificate file.
* @param string $key The path to the private key file.
* @param null|string $ca The path to the certificate authority file.
* @option activate Enable certification after creation.
* @command ssl:create
*/
public function sslCertificateCreate(
SslCertificates $certificatesAdapter,
$uuid,
$environment,
$label,
$certificate,
$key,
$ca = null,
$options = ['activate']
) {
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);

$confirmMessage = 'Are you sure you want to install this new SSL certificate? (It will not be activated unless the --activate option is passed).';
if ($options['activate']) {
$confirmMessage = 'Are you sure you want to install and activate this new SSL certificate? Activating this certificate will deactivate all other non-legacy certificates.';
}
if ($this->confirm($confirmMessage)) {
if (!file_exists($certificate) || !is_readable($certificate)) {
throw new \Exception(sprintf('Cannot open certificate file at %s.', $certificate));
}
$certificate = strval(file_get_contents($certificate));

if (!file_exists($key) || !is_readable($key)) {
throw new \Exception(sprintf('Cannot open key file at %s.', $key));
}
$key = strval(file_get_contents($key));

if ($ca !== null) {
if (!file_exists($ca) || !is_readable($ca)) {
throw new \Exception(sprintf('Cannot open ca file at %s.', $ca));
}
$ca = strval(file_get_contents($ca));
}

$this->say(sprintf('Installing new certificate %s on %s environment.', $label, $environment->label));

$response = $certificatesAdapter->create(
$environment->uuid,
$label,
$certificate,
$key,
$ca
);

$this->waitForNotification($response);

if ($options['activate']) {
$certificates = $certificatesAdapter->getAll($environment->uuid);
foreach ($certificates as $installedCertificate) {
/**
* @var SslCertificateResponse $certificate
*/
if ($installedCertificate->label === $label && !$installedCertificate->flags->active) {
$this->say(sprintf('Activating certificate %s on %s environment.', $installedCertificate->label, $environment->label));
$response = $certificatesAdapter->enable($environment->uuid, $installedCertificate->id);
$this->waitForNotification($response);
}
}
}
}
}
}
12 changes: 11 additions & 1 deletion tests/AcquiaCliTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,26 @@ public static function getFixtureMap()
'get' => 'LogForwarding/getLogForwarding.json'
],
'/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates' => [
'get' => 'SslCertificates/getAllSslCertificates.json'
'get' => 'SslCertificates/getAllSslCertificates.json',
'post' => 'SslCertificates/createSslCertificate.json'
],
'/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates/1234' => [
'get' => 'SslCertificates/getSslCertificate.json'
],
'/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates/4/actions/activate' => [
'post' => 'SslCertificates/activateSslCertificate.json'
],
'/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates/1234/actions/activate' => [
'post' => 'SslCertificates/activateSslCertificate.json'
],
'/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates/1234/actions/deactivate' => [
'post' => 'SslCertificates/deactivateSslCertificate.json'
],
'/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates/7/actions/deactivate' => [
'post' => 'SslCertificates/deactivateSslCertificate.json'
],
'/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/ssl/certificates/3/actions/deactivate' => [
'post' => 'SslCertificates/deactivateSslCertificate.json'
]
];
}
Expand Down
54 changes: 52 additions & 2 deletions tests/Commands/SslCertificateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testSslCertificateInfo($command, $expected)

public function sslCertificateProvider()
{
$sslCertificatesPath = dirname(__DIR__) . "/Fixtures/SslCertificates";

$listResponse = <<<LIST
+----+--------------------+-----------------+--------------------------+--------+
Expand Down Expand Up @@ -58,11 +59,60 @@ public function sslCertificateProvider()
],
[
['ssl:enable', 'devcloud:devcloud2', 'dev', '1234'],
'> Enabling certificate on Dev environment' . PHP_EOL,
'> Activating certificate on Dev environment.' . PHP_EOL,
],
[
['ssl:disable', 'devcloud:devcloud2', 'dev', '1234'],
'> Disabling certificate on Dev environment' . PHP_EOL,
'> Disabling certificate on Dev environment.' . PHP_EOL,
],
[
['ssl:create',
'devcloud:devcloud2',
'dev',
'Test Certificate 2',
$sslCertificatesPath . '/cert.pem',
$sslCertificatesPath . '/key.pem',
$sslCertificatesPath . '/ca.pem',
'--activate'],
'> Installing new certificate Test Certificate 2 on Dev environment.' . PHP_EOL .
'> Activating certificate Test Certificate 2 on Dev environment.' . PHP_EOL
],
[
['ssl:create',
'devcloud:devcloud2',
'dev',
'Test Certificate 2',
$sslCertificatesPath . '/cert.pem',
$sslCertificatesPath . '/key.pem'],
'> Installing new certificate Test Certificate 2 on Dev environment.' . PHP_EOL,
],
[
['ssl:create',
'devcloud:devcloud2',
'dev',
'Test Certificate 2',
'/nopath/cert.pem',
$sslCertificatesPath . '/key.pem'],
' [error] Cannot open certificate file at /nopath/cert.pem. ' . PHP_EOL,
],
[
['ssl:create',
'devcloud:devcloud2',
'dev',
'Test Certificate 2',
$sslCertificatesPath . '/cert.pem',
'/nopath/key.pem'],
' [error] Cannot open key file at /nopath/key.pem. ' . PHP_EOL,
],
[
['ssl:create',
'devcloud:devcloud2',
'dev',
'Test Certificate 2',
$sslCertificatesPath . '/cert.pem',
$sslCertificatesPath . '/key.pem',
'/nopath/ca.pem'],
' [error] Cannot open ca file at /nopath/ca.pem. ' . PHP_EOL,
]
];
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/SslCertificates/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN CERTIFICATE-----
123abc....
-----END CERTIFICATE-----
3 changes: 3 additions & 0 deletions tests/Fixtures/SslCertificates/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN CERTIFICATE-----
abc123....
-----END CERTIFICATE-----
3 changes: 3 additions & 0 deletions tests/Fixtures/SslCertificates/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN RSA PRIVATE KEY-----
secret....
-----END RSA PRIVATE KEY-----

0 comments on commit f782245

Please sign in to comment.