cloudflare-php
cloudflare-php copied to clipboard
Issues w/ creating Firewall rules
I keep getting a strange issue with this library and trying to create firewall rules. Some of the time, the code below works, but most of the time, I get a 400 bad request error with code "10202" filter at index 0.
I don't see where there are any issues with my expression and don't understand why this sometimes works but I get an error also with the same exact code being run.
require __DIR__ . '/vendor/autoload.php';
$key = new Cloudflare\API\Auth\APIKey('***', '***');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$zones = new Cloudflare\API\Endpoints\Zones($adapter);
$fw = new Cloudflare\API\Endpoints\Firewall($adapter);
$sites = $zones->listZones('', '', 1, 300, '', '', 'all');
foreach ( $sites->result as $site ) :
$site_id = $site->id;
$site_name = $site->name;
$expression = 'ip.geoip.country ne {"US" "MX" "CA"}';
$config = new Cloudflare\API\Configurations\FirewallRuleOptions();
$config->setActionBlock();
$result = $fw->createFirewallRule(
$site_id,
$expression,
$config,
'Country Block'
);
endforeach;
I found out the error was telling me that the filter already exists. However, I don't see anywhere in this library to clear out old filters or anything. Is their an end-point to do so?
I found the same issue. Firewall create and delete rules correctly but doesn't update the rule.
Yeah, this library doesn't seem to support that. What you need to do is add a new function to Firewall.php that uses the filters endpoint, something like
public function updateFirewallFilter(
string $zoneID,
string $filterID,
string $expression
): \stdClass {
$filter = array(
'id' => $filterID,
'expression' => $expression,
'paused' => false
);
$response = $this->adapter->put('zones/' . $zoneID . '/filters/' . $filterID, $filter);
$body = json_decode($response->getBody());
return $body->result;
}