BlockerHelper/backends/cloud_armor.pm

filter: Statementstmt 47.4% 130 / 274 Branchbran 45.4% 50 / 110 Conditioncond 41.6% 10 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 81.4% 22 / 27 Podpod 100% 13 / 13 totaltotal 48.9% 227 / 464 CC 66 SCAR 26.6
CC
66
Cov
47%
unban
30.1
unban_cidr
24.5
ban
23.4
CRAP 14.3
« BlockerHelper/backends/hosts_deny.pm ↑ Summary BlockerHelper/backends/file_reload.pm »
1 package Net::Firewall::BlockerHelper::backends::cloud_armor;
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::cloud_armor - Google Cloud Armor backend via the gcloud 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 => 'cloud_armor',
28 name => 'ssh',
29 options => {
30 policy => 'my-policy',
31 priority => 1000,
32 },
33 );
34  
35 $fw_helper->init_backend;
36 $fw_helper->ban( ban => '1.2.3.4' );
37 $fw_helper->unban( ban => '1.2.3.4' );
38  
39 =head1 DESCRIPTION
40  
41 Blocks IPs with Google Cloud Armor by maintaining the C<--src-ip-ranges> of a
42 single deny rule (identified by its priority) in a security policy, using the
43 C<gcloud> CLI. The full set of currently banned IPs is rendered into the rule
44 on every change, so ban/unban are idempotent. Both IPv4 and IPv6 share the one
45 rule.
46  
47 Cloud Armor caps the source ranges of a rule, ten per match condition at the
48 time of writing, so bans past that limit will fail. A failed ban is rolled
49 back out of the local ban list so it can not wedge later bans by being
50 re-rendered into every subsequent update. A failed unban is not rolled back;
51 the range is already gone locally and the next successful update converges
52 the rule.
53  
54 This backend manages only the rule's source ranges. The security policy, the
55 deny rule at the given priority, and the policy's attachment to a backend
56 service or load balancer must already exist.
57  
58 Requires C<gcloud> in the C<PATH>, authenticated (eg via an activated service
59 account) with rights to update the policy.
60  
61 =head1 METHODS
62  
63 =head2 new
64  
65 Initiates the object.
66  
67 - options :: Backend specific options. See below.
68 - name :: Required by Net::Firewall::BlockerHelper, otherwise unused.
69  
70 The options hash accepts the following.
71  
72 - gcloud_cmd :: Path to the gcloud binary.
73 - Default :: gcloud
74  
75 - policy :: Name of the security policy. Required.
76 - Default :: undef
77  
78 - priority :: Priority of the deny rule whose source ranges are managed.
79 - Default :: 1000
80  
81 - project :: Optional GCP project; appended as --project when set.
82 - Default :: undef
83  
84 All errors are considered fatal, meaning if new fails it will die.
85  
86 =cut
87  
88 sub new {
89 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=11 SCAR=25.9
Pod documented
90  
91 4 my $self = {
92 perror => undef,
93 error => undef,
94 errorLine => undef,
95 errorFilename => undef,
96 errorString => "",
97 errorExtra => {
98 all_errors_fatal => 1,
99 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
100 # is kept for the name documented in its POD
101 all_fatal => 1,
102 flags => {
103 1 => 'notInited',
104 8 => 'optionsNotHash',
105 9 => 'noBanItem',
106 10 => 'banItemNotIP',
107 12 => 'backendInitError',
108 13 => 'banFailed',
109 14 => 'unbanFailed',
110 15 => 'listFailed',
111 16 => 'reInitFailed',
112 17 => 'teardownFailed',
113 18 => 'alreadyInited',
114 24 => 'checkFailed',
115 25 => 'flushFailed',
116 30 => 'policyNotDefined',
117 31 => 'banCidrFailed',
118 32 => 'unbanCidrFailed',
119 33 => 'cidrItemNotCidr',
120 34 => 'cidrNotSupported',
121 35 => 'listCidrFailed',
122 },
123 fatal_flags => {},
124 perror_not_fatal => 0,
125 },
126 options => {},
127 ports => [],
128 protocols => [],
129 testing => undef,
130 test_data => undef,
131 prefix => 'kur',
132 name => undef,
133 frontend_obj => undef,
134 inited => 0,
135 banned => {},
136 banned_cidr => {},
137 cidr_supported => 1,
138 };
139 4 bless $self;
140  
141 4 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 4 0 4
142 4 $self->{testing} = $opts{testing};
143 }
144 4 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 4 0 4
145 4 $self->{frontend_obj} = $opts{frontend_obj};
146 }
147 4 ▶ if ( defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 4 0 4
148 4 $self->{name} = $opts{name};
149 }
150  
151 4 ▶ if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'} and ref $opts{'options'} ne 'HASH') { } 0 4 4
elsif (defined $opts{'options'}) { } 4 0 4
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
!ll&&!rl&&r
040
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
152 0 $self->{perror} = 1;
153 0 $self->{error} = 8;
154 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
155 0 $self->warn;
156 } elsif ( defined( $opts{options} ) ) {
157 4 $self->{options} = $opts{options};
158 }
159  
160 4 ▶ if ( !defined( $self->{options}{policy} ) || $self->{options}{policy} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"policy"} or $self->{"options"}{"policy"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"policy"} or $self->{"options"}{"policy"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"policy"} or $self->{"options"}{"policy"} eq ""
not defined $self->{"options"}{"policy"} $self->{"options"}{"policy"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"policy"}B= $self->{"options"}{"policy"} eq ""
ABresult
1X1
011
000
161 1 $self->{perror} = 1;
162 1 $self->{error} = 30;
163 1 $self->{errorString} = 'the option policy is undef or blank';
164 1 $self->warn;
165 }
166  
167 3 ▶ $self->{options}{gcloud_cmd} = 'gcloud' if ( !defined( $self->{options}{gcloud_cmd} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"gcloud_cmd"} 2 1 3
168 3 ▶ $self->{options}{priority} = 1000 if ( !defined( $self->{options}{priority} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"priority"} 2 1 3
169  
170 3 return $self;
171 } ## end sub new
172  
173 # Internal helper. Returns the trailing --project argument when configured, or
174 # an empty string.
175 sub _suffix {
176 21 ▶ my ($self) = @_;
Subroutine _suffix: called CC=3 SCAR=11.0
177  
178 21 ▶ if ( defined( $self->{options}{project} ) && $self->{options}{project} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"project"} and $self->{"options"}{"project"} ne "") 3 18 21
Condition66%2/3
defined $self->{"options"}{"project"} and $self->{"options"}{"project"} ne ""
!ll&&!rl&&r
1803
MC/DC50%1/2
defined $self->{"options"}{"project"} and $self->{"options"}{"project"} ne ""
defined $self->{"options"}{"project"} $self->{"options"}{"project"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"project"}B= $self->{"options"}{"project"} ne ""
ABresult
0X0
100
111
179 3 return ' --project ' . $self->{options}{project};
180 }
181 18 return '';
182 } ## end sub _suffix
183  
184 # Internal helper. Returns the current banned IPs, sorted, each as a CIDR
185 # (/32 for IPv4, /128 for IPv6), joined with commas.
186 sub _ranges {
187 11 ▶ my ($self) = @_;
Subroutine _ranges: called CC=4 SCAR=13.9
188  
189 11 my @ranges;
190 11 foreach my $ip ( sort( keys( %{ $self->{banned} } ) ) ) {
191 19 ▶ push( @ranges, $ip . ( ( $ip =~ /\A$IPv4_re\z/ ) ? '/32' : '/128' ) );
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ ? : 15 4 19
192 }
193  
194 # CIDR ranges already carry a prefix, so they are emitted verbatim
195 11 foreach my $cidr ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
196 2 push( @ranges, $cidr );
197 }
198  
199 11 return join( ',', @ranges );
200 } ## end sub _ranges
201  
202 # Internal helper. Returns the gcloud command that sets the rule's source
203 # ranges to the currently banned set.
204 sub _update_command {
205 11 ▶ my ($self) = @_;
Subroutine _update_command: called CC=1 SCAR=0.0
206  
207 return
208 $self->{options}{gcloud_cmd}
209 . ' compute security-policies rules update '
210 . $self->{options}{priority}
211 . ' --security-policy '
212 . $self->{options}{policy}
213 11 . ' --src-ip-ranges '
214 . $self->_ranges
215 . $self->_suffix;
216 } ## end sub _update_command
217  
218 # Internal helper. Returns the gcloud command used to verify the rule exists.
219 sub _describe_command {
220 10 ▶ my ($self) = @_;
Subroutine _describe_command: called CC=1 SCAR=0.0
221  
222 return
223 $self->{options}{gcloud_cmd}
224 . ' compute security-policies rules describe '
225 . $self->{options}{priority}
226 . ' --security-policy '
227 . $self->{options}{policy}
228 10 . $self->_suffix;
229 } ## end sub _describe_command
230  
231 # Internal helper. Runs a command unless testing, raising the passed error flag
232 # on a non-zero exit.
233 sub _run {
234 2 ▶ my ( $self, $command, $error_flag, $rollback ) = @_;
Subroutine _run: called CC=3 SCAR=11.4
235  
236 2 my $output = `$command 2>&1`;
237 2 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 0 2
238 # errors are fatal, so any state rollback has to happen before warn
239 2 ▶ $rollback->() if ( defined($rollback) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rollback 2 0 2
240 2 $self->{error} = $error_flag;
241 2 $self->{errorString} = 'command "' . $command . '" failed... ' . $output;
242 2 $self->warn;
243 }
244  
245 0 return;
246 } ## end sub _run
247  
248 =head2 init
249  
250 Initiates the backend, verifying the deny rule at the configured priority
251 exists via C<gcloud compute security-policies rules describe>.
252  
253 =cut
254  
255 sub init {
256 3 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=3 SCAR=12.7
Pod documented
257  
258 3 $self->errorblank;
259  
260 3 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 3 3
261 0 $self->{error} = 18;
262 0 $self->{errorString} = 'backend has already been inited';
263 0 $self->warn;
264 }
265  
266 3 my @commands = ( $self->_describe_command );
267  
268 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
269 3 $self->{frontend_obj}->{test_data} = \@commands;
270 } else {
271 0 $self->_run( $commands[0], 12 );
272 }
273  
274 3 $self->{inited} = 1;
275 } ## end sub init
276  
277 =head2 ban
278  
279 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
280 and lowercased, then added to the ban list. The rule's source ranges are
281 then rewritten via C<gcloud compute security-policies rules update>, the IP
282 included as a /32 (IPv4) or /128 (IPv6) range. Banning an already banned IP
283 is a noop.
284  
285 $fw_helper->ban( ban => $ip );
286  
287 =cut
288  
289 sub ban {
290 7 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=9 SCAR=31.5
Pod documented
291  
292 7 $self->errorblank;
293  
294 7 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 7 7
295 0 $self->{error} = 1;
296 0 $self->{errorString} = 'backend has not been inited';
297 0 $self->warn;
298 0 return;
299 }
300  
301 7 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 7 7
elsif (ref $opts{'ban'} ne '') { } 0 7 7
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/) { } 0 7 7
302 0 $self->{error} = 9;
303 0 $self->{errorString} = 'Nothing specified for the value ban';
304 0 $self->warn;
305 0 return;
306 } elsif ( ref( $opts{ban} ) ne '' ) {
307 0 $self->{error} = 10;
308 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
309 0 $self->warn;
310 0 return;
311 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
l!l&&r!l&&!r
610
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
ABresult
1X1
011
000
312 && $opts{ban} !~ /\A$IPv6_re\z/ )
313 {
314 0 $self->{error} = 10;
315 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
316 0 $self->warn;
317 0 return;
318 }
319  
320 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
321 7 $opts{ban} = lc( $opts{ban} );
322  
323 7 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 7 7
324 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
325 0 $self->{frontend_obj}->{test_data} = 'already banned';
326 }
327 0 return;
328 }
329  
330 7 $self->{banned}{ $opts{ban} } = 1;
331  
332 7 my $command = $self->_update_command;
333 7 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 6 1 7
334 6 $self->{frontend_obj}->{test_data} = $command;
335 } else {
336 1 ▶ $self->_run( $command, 13, sub { delete( $self->{banned}{ $opts{ban} } ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
337 }
338 } ## end sub ban
339  
340 =head2 unban
341  
342 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
343 and lowercased, then removed from the ban list. The rule's source ranges are
344 rewritten without it via C<gcloud compute security-policies rules update>.
345 Unbanning an IP that is not banned is a noop.
346  
347 $fw_helper->unban( ban => $ip );
348  
349 =cut
350  
351 sub unban {
352 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=9 SCAR=34.0
Pod documented
353  
354 1 $self->errorblank;
355  
356 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
357 0 $self->{error} = 1;
358 0 $self->{errorString} = 'backend has not been inited';
359 0 $self->warn;
360 0 return;
361 }
362  
363 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/) { } 0 1 1
364 0 $self->{error} = 9;
365 0 $self->{errorString} = 'Nothing specified for the value ban';
366 0 $self->warn;
367 0 return;
368 } elsif ( ref( $opts{ban} ) ne '' ) {
369 0 $self->{error} = 10;
370 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
371 0 $self->warn;
372 0 return;
373 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/
ABresult
1X1
011
000
374 && $opts{ban} !~ /\A$IPv6_re\z/ )
375 {
376 0 $self->{error} = 10;
377 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
378 0 $self->warn;
379 0 return;
380 }
381  
382 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
383 1 $opts{ban} = lc( $opts{ban} );
384  
385 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
386 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
387 0 $self->{frontend_obj}->{test_data} = 'not banned';
388 }
389 0 return;
390 }
391  
392 1 delete( $self->{banned}{ $opts{ban} } );
393  
394 1 my $command = $self->_update_command;
395 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
396 1 $self->{frontend_obj}->{test_data} = $command;
397 } else {
398 0 $self->_run( $command, 14 );
399 }
400 } ## end sub unban
401  
402 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
403 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
404 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
405 # IPv6). Returns false otherwise.
406 sub _valid_cidr {
407 3 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
408  
409 3 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 3 3
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
003
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
410  
411 3 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 3 0 3
412 3 my ( $addr, $prefix ) = ( $1, $2 );
413 3 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ and $prefix <= 32 3 0 3
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
003
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
414 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::cloud_armor::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
415 }
416  
417 0 return 0;
418 } ## end sub _valid_cidr
419  
420 =head2 ban_cidr
421  
422 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
423 CIDR range and lowercased, then added to the rule's source ranges via
424 C<gcloud compute security-policies rules update>. Banning an already banned
425 range is a noop.
426  
427 $fw_helper->ban_cidr( ban => '1.2.3.0/24' );
428  
429 =cut
430  
431 sub ban_cidr {
432 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=8 SCAR=30.1
Pod documented
433  
434 2 $self->errorblank;
435  
436 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
437 0 $self->{error} = 1;
438 0 $self->{errorString} = 'backend has not been inited';
439 0 $self->warn;
440 0 return;
441 }
442  
443 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 2 2
444 0 $self->{error} = 9;
445 0 $self->{errorString} = 'Nothing specified for the value ban';
446 0 $self->warn;
447 0 return;
448 } elsif ( ref( $opts{ban} ) ne '' ) {
449 0 $self->{error} = 33;
450 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
451 0 $self->warn;
452 0 return;
453 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
454 0 $self->{error} = 33;
455 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
456 0 $self->warn;
457 0 return;
458 }
459  
460 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
461 2 $opts{ban} = lc( $opts{ban} );
462  
463 2 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 2 2
464 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
465 0 $self->{frontend_obj}->{test_data} = 'already banned';
466 }
467 0 return;
468 }
469  
470 2 $self->{banned_cidr}{ $opts{ban} } = 1;
471  
472 2 my $command = $self->_update_command;
473 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 1 2
474 1 $self->{frontend_obj}->{test_data} = $command;
475 } else {
476 1 ▶ $self->_run( $command, 31, sub { delete( $self->{banned_cidr}{ $opts{ban} } ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
477 }
478 } ## end sub ban_cidr
479  
480 =head2 unban_cidr
481  
482 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
483 CIDR range and lowercased, then the rule's source ranges are rewritten
484 without it. Unbanning a range that is not banned is a noop.
485  
486 $fw_helper->unban_cidr( ban => '1.2.3.0/24' );
487  
488 =cut
489  
490 sub unban_cidr {
491 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=8 SCAR=32.0
Pod documented
492  
493 1 $self->errorblank;
494  
495 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
496 0 $self->{error} = 1;
497 0 $self->{errorString} = 'backend has not been inited';
498 0 $self->warn;
499 0 return;
500 }
501  
502 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
503 0 $self->{error} = 9;
504 0 $self->{errorString} = 'Nothing specified for the value ban';
505 0 $self->warn;
506 0 return;
507 } elsif ( ref( $opts{ban} ) ne '' ) {
508 0 $self->{error} = 33;
509 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
510 0 $self->warn;
511 0 return;
512 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
513 0 $self->{error} = 33;
514 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
515 0 $self->warn;
516 0 return;
517 }
518  
519 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
520 1 $opts{ban} = lc( $opts{ban} );
521  
522 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
523 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
524 0 $self->{frontend_obj}->{test_data} = 'not banned';
525 }
526 0 return;
527 }
528  
529 1 delete( $self->{banned_cidr}{ $opts{ban} } );
530  
531 1 my $command = $self->_update_command;
532 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
533 1 $self->{frontend_obj}->{test_data} = $command;
534 } else {
535 0 $self->_run( $command, 32 );
536 }
537 } ## end sub unban_cidr
538  
539 =head2 list_cidr
540  
541 List banned CIDR ranges. Returns an array of the currently banned CIDR
542 ranges. Single IPs are not included; for those see L</list>.
543  
544 my @banned_cidrs = $fw_helper->list_cidr;
545  
546 =cut
547  
548 sub list_cidr {
549 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
550  
551 2 $self->errorblank;
552  
553 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
554 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
555 }
556  
557 2 return keys( %{ $self->{banned_cidr} } );
558 }
559  
560 =head2 list
561  
562 List banned IPs. Returns an array of the currently banned single IPs. CIDR
563 ranges are not included; for those see L</list_cidr>.
564  
565 my @banned = $fw_helper->list;
566  
567 =cut
568  
569 sub list {
570 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
571  
572 0 $self->errorblank;
573  
574 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
575 0 $self->{frontend_obj}->{test_data} = 'list';
576 }
577  
578 0 return keys( %{ $self->{banned} } );
579 }
580  
581 =head2 re_init
582  
583 Tears down and re-initiates, then re-applies the full retained banned set
584 (single IPs and CIDR ranges) to the rule's source ranges with a single
585 gcloud update.
586  
587 =cut
588  
589 sub re_init {
590 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=3 SCAR=24.8
Pod documented
591  
592 0 $self->errorblank;
593  
594 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
595 0 $self->{error} = 1;
596 0 $self->{errorString} = 'backend has not been inited';
597 0 $self->warn;
598 0 return;
599 }
600  
601 {
602 0 local $@;
603 0 eval { $self->teardown; };
604 }
605 0 $self->init;
606  
607 0 my $command = $self->_update_command;
608 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
609 0 $self->{frontend_obj}->{test_data} = [$command];
610 } else {
611 0 $self->_run( $command, 13 );
612 }
613  
614 0 $self->{inited} = 1;
615 } ## end sub re_init
616  
617 =head2 teardown
618  
619 Empties the rule's source ranges. The internal ban list is kept so a
620 following re_init restores them.
621  
622 =cut
623  
624 sub teardown {
625 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=2 SCAR=17.9
Pod documented
626  
627 0 $self->errorblank;
628  
629 0 $self->{inited} = 0;
630  
631 # render the update with an empty ban set without disturbing the retained lists
632 0 my %saved = %{ $self->{banned} };
633 0 my %saved_cidr = %{ $self->{banned_cidr} };
634 0 $self->{banned} = {};
635 0 $self->{banned_cidr} = {};
636 0 my $command = $self->_update_command;
637 0 $self->{banned} = \%saved;
638 0 $self->{banned_cidr} = \%saved_cidr;
639  
640 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
641 0 $self->{frontend_obj}->{test_data} = [$command];
642 } else {
643 0 $self->_run( $command, 17 );
644 }
645 } ## end sub teardown
646  
647 =head2 stop
648  
649 Alias for L</teardown>.
650  
651 =cut
652  
653 sub stop {
654 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
655  
656 0 return $self->teardown(%opts);
657 }
658  
659 =head2 check
660  
661 Verifies the rule still exists via C<gcloud compute security-policies rules
662 describe>. Returns a true value on a zero exit and a false value otherwise.
663  
664 =cut
665  
666 sub check {
667 7 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=13.0
Pod documented
668  
669 7 $self->errorblank;
670  
671 7 my $command = $self->_describe_command;
672  
673 7 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 7 0 7
674 7 $self->{frontend_obj}->{test_data} = [$command];
675 7 return 1;
676 }
677  
678 0 my $output = `$command 2>&1`;
679 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
680 } ## end sub check
681  
682 =head2 flush
683  
684 Removes all currently banned IPs and CIDR ranges at once by clearing the
685 ban lists and rewriting the rule's source ranges empty with a single gcloud
686 update.
687  
688 =cut
689  
690 sub flush {
691 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=3 SCAR=24.8
Pod documented
692  
693 0 $self->errorblank;
694  
695 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
696 0 $self->{error} = 1;
697 0 $self->{errorString} = 'backend has not been inited';
698 0 $self->warn;
699 0 return;
700 }
701  
702 # the single update below rewrites the rule with both sets empty at once
703 0 $self->{banned} = {};
704 0 $self->{banned_cidr} = {};
705  
706 0 my $command = $self->_update_command;
707 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
708 0 $self->{frontend_obj}->{test_data} = [$command];
709 } else {
710 0 $self->_run( $command, 25 );
711 }
712 } ## end sub flush
713  
714 =head1 ERROR CODES / FLAGS
715  
716 Error handling is provided by L<Error::Helper>. All
717 errors are considered fatal.
718  
719 =head2 1, notInited
720  
721 The backend has not been inited yet.
722  
723 =head2 8, optionsNotHash
724  
725 The item passed to new for options is not a hash.
726  
727 =head2 9, noBanItem
728  
729 No IP or CIDR range specified to ban or unban.
730  
731 =head2 10, banItemNotIP
732  
733 The item to ban is not an IP. Either wrong ref type or regexp
734 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
735  
736 =head2 12, backendInitError
737  
738 Failed to init the backend.
739  
740 =head2 13, banFailed
741  
742 Failed to ban the item.
743  
744 =head2 14, unbanFailed
745  
746 Failed to unban the item.
747  
748 =head2 15, listFailed
749  
750 Failed to get a list of bans.
751  
752 =head2 16, reInitFailed
753  
754 Failed to re_init the backend.
755  
756 =head2 17, teardownFailed
757  
758 Failed to teardown the backend.
759  
760 =head2 18, alreadyInited
761  
762 init called, but the backend has already been inited.
763  
764 =head2 24, checkFailed
765  
766 The backend check raised an error.
767  
768 =head2 25, flushFailed
769  
770 Failed to flush the bans.
771  
772 =head2 30, policyNotDefined
773  
774 The option policy is undef or blank.
775  
776 =head2 31, banCidrFailed
777  
778 Failed to ban the CIDR range.
779  
780 =head2 32, unbanCidrFailed
781  
782 Failed to unban the CIDR range.
783  
784 =head2 33, cidrItemNotCidr
785  
786 The item to ban is not a CIDR range. Either wrong ref type or it is not an
787 IPv4 or IPv6 address followed by a prefix length valid for its family.
788  
789 =head2 34, cidrNotSupported
790  
791 The backend does not support CIDR bans.
792  
793 =head2 35, listCidrFailed
794  
795 Failed to get a list of CIDR bans.
796  
797 =head1 AUTHOR
798  
799 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
800  
801 =head1 LICENSE AND COPYRIGHT
802  
803 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
804  
805 This is free software, licensed under:
806  
807 The GNU Lesser General Public License, Version 2.1, February 1999
808  
809 =cut
810  
811 1 1; # End of Net::Firewall::BlockerHelper::backends::cloud_armor
« BlockerHelper/backends/hosts_deny.pm ↑ Summary BlockerHelper/backends/file_reload.pm »