BlockerHelper/backends/aws_wafv2.pm

filter: Statementstmt 46.0% 151 / 328 Branchbran 42.7% 59 / 138 Conditioncond 51.1% 23 / 45 MC/DCmcdc 21.4% 6 / 28 Subroutinesub 78.5% 22 / 28 Podpod 100% 13 / 13 totaltotal 47.2% 274 / 580 CC 94 SCAR 31.8
CC
94
Cov
46%
unban
110.0
ban_cidr
37.6
unban_cidr
30.4
CRAP 24.1
« BlockerHelper/backends/opnsense.pm ↑ Summary BlockerHelper/backends/linux_ip_route.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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
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.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.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 3 ▶sub new {
Subroutine new: called CC=11 SCAR=26.3
Pod documented
96 3 my ( $blank, %opts ) = @_;
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 --region argument to append to every aws
182 # invocation, or nothing when no region is configured.
183 #
184 # Emitting the flag only when the option is set is deliberate rather than
185 # defaulting to a region here. With it left off, the aws CLI falls back to its
186 # own resolution order, the AWS_REGION environment variable and then the
187 # profile's configured region, which is what an instance running inside an
188 # already configured environment wants. Hardcoding a default would silently
189 # override that.
190 #
191 # Note the CLOUDFRONT scope is only served out of us-east-1, so an instance
192 # using that scope needs either the region option or an environment already
193 # pointing there.
194 #
195 # Takes no arguments.
196 #
197 # Returns the argument as a string with a leading space, ready to be
198 # concatenated onto a command, or the empty string when the region option is
199 # unset or empty. Never returns undef.
200 #
201 # # with the region option set to us-west-2
202 # $self->_region_suffix; # ' --region us-west-2'
203 #
204 # # with no region configured
205 # $self->_region_suffix; # ''
206 24 ▶sub _region_suffix {
Subroutine _region_suffix: called CC=3 SCAR=13.3
207 24 my ($self) = @_;
208  
209 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
210 0 return ' --region ' . $self->{options}{region};
211 }
212 24 return '';
213 }
214  
215  
216 # Internal helper. Returns the configured WAFv2 IP set name and id for one
217 # address family.
218 #
219 # A WAFv2 IP set is single family, created as either IPV4 or IPV6, so blocking
220 # both means two separate sets on the AWS side. Both a name and an id are
221 # needed to address one: the aws CLI requires both on every call, as the name
222 # alone is not unique.
223 #
224 # Either family may be left unconfigured, which is how an instance is told to
225 # handle only IPv4 or only IPv6. That is why this returns whatever is there
226 # rather than erroring, and why callers go through _family_configured before
227 # using the result.
228 #
229 # Args:
230 #
231 # fam - The address family, as the number 4 or 6. Anything other than 4 is
232 # treated as 6, so this is really a two way branch rather than a
233 # validated argument.
234 #
235 # Returns a two element list of the name and the id, in that order. Either or
236 # both may be undef, or set but empty, when that family is not configured.
237 #
238 # my ( $name, $id ) = $self->_conf_for_family(4);
239 # # the name4 and id4 options
240 #
241 # my ( $name, $id ) = $self->_conf_for_family(6);
242 # # the name6 and id6 options, both undef if v6 is not configured
243 40 ▶sub _conf_for_family {
Subroutine _conf_for_family: called CC=2 SCAR=6.9
244 40 my ( $self, $fam ) = @_;
245  
246 40 ▶ if ( $fam == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam == 4) 23 17 40
247 23 return ( $self->{options}{name4}, $self->{options}{id4} );
248 }
249 17 return ( $self->{options}{name6}, $self->{options}{id6} );
250 }
251  
252 # Internal helper. Says whether one address family has a usable IP set
253 # configured, meaning both a name and an id are present and non empty.
254 #
255 # Both are required because the aws CLI needs both to address a set; having
256 # only one is as useless as having neither and is treated the same way. This
257 # is the guard every code path goes through before touching a family, so a
258 # half configured family is skipped rather than producing a command with an
259 # empty --name or --id that AWS would reject.
260 #
261 # Args:
262 #
263 # fam - The address family, as the number 4 or 6. Anything other than 4 is
264 # treated as 6.
265 #
266 # Returns 1 when that family has both a name and an id set to something non
267 # empty, 0 otherwise. Always one of those two values, never undef.
268 #
269 # # with name4 and id4 both set
270 # $self->_family_configured(4); # 1
271 #
272 # # with neither name6 nor id6 set
273 # $self->_family_configured(6); # 0
274 #
275 # # with name6 set but id6 missing: still not usable
276 # $self->_family_configured(6); # 0
277 21 ▶sub _family_configured {
Subroutine _family_configured: called CC=5 SCAR=16.6
278 21 my ( $self, $fam ) = @_;
279  
280 21 my ( $name, $id ) = $self->_conf_for_family($fam);
281 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
282 }
283  
284 # Internal helper. Returns which address families this instance actually has
285 # IP sets for, which is what every operation loops over.
286 #
287 # Because a WAFv2 IP set is single family and either family may be left
288 # unconfigured, there is no fixed set of things to act on: an instance may
289 # manage one set or two. Rather than have init, ban, unban, teardown, and the
290 # rest each work that out, they all iterate over this.
291 #
292 # Takes no arguments.
293 #
294 # Returns the families as a list of the numbers 4 and 6, in that order,
295 # containing only those that are fully configured. May be a single element,
296 # and may be empty if neither family is configured, though new rejects that
297 # case so it should not arise in practice.
298 #
299 # # with both families configured
300 # my @families = $self->_configured_families; # ( 4, 6 )
301 #
302 # # with only IPv4 configured
303 # my @families = $self->_configured_families; # ( 4 )
304 #
305 # # the usual shape at the call sites
306 # foreach my $fam ( $self->_configured_families ) {
307 # $self->_apply_family( $fam, 13, \@commands );
308 # }
309 8 ▶sub _configured_families {
Subroutine _configured_families: called CC=3 SCAR=11.0
310 8 my ($self) = @_;
311  
312 8 my @families;
313 8 ▶ push( @families, 4 ) if ( $self->_family_configured(4) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_family_configured(4) 8 0 8
314 8 ▶ push( @families, 6 ) if ( $self->_family_configured(6) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_family_configured(6) 6 2 8
315 8 return @families;
316 }
317  
318 # Internal helper. Renders the current bans belonging to one address family
319 # into the address list that update-ip-set takes.
320 #
321 # WAFv2 IP sets are replace-in-full rather than incremental: there is no add
322 # or remove one address call, so every ban and unban rewrites the entire set
323 # from the internal state. That is what this produces.
324 #
325 # Everything is emitted as CIDR, since that is the only form WAFv2 accepts. A
326 # single address therefore gets an explicit host prefix appended, /32 or /128
327 # by family. Banned ranges already carry a prefix and go out as they are.
328 #
329 # Single addresses and ranges are pulled from two separate internal lists but
330 # land in the same set, since AWS does not distinguish them. The two are
331 # emitted as two sorted runs rather than one merged sorted list, so the
332 # addresses come first and the ranges after. That is stable, which is what
333 # matters for the test_data comparisons, just not globally sorted.
334 #
335 # The family of a range is decided from the address portion before the prefix,
336 # because a range always holds a "/" that would never match the IPv4 regexp.
337 #
338 # Args:
339 #
340 # fam - The address family to render, as the number 4 or 6. Entries of the
341 # other family are skipped. Anything other than 4 behaves as 6.
342 #
343 # Returns the addresses as a single space joined string, ready to follow
344 # --addresses on the command line. Returns the empty string when nothing of
345 # that family is banned, which is the correct way to empty a WAFv2 IP set and
346 # is what teardown and flush rely on.
347 #
348 # # with 10.0.0.1 and 10.0.0.0/8 banned
349 # $self->_render_family(4); # '10.0.0.1/32 10.0.0.0/8'
350 #
351 # # the same instance, asked for v6
352 # $self->_render_family(6); # ''
353 #
354 # # with 2001:db8::1 banned
355 # $self->_render_family(6); # '2001:db8::1/128'
356 5 ▶sub _render_family {
Subroutine _render_family: called CC=15 SCAR=28.2
357 5 my ( $self, $fam ) = @_;
358  
359 5 my @addresses;
360 5 foreach my $ip ( sort( keys( %{ $self->{banned} } ) ) ) {
361 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
362 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
363 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
364 8 ▶ push( @addresses, $ip . ( $is_v4 ? '/32' : '/128' ) );
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 7 1 8
365 }
366  
367 # banned CIDR ranges live in the same IP set; the address part before the
368 # prefix determines the family and the CIDR is used as is
369 5 foreach my $cidr ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
370 1 my ($addr) = $cidr =~ m!\A(.+)/[0-9]{1,3}\z!;
371 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
372 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
373 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
374 1 push( @addresses, $cidr );
375 }
376  
377 5 return join( ' ', @addresses );
378 } ## end sub _render_family
379  
380  
381 # Internal helper. Returns the arguments that identify one WAFv2 IP set,
382 # shared by every command that addresses one.
383 #
384 # The aws CLI needs all three of scope, name, and id to locate an IP set on
385 # each call, plus the region when one is configured. Since both get-ip-set and
386 # update-ip-set take exactly the same identifying arguments, they are built
387 # once here so the two cannot drift and end up addressing different sets.
388 #
389 # Args:
390 #
391 # name - The IP set's name, as a plain string, from the name4 or name6
392 # option. Expected to be non empty; callers go through
393 # _family_configured first.
394 #
395 # id - The IP set's id, as a plain string, from the id4 or id6 option.
396 # This is the AWS assigned identifier, not something derived from
397 # the name. Also expected to be non empty.
398 #
399 # Returns the arguments as a single string with a leading space, ready to be
400 # concatenated onto a command. The scope comes from the scope option, which is
401 # 'REGIONAL' by default and may also be 'CLOUDFRONT'.
402 #
403 # $self->_set_suffix( 'blocklist-v4', 'a1b2c3d4-...' );
404 # # ' --scope REGIONAL --name blocklist-v4 --id a1b2c3d4-...'
405 #
406 # # with the region option set, it is appended
407 # # ' --scope REGIONAL --name blocklist-v4 --id a1b2c3d4-... --region us-west-2'
408 24 ▶sub _set_suffix {
Subroutine _set_suffix: called CC=1 SCAR=0.0
409 24 my ( $self, $name, $id ) = @_;
410  
411 24 return ' --scope ' . $self->{options}{scope} . ' --name ' . $name . ' --id ' . $id . $self->_region_suffix;
412 }
413  
414 # Internal helper. Returns the get-ip-set command for one IP set.
415 #
416 # This is used for two different things. init runs it to confirm each
417 # configured set actually exists and is reachable before declaring the backend
418 # ready, and _apply_family runs it immediately before every write to pick up
419 # the current lock token, which update-ip-set will not proceed without.
420 #
421 # Args:
422 #
423 # name - The IP set's name, as a plain string, from the name4 or name6
424 # option.
425 #
426 # id - The IP set's AWS assigned id, as a plain string, from the id4 or
427 # id6 option.
428 #
429 # Returns the command as a single string ready to hand to the runner. The aws
430 # binary comes from the aws_cmd option, 'aws' by default.
431 #
432 # $self->_get_command( 'blocklist-v4', 'a1b2c3d4-...' );
433 # # aws wafv2 get-ip-set --scope REGIONAL --name blocklist-v4 --id a1b2c3d4-...
434 19 ▶sub _get_command {
Subroutine _get_command: called CC=1 SCAR=0.0
435 19 my ( $self, $name, $id ) = @_;
436  
437 19 return $self->{options}{aws_cmd} . ' wafv2 get-ip-set' . $self->_set_suffix( $name, $id );
438 }
439  
440 # Internal helper. Returns the update-ip-set command that writes a rendered
441 # address list into one IP set.
442 #
443 # WAFv2 has no incremental add or remove, so this always replaces the set's
444 # entire contents. The lock token is AWS's optimistic concurrency control: it
445 # comes back from get-ip-set, must be passed on the write, and is rejected if
446 # anything else has modified the set in between. That is what stops two
447 # processes from each writing back a list based on a stale read.
448 #
449 # Args:
450 #
451 # name - The IP set's name, as a plain string, from the name4 or
452 # name6 option.
453 #
454 # id - The IP set's AWS assigned id, as a plain string.
455 #
456 # addresses - The complete new contents of the set, as the space joined
457 # CIDR string produced by _render_family. May be the empty
458 # string, which empties the set.
459 #
460 # token - The lock token from the immediately preceding get-ip-set, as
461 # a plain string. In testing mode callers pass the literal
462 # placeholder '<lock-token>' instead, since no get is run.
463 #
464 # Returns the command as a single string ready to hand to the runner.
465 #
466 # $self->_update_command( 'blocklist-v4', 'a1b2c3d4-...',
467 # '10.0.0.1/32 10.0.0.0/8', 'abc123' );
468 # # aws wafv2 update-ip-set --scope REGIONAL --name blocklist-v4 --id a1b2c3d4-... --addresses 10.0.0.1/32 10.0.0.0/8 --lock-token abc123
469 #
470 # # emptying the set, as teardown and flush do
471 # $self->_update_command( 'blocklist-v4', 'a1b2c3d4-...', '', 'abc123' );
472 # # aws wafv2 update-ip-set --scope REGIONAL --name blocklist-v4 --id a1b2c3d4-... --addresses --lock-token abc123
473 5 ▶sub _update_command {
Subroutine _update_command: called CC=1 SCAR=0.0
474 5 my ( $self, $name, $id, $addresses, $token ) = @_;
475  
476 return
477 $self->{options}{aws_cmd}
478 5 . ' wafv2 update-ip-set'
479 . $self->_set_suffix( $name, $id )
480 . ' --addresses '
481 . $addresses
482 . ' --lock-token '
483 . $token;
484 } ## end sub _update_command
485  
486 # Internal helper. Pushes the current bans for one address family out to its
487 # WAFv2 IP set. This is the only place the AWS side is written, so ban, unban,
488 # ban_cidr, unban_cidr, re_init, teardown, and flush all funnel through it,
489 # once per configured family.
490 #
491 # Because WAFv2 replaces the set in full and guards writes with a lock token,
492 # every write is unavoidably two round trips: a get-ip-set to obtain the
493 # current token, then an update-ip-set carrying it. They have to stay paired,
494 # which is why this is one helper rather than the callers sequencing them.
495 #
496 # The token is scraped out of the get's JSON with a regexp rather than by
497 # decoding it. A token that cannot be found falls back to the empty string,
498 # which AWS will reject, so a parse failure surfaces as the update failing
499 # rather than as a silent write of the wrong thing.
500 #
501 # Nothing here dies. A failure of either command sets the error to the passed
502 # code, sets errorString with the command and its output, and warns.
503 #
504 # Args:
505 #
506 # fam - The address family to apply, as the number 4 or 6. Expected
507 # to be one that _configured_families returned.
508 #
509 # error_flag - The numeric error code to raise on failure, so the error
510 # reads as the operation that triggered the write. Callers
511 # pass the code matching themselves: 13 from ban and re_init,
512 # 14 from unban, 31 and 32 from the CIDR paths, 17 from
513 # teardown, and 25 from flush.
514 #
515 # acc - An arrayref used only in testing mode, to which the two
516 # commands that would have been run are appended. Callers
517 # pass one they later hand to test_data. Ignored entirely
518 # outside testing mode, but still expected to be a valid
519 # arrayref.
520 #
521 # Returns nothing, on success and on failure alike. The caller checks the
522 # error state rather than a return value.
523 #
524 # # from ban, so a failure reads as banFailed
525 # my @commands;
526 # foreach my $fam ( $self->_configured_families ) {
527 # $self->_apply_family( $fam, 13, \@commands );
528 # }
529 #
530 # In testing mode nothing is run. The two commands are appended to the
531 # accumulator with the token replaced by the literal placeholder
532 # '<lock-token>', since there is no get to take a real one from:
533 #
534 # aws wafv2 get-ip-set --scope REGIONAL --name blocklist-v4 --id a1b2c3d4-...
535 # aws wafv2 update-ip-set --scope REGIONAL --name blocklist-v4 --id a1b2c3d4-... --addresses 10.0.0.1/32 --lock-token <lock-token>
536 5 ▶sub _apply_family {
Subroutine _apply_family: called CC=5 SCAR=26.3
537 5 my ( $self, $fam, $error_flag, $acc ) = @_;
538  
539 5 my ( $name, $id ) = $self->_conf_for_family($fam);
540 5 my $addresses = $self->_render_family($fam);
541 5 my $get = $self->_get_command( $name, $id );
542  
543 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
544 5 push( @{$acc}, $get, $self->_update_command( $name, $id, $addresses, '<lock-token>' ) );
545 5 return;
546 }
547  
548 0 my $get_out = `$get 2>&1`;
549 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
550 0 $self->{error} = $error_flag;
551 0 $self->{errorString} = 'command "' . $get . '" failed... ' . $get_out;
552 0 $self->warn;
553 0 return;
554 }
555  
556 0 my ($token) = $get_out =~ /"LockToken"\s*:\s*"([^"]+)"/;
557 0 ▶ $token = '' if ( !defined($token) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 0 0
558  
559 0 my $update = $self->_update_command( $name, $id, $addresses, $token );
560 0 my $update_out = `$update 2>&1`;
561 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
562 0 $self->{error} = $error_flag;
563 0 $self->{errorString} = 'command "' . $update . '" failed... ' . $update_out;
564 0 $self->warn;
565 }
566  
567 0 return;
568 } ## end sub _apply_family
569  
570 =head2 init
571  
572 Initiates the backend. Runs C<aws wafv2 get-ip-set> for each configured IP
573 set to verify it exists and is reachable, erroring if any of them fail.
574 Nothing is modified.
575  
576 Note that for bans to have an effect the Web ACL referencing the IP sets
577 must be associated with the resources to be protected. See L</DESCRIPTION>.
578  
579 =cut
580  
581 2 ▶sub init {
Subroutine init: called CC=6 SCAR=24.0
Pod documented
582 2 my ( $self, %opts ) = @_;
583  
584 2 $self->errorblank;
585  
586 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
587 0 $self->{error} = 18;
588 0 $self->{errorString} = 'backend has already been inited';
589 0 $self->warn;
590 }
591  
592 2 my @commands;
593 2 foreach my $fam ( $self->_configured_families ) {
594 3 my ( $name, $id ) = $self->_conf_for_family($fam);
595 3 push( @commands, $self->_get_command( $name, $id ) );
596 }
597  
598 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
599 2 $self->{frontend_obj}->{test_data} = \@commands;
600 } else {
601 0 foreach my $command (@commands) {
602 0 my $output = `$command 2>&1`;
603 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
604 0 $self->{error} = 12;
605 0 $self->{errorString} = 'init failed. command "' . $command . '" failed... ' . $output;
606 0 $self->warn;
607 }
608 }
609 } ## end else [ if ( $self->{testing} ) ]
610  
611 2 $self->{inited} = 1;
612 } ## end sub init
613  
614 =head2 ban
615  
616 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
617 and lowercased, then added to the ban list. The IP set for its family is
618 then updated via a C<get-ip-set> to fetch the current LockToken followed by
619 an C<update-ip-set> supplying the full banned list for that family, with
620 single IPs rendered as /32 or /128 CIDRs. Banning an IP whose family has no
621 IP set configured is an error. Banning an already banned IP is a noop.
622  
623 $fw_helper->ban( ban => $ip );
624  
625 =cut
626  
627 4 ▶sub ban {
Subroutine ban: called CC=11 SCAR=32.9
Pod documented
628 4 my ( $self, %opts ) = @_;
629  
630 4 $self->errorblank;
631  
632 4 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 4 4
633 0 $self->{error} = 1;
634 0 $self->{errorString} = 'backend has not been inited';
635 0 $self->warn;
636 0 return;
637 }
638  
639 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
640 0 $self->{error} = 9;
641 0 $self->{errorString} = 'Nothing specified for the value ban';
642 0 $self->warn;
643 0 return;
644 } elsif ( ref( $opts{ban} ) ne '' ) {
645 0 $self->{error} = 10;
646 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
647 0 $self->warn;
648 0 return;
649 ▶ } 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
650 && $opts{ban} !~ /\A$IPv6_re\z/ )
651 {
652 0 $self->{error} = 10;
653 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
654 0 $self->warn;
655 0 return;
656 }
657  
658 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
659 4 $opts{ban} = lc( $opts{ban} );
660  
661 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
662 4 ▶ if ( !$self->_family_configured($fam) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_family_configured($fam)) 1 3 4
663 1 $self->{error} = 30;
664 1 $self->{errorString} = 'no IPv' . $fam . ' IP set is configured, cannot ban "' . $opts{ban} . '"';
665 1 $self->warn;
666 0 return;
667 }
668  
669 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
670 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
671 0 $self->{frontend_obj}->{test_data} = 'already banned';
672 }
673 0 return;
674 }
675  
676 3 $self->{banned}{ $opts{ban} } = 1;
677  
678 3 my @commands;
679 3 $self->_apply_family( $fam, 13, \@commands );
680 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 3 0 3
681 3 $self->{frontend_obj}->{test_data} = \@commands;
682 }
683 } ## end sub ban
684  
685 =head2 unban
686  
687 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
688 and lowercased, then removed from the ban list. The IP set for its family is
689 then rewritten without it via a C<get-ip-set> plus C<update-ip-set> pair.
690 Unbanning an IP that is not banned is a noop.
691  
692 $fw_helper->unban( ban => $ip );
693  
694 =cut
695  
696 0 ▶sub unban {
Subroutine unban: not called CC=10 SCAR=47.0
Pod documented
697 0 my ( $self, %opts ) = @_;
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 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
709 0 $self->{error} = 9;
710 0 $self->{errorString} = 'Nothing specified for the value ban';
711 0 $self->warn;
712 0 return;
713 } elsif ( ref( $opts{ban} ) ne '' ) {
714 0 $self->{error} = 10;
715 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
716 0 $self->warn;
717 0 return;
718 ▶ } 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
719 && $opts{ban} !~ /\A$IPv6_re\z/ )
720 {
721 0 $self->{error} = 10;
722 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
723 0 $self->warn;
724 0 return;
725 }
726  
727 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
728 0 $opts{ban} = lc( $opts{ban} );
729  
730 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
731 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
732 0 $self->{frontend_obj}->{test_data} = 'not banned';
733 }
734 0 return;
735 }
736  
737 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
738 0 delete( $self->{banned}{ $opts{ban} } );
739  
740 0 my @commands;
741 0 $self->_apply_family( $fam, 14, \@commands );
742 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
743 0 $self->{frontend_obj}->{test_data} = \@commands;
744 }
745 } ## end sub unban
746  
747 =head2 ban_cidr
748  
749 Bans a CIDR range by adding it to the IP set for its family. WAFv2 IP sets
750 store CIDR ranges natively, so the range is used as is. The value of ban is
751 validated as being a IPv4 or IPv6 CIDR and lowercased. Banning a CIDR whose
752 family has no IP set configured is an error. Banning an already banned CIDR
753 is a noop.
754  
755 $fw_helper->ban_cidr( ban => '1.2.3.0/24' );
756  
757 =cut
758  
759 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=11 SCAR=36.3
Pod documented
760 1 my ( $self, %opts ) = @_;
761  
762 1 $self->errorblank;
763  
764 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
765 0 $self->{error} = 1;
766 0 $self->{errorString} = 'backend has not been inited';
767 0 $self->warn;
768 0 return;
769 }
770  
771 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
772 0 $self->{error} = 9;
773 0 $self->{errorString} = 'Nothing specified for the value ban';
774 0 $self->warn;
775 0 return;
776 } elsif ( ref( $opts{ban} ) ne '' ) {
777 0 $self->{error} = 33;
778 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
779 0 $self->warn;
780 0 return;
781 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
782 0 $self->{error} = 33;
783 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
784 0 $self->warn;
785 0 return;
786 }
787  
788 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
789 1 $opts{ban} = lc( $opts{ban} );
790  
791 1 my ($addr) = $opts{ban} =~ m!\A(.+)/[0-9]{1,3}\z!;
792 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
793 1 ▶ if ( !$self->_family_configured($fam) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_family_configured($fam)) 0 1 1
794 0 $self->{error} = 30;
795 0 $self->{errorString} = 'no IPv' . $fam . ' IP set is configured, cannot ban "' . $opts{ban} . '"';
796 0 $self->warn;
797 0 return;
798 }
799  
800 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
801 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
802 0 $self->{frontend_obj}->{test_data} = 'already banned';
803 }
804 0 return;
805 }
806  
807 1 $self->{banned_cidr}{ $opts{ban} } = 1;
808  
809 1 my @commands;
810 1 $self->_apply_family( $fam, 31, \@commands );
811 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
812 1 $self->{frontend_obj}->{test_data} = \@commands;
813 }
814 } ## end sub ban_cidr
815  
816 =head2 unban_cidr
817  
818 Unbans a CIDR range by removing it from the IP set for its family, rewriting
819 the set via a C<get-ip-set> plus C<update-ip-set> pair. The value of ban is
820 validated as being a IPv4 or IPv6 CIDR and lowercased. Unbanning a CIDR that
821 is not banned is a noop.
822  
823 $fw_helper->unban_cidr( ban => '1.2.3.0/24' );
824  
825 =cut
826  
827 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=10 SCAR=34.1
Pod documented
828 1 my ( $self, %opts ) = @_;
829  
830 1 $self->errorblank;
831  
832 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
833 0 $self->{error} = 1;
834 0 $self->{errorString} = 'backend has not been inited';
835 0 $self->warn;
836 0 return;
837 }
838  
839 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
840 0 $self->{error} = 9;
841 0 $self->{errorString} = 'Nothing specified for the value ban';
842 0 $self->warn;
843 0 return;
844 } elsif ( ref( $opts{ban} ) ne '' ) {
845 0 $self->{error} = 33;
846 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
847 0 $self->warn;
848 0 return;
849 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
850 0 $self->{error} = 33;
851 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
852 0 $self->warn;
853 0 return;
854 }
855  
856 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
857 1 $opts{ban} = lc( $opts{ban} );
858  
859 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
860 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
861 0 $self->{frontend_obj}->{test_data} = 'not banned';
862 }
863 0 return;
864 }
865  
866 1 my ($addr) = $opts{ban} =~ m!\A(.+)/[0-9]{1,3}\z!;
867 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
868 1 delete( $self->{banned_cidr}{ $opts{ban} } );
869  
870 1 my @commands;
871 1 $self->_apply_family( $fam, 32, \@commands );
872 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
873 1 $self->{frontend_obj}->{test_data} = \@commands;
874 }
875 } ## end sub unban_cidr
876  
877 =head2 list_cidr
878  
879 List banned CIDR ranges. Returns an array of the currently banned CIDR
880 ranges from internal state; the IP sets are not queried.
881  
882 my @banned_cidrs = $fw_helper->list_cidr;
883  
884 =cut
885  
886 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
887 2 my ( $self, %opts ) = @_;
888  
889 2 $self->errorblank;
890  
891 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
892 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
893 }
894  
895 2 return keys( %{ $self->{banned_cidr} } );
896 } ## end sub list_cidr
897  
898 =head2 list
899  
900 List banned IPs. Returns an array of the currently banned single IPs from
901 internal state; the IP sets are not queried. CIDR ranges are not included;
902 for those see L</list_cidr>.
903  
904 my @banned = $fw_helper->list;
905  
906 =cut
907  
908 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
909 0 my ( $self, %opts ) = @_;
910  
911 0 $self->errorblank;
912  
913 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
914 0 $self->{frontend_obj}->{test_data} = 'list';
915 }
916  
917 0 return keys( %{ $self->{banned} } );
918 } ## end sub list
919  
920 =head2 re_init
921  
922 Tears down and re-initiates, then re-applies the full retained ban list,
923 both single IPs and CIDR ranges, to each configured IP set via
924 C<get-ip-set> plus C<update-ip-set>.
925  
926 $fw_helper->re_init;
927  
928 =cut
929  
930 0 ▶sub re_init {
Subroutine re_init: not called CC=3 SCAR=24.8
Pod documented
931 0 my ( $self, %opts ) = @_;
932  
933 0 $self->errorblank;
934  
935 {
936 0 local $@;
937 0 eval { $self->teardown; };
938 }
939 0 $self->init;
940  
941 0 my @commands;
942 0 foreach my $fam ( $self->_configured_families ) {
943 0 $self->_apply_family( $fam, 13, \@commands );
944 }
945 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
946 0 $self->{frontend_obj}->{test_data} = \@commands;
947 }
948  
949 0 $self->{inited} = 1;
950 } ## end sub re_init
951  
952 =head2 teardown
953  
954 Empties each configured IP set by pushing an empty address list via
955 C<update-ip-set>. The internal ban list is kept so a following re_init
956 restores it. The IP sets themselves are not deleted.
957  
958 $fw_helper->teardown;
959  
960 =cut
961  
962 0 ▶sub teardown {
Subroutine teardown: not called CC=3 SCAR=24.8
Pod documented
963 0 my ( $self, %opts ) = @_;
964  
965 0 $self->errorblank;
966  
967 0 $self->{inited} = 0;
968  
969 # render updates against an empty ban set without disturbing the retained
970 # lists; both single IPs and CIDR ranges live in the same IP sets
971 0 my %saved = %{ $self->{banned} };
972 0 my %saved_cidr = %{ $self->{banned_cidr} };
973 0 $self->{banned} = {};
974 0 $self->{banned_cidr} = {};
975  
976 0 my @commands;
977 0 foreach my $fam ( $self->_configured_families ) {
978 0 $self->_apply_family( $fam, 17, \@commands );
979 }
980  
981 0 $self->{banned} = \%saved;
982 0 $self->{banned_cidr} = \%saved_cidr;
983  
984 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
985 0 $self->{frontend_obj}->{test_data} = \@commands;
986 }
987 } ## end sub teardown
988  
989 =head2 stop
990  
991 Alias for L</teardown>.
992  
993 $fw_helper->stop;
994  
995 =cut
996  
997 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
998 0 my ( $self, %opts ) = @_;
999  
1000 0 return $self->teardown(%opts);
1001 }
1002  
1003 =head2 check
1004  
1005 Verifies each configured IP set is still fetchable by running
1006 C<aws wafv2 get-ip-set> against it. Returns 1 if all succeed and 0 if any
1007 fail.
1008  
1009 $result=$fw_helper->check;
1010  
1011 =cut
1012  
1013 6 ▶sub check {
Subroutine check: called CC=5 SCAR=19.1
Pod documented
1014 6 my ( $self, %opts ) = @_;
1015  
1016 6 $self->errorblank;
1017  
1018 6 my @commands;
1019 6 foreach my $fam ( $self->_configured_families ) {
1020 11 my ( $name, $id ) = $self->_conf_for_family($fam);
1021 11 push( @commands, $self->_get_command( $name, $id ) );
1022 }
1023  
1024 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
1025 6 $self->{frontend_obj}->{test_data} = \@commands;
1026 6 return 1;
1027 }
1028  
1029 0 foreach my $command (@commands) {
1030 0 my $output = `$command 2>&1`;
1031 0 ▶ return 0 if ( $? != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 0 0
1032 }
1033  
1034 0 return 1;
1035 } ## end sub check
1036  
1037 =head2 flush
1038  
1039 Removes all bans at once by clearing the internal ban lists, both single
1040 IPs and CIDR ranges, and pushing the now empty address list to each
1041 configured IP set via C<update-ip-set>.
1042  
1043 $fw_helper->flush;
1044  
1045 =cut
1046  
1047 0 ▶sub flush {
Subroutine flush: not called CC=4 SCAR=30.0
Pod documented
1048 0 my ( $self, %opts ) = @_;
1049  
1050 0 $self->errorblank;
1051  
1052 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1053 0 $self->{error} = 1;
1054 0 $self->{errorString} = 'backend has not been inited';
1055 0 $self->warn;
1056 0 return;
1057 }
1058  
1059 # the IP sets are emptied at once, dropping both single IPs and CIDR ranges
1060 0 $self->{banned} = {};
1061 0 $self->{banned_cidr} = {};
1062  
1063 0 my @commands;
1064 0 foreach my $fam ( $self->_configured_families ) {
1065 0 $self->_apply_family( $fam, 25, \@commands );
1066 }
1067 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1068 0 $self->{frontend_obj}->{test_data} = \@commands;
1069 }
1070 } ## end sub flush
1071  
1072 =head1 ERROR CODES / FLAGS
1073  
1074 Error handling is provided by L<Error::Helper>. All
1075 errors are considered fatal.
1076  
1077 =head2 1, notInited
1078  
1079 The backend has not been inited yet.
1080  
1081 =head2 8, optionsNotHash
1082  
1083 The item passed to new for options is not a hash.
1084  
1085 =head2 9, noBanItem
1086  
1087 No IP or CIDR range specified to ban or unban.
1088  
1089 =head2 10, banItemNotIP
1090  
1091 The item to ban is not an IP. Either wrong ref type or regexp
1092 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1093  
1094 =head2 12, backendInitError
1095  
1096 Failed to init the backend. A get-ip-set for a configured IP set failed.
1097  
1098 =head2 13, banFailed
1099  
1100 Failed to ban the item.
1101  
1102 =head2 14, unbanFailed
1103  
1104 Failed to unban the item.
1105  
1106 =head2 15, listFailed
1107  
1108 Failed to get a list of bans.
1109  
1110 =head2 16, reInitFailed
1111  
1112 Failed to re_init the backend.
1113  
1114 =head2 17, teardownFailed
1115  
1116 Failed to teardown the backend.
1117  
1118 =head2 18, alreadyInited
1119  
1120 init called, but the backend has already been inited.
1121  
1122 =head2 20, scopeInvalid
1123  
1124 The scope option is not either "REGIONAL" or "CLOUDFRONT".
1125  
1126 =head2 24, checkFailed
1127  
1128 The backend check raised an error.
1129  
1130 =head2 25, flushFailed
1131  
1132 Failed to flush the bans.
1133  
1134 =head2 30, ipsetNotConfigured
1135  
1136 No IP set is configured for the family, IPv4 or IPv6, of the item.
1137  
1138 =head2 31, banCidrFailed
1139  
1140 Failed to ban the CIDR range.
1141  
1142 =head2 32, unbanCidrFailed
1143  
1144 Failed to unban the CIDR range.
1145  
1146 =head2 33, cidrItemNotCidr
1147  
1148 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1149 IPv4 or IPv6 address followed by a prefix length valid for its family.
1150  
1151 =head2 34, cidrNotSupported
1152  
1153 The backend does not support CIDR bans.
1154  
1155 =head2 35, listCidrFailed
1156  
1157 Failed to get a list of CIDR bans.
1158  
1159 =head1 AUTHOR
1160  
1161 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1162  
1163 =head1 LICENSE AND COPYRIGHT
1164  
1165 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
1166  
1167 This is free software, licensed under:
1168  
1169 The GNU Lesser General Public License, Version 2.1, February 1999
1170  
1171 =cut
1172  
1173 1 1; # End of Net::Firewall::BlockerHelper::backends::aws_wafv2
« BlockerHelper/backends/opnsense.pm ↑ Summary BlockerHelper/backends/linux_ip_route.pm »