BlockerHelper/backends/azure.pm

filter: Statementstmt 42.5% 115 / 270 Branchbran 38.0% 38 / 100 Conditioncond 52.3% 11 / 21 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 72.0% 18 / 25 Podpod 100% 13 / 13 totaltotal 44.9% 199 / 443 CC 60 SCAR 30.8
CC
60
Cov
42%
unban
90.0
ban
28.0
ban_cidr
24.5
CRAP 21.8
« BlockerHelper/backends/xdp.pm ↑ Summary BlockerHelper/backends/hosts_deny.pm »
1 package Net::Firewall::BlockerHelper::backends::azure;
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::azure - Azure NSG backend via the az 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 => 'azure',
28 name => 'ssh',
29 options => {
30 resource_group => 'my-rg',
31 nsg => 'my-nsg',
32 rule => 'blocklist',
33 },
34 );
35  
36 $fw_helper->init_backend;
37 $fw_helper->ban( ban => '1.2.3.4' );
38 $fw_helper->unban( ban => '1.2.3.4' );
39  
40 =head1 DESCRIPTION
41  
42 Blocks IPs on Azure by maintaining the C<--source-address-prefixes> of a
43 single deny rule in a Network Security Group, using the C<az> CLI. The full
44 set of currently banned IPs is rendered into the rule on every change, so
45 ban/unban are idempotent. Both IPv4 and IPv6 share the one rule.
46  
47 This backend manages only the rule's source prefixes. The NSG, the inbound
48 deny security rule, and the NSG's association with the subnets or NICs to be
49 protected must already exist.
50  
51 Requires the C<az> CLI in the C<PATH>, logged in with rights to update the
52 rule.
53  
54 =head1 NOTES
55  
56 This backend was written going off the az CLI docs and actual testing is
57 needed to double check a few things as the exact behavior is not clear.
58  
59 When the ban list goes empty, as at teardown or flush, the rule update
60 ends up passing C<--source-address-prefixes> with no value, and whether
61 the az CLI accepts that or errors needs checking.
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Initiates the backend object. Nothing is contacted at this point; that is done by L</init>.
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 - az_cmd :: Path to the az binary.
75 - Default :: az
76  
77 - resource_group :: Resource group of the NSG. Required.
78 - Default :: undef
79  
80 - nsg :: Network Security Group name. Required.
81 - Default :: undef
82  
83 - rule :: Security rule whose source prefixes are managed. Required.
84 - Default :: undef
85  
86 - subscription :: Optional subscription; appended as --subscription when set.
87 - Default :: undef
88  
89 All errors are considered fatal, meaning if new fails it will die.
90  
91 =cut
92  
93 5 ▶sub new {
Subroutine new: called CC=14 SCAR=28.2
Pod documented
94 5 my ( $blank, %opts ) = @_;
95  
96 5 my $self = {
97 perror => undef,
98 error => undef,
99 errorLine => undef,
100 errorFilename => undef,
101 errorString => "",
102 errorExtra => {
103 all_errors_fatal => 1,
104 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
105 # is kept for the name documented in its POD
106 all_fatal => 1,
107 flags => {
108 1 => 'notInited',
109 8 => 'optionsNotHash',
110 9 => 'noBanItem',
111 10 => 'banItemNotIP',
112 12 => 'backendInitError',
113 13 => 'banFailed',
114 14 => 'unbanFailed',
115 15 => 'listFailed',
116 16 => 'reInitFailed',
117 17 => 'teardownFailed',
118 18 => 'alreadyInited',
119 24 => 'checkFailed',
120 25 => 'flushFailed',
121 30 => 'resourceGroupNotDefined',
122 31 => 'nsgNotDefined',
123 32 => 'ruleNotDefined',
124 33 => 'banCidrFailed',
125 34 => 'unbanCidrFailed',
126 35 => 'cidrItemNotCidr',
127 36 => 'cidrNotSupported',
128 37 => 'listCidrFailed',
129 },
130 fatal_flags => {},
131 perror_not_fatal => 0,
132 },
133 options => {},
134 ports => [],
135 protocols => [],
136 testing => undef,
137 test_data => undef,
138 prefix => 'kur',
139 name => undef,
140 frontend_obj => undef,
141 inited => 0,
142 banned => {},
143 banned_cidr => {},
144 cidr_supported => 1,
145 };
146 5 bless $self;
147  
148 5 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 5 0 5
149 5 $self->{testing} = $opts{testing};
150 }
151 5 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 5 0 5
152 5 $self->{frontend_obj} = $opts{frontend_obj};
153 }
154 5 ▶ if ( defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 5 0 5
155 5 $self->{name} = $opts{name};
156 }
157  
158 5 ▶ if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'} and ref $opts{'options'} ne 'HASH') { } 0 5 5
elsif (defined $opts{'options'}) { } 5 0 5
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
!ll&&!rl&&r
050
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
159 0 $self->{perror} = 1;
160 0 $self->{error} = 8;
161 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
162 0 $self->warn;
163 } elsif ( defined( $opts{options} ) ) {
164 5 $self->{options} = $opts{options};
165 }
166  
167 5 ▶ if ( !defined( $self->{options}{resource_group} ) || $self->{options}{resource_group} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"resource_group"} or $self->{"options"}{"resource_group"} eq "") 1 4 5
Condition66%2/3
not defined $self->{"options"}{"resource_group"} or $self->{"options"}{"resource_group"} eq ""
l!l&&r!l&&!r
104
MC/DC50%1/2
not defined $self->{"options"}{"resource_group"} or $self->{"options"}{"resource_group"} eq ""
not defined $self->{"options"}{"resource_group"} $self->{"options"}{"resource_group"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"resource_group"}B= $self->{"options"}{"resource_group"} eq ""
ABresult
1X1
011
000
168 1 $self->{perror} = 1;
169 1 $self->{error} = 30;
170 1 $self->{errorString} = 'the option resource_group is undef or blank';
171 1 $self->warn;
172 }
173 4 ▶ if ( !defined( $self->{options}{nsg} ) || $self->{options}{nsg} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"nsg"} or $self->{"options"}{"nsg"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"nsg"} or $self->{"options"}{"nsg"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"nsg"} or $self->{"options"}{"nsg"} eq ""
not defined $self->{"options"}{"nsg"} $self->{"options"}{"nsg"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"nsg"}B= $self->{"options"}{"nsg"} eq ""
ABresult
1X1
011
000
174 1 $self->{perror} = 1;
175 1 $self->{error} = 31;
176 1 $self->{errorString} = 'the option nsg is undef or blank';
177 1 $self->warn;
178 }
179 3 ▶ if ( !defined( $self->{options}{rule} ) || $self->{options}{rule} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"rule"} or $self->{"options"}{"rule"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"rule"} or $self->{"options"}{"rule"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"rule"} or $self->{"options"}{"rule"} eq ""
not defined $self->{"options"}{"rule"} $self->{"options"}{"rule"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"rule"}B= $self->{"options"}{"rule"} eq ""
ABresult
1X1
011
000
180 1 $self->{perror} = 1;
181 1 $self->{error} = 32;
182 1 $self->{errorString} = 'the option rule is undef or blank';
183 1 $self->warn;
184 }
185  
186 2 ▶ $self->{options}{az_cmd} = 'az' if ( !defined( $self->{options}{az_cmd} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"az_cmd"} 2 0 2
187  
188 2 return $self;
189 } ## end sub new
190  
191 # Internal helper. Returns the --subscription argument to append to every az
192 # invocation, or nothing when no subscription is configured.
193 #
194 # Emitting the flag only when the option is set is deliberate rather than
195 # defaulting to one here. With it left off, az uses whichever subscription the
196 # logged in account has set as its default, which is what an instance running
197 # in an already configured environment wants. It only needs specifying on a
198 # host whose account can see several subscriptions and whose default is not
199 # the right one.
200 #
201 # Takes no arguments.
202 #
203 # Returns the argument as a string with a leading space, ready to be
204 # concatenated onto a command, or the empty string when the subscription
205 # option is unset or empty. Never returns undef.
206 #
207 # # with the subscription option set
208 # $self->_suffix; # ' --subscription sub-123'
209 #
210 # # with no subscription configured
211 # $self->_suffix; # ''
212 14 ▶sub _suffix {
Subroutine _suffix: called CC=3 SCAR=11.0
213 14 my ($self) = @_;
214  
215 14 ▶ if ( defined( $self->{options}{subscription} ) && $self->{options}{subscription} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"subscription"} and $self->{"options"}{"subscription"} ne "") 3 11 14
Condition66%2/3
defined $self->{"options"}{"subscription"} and $self->{"options"}{"subscription"} ne ""
!ll&&!rl&&r
1103
MC/DC50%1/2
defined $self->{"options"}{"subscription"} and $self->{"options"}{"subscription"} ne ""
defined $self->{"options"}{"subscription"} $self->{"options"}{"subscription"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"subscription"}B= $self->{"options"}{"subscription"} ne ""
ABresult
0X0
100
111
216 3 return ' --subscription ' . $self->{options}{subscription};
217 }
218 11 return '';
219 }
220  
221  
222 # Internal helper. Returns the arguments that identify the NSG security rule
223 # this instance manages, shared by every command that touches it.
224 #
225 # Azure addresses a rule by the three levels it is nested in: the resource
226 # group holding the network security group, the NSG itself, and the rule
227 # within it. All three are needed on every call. Since the show and update
228 # commands take exactly the same identifying arguments, they are built once
229 # here so the two cannot drift and end up addressing different rules.
230 #
231 # Note this backend does not create the rule. The NSG and the rule are
232 # expected to already exist and to be wired into the NSG's rule ordering by
233 # whoever set them up; all this backend does is rewrite the rule's source
234 # prefixes.
235 #
236 # Takes no arguments; all three names come from the options.
237 #
238 # Returns the arguments as a single string with a leading space, ready to be
239 # concatenated onto a command. The subscription is not included; that comes
240 # from _suffix and goes at the end.
241 #
242 # $self->_base;
243 # # ' --resource-group my-rg --nsg-name my-nsg --name blocklist'
244 14 ▶sub _base {
Subroutine _base: called CC=1 SCAR=0.0
245 14 my ($self) = @_;
246  
247 return
248 ' --resource-group '
249 . $self->{options}{resource_group}
250 . ' --nsg-name '
251 . $self->{options}{nsg}
252 . ' --name '
253 14 . $self->{options}{rule};
254 } ## end sub _base
255  
256 # Internal helper. Renders the current bans into the source prefix list that
257 # the NSG rule update takes.
258 #
259 # An NSG rule's source prefixes are replace-in-full: there is no add or remove
260 # one prefix call, so every ban and unban rewrites the whole list from the
261 # internal state. That is what this produces.
262 #
263 # Everything is emitted as CIDR, so a single address gets an explicit host
264 # prefix appended, /32 or /128 by family. Banned ranges already carry a prefix
265 # and go out as they are. Both families go into the same rule, unlike the AWS
266 # backend where the two are separate objects.
267 #
268 # Single addresses and ranges are pulled from two separate internal lists and
269 # emitted as two sorted runs rather than one merged sorted list, so the
270 # addresses come first and the ranges after. That is stable, which is what
271 # matters for the test_data comparisons, just not globally sorted.
272 #
273 # Takes no arguments; the bans come from the object's banned and banned_cidr
274 # hashes.
275 #
276 # Returns the prefixes as a single space joined string, ready to follow
277 # --source-address-prefixes on the command line. Returns the empty string when
278 # nothing is banned, which is what teardown and flush push out to empty the
279 # rule.
280 #
281 # # with 10.0.0.1, 2001:db8::1, and 10.0.0.0/8 banned
282 # $self->_prefixes;
283 # # '10.0.0.1/32 2001:db8::1/128 10.0.0.0/8'
284 #
285 # # with nothing banned
286 # $self->_prefixes; # ''
287 6 ▶sub _prefixes {
Subroutine _prefixes: called CC=4 SCAR=13.9
288 6 my ($self) = @_;
289  
290 6 my @prefixes;
291 6 foreach my $ip ( sort( keys( %{ $self->{banned} } ) ) ) {
292 13 ▶ push( @prefixes, $ip . ( ( $ip =~ /\A$IPv4_re\z/ ) ? '/32' : '/128' ) );
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ ? : 10 3 13
293 }
294 # CIDR ranges already carry a prefix and are added verbatim
295 6 foreach my $cidr ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
296 1 push( @prefixes, $cidr );
297 }
298  
299 6 return join( ' ', @prefixes );
300 } ## end sub _prefixes
301  
302 # Internal helper. Returns the az command that writes the current bans into
303 # the NSG rule's source prefixes. This is the command every ban, unban,
304 # re_init, teardown, and flush ultimately runs.
305 #
306 # The rule is rewritten in full each time, since Azure offers no incremental
307 # prefix add or remove. Note there is no concurrency guard here, unlike the
308 # AWS backend's lock token: two processes updating the same rule at once will
309 # each write back a list based on their own state and the last one wins.
310 #
311 # Takes no arguments; the rule identity, the prefixes, and the subscription
312 # are all assembled from the object.
313 #
314 # Returns the command as a single string ready to hand to the runner. The az
315 # binary comes from the az_cmd option, 'az' by default.
316 #
317 # # with 10.0.0.1, 2001:db8::1, and 10.0.0.0/8 banned
318 # $self->_update_command;
319 # # az network nsg rule update --resource-group my-rg --nsg-name my-nsg --name blocklist --source-address-prefixes 10.0.0.1/32 2001:db8::1/128 10.0.0.0/8
320 6 ▶sub _update_command {
Subroutine _update_command: called CC=1 SCAR=0.0
321 6 my ($self) = @_;
322  
323 return
324 $self->{options}{az_cmd}
325 6 . ' network nsg rule update'
326 . $self->_base
327 . ' --source-address-prefixes '
328 . $self->_prefixes
329 . $self->_suffix;
330 } ## end sub _update_command
331  
332 # Internal helper. Returns the az command that confirms the NSG rule exists,
333 # used by init and by check.
334 #
335 # Since this backend creates nothing, the rule existing is the whole of its
336 # setup, and a rule deleted out from under a running process is the failure
337 # this detects. init runs it so a misconfigured resource group, NSG, or rule
338 # name fails immediately rather than at the first ban, and check runs it to
339 # decide whether the self heal path should re-push the current bans.
340 #
341 # Only the exit status is looked at; nothing parses the rule definition that
342 # comes back.
343 #
344 # Takes no arguments.
345 #
346 # Returns the command as a single string ready to hand to the runner, whose
347 # exit status is 0 when the rule is there.
348 #
349 # $self->_show_command;
350 # # az network nsg rule show --resource-group my-rg --nsg-name my-nsg --name blocklist
351 #
352 # # with the subscription option set, it is appended
353 # # az network nsg rule show --resource-group my-rg --nsg-name my-nsg --name blocklist --subscription sub-123
354 8 ▶sub _show_command {
Subroutine _show_command: called CC=1 SCAR=0.0
355 8 my ($self) = @_;
356  
357 8 return $self->{options}{az_cmd} . ' network nsg rule show' . $self->_base . $self->_suffix;
358 }
359  
360 # Internal helper. Runs one az command and turns a non zero exit into an
361 # error, so the callers do not each repeat the same capture-and-check.
362 #
363 # stderr is folded into stdout so that whatever az complained about ends up in
364 # the errorString rather than being lost to the terminal. The output is only
365 # used for that; nothing parses it.
366 #
367 # Nothing here dies. A failure sets the error to the passed code, sets
368 # errorString with the command and its output, and warns, leaving the caller
369 # to check the error state.
370 #
371 # Note this does not itself check testing mode despite what the callers'
372 # structure might suggest. Callers decide whether to run or to record, and
373 # only call this when actually running.
374 #
375 # Args:
376 #
377 # command - The complete shell command to run, as a plain string,
378 # normally from _update_command or _show_command. Run through
379 # the shell, so it is expected to be already quoted as
380 # needed.
381 #
382 # error_flag - The numeric error code to raise on a non zero exit, so the
383 # error reads as the operation that triggered it rather than
384 # as a generic command failure. Callers pass the code
385 # matching themselves, such as 13 from ban or 17 from
386 # teardown.
387 #
388 # Returns nothing, on success and on failure alike.
389 #
390 # # from ban, so a failure reads as banFailed
391 # $self->_run( $self->_update_command, 13 );
392 #
393 # # from init, verifying the rule is there
394 # $self->_run( $self->_show_command, 12 );
395 0 ▶sub _run {
Subroutine _run: not called CC=2 SCAR=17.9
396 0 my ( $self, $command, $error_flag ) = @_;
397  
398 0 my $output = `$command 2>&1`;
399 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
400 0 $self->{error} = $error_flag;
401 0 $self->{errorString} = 'command "' . $command . '" failed... ' . $output;
402 0 $self->warn;
403 }
404  
405 0 return;
406 } ## end sub _run
407  
408 =head2 init
409  
410 Initiates the backend, verifying the configured security rule exists via
411 C<az network nsg rule show>. Nothing is created; the NSG and rule must
412 already exist.
413  
414 =cut
415  
416 2 ▶sub init {
Subroutine init: called CC=3 SCAR=12.7
Pod documented
417 2 my ( $self, %opts ) = @_;
418  
419 2 $self->errorblank;
420  
421 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
422 0 $self->{error} = 18;
423 0 $self->{errorString} = 'backend has already been inited';
424 0 $self->warn;
425 }
426  
427 2 my @commands = ( $self->_show_command );
428  
429 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
430 2 $self->{frontend_obj}->{test_data} = \@commands;
431 } else {
432 0 $self->_run( $commands[0], 12 );
433 }
434  
435 2 $self->{inited} = 1;
436 } ## end sub init
437  
438 =head2 ban
439  
440 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
441 and lowercased, then added to the ban list. The full set of banned IPs
442 (rendered as /32 or /128 prefixes) and CIDR ranges is then pushed to the
443 rule via C<az network nsg rule update --source-address-prefixes>. Banning
444 an already banned IP is a noop.
445  
446 $fw_helper->ban( ban => $ip );
447  
448 =cut
449  
450 4 ▶sub ban {
Subroutine ban: called CC=9 SCAR=33.3
Pod documented
451 4 my ( $self, %opts ) = @_;
452  
453 4 $self->errorblank;
454  
455 4 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 4 4
456 0 $self->{error} = 1;
457 0 $self->{errorString} = 'backend has not been inited';
458 0 $self->warn;
459 0 return;
460 }
461  
462 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::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/) { } 0 4 4
463 0 $self->{error} = 9;
464 0 $self->{errorString} = 'Nothing specified for the value ban';
465 0 $self->warn;
466 0 return;
467 } elsif ( ref( $opts{ban} ) ne '' ) {
468 0 $self->{error} = 10;
469 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
470 0 $self->warn;
471 0 return;
472 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
l!l&&r!l&&!r
310
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
ABresult
1X1
011
000
473 && $opts{ban} !~ /\A$IPv6_re\z/ )
474 {
475 0 $self->{error} = 10;
476 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
477 0 $self->warn;
478 0 return;
479 }
480  
481 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
482 4 $opts{ban} = lc( $opts{ban} );
483  
484 4 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 4 4
485 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
486 0 $self->{frontend_obj}->{test_data} = 'already banned';
487 }
488 0 return;
489 }
490  
491 4 $self->{banned}{ $opts{ban} } = 1;
492  
493 4 my $command = $self->_update_command;
494 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 4 0 4
495 4 $self->{frontend_obj}->{test_data} = $command;
496 } else {
497 0 $self->_run( $command, 13 );
498 }
499 } ## end sub ban
500  
501 =head2 unban
502  
503 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
504 and lowercased, then removed from the ban list. The rule's source prefixes
505 are re-rendered without it via C<az network nsg rule update>. Unbanning an
506 IP that is not banned is a noop.
507  
508 $fw_helper->unban( ban => $ip );
509  
510 =cut
511  
512 0 ▶sub unban {
Subroutine unban: not called CC=9 SCAR=45.0
Pod documented
513 0 my ( $self, %opts ) = @_;
514  
515 0 $self->errorblank;
516  
517 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
518 0 $self->{error} = 1;
519 0 $self->{errorString} = 'backend has not been inited';
520 0 $self->warn;
521 0 return;
522 }
523  
524 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::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/) { } 0 0 0
525 0 $self->{error} = 9;
526 0 $self->{errorString} = 'Nothing specified for the value ban';
527 0 $self->warn;
528 0 return;
529 } elsif ( ref( $opts{ban} ) ne '' ) {
530 0 $self->{error} = 10;
531 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
532 0 $self->warn;
533 0 return;
534 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::azure::IPv6_re\z/
ABresult
1X1
011
000
535 && $opts{ban} !~ /\A$IPv6_re\z/ )
536 {
537 0 $self->{error} = 10;
538 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
539 0 $self->warn;
540 0 return;
541 }
542  
543 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
544 0 $opts{ban} = lc( $opts{ban} );
545  
546 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
547 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
548 0 $self->{frontend_obj}->{test_data} = 'not banned';
549 }
550 0 return;
551 }
552  
553 0 delete( $self->{banned}{ $opts{ban} } );
554  
555 0 my $command = $self->_update_command;
556 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
557 0 $self->{frontend_obj}->{test_data} = $command;
558 } else {
559 0 $self->_run( $command, 14 );
560 }
561 } ## end sub unban
562  
563 =head2 ban_cidr
564  
565 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
566 CIDR and lowercased, then added verbatim to the rule's source prefixes via
567 C<az network nsg rule update>. Banning an already banned range is a noop.
568  
569 $fw_helper->ban_cidr( ban => '1.2.3.0/24' );
570  
571 =cut
572  
573 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=8 SCAR=32.0
Pod documented
574 1 my ( $self, %opts ) = @_;
575  
576 1 $self->errorblank;
577  
578 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
579 0 $self->{error} = 1;
580 0 $self->{errorString} = 'backend has not been inited';
581 0 $self->warn;
582 0 return;
583 }
584  
585 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
586 0 $self->{error} = 9;
587 0 $self->{errorString} = 'Nothing specified for the value ban';
588 0 $self->warn;
589 0 return;
590 } elsif ( ref( $opts{ban} ) ne '' ) {
591 0 $self->{error} = 35;
592 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
593 0 $self->warn;
594 0 return;
595 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
596 0 $self->{error} = 35;
597 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
598 0 $self->warn;
599 0 return;
600 }
601  
602 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
603 1 $opts{ban} = lc( $opts{ban} );
604  
605 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
606 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
607 0 $self->{frontend_obj}->{test_data} = 'already banned';
608 }
609 0 return;
610 }
611  
612 1 $self->{banned_cidr}{ $opts{ban} } = 1;
613  
614 1 my $command = $self->_update_command;
615 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
616 1 $self->{frontend_obj}->{test_data} = $command;
617 } else {
618 0 $self->_run( $command, 33 );
619 }
620 } ## end sub ban_cidr
621  
622 =head2 unban_cidr
623  
624 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
625 CIDR and lowercased, then the rule's source prefixes are re-rendered
626 without it via C<az network nsg rule update>. Unbanning a range that is not
627 banned is a noop.
628  
629 $fw_helper->unban_cidr( ban => '1.2.3.0/24' );
630  
631 =cut
632  
633 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=8 SCAR=32.0
Pod documented
634 1 my ( $self, %opts ) = @_;
635  
636 1 $self->errorblank;
637  
638 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
639 0 $self->{error} = 1;
640 0 $self->{errorString} = 'backend has not been inited';
641 0 $self->warn;
642 0 return;
643 }
644  
645 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
646 0 $self->{error} = 9;
647 0 $self->{errorString} = 'Nothing specified for the value ban';
648 0 $self->warn;
649 0 return;
650 } elsif ( ref( $opts{ban} ) ne '' ) {
651 0 $self->{error} = 35;
652 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
653 0 $self->warn;
654 0 return;
655 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
656 0 $self->{error} = 35;
657 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
658 0 $self->warn;
659 0 return;
660 }
661  
662 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
663 1 $opts{ban} = lc( $opts{ban} );
664  
665 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
666 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
667 0 $self->{frontend_obj}->{test_data} = 'not banned';
668 }
669 0 return;
670 }
671  
672 1 delete( $self->{banned_cidr}{ $opts{ban} } );
673  
674 1 my $command = $self->_update_command;
675 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
676 1 $self->{frontend_obj}->{test_data} = $command;
677 } else {
678 0 $self->_run( $command, 34 );
679 }
680 } ## end sub unban_cidr
681  
682 =head2 list_cidr
683  
684 List banned CIDR ranges.
685  
686 my @banned_cidrs = $fw_helper->list_cidr;
687  
688 =cut
689  
690 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
691 2 my ( $self, %opts ) = @_;
692  
693 2 $self->errorblank;
694  
695 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
696 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
697 }
698  
699 2 return keys( %{ $self->{banned_cidr} } );
700 } ## end sub list_cidr
701  
702 =head2 list
703  
704 List banned IPs. Returns an array of the currently banned single IPs from
705 the in-memory ban list; the rule is not queried. CIDR ranges are not
706 included; for those see L</list_cidr>.
707  
708 my @banned = $fw_helper->list;
709  
710 =cut
711  
712 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
713 0 my ( $self, %opts ) = @_;
714  
715 0 $self->errorblank;
716  
717 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
718 0 $self->{frontend_obj}->{test_data} = 'list';
719 }
720  
721 0 return keys( %{ $self->{banned} } );
722 } ## end sub list
723  
724 =head2 re_init
725  
726 Tears down (errors ignored) and re-initiates the backend, then re-applies
727 the full retained set of banned IPs and CIDR ranges to the rule via
728 C<az network nsg rule update>.
729  
730 =cut
731  
732 0 ▶sub re_init {
Subroutine re_init: not called CC=2 SCAR=17.9
Pod documented
733 0 my ( $self, %opts ) = @_;
734  
735 0 $self->errorblank;
736  
737 {
738 0 local $@;
739 0 eval { $self->teardown; };
740 }
741 0 $self->init;
742  
743 0 my $command = $self->_update_command;
744 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
745 0 $self->{frontend_obj}->{test_data} = [$command];
746 } else {
747 0 $self->_run( $command, 13 );
748 }
749  
750 0 $self->{inited} = 1;
751 } ## end sub re_init
752  
753 =head2 teardown
754  
755 Empties the rule's source prefixes. The internal ban list is kept so a
756 following re_init restores them.
757  
758 =cut
759  
760 0 ▶sub teardown {
Subroutine teardown: not called CC=2 SCAR=17.9
Pod documented
761 0 my ( $self, %opts ) = @_;
762  
763 0 $self->errorblank;
764  
765 0 $self->{inited} = 0;
766  
767 # render the update with an empty ban set without disturbing the retained
768 # lists, so a following re_init restores both single IPs and CIDR ranges
769 0 my %saved = %{ $self->{banned} };
770 0 my %saved_cidr = %{ $self->{banned_cidr} };
771 0 $self->{banned} = {};
772 0 $self->{banned_cidr} = {};
773 0 my $command = $self->_update_command;
774 0 $self->{banned} = \%saved;
775 0 $self->{banned_cidr} = \%saved_cidr;
776  
777 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
778 0 $self->{frontend_obj}->{test_data} = [$command];
779 } else {
780 0 $self->_run( $command, 17 );
781 }
782 } ## end sub teardown
783  
784 =head2 stop
785  
786 Alias for L</teardown>.
787  
788 =cut
789  
790 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
791 0 my ( $self, %opts ) = @_;
792  
793 0 return $self->teardown(%opts);
794 }
795  
796 =head2 check
797  
798 Verifies the rule still exists by running C<az network nsg rule show>.
799 Returns 1 if the command exits zero and 0 otherwise.
800  
801 =cut
802  
803 6 ▶sub check {
Subroutine check: called CC=3 SCAR=13.0
Pod documented
804 6 my ( $self, %opts ) = @_;
805  
806 6 $self->errorblank;
807  
808 6 my $command = $self->_show_command;
809  
810 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
811 6 $self->{frontend_obj}->{test_data} = [$command];
812 6 return 1;
813 }
814  
815 0 my $output = `$command 2>&1`;
816 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
817 } ## end sub check
818  
819 =head2 flush
820  
821 Removes all bans at once by clearing both the single IP and CIDR ban lists
822 and re-rendering the rule's now empty source prefixes via
823 C<az network nsg rule update>.
824  
825 =cut
826  
827 0 ▶sub flush {
Subroutine flush: not called CC=3 SCAR=24.8
Pod documented
828 0 my ( $self, %opts ) = @_;
829  
830 0 $self->errorblank;
831  
832 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
833 0 $self->{error} = 1;
834 0 $self->{errorString} = 'backend has not been inited';
835 0 $self->warn;
836 0 return;
837 }
838  
839 # the rule is re-rendered from both sets, so clear both to empty it
840 0 $self->{banned} = {};
841 0 $self->{banned_cidr} = {};
842  
843 0 my $command = $self->_update_command;
844 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
845 0 $self->{frontend_obj}->{test_data} = [$command];
846 } else {
847 0 $self->_run( $command, 25 );
848 }
849 } ## end sub flush
850  
851 =head1 ERROR CODES / FLAGS
852  
853 Error handling is provided by L<Error::Helper>. All
854 errors are considered fatal.
855  
856 =head2 1, notInited
857  
858 The backend has not been inited yet.
859  
860 =head2 8, optionsNotHash
861  
862 The item passed to new for options is not a hash.
863  
864 =head2 9, noBanItem
865  
866 No IP or CIDR range specified to ban or unban.
867  
868 =head2 10, banItemNotIP
869  
870 The item to ban is not an IP. Either wrong ref type or regexp
871 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
872  
873 =head2 12, backendInitError
874  
875 Failed to init the backend.
876  
877 =head2 13, banFailed
878  
879 Failed to ban the item.
880  
881 =head2 14, unbanFailed
882  
883 Failed to unban the item.
884  
885 =head2 15, listFailed
886  
887 Failed to get a list of bans.
888  
889 =head2 16, reInitFailed
890  
891 Failed to re_init the backend.
892  
893 =head2 17, teardownFailed
894  
895 Failed to teardown the backend.
896  
897 =head2 18, alreadyInited
898  
899 init called, but the backend has already been inited.
900  
901 =head2 24, checkFailed
902  
903 The backend check raised an error.
904  
905 =head2 25, flushFailed
906  
907 Failed to flush the bans.
908  
909 =head2 30, resourceGroupNotDefined
910  
911 The option resource_group is undef or blank.
912  
913 =head2 31, nsgNotDefined
914  
915 The option nsg is undef or blank.
916  
917 =head2 32, ruleNotDefined
918  
919 The option rule is undef or blank.
920  
921 =head2 33, banCidrFailed
922  
923 Failed to ban the CIDR range.
924  
925 =head2 34, unbanCidrFailed
926  
927 Failed to unban the CIDR range.
928  
929 =head2 35, cidrItemNotCidr
930  
931 The item to ban is not a CIDR range. Either wrong ref type or it is not an
932 IPv4 or IPv6 address followed by a prefix length valid for its family.
933  
934 =head2 36, cidrNotSupported
935  
936 The backend does not support CIDR bans.
937  
938 =head2 37, listCidrFailed
939  
940 Failed to get a list of CIDR bans.
941  
942 =head1 AUTHOR
943  
944 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
945  
946 =head1 LICENSE AND COPYRIGHT
947  
948 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
949  
950 This is free software, licensed under:
951  
952 The GNU Lesser General Public License, Version 2.1, February 1999
953  
954 =cut
955  
956 1 1; # End of Net::Firewall::BlockerHelper::backends::azure
« BlockerHelper/backends/xdp.pm ↑ Summary BlockerHelper/backends/hosts_deny.pm »