Skip to content

Commit

Permalink
Merge 43ca6fe into d965cf9
Browse files Browse the repository at this point in the history
  • Loading branch information
solocal-ecommerce committed Jun 2, 2020
2 parents d965cf9 + 43ca6fe commit ad69863
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 12 deletions.
92 changes: 81 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 All @@ -65,7 +65,7 @@ public function sslCertificateList(
*
* @param string $uuid
* @param string $environment
* @param int $certificateId
* @param int $certificateId
*
* @command ssl:info
*/
Expand All @@ -91,7 +91,7 @@ public function sslCertificateInfo(
*
* @param string $uuid
* @param string $environment
* @param int $certificateId
* @param int $certificateId
*
* @command ssl:enable
*/
Expand All @@ -115,7 +115,7 @@ public function sslCertificateEnable(
*
* @param string $uuid
* @param string $environment
* @param int $certificateId
* @param int $certificateId
*
* @command ssl:disable
*/
Expand All @@ -133,4 +133,74 @@ public function sslCertificateDisable(
$this->waitForNotification($response);
}
}

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

if ($this->confirm('Are you sure you want to install this new SSL certificate?')) {
$this->say(sprintf('Installing new certificate %s on %s environment', $label, $environment->label));

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

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

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

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

$this->waitForNotification($response);

if ($options['enable']) {
$certificates = $certificatesAdapter->getAll($environment->uuid);
foreach ($certificates as $certificate) {
/**
* @var SslCertificateResponse $certificate
*/
if ($certificate->label == $label && !$certificate->flags->active) {
$this->say(sprintf('Enabling certificate on %s environment', $environment->label));
$response = $certificatesAdapter->enable($environment->uuid, $certificate->id);
$this->waitForNotification($response);
}
}
}
}
}
}
6 changes: 5 additions & 1 deletion tests/AcquiaCliTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,15 @@ 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'
],
Expand Down
22 changes: 22 additions & 0 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 @@ -63,6 +64,27 @@ public function sslCertificateProvider()
[
['ssl:disable', 'devcloud:devcloud2', 'dev', '1234'],
'> Disabling certificate on Dev environment' . PHP_EOL,
],
[
['ssl:create',
'devcloud:devcloud2',
'dev',
'Test Certificate 2',
$sslCertificatesPath . '/cert.pem',
$sslCertificatesPath . '/key.pem',
$sslCertificatesPath . '/ca.pem',
'--enable'],
'> Installing new certificate Test Certificate 2 on Dev environment' . PHP_EOL .
'> Enabling certificate 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,
]
];
}
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 ad69863

Please sign in to comment.