From 50c8524bbad7fdeadd95dc23590e849bc69341f9 Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Thu, 5 May 2022 15:47:35 +0200 Subject: [PATCH 1/3] Add support for Certificate Transparency Monitoring --- src/Endpoints/Certificates.php | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Endpoints/Certificates.php b/src/Endpoints/Certificates.php index a1888b5e..16929a4e 100644 --- a/src/Endpoints/Certificates.php +++ b/src/Endpoints/Certificates.php @@ -83,4 +83,46 @@ public function createCertificate(CertificateConfig $config): bool return false; } + + /** + * Get the certificate transparency monitoring configuration for the zone. + * + * @param string $zoneID + * @return mixed + */ + public function getCertificateTransparencyMonitoring(string $zoneID) + { + $return = $this->adapter->get( + 'zones/' . $zoneID . '/ct/alerting' + ); + $body = json_decode($return->getBody()); + if (isset($body->result)) { + return $body->result; + } + return false; + } + + /** + * Update the certificate transparency monitoring configuration for the zone. + * + * @param string $zoneID The ID of the zone + * @param bool $enabled Enabling of CT monitoring for the zone. + * @param array $emails List of notification email address for this zone. + * @return bool + */ + public function updateCertificateTransparencyMonitoring(string $zoneID, bool $enabled = null, array $emails = []) + { + $return = $this->adapter->patch( + 'zones/' . $zoneID . '/ct/alerting', + [ + 'enabled' => $enabled == true ? true : false, + 'emails' => $emails + ] + ); + $body = json_decode($return->getBody()); + if (isset($body->success) && $body->success == true) { + return true; + } + return false; + } } From 4d365a3953995d5c28ca4ce88c96da9791a55231 Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Thu, 5 May 2022 15:57:00 +0200 Subject: [PATCH 2/3] Code style fixes --- src/Endpoints/Certificates.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Endpoints/Certificates.php b/src/Endpoints/Certificates.php index 16929a4e..d406df0f 100644 --- a/src/Endpoints/Certificates.php +++ b/src/Endpoints/Certificates.php @@ -100,14 +100,14 @@ public function getCertificateTransparencyMonitoring(string $zoneID) return $body->result; } return false; - } + } /** * Update the certificate transparency monitoring configuration for the zone. * * @param string $zoneID The ID of the zone * @param bool $enabled Enabling of CT monitoring for the zone. - * @param array $emails List of notification email address for this zone. + * @param array $emails List of notification email address for this zone. * @return bool */ public function updateCertificateTransparencyMonitoring(string $zoneID, bool $enabled = null, array $emails = []) From 5b2229b2e07d9ebbfd4fdd5ce801cb34676adc53 Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Mon, 9 May 2022 16:09:11 +0200 Subject: [PATCH 3/3] Add missing unit tests --- tests/Endpoints/CertificatesTest.php | 46 +++++++++++++++++++ .../getCertificateTransparencyMonitoring.json | 11 +++++ ...dateCertificateTransparencyMonitoring.json | 11 +++++ 3 files changed, 68 insertions(+) create mode 100644 tests/Fixtures/Endpoints/getCertificateTransparencyMonitoring.json create mode 100644 tests/Fixtures/Endpoints/updateCertificateTransparencyMonitoring.json diff --git a/tests/Endpoints/CertificatesTest.php b/tests/Endpoints/CertificatesTest.php index aa6c80d0..47fce274 100644 --- a/tests/Endpoints/CertificatesTest.php +++ b/tests/Endpoints/CertificatesTest.php @@ -117,4 +117,50 @@ public function testCreateCertificate() $this->assertTrue($result); } + + public function testGetCertificateTransparencyMonitoring() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getCertificateTransparencyMonitoring.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/ct/alerting') + ); + + $certEndpoint = new Certificates($mock); + $result = $certEndpoint->getCertificateTransparencyMonitoring('023e105f4ecef8ad9ca31a8372d0c353'); + + $this->assertEquals('email@example.com', $result->emails[0]); + } + + public function testUpdateCertificateTransparencyMonitoring() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateCertificateTransparencyMonitoring.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/ct/alerting'), + $this->equalTo([ + 'enabled' => true, + 'emails' => ['email@example.com'] + ]) + ); + + $certEndpoint = new Certificates($mock); + $result = $certEndpoint->updateCertificateTransparencyMonitoring( + '023e105f4ecef8ad9ca31a8372d0c353', + true, + ['email@example.com'] + ); + + $this->assertTrue($result); + } } diff --git a/tests/Fixtures/Endpoints/getCertificateTransparencyMonitoring.json b/tests/Fixtures/Endpoints/getCertificateTransparencyMonitoring.json new file mode 100644 index 00000000..e9a7c98d --- /dev/null +++ b/tests/Fixtures/Endpoints/getCertificateTransparencyMonitoring.json @@ -0,0 +1,11 @@ +{ + "result": { + "enabled": true, + "emails": [ + "email@example.com" + ] + }, + "success": true, + "errors": null, + "messages": null +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateCertificateTransparencyMonitoring.json b/tests/Fixtures/Endpoints/updateCertificateTransparencyMonitoring.json new file mode 100644 index 00000000..ac786659 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateCertificateTransparencyMonitoring.json @@ -0,0 +1,11 @@ +{ + "result": { + "enabled": true, + "emails": [ + "email@example.com" + ] + }, + "success": true, + "errors": null, + "messages": null +} \ No newline at end of file