BlockerHelper/backends/fastly.pm

filter: Statementstmt 29.4% 128 / 435 Branchbran 30.1% 53 / 176 Conditioncond 26.6% 16 / 60 MC/DCmcdc 8.8% 3 / 34 Subroutinesub 66.6% 18 / 27 Podpod 100% 13 / 13 totaltotal 31.0% 231 / 745 CC 116 SCAR 39.5
CC
116
Cov
29%
re_init
110.0
_unban_cidr_range
90.0
flush
90.0
CRAP 52.1
« BlockerHelper/backends/dummy.pm ↑ Summary BlockerHelper/backends/cloudflare.pm »
1 package Net::Firewall::BlockerHelper::backends::fastly;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::fastly - Fastly Edge ACL backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'fastly',
28 name => 'ssh',
29 options => {
30 token => $fastly_api_token,
31 service => $fastly_service_id,
32 acl => $fastly_acl_id,
33 },
34 );
35  
36 $fw_helper->init_backend;
37 $fw_helper->ban( ban => '1.2.3.4' );
38 $fw_helper->unban( ban => '1.2.3.4' );
39  
40 =head1 DESCRIPTION
41  
42 Blocks IPs remotely via the Fastly API by adding and removing entries in an
43 Edge ACL. An ACL entry is created per IP, with a /32 subnet for IPv4 and a
44 /128 subnet for IPv6. A VCL service that references the ACL and takes action
45 on matching requests must already exist.
46  
47 Auth is via a Fastly API token, passed in the C<token> option and sent in the
48 C<Fastly-Key> header.
49  
50 Blocking is per IP; ports and protocols are not supported and specifying them
51 is an error.
52  
53 L<LWP::UserAgent> is only loaded at run time, so it is only required if this
54 backend is actually used. For https, L<LWP::Protocol::https> must be present
55 as well.
56  
57 =head1 NOTES
58  
59 This backend was written going off the API docs and actual testing is
60 needed to double check a few things as the exact behavior is not clear.
61  
62 teardown currently only removes the single IP entries from the ACL,
63 leaving CIDR entries in place, while flush removes both. A re_init after
64 a teardown will re-add the CIDR entries, and how the API handles creating
65 an entry that already exists needs checking.
66  
67 =head1 METHODS
68  
69 =head2 new
70  
71 Initiates the the object.
72  
73 - options :: Backend specific options. See below.
74 - Default :: {}
75  
76 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
77 - default :: kur
78  
79 - name :: Name of this specific instance. This must be specified.
80 - default :: undef
81  
82 Ports and protocols are not supported by this backend and specifying either
83 is an error.
84  
85 The options hash accepts the following.
86  
87 - token :: Fastly API token, sent in the Fastly-Key header.
88 - Default :: undef
89  
90 - service :: Fastly service ID the ACL belongs to.
91 - Default :: undef
92  
93 - acl :: Fastly Edge ACL ID to manage entries under.
94 - Default :: undef
95  
96 - timeout :: HTTP timeout in seconds.
97 - Default :: 30
98  
99 - insecure :: If true, skip TLS certificate verification.
100 - Default :: 0
101  
102 All errors are considered fatal, meaning if new fails it will die.
103  
104 =cut
105  
106 sub new {
107 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=24 SCAR=42.8
Pod documented
108  
109 4 my $self = {
110 perror => undef,
111 error => undef,
112 errorLine => undef,
113 errorFilename => undef,
114 errorString => "",
115 errorExtra => {
116 all_errors_fatal => 1,
117 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
118 # is kept for the name documented in its POD
119 all_fatal => 1,
120 flags => {
121 1 => 'notInited',
122 6 => 'invalidPrefixSpecified',
123 7 => 'invalidName',
124 8 => 'optionsNotHash',
125 9 => 'noBanItem',
126 10 => 'banItemNotIP',
127 12 => 'backendInitError',
128 13 => 'banFailed',
129 14 => 'unbanFailed',
130 15 => 'listFailed',
131 16 => 'reInitFailed',
132 17 => 'teardownFailed',
133 18 => 'alreadyInited',
134 23 => 'initFailed',
135 24 => 'checkFailed',
136 25 => 'flushFailed',
137 26 => 'portsNotSupported',
138 27 => 'protocolsNotSupported',
139 30 => 'serviceNotDefined',
140 31 => 'tokenNotDefined',
141 32 => 'aclNotDefined',
142 33 => 'banCidrFailed',
143 34 => 'unbanCidrFailed',
144 35 => 'cidrItemNotCidr',
145 36 => 'cidrNotSupported',
146 37 => 'listCidrFailed',
147 },
148 fatal_flags => {},
149 perror_not_fatal => 0,
150 },
151 options => {},
152 ports => [],
153 protocols => [],
154 testing => undef,
155 test_data => undef,
156 prefix => 'kur',
157 name => undef,
158 frontend_obj => undef,
159 inited => 0,
160 banned => {},
161 banned_cidr => {},
162 cidr_supported => 1,
163 ua => undef,
164 };
165 4 bless $self;
166  
167 # blocking is per IP; the ACL has no concept of ports or protocols
168 4 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) eq 'ARRAY' && defined( $opts{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]) 0 4 4
Condition33%2/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
040
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} ref $opts{"ports"} eq "ARRAY" defined $opts{"ports"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"ports"}B= ref $opts{"ports"} eq "ARRAY"C= defined $opts{"ports"}[0]
ABCresult
0XX0
10X0
1100
1111
169 0 $self->{perror} = 1;
170 0 $self->{error} = 26;
171 0 $self->{errorString} = 'the fastly backend blocks whole IPs and does not support ports';
172 0 $self->warn;
173 }
174 4 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) eq 'ARRAY' && defined( $opts{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]) 0 4 4
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
040
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
175 0 $self->{perror} = 1;
176 0 $self->{error} = 27;
177 0 $self->{errorString} = 'the fastly backend blocks whole IPs and does not support protocols';
178 0 $self->warn;
179 }
180  
181 # make sure prefix is sane if defiend
182 4 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 4 4
elsif (defined $opts{'prefix'}) { } 4 0 4
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
040
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
183 0 $self->{perror} = 1;
184 0 $self->{error} = 6;
185 $self->{errorString}
186 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
187 0 $self->warn;
188 } elsif ( defined( $opts{prefix} ) ) {
189 4 $self->{prefix} = $opts{prefix};
190 }
191  
192 # make sure we have a name and that it is valid
193 4 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 4 4
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 4 4
194 0 $self->{perror} = 1;
195 0 $self->{error} = 7;
196 0 $self->{errorString} = 'name is undef';
197 0 $self->warn;
198 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
199 0 $self->{perror} = 1;
200 0 $self->{error} = 7;
201 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
202 0 $self->warn;
203 }
204 4 $self->{name} = $opts{name};
205  
206 # used internally for testing
207 4 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 4 0 4
208 4 $self->{testing} = $opts{testing};
209 }
210 4 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 4 0 4
211 4 $self->{frontend_obj} = $opts{frontend_obj};
212 }
213  
214 4 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 4 0 4
215 4 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 4 4
216 0 $self->{perror} = 1;
217 0 $self->{error} = 8;
218 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
219 0 $self->warn;
220 }
221 4 $self->{options} = $opts{options};
222 }
223  
224 # required options
225 4 ▶ if ( !defined( $self->{options}{token} ) || $self->{options}{token} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"token"} or $self->{"options"}{"token"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"token"} or $self->{"options"}{"token"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"token"} or $self->{"options"}{"token"} eq ""
not defined $self->{"options"}{"token"} $self->{"options"}{"token"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"token"}B= $self->{"options"}{"token"} eq ""
ABresult
1X1
011
000
226 1 $self->{perror} = 1;
227 1 $self->{error} = 31;
228 1 $self->{errorString} = 'the option token is undef or blank';
229 1 $self->warn;
230 }
231 3 ▶ if ( !defined( $self->{options}{service} ) || $self->{options}{service} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"service"} or $self->{"options"}{"service"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"service"} or $self->{"options"}{"service"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"service"} or $self->{"options"}{"service"} eq ""
not defined $self->{"options"}{"service"} $self->{"options"}{"service"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"service"}B= $self->{"options"}{"service"} eq ""
ABresult
1X1
011
000
232 1 $self->{perror} = 1;
233 1 $self->{error} = 30;
234 1 $self->{errorString} = 'the option service is undef or blank';
235 1 $self->warn;
236 }
237 2 ▶ if ( !defined( $self->{options}{acl} ) || $self->{options}{acl} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"acl"} or $self->{"options"}{"acl"} eq "") 1 1 2
Condition66%2/3
not defined $self->{"options"}{"acl"} or $self->{"options"}{"acl"} eq ""
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $self->{"options"}{"acl"} or $self->{"options"}{"acl"} eq ""
not defined $self->{"options"}{"acl"} $self->{"options"}{"acl"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"acl"}B= $self->{"options"}{"acl"} eq ""
ABresult
1X1
011
000
238 1 $self->{perror} = 1;
239 1 $self->{error} = 32;
240 1 $self->{errorString} = 'the option acl is undef or blank';
241 1 $self->warn;
242 }
243  
244 # defaults
245 1 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 1 0 1
246 1 ▶ $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"insecure"} 1 0 1
247  
248 1 return $self;
249 } ## end sub new
250  
251 # Internal helper. Returns the ACL entries endpoint.
252 sub _entries_url {
253 8 ▶ my ($self) = @_;
Subroutine _entries_url: called CC=1 SCAR=0.0
254  
255 return
256 'https://api.fastly.com/service/'
257 . $self->{options}{service}
258 . '/acl/'
259 . $self->{options}{acl}
260 8 . '/entries';
261 }
262  
263 # Internal helper. Returns the ACL entry endpoint, optionally suffixed with an
264 # entry ID.
265 sub _entry_url {
266 5 ▶ my ( $self, $id ) = @_;
Subroutine _entry_url: called CC=2 SCAR=6.9
267  
268 my $url
269 = 'https://api.fastly.com/service/'
270 . $self->{options}{service}
271 . '/acl/'
272 . $self->{options}{acl}
273 5 . '/entry';
274  
275 5 ▶ if ( defined($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 2 3 5
276 2 $url .= '/' . $id;
277 }
278  
279 5 return $url;
280 }
281  
282 # Internal helper. Minimal percent encoder so URI::Escape is not needed.
283 sub _uri_escape {
284 0 ▶ my ( $self, $string ) = @_;
Subroutine _uri_escape: not called CC=1 SCAR=6.9
285  
286 0 $string =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
287  
288 0 return $string;
289 }
290  
291 # Internal helper. Returns a canonical JSON::PP encoder/decoder.
292 sub _json {
293 3 ▶ my ($self) = @_;
Subroutine _json: called CC=1 SCAR=0.0
294  
295 3 require JSON::PP;
296 3 return JSON::PP->new->canonical->utf8;
297 }
298  
299 # Internal helper. Performs a HTTP request via LWP::UserAgent, returning the
300 # decoded JSON body (or undef for an empty body) and dying with a explanation on
301 # any HTTP level failure. Never called in testing mode.
302 sub _request {
303 0 ▶ my ( $self, $method, $url, $body ) = @_;
Subroutine _request: not called CC=8 SCAR=42.8
304  
305 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
306 0 local $@;
307 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%args) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::fastly::VERSION, "timeout", $self->{"options"}{"timeout"}); if ($self->{"options"}{"insecure"}) { $args{"ssl_opts"} = {"verify_hostname", 0, "SSL_verify_mode", 0}; } ; $self->{"ua"} = "LWP::UserAgent"->new(%args); 1 } } or die 'failed to load LWP::UserAgent, which the fastly backend requires... ' . $@ 0 0 0
308 0 require LWP::UserAgent;
309 my %args = (
310 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
311 timeout => $self->{options}{timeout},
312 0 );
313 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
314 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
315 }
316 0 $self->{ua} = LWP::UserAgent->new(%args);
317 0 1;
318 } or die( 'failed to load LWP::UserAgent, which the fastly backend requires... ' . $@ );
319 }
320  
321 0 my @headers = ( 'Content-Type' => 'application/json', 'Fastly-Key' => $self->{options}{token} );
322  
323 0 require HTTP::Request;
324 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
325  
326 0 my $response = $self->{ua}->request($request);
327  
328 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
329 0 my $detail = $response->decoded_content;
330 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
331 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
332 }
333  
334 0 my $decoded;
335 0 my $content = $response->decoded_content;
336 0 ▶ if ( defined($content) && $content ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and $content ne "") 0 0 0
Condition0%0/3
defined $content and $content ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content ne ""
defined $content $content ne ""
Truth table0%0/3
A and B
A= defined $contentB= $content ne ""
ABresult
0X0
100
111
337 0 local $@;
338 0 eval { $decoded = $self->_json->decode($content); };
339 }
340  
341 0 return $decoded;
342 } ## end sub _request
343  
344 =head2 init
345  
346 Initiates the backend. Verifies the credentials and endpoint by fetching a
347 single ACL entry.
348  
349 $backend->init;
350  
351 =cut
352  
353 sub init {
354 1 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.3
Pod documented
355  
356 1 $self->errorblank;
357  
358 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
359 0 $self->{error} = 18;
360 0 $self->{errorString} = 'backend has already been inited';
361 0 $self->warn;
362 }
363  
364 1 my $url = $self->_entries_url . '?per_page=1';
365  
366 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
367 1 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
368 } else {
369 0 local $@;
370 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the ACL entries endpoint failed... ' . $@; $self->warn } 0 0 0
371 0 $self->{error} = 23;
372 0 $self->{errorString} = 'init failed. probing the ACL entries endpoint failed... ' . $@;
373 0 $self->warn;
374 };
375 }
376  
377 1 $self->{inited} = 1;
378 } ## end sub init
379  
380 =head2 ban
381  
382 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
383 and lowercased, then an ACL entry is created for it via a POST, with a /32
384 subnet for IPv4 and a /128 subnet for IPv6. Banning an already banned IP is
385 a noop.
386  
387 $backend->ban(ban => $ip);
388  
389 =cut
390  
391 sub ban {
392 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=11 SCAR=37.7
Pod documented
393  
394 2 $self->errorblank;
395  
396 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
397 0 $self->{error} = 1;
398 0 $self->{errorString} = 'backend has not been inited';
399 0 $self->warn;
400 0 return;
401 }
402  
403 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/) { } 0 2 2
404 0 $self->{error} = 9;
405 0 $self->{errorString} = 'Nothing specified for the value ban';
406 0 $self->warn;
407 0 return;
408 } elsif ( ref( $opts{ban} ) ne '' ) {
409 0 $self->{error} = 10;
410 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
411 0 $self->warn;
412 0 return;
413 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
l!l&&r!l&&!r
110
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
ABresult
1X1
011
000
414 && $opts{ban} !~ /\A$IPv6_re\z/ )
415 {
416 0 $self->{error} = 10;
417 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
418 0 $self->warn;
419 0 return;
420 }
421  
422 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
423 2 $opts{ban} = lc( $opts{ban} );
424  
425 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
426 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
427 0 $self->{frontend_obj}->{test_data} = 'already banned';
428 }
429 0 return;
430 }
431  
432 2 ▶ my $subnet = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ ? : 1 1 2
433 2 my $body = $self->_json->encode( { ip => $opts{ban}, subnet => $subnet } );
434  
435 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
436 2 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_entry_url, content => $body } ];
437 } else {
438 0 local $@;
439 0 ▶ eval { $self->_request( 'POST', $self->_entry_url, $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $self->_entry_url, $body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
440 0 $self->{error} = 13;
441 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
442 0 $self->warn;
443 0 return;
444 };
445 }
446  
447 2 $self->{banned}{ $opts{ban} } = 1;
448 } ## end sub ban
449  
450 # Internal helper. Looks up the ACL entry for the IP and deletes it by its ID.
451 # An entry that can not be found is treated as already unbanned. Dies on
452 # failure. Never called in testing mode.
453 sub _unban_ip {
454 0 ▶ my ( $self, $ip ) = @_;
Subroutine _unban_ip: not called CC=7 SCAR=40.3
455  
456 0 my $decoded = $self->_request( 'GET', $self->_entries_url );
457  
458 0 my $id;
459 0 ▶ if ( ref($decoded) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded eq "ARRAY") 0 0 0
460 0 foreach my $entry ( @{$decoded} ) {
461 0 ▶ if ( ref($entry) eq 'HASH' && defined( $entry->{ip} ) && $entry->{ip} eq $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $ip) 0 0 0
Condition0%0/6
ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $ip
!ll&&!rl&&r
000
ref $entry eq "HASH" and defined $entry->{"ip"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $ip
ref $entry eq "HASH" defined $entry->{"ip"} $entry->{"ip"} eq $ip
Truth table0%0/4
A and B and C
A= ref $entry eq "HASH"B= defined $entry->{"ip"}C= $entry->{"ip"} eq $ip
ABCresult
0XX0
10X0
1100
1111
462 0 $id = $entry->{id};
463 0 last;
464 }
465 }
466 }
467  
468 # no entry found means something else removed it already, which is fine
469 0 ▶ if ( !defined($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
470 0 return;
471 }
472  
473 0 $self->_request( 'DELETE', $self->_entry_url($id) );
474  
475 0 return;
476 } ## end sub _unban_ip
477  
478 =head2 unban
479  
480 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
481 and lowercased, then the ACL entries are fetched, the entry for the IP is
482 found, and it is deleted by its ID. An entry that can not be found is
483 treated as already unbanned. Unbanning an IP that is not banned is a noop.
484  
485 $backend->unban(ban => $ip);
486  
487 =cut
488  
489 sub unban {
490 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=38.2
Pod documented
491  
492 1 $self->errorblank;
493  
494 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
495 0 $self->{error} = 1;
496 0 $self->{errorString} = 'backend has not been inited';
497 0 $self->warn;
498 0 return;
499 }
500  
501 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/) { } 0 1 1
502 0 $self->{error} = 9;
503 0 $self->{errorString} = 'Nothing specified for the value ban';
504 0 $self->warn;
505 0 return;
506 } elsif ( ref( $opts{ban} ) ne '' ) {
507 0 $self->{error} = 10;
508 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
509 0 $self->warn;
510 0 return;
511 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/
ABresult
1X1
011
000
512 && $opts{ban} !~ /\A$IPv6_re\z/ )
513 {
514 0 $self->{error} = 10;
515 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
516 0 $self->warn;
517 0 return;
518 }
519  
520 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
521 1 $opts{ban} = lc( $opts{ban} );
522  
523 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
524 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
525 0 $self->{frontend_obj}->{test_data} = 'not banned';
526 }
527 0 return;
528 }
529  
530 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
531 $self->{frontend_obj}->{test_data} = [
532 1 { method => 'GET', url => $self->_entries_url },
533 { method => 'DELETE', url => $self->_entry_url('<id>') },
534 ];
535 } else {
536 0 local $@;
537 0 ▶ eval { $self->_unban_ip( $opts{ban} ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_ip($opts{"ban"}); 1 } } or do { $self->{"error"} = 14; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
538 0 $self->{error} = 14;
539 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
540 0 $self->warn;
541 0 return;
542 };
543 }
544  
545 1 delete( $self->{banned}{ $opts{ban} } );
546 } ## end sub unban
547  
548 =head2 list
549  
550 List banned IPs. Returns an array of the currently banned single IPs. CIDR
551 ranges are not included; for those see L</list_cidr>.
552  
553 my @banned = $backend->list;
554  
555 =cut
556  
557 sub list {
558 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
559  
560 0 $self->errorblank;
561  
562 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
563 0 $self->{frontend_obj}->{test_data} = 'list';
564 }
565  
566 0 return keys( %{ $self->{banned} } );
567 }
568  
569 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
570 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
571 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
572 # IPv6). Returns false otherwise.
573 sub _valid_cidr {
574 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
575  
576 2 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 2 2
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
577  
578 2 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 2 0 2
579 2 my ( $addr, $prefix ) = ( $1, $2 );
580 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
581 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
582 }
583  
584 0 return 0;
585 } ## end sub _valid_cidr
586  
587 =head2 ban_cidr
588  
589 Bans a CIDR range by creating an ACL entry for it. The range is split into its
590 address and prefix length, sent as the ACL entry's ip and subnet, mirroring how
591 a single IP is banned as a /32 or /128 entry. The value of ban is validated
592 as being a IPv4 or IPv6 CIDR range and lowercased. Banning an already
593 banned range is a noop.
594  
595 $backend->ban_cidr(ban => '1.2.3.0/24');
596  
597 =cut
598  
599 sub ban_cidr {
600 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=35.2
Pod documented
601  
602 1 $self->errorblank;
603  
604 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
605 0 $self->{error} = 1;
606 0 $self->{errorString} = 'backend has not been inited';
607 0 $self->warn;
608 0 return;
609 }
610  
611 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 1 1
612 0 $self->{error} = 9;
613 0 $self->{errorString} = 'Nothing specified for the value ban';
614 0 $self->warn;
615 0 return;
616 } elsif ( ref( $opts{ban} ) ne '' ) {
617 0 $self->{error} = 35;
618 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
619 0 $self->warn;
620 0 return;
621 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
622 0 $self->{error} = 35;
623 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
624 0 $self->warn;
625 0 return;
626 }
627  
628 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
629 1 $opts{ban} = lc( $opts{ban} );
630  
631 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
632 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
633 0 $self->{frontend_obj}->{test_data} = 'already banned';
634 }
635 0 return;
636 }
637  
638 1 my ( $cidr_ip, $cidr_subnet ) = split( m!/!, $opts{ban}, 2 );
639 # numify the prefix length so it is encoded as a JSON number, matching how
640 # ban() sends the /32 or /128 subnet
641 1 $cidr_subnet += 0;
642 1 my $body = $self->_json->encode( { ip => $cidr_ip, subnet => $cidr_subnet } );
643  
644 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
645 1 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $self->_entry_url, content => $body } ];
646 } else {
647 0 local $@;
648 0 ▶ eval { $self->_request( 'POST', $self->_entry_url, $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $self->_entry_url, $body); 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
649 0 $self->{error} = 33;
650 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
651 0 $self->warn;
652 0 return;
653 };
654 }
655  
656 1 $self->{banned_cidr}{ $opts{ban} } = 1;
657 } ## end sub ban_cidr
658  
659 # Internal helper. Looks up the ACL entry for the CIDR range and deletes it by
660 # its ID. An entry that can not be found is treated as already unbanned. Dies on
661 # failure. Never called in testing mode.
662 sub _unban_cidr_range {
663 0 ▶ my ( $self, $cidr ) = @_;
Subroutine _unban_cidr_range: not called CC=9 SCAR=45.0
664  
665 0 my ( $cidr_ip, $cidr_subnet ) = split( m!/!, $cidr, 2 );
666  
667 0 my $decoded = $self->_request( 'GET', $self->_entries_url );
668  
669 0 my $id;
670 0 ▶ if ( ref($decoded) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded eq "ARRAY") 0 0 0
671 0 foreach my $entry ( @{$decoded} ) {
672 0 ▶ if ( ref($entry) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $cidr_ip and defined $entry->{"subnet"} and $entry->{"subnet"} eq $cidr_subnet) 0 0 0
Condition0%0/12
ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $cidr_ip and defined $entry->{"subnet"} and $entry->{"subnet"} eq $cidr_subnet
!ll&&!rl&&r
000
ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $cidr_ip and defined $entry->{"subnet"}
!ll&&!rl&&r
000
ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $cidr_ip
!ll&&!rl&&r
000
ref $entry eq "HASH" and defined $entry->{"ip"}
!ll&&!rl&&r
000
MC/DC0%0/5
ref $entry eq "HASH" and defined $entry->{"ip"} and $entry->{"ip"} eq $cidr_ip and defined $entry->{"subnet"} and $entry->{"subnet"} eq $cidr_subnet
ref $entry eq "HASH" defined $entry->{"ip"} $entry->{"ip"} eq $cidr_ip defined $entry->{"subnet"} $entry->{"subnet"} eq $cidr_subnet
Truth table0%0/6
A and B and C and D and E
A= ref $entry eq "HASH"B= defined $entry->{"ip"}C= $entry->{"ip"} eq $cidr_ipD= defined $entry->{"subnet"}E= $entry->{"subnet"} eq $cidr_subnet
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
673 && defined( $entry->{ip} )
674 && $entry->{ip} eq $cidr_ip
675 && defined( $entry->{subnet} )
676 && $entry->{subnet} eq $cidr_subnet )
677 {
678 0 $id = $entry->{id};
679 0 last;
680 }
681 }
682 }
683  
684 # no entry found means something else removed it already, which is fine
685 0 ▶ if ( !defined($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
686 0 return;
687 }
688  
689 0 $self->_request( 'DELETE', $self->_entry_url($id) );
690  
691 0 return;
692 } ## end sub _unban_cidr_range
693  
694 =head2 unban_cidr
695  
696 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
697 CIDR range and lowercased, then the ACL entries are fetched, the entry
698 matching its address and prefix length is found, and it is deleted by its
699 ID. An entry that can not be found is treated as already unbanned.
700 Unbanning a range that is not banned is a noop.
701  
702 $backend->unban_cidr(ban => '1.2.3.0/24');
703  
704 =cut
705  
706 sub unban_cidr {
707 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=36.4
Pod documented
708  
709 1 $self->errorblank;
710  
711 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
712 0 $self->{error} = 1;
713 0 $self->{errorString} = 'backend has not been inited';
714 0 $self->warn;
715 0 return;
716 }
717  
718 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 1 1
719 0 $self->{error} = 9;
720 0 $self->{errorString} = 'Nothing specified for the value ban';
721 0 $self->warn;
722 0 return;
723 } elsif ( ref( $opts{ban} ) ne '' ) {
724 0 $self->{error} = 35;
725 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
726 0 $self->warn;
727 0 return;
728 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
729 0 $self->{error} = 35;
730 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
731 0 $self->warn;
732 0 return;
733 }
734  
735 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
736 1 $opts{ban} = lc( $opts{ban} );
737  
738 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
739 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
740 0 $self->{frontend_obj}->{test_data} = 'not banned';
741 }
742 0 return;
743 }
744  
745 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
746 $self->{frontend_obj}->{test_data} = [
747 1 { method => 'GET', url => $self->_entries_url },
748 { method => 'DELETE', url => $self->_entry_url('<id>') },
749 ];
750 } else {
751 0 local $@;
752 0 ▶ eval { $self->_unban_cidr_range( $opts{ban} ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_cidr_range($opts{"ban"}); 1 } } or do { $self->{"error"} = 34; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
753 0 $self->{error} = 34;
754 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
755 0 $self->warn;
756 0 return;
757 };
758 }
759  
760 1 delete( $self->{banned_cidr}{ $opts{ban} } );
761 } ## end sub unban_cidr
762  
763 =head2 list_cidr
764  
765 List banned CIDR ranges. Returns an array of the currently banned CIDR
766 ranges. Single IPs are not included; for those see L</list>.
767  
768 my @banned_cidrs = $backend->list_cidr;
769  
770 =cut
771  
772 sub list_cidr {
773 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
774  
775 2 $self->errorblank;
776  
777 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
778 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
779 }
780  
781 2 return keys( %{ $self->{banned_cidr} } );
782 }
783  
784 =head2 re_init
785  
786 Tells the backend to re-init it's self.
787  
788 This will call teardown and init again. After that it will re-add all
789 previously added bans, POSTing an ACL entry for each retained IP and CIDR
790 range.
791  
792 $backend->re_init;
793  
794 =cut
795  
796 sub re_init {
797 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=10 SCAR=47.0
Pod documented
798  
799 0 $self->errorblank;
800  
801 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
802 0 $self->{error} = 1;
803 0 $self->{errorString} = 'backend has not been inited';
804 0 $self->warn;
805 0 return;
806 }
807  
808 # teardown is best effort here as a partially or fully wiped setup is
809 # exactly what re_init needs to recover from; init cleans up any remnants
810 {
811 0 local $@;
812 0 eval { $self->teardown; };
813 }
814 0 $self->init;
815  
816 0 my @re_init_test_data;
817 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
818 0 ▶ my $subnet = ( $item =~ /\A$IPv4_re\z/ ) ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$item =~ /\A$Net::Firewall::BlockerHelper::backends::fastly::IPv4_re\z/ ? : 0 0 0
819 0 my $body = $self->_json->encode( { ip => $item, subnet => $subnet } );
820  
821 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
822 0 push( @re_init_test_data, { method => 'POST', url => $self->_entry_url, content => $body } );
823 } else {
824 0 local $@;
825 0 ▶ eval { $self->_request( 'POST', $self->_entry_url, $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $self->_entry_url, $body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
826 0 $self->{error} = 13;
827 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
828 0 $self->warn;
829 };
830 }
831 } ## end foreach my $item ( keys( %{ ...}))
832  
833 # re-add the CIDR ranges as well, splitting each into its ip and subnet the
834 # same way ban_cidr does
835 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
836 0 my ( $cidr_ip, $cidr_subnet ) = split( m!/!, $item, 2 );
837 0 $cidr_subnet += 0;
838 0 my $body = $self->_json->encode( { ip => $cidr_ip, subnet => $cidr_subnet } );
839  
840 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
841 0 push( @re_init_test_data, { method => 'POST', url => $self->_entry_url, content => $body } );
842 } else {
843 0 local $@;
844 0 ▶ eval { $self->_request( 'POST', $self->_entry_url, $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $self->_entry_url, $body); 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
845 0 $self->{error} = 33;
846 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
847 0 $self->warn;
848 };
849 }
850 } ## end foreach my $item ( keys( %{ ...}))
851  
852 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
853 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
854 }
855  
856 0 $self->{inited} = 1;
857 } ## end sub re_init
858  
859 =head2 teardown
860  
861 Tears down the setup for the backend by removing the ACL entry for each
862 currently banned IP. The internal list of bans is kept, so a following
863 re_init will re-add them.
864  
865 $backend->teardown;
866  
867 =cut
868  
869 sub teardown {
870 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
871  
872 0 $self->errorblank;
873  
874 0 $self->{inited} = 0;
875  
876 0 my @requests;
877 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
878 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
879 0 push(
880 @requests,
881 { method => 'GET', url => $self->_entries_url },
882 { method => 'DELETE', url => $self->_entry_url('<id>') }
883 );
884 } else {
885 0 local $@;
886 0 ▶ eval { $self->_unban_ip($item); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_ip($item); 1 } } or do { $self->{"error"} = 17; $self->{"errorString"} = 'teardown failed removing the entry for "' . $item . '"... ' . $@; $self->warn } 0 0 0
887 0 $self->{error} = 17;
888 0 $self->{errorString} = 'teardown failed removing the entry for "' . $item . '"... ' . $@;
889 0 $self->warn;
890 };
891 }
892 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
893  
894 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
895 0 $self->{frontend_obj}->{test_data} = \@requests;
896 }
897 } ## end sub teardown
898  
899 =head2 stop
900  
901 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
902 concept.
903  
904 $backend->stop;
905  
906 =cut
907  
908 sub stop {
909 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
910  
911 0 return $self->teardown(%opts);
912 }
913  
914 =head2 check
915  
916 Verifies the endpoint and credentials are still usable by fetching a single
917 ACL entry. Returns a true value if so and a false value otherwise. This is the
918 equivalent of fail2ban's C<actioncheck>.
919  
920 if ( !$backend->check ) {
921 $backend->re_init;
922 }
923  
924 =cut
925  
926 sub check {
927 5 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=14.7
Pod documented
928  
929 5 $self->errorblank;
930  
931 5 my $url = $self->_entries_url . '?per_page=1';
932  
933 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
934 5 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
935 5 return 1;
936 }
937  
938 0 local $@;
939 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or return 0 0 0 0
940  
941 0 return 1;
942 } ## end sub check
943  
944 =head2 flush
945  
946 Removes all currently banned IPs at once by removing their ACL entries and
947 forgetting them. This is the equivalent of fail2ban's C<actionflush>.
948  
949 $backend->flush;
950  
951 =cut
952  
953 sub flush {
954 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=9 SCAR=45.0
Pod documented
955  
956 0 $self->errorblank;
957  
958 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
959 0 $self->{error} = 1;
960 0 $self->{errorString} = 'backend has not been inited';
961 0 $self->warn;
962 0 return;
963 }
964  
965 0 my @requests;
966 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
967 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
968 0 push(
969 @requests,
970 { method => 'GET', url => $self->_entries_url },
971 { method => 'DELETE', url => $self->_entry_url('<id>') }
972 );
973 } else {
974 0 local $@;
975 0 ▶ eval { $self->_unban_ip($item); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_ip($item); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed removing the entry for "' . $item . '"... ' . $@; $self->warn } 0 0 0
976 0 $self->{error} = 25;
977 0 $self->{errorString} = 'flush failed removing the entry for "' . $item . '"... ' . $@;
978 0 $self->warn;
979 };
980 }
981 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
982  
983 # remove the CIDR entries the same way
984 0 foreach my $item ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
985 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
986 0 push(
987 @requests,
988 { method => 'GET', url => $self->_entries_url },
989 { method => 'DELETE', url => $self->_entry_url('<id>') }
990 );
991 } else {
992 0 local $@;
993 0 ▶ eval { $self->_unban_cidr_range($item); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_cidr_range($item); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed removing the entry for "' . $item . '"... ' . $@; $self->warn } 0 0 0
994 0 $self->{error} = 25;
995 0 $self->{errorString} = 'flush failed removing the entry for "' . $item . '"... ' . $@;
996 0 $self->warn;
997 };
998 }
999 } ## end foreach my $item ( sort( keys( %{ $self->{banned_cidr...})))
1000  
1001 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1002 0 $self->{frontend_obj}->{test_data} = \@requests;
1003 }
1004  
1005 0 $self->{banned} = {};
1006 0 $self->{banned_cidr} = {};
1007 } ## end sub flush
1008  
1009 =head1 ERROR CODES / FLAGS
1010  
1011 Error handling is provided by L<Error::Helper>. All
1012 errors are considered fatal.
1013  
1014 =head2 1, notInited
1015  
1016 The backend has not been inited yet.
1017  
1018 =head2 6, invalidPrefixSpecified
1019  
1020 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1021  
1022 =head2 7, invalidName
1023  
1024 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1025  
1026 =head2 8, optionsNotHash
1027  
1028 The item passed to new for options is not a hash.
1029  
1030 =head2 9, noBanItem
1031  
1032 No IP or CIDR range specified to ban or unban.
1033  
1034 =head2 10, banItemNotIP
1035  
1036 The item to ban is not an IP. Either wrong ref type or regexp
1037 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1038  
1039 =head2 12, backendInitError
1040  
1041 Failed to init the backend.
1042  
1043 =head2 13, banFailed
1044  
1045 Failed to ban the item.
1046  
1047 =head2 14, unbanFailed
1048  
1049 Failed to unban the item.
1050  
1051 =head2 15, listFailed
1052  
1053 Failed to get a list of bans.
1054  
1055 =head2 16, reInitFailed
1056  
1057 Failed to re_init the backend.
1058  
1059 =head2 17, teardownFailed
1060  
1061 Failed to teardown the backend.
1062  
1063 =head2 18, alreadyInited
1064  
1065 init called, but the backend has already been inited.
1066  
1067 =head2 23, initFailed
1068  
1069 Init failed. Probing the ACL entries endpoint failed.
1070  
1071 =head2 24, checkFailed
1072  
1073 The backend check raised an error.
1074  
1075 =head2 25, flushFailed
1076  
1077 Failed to flush the bans.
1078  
1079 =head2 26, portsNotSupported
1080  
1081 The fastly backend blocks whole IPs and does not support ports.
1082  
1083 =head2 27, protocolsNotSupported
1084  
1085 The fastly backend blocks whole IPs and does not support protocols.
1086  
1087 =head2 30, serviceNotDefined
1088  
1089 The option service is undef or blank.
1090  
1091 =head2 31, tokenNotDefined
1092  
1093 The option token is undef or blank.
1094  
1095 =head2 32, aclNotDefined
1096  
1097 The option acl is undef or blank.
1098  
1099 =head2 33, banCidrFailed
1100  
1101 Failed to ban the CIDR range.
1102  
1103 =head2 34, unbanCidrFailed
1104  
1105 Failed to unban the CIDR range.
1106  
1107 =head2 35, cidrItemNotCidr
1108  
1109 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1110 IPv4 or IPv6 address followed by a prefix length valid for its family.
1111  
1112 =head2 36, cidrNotSupported
1113  
1114 The backend does not support CIDR bans.
1115  
1116 =head2 37, listCidrFailed
1117  
1118 Failed to get a list of CIDR bans.
1119  
1120 =head1 AUTHOR
1121  
1122 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1123  
1124 =head1 BUGS
1125  
1126 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1127 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
1128  
1129 =head1 LICENSE AND COPYRIGHT
1130  
1131 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1132  
1133 This is free software, licensed under:
1134  
1135 The GNU Lesser General Public License, Version 2.1, February 1999
1136  
1137  
1138 =cut
1139  
1140 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/dummy.pm ↑ Summary BlockerHelper/backends/cloudflare.pm »