| 1 | package App::FargateStack::Builder::WafV2; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use App::WafV2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use App::FargateStack::Builder::Utils qw(dmp slurp_file log_die display_diffs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use App::FargateStack::Constants; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use CLI::Simple::Constants qw(:booleans :chars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use English qw(no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ######################################################################## | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub managed_rules { | ||||||||||||||||||||||||||
|
Subroutine
managed_rules: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | ######################################################################## | ||||||||||||||||||||||||||||
| 22 | 0 | my ( $self, $managed_rules ) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | my @remove_list; | |||||||||||||||||||||||||||
| 25 | my @managed_rule_list; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | foreach my $rule_set ( @{$managed_rules} ) { | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ( $rule_set =~ /^[\-](.*)$/xsmi ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | push @remove_list, $1; | |||||||||||||||||||||||||||
| 30 | 0 | next; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( $WAF_MANAGED_RULE_BUNDLES{$rule_set} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | 0 | foreach ( @{ $WAF_MANAGED_RULE_BUNDLES{$rule_set} } ) { | |||||||||||||||||||||||||||
| 35 | 0 | push @managed_rule_list, @{ $WAF_MANAGED_RULES{$_} }; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | elsif ( $WAF_MANAGED_RULES{$rule_set} ) { | ||||||||||||||||||||||||||||
| 39 | 0 | push @managed_rule_list, @{ $WAF_MANAGED_RULES{$rule_set} }; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||
| 42 | 0 | log_die( $self, 'ERROR: no such managed rule: %s', $rule_set ); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | foreach my $rule (@remove_list) { | |||||||||||||||||||||||||||
| 47 | 0 | @managed_rule_list = grep { $rule ne $_ } @managed_rule_list; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return \@managed_rule_list; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ######################################################################## | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub create_rule_list { | ||||||||||||||||||||||||||
|
Subroutine
create_rule_list: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | ######################################################################## | ||||||||||||||||||||||||||||
| 56 | 0 | my ( $self, $waf_config ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my @rule_list; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my $priority = 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $rule_stub = decode_json($WAF_RULE_STUB); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ( !$waf_config->{managed_rules} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $waf_config->{managed_rules} = [qw(default)]; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $managed_rules = $self->managed_rules( $waf_config->{managed_rules} ); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | foreach my $metric_name ( @{$managed_rules} ) { | |||||||||||||||||||||||||||
| 71 | 0 | my $rule = dclone $rule_stub; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | $rule->{Name} = $metric_name; | |||||||||||||||||||||||||||
| 74 | 0 | $rule->{Statement}->{ManagedRuleGroupStatement}->{Name} = $metric_name; | |||||||||||||||||||||||||||
| 75 | 0 | $rule->{VisibilityConfig}->{MetricName} = $metric_name; | |||||||||||||||||||||||||||
| 76 | 0 | $rule->{Priority} = $priority++; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | push @rule_list, $rule; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | return \@rule_list; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | ######################################################################## | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub build_waf { | ||||||||||||||||||||||||||
|
Subroutine
build_waf: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | ######################################################################## | ||||||||||||||||||||||||||||
| 87 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | ###################################################################### | ||||||||||||||||||||||||||||
| 90 | ## init | ||||||||||||||||||||||||||||
| 91 | ###################################################################### | ||||||||||||||||||||||||||||
| 92 | 0 | my $config = $self->get_config; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $dryrun = $self->get_dryrun; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # if no alb or no waf section skip this build (avoid autovivification) | ||||||||||||||||||||||||||||
| 97 | return | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if !exists $config->{alb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my $alb = $config->{alb}; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | return | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if !exists $alb->{waf}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $waf_config = $alb->{waf}; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | return | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if !$waf_config->{enabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | log_die( $self, 'ERROR: no ALB ARN? you cannot have a WAF without an ALB' ) | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if !$alb->{arn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | $waf_config->{name} //= $self->create_default('web-acl-name'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$waf_config->{"name"} //= $self->create_default("web-acl-name")
MC/DC0%0/2
$waf_config->{"name"} //= $self->create_default("web-acl-name")
$waf_config->{"name"}
$self->create_default("web-acl-name")
Truth table0%0/3
A //= B
A= $waf_config->{"name"}B= $self->create_default("web-acl-name")
| |||||||||||||||||||||||||||||
| 114 | 0 | my $name = $waf_config->{name}; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my $waf = $self->fetch_wafv2; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | ###################################################################### | ||||||||||||||||||||||||||||
| 119 | ## create or update web-acl | ||||||||||||||||||||||||||||
| 120 | ###################################################################### | ||||||||||||||||||||||||||||
| 121 | 0 | my $web_acl = $waf->list_web_acls( | |||||||||||||||||||||||||||
| 122 | scope => 'REGIONAL', | ||||||||||||||||||||||||||||
| 123 | query => sprintf 'WebACLs[?Name==`%s`]', | ||||||||||||||||||||||||||||
| 124 | $name | ||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | $waf->check_result( message => 'ERROR: could not list web acls' ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( $web_acl && @{$web_acl} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$web_acl and @{$web_acl;}
MC/DC0%0/2
$web_acl and @{$web_acl;}
$web_acl
@{$web_acl;}
Truth table0%0/3
A and B
A= $web_aclB= @{$web_acl;}
| |||||||||||||||||||||||||||||
| 130 | 0 | my $web_acl_id = $web_acl->[0]->{Id}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | $web_acl = $waf->get_web_acl( name => $name, id => $web_acl_id ); | |||||||||||||||||||||||||||
| 133 | 0 | $waf->check_result( message => 'ERROR: could not get web-acl: [%s]', $name ); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my $web_acl_arn = $web_acl->{WebACL}->{ARN}; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # check to see if rules have been updated | ||||||||||||||||||||||||||||
| 138 | 0 | my $new_rules = $self->check_rules( waf => $waf, waf_config => $waf_config ); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # check to see if someone has mucked with web-acl.json | ||||||||||||||||||||||||||||
| 141 | 0 | my $needs_update = $self->check_web_acl_state( | |||||||||||||||||||||||||||
| 142 | name => $name, | ||||||||||||||||||||||||||||
| 143 | id => $web_acl_id, | ||||||||||||||||||||||||||||
| 144 | waf_config => $waf_config, | ||||||||||||||||||||||||||||
| 145 | waf => $waf, | ||||||||||||||||||||||||||||
| 146 | web_acl => $web_acl, | ||||||||||||||||||||||||||||
| 147 | rules => $new_rules, | ||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # we would have died already if there was a conflict, so now we | ||||||||||||||||||||||||||||
| 151 | # have either FALSE = no update needed or TRUE = need to update web-acl | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($needs_update) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $self->log_warn( 'waf: web-acl: [%s] has changed...will be updated...%s', $name, $dryrun ); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ( !$dryrun ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $self->update_web_acl( $waf_config->{lock_token} ); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | $self->save_web_acl( | ||||||||||||||||||||||||||||
| 159 | id => $waf_config->{id}, | ||||||||||||||||||||||||||||
| 160 | 0 | scope => 'REGIONAL', | |||||||||||||||||||||||||||
| 161 | name => $name, | ||||||||||||||||||||||||||||
| 162 | waf_config => $waf_config, | ||||||||||||||||||||||||||||
| 163 | waf => $waf, | ||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||
| 168 | 0 | $self->log_info( 'waf: web-acl: [%s] has not changed...skipping', $name ); | |||||||||||||||||||||||||||
| 169 | 0 | $self->inc_existing_resources( waf => $waf_config->{arn} ); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $arns = $waf->list_resources_for_web_acl( $web_acl_arn, 'ResourceArns' ); | |||||||||||||||||||||||||||
| 173 | 0 | $waf->check_result( message => 'ERROR: could not list resources for web acl: [%s]', $web_acl_arn ); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ( !@{$arns} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | $self->log_warn( 'waf: web-acl: [%s] not associated...will be associated with ALB: [%s]...%s', | ||||||||||||||||||||||||||||
| 177 | 0 | $name, $alb->{name}, $dryrun ); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( !$dryrun ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | $self->associate_web_acl( | ||||||||||||||||||||||||||||
| 181 | web_acl_arn => $web_acl_arn, | ||||||||||||||||||||||||||||
| 182 | resource_arn => $alb->{arn}, | ||||||||||||||||||||||||||||
| 183 | 0 | waf => $waf, | |||||||||||||||||||||||||||
| 184 | name => $name | ||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||
| 190 | 0 | $self->log_warn( 'waf: web-acl: [%s] does not exist...will be created...%s', $name, $dryrun ); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | $self->inc_required_resources( | ||||||||||||||||||||||||||||
| 193 | waf => [ | ||||||||||||||||||||||||||||
| 194 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 195 | 0 | my ($dryrun) = @_; | |||||||||||||||||||||||||||
| 196 | 0 | ▶ | return $dryrun ? 'arn:???' : $waf_config->{arn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 0 | ] | |||||||||||||||||||||||||||
| 199 | ); | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | my $rules = $self->create_rule_list($waf_config); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | $self->log_warn( 'waf: rule list from: [%s]', join q{,}, @{ $waf_config->{managed_rules} } ); | |||||||||||||||||||||||||||
| 204 | 0 | $self->log_debug( "waf: managed rules: %s\n", join "\n", Dumper $rules ); | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ( !$dryrun ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | $self->create_web_acl( | |||||||||||||||||||||||||||
| 208 | name => $name, | ||||||||||||||||||||||||||||
| 209 | waf_config => $waf_config, | ||||||||||||||||||||||||||||
| 210 | waf => $waf, | ||||||||||||||||||||||||||||
| 211 | rules => $rules | ||||||||||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | return $SUCCESS; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | ######################################################################## | ||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub check_rules { | ||||||||||||||||||||||||||
|
Subroutine
check_rules: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 221 | ######################################################################## | ||||||||||||||||||||||||||||
| 222 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | my ( $waf, $waf_config ) = @args{qw(waf waf_config web_acl)}; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | my $web_acl = $self->fetch_web_acl; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | my $current_rules = $web_acl->{WebACL}->{Rules}; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | my $rules = $self->create_rule_list($waf_config); | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | return $rules | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | if JSON->new->pretty->canonical->encode($rules) ne JSON->new->pretty->canonical->encode($current_rules); | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | return; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | ######################################################################## | ||||||||||||||||||||||||||||
| 239 | 0 | ▶ | sub check_web_acl_state { | ||||||||||||||||||||||||||
|
Subroutine
check_web_acl_state: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 240 | ######################################################################## | ||||||||||||||||||||||||||||
| 241 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | my ( $name, $id, $waf_config, $waf, $web_acl, $rules ) = @args{qw(name id waf_config waf web_acl rules)}; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | my ( $aws_id, $aws_arn ) = @{ $web_acl->{WebACL} }{qw(Id ARN)}; | |||||||||||||||||||||||||||
| 246 | 0 | my $aws_lock_token = $web_acl->{LockToken}; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | my $local_web_acl = $self->fetch_web_acl; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | ▶ | if ($rules) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 0 | $self->log_warn('waf: rules have changed, updating web-acl.json'); | |||||||||||||||||||||||||||
| 252 | 0 | $local_web_acl->{WebACL}->{Rules} = $rules; | |||||||||||||||||||||||||||
| 253 | 0 | $self->save_web_acl( name => $name, web_acl => $local_web_acl, waf => $waf, waf_config => $waf_config ); | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | # determine if the web-acl has been modified "out-of-band" | ||||||||||||||||||||||||||||
| 257 | # and/or our local web-acl.json file has been modified | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | my $md5 = $self->calculate_md5($web_acl); | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 0 | my $md5_match = $md5 eq $waf_config->{md5}; | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 0 | ▶ | if ( $md5 ne $waf_config->{md5} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 0 | $self->log_warn( 'waf: MD5 hashes do not match: [%s] != [%s]', $waf_config->{md5}, $md5 ); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | my $lock_token_match = $aws_lock_token eq $waf_config->{lock_token}; | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | ▶ | return $FALSE # no need to update, everything OK | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$md5_match and $lock_token_match
MC/DC0%0/2
$md5_match and $lock_token_match
$md5_match
$lock_token_match
Truth table0%0/3
A and B
A= $md5_matchB= $lock_token_match
| |||||||||||||||||||||||||||||
| 270 | if $md5_match && $lock_token_match; | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | return $TRUE # update OK | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $md5_match and $lock_token_match
MC/DC0%0/2
not $md5_match and $lock_token_match
not $md5_match
$lock_token_match
Truth table0%0/3
not(A) and B
A= not $md5_matchB= $lock_token_match
| |||||||||||||||||||||||||||||
| 273 | if !$md5_match && $lock_token_match; | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | if ( $md5_match && !$lock_token_match ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$md5_match and not $lock_token_match
MC/DC0%0/2
$md5_match and not $lock_token_match
$md5_match
not $lock_token_match
Truth table0%0/3
A and not(B)
A= $md5_matchB= not $lock_token_match
| |||||||||||||||||||||||||||||
| 276 | 0 | my $err_msg = <<'END_OF_ERR_MESSAGE'; | |||||||||||||||||||||||||||
| 277 | WAF configuration appears to be in sync, but the remote resource has | ||||||||||||||||||||||||||||
| 278 | abeen modified out-of-band. The local configuration file is being updated with | ||||||||||||||||||||||||||||
| 279 | the latest LockToken. Please run 'plan' again to confirm. | ||||||||||||||||||||||||||||
| 280 | END_OF_ERR_MESSAGE | ||||||||||||||||||||||||||||
| 281 | 0 | $waf_config->{id} = $aws_id; | |||||||||||||||||||||||||||
| 282 | 0 | $waf_config->{arn} = $aws_arn; | |||||||||||||||||||||||||||
| 283 | 0 | $waf_config->{lock_token} = $aws_lock_token; | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | return $FALSE; # no need to update, just update local state | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # if we are here, then conflict !$md5_match && !$lock_token_match | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # rut-roh...conflict | ||||||||||||||||||||||||||||
| 291 | 0 | my $diff = $self->web_acl_diffs($web_acl); | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 0 | my $err_msg = <<'END_OF_ERR_MESSAGE'; | |||||||||||||||||||||||||||
| 294 | ERROR: State conflict detected for Web ACL %s | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | A change has been made to the local 'web-acl.json' file, but the | ||||||||||||||||||||||||||||
| 297 | Web ACL has also been modified in AWS since the last run. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | Applying local changes now would overwrite the remote modifications. | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | %s | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | To resolve this conflict: | ||||||||||||||||||||||||||||
| 304 | 1. Review the diff above to understand the remote changes. | ||||||||||||||||||||||||||||
| 305 | 2. Manually merge the desired remote changes into your local 'web-acl.json' file. | ||||||||||||||||||||||||||||
| 306 | 3. Once the local file represents the true desired state, run 'app-FargateStack plan' again. | ||||||||||||||||||||||||||||
| 307 | END_OF_ERR_MESSAGE | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | log_die( $self, $err_msg, $name, $diff, $name ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | if !$self->get_force; | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 0 | $waf_config->{lock_token} = $aws_lock_token; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | return $TRUE; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | ######################################################################## | ||||||||||||||||||||||||||||
| 318 | 0 | ▶ | sub create_web_acl { | ||||||||||||||||||||||||||
|
Subroutine
create_web_acl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 319 | ######################################################################## | ||||||||||||||||||||||||||||
| 320 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | my ( $name, $waf_config, $waf, $rule_list ) = @args{qw(name waf_config waf rules)}; | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | my $web_acl_summary = $waf->create_web_acl( | ||||||||||||||||||||||||||||
| 325 | name => $name, | ||||||||||||||||||||||||||||
| 326 | rules => $rule_list, | ||||||||||||||||||||||||||||
| 327 | query => 'Summary', | ||||||||||||||||||||||||||||
| 328 | scope => 'REGIONAL', | ||||||||||||||||||||||||||||
| 329 | metric_name => $self->get_config->{app}->{name}, | ||||||||||||||||||||||||||||
| 330 | 0 | ); | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | $waf->check_result( message => 'ERROR: unable to create web-acl: [%s]', $name ); | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 0 | @{$waf_config}{qw(id lock_token arn)} = @{$web_acl_summary}{qw(Id LockToken ARN)}; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | my $web_acl = $self->save_web_acl( | ||||||||||||||||||||||||||||
| 337 | id => $waf_config->{id}, | ||||||||||||||||||||||||||||
| 338 | 0 | scope => 'REGIONAL', | |||||||||||||||||||||||||||
| 339 | name => $name, | ||||||||||||||||||||||||||||
| 340 | waf_config => $waf_config, | ||||||||||||||||||||||||||||
| 341 | waf => $waf, | ||||||||||||||||||||||||||||
| 342 | ); | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | $self->associate_web_acl( | ||||||||||||||||||||||||||||
| 345 | web_acl_arn => $waf_config->{arn}, | ||||||||||||||||||||||||||||
| 346 | resource_arn => $self->get_config->{alb}->{arn}, | ||||||||||||||||||||||||||||
| 347 | 0 | name => $name, | |||||||||||||||||||||||||||
| 348 | waf => $waf | ||||||||||||||||||||||||||||
| 349 | ); | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | return; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | ######################################################################## | ||||||||||||||||||||||||||||
| 355 | 0 | ▶ | sub associate_web_acl { | ||||||||||||||||||||||||||
|
Subroutine
associate_web_acl: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 356 | ######################################################################## | ||||||||||||||||||||||||||||
| 357 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 0 | my ( $web_acl_arn, $resource_arn, $waf, $name ) = @args{qw(web_acl_arn resource_arn waf name)}; | |||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 0 | my $timeout = $WAF_AVAILABILITY_TIMEOUT; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # may need to wait for WAF to become available before associating... | ||||||||||||||||||||||||||||
| 364 | 0 | $self->log_warn('waf: associating WAF with ALB...'); | |||||||||||||||||||||||||||
| 365 | 0 | ▶ | while ($TRUE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | $waf->associate_web_acl( $web_acl_arn, $resource_arn ); | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | $waf->check_result( | |||||||||||||||||||||||||||
| 369 | message => 'ERROR: could not associate web-acl: [%s] with ALB', | ||||||||||||||||||||||||||||
| 370 | params => [$name], | ||||||||||||||||||||||||||||
| 371 | regexp => qr/WAFUnavailableEntityException/xmsi | ||||||||||||||||||||||||||||
| 372 | ); | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 0 | $self->log_warn( 'waf: waiting for resource to become available...sleeping for %s seconds - %s until timeout', | |||||||||||||||||||||||||||
| 375 | $WAF_AVAILABILITY_SLEEP_TIME, $timeout ); | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 0 | sleep $WAF_AVAILABILITY_SLEEP_TIME; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 0 | $timeout -= $WAF_AVAILABILITY_SLEEP_TIME; | |||||||||||||||||||||||||||
| 380 | 0 | ▶ | last if $timeout <= 0 || !$waf->get_error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$timeout <= 0 or not $waf->get_error
MC/DC0%0/2
$timeout <= 0 or not $waf->get_error
$timeout <= 0
not $waf->get_error
Truth table0%0/3
A or not(B)
A= $timeout <= 0B= not $waf->get_error
| |||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | log_die( $self, 'ERROR: unable to associate web-acl to ALB: %s', $waf->get_error ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | if $waf->get_error; | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 0 | return; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | ######################################################################## | ||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub calculate_md5 { | ||||||||||||||||||||||||||
|
Subroutine
calculate_md5: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 391 | ######################################################################## | ||||||||||||||||||||||||||||
| 392 | 0 | my ( $self, $web_acl ) = @_; | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 0 | my $json = JSON->new->pretty->canonical->encode($web_acl); | |||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 0 | return md5_hex($json); | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | ######################################################################## | ||||||||||||||||||||||||||||
| 400 | 0 | ▶ | sub fetch_web_acl { | ||||||||||||||||||||||||||
|
Subroutine
fetch_web_acl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 401 | ######################################################################## | ||||||||||||||||||||||||||||
| 402 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 0 | return slurp_file( 'web-acl.json', $TRUE ); | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | ######################################################################## | ||||||||||||||||||||||||||||
| 408 | 0 | ▶ | sub save_web_acl { | ||||||||||||||||||||||||||
|
Subroutine
save_web_acl: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 409 | ######################################################################## | ||||||||||||||||||||||||||||
| 410 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 0 | my ( $id, $name, $scope, $waf_config, $waf, $web_acl ) = @args{qw(id name scope waf_config waf web_acl)}; | |||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 0 | ▶ | $web_acl //= $waf->get_web_acl( | ||||||||||||||||||||||||||
|
Condition0%0/3
$web_acl //= $waf->get_web_acl("scope", $scope, "id", $id, "name", $name)
MC/DC0%0/2
$web_acl //= $waf->get_web_acl("scope", $scope, "id", $id, "name", $name)
$web_acl
$waf->get_web_acl("scope", $scope, "id", $id, "name", $name)
Truth table0%0/3
A //= B
A= $web_aclB= $waf->get_web_acl("scope", $scope, "id", $id, "name", $name)
| |||||||||||||||||||||||||||||
| 415 | scope => $scope, | ||||||||||||||||||||||||||||
| 416 | id => $id, | ||||||||||||||||||||||||||||
| 417 | name => $name, | ||||||||||||||||||||||||||||
| 418 | ); | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | $waf->check_result( message => 'ERROR: could not get web-acl: [%s]', $name ); | |||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 0 | $waf_config->{md5} = $self->calculate_md5($web_acl); | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 0 | my $json = JSON->new->pretty->canonical->encode($web_acl); | |||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 0 | ▶ | open my $fh, '>', 'web-acl.json' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | or croak "ERROR: could not open web-acl.json for writing\n"; | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 0 | print {$fh} $json; | |||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 0 | close $fh; | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 0 | return; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # produce a diff between the stored web-acl and the current AWS web-acl | ||||||||||||||||||||||||||||
| 437 | ######################################################################## | ||||||||||||||||||||||||||||
| 438 | 0 | ▶ | sub web_acl_diffs { | ||||||||||||||||||||||||||
|
Subroutine
web_acl_diffs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 439 | ######################################################################## | ||||||||||||||||||||||||||||
| 440 | 0 | my ( $self, $web_acl ) = @_; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | my $current_web_acl = slurp_file( 'web-acl.json', $TRUE ); | |||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 0 | return $self->display_diffs( $current_web_acl, $web_acl ); | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | ######################################################################## | ||||||||||||||||||||||||||||
| 448 | 0 | ▶ | sub update_web_acl { | ||||||||||||||||||||||||||
|
Subroutine
update_web_acl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 449 | ######################################################################## | ||||||||||||||||||||||||||||
| 450 | 0 | my ( $self, $lock_token ) = @_; | |||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 0 | my $web_acl = $self->fetch_web_acl; | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | my ( $id, $visibility_config, $rules, $description, $default_action ) | ||||||||||||||||||||||||||||
| 455 | 0 | = @{ $web_acl->{WebACL} }{qw(Id VisibilityConfig Rules Description DefaultAction)}; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | my $scope = 'REGIONAL'; | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 0 | my $name = $self->get_config->{alb}->{waf}->{name}; | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 0 | my $waf = $self->fetch_wafv2; | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | my $result = $waf->update_web_acl( | ||||||||||||||||||||||||||||
| 464 | scope => $scope, | ||||||||||||||||||||||||||||
| 465 | name => $name, | ||||||||||||||||||||||||||||
| 466 | visibility_config => encode_json($visibility_config), | ||||||||||||||||||||||||||||
| 467 | rules => $rules, | ||||||||||||||||||||||||||||
| 468 | lock_token => $lock_token, | ||||||||||||||||||||||||||||
| 469 | default_action => encode_json($default_action), | ||||||||||||||||||||||||||||
| 470 | description => $description, | ||||||||||||||||||||||||||||
| 471 | metric_name => $self->get_config->{app}->{name}, | ||||||||||||||||||||||||||||
| 472 | 0 | id => $id, | |||||||||||||||||||||||||||
| 473 | ); | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 0 | $waf->check_result( message => 'ERROR: could not update web-acl: [%s]', $name ); | |||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 0 | return; | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 1 | 1; | |||||||||||||||||||||||||||