From 6b3fdaa7761f4c5a467f2b51460b1d728e30e002 Mon Sep 17 00:00:00 2001 From: Tim Clifford Date: Fri, 21 Aug 2020 11:27:27 +0100 Subject: [PATCH] Hardening API failure issues --- src/Fastly/Types/FastlyCertificates.php | 55 ++++++++++++++----- src/Fastly/Types/FastlyPrivateKeys.php | 32 ++++++++--- tests/Fastly/FastlyTLSBulkCertificateTest.php | 1 - 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/Fastly/Types/FastlyCertificates.php b/src/Fastly/Types/FastlyCertificates.php index 647c753..834b6e1 100644 --- a/src/Fastly/Types/FastlyCertificates.php +++ b/src/Fastly/Types/FastlyCertificates.php @@ -21,7 +21,13 @@ class FastlyCertificates extends FastlyRequest public function get_tls_certificate($id = '') { $endpoint = $this->build_endpoint('tls/certificates/' . $id); - $certificate_response = $this->send('GET', $endpoint); + + try { + $certificate_response = $this->send('GET', $endpoint); + } catch (RequestException $e) { + $this->error = $e; + return $e->getMessage(); + } if ($certificate_response !== null || $certificate_response != []) { $output = $this->build_output($certificate_response); @@ -41,10 +47,16 @@ public function get_tls_certificates($filter = []) { $filter_encoded_query = http_build_query($filter); $endpoint = !empty($filter) ? 'tls/certificates?'.$filter_encoded_query : 'tls/certificates'; - $certificates_response = $this->send('GET', $this->build_endpoint($endpoint)); + + try { + $certificates_response = $this->send('GET', $this->build_endpoint($endpoint)); + } catch (RequestException $e) { + $this->error = $e; + return $e->getMessage(); + } $certificates = []; - if ($certificates_response !== null || $certificates_response != []) { + if (!isEmpty($certificates_response)) { $output = $this->build_output($certificates_response); $this->data = $output['data']; $this->meta = $output['meta']; @@ -55,6 +67,9 @@ public function get_tls_certificates($filter = []) $certificates['meta'][] = $this->meta; } + else { + return ['data' => "No certificates returned."]; + } return $certificates; } @@ -68,13 +83,19 @@ public function get_tls_certificates($filter = []) public function getTLSBulkCertificate($id = '') { $endpoint = $this->build_endpoint('tls/bulk/certificates/' . $id); - $certificate_response = $this->send('GET', $endpoint); + + try { + $certificate_response = $this->send('GET', $endpoint); + } catch (RequestException $e) { + $this->error = $e; + return $e->getMessage(); + } if ($certificate_response !== null && $certificate_response != []) { - $output = $this->build_output($certificate_response); - $this->data = $output['data']; + $output = $this->build_output($certificate_response); + $this->data = $output['data']; - return new FastlyBulkCertificate($output['data']); + return new FastlyBulkCertificate($output['data']); } } @@ -87,16 +108,20 @@ public function getTLSBulkCertificate($id = '') public function getTLSBulkCertificates($options = '') { if ($options === '' || $options === null) { - $certificates_response = $this->send( - 'GET', - $this->build_endpoint('tls/bulk/certificates') - ); + try { + $certificates_response = $this->send('GET', $this->build_endpoint('tls/bulk/certificates')); + } catch (RequestException $e) { + $this->error = $e; + return $e->getMessage(); + } } else { - $certificates_response = $this->send( - 'GET', - $this->build_endpoint('tls/bulk/certificates?'.$options) - ); + try { + $certificates_response = $this->send('GET', $this->build_endpoint('tls/bulk/certificates?'.$options)); + } catch (RequestException $e) { + $this->error = $e; + return $e->getMessage(); + } } $certificates = []; diff --git a/src/Fastly/Types/FastlyPrivateKeys.php b/src/Fastly/Types/FastlyPrivateKeys.php index c6ddac6..9b78ee3 100644 --- a/src/Fastly/Types/FastlyPrivateKeys.php +++ b/src/Fastly/Types/FastlyPrivateKeys.php @@ -53,7 +53,12 @@ public function send_private_key($private_key, $name = '') */ public function get_private_key($id) { - $result = $this->send('GET', $this->build_endpoint('tls/private_keys/') . $id); + try { + $result = $this->send('GET', $this->build_endpoint('tls/private_keys/') . $id); + } catch (RequestException $e) { + $this->error = $e; + return $e->getMessage(); + } if ($result) { return new FastlyPrivateKey($this->build_output($result)['data']); @@ -72,20 +77,29 @@ public function get_private_keys($filter = []) { $filter_encoded_query = http_build_query($filter); $endpoint = !empty($filter) ? 'tls/private_keys?'.$filter_encoded_query : 'tls/private_keys'; - $keys_response = $this->send('GET', $this->build_endpoint($endpoint)); + + try { + $keys_response = $this->send('GET', $this->build_endpoint($endpoint)); + } catch (RequestException $e) { + $this->error = $e; + return $e->getMessage(); + } $keys = []; if ($keys_response !== null || $keys_response != []) { - $output = $this->build_output($keys_response); - $this->data = $output['data']; - $this->meta = $output['meta']; + $output = $this->build_output($keys_response); + $this->data = $output['data']; + $this->meta = $output['meta']; - foreach ($this->data as $private_key) { - $keys['data'][] = new FastlyPrivateKey($private_key); - } + foreach ($this->data as $private_key) { + $keys['data'][] = new FastlyPrivateKey($private_key); + } - $keys['meta'][] = $this->meta; + $keys['meta'][] = $this->meta; + } + else { + return ['data' => "No private keys returned."]; } return $keys; diff --git a/tests/Fastly/FastlyTLSBulkCertificateTest.php b/tests/Fastly/FastlyTLSBulkCertificateTest.php index fb5cbd1..f17f97e 100644 --- a/tests/Fastly/FastlyTLSBulkCertificateTest.php +++ b/tests/Fastly/FastlyTLSBulkCertificateTest.php @@ -34,7 +34,6 @@ public function testGetFastlyCertificates() // Get whole response from API. $this->assertArrayHasKey('data', $certificates); - $this->assertArrayHasKey('meta', $certificates); } public function testGetPlatformTLSCertificates()