Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Adding bulk cert with bundled CA splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed May 12, 2020
1 parent 245303c commit 8e97317
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 140 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
FASTLY_API_KEY="2arxxxxxxxxxxxxxxxxxxxxxxxxRIL"
FASTLY_SERVICE_ID="5CLxxxxxxxxxxxxxxxxxxOoLP"
FASTLY_CONFIG_ID="UZxxxxxxxxxxxxxxxxxxTA"
12 changes: 5 additions & 7 deletions src/Fastly/Request/FastlyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ public function send($method, $uri, array $options = [])
$body = $this->get_body($response);
$this->statusCode[] = $response->getStatusCode();

if ($body === '' || $body === null) {
$this->output[] = "No Content";
} else {
$this->output[] = $body;
if (!empty($body)) {
$this->output[] = $body;
}
},
'rejected' => function (RequestException $e) {
Expand Down Expand Up @@ -109,10 +107,10 @@ public function get_error()
*/
private function get_body(ResponseInterface $response)
{
if ($response->getStatusCode() === "204") {
return "204 No Content";
if ($response->getStatusCode() === 204) {
return $response->getReasonPhrase();
} else {
return (string)$response->getBody();
return (string) $response->getBody();
}
}

Expand Down
64 changes: 63 additions & 1 deletion src/Fastly/Types/FastlyCertificates.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function send_tls_certificate($signed_certificate, $name = '')
}

/**
* Send bulk certificates.
* Send bulk certificates with split public and intermediates certificates.
*
* @param $signed_certificate
* @param $intermediates_cert
Expand Down Expand Up @@ -189,6 +189,68 @@ public function send_bulk_tls_certificates($signed_certificate, $intermediates_c
return $this->get_error();
}

/**
* Send bulk certificates.
*
* @param string $chained_certificate
* @param string $configurations_id
*
* @return array|mixed|string
*/
public function send_bulk_chained_tls_certificates($chained_certificate, $configurations_id)
{
$endpoint = $this->build_endpoint('tls/bulk/certificates');

$certificates = $this->split_certificates($chained_certificate);

$options = [
"data" => [
"type" => "tls_bulk_certificate",
"attributes" => [
"cert_blob" => $certificates['public'],
"intermediates_blob" => $certificates['chained']
],
"relationships" => [
"tls_configurations" => [
"data" => [
[
"type" => "tls_configuration",
"id" => $configurations_id
]
]
]
]
]
];

try {
$result = $this->send('POST', $endpoint, $options);
} catch (RequestException $e) {
$this->error[] = $e;
return $e->getMessage();
}

if ($result) {
return new FastlyBulkCertificate($this->build_output($result)['data']);
}
return $this->get_error();
}

/**
* @param string $chained_certificate
* @return array
*/
private function split_certificates(string $chained_certificate)
{
list($public, $chained) = preg_split('~(?<=\-----END CERTIFICATE-----)\s~', $chained_certificate,
NULL,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

return [
'public' => $public,
'chained' => $chained
];
}

/**
* Replace a TLS certificate with a new TLS certificate.
*
Expand Down
59 changes: 59 additions & 0 deletions tests/Fastly/FastlyTLSBulkCertificateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Fastly\Tests;

use Dotenv\Dotenv;
use Fastly\Fastly;

class FastlyTLSBulkCertificateTest extends \PHPUnit\Framework\TestCase
{

private $fastly;
private $fastly_service_id;

protected function setUp(): void
{
$dotenv = Dotenv::createImmutable('./');
$dotenv->load();

$fastly_api_token = getenv('FASTLY_API_KEY');
$this->fastly_service_id = getenv('FASTLY_SERVICE_ID');

$this->fastly = new Fastly($fastly_api_token, $this->fastly_service_id);

$this->private_key = file_get_contents('tests/Fastly/Fixtures/key.pem');
$this->public_chained_cert = file_get_contents('tests/Fastly/Fixtures/public_and_chained_certificate.pem');
$this->configurations_id = getenv('FASTLY_CONFIG_ID');
}

//public function testGetCertificates()
//{
// $certificatesObject = $this->fastly->certificates;
// $certificates = $certificatesObject->get_tls_certificates();
//
// // Get whole response from API.
// $this->assertArrayHasKey('data', $certificates);
// $this->assertArrayHasKey('links', $certificates);
// $this->assertArrayHasKey('meta', $certificates);
//}

//public function testGetCertificateByID()
//{
// $certificatesObject = $this->fastly->certificates;
// $certificate = $certificatesObject->get_tls_certificate("1JP0gerEJXIxImRnRLckug");
//
// $this->assertArrayHasKey('id', $certificate);
//}

//public function testSendBulkCertificates()
//{
// $certificatesObject = $this->fastly->certificates;
//
// $response = $certificatesObject->send_bulk_chained_tls_certificates(
// $this->public_chained_cert,
// $this->configurations_id
// );
//
// $this->assertObjectHasAttribute('data', $response);
//}
}
96 changes: 0 additions & 96 deletions tests/Fastly/FastlyTLSCertTest.php

This file was deleted.

68 changes: 32 additions & 36 deletions tests/Fastly/FastlyTLSPrivateKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,37 @@ protected function setUp(): void
$this->private_key = $pkeyout;
}

public function testGetTLSPrivateKeys()
{
$keys = $this->fastly->private_keys;
$response = $keys->get_private_keys();

$this->assertArrayHasKey('data', $response);
}

public function testGetSpecificTLSPrivateKey()
{
$id = '2RZmS0uEBI4mnyXI0ztqx0';
$keys = $this->fastly->private_keys;

$get_key = $keys->get_private_key($id);
$this->assertArrayHasKey('data', $get_key);
}

// Not enabled since they created key can not be deleted. Read below.
// public function testUploadPrivateKeys()
// {
// $keys = $this->fastly->private_keys;
// $response = $keys->send_private_key($this->private_key, $name = '');

// $this->assertEquals('tls_private_key', $response['data']['type']);
// $this->assertArrayHasKey('id', $response['data']);
// $this->assertArrayHasKey('attributes', $response['data']);
// }

// Deleting doesn't seem to have any effect as the key can be retrieved later.
// public function testDeletePrivateKeys()
// {
// $id = "6bWQlIGscXMA86GChdi7q9";
//public function testGetTLSPrivateKeys()
//{
// $keys = $this->fastly->private_keys;
// $response = $keys->delete_private_key($id);
// $response = $keys->get_private_keys();
//
// $this->assertArrayHasKey('data', $response);
//}

//public function testGetSpecificTLSPrivateKey()
//{
// $id = '2RZmS0uEBI4mnyXI0ztqx0';
// $keys = $this->fastly->private_keys;
//
// $get_key = $keys->get_private_key($id);
// $this->assertArrayHasKey('data', $get_key);
//}

//public function testUploadPrivateKeys()
//{
// $keys = $this->fastly->private_keys;
// $response = $keys->send_private_key($this->private_key, $name = '');
//
// $this->assertEquals('tls_private_key', $response->data['type']);
//}

//public function testDeletePrivateKeys()
//{
// $id = "64I6TIuQZLvp5Y74cIqVC2";
// $keys = $this->fastly->private_keys;
// $response = $keys->delete_private_key($id);
//
// $this->assertStringContainsString('No Content', $response[0]);
// }
}
// $this->assertStringContainsString('No Content', $response[0]);
//}
}

0 comments on commit 8e97317

Please sign in to comment.