From c639f20cea517239d9be54d6857320ab5a3b8669 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 15 Jun 2021 00:49:14 +0100 Subject: [PATCH 01/10] Add More Custom Hostname Creation Parameters This now matches what is available as per the documentation --- src/Endpoints/CustomHostnames.php | 23 ++++++++-- tests/Endpoints/CustomHostnamesTest.php | 60 ++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index a7f09b09..8846c065 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -14,7 +14,7 @@ class CustomHostnames implements API { use BodyAccessorTrait; - + private $adapter; public function __construct(Adapter $adapter) @@ -30,12 +30,15 @@ public function __construct(Adapter $adapter) * @param string $hostname * @param string $sslMethod * @param string $sslType - * @param array $sslSettings + * @param array $sslSettings * @param string $customOriginServer - * @param bool $wildcard + * @param bool $wildcard + * @param string $bundleMethod + * @param string $customKey + * @param string $customCertificate * @return \stdClass */ - public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv', array $sslSettings = [], string $customOriginServer = '', bool $wildcard = false): \stdClass + public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv', array $sslSettings = [], string $customOriginServer = '', bool $wildcard = false, string $bundleMethod = '', string $customKey = '', string $customCertificate = ''): \stdClass { $options = [ 'hostname' => $hostname, @@ -51,6 +54,18 @@ public function addHostname(string $zoneID, string $hostname, string $sslMethod $options['custom_origin_server'] = $customOriginServer; } + if (empty($bundleMethod) === false) { + $options['bundle_method'] = $bundleMethod; + } + + if (empty($customKey) === false) { + $options['custom_key'] = $customKey; + } + + if (empty($customCertificate) === false) { + $options['custom_certificate'] = $customCertificate; + } + $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); $this->body = json_decode($zone->getBody()); return $this->body->result; diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 7298d869..4cfd9711 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -17,6 +17,61 @@ public function testAddHostname() $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock->method('post')->willReturn($response); + $customKey = <<expects($this->once()) ->method('post') ->with( @@ -33,6 +88,9 @@ public function testAddHostname() 'min_tls_version' => '1.2' ] ], + 'bundle_method' => 'optimal', + 'custom_key' => $customKey, + 'custom_certificate' => $customCertificate, 'wildcard' => true, ]) ); @@ -43,7 +101,7 @@ public function testAddHostname() 'http3' => 'on', 'min_tls_version' => '1.2' ]; - $hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv', $sslSettings, 'origin.example.com', true); + $hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv', $sslSettings, 'origin.example.com', true, 'optimal', $customKey, $customCertificate); $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); } From 9955166ae8641a6255c84b239ff0c08338c8dcff Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 15 Jun 2021 00:56:23 +0100 Subject: [PATCH 02/10] Tidy Up Longer Method/Function Signatures/Calls I've added a few new parameters which has made these lines rather long --- src/Endpoints/CustomHostnames.php | 14 ++++++++++++-- tests/Endpoints/CustomHostnamesTest.php | 13 ++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 8846c065..6e4281e0 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -38,8 +38,18 @@ public function __construct(Adapter $adapter) * @param string $customCertificate * @return \stdClass */ - public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv', array $sslSettings = [], string $customOriginServer = '', bool $wildcard = false, string $bundleMethod = '', string $customKey = '', string $customCertificate = ''): \stdClass - { + public function addHostname( + string $zoneID, + string $hostname, + string $sslMethod = 'http', + string $sslType = 'dv', + array $sslSettings = [], + string $customOriginServer = '', + bool $wildcard = false, + string $bundleMethod = '', + string $customKey = '', + string $customCertificate = '' + ): \stdClass { $options = [ 'hostname' => $hostname, 'ssl' => [ diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 4cfd9711..5c19ccfc 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -101,7 +101,18 @@ public function testAddHostname() 'http3' => 'on', 'min_tls_version' => '1.2' ]; - $hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv', $sslSettings, 'origin.example.com', true, 'optimal', $customKey, $customCertificate); + $hostname->addHostname( + '023e105f4ecef8ad9ca31a8372d0c353', + 'app.example.com', + 'http', + 'dv', + $sslSettings, + 'origin.example.com', + true, + 'optimal', + $customKey, + $customCertificate + ); $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); } From f473e5f73cf6609c7071d55b493b9f48924336e0 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 15 Jun 2021 10:52:44 +0100 Subject: [PATCH 03/10] Correct A Request Parameter Namespace This wasn't taking affect before as it wasn't in the object the API was expecting it to be in --- src/Endpoints/CustomHostnames.php | 10 +++++----- tests/Endpoints/CustomHostnamesTest.php | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 6e4281e0..811b8337 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -55,9 +55,9 @@ public function addHostname( 'ssl' => [ 'method' => $sslMethod, 'type' => $sslType, - 'settings' => $sslSettings + 'settings' => $sslSettings, + 'wildcard' => $wildcard, ], - 'wildcard' => $wildcard, ]; if (!empty($customOriginServer)) { @@ -65,15 +65,15 @@ public function addHostname( } if (empty($bundleMethod) === false) { - $options['bundle_method'] = $bundleMethod; + $options['ssl']['bundle_method'] = $bundleMethod; } if (empty($customKey) === false) { - $options['custom_key'] = $customKey; + $options['ssl']['custom_key'] = $customKey; } if (empty($customCertificate) === false) { - $options['custom_certificate'] = $customCertificate; + $options['ssl']['custom_certificate'] = $customCertificate; } $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 5c19ccfc..c19c286b 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -85,13 +85,13 @@ public function testAddHostname() 'settings' => [ 'http2' => 'on', 'http3' => 'on', - 'min_tls_version' => '1.2' - ] + 'min_tls_version' => '1.2', + ], + 'bundle_method' => 'optimal', + 'custom_key' => $customKey, + 'custom_certificate' => $customCertificate, + 'wildcard' => true, ], - 'bundle_method' => 'optimal', - 'custom_key' => $customKey, - 'custom_certificate' => $customCertificate, - 'wildcard' => true, ]) ); From 1a2d2ae7083929a75f6b9b1f66f1be6c5268218d Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 09:29:08 +0100 Subject: [PATCH 04/10] Loosen Empty Check Strictness This was asked for in a CR --- src/Endpoints/CustomHostnames.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 811b8337..6d6733c2 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -64,15 +64,15 @@ public function addHostname( $options['custom_origin_server'] = $customOriginServer; } - if (empty($bundleMethod) === false) { + if (!empty($bundleMethod)) { $options['ssl']['bundle_method'] = $bundleMethod; } - if (empty($customKey) === false) { + if (!empty($customKey)) { $options['ssl']['custom_key'] = $customKey; } - if (empty($customCertificate) === false) { + if (!empty($customCertificate)) { $options['ssl']['custom_certificate'] = $customCertificate; } From be886c36cbfc027a2ad0702eab6a658feeb1955b Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 10:07:14 +0100 Subject: [PATCH 05/10] Correct Some Bad Spacing This was my bad --- tests/Endpoints/CustomHostnamesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index c19c286b..fd584e44 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -47,7 +47,7 @@ public function testAddHostname() /cMkj3wSfC341hFaJEuG1+PcxA== -----END PRIVATE KEY----- KEY; - $customCertificate = << Date: Tue, 22 Jun 2021 10:34:28 +0100 Subject: [PATCH 06/10] Move The Custom SSL To An Array This kinda kicks the can down the road, but allows me to appease PHPMD (for now) --- src/Endpoints/CustomHostnames.php | 14 ++++++-------- tests/Endpoints/CustomHostnamesTest.php | 8 ++++++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 6d6733c2..75403659 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -34,8 +34,7 @@ public function __construct(Adapter $adapter) * @param string $customOriginServer * @param bool $wildcard * @param string $bundleMethod - * @param string $customKey - * @param string $customCertificate + * @param array $customSsl * @return \stdClass */ public function addHostname( @@ -47,8 +46,7 @@ public function addHostname( string $customOriginServer = '', bool $wildcard = false, string $bundleMethod = '', - string $customKey = '', - string $customCertificate = '' + array $customSsl = [] ): \stdClass { $options = [ 'hostname' => $hostname, @@ -68,12 +66,12 @@ public function addHostname( $options['ssl']['bundle_method'] = $bundleMethod; } - if (!empty($customKey)) { - $options['ssl']['custom_key'] = $customKey; + if (!empty($customSsl['key'])) { + $options['ssl']['custom_key'] = $customSsl['key']; } - if (!empty($customCertificate)) { - $options['ssl']['custom_certificate'] = $customCertificate; + if (!empty($customSsl['certificate'])) { + $options['ssl']['custom_certificate'] = $customSsl['certificate']; } $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index fd584e44..276776c4 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -47,6 +47,7 @@ public function testAddHostname() /cMkj3wSfC341hFaJEuG1+PcxA== -----END PRIVATE KEY----- KEY; + $customCertificate = << 'on', 'min_tls_version' => '1.2' ]; + $hostname->addHostname( '023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', @@ -110,8 +112,10 @@ public function testAddHostname() 'origin.example.com', true, 'optimal', - $customKey, - $customCertificate + [ + 'key' => $customKey, + 'certificate' => $customCertificate, + ] ); $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); } From d9958973e04fbed1e9d214bd3b55972c88327d28 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 10:42:20 +0100 Subject: [PATCH 07/10] Appease PHPMD This was a good spot, as I can reuse this declaration in another PR --- tests/Endpoints/CustomHostnamesTest.php | 129 +++++++++++++----------- 1 file changed, 68 insertions(+), 61 deletions(-) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 276776c4..f3fae3d2 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -17,61 +17,7 @@ public function testAddHostname() $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock->method('post')->willReturn($response); - $customKey = <<getCustomSsl(); $mock->expects($this->once()) ->method('post') @@ -89,8 +35,8 @@ public function testAddHostname() 'min_tls_version' => '1.2', ], 'bundle_method' => 'optimal', - 'custom_key' => $customKey, - 'custom_certificate' => $customCertificate, + 'custom_key' => $customSsl['key'], + 'custom_certificate' => $customSsl['certificate'], 'wildcard' => true, ], ]) @@ -112,10 +58,7 @@ public function testAddHostname() 'origin.example.com', true, 'optimal', - [ - 'key' => $customKey, - 'certificate' => $customCertificate, - ] + $customSsl ); $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); } @@ -231,4 +174,68 @@ public function testDeleteHostname() $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id); $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->id); } + + private function getCustomSsl(): array + { + $customKey = << $customKey, + 'certificate' => $customCertificate, + ]; + } } From ed822d19ff21e477fabfe50f266d848b934f575c Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 10:51:41 +0100 Subject: [PATCH 08/10] Move An LoC I needed to trigger a github action after a network timeout --- tests/Endpoints/CustomHostnamesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index f3fae3d2..6af54489 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -14,11 +14,11 @@ public function testAddHostname() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json'); + $customSsl = $this->getCustomSsl(); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock->method('post')->willReturn($response); - $customSsl = $this->getCustomSsl(); - $mock->expects($this->once()) ->method('post') ->with( From 969fc8cb59070373081dfea15225c2fc76c6dd14 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 10:57:05 +0100 Subject: [PATCH 09/10] Revert "Move An LoC" This reverts commit ed822d19ff21e477fabfe50f266d848b934f575c. --- tests/Endpoints/CustomHostnamesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index fb3a032a..484c2065 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -14,11 +14,11 @@ public function testAddHostname() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json'); - $customSsl = $this->getCustomSsl(); - $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock->method('post')->willReturn($response); + $customSsl = $this->getCustomSsl(); + $mock->expects($this->once()) ->method('post') ->with( From dfbc5a10d220f3a9fb1fa5e182307c96c1d1c7c7 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 11:01:09 +0100 Subject: [PATCH 10/10] Revert "Revert "Move An LoC"" This reverts commit 969fc8cb59070373081dfea15225c2fc76c6dd14. --- tests/Endpoints/CustomHostnamesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 484c2065..fb3a032a 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -14,11 +14,11 @@ public function testAddHostname() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json'); + $customSsl = $this->getCustomSsl(); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock->method('post')->willReturn($response); - $customSsl = $this->getCustomSsl(); - $mock->expects($this->once()) ->method('post') ->with(