Farly/ASA/Filter.pm

filter: Statementstmt 97.2% 106 / 109 Branchbran 88.6% 39 / 44 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 93.0% 161 / 173 CC 24 SCAR 19.1
CC
24
Cov
95%
run
11.0
_process_acl
7.0
_process_section
7.0
CRAP 6.8
« Farly/Rule/Optimizer.pm ↑ Summary Farly/Object/Set.pm »
1 package Farly::ASA::Filter;
2
3 10 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 10 our $VERSION = '0.26';
10
11 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 7 my ($class) = @_;
13
14 7 my $self = {
15 FILE => undef,
16 PREPARSE => [],
17 OG_INDEX => {}, #object-group to type mapping
18 ACL_ID => {}, #for inserting line numbers
19 };
20 7 bless $self, $class;
21
22 7 $log->info("$self NEW");
23
24 7 return $self;
25 }
26
27 7 ▶sub set_file {
Subroutine set_file: called CC=1 SCAR=0.0
Pod undocumented
28 7 my ( $self, $file ) = @_;
29 7 $self->{FILE} = $file;
30 7 $log->info( "$self set FILE to " . $self->{FILE} );
31 }
32
33 430 ▶sub append {
Subroutine append: called CC=2 SCAR=7.0
Pod undocumented
34 430 my ( $self, $string ) = @_;
35 430 ▶ defined($string)
Branch: 1 branch
BranchTrueFalseTotal
defined $string or confess(" $self attempted to append undefined string to PREPARSE") 430 0 430
36 or confess " $self attempted to append undefined string to PREPARSE";
37 430 push @{ $self->{PREPARSE} }, $string;
38 }
39
40 7 ▶sub run {
Subroutine run: called CC=11 SCAR=24.0
Pod undocumented
41 7 my ($self) = @_;
42
43 7 my $file = $self->{FILE};
44
45 7 my $interface_options = "nameif|security-level|ip address"; #shutdown
46 7 my $object_options = "host|range|subnet|service";
47 7 my $group_options = "network-object|port-object|group-object|protocol-object|description|icmp-object|service-object";
48 7 my $unsupported_acl_type = "ethertype|standard|webtype";
49
50 7 ▶ while ( my $line = $file->getline() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $file->getline) 346 7 353
51
52 346 $log->trace("$self SCAN $line");
53
54 346 ▶ if ( $line =~ /^hostname (\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^hostname (\S+)/) 7 339 346
55 7 $self->append($line);
56 7 next;
57 }
58 339 ▶ if ( $line =~ /^name (\S+) (\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^name (\S+) (\S+)/) 14 325 339
59 14 $self->append($line);
60 14 next;
61 }
62 325 ▶ if ( $line =~ /^interface/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^interface/) 7 318 325
63 7 $self->_process_section( $line, $interface_options, 1 );
64 7 next;
65 }
66 318 ▶ if ( $line =~ /^object\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^object\s/) 41 277 318
67 41 $self->_process_section( $line, $object_options );
68 41 next;
69 }
70 277 ▶ if ( $line =~ /^object-group (\S+) (\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^object-group (\S+) (\S+)/) 90 187 277
71 90 my $type = $1;
72 90 my $id = $2;
73 90 $self->{OG_INDEX}->{$id} = $type;
74 90 $log->debug("added OG_INDEX $id $type");
75 90 $self->_process_section( $line, $group_options );
76 90 next;
77 }
78 187 ▶ if ( $line =~ /^access-list (.*) $unsupported_acl_type/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^access-list (.*) $unsupported_acl_type/) 0 187 187
79 0 $log->info("$self SKIPPED access-list '$line'");
80 0 next;
81 }
82
83 #access-list outside-in line 3 extended permit tcp OG_NETWORK internal OG_SERVICE highports host 192.168.2.1 eq 80
84 187 ▶ if ( $line =~ /^access-list/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^access-list/) 111 76 187
85 111 my $p_line = $self->_process_acl($line);
86 111 $log->debug("$self pre-processed line '$p_line'");
87 111 $self->append($p_line);
88 111 next;
89 }
90 76 ▶ if ( $line =~ /^access-group/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^access-group/) 7 69 76
91 7 $self->append($line);
92 7 next;
93 }
94 69 ▶ if ( $line =~ /^route/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^route/) 12 57 69
95 12 $self->append($line);
96 }
97 }
98
99 7 return @{ $self->{PREPARSE} };
100 }
101
102 138 ▶sub _process_section {
Subroutine _process_section: called CC=7 SCAR=19.5
103 138 my ( $self, $header, $options, $full_sect ) = @_;
104
105 138 my $file = $self->{FILE};
106 138 my $pos = $file->getpos();
107 138 my $line = $file->getline();
108
109 138 $log->debug("$header");
110 138 my $header_pos = $pos;
111
112 138 ▶ while ( $line && $line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line and $line =~ /^\s/) 326 138 464
Condition100%3/3
$line and $line =~ /^\s/
!ll&&!rl&&r
6132326
MC/DC100%2/2
$line and $line =~ /^\s/
$line $line =~ /^\s/
Truth table100%3/3
A and B
A= $lineB= $line =~ /^\s/
ABresult
0X0
100
111
113
114 326 ▶ if ( $line =~ /^\s(?=$options)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s(?=$options)/) { } 293 33 326
115 293 $log->debug("$line");
116 293 ▶ if ( defined($full_sect) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $full_sect) { } 21 272 293
117 21 $header .= $line;
118 }
119 else {
120 272 $self->append( $header . $line );
121 }
122 }
123 else {
124 33 chomp($line);
125 33 $log->warn("unknown option in line '$line'");
126 }
127
128 326 $pos = $file->getpos();
129 326 $line = $file->getline();
130 }
131
132 138 ▶ if ( defined($full_sect) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $full_sect) 7 131 138
133 7 $self->append($header);
134 }
135
136 138 ▶ if ( $pos eq $header_pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos eq $header_pos) 0 138 138
137 0 $log->warn("empty section : '$header'");
138 }
139
140 138 $file->setpos($pos);
141 }
142
143 111 ▶sub _process_acl {
Subroutine _process_acl: called CC=7 SCAR=19.5
144 111 my ( $self, $line ) = @_;
145
146 # add line number to configuration access-list
147 111 ▶ if ( $line =~ /^access-list (\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^access-list (\S+)/) 111 0 111
148 111 my $acl_id = $1;
149 111 ▶ if ( !$self->{ACL_ID}->{$acl_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'ACL_ID'}{$acl_id}) { } 13 98 111
150 13 $self->{ACL_ID}->{$acl_id} = 1;
151 }
152 else {
153 98 $self->{ACL_ID}->{$acl_id}++;
154 }
155 111 my $line_count = $self->{ACL_ID}->{$acl_id};
156 111 $line =~ s/access-list $acl_id/access-list $acl_id line $line_count/;
157 }
158
159 # translate "object-group" to OG_<TYPE> format
160 111 ▶ if ( $line =~ /object-group/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /object-group/) 42 69 111
161
162 42 my @lineArr = split( /\s+/, $line );
163
164 42 ▶ while (@lineArr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lineArr) 518 42 560
165 518 my $string = shift @lineArr;
166 518 ▶ if ( $string =~ /object-group/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /object-group/) 77 441 518
167 77 my $og_ID = shift @lineArr;
168
169 77 ▶ my $og_type = $self->{OG_INDEX}->{$og_ID}
Branch: 1 branch
BranchTrueFalseTotal
my $og_type = $self->{"OG_INDEX"}{$og_ID} or confess("no object-group type for $og_ID") 77 0 77
170 or confess "no object-group type for $og_ID";
171
172 77 my $new_og_type = "OG_" . uc($og_type);
173 77 $line =~ s/object-group $og_ID/$new_og_type $og_ID/;
174 }
175 }
176 }
177
178 111 return $line;
179 }
180
181 10 1;
182 __END__
« Farly/Rule/Optimizer.pm ↑ Summary Farly/Object/Set.pm »