Farly/Rule/Expander.pm

filter: Statementstmt 88.4% 115 / 130 Branchbran 73.9% 34 / 46 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 93.3% 14 / 15 Podpod 75.0% 3 / 4 totaltotal 86.1% 180 / 209 CC 31 SCAR 21.1
CC
31
Cov
85%
expand
13.6
_expand_vip
12.0
_set_defaults
11.0
CRAP 8.2
« Farly/Template/Cisco.pm ↑ Summary Farly/ASA/Rewriter.pm »
1 package Farly::Rule::Expander;
2
3 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Farly::Object::Aggregate;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 3 our $VERSION = '0.26';
11
12 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
13 3 my ( $class, $fw ) = @_;
14
15 3 ▶ confess "configuration container object required"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fw 0 3 3
16 unless ( defined($fw) );
17
18 3 ▶ confess "Farly::Object::List object required"
Branch: 1 branch
BranchTrueFalseTotal
unless $fw->isa("Farly::Object::List") 0 3 3
19 unless ( $fw->isa('Farly::Object::List') );
20
21 3 my $self = {
22 CONFIG => $fw,
23 AGGREGATE => undef,
24 };
25
26 3 bless $self, $class;
27
28 3 $log->info("$self new");
29 3 $log->info( "$self CONFIG " . $self->{CONFIG} );
30
31 3 $self->_init();
32
33 3 return $self;
34 }
35
36 6 ▶sub config { return $_[0]->{CONFIG}; }
Subroutine config: called CC=1 SCAR=0.0
Pod undocumented
37 57 ▶sub _agg { return $_[0]->{AGGREGATE}; }
Subroutine _agg: called CC=1 SCAR=0.0
38
39 3 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
40 3 my ($self) = @_;
41 3 $self->{AGGREGATE} = Farly::Object::Aggregate->new( $self->config );
42 3 $self->{AGGREGATE}->groupby( 'ENTRY', 'ID' );
43 }
44
45 66 ▶sub _set_defaults {
Subroutine _set_defaults: called CC=11 SCAR=24.0
46 66 my ( $self, $ce ) = @_;
47
48 66 my $RULE = Farly::Object->new();
49 66 $RULE->set( 'ENTRY', Farly::Value::String->new('RULE') );
50
51 66 my $IP = Farly::Transport::Protocol->new('0');
52 66 my $TCP = Farly::Transport::Protocol->new('6');
53 66 my $UDP = Farly::Transport::Protocol->new('17');
54 66 my $ICMP = Farly::Transport::Protocol->new('1');
55
56 #Check if the config entry is an access-list
57 66 ▶ if ( $ce->matches($RULE) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ce->matches($RULE)) { } 66 0 66
58
59 66 ▶ return if ( $ce->has_defined('COMMENT') );
Branch: 1 branch
BranchTrueFalseTotal
if $ce->has_defined("COMMENT") 6 60 66
60
61 #Check if the access-list protocol is ip, tcp or udp
62 60 ▶ if ( $ce->get('PROTOCOL')->equals($IP)
Branch: 1 branch
BranchTrueFalseTotal
if ($ce->get("PROTOCOL")->equals($IP) or $ce->get("PROTOCOL")->equals($TCP) or $ce->get("PROTOCOL")->equals($UDP)) 42 18 60
Condition100%6/6
$ce->get("PROTOCOL")->equals($IP) or $ce->get("PROTOCOL")->equals($TCP) or $ce->get("PROTOCOL")->equals($UDP)
l!l&&r!l&&!r
33918
$ce->get("PROTOCOL")->equals($IP) or $ce->get("PROTOCOL")->equals($TCP)
l!l&&r!l&&!r
62727
MC/DC100%3/3
$ce->get("PROTOCOL")->equals($IP) or $ce->get("PROTOCOL")->equals($TCP) or $ce->get("PROTOCOL")->equals($UDP)
$ce->get("PROTOCOL")->equals($IP) $ce->get("PROTOCOL")->equals($TCP) $ce->get("PROTOCOL")->equals($UDP)
Truth table100%4/4
A or B or C
A= $ce->get("PROTOCOL")->equals($IP)B= $ce->get("PROTOCOL")->equals($TCP)C= $ce->get("PROTOCOL")->equals($UDP)
ABCresult
1XX1
01X1
0011
0000
63 || $ce->get('PROTOCOL')->equals($TCP)
64 || $ce->get('PROTOCOL')->equals($UDP) )
65 {
66
67 42 $log->debug("defaulting ports for $ce");
68
69 #if a srcport is not defined, define all ports
70 42 ▶ if ( !$ce->has_defined('SRC_PORT') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ce->has_defined("SRC_PORT")) 18 24 42
71
72 18 $ce->set( 'SRC_PORT', Farly::Transport::PortRange->new( 1, 65535 ) );
73 18 $log->debug( 'set SRC_PORT = ' . $ce->get('SRC_PORT') );
74 }
75
76 #if a dst port is not defined, define all ports
77 42 ▶ if ( !$ce->has_defined('DST_PORT') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ce->has_defined("DST_PORT")) 6 36 42
78
79 6 $ce->set( 'DST_PORT', Farly::Transport::PortRange->new( 1, 65535 ) );
80 6 $log->debug( "set DST_PORT = " . $ce->get('DST_PORT') );
81 }
82 }
83
84 60 ▶ if ( $ce->get('PROTOCOL')->equals($IP)
Branch: 1 branch
BranchTrueFalseTotal
if ($ce->get("PROTOCOL")->equals($IP) or $ce->get("PROTOCOL")->equals($ICMP)) 21 39 60
Condition100%3/3
$ce->get("PROTOCOL")->equals($IP) or $ce->get("PROTOCOL")->equals($ICMP)
l!l&&r!l&&!r
61539
MC/DC100%2/2
$ce->get("PROTOCOL")->equals($IP) or $ce->get("PROTOCOL")->equals($ICMP)
$ce->get("PROTOCOL")->equals($IP) $ce->get("PROTOCOL")->equals($ICMP)
Truth table100%3/3
A or B
A= $ce->get("PROTOCOL")->equals($IP)B= $ce->get("PROTOCOL")->equals($ICMP)
ABresult
1X1
011
000
85 || $ce->get('PROTOCOL')->equals($ICMP) )
86 {
87 21 $log->debug("defaulting ports for $ce");
88
89 #if an icmp type is not defined, define all icmp types as -1
90 21 ▶ if ( !$ce->has_defined('ICMP_TYPE') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ce->has_defined("ICMP_TYPE")) 9 12 21
91
92 9 $ce->set( 'ICMP_TYPE', Farly::IPv4::ICMPType->new(-1) );
93 9 $log->debug('set ICMP_TYPE to -1 ');
94 }
95 }
96 }
97 else {
98 0 confess "_set_defaults is for RULE objects only";
99 }
100 }
101
102 3 ▶sub expand_all {
Subroutine expand_all: called CC=3 SCAR=11.0
Pod documented
103 3 my ($self) = @_;
104
105 3 my $expanded = Farly::Object::List->new();
106
107 3 my $RULE = Farly::Value::String->new('RULE');
108
109 3 my $RULE_SEARCH = Farly::Object->new();
110 3 $RULE_SEARCH->set( 'ENTRY', $RULE );
111
112 3 my $rules = Farly::Object::List->new();
113
114 3 $self->config->matches( $RULE_SEARCH, $rules );
115
116 3 foreach my $ce ( $rules->iter() ) {
117 51 eval {
118 51 my $clone = $ce->clone();
119 51 $self->expand( $clone, $expanded );
120 };
121 51 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 51 51
122 0 confess "$@ \n expand failed for ", $ce->dump(), "\n";
123 }
124 }
125
126 3 return $expanded;
127 }
128
129 # { 'key' => ::HashRef } refers to one or more actual Objects
130 # Replace the ::HashRef with a ::Set of the actual objects
131 # the actual objects might hold a ::HashRef
132 # { 'key' => ::Set } is a list of config ::Hash or ::HashRef's.
133 # For every object in the Set clone the RULE object
134 # and replace the RULE value with the object from the ::Set
135 # { 'key' => Farly::Object }
136 # use "OBJECT" key/value in the raw RULE object
137
138 51 ▶sub expand {
Subroutine expand: called CC=13 SCAR=26.1
Pod documented
139 51 my ( $self, $rule, $result ) = @_;
140
141 51 my $is_expanded;
142 my @stack;
143 51 push @stack, $rule;
144
145 51 my $COMMENT = Farly::Object->new();
146 51 $COMMENT->set( 'OBJECT_TYPE', Farly::Value::String->new('COMMENT') );
147
148 51 my $SERVICE = Farly::Object->new();
149 51 $SERVICE->set( 'OBJECT_TYPE', Farly::Value::String->new('SERVICE') );
150
151 51 my $VIP = Farly::Object->new();
152 51 $VIP->set( 'OBJECT_TYPE', Farly::Value::String->new('VIP') );
153
154 51 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 256 51 307
155 256 my $ce = pop @stack;
156
157 256 foreach my $key ( $ce->get_keys() ) {
158
159 1309 my $value = $ce->get($key);
160
161 1309 $log->debug("entry $ce : key = $key : value = $value");
162
163 1309 $is_expanded = 1;
164
165 1309 ▶ if ( $value->isa('Farly::Object::Ref') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value->isa('Farly::Object::Ref')) { } 57 1252 1309
elsif ($value->isa('Farly::Object::List')) { } 57 1195 1252
elsif ($value->isa('Farly::Object')) { } 76 1119 1195
166
167 57 $is_expanded = 0;
168
169 57 my $actual = $self->_agg->matches($value);
170
171 57 ▶ if ( !defined $actual ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $actual) 0 57 57
172 0 confess "actual not found for $key";
173 }
174
175 57 $ce->set( $key, $actual );
176
177 57 push @stack, $ce;
178
179 57 last;
180 }
181 elsif ( $value->isa('Farly::Object::List') ) {
182
183 57 $is_expanded = 0;
184
185 57 $log->debug("$ce => $key isa $value");
186
187 57 foreach my $object ( $value->iter() ) {
188
189 72 my $clone = $ce->clone();
190
191 72 $clone->set( $key, $object );
192
193 72 push @stack, $clone;
194 }
195
196 57 last;
197 }
198 elsif ( $value->isa('Farly::Object') ) {
199
200 76 $is_expanded = 0;
201
202 76 my $clone = $ce->clone();
203
204 76 ▶ if ( $value->matches($COMMENT) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->matches($COMMENT)) 0 76 76
205
206 0 $log->debug( "skipped group comment :\n" . $ce->dump() . "\n" );
207
208 0 last;
209 }
210 76 ▶ if ( $value->matches($VIP) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value->matches($VIP)) { } 0 76 76
elsif ($value->matches($SERVICE)) { } 12 64 76
elsif ($value->has_defined('OBJECT')) { } 64 0 64
211
212 0 $self->_expand_vip( $key, $clone, $value );
213 }
214 elsif ( $value->matches($SERVICE) ) {
215
216 12 $self->_expand_service( $clone, $value );
217 }
218 elsif ( $value->has_defined('OBJECT') ) {
219
220 64 $clone->set( $key, $value->get('OBJECT') );
221 }
222 else {
223
224 0 $log->warn( "skipped $ce property $key has no OBJECT\n" . $ce->dump() );
225
226 0 last;
227 }
228
229 76 push @stack, $clone;
230
231 76 last;
232 }
233 }
234
235 256 ▶ if ($is_expanded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_expanded) 66 190 256
236 66 $self->_set_defaults($ce);
237 66 $result->add($ce);
238 }
239 }
240
241 51 return $result;
242 }
243
244 12 ▶sub _expand_service {
Subroutine _expand_service: called CC=3 SCAR=11.0
245 12 my ( $self, $clone, $service_object ) = @_;
246 12 my @keys = qw(PROTOCOL SRC_PORT DST_PORT ICMP_TYPE);
247 12 foreach my $key (@keys) {
248 48 ▶ if ( $service_object->has_defined($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_object->has_defined($key)) 24 24 48
249 24 $clone->set( $key, $service_object->get($key) );
250 }
251 }
252 12 return;
253 }
254
255 0 ▶sub _expand_vip {
Subroutine _expand_vip: not called CC=3 SCAR=24.8
256 0 my ( $self, $key, $clone, $vip_object ) = @_;
257
258 0 $log->debug("processing VIP $vip_object : key = $key");
259
260 0 ▶ if ( $key eq 'DST_IP' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'DST_IP') { } 0 0 0
elsif ($key eq 'DST_PORT') { } 0 0 0
261 0 $clone->set( $key, $vip_object->get('REAL_IP') );
262 }
263 elsif ( $key eq 'DST_PORT' ) {
264 0 $clone->set( $key, $vip_object->get('REAL_PORT') );
265 }
266 else {
267 0 confess "invalid key for VIP\n", "key $key \n", "rule: ",
268 $clone->dump(), "\n", "vip: ", $vip_object->dump(), "\n";
269 }
270
271 0 return;
272 }
273
274 3 1;
275 __END__
« Farly/Template/Cisco.pm ↑ Summary Farly/ASA/Rewriter.pm »