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

Add support for Certificate Transparency Monitoring #211

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Endpoints/Certificates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
46 changes: 46 additions & 0 deletions tests/Endpoints/CertificatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]', $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 protected]']
])
);

$certEndpoint = new Certificates($mock);
$result = $certEndpoint->updateCertificateTransparencyMonitoring(
'023e105f4ecef8ad9ca31a8372d0c353',
true,
['[email protected]']
);

$this->assertTrue($result);
}
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Endpoints/getCertificateTransparencyMonitoring.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"result": {
"enabled": true,
"emails": [
"[email protected]"
]
},
"success": true,
"errors": null,
"messages": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"result": {
"enabled": true,
"emails": [
"[email protected]"
]
},
"success": true,
"errors": null,
"messages": null
}