| 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 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::cloud_armor - Google Cloud Armor backend via the gcloud 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 => '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 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 89 | 4 | my ( $blank, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 142 | 4 | $self->{testing} = $opts{testing}; | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | 4 | ▶ | if ( defined( $opts{frontend_obj} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 4 | $self->{frontend_obj} = $opts{frontend_obj}; | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | 4 | ▶ | if ( defined( $opts{name} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | 4 | $self->{name} = $opts{name}; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 4 | ▶ | if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
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"
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $self->{"options"}{"policy"} or $self->{"options"}{"policy"} eq ""
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 ""
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | $self->{options}{priority} = 1000 if ( !defined( $self->{options}{priority} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 171 | } ## end sub new | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | # Internal helper. Returns the --project argument to append to every gcloud | ||||||||||||||||||||||||||||||||
| 174 | # invocation, or nothing when no project is configured. | ||||||||||||||||||||||||||||||||
| 175 | # | ||||||||||||||||||||||||||||||||
| 176 | # Emitting the flag only when the option is set is deliberate rather than | ||||||||||||||||||||||||||||||||
| 177 | # defaulting to one here. With it left off, gcloud uses whichever project is | ||||||||||||||||||||||||||||||||
| 178 | # set in the active configuration, which is what an instance running in an | ||||||||||||||||||||||||||||||||
| 179 | # already configured environment wants. It only needs specifying on a host | ||||||||||||||||||||||||||||||||
| 180 | # whose account can see several projects and whose default is not the right | ||||||||||||||||||||||||||||||||
| 181 | # one. | ||||||||||||||||||||||||||||||||
| 182 | # | ||||||||||||||||||||||||||||||||
| 183 | # Takes no arguments. | ||||||||||||||||||||||||||||||||
| 184 | # | ||||||||||||||||||||||||||||||||
| 185 | # Returns the argument as a string with a leading space, ready to be | ||||||||||||||||||||||||||||||||
| 186 | # concatenated onto a command, or the empty string when the project option is | ||||||||||||||||||||||||||||||||
| 187 | # unset or empty. Never returns undef. | ||||||||||||||||||||||||||||||||
| 188 | # | ||||||||||||||||||||||||||||||||
| 189 | # # with the project option set | ||||||||||||||||||||||||||||||||
| 190 | # $self->_suffix; # ' --project my-project' | ||||||||||||||||||||||||||||||||
| 191 | # | ||||||||||||||||||||||||||||||||
| 192 | # # with no project configured | ||||||||||||||||||||||||||||||||
| 193 | # $self->_suffix; # '' | ||||||||||||||||||||||||||||||||
| 194 | 21 | ▶ | sub _suffix { | ||||||||||||||||||||||||||||||
|
Subroutine
_suffix: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 195 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 21 | ▶ | if ( defined( $self->{options}{project} ) && $self->{options}{project} ne '' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"options"}{"project"} and $self->{"options"}{"project"} ne ""
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 ""
| |||||||||||||||||||||||||||||||||
| 198 | 3 | return ' --project ' . $self->{options}{project}; | |||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | 18 | return ''; | |||||||||||||||||||||||||||||||
| 201 | } ## end sub _suffix | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | # Internal helper. Renders the current bans into the source range list that | ||||||||||||||||||||||||||||||||
| 204 | # the Cloud Armor rule update takes. | ||||||||||||||||||||||||||||||||
| 205 | # | ||||||||||||||||||||||||||||||||
| 206 | # A Cloud Armor rule's source ranges are replace-in-full: there is no add or | ||||||||||||||||||||||||||||||||
| 207 | # remove one range call, so every ban and unban rewrites the whole list from | ||||||||||||||||||||||||||||||||
| 208 | # the internal state. That is what this produces. | ||||||||||||||||||||||||||||||||
| 209 | # | ||||||||||||||||||||||||||||||||
| 210 | # Everything is emitted as CIDR, so a single address gets an explicit host | ||||||||||||||||||||||||||||||||
| 211 | # prefix appended, /32 or /128 by family. Banned ranges already carry a prefix | ||||||||||||||||||||||||||||||||
| 212 | # and go out as they are. Both families go into the same rule. | ||||||||||||||||||||||||||||||||
| 213 | # | ||||||||||||||||||||||||||||||||
| 214 | # Note the separator is a comma rather than a space, unlike the AWS and Azure | ||||||||||||||||||||||||||||||||
| 215 | # backends. That is what --src-ip-ranges expects, and it means the whole list | ||||||||||||||||||||||||||||||||
| 216 | # is one shell word, so no quoting is needed around it. | ||||||||||||||||||||||||||||||||
| 217 | # | ||||||||||||||||||||||||||||||||
| 218 | # Single addresses and ranges are pulled from two separate internal lists and | ||||||||||||||||||||||||||||||||
| 219 | # emitted as two sorted runs rather than one merged sorted list, so the | ||||||||||||||||||||||||||||||||
| 220 | # addresses come first and the ranges after. That is stable, which is what | ||||||||||||||||||||||||||||||||
| 221 | # matters for the test_data comparisons, just not globally sorted. | ||||||||||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||||||||||
| 223 | # Takes no arguments; the bans come from the object's banned and banned_cidr | ||||||||||||||||||||||||||||||||
| 224 | # hashes. | ||||||||||||||||||||||||||||||||
| 225 | # | ||||||||||||||||||||||||||||||||
| 226 | # Returns the ranges as a single comma joined string, ready to follow | ||||||||||||||||||||||||||||||||
| 227 | # --src-ip-ranges on the command line. Returns the empty string when nothing | ||||||||||||||||||||||||||||||||
| 228 | # is banned. | ||||||||||||||||||||||||||||||||
| 229 | # | ||||||||||||||||||||||||||||||||
| 230 | # # with 10.0.0.1, 2001:db8::1, and 10.0.0.0/8 banned | ||||||||||||||||||||||||||||||||
| 231 | # $self->_ranges; | ||||||||||||||||||||||||||||||||
| 232 | # # '10.0.0.1/32,2001:db8::1/128,10.0.0.0/8' | ||||||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||||||
| 234 | # # with nothing banned | ||||||||||||||||||||||||||||||||
| 235 | # $self->_ranges; # '' | ||||||||||||||||||||||||||||||||
| 236 | 11 | ▶ | sub _ranges { | ||||||||||||||||||||||||||||||
|
Subroutine
_ranges: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 237 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | 11 | my @ranges; | |||||||||||||||||||||||||||||||
| 240 | 11 | foreach my $ip ( sort( keys( %{ $self->{banned} } ) ) ) { | |||||||||||||||||||||||||||||||
| 241 | 19 | ▶ | push( @ranges, $ip . ( ( $ip =~ /\A$IPv4_re\z/ ) ? '/32' : '/128' ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | # CIDR ranges already carry a prefix, so they are emitted verbatim | ||||||||||||||||||||||||||||||||
| 245 | 11 | foreach my $cidr ( sort( keys( %{ $self->{banned_cidr} } ) ) ) { | |||||||||||||||||||||||||||||||
| 246 | 2 | push( @ranges, $cidr ); | |||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 11 | return join( ',', @ranges ); | |||||||||||||||||||||||||||||||
| 250 | } ## end sub _ranges | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | # Internal helper. Returns the gcloud command that writes the current bans | ||||||||||||||||||||||||||||||||
| 253 | # into the Cloud Armor rule's source ranges. This is the command every ban, | ||||||||||||||||||||||||||||||||
| 254 | # unban, re_init, teardown, and flush ultimately runs. | ||||||||||||||||||||||||||||||||
| 255 | # | ||||||||||||||||||||||||||||||||
| 256 | # The rule is addressed by its priority within the policy rather than by a | ||||||||||||||||||||||||||||||||
| 257 | # name, since that is how Cloud Armor identifies rules. It is rewritten in | ||||||||||||||||||||||||||||||||
| 258 | # full each time, as gcloud offers no incremental range add or remove, and | ||||||||||||||||||||||||||||||||
| 259 | # there is no concurrency guard: two processes updating the same rule at once | ||||||||||||||||||||||||||||||||
| 260 | # will each write back a list based on their own state and the last one wins. | ||||||||||||||||||||||||||||||||
| 261 | # | ||||||||||||||||||||||||||||||||
| 262 | # Takes no arguments; the policy, priority, ranges, and project are all | ||||||||||||||||||||||||||||||||
| 263 | # assembled from the object. | ||||||||||||||||||||||||||||||||
| 264 | # | ||||||||||||||||||||||||||||||||
| 265 | # Returns the command as a single string ready to hand to the runner. The | ||||||||||||||||||||||||||||||||
| 266 | # gcloud binary comes from the gcloud_cmd option, 'gcloud' by default. | ||||||||||||||||||||||||||||||||
| 267 | # | ||||||||||||||||||||||||||||||||
| 268 | # # policy my-policy at priority 1000, with 10.0.0.1 and 10.0.0.0/8 banned | ||||||||||||||||||||||||||||||||
| 269 | # $self->_update_command; | ||||||||||||||||||||||||||||||||
| 270 | # # gcloud compute security-policies rules update 1000 --security-policy my-policy --src-ip-ranges 10.0.0.1/32,10.0.0.0/8 | ||||||||||||||||||||||||||||||||
| 271 | 11 | ▶ | sub _update_command { | ||||||||||||||||||||||||||||||
|
Subroutine
_update_command: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 272 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | return | ||||||||||||||||||||||||||||||||
| 275 | $self->{options}{gcloud_cmd} | ||||||||||||||||||||||||||||||||
| 276 | . ' compute security-policies rules update ' | ||||||||||||||||||||||||||||||||
| 277 | . $self->{options}{priority} | ||||||||||||||||||||||||||||||||
| 278 | . ' --security-policy ' | ||||||||||||||||||||||||||||||||
| 279 | . $self->{options}{policy} | ||||||||||||||||||||||||||||||||
| 280 | 11 | . ' --src-ip-ranges ' | |||||||||||||||||||||||||||||||
| 281 | . $self->_ranges | ||||||||||||||||||||||||||||||||
| 282 | . $self->_suffix; | ||||||||||||||||||||||||||||||||
| 283 | } ## end sub _update_command | ||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | # Internal helper. Returns the gcloud command that confirms the Cloud Armor | ||||||||||||||||||||||||||||||||
| 286 | # rule exists, used by init and by check. | ||||||||||||||||||||||||||||||||
| 287 | # | ||||||||||||||||||||||||||||||||
| 288 | # Since this backend creates nothing, the rule existing at the configured | ||||||||||||||||||||||||||||||||
| 289 | # priority is the whole of its setup, and a rule deleted out from under a | ||||||||||||||||||||||||||||||||
| 290 | # running process is the failure this detects. init runs it so a misconfigured | ||||||||||||||||||||||||||||||||
| 291 | # policy or priority fails immediately rather than at the first ban, and check | ||||||||||||||||||||||||||||||||
| 292 | # runs it to decide whether the self heal path should re-push the current | ||||||||||||||||||||||||||||||||
| 293 | # bans. | ||||||||||||||||||||||||||||||||
| 294 | # | ||||||||||||||||||||||||||||||||
| 295 | # Only the exit status is looked at; nothing parses the rule definition that | ||||||||||||||||||||||||||||||||
| 296 | # comes back. | ||||||||||||||||||||||||||||||||
| 297 | # | ||||||||||||||||||||||||||||||||
| 298 | # Takes no arguments. | ||||||||||||||||||||||||||||||||
| 299 | # | ||||||||||||||||||||||||||||||||
| 300 | # Returns the command as a single string ready to hand to the runner, whose | ||||||||||||||||||||||||||||||||
| 301 | # exit status is 0 when the rule is there. | ||||||||||||||||||||||||||||||||
| 302 | # | ||||||||||||||||||||||||||||||||
| 303 | # $self->_describe_command; | ||||||||||||||||||||||||||||||||
| 304 | # # gcloud compute security-policies rules describe 1000 --security-policy my-policy | ||||||||||||||||||||||||||||||||
| 305 | # | ||||||||||||||||||||||||||||||||
| 306 | # # with the project option set, it is appended | ||||||||||||||||||||||||||||||||
| 307 | # # gcloud compute security-policies rules describe 1000 --security-policy my-policy --project my-project | ||||||||||||||||||||||||||||||||
| 308 | 10 | ▶ | sub _describe_command { | ||||||||||||||||||||||||||||||
|
Subroutine
_describe_command: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 309 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | return | ||||||||||||||||||||||||||||||||
| 312 | $self->{options}{gcloud_cmd} | ||||||||||||||||||||||||||||||||
| 313 | . ' compute security-policies rules describe ' | ||||||||||||||||||||||||||||||||
| 314 | . $self->{options}{priority} | ||||||||||||||||||||||||||||||||
| 315 | . ' --security-policy ' | ||||||||||||||||||||||||||||||||
| 316 | . $self->{options}{policy} | ||||||||||||||||||||||||||||||||
| 317 | 10 | . $self->_suffix; | |||||||||||||||||||||||||||||||
| 318 | } ## end sub _describe_command | ||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||
| 320 | # Internal helper. Runs one gcloud command, turns a non zero exit into an | ||||||||||||||||||||||||||||||||
| 321 | # error, and optionally undoes a speculative state change first. | ||||||||||||||||||||||||||||||||
| 322 | # | ||||||||||||||||||||||||||||||||
| 323 | # The rollback exists because of the order the ban path works in. The address | ||||||||||||||||||||||||||||||||
| 324 | # is added to the internal ban list before the command runs, since the command | ||||||||||||||||||||||||||||||||
| 325 | # is built from that list, so a failed push would otherwise leave the object | ||||||||||||||||||||||||||||||||
| 326 | # believing an address is banned when nothing was written. Handing in a | ||||||||||||||||||||||||||||||||
| 327 | # closure that removes it again keeps the internal state honest. | ||||||||||||||||||||||||||||||||
| 328 | # | ||||||||||||||||||||||||||||||||
| 329 | # It has to run before warn rather than after, because errors in this dist are | ||||||||||||||||||||||||||||||||
| 330 | # fatal by default: warn may not return, and anything after it would never | ||||||||||||||||||||||||||||||||
| 331 | # execute. | ||||||||||||||||||||||||||||||||
| 332 | # | ||||||||||||||||||||||||||||||||
| 333 | # Only the paths that add something pass a rollback. Unbanning removes from | ||||||||||||||||||||||||||||||||
| 334 | # the list before the command too, but leaving an address out of the list | ||||||||||||||||||||||||||||||||
| 335 | # after a failed unban is the safer of the two wrong answers, so those callers | ||||||||||||||||||||||||||||||||
| 336 | # pass nothing. | ||||||||||||||||||||||||||||||||
| 337 | # | ||||||||||||||||||||||||||||||||
| 338 | # stderr is folded into stdout so that whatever gcloud complained about ends | ||||||||||||||||||||||||||||||||
| 339 | # up in the errorString. The output is only used for that; nothing parses it. | ||||||||||||||||||||||||||||||||
| 340 | # | ||||||||||||||||||||||||||||||||
| 341 | # Args: | ||||||||||||||||||||||||||||||||
| 342 | # | ||||||||||||||||||||||||||||||||
| 343 | # command - The complete shell command to run, as a plain string, | ||||||||||||||||||||||||||||||||
| 344 | # normally from _update_command or _describe_command. Run | ||||||||||||||||||||||||||||||||
| 345 | # through the shell, so it is expected to be already quoted | ||||||||||||||||||||||||||||||||
| 346 | # as needed. | ||||||||||||||||||||||||||||||||
| 347 | # | ||||||||||||||||||||||||||||||||
| 348 | # error_flag - The numeric error code to raise on a non zero exit, so the | ||||||||||||||||||||||||||||||||
| 349 | # error reads as the operation that triggered it. Callers | ||||||||||||||||||||||||||||||||
| 350 | # pass the code matching themselves: 12 from init, 13 from | ||||||||||||||||||||||||||||||||
| 351 | # ban, 14 from unban, 31 and 32 from the CIDR paths. | ||||||||||||||||||||||||||||||||
| 352 | # | ||||||||||||||||||||||||||||||||
| 353 | # rollback - Optional coderef, called with no arguments if and only if | ||||||||||||||||||||||||||||||||
| 354 | # the command fails, before the error is raised. Expected to | ||||||||||||||||||||||||||||||||
| 355 | # undo whatever internal state change was made in | ||||||||||||||||||||||||||||||||
| 356 | # anticipation of the command succeeding. Omitted by callers | ||||||||||||||||||||||||||||||||
| 357 | # that have nothing to undo. | ||||||||||||||||||||||||||||||||
| 358 | # | ||||||||||||||||||||||||||||||||
| 359 | # Returns nothing, on success and on failure alike. | ||||||||||||||||||||||||||||||||
| 360 | # | ||||||||||||||||||||||||||||||||
| 361 | # # from init, just verifying the rule is there | ||||||||||||||||||||||||||||||||
| 362 | # $self->_run( $self->_describe_command, 12 ); | ||||||||||||||||||||||||||||||||
| 363 | # | ||||||||||||||||||||||||||||||||
| 364 | # # from ban, undoing the speculative add if the push fails | ||||||||||||||||||||||||||||||||
| 365 | # $self->_run( $command, 13, sub { delete( $self->{banned}{ $opts{ban} } ) } ); | ||||||||||||||||||||||||||||||||
| 366 | 2 | ▶ | sub _run { | ||||||||||||||||||||||||||||||
|
Subroutine
_run: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||
| 367 | 2 | my ( $self, $command, $error_flag, $rollback ) = @_; | |||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | 2 | my $output = `$command 2>&1`; | |||||||||||||||||||||||||||||||
| 370 | 2 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 371 | # errors are fatal, so any state rollback has to happen before warn | ||||||||||||||||||||||||||||||||
| 372 | 2 | ▶ | $rollback->() if ( defined($rollback) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 373 | 2 | $self->{error} = $error_flag; | |||||||||||||||||||||||||||||||
| 374 | 2 | $self->{errorString} = 'command "' . $command . '" failed... ' . $output; | |||||||||||||||||||||||||||||||
| 375 | 2 | $self->warn; | |||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | 0 | return; | |||||||||||||||||||||||||||||||
| 379 | } ## end sub _run | ||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||
| 381 | =head2 init | ||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||
| 383 | Initiates the backend, verifying the deny rule at the configured priority | ||||||||||||||||||||||||||||||||
| 384 | exists via C<gcloud compute security-policies rules describe>. | ||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | =cut | ||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | 3 | ▶ | sub init { | ||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 389 | 3 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | 3 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | 3 | ▶ | if ( $self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 394 | 0 | $self->{error} = 18; | |||||||||||||||||||||||||||||||
| 395 | 0 | $self->{errorString} = 'backend has already been inited'; | |||||||||||||||||||||||||||||||
| 396 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | 3 | my @commands = ( $self->_describe_command ); | |||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | 3 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 402 | 3 | $self->{frontend_obj}->{test_data} = \@commands; | |||||||||||||||||||||||||||||||
| 403 | } else { | ||||||||||||||||||||||||||||||||
| 404 | 0 | $self->_run( $commands[0], 12 ); | |||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | 3 | $self->{inited} = 1; | |||||||||||||||||||||||||||||||
| 408 | } ## end sub init | ||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | =head2 ban | ||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||
| 412 | Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address | ||||||||||||||||||||||||||||||||
| 413 | and lowercased, then added to the ban list. The rule's source ranges are | ||||||||||||||||||||||||||||||||
| 414 | then rewritten via C<gcloud compute security-policies rules update>, the IP | ||||||||||||||||||||||||||||||||
| 415 | included as a /32 (IPv4) or /128 (IPv6) range. Banning an already banned IP | ||||||||||||||||||||||||||||||||
| 416 | is a noop. | ||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||
| 418 | $fw_helper->ban( ban => $ip ); | ||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||
| 420 | =cut | ||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||
| 422 | 7 | ▶ | sub ban { | ||||||||||||||||||||||||||||||
|
Subroutine
ban: called CC=9 SCAR=31.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 423 | 7 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | 7 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | 7 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 428 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 429 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 430 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 431 | 0 | return; | |||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||
| 434 | 7 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 435 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 436 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 437 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 438 | 0 | return; | |||||||||||||||||||||||||||||||
| 439 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 440 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 441 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 442 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 443 | 0 | return; | |||||||||||||||||||||||||||||||
| 444 | ▶ | } 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/
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/
| |||||||||||||||||||||||||||||||||
| 445 | && $opts{ban} !~ /\A$IPv6_re\z/ ) | ||||||||||||||||||||||||||||||||
| 446 | { | ||||||||||||||||||||||||||||||||
| 447 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 448 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP'; | |||||||||||||||||||||||||||||||
| 449 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 450 | 0 | return; | |||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||
| 453 | # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 454 | 7 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | 7 | ▶ | if ( $self->{banned}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 457 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 458 | 0 | $self->{frontend_obj}->{test_data} = 'already banned'; | |||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||
| 460 | 0 | return; | |||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||
| 463 | 7 | $self->{banned}{ $opts{ban} } = 1; | |||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||
| 465 | 7 | my $command = $self->_update_command; | |||||||||||||||||||||||||||||||
| 466 | 7 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 467 | 6 | $self->{frontend_obj}->{test_data} = $command; | |||||||||||||||||||||||||||||||
| 468 | } else { | ||||||||||||||||||||||||||||||||
| 469 | 1 | ▶ | $self->_run( $command, 13, sub { delete( $self->{banned}{ $opts{ban} } ) } ); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||
| 471 | } ## end sub ban | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | =head2 unban | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address | ||||||||||||||||||||||||||||||||
| 476 | and lowercased, then removed from the ban list. The rule's source ranges are | ||||||||||||||||||||||||||||||||
| 477 | rewritten without it via C<gcloud compute security-policies rules update>. | ||||||||||||||||||||||||||||||||
| 478 | Unbanning an IP that is not banned is a noop. | ||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | $fw_helper->unban( ban => $ip ); | ||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||
| 482 | =cut | ||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||
| 484 | 1 | ▶ | sub unban { | ||||||||||||||||||||||||||||||
|
Subroutine
unban: called CC=9 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 485 | 1 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||
| 487 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||
| 489 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 490 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 491 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 492 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 493 | 0 | return; | |||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||
| 496 | 1 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 497 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 498 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 499 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 500 | 0 | return; | |||||||||||||||||||||||||||||||
| 501 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 502 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 503 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 504 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 505 | 0 | return; | |||||||||||||||||||||||||||||||
| 506 | ▶ | } 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/
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/
| |||||||||||||||||||||||||||||||||
| 507 | && $opts{ban} !~ /\A$IPv6_re\z/ ) | ||||||||||||||||||||||||||||||||
| 508 | { | ||||||||||||||||||||||||||||||||
| 509 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 510 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP'; | |||||||||||||||||||||||||||||||
| 511 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 512 | 0 | return; | |||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||
| 515 | # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 516 | 1 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||
| 518 | 1 | ▶ | if ( !$self->{banned}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 519 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 520 | 0 | $self->{frontend_obj}->{test_data} = 'not banned'; | |||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||
| 522 | 0 | return; | |||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||
| 525 | 1 | delete( $self->{banned}{ $opts{ban} } ); | |||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||
| 527 | 1 | my $command = $self->_update_command; | |||||||||||||||||||||||||||||||
| 528 | 1 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 529 | 1 | $self->{frontend_obj}->{test_data} = $command; | |||||||||||||||||||||||||||||||
| 530 | } else { | ||||||||||||||||||||||||||||||||
| 531 | 0 | $self->_run( $command, 14 ); | |||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||
| 533 | } ## end sub unban | ||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||
| 535 | =head2 ban_cidr | ||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||
| 537 | Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 | ||||||||||||||||||||||||||||||||
| 538 | CIDR range and lowercased, then added to the rule's source ranges via | ||||||||||||||||||||||||||||||||
| 539 | C<gcloud compute security-policies rules update>. Banning an already banned | ||||||||||||||||||||||||||||||||
| 540 | range is a noop. | ||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||
| 542 | $fw_helper->ban_cidr( ban => '1.2.3.0/24' ); | ||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | =cut | ||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||
| 546 | 2 | ▶ | sub ban_cidr { | ||||||||||||||||||||||||||||||
|
Subroutine
ban_cidr: called CC=8 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 547 | 2 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||
| 549 | 2 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||
| 551 | 2 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 552 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 553 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 554 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 555 | 0 | return; | |||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||
| 558 | 2 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 559 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 560 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 561 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 562 | 0 | return; | |||||||||||||||||||||||||||||||
| 563 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 564 | 0 | $self->{error} = 33; | |||||||||||||||||||||||||||||||
| 565 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 566 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 567 | 0 | return; | |||||||||||||||||||||||||||||||
| 568 | } elsif ( !$self->_valid_cidr( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||
| 569 | 0 | $self->{error} = 33; | |||||||||||||||||||||||||||||||
| 570 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR'; | |||||||||||||||||||||||||||||||
| 571 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 572 | 0 | return; | |||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||
| 575 | # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 576 | 2 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||
| 578 | 2 | ▶ | if ( $self->{banned_cidr}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 579 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 580 | 0 | $self->{frontend_obj}->{test_data} = 'already banned'; | |||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||
| 582 | 0 | return; | |||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||
| 585 | 2 | $self->{banned_cidr}{ $opts{ban} } = 1; | |||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | 2 | my $command = $self->_update_command; | |||||||||||||||||||||||||||||||
| 588 | 2 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 589 | 1 | $self->{frontend_obj}->{test_data} = $command; | |||||||||||||||||||||||||||||||
| 590 | } else { | ||||||||||||||||||||||||||||||||
| 591 | 1 | ▶ | $self->_run( $command, 31, sub { delete( $self->{banned_cidr}{ $opts{ban} } ) } ); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||
| 593 | } ## end sub ban_cidr | ||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||
| 595 | =head2 unban_cidr | ||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||
| 597 | Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 | ||||||||||||||||||||||||||||||||
| 598 | CIDR range and lowercased, then the rule's source ranges are rewritten | ||||||||||||||||||||||||||||||||
| 599 | without it. Unbanning a range that is not banned is a noop. | ||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||
| 601 | $fw_helper->unban_cidr( ban => '1.2.3.0/24' ); | ||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||
| 603 | =cut | ||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||
| 605 | 1 | ▶ | sub unban_cidr { | ||||||||||||||||||||||||||||||
|
Subroutine
unban_cidr: called CC=8 SCAR=32.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 606 | 1 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||
| 608 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||
| 610 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 611 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 612 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 613 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 614 | 0 | return; | |||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||
| 617 | 1 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 618 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 619 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 620 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 621 | 0 | return; | |||||||||||||||||||||||||||||||
| 622 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 623 | 0 | $self->{error} = 33; | |||||||||||||||||||||||||||||||
| 624 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 625 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 626 | 0 | return; | |||||||||||||||||||||||||||||||
| 627 | } elsif ( !$self->_valid_cidr( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||
| 628 | 0 | $self->{error} = 33; | |||||||||||||||||||||||||||||||
| 629 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR'; | |||||||||||||||||||||||||||||||
| 630 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 631 | 0 | return; | |||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||
| 634 | # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 635 | 1 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||
| 637 | 1 | ▶ | if ( !$self->{banned_cidr}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 638 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 639 | 0 | $self->{frontend_obj}->{test_data} = 'not banned'; | |||||||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||||||
| 641 | 0 | return; | |||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||
| 644 | 1 | delete( $self->{banned_cidr}{ $opts{ban} } ); | |||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||
| 646 | 1 | my $command = $self->_update_command; | |||||||||||||||||||||||||||||||
| 647 | 1 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 648 | 1 | $self->{frontend_obj}->{test_data} = $command; | |||||||||||||||||||||||||||||||
| 649 | } else { | ||||||||||||||||||||||||||||||||
| 650 | 0 | $self->_run( $command, 32 ); | |||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||
| 652 | } ## end sub unban_cidr | ||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||
| 654 | =head2 list_cidr | ||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||
| 656 | List banned CIDR ranges. Returns an array of the currently banned CIDR | ||||||||||||||||||||||||||||||||
| 657 | ranges. Single IPs are not included; for those see L</list>. | ||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||
| 659 | my @banned_cidrs = $fw_helper->list_cidr; | ||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||
| 661 | =cut | ||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||
| 663 | 2 | ▶ | sub list_cidr { | ||||||||||||||||||||||||||||||
|
Subroutine
list_cidr: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 664 | 2 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||
| 666 | 2 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||
| 668 | 2 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 669 | 2 | $self->{frontend_obj}->{test_data} = 'list_cidr'; | |||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||
| 672 | 2 | return keys( %{ $self->{banned_cidr} } ); | |||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||
| 675 | =head2 list | ||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||
| 677 | List banned IPs. Returns an array of the currently banned single IPs. CIDR | ||||||||||||||||||||||||||||||||
| 678 | ranges are not included; for those see L</list_cidr>. | ||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||
| 680 | my @banned = $fw_helper->list; | ||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||
| 682 | =cut | ||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||
| 684 | 0 | ▶ | sub list { | ||||||||||||||||||||||||||||||
|
Subroutine
list: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 685 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||
| 687 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||
| 689 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 690 | 0 | $self->{frontend_obj}->{test_data} = 'list'; | |||||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||
| 693 | 0 | return keys( %{ $self->{banned} } ); | |||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||
| 696 | =head2 re_init | ||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||
| 698 | Tears down and re-initiates, then re-applies the full retained banned set | ||||||||||||||||||||||||||||||||
| 699 | (single IPs and CIDR ranges) to the rule's source ranges with a single | ||||||||||||||||||||||||||||||||
| 700 | gcloud update. | ||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||
| 702 | =cut | ||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||
| 704 | 0 | ▶ | sub re_init { | ||||||||||||||||||||||||||||||
|
Subroutine
re_init: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 705 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||
| 707 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||
| 709 | { | ||||||||||||||||||||||||||||||||
| 710 | 0 | local $@; | |||||||||||||||||||||||||||||||
| 711 | 0 | eval { $self->teardown; }; | |||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||
| 713 | 0 | $self->init; | |||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||
| 715 | 0 | my $command = $self->_update_command; | |||||||||||||||||||||||||||||||
| 716 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 717 | 0 | $self->{frontend_obj}->{test_data} = [$command]; | |||||||||||||||||||||||||||||||
| 718 | } else { | ||||||||||||||||||||||||||||||||
| 719 | 0 | $self->_run( $command, 13 ); | |||||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||
| 722 | 0 | $self->{inited} = 1; | |||||||||||||||||||||||||||||||
| 723 | } ## end sub re_init | ||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||
| 725 | =head2 teardown | ||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||
| 727 | Empties the rule's source ranges. The internal ban list is kept so a | ||||||||||||||||||||||||||||||||
| 728 | following re_init restores them. | ||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||
| 730 | =cut | ||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||
| 732 | 0 | ▶ | sub teardown { | ||||||||||||||||||||||||||||||
|
Subroutine
teardown: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 733 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||
| 735 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||
| 737 | 0 | $self->{inited} = 0; | |||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||
| 739 | # render the update with an empty ban set without disturbing the retained lists | ||||||||||||||||||||||||||||||||
| 740 | 0 | my %saved = %{ $self->{banned} }; | |||||||||||||||||||||||||||||||
| 741 | 0 | my %saved_cidr = %{ $self->{banned_cidr} }; | |||||||||||||||||||||||||||||||
| 742 | 0 | $self->{banned} = {}; | |||||||||||||||||||||||||||||||
| 743 | 0 | $self->{banned_cidr} = {}; | |||||||||||||||||||||||||||||||
| 744 | 0 | my $command = $self->_update_command; | |||||||||||||||||||||||||||||||
| 745 | 0 | $self->{banned} = \%saved; | |||||||||||||||||||||||||||||||
| 746 | 0 | $self->{banned_cidr} = \%saved_cidr; | |||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||
| 748 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 749 | 0 | $self->{frontend_obj}->{test_data} = [$command]; | |||||||||||||||||||||||||||||||
| 750 | } else { | ||||||||||||||||||||||||||||||||
| 751 | 0 | $self->_run( $command, 17 ); | |||||||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||||||
| 753 | } ## end sub teardown | ||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||
| 755 | =head2 stop | ||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||
| 757 | Alias for L</teardown>. | ||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||
| 759 | =cut | ||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||
| 761 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 762 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||
| 764 | 0 | return $self->teardown(%opts); | |||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||
| 767 | =head2 check | ||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||
| 769 | Verifies the rule still exists via C<gcloud compute security-policies rules | ||||||||||||||||||||||||||||||||
| 770 | describe>. Returns a true value on a zero exit and a false value otherwise. | ||||||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||||||
| 772 | =cut | ||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||
| 774 | 7 | ▶ | sub check { | ||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=13.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 775 | 7 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||
| 777 | 7 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||
| 779 | 7 | my $command = $self->_describe_command; | |||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||
| 781 | 7 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 782 | 7 | $self->{frontend_obj}->{test_data} = [$command]; | |||||||||||||||||||||||||||||||
| 783 | 7 | return 1; | |||||||||||||||||||||||||||||||
| 784 | } | ||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||
| 786 | 0 | my $output = `$command 2>&1`; | |||||||||||||||||||||||||||||||
| 787 | 0 | ▶ | return $? == 0 ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 788 | } ## end sub check | ||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||
| 790 | =head2 flush | ||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||
| 792 | Removes all currently banned IPs and CIDR ranges at once by clearing the | ||||||||||||||||||||||||||||||||
| 793 | ban lists and rewriting the rule's source ranges empty with a single gcloud | ||||||||||||||||||||||||||||||||
| 794 | update. | ||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||
| 796 | =cut | ||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||
| 798 | 0 | ▶ | sub flush { | ||||||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 799 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||
| 801 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||
| 803 | 0 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 804 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 805 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 806 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 807 | 0 | return; | |||||||||||||||||||||||||||||||
| 808 | } | ||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||
| 810 | # the single update below rewrites the rule with both sets empty at once | ||||||||||||||||||||||||||||||||
| 811 | 0 | $self->{banned} = {}; | |||||||||||||||||||||||||||||||
| 812 | 0 | $self->{banned_cidr} = {}; | |||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||
| 814 | 0 | my $command = $self->_update_command; | |||||||||||||||||||||||||||||||
| 815 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 816 | 0 | $self->{frontend_obj}->{test_data} = [$command]; | |||||||||||||||||||||||||||||||
| 817 | } else { | ||||||||||||||||||||||||||||||||
| 818 | 0 | $self->_run( $command, 25 ); | |||||||||||||||||||||||||||||||
| 819 | } | ||||||||||||||||||||||||||||||||
| 820 | } ## end sub flush | ||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||
| 822 | =head1 ERROR CODES / FLAGS | ||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||
| 824 | Error handling is provided by L<Error::Helper>. All | ||||||||||||||||||||||||||||||||
| 825 | errors are considered fatal. | ||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||
| 827 | =head2 1, notInited | ||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||
| 829 | The backend has not been inited yet. | ||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||
| 831 | =head2 8, optionsNotHash | ||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||
| 833 | The item passed to new for options is not a hash. | ||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||
| 835 | =head2 9, noBanItem | ||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||
| 837 | No IP or CIDR range specified to ban or unban. | ||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||
| 839 | =head2 10, banItemNotIP | ||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||
| 841 | The item to ban is not an IP. Either wrong ref type or regexp | ||||||||||||||||||||||||||||||||
| 842 | test using L<Regexp::IPv4> and L<Regexp::IPv6> failed. | ||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||
| 844 | =head2 12, backendInitError | ||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||
| 846 | Failed to init the backend. | ||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||
| 848 | =head2 13, banFailed | ||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||
| 850 | Failed to ban the item. | ||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||
| 852 | =head2 14, unbanFailed | ||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||
| 854 | Failed to unban the item. | ||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||
| 856 | =head2 15, listFailed | ||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||
| 858 | Failed to get a list of bans. | ||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||
| 860 | =head2 16, reInitFailed | ||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||
| 862 | Failed to re_init the backend. | ||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||
| 864 | =head2 17, teardownFailed | ||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||
| 866 | Failed to teardown the backend. | ||||||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||||||
| 868 | =head2 18, alreadyInited | ||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||
| 870 | init called, but the backend has already been inited. | ||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||
| 872 | =head2 24, checkFailed | ||||||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||||||
| 874 | The backend check raised an error. | ||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||
| 876 | =head2 25, flushFailed | ||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||
| 878 | Failed to flush the bans. | ||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||
| 880 | =head2 30, policyNotDefined | ||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||
| 882 | The option policy is undef or blank. | ||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||
| 884 | =head2 31, banCidrFailed | ||||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||||
| 886 | Failed to ban the CIDR range. | ||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||
| 888 | =head2 32, unbanCidrFailed | ||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||
| 890 | Failed to unban the CIDR range. | ||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||
| 892 | =head2 33, cidrItemNotCidr | ||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||
| 894 | The item to ban is not a CIDR range. Either wrong ref type or it is not an | ||||||||||||||||||||||||||||||||
| 895 | IPv4 or IPv6 address followed by a prefix length valid for its family. | ||||||||||||||||||||||||||||||||
| 896 | |||||||||||||||||||||||||||||||||
| 897 | =head2 34, cidrNotSupported | ||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||
| 899 | The backend does not support CIDR bans. | ||||||||||||||||||||||||||||||||
| 900 | |||||||||||||||||||||||||||||||||
| 901 | =head2 35, listCidrFailed | ||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||
| 903 | Failed to get a list of CIDR bans. | ||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||
| 905 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||
| 907 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >> | ||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||
| 909 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||
| 911 | This software is Copyright (c) 2023 by Zane C. Bowers-Hadley. | ||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||
| 913 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||
| 915 | The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||
| 917 | =cut | ||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||
| 919 | 1 | 1; # End of Net::Firewall::BlockerHelper::backends::cloud_armor | |||||||||||||||||||||||||||||||