Skip to content

Commit 694342f

Browse files
authored
Merge pull request #98 from llbbl/firewall-rules
Adding new Firewall rules Class
2 parents ca426ab + 8364249 commit 694342f

8 files changed

Lines changed: 450 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Cloudflare\API\Configurations;
4+
5+
class FirewallRuleOptions implements Configurations
6+
{
7+
protected $configs = [
8+
'paused' => false,
9+
'action' => 'block'
10+
];
11+
12+
public function getArray(): array
13+
{
14+
return $this->configs;
15+
}
16+
17+
public function setPaused(bool $paused)
18+
{
19+
$this->configs['paused'] = $paused;
20+
}
21+
22+
public function setActionBlock()
23+
{
24+
$this->configs['action'] = 'block';
25+
}
26+
27+
public function setActionAllow()
28+
{
29+
$this->configs['action'] = 'allow';
30+
}
31+
32+
public function setActionChallenge()
33+
{
34+
$this->configs['action'] = 'challenge';
35+
}
36+
37+
public function setActionJsChallenge()
38+
{
39+
$this->configs['action'] = 'js_challenge';
40+
}
41+
42+
public function setActionLog()
43+
{
44+
$this->configs['action'] = 'log';
45+
}
46+
}

src/Endpoints/Firewall.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Cloudflare\API\Endpoints;
4+
5+
use Cloudflare\API\Adapter\Adapter;
6+
use Cloudflare\API\Configurations\FirewallRuleOptions;
7+
8+
class Firewall implements API
9+
{
10+
private $adapter;
11+
12+
public function __construct(Adapter $adapter)
13+
{
14+
$this->adapter = $adapter;
15+
}
16+
17+
public function createFirewallRules(
18+
string $zoneID,
19+
array $rules
20+
): bool {
21+
$query = $this->adapter->post('zones/' . $zoneID . '/firewall/rules', $rules);
22+
$body = json_decode($query->getBody());
23+
24+
foreach ($body->result as $result) {
25+
if (!isset($result->id)) {
26+
return false;
27+
}
28+
}
29+
30+
return true;
31+
}
32+
33+
public function createFirewallRule(
34+
string $zoneID,
35+
string $expression,
36+
FirewallRuleOptions $options,
37+
string $description = null,
38+
int $priority = null
39+
): bool {
40+
$rule = array_merge([
41+
'filter' => [
42+
'expression' => $expression,
43+
'paused' => false
44+
]
45+
], $options->getArray());
46+
47+
if ($description !== null) {
48+
$rule['description'] = $description;
49+
}
50+
51+
if ($priority !== null) {
52+
$rule['priority'] = $priority;
53+
}
54+
55+
return $this->createFirewallRules($zoneID, [$rule]);
56+
}
57+
58+
public function listFirewallRules(
59+
string $zoneID,
60+
int $page = 1,
61+
int $perPage = 50
62+
): \stdClass {
63+
$query = [
64+
'page' => $page,
65+
'per_page' => $perPage,
66+
];
67+
68+
$rules = $this->adapter->get('zones/' . $zoneID . '/firewall/rules', $query);
69+
$body = json_decode($rules->getBody());
70+
71+
return (object)['result' => $body->result, 'result_info' => $body->result_info];
72+
}
73+
74+
public function deleteFirewallRule(
75+
string $zoneID,
76+
string $ruleID
77+
): bool {
78+
$rule = $this->adapter->delete('zones/' . $zoneID . '/firewall/rules/' . $ruleID);
79+
80+
$body = json_decode($rule->getBody());
81+
82+
if (isset($body->result->id)) {
83+
return true;
84+
}
85+
86+
return false;
87+
}
88+
89+
public function updateFirewallRule(
90+
string $zoneID,
91+
string $ruleID,
92+
string $filterID,
93+
string $expression,
94+
FirewallRuleOptions $options,
95+
string $description = null,
96+
int $priority = null
97+
): \stdClass {
98+
$rule = array_merge([
99+
'id' => $ruleID,
100+
'filter' => [
101+
'id' => $filterID,
102+
'expression' => $expression,
103+
'paused' => false
104+
]
105+
], $options->getArray());
106+
107+
if ($description !== null) {
108+
$rule['description'] = $description;
109+
}
110+
111+
if ($priority !== null) {
112+
$rule['priority'] = $priority;
113+
}
114+
115+
$rule = $this->adapter->put('zones/' . $zoneID . '/firewall/rules/' . $ruleID, $rule);
116+
$body = json_decode($rule->getBody());
117+
118+
return $body->result;
119+
}
120+
}

tests/Endpoints/FirewallTest.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
class FirewallTest extends TestCase
4+
{
5+
public function testCreatePageRules()
6+
{
7+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createFirewallRules.json');
8+
9+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
10+
$mock->method('post')->willReturn($response);
11+
12+
$mock->expects($this->once())
13+
->method('post')
14+
->with(
15+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules'),
16+
$this->equalTo([
17+
[
18+
'action' => 'block',
19+
'description' => 'Foo',
20+
'filter' => [
21+
'expression' => 'http.cookie eq "foo"',
22+
'paused' => false
23+
],
24+
],
25+
[
26+
'action' => 'block',
27+
'description' => 'Bar',
28+
'filter' => [
29+
'expression' => 'http.cookie eq "bar"',
30+
'paused' => false
31+
],
32+
]
33+
])
34+
);
35+
36+
$firewall = new Cloudflare\API\Endpoints\Firewall($mock);
37+
$result = $firewall->createFirewallRules(
38+
'023e105f4ecef8ad9ca31a8372d0c353',
39+
[
40+
[
41+
'filter' => [
42+
'expression' => 'http.cookie eq "foo"',
43+
'paused' => false
44+
],
45+
'action' => 'block',
46+
'description' => 'Foo'
47+
],
48+
[
49+
'filter' => [
50+
'expression' => 'http.cookie eq "bar"',
51+
'paused' => false
52+
],
53+
'action' => 'block',
54+
'description' => 'Bar'
55+
],
56+
]
57+
);
58+
$this->assertTrue($result);
59+
}
60+
61+
public function testCreatePageRule()
62+
{
63+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createFirewallRule.json');
64+
65+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
66+
$mock->method('post')->willReturn($response);
67+
68+
$mock->expects($this->once())
69+
->method('post')
70+
->with(
71+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules'),
72+
$this->equalTo([
73+
[
74+
'action' => 'block',
75+
'description' => 'Foobar',
76+
'filter' => [
77+
'expression' => 'http.cookie eq "foobar"',
78+
'paused' => false
79+
],
80+
'paused' => false
81+
]
82+
])
83+
);
84+
85+
$firewall = new Cloudflare\API\Endpoints\Firewall($mock);
86+
$options = new \Cloudflare\API\Configurations\FirewallRuleOptions();
87+
$options->setActionBlock();
88+
$result = $firewall->createFirewallRule(
89+
'023e105f4ecef8ad9ca31a8372d0c353',
90+
'http.cookie eq "foobar"',
91+
$options,
92+
'Foobar'
93+
);
94+
$this->assertTrue($result);
95+
}
96+
97+
public function testListFirewallRules()
98+
{
99+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listFirewallRules.json');
100+
101+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
102+
$mock->method('get')->willReturn($response);
103+
104+
$mock->expects($this->once())
105+
->method('get')
106+
->with(
107+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules'),
108+
$this->equalTo([
109+
'page' => 1,
110+
'per_page' => 50
111+
])
112+
);
113+
114+
$firewall = new Cloudflare\API\Endpoints\Firewall($mock);
115+
$result = $firewall->listFirewallRules('023e105f4ecef8ad9ca31a8372d0c353');
116+
117+
$this->assertObjectHasAttribute('result', $result);
118+
$this->assertObjectHasAttribute('result_info', $result);
119+
120+
$this->assertEquals('970b10321e3f4adda674c912b5f76591', $result->result[0]->id);
121+
}
122+
123+
public function testDeleteFirewallRule()
124+
{
125+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteFirewallRule.json');
126+
127+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
128+
$mock->method('delete')->willReturn($response);
129+
130+
$mock->expects($this->once())
131+
->method('delete')
132+
->with(
133+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules/970b10321e3f4adda674c912b5f76591')
134+
);
135+
136+
$firewall = new Cloudflare\API\Endpoints\Firewall($mock);
137+
$firewall->deleteFirewallRule('023e105f4ecef8ad9ca31a8372d0c353', '970b10321e3f4adda674c912b5f76591');
138+
}
139+
140+
public function testUpdateFirewallRule()
141+
{
142+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateFirewallRule.json');
143+
144+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
145+
$mock->method('put')->willReturn($response);
146+
147+
$mock->expects($this->once())
148+
->method('put')
149+
->with(
150+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules/970b10321e3f4adda674c912b5f76591'),
151+
$this->equalTo([
152+
'id' => '970b10321e3f4adda674c912b5f76591',
153+
'action' => 'block',
154+
'description' => 'Foo',
155+
'filter' => [
156+
'id' => '5def9c4297e0466cb0736b838345d910',
157+
'expression' => 'http.cookie eq "foo"',
158+
'paused' => false
159+
],
160+
'paused' => false
161+
])
162+
);
163+
164+
$firewall = new Cloudflare\API\Endpoints\Firewall($mock);
165+
$options = new \Cloudflare\API\Configurations\FirewallRuleOptions();
166+
$options->setActionBlock();
167+
$result = $firewall->updateFirewallRule(
168+
'023e105f4ecef8ad9ca31a8372d0c353',
169+
'970b10321e3f4adda674c912b5f76591',
170+
'5def9c4297e0466cb0736b838345d910',
171+
'http.cookie eq "foo"',
172+
$options,
173+
'Foo'
174+
);
175+
$this->assertEquals('970b10321e3f4adda674c912b5f76591', $result->id);
176+
}
177+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"result": [
3+
{
4+
"id": "970b10321e3f4adda674c912b5f76591",
5+
"paused": false,
6+
"description": "Foobar",
7+
"action": "block",
8+
"filter": {
9+
"id": "70f39827184d487e97cc286b960f4cc3",
10+
"expression": "http.cookie eq \"foobar\"",
11+
"paused": false
12+
},
13+
"created_on": "2019-07-05T15:53:15Z",
14+
"modified_on": "2019-07-05T15:53:15Z"
15+
}
16+
],
17+
"success": true,
18+
"errors": [],
19+
"messages": []
20+
}

0 commit comments

Comments
 (0)