Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install, Enable and Disable SSLCertificateCommands #82

Closed
wants to merge 14 commits into from
97 changes: 86 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,79 @@ 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 %s on %s environment', $certificate->label, $environment->label));
$response = $certificatesAdapter->enable($environment->uuid, $certificate->id);
$this->waitForNotification($response);
}elseif ($certificate->flags->active){
// Make sure all the others certificates are disabled
$this->say(sprintf('Disabling certificate %s on %s environment', $certificate->label, $environment->label));
$response = $certificatesAdapter->disable($environment->uuid, $certificate->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 @@ -421,16 +421,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
24 changes: 24 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,29 @@ 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 .
'> Disabling certificate on Dev environment' . PHP_EOL .
'> Disabling certificate Test Certificate 1 on Dev environment' . PHP_EOL .
'> Enabling 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,
]
];
}
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-----