BlockerHelper/backends/aws_wafv2.pm

filter: Statementstmt 45.8% 156 / 340 Branchbran 41.8% 62 / 148 Conditioncond 46.3% 25 / 54 MC/DCmcdc 17.6% 6 / 34 Subroutinesub 79.3% 23 / 29 Podpod 100% 13 / 13 totaltotal 46.1% 285 / 618 CC 102 SCAR 31.8
CC
102
Cov
45%
unban
110.0
ban_cidr
37.6
unban_cidr
30.4
CRAP 24.1
« BlockerHelper/backends/linux_ip_route.pm ↑ Summary BlockerHelper/backends/xdp.pm »
1 package Net::Firewall::BlockerHelper::backends::aws_wafv2;
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::aws_wafv2 - AWS WAFv2 IP set backend via the aws CLI.
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 => 'aws_wafv2',
28 name => 'ssh',
29 options => {
30 scope => 'REGIONAL',
31 name4 => 'blocklist-v4',
32 id4 => 'aaaa-bbbb',
33 name6 => 'blocklist-v6',
34 id6 => 'cccc-dddd',
35 },
36 );
37  
38 $fw_helper->init_backend;
39 $fw_helper->ban( ban => '1.2.3.4' );
40 $fw_helper->unban( ban => '1.2.3.4' );
41  
42 =head1 DESCRIPTION
43  
44 Blocks IPs by maintaining the addresses of AWS WAFv2 IP sets, using the C<aws>
45 CLI. A WAF rule referencing the IP set(s) then blocks the traffic.
46  
47 A WAFv2 IP set holds a single address family, so this backend manages up to
48 two of them: an IPv4 set (C<name4>/C<id4>) and an IPv6 set (C<name6>/C<id6>).
49 Banning an address of a family whose set is not configured is an error.
50  
51 Updating an IP set is a two step operation: a C<get-ip-set> to obtain the
52 current C<LockToken>, then an C<update-ip-set> supplying the full desired
53 address list plus that token (WAFv2 uses optimistic locking). The full banned
54 set for the family is rendered on every change.
55  
56 This backend manages only the IP set contents; the IP set(s) and the WAF rule
57 and Web ACL referencing them must already exist. The Web ACL must also be
58 associated with the resources to be protected, ALBs, API Gateways, and the
59 like for REGIONAL or the distribution for CLOUDFRONT, as a Web ACL nothing
60 uses blocks nothing.
61  
62 Requires the C<aws> CLI in the C<PATH>, configured with credentials able to
63 get and update the IP sets.
64  
65 =head1 METHODS
66  
67 =head2 new
68  
69 - options :: Backend specific options. See below.
70 - name :: Required by Net::Firewall::BlockerHelper, otherwise unused.
71  
72 The options hash accepts the following.
73  
74 - aws_cmd :: Path to the aws binary.
75 - Default :: aws
76  
77 - scope :: WAFv2 scope, 'REGIONAL' or 'CLOUDFRONT'.
78 - Default :: REGIONAL
79  
80 - region :: Optional region; appended as --region when set.
81 - Default :: undef
82  
83 - name4 / id4 :: Name and id of the IPv4 IP set.
84 - Default :: undef
85  
86 - name6 / id6 :: Name and id of the IPv6 IP set.
87 - Default :: undef
88  
89 At least the family being banned must have both its name and id set.
90  
91 All errors are considered fatal, meaning if new fails it will die.
92  
93 =cut
94  
95 sub new {
96 3 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=11 SCAR=26.3
Pod documented
97  
98 3 my $self = {
99 perror => undef,
100 error => undef,
101 errorLine => undef,
102 errorFilename => undef,
103 errorString => "",
104 errorExtra => {
105 all_errors_fatal => 1,
106 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
107 # is kept for the name documented in its POD
108 all_fatal => 1,
109 flags => {
110 1 => 'notInited',
111 8 => 'optionsNotHash',
112 9 => 'noBanItem',
113 10 => 'banItemNotIP',
114 12 => 'backendInitError',
115 13 => 'banFailed',
116 14 => 'unbanFailed',
117 15 => 'listFailed',
118 16 => 'reInitFailed',
119 17 => 'teardownFailed',
120 18 => 'alreadyInited',
121 20 => 'scopeInvalid',
122 24 => 'checkFailed',
123 25 => 'flushFailed',
124 30 => 'ipsetNotConfigured',
125 31 => 'banCidrFailed',
126 32 => 'unbanCidrFailed',
127 33 => 'cidrItemNotCidr',
128 34 => 'cidrNotSupported',
129 35 => 'listCidrFailed',
130 },
131 fatal_flags => {},
132 perror_not_fatal => 0,
133 },
134 options => {},
135 ports => [],
136 protocols => [],
137 testing => undef,
138 test_data => undef,
139 prefix => 'kur',
140 name => undef,
141 frontend_obj => undef,
142 inited => 0,
143 banned => {},
144 banned_cidr => {},
145 cidr_supported => 1,
146 };
147 3 bless $self;
148  
149 3 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 3 0 3
150 3 $self->{testing} = $opts{testing};
151 }
152 3 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 3 0 3
153 3 $self->{frontend_obj} = $opts{frontend_obj};
154 }
155 3 ▶ if ( defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 3 0 3
156 3 $self->{name} = $opts{name};
157 }
158  
159 3 ▶ if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'} and ref $opts{'options'} ne 'HASH') { } 0 3 3
elsif (defined $opts{'options'}) { } 3 0 3
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
!ll&&!rl&&r
030
MC/DC0%0/2
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
defined $opts{"options"} ref $opts{"options"} ne "HASH"
Truth table33%1/3
A and B
A= defined $opts{"options"}B= ref $opts{"options"} ne "HASH"
ABresult
0X0
100
111
160 0 $self->{perror} = 1;
161 0 $self->{error} = 8;
162 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
163 0 $self->warn;
164 } elsif ( defined( $opts{options} ) ) {
165 3 $self->{options} = $opts{options};
166 }
167  
168 3 ▶ $self->{options}{aws_cmd} = 'aws' if ( !defined( $self->{options}{aws_cmd} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"aws_cmd"} 3 0 3
169 3 ▶ $self->{options}{scope} = 'REGIONAL' if ( !defined( $self->{options}{scope} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"scope"} 2 1 3
170  
171 3 ▶ if ( $self->{options}{scope} ne 'REGIONAL' && $self->{options}{scope} ne 'CLOUDFRONT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"scope"} ne "REGIONAL" and $self->{"options"}{"scope"} ne "CLOUDFRONT") 1 2 3
Condition66%2/3
$self->{"options"}{"scope"} ne "REGIONAL" and $self->{"options"}{"scope"} ne "CLOUDFRONT"
!ll&&!rl&&r
201
MC/DC50%1/2
$self->{"options"}{"scope"} ne "REGIONAL" and $self->{"options"}{"scope"} ne "CLOUDFRONT"
$self->{"options"}{"scope"} ne "REGIONAL" $self->{"options"}{"scope"} ne "CLOUDFRONT"
Truth table66%2/3
A and B
A= $self->{"options"}{"scope"} ne "REGIONAL"B= $self->{"options"}{"scope"} ne "CLOUDFRONT"
ABresult
0X0
100
111
172 1 $self->{perror} = 1;
173 1 $self->{error} = 20;
174 1 $self->{errorString} = 'scope is "' . $self->{options}{scope} . '" and not "REGIONAL" or "CLOUDFRONT"';
175 1 $self->warn;
176 }
177  
178 2 return $self;
179 } ## end sub new
180  
181 # Internal helper. Returns the trailing --region argument when configured.
182 sub _region_suffix {
183 24 ▶ my ($self) = @_;
Subroutine _region_suffix: called CC=3 SCAR=13.3
184  
185 24 ▶ if ( defined( $self->{options}{region} ) && $self->{options}{region} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"region"} and $self->{"options"}{"region"} ne "") 0 24 24
Condition33%1/3
defined $self->{"options"}{"region"} and $self->{"options"}{"region"} ne ""
!ll&&!rl&&r
2400
MC/DC0%0/2
defined $self->{"options"}{"region"} and $self->{"options"}{"region"} ne ""
defined $self->{"options"}{"region"} $self->{"options"}{"region"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"options"}{"region"}B= $self->{"options"}{"region"} ne ""
ABresult
0X0
100
111
186 0 return ' --region ' . $self->{options}{region};
187 }
188 24 return '';
189 }
190  
191  
192 # Internal helper. Returns the (name, id) pair for family 4 or 6, either of
193 # which may be undef when not configured.
194 sub _conf_for_family {
195 40 ▶ my ( $self, $fam ) = @_;
Subroutine _conf_for_family: called CC=2 SCAR=6.9
196  
197 40 ▶ if ( $fam == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam == 4) 23 17 40
198 23 return ( $self->{options}{name4}, $self->{options}{id4} );
199 }
200 17 return ( $self->{options}{name6}, $self->{options}{id6} );
201 }
202  
203 # Internal helper. True if the given family (4 or 6) has both a name and id.
204 sub _family_configured {
205 21 ▶ my ( $self, $fam ) = @_;
Subroutine _family_configured: called CC=5 SCAR=16.6
206  
207 21 my ( $name, $id ) = $self->_conf_for_family($fam);
208 21 ▶ return ( defined($name) && $name ne '' && defined($id) && $id ne '' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && $name ne '' && defined $id && $id ne '' ? : 18 3 21
Condition66%6/9
defined $name and $name ne "" and defined $id and $id ne ""
!ll&&!rl&&r
3018
defined $name and $name ne "" and defined $id
!ll&&!rl&&r
3018
defined $name and $name ne ""
!ll&&!rl&&r
3018
MC/DC25%1/4
defined $name and $name ne "" and defined $id and $id ne ""
defined $name $name ne "" defined $id $id ne ""
Truth table40%2/5
A and B and C and D
A= defined $nameB= $name ne ""C= defined $idD= $id ne ""
ABCDresult
0XXX0
10XX0
110X0
11100
11111
209 }
210  
211 # Internal helper. Returns the list of configured families (4 and/or 6).
212 sub _configured_families {
213 8 ▶ my ($self) = @_;
Subroutine _configured_families: called CC=3 SCAR=11.0
214  
215 8 my @families;
216 8 ▶ push( @families, 4 ) if ( $self->_family_configured(4) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_family_configured(4) 8 0 8
217 8 ▶ push( @families, 6 ) if ( $self->_family_configured(6) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_family_configured(6) 6 2 8
218 8 return @families;
219 }
220  
221 # Internal helper. Returns the banned IPs and CIDR ranges of the given family,
222 # sorted, each as a CIDR, space joined. Single IPs are rendered with an explicit
223 # /32 or /128; banned CIDR ranges are already CIDRs and are emitted as is.
224 sub _render_family {
225 5 ▶ my ( $self, $fam ) = @_;
Subroutine _render_family: called CC=15 SCAR=28.2
226  
227 5 my @addresses;
228 5 foreach my $ip ( sort( keys( %{ $self->{banned} } ) ) ) {
229 12 ▶ my $is_v4 = ( $ip =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ ? : 9 3 12
230 12 ▶ next if ( $fam == 4 && !$is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $fam == 4 and not $is_v4 2 10 12
Condition100%3/3
$fam == 4 and not $is_v4
!ll&&!rl&&r
372
MC/DC100%2/2
$fam == 4 and not $is_v4
$fam == 4 not $is_v4
Truth table100%3/3
A and not(B)
A= $fam == 4B= not $is_v4
ABresult
0X0
100
111
231 10 ▶ next if ( $fam == 6 && $is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $fam == 6 and $is_v4 2 8 10
Condition100%3/3
$fam == 6 and $is_v4
!ll&&!rl&&r
712
MC/DC100%2/2
$fam == 6 and $is_v4
$fam == 6 $is_v4
Truth table100%3/3
A and B
A= $fam == 6B= $is_v4
ABresult
0X0
100
111
232 8 ▶ push( @addresses, $ip . ( $is_v4 ? '/32' : '/128' ) );
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 7 1 8
233 }
234  
235 # banned CIDR ranges live in the same IP set; the address part before the
236 # prefix determines the family and the CIDR is used as is
237 5 foreach my $cidr ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
238 1 my ($addr) = $cidr =~ m!\A(.+)/[0-9]{1,3}\z!;
239 1 ▶ my $is_v4 = ( defined($addr) && $addr =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $addr && $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ ? : 1 0 1
Condition33%1/3
defined $addr and $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $addr and $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
defined $addr $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
Truth table33%1/3
A and B
A= defined $addrB= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
ABresult
0X0
100
111
240 1 ▶ next if ( $fam == 4 && !$is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $fam == 4 and not $is_v4 0 1 1
Condition33%1/3
$fam == 4 and not $is_v4
!ll&&!rl&&r
010
MC/DC0%0/2
$fam == 4 and not $is_v4
$fam == 4 not $is_v4
Truth table33%1/3
A and not(B)
A= $fam == 4B= not $is_v4
ABresult
0X0
100
111
241 1 ▶ next if ( $fam == 6 && $is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $fam == 6 and $is_v4 0 1 1
Condition33%1/3
$fam == 6 and $is_v4
!ll&&!rl&&r
100
MC/DC0%0/2
$fam == 6 and $is_v4
$fam == 6 $is_v4
Truth table33%1/3
A and B
A= $fam == 6B= $is_v4
ABresult
0X0
100
111
242 1 push( @addresses, $cidr );
243 }
244  
245 5 return join( ' ', @addresses );
246 } ## end sub _render_family
247  
248  
249 # Internal helper. Returns the shared --scope/--name/--id (and --region)
250 # arguments identifying an IP set.
251 sub _set_suffix {
252 24 ▶ my ( $self, $name, $id ) = @_;
Subroutine _set_suffix: called CC=1 SCAR=0.0
253  
254 24 return ' --scope ' . $self->{options}{scope} . ' --name ' . $name . ' --id ' . $id . $self->_region_suffix;
255 }
256  
257 # Internal helper. Returns the get-ip-set command for an IP set.
258 sub _get_command {
259 19 ▶ my ( $self, $name, $id ) = @_;
Subroutine _get_command: called CC=1 SCAR=0.0
260  
261 19 return $self->{options}{aws_cmd} . ' wafv2 get-ip-set' . $self->_set_suffix( $name, $id );
262 }
263  
264 # Internal helper. Returns the update-ip-set command for an IP set with the
265 # given rendered addresses and lock token.
266 sub _update_command {
267 5 ▶ my ( $self, $name, $id, $addresses, $token ) = @_;
Subroutine _update_command: called CC=1 SCAR=0.0
268  
269 return
270 $self->{options}{aws_cmd}
271 5 . ' wafv2 update-ip-set'
272 . $self->_set_suffix( $name, $id )
273 . ' --addresses '
274 . $addresses
275 . ' --lock-token '
276 . $token;
277 } ## end sub _update_command
278  
279 # Internal helper. Applies the current banned state for one family by running a
280 # get-ip-set then an update-ip-set. In testing it appends the two commands (with
281 # a <lock-token> placeholder) to the passed accumulator; in real mode it runs the
282 # get, extracts the LockToken, and runs the update, raising the error flag on
283 # failure.
284 sub _apply_family {
285 5 ▶ my ( $self, $fam, $error_flag, $acc ) = @_;
Subroutine _apply_family: called CC=5 SCAR=26.3
286  
287 5 my ( $name, $id ) = $self->_conf_for_family($fam);
288 5 my $addresses = $self->_render_family($fam);
289 5 my $get = $self->_get_command( $name, $id );
290  
291 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
292 5 push( @{$acc}, $get, $self->_update_command( $name, $id, $addresses, '<lock-token>' ) );
293 5 return;
294 }
295  
296 0 my $get_out = `$get 2>&1`;
297 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
298 0 $self->{error} = $error_flag;
299 0 $self->{errorString} = 'command "' . $get . '" failed... ' . $get_out;
300 0 $self->warn;
301 0 return;
302 }
303  
304 0 my ($token) = $get_out =~ /"LockToken"\s*:\s*"([^"]+)"/;
305 0 ▶ $token = '' if ( !defined($token) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 0 0
306  
307 0 my $update = $self->_update_command( $name, $id, $addresses, $token );
308 0 my $update_out = `$update 2>&1`;
309 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
310 0 $self->{error} = $error_flag;
311 0 $self->{errorString} = 'command "' . $update . '" failed... ' . $update_out;
312 0 $self->warn;
313 }
314  
315 0 return;
316 } ## end sub _apply_family
317  
318 =head2 init
319  
320 Initiates the backend. Runs C<aws wafv2 get-ip-set> for each configured IP
321 set to verify it exists and is reachable, erroring if any of them fail.
322 Nothing is modified.
323  
324 Note that for bans to have an effect the Web ACL referencing the IP sets
325 must be associated with the resources to be protected. See L</DESCRIPTION>.
326  
327 =cut
328  
329 sub init {
330 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=6 SCAR=24.0
Pod documented
331  
332 2 $self->errorblank;
333  
334 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
335 0 $self->{error} = 18;
336 0 $self->{errorString} = 'backend has already been inited';
337 0 $self->warn;
338 }
339  
340 2 my @commands;
341 2 foreach my $fam ( $self->_configured_families ) {
342 3 my ( $name, $id ) = $self->_conf_for_family($fam);
343 3 push( @commands, $self->_get_command( $name, $id ) );
344 }
345  
346 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
347 2 $self->{frontend_obj}->{test_data} = \@commands;
348 } else {
349 0 foreach my $command (@commands) {
350 0 my $output = `$command 2>&1`;
351 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
352 0 $self->{error} = 12;
353 0 $self->{errorString} = 'init failed. command "' . $command . '" failed... ' . $output;
354 0 $self->warn;
355 }
356 }
357 } ## end else [ if ( $self->{testing} ) ]
358  
359 2 $self->{inited} = 1;
360 } ## end sub init
361  
362 =head2 ban
363  
364 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
365 and lowercased, then added to the ban list. The IP set for its family is
366 then updated via a C<get-ip-set> to fetch the current LockToken followed by
367 an C<update-ip-set> supplying the full banned list for that family, with
368 single IPs rendered as /32 or /128 CIDRs. Banning an IP whose family has no
369 IP set configured is an error. Banning an already banned IP is a noop.
370  
371 $fw_helper->ban( ban => $ip );
372  
373 =cut
374  
375 sub ban {
376 4 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=11 SCAR=32.9
Pod documented
377  
378 4 $self->errorblank;
379  
380 4 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 4 4
381 0 $self->{error} = 1;
382 0 $self->{errorString} = 'backend has not been inited';
383 0 $self->warn;
384 0 return;
385 }
386  
387 4 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 4 4
elsif (ref $opts{'ban'} ne '') { } 0 4 4
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/) { } 0 4 4
388 0 $self->{error} = 9;
389 0 $self->{errorString} = 'Nothing specified for the value ban';
390 0 $self->warn;
391 0 return;
392 } elsif ( ref( $opts{ban} ) ne '' ) {
393 0 $self->{error} = 10;
394 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
395 0 $self->warn;
396 0 return;
397 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
l!l&&r!l&&!r
220
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
ABresult
1X1
011
000
398 && $opts{ban} !~ /\A$IPv6_re\z/ )
399 {
400 0 $self->{error} = 10;
401 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
402 0 $self->warn;
403 0 return;
404 }
405  
406 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
407 4 $opts{ban} = lc( $opts{ban} );
408  
409 4 ▶ my $fam = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? 4 : 6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ ? : 2 2 4
410 4 ▶ if ( !$self->_family_configured($fam) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_family_configured($fam)) 1 3 4
411 1 $self->{error} = 30;
412 1 $self->{errorString} = 'no IPv' . $fam . ' IP set is configured, cannot ban "' . $opts{ban} . '"';
413 1 $self->warn;
414 0 return;
415 }
416  
417 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
418 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
419 0 $self->{frontend_obj}->{test_data} = 'already banned';
420 }
421 0 return;
422 }
423  
424 3 $self->{banned}{ $opts{ban} } = 1;
425  
426 3 my @commands;
427 3 $self->_apply_family( $fam, 13, \@commands );
428 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 3 0 3
429 3 $self->{frontend_obj}->{test_data} = \@commands;
430 }
431 } ## end sub ban
432  
433 =head2 unban
434  
435 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
436 and lowercased, then removed from the ban list. The IP set for its family is
437 then rewritten without it via a C<get-ip-set> plus C<update-ip-set> pair.
438 Unbanning an IP that is not banned is a noop.
439  
440 $fw_helper->unban( ban => $ip );
441  
442 =cut
443  
444 sub unban {
445 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=10 SCAR=47.0
Pod documented
446  
447 0 $self->errorblank;
448  
449 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
450 0 $self->{error} = 1;
451 0 $self->{errorString} = 'backend has not been inited';
452 0 $self->warn;
453 0 return;
454 }
455  
456 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/) { } 0 0 0
457 0 $self->{error} = 9;
458 0 $self->{errorString} = 'Nothing specified for the value ban';
459 0 $self->warn;
460 0 return;
461 } elsif ( ref( $opts{ban} ) ne '' ) {
462 0 $self->{error} = 10;
463 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
464 0 $self->warn;
465 0 return;
466 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/
ABresult
1X1
011
000
467 && $opts{ban} !~ /\A$IPv6_re\z/ )
468 {
469 0 $self->{error} = 10;
470 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
471 0 $self->warn;
472 0 return;
473 }
474  
475 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
476 0 $opts{ban} = lc( $opts{ban} );
477  
478 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
479 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
480 0 $self->{frontend_obj}->{test_data} = 'not banned';
481 }
482 0 return;
483 }
484  
485 0 ▶ my $fam = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? 4 : 6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ ? : 0 0 0
486 0 delete( $self->{banned}{ $opts{ban} } );
487  
488 0 my @commands;
489 0 $self->_apply_family( $fam, 14, \@commands );
490 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
491 0 $self->{frontend_obj}->{test_data} = \@commands;
492 }
493 } ## end sub unban
494  
495 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
496 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
497 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
498 # IPv6). Returns false otherwise.
499 sub _valid_cidr {
500 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
501  
502 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
503  
504 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
505 2 my ( $addr, $prefix ) = ( $1, $2 );
506 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
507 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
508 }
509  
510 0 return 0;
511 } ## end sub _valid_cidr
512  
513 =head2 ban_cidr
514  
515 Bans a CIDR range by adding it to the IP set for its family. WAFv2 IP sets
516 store CIDR ranges natively, so the range is used as is. The value of ban is
517 validated as being a IPv4 or IPv6 CIDR and lowercased. Banning a CIDR whose
518 family has no IP set configured is an error. Banning an already banned CIDR
519 is a noop.
520  
521 $fw_helper->ban_cidr( ban => '1.2.3.0/24' );
522  
523 =cut
524  
525 sub ban_cidr {
526 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=11 SCAR=36.3
Pod documented
527  
528 1 $self->errorblank;
529  
530 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
531 0 $self->{error} = 1;
532 0 $self->{errorString} = 'backend has not been inited';
533 0 $self->warn;
534 0 return;
535 }
536  
537 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
538 0 $self->{error} = 9;
539 0 $self->{errorString} = 'Nothing specified for the value ban';
540 0 $self->warn;
541 0 return;
542 } elsif ( ref( $opts{ban} ) ne '' ) {
543 0 $self->{error} = 33;
544 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
545 0 $self->warn;
546 0 return;
547 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
548 0 $self->{error} = 33;
549 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
550 0 $self->warn;
551 0 return;
552 }
553  
554 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
555 1 $opts{ban} = lc( $opts{ban} );
556  
557 1 my ($addr) = $opts{ban} =~ m!\A(.+)/[0-9]{1,3}\z!;
558 1 ▶ my $fam = ( defined($addr) && $addr =~ /\A$IPv4_re\z/ ) ? 4 : 6;
Branch: 1 branch
BranchTrueFalseTotal
defined $addr && $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ ? : 1 0 1
Condition33%1/3
defined $addr and $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $addr and $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
defined $addr $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
Truth table33%1/3
A and B
A= defined $addrB= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
ABresult
0X0
100
111
559 1 ▶ if ( !$self->_family_configured($fam) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_family_configured($fam)) 0 1 1
560 0 $self->{error} = 30;
561 0 $self->{errorString} = 'no IPv' . $fam . ' IP set is configured, cannot ban "' . $opts{ban} . '"';
562 0 $self->warn;
563 0 return;
564 }
565  
566 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
567 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
568 0 $self->{frontend_obj}->{test_data} = 'already banned';
569 }
570 0 return;
571 }
572  
573 1 $self->{banned_cidr}{ $opts{ban} } = 1;
574  
575 1 my @commands;
576 1 $self->_apply_family( $fam, 31, \@commands );
577 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
578 1 $self->{frontend_obj}->{test_data} = \@commands;
579 }
580 } ## end sub ban_cidr
581  
582 =head2 unban_cidr
583  
584 Unbans a CIDR range by removing it from the IP set for its family, rewriting
585 the set via a C<get-ip-set> plus C<update-ip-set> pair. The value of ban is
586 validated as being a IPv4 or IPv6 CIDR and lowercased. Unbanning a CIDR that
587 is not banned is a noop.
588  
589 $fw_helper->unban_cidr( ban => '1.2.3.0/24' );
590  
591 =cut
592  
593 sub unban_cidr {
594 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=10 SCAR=34.1
Pod documented
595  
596 1 $self->errorblank;
597  
598 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
599 0 $self->{error} = 1;
600 0 $self->{errorString} = 'backend has not been inited';
601 0 $self->warn;
602 0 return;
603 }
604  
605 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
606 0 $self->{error} = 9;
607 0 $self->{errorString} = 'Nothing specified for the value ban';
608 0 $self->warn;
609 0 return;
610 } elsif ( ref( $opts{ban} ) ne '' ) {
611 0 $self->{error} = 33;
612 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
613 0 $self->warn;
614 0 return;
615 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
616 0 $self->{error} = 33;
617 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
618 0 $self->warn;
619 0 return;
620 }
621  
622 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
623 1 $opts{ban} = lc( $opts{ban} );
624  
625 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
626 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
627 0 $self->{frontend_obj}->{test_data} = 'not banned';
628 }
629 0 return;
630 }
631  
632 1 my ($addr) = $opts{ban} =~ m!\A(.+)/[0-9]{1,3}\z!;
633 1 ▶ my $fam = ( defined($addr) && $addr =~ /\A$IPv4_re\z/ ) ? 4 : 6;
Branch: 1 branch
BranchTrueFalseTotal
defined $addr && $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/ ? : 1 0 1
Condition33%1/3
defined $addr and $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $addr and $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
defined $addr $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
Truth table33%1/3
A and B
A= defined $addrB= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::aws_wafv2::IPv4_re\z/
ABresult
0X0
100
111
634 1 delete( $self->{banned_cidr}{ $opts{ban} } );
635  
636 1 my @commands;
637 1 $self->_apply_family( $fam, 32, \@commands );
638 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
639 1 $self->{frontend_obj}->{test_data} = \@commands;
640 }
641 } ## end sub unban_cidr
642  
643 =head2 list_cidr
644  
645 List banned CIDR ranges. Returns an array of the currently banned CIDR
646 ranges from internal state; the IP sets are not queried.
647  
648 my @banned_cidrs = $fw_helper->list_cidr;
649  
650 =cut
651  
652 sub list_cidr {
653 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
654  
655 2 $self->errorblank;
656  
657 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
658 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
659 }
660  
661 2 return keys( %{ $self->{banned_cidr} } );
662 } ## end sub list_cidr
663  
664 =head2 list
665  
666 List banned IPs. Returns an array of the currently banned single IPs from
667 internal state; the IP sets are not queried. CIDR ranges are not included;
668 for those see L</list_cidr>.
669  
670 my @banned = $fw_helper->list;
671  
672 =cut
673  
674 sub list {
675 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
676  
677 0 $self->errorblank;
678  
679 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
680 0 $self->{frontend_obj}->{test_data} = 'list';
681 }
682  
683 0 return keys( %{ $self->{banned} } );
684 } ## end sub list
685  
686 =head2 re_init
687  
688 Tears down and re-initiates, then re-applies the full retained ban list,
689 both single IPs and CIDR ranges, to each configured IP set via
690 C<get-ip-set> plus C<update-ip-set>.
691  
692 $fw_helper->re_init;
693  
694 =cut
695  
696 sub re_init {
697 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=4 SCAR=30.0
Pod documented
698  
699 0 $self->errorblank;
700  
701 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
702 0 $self->{error} = 1;
703 0 $self->{errorString} = 'backend has not been inited';
704 0 $self->warn;
705 0 return;
706 }
707  
708 {
709 0 local $@;
710 0 eval { $self->teardown; };
711 }
712 0 $self->init;
713  
714 0 my @commands;
715 0 foreach my $fam ( $self->_configured_families ) {
716 0 $self->_apply_family( $fam, 13, \@commands );
717 }
718 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
719 0 $self->{frontend_obj}->{test_data} = \@commands;
720 }
721  
722 0 $self->{inited} = 1;
723 } ## end sub re_init
724  
725 =head2 teardown
726  
727 Empties each configured IP set by pushing an empty address list via
728 C<update-ip-set>. The internal ban list is kept so a following re_init
729 restores it. The IP sets themselves are not deleted.
730  
731 $fw_helper->teardown;
732  
733 =cut
734  
735 sub teardown {
736 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=3 SCAR=24.8
Pod documented
737  
738 0 $self->errorblank;
739  
740 0 $self->{inited} = 0;
741  
742 # render updates against an empty ban set without disturbing the retained
743 # lists; both single IPs and CIDR ranges live in the same IP sets
744 0 my %saved = %{ $self->{banned} };
745 0 my %saved_cidr = %{ $self->{banned_cidr} };
746 0 $self->{banned} = {};
747 0 $self->{banned_cidr} = {};
748  
749 0 my @commands;
750 0 foreach my $fam ( $self->_configured_families ) {
751 0 $self->_apply_family( $fam, 17, \@commands );
752 }
753  
754 0 $self->{banned} = \%saved;
755 0 $self->{banned_cidr} = \%saved_cidr;
756  
757 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
758 0 $self->{frontend_obj}->{test_data} = \@commands;
759 }
760 } ## end sub teardown
761  
762 =head2 stop
763  
764 Alias for L</teardown>.
765  
766 $fw_helper->stop;
767  
768 =cut
769  
770 sub stop {
771 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
772  
773 0 return $self->teardown(%opts);
774 }
775  
776 =head2 check
777  
778 Verifies each configured IP set is still fetchable by running
779 C<aws wafv2 get-ip-set> against it. Returns 1 if all succeed and 0 if any
780 fail.
781  
782 $result=$fw_helper->check;
783  
784 =cut
785  
786 sub check {
787 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=5 SCAR=19.1
Pod documented
788  
789 6 $self->errorblank;
790  
791 6 my @commands;
792 6 foreach my $fam ( $self->_configured_families ) {
793 11 my ( $name, $id ) = $self->_conf_for_family($fam);
794 11 push( @commands, $self->_get_command( $name, $id ) );
795 }
796  
797 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
798 6 $self->{frontend_obj}->{test_data} = \@commands;
799 6 return 1;
800 }
801  
802 0 foreach my $command (@commands) {
803 0 my $output = `$command 2>&1`;
804 0 ▶ return 0 if ( $? != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 0 0
805 }
806  
807 0 return 1;
808 } ## end sub check
809  
810 =head2 flush
811  
812 Removes all bans at once by clearing the internal ban lists, both single
813 IPs and CIDR ranges, and pushing the now empty address list to each
814 configured IP set via C<update-ip-set>.
815  
816 $fw_helper->flush;
817  
818 =cut
819  
820 sub flush {
821 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=4 SCAR=30.0
Pod documented
822  
823 0 $self->errorblank;
824  
825 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
826 0 $self->{error} = 1;
827 0 $self->{errorString} = 'backend has not been inited';
828 0 $self->warn;
829 0 return;
830 }
831  
832 # the IP sets are emptied at once, dropping both single IPs and CIDR ranges
833 0 $self->{banned} = {};
834 0 $self->{banned_cidr} = {};
835  
836 0 my @commands;
837 0 foreach my $fam ( $self->_configured_families ) {
838 0 $self->_apply_family( $fam, 25, \@commands );
839 }
840 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
841 0 $self->{frontend_obj}->{test_data} = \@commands;
842 }
843 } ## end sub flush
844  
845 =head1 ERROR CODES / FLAGS
846  
847 Error handling is provided by L<Error::Helper>. All
848 errors are considered fatal.
849  
850 =head2 1, notInited
851  
852 The backend has not been inited yet.
853  
854 =head2 8, optionsNotHash
855  
856 The item passed to new for options is not a hash.
857  
858 =head2 9, noBanItem
859  
860 No IP or CIDR range specified to ban or unban.
861  
862 =head2 10, banItemNotIP
863  
864 The item to ban is not an IP. Either wrong ref type or regexp
865 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
866  
867 =head2 12, backendInitError
868  
869 Failed to init the backend. A get-ip-set for a configured IP set failed.
870  
871 =head2 13, banFailed
872  
873 Failed to ban the item.
874  
875 =head2 14, unbanFailed
876  
877 Failed to unban the item.
878  
879 =head2 15, listFailed
880  
881 Failed to get a list of bans.
882  
883 =head2 16, reInitFailed
884  
885 Failed to re_init the backend.
886  
887 =head2 17, teardownFailed
888  
889 Failed to teardown the backend.
890  
891 =head2 18, alreadyInited
892  
893 init called, but the backend has already been inited.
894  
895 =head2 20, scopeInvalid
896  
897 The scope option is not either "REGIONAL" or "CLOUDFRONT".
898  
899 =head2 24, checkFailed
900  
901 The backend check raised an error.
902  
903 =head2 25, flushFailed
904  
905 Failed to flush the bans.
906  
907 =head2 30, ipsetNotConfigured
908  
909 No IP set is configured for the family, IPv4 or IPv6, of the item.
910  
911 =head2 31, banCidrFailed
912  
913 Failed to ban the CIDR range.
914  
915 =head2 32, unbanCidrFailed
916  
917 Failed to unban the CIDR range.
918  
919 =head2 33, cidrItemNotCidr
920  
921 The item to ban is not a CIDR range. Either wrong ref type or it is not an
922 IPv4 or IPv6 address followed by a prefix length valid for its family.
923  
924 =head2 34, cidrNotSupported
925  
926 The backend does not support CIDR bans.
927  
928 =head2 35, listCidrFailed
929  
930 Failed to get a list of CIDR bans.
931  
932 =head1 AUTHOR
933  
934 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
935  
936 =head1 LICENSE AND COPYRIGHT
937  
938 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
939  
940 This is free software, licensed under:
941  
942 The GNU Lesser General Public License, Version 2.1, February 1999
943  
944 =cut
945  
946 1 1; # End of Net::Firewall::BlockerHelper::backends::aws_wafv2
« BlockerHelper/backends/linux_ip_route.pm ↑ Summary BlockerHelper/backends/xdp.pm »