App-FargateStack

 view release on metacpan or  search on metacpan

lib/App/FargateStack/Builder/WafV2.pm  view on Meta::CPAN


########################################################################
sub calculate_md5 {
########################################################################
  my ( $self, $web_acl ) = @_;

  my $json = JSON->new->pretty->canonical->encode($web_acl);

  return md5_hex($json);
}

########################################################################
sub fetch_web_acl {
########################################################################
  my ($self) = @_;

  return slurp_file( 'web-acl.json', $TRUE );
}

########################################################################
sub save_web_acl {
########################################################################
  my ( $self, %args ) = @_;

  my ( $id, $name, $scope, $waf_config, $waf, $web_acl ) = @args{qw(id name scope waf_config waf web_acl)};

  $web_acl //= $waf->get_web_acl(
    scope => $scope,
    id    => $id,
    name  => $name,
  );

  $waf->check_result( message => 'ERROR: could not get web-acl: [%s]', $name );

  $waf_config->{md5} = $self->calculate_md5($web_acl);

  my $json = JSON->new->pretty->canonical->encode($web_acl);

  open my $fh, '>', 'web-acl.json'
    or croak "ERROR: could not open web-acl.json for writing\n";

  print {$fh} $json;

  close $fh;

  return;
}

# produce a diff between the stored web-acl and the current AWS web-acl
########################################################################
sub web_acl_diffs {
########################################################################
  my ( $self, $web_acl ) = @_;

  my $current_web_acl = slurp_file( 'web-acl.json', $TRUE );

  return $self->display_diffs( $current_web_acl, $web_acl );
}

########################################################################
sub update_web_acl {
########################################################################
  my ( $self, $lock_token ) = @_;

  my $web_acl = $self->fetch_web_acl;

  my ( $id, $visibility_config, $rules, $description, $default_action )
    = @{ $web_acl->{WebACL} }{qw(Id VisibilityConfig Rules Description DefaultAction)};

  my $scope = 'REGIONAL';

  my $name = $self->get_config->{alb}->{waf}->{name};

  my $waf = $self->fetch_wafv2;

  my $result = $waf->update_web_acl(
    scope             => $scope,
    name              => $name,
    visibility_config => encode_json($visibility_config),
    rules             => $rules,
    lock_token        => $lock_token,
    default_action    => encode_json($default_action),
    description       => $description,
    metric_name       => $self->get_config->{app}->{name},
    id                => $id,
  );

  $waf->check_result( message => 'ERROR: could not update web-acl: [%s]', $name );

  return;
}

1;



( run in 0.967 second using v1.01-cache-2.11-cpan-39bf76dae61 )