From 29182a7ace361fdeb43e8613372babd1722f681e Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Tue, 15 Dec 2020 19:45:01 +1300 Subject: [PATCH 1/2] Adds function to get all bulk certificates via pagination --- src/Fastly/Types/FastlyCertificates.php | 36 +++++++++++++++++++ tests/Fastly/FastlyTLSBulkCertificateTest.php | 12 +++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Fastly/Types/FastlyCertificates.php b/src/Fastly/Types/FastlyCertificates.php index 3ba6a8a..277963d 100644 --- a/src/Fastly/Types/FastlyCertificates.php +++ b/src/Fastly/Types/FastlyCertificates.php @@ -7,6 +7,10 @@ class FastlyCertificates extends FastlyRequest { + + const MAX_CERTIFICATES_FROM_ENDPOINT = 200; + const MAX_CERTIFICATE_PAGES_TO_PULL = 50; //this will allow up to 10000 certificates + public $data; public $links; public $meta; @@ -142,6 +146,38 @@ public function getTLSBulkCertificates($options = '') return $certificates; } + public function getAllBulkCertificates() + { + $lastPage = false; + $options = sprintf("page[size]=%d", + self::MAX_CERTIFICATES_FROM_ENDPOINT); + $pageCount = 0; + + $totalCertificateData = []; + + while (!$lastPage && $pageCount++ < self::MAX_CERTIFICATES_FROM_ENDPOINT) { + $certificateData = self::getTLSBulkCertificates($options); + $totalCertificateData[] = $certificateData['data']; + + $currentPage = $certificateData['meta'][0]['current_page']; + $totalPages = $certificateData['meta'][0]['total_pages']; + + if ($currentPage >= $totalPages) { + $lastPage = true; + } else { + $options = sprintf("page[size]=%d&page[number]=%d", + self::MAX_CERTIFICATES_FROM_ENDPOINT, ++$currentPage); + } + } + + return [ + 'data' => array_reduce($totalCertificateData, function ($c, $e) { return array_merge($c, $e);}, []), + 'links' => [], + 'meta' => [], + ]; + + } + /** * Upload a new certificate. * diff --git a/tests/Fastly/FastlyTLSBulkCertificateTest.php b/tests/Fastly/FastlyTLSBulkCertificateTest.php index f17f97e..a8cc5d2 100644 --- a/tests/Fastly/FastlyTLSBulkCertificateTest.php +++ b/tests/Fastly/FastlyTLSBulkCertificateTest.php @@ -31,7 +31,6 @@ public function testGetFastlyCertificates() { $certificatesObject = $this->fastly->certificates; $certificates = $certificatesObject->get_tls_certificates(); - // Get whole response from API. $this->assertArrayHasKey('data', $certificates); } @@ -39,14 +38,21 @@ public function testGetFastlyCertificates() public function testGetPlatformTLSCertificates() { $certificatesObject = $this->fastly->certificates; - $certificates = $certificatesObject->getTLSBulkCertificates(); - + $certificates = $certificatesObject->getTLSBulkCertificates('page[size]=200'); // Get whole response from API. $this->assertArrayHasKey('data', $certificates); $this->assertArrayHasKey('links', $certificates); $this->assertArrayHasKey('meta', $certificates); } + public function testGetAllBulkCertificates() + { + $certificates = $this->fastly->certificates->getAllBulkCertificates(); + $this->assertArrayHasKey('data', $certificates); + $this->assertArrayHasKey('links', $certificates); + $this->assertArrayHasKey('meta', $certificates); + } + //public function testGetCertificateByID() //{ // $certificatesObject = $this->fastly->certificates; From 950cb44e14e16030fb32c5f882dfc721fa9618fa Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Tue, 15 Dec 2020 19:53:10 +1300 Subject: [PATCH 2/2] Adds some basic defense to getAllBulkCertificates --- src/Fastly/Types/FastlyCertificates.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Fastly/Types/FastlyCertificates.php b/src/Fastly/Types/FastlyCertificates.php index 277963d..8e4f8bb 100644 --- a/src/Fastly/Types/FastlyCertificates.php +++ b/src/Fastly/Types/FastlyCertificates.php @@ -162,6 +162,10 @@ public function getAllBulkCertificates() $currentPage = $certificateData['meta'][0]['current_page']; $totalPages = $certificateData['meta'][0]['total_pages']; + if(!is_numeric($currentPage) || !is_numeric($totalPages)) { + throw new \Exception("Call to getAllBulkCertificates resulted in bad results from Fastly API"); + } + if ($currentPage >= $totalPages) { $lastPage = true; } else {