lib/Rex/Resource/firewall/Provider/ufw.pm

filter: Statementstmt 13.0% 20 / 153 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 33.3% 6 / 18 Podpod 0.0% 0 / 6 totaltotal 7.7% 26 / 334 CC 63 SCAR 61.4
CC
63
Cov
7%
_ufw_rule
992.0
_generate_rule_array
306.0
_ufw_logging
110.0
CRAP 465.9
« lib/Rex/Commands/Service.pm ↑ Summary lib/Rex/Interface/Connection/OpenSSH.pm »
1 #
2 # (c) Andrew Beverley
3 # (c) Jan Gehring <jan.gehring@gmail.com>
4 #
5  
6 package Rex::Resource::firewall::Provider::ufw;
7  
8 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '1.16.1'; # VERSION
12  
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use base qw(Rex::Resource::firewall::Provider::base);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 my %__action_map = (
20 accept => "allow",
21 allow => "allow",
22 deny => "deny", ## -j DROP
23 drop => "deny", ## -j DROP
24 reject => "reject", ## -j REJECT
25 limit => "limit",
26 );
27  
28 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
29 0 my $that = shift;
30 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
31 0 my $self = $proto->SUPER::new(@_);
32  
33 0 bless( $self, $proto );
34  
35 0 return $self;
36 }
37  
38 0 ▶sub present {
Subroutine present: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my ( $self, $rule_config ) = @_;
40  
41 0 my @ufw_params = $self->_generate_rule_array($rule_config);
42  
43 0 return $self->_ufw_rule( grep { defined } @ufw_params );
44 }
45  
46 0 ▶sub absent {
Subroutine absent: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my ( $self, $rule_config ) = @_;
48  
49 0 $rule_config->{delete} = 1;
50 0 my @ufw_params = $self->_generate_rule_array($rule_config);
51  
52 0 return $self->_ufw_rule( grep { defined } @ufw_params );
53 }
54  
55 0 ▶sub enable {
Subroutine enable: not called CC=1 SCAR=6.9
Pod undocumented
56 0 my ( $self, $rule_config ) = @_;
57 0 return $self->_ufw_disable_enable("enable");
58 }
59  
60 0 ▶sub disable {
Subroutine disable: not called CC=1 SCAR=6.9
Pod undocumented
61 0 my ( $self, $rule_config ) = @_;
62 0 return $self->_ufw_disable_enable("disable");
63 }
64  
65 0 ▶sub logging {
Subroutine logging: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my ( $self, $logging ) = @_;
67 0 return $self->_ufw_logging($logging);
68 }
69  
70 0 ▶sub _ufw_rule {
Subroutine _ufw_rule: not called CC=31 SCAR=69.0
71  
72 0 my ( $self, $action, @params ) = @_;
73 0 my %torun = (
74 action => $action, # allow, deny, limit etc
75 commands => [],
76 );
77  
78 0 my $has_app; # Has app parameters
79 my $has_port; # Has port parameters
80  
81 0 ▶ while ( my $param = shift @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param = shift @params) 0 0 0
82 0 ▶ if ( $param eq 'proto' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($param eq 'proto') { } 0 0 0
elsif ($param eq 'from' or $param eq 'to') { } 0 0 0
elsif ($param eq 'app') { } 0 0 0
elsif ($param eq 'direction') { } 0 0 0
elsif ($param eq 'log') { } 0 0 0
elsif ($param eq 'delete') { } 0 0 0
elsif ($param =~ m[^\d+(/(tcp|udp))?$]u) { } 0 0 0
83 0 my $proto = shift @params;
84 0 ▶ die "Invalid protocol $proto"
Branch: 1 branch
BranchTrueFalseTotal
unless $proto eq "tcp" or $proto eq "udp" 0 0 0
Condition0%0/3
$proto eq "tcp" or $proto eq "udp"
l!l&&r!l&&!r
000
MC/DC0%0/2
$proto eq "tcp" or $proto eq "udp"
$proto eq "tcp" $proto eq "udp"
Truth table0%0/3
A or B
A= $proto eq "tcp"B= $proto eq "udp"
ABresult
1X1
011
000
85 unless ( $proto eq 'tcp' || $proto eq 'udp' );
86 0 $torun{proto} = "proto $proto";
87 }
88 ▶ elsif ( $param eq 'from' || $param eq 'to' ) {
Condition0%0/3
$param eq "from" or $param eq "to"
l!l&&r!l&&!r
000
MC/DC0%0/2
$param eq "from" or $param eq "to"
$param eq "from" $param eq "to"
Truth table0%0/3
A or B
A= $param eq "from"B= $param eq "to"
ABresult
1X1
011
000
89 0 my $address = shift @params;
90 0 push @{ $torun{commands} }, ( $param => $address );
91  
92 # See if next rule is a port
93 0 ▶ if ( $params[0] && $params[0] eq 'port' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params[0] and $params[0] eq 'port') { } 0 0 0
elsif ($params[0] and $params[0] eq 'app') { } 0 0 0
Condition0%0/3
$params[0] and $params[0] eq "port"
!ll&&!rl&&r
000
MC/DC0%0/2
$params[0] and $params[0] eq "port"
$params[0] $params[0] eq "port"
Truth table0%0/3
A and B
A= $params[0]B= $params[0] eq "port"
ABresult
0X0
100
111
94 0 shift @params;
95 0 my $port = shift @params;
96 0 push @{ $torun{commands} }, ( port => $port );
97 0 $has_port = 1;
98 }
99 ▶ elsif ( $params[0] && $params[0] eq 'app' ) {
Condition0%0/3
$params[0] and $params[0] eq "app"
!ll&&!rl&&r
000
MC/DC0%0/2
$params[0] and $params[0] eq "app"
$params[0] $params[0] eq "app"
Truth table0%0/3
A and B
A= $params[0]B= $params[0] eq "app"
ABresult
0X0
100
111
100 0 shift @params;
101 0 my $app = shift @params;
102 0 push @{ $torun{commands} }, ( app => $app );
103 0 $has_app = 1;
104 }
105 }
106 elsif ( $param eq 'app' ) {
107  
108 # App can appear on its own, or in combination with from/to above
109 0 my $app = shift @params;
110 0 $torun{app} = $app;
111 0 $has_app = 1;
112 }
113 elsif ( $param eq 'direction' ) {
114 0 my $direction = shift @params;
115 0 ▶ die "Invalid direction $direction"
Branch: 1 branch
BranchTrueFalseTotal
unless $direction eq "in" or $direction eq "out" 0 0 0
Condition0%0/3
$direction eq "in" or $direction eq "out"
l!l&&r!l&&!r
000
MC/DC0%0/2
$direction eq "in" or $direction eq "out"
$direction eq "in" $direction eq "out"
Truth table0%0/3
A or B
A= $direction eq "in"B= $direction eq "out"
ABresult
1X1
011
000
116 unless ( $direction eq 'in' || $direction eq 'out' );
117 0 $torun{direction} = $direction;
118  
119 # See if next rule is an interface
120 0 ▶ if ( $params[0] && $params[0] eq 'on' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params[0] and $params[0] eq "on") 0 0 0
Condition0%0/3
$params[0] and $params[0] eq "on"
!ll&&!rl&&r
000
MC/DC0%0/2
$params[0] and $params[0] eq "on"
$params[0] $params[0] eq "on"
Truth table0%0/3
A and B
A= $params[0]B= $params[0] eq "on"
ABresult
0X0
100
111
121 0 shift @params;
122 0 my $interface = shift @params;
123 0 $torun{on} = "on $interface";
124 }
125 }
126 elsif ( $param eq 'log' ) {
127 0 my $log = shift @params;
128 0 ▶ if ( $log eq 'new' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($log eq 'new') { } 0 0 0
elsif ($log eq 'all') { } 0 0 0
129 0 $torun{log} = 'log';
130 }
131 elsif ( $log eq 'all' ) {
132 0 $torun{log} = 'log-all';
133 }
134 else {
135 0 die "Invalid logging option $log";
136 }
137 }
138 elsif ( $param eq 'delete' ) {
139 0 ▶ $torun{delete} = 'delete' if shift @params;
Branch: 1 branch
BranchTrueFalseTotal
if shift @params 0 0 0
140 }
141 elsif ( $param =~ m/^\d+(\/(tcp|udp))?$/ ) {
142 0 ▶ if ( scalar @{ $torun{commands} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$torun{"commands"};} == 0) 0 0 0
143 0 push @{ $torun{commands} }, $param;
144 }
145 }
146 else {
147 0 die qq(Unexpected parameter "$param" supplied to ufw rule $action);
148 }
149 }
150  
151 0 ▶ die "Do not specify port parameter with app parameter"
Branch: 1 branch
BranchTrueFalseTotal
if $has_app and $has_port 0 0 0
Condition0%0/3
$has_app and $has_port
!ll&&!rl&&r
000
MC/DC0%0/2
$has_app and $has_port
$has_app $has_port
Truth table0%0/3
A and B
A= $has_appB= $has_port
ABresult
0X0
100
111
152 if $has_app && $has_port;
153  
154 die "Do not specify protocol parameter with app parameter"
155 0 ▶ if $has_app && $torun{proto};
Branch: 1 branch
BranchTrueFalseTotal
if $has_app and $torun{"proto"} 0 0 0
Condition0%0/3
$has_app and $torun{"proto"}
!ll&&!rl&&r
000
MC/DC0%0/2
$has_app and $torun{"proto"}
$has_app $torun{"proto"}
Truth table0%0/3
A and B
A= $has_appB= $torun{"proto"}
ABresult
0X0
100
111
156  
157 0 my $cmd;
158 0 for my $param (qw/delete action direction on log app proto/) {
159 0 ▶ $cmd .= " $torun{$param}" if $torun{$param};
Branch: 1 branch
BranchTrueFalseTotal
if $torun{$param} 0 0 0
160 }
161 0 $cmd .= " @{$torun{commands}}";
162  
163 0 my $return = $self->_ufw_exec($cmd);
164  
165 0 ▶ if ( $return =~ /(inserted|updated|deleted|added)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return =~ /(inserted|updated|deleted|added)/u) 0 0 0
166 0 return 1;
167 }
168  
169 0 return 0;
170 }
171  
172 0 ▶sub _ufw_disable_enable {
Subroutine _ufw_disable_enable: not called CC=5 SCAR=34.0
173 0 my $self = shift;
174 0 my $action = shift;
175 0 my $return = $self->_ufw_exec('status');
176  
177 0 ▶ my $needed = $action eq 'enable' ? 'inactive' : 'active';
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'enable' ? : 0 0 0
178 0 ▶ if ( $return =~ /Status: $needed/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return =~ /Status: $needed/u) 0 0 0
179 0 my $ret = $self->_ufw_exec("--force $action");
180 0 ▶ my $success =
Branch: 1 branch
BranchTrueFalseTotal
$action eq 'enable' ? : 0 0 0
181 $action eq 'enable'
182 ? 'Firewall is active and enabled'
183 : 'Firewall stopped and disabled';
184 0 ▶ if ( $ret =~ /$success/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret =~ /$success/u) { } 0 0 0
185 0 return 1;
186 }
187 else {
188 0 Rex::Logger::info( "Unexpected ufw response: $ret", "warn" );
189 }
190 }
191  
192 0 return 0;
193 }
194  
195 0 ▶sub _ufw_logging {
Subroutine _ufw_logging: not called CC=10 SCAR=47.0
196 0 my $self = shift;
197 0 my $param = shift;
198  
199 0 ▶ $param =~ /(on|off|low|medium|high|full)/
Branch: 1 branch
BranchTrueFalseTotal
$param =~ /(on|off|low|medium|high|full)/u or die "Invalid logging parameter: $param" 0 0 0
200 or die "Invalid logging parameter: $param";
201  
202 0 my $current = $self->_ufw_exec('status verbose');
203  
204 0 my $need_update;
205 0 ▶ if ( $param eq 'on' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param eq 'on') { } 0 0 0
elsif ($param eq 'off') { } 0 0 0
206 0 ▶ $need_update = 1 unless $current =~ /^Logging: on/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $current =~ /^Logging: on/mu 0 0 0
207 }
208 elsif ( $param eq 'off' ) {
209 0 ▶ $need_update = 1 unless $current =~ /^Logging: off$/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $current =~ /^Logging: off$/mu 0 0 0
210 }
211 else {
212 0 ▶ $need_update = 1 unless $current =~ /^Logging: on \($param\)$/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $current =~ /^Logging: on \($param\)$/mu 0 0 0
213 }
214  
215 0 ▶ if ($need_update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_update) 0 0 0
216 0 my $ret = $self->_ufw_exec("logging $param");
217 0 ▶ my $success =
Branch: 1 branch
BranchTrueFalseTotal
$param eq 'off' ? : 0 0 0
218 $param eq 'off'
219 ? 'Logging disabled'
220 : 'Logging enabled';
221 0 ▶ if ( $ret eq $success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret eq $success) { } 0 0 0
222 0 return 1;
223 }
224 else {
225 0 Rex::Logger::info( "Unexpected ufw response: $ret", "warn" );
226 }
227 }
228 }
229  
230 0 ▶sub _ufw_exec {
Subroutine _ufw_exec: not called CC=3 SCAR=24.8
231 0 my $self = shift;
232 0 my $cmd = shift;
233  
234 0 $cmd = "ufw $cmd";
235  
236 0 ▶ if ( can_run("ufw") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_run('ufw')) { } 0 0 0
237 0 ▶ my ( $output, $err ) = i_run $cmd, sub { @_ }, fail_ok => 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
238  
239 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
240 0 Rex::Logger::info( "Error running ufw command: $cmd, received $err",
241 "warn" );
242 0 die("Error running ufw rule: $cmd");
243 }
244 0 Rex::Logger::debug("Output from ufw: $output");
245 0 return $output;
246 }
247 else {
248 0 Rex::Logger::info("UFW not found.");
249 0 die("UFW not found.");
250 }
251 }
252  
253 0 ▶sub _generate_rule_array {
Subroutine _generate_rule_array: not called CC=17 SCAR=57.2
254 0 my ( $self, $rule_config ) = @_;
255  
256 my $action = $__action_map{ $rule_config->{action} }
257 0 ▶ or die qq(Unknown action "$rule_config->{action}" for UFW provider);
Branch: 1 branch
BranchTrueFalseTotal
my $action = $__action_map{$rule_config->{"action"}} or die qq[Unknown action "$rule_config->{"action"}" for UFW provider] 0 0 0
258 0 ▶ $rule_config->{dport} ||= $rule_config->{port};
Condition0%0/3
$rule_config->{"dport"} ||= $rule_config->{"port"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rule_config->{"dport"} ||= $rule_config->{"port"}
$rule_config->{"dport"} $rule_config->{"port"}
Truth table0%0/3
A ||= B
A= $rule_config->{"dport"}B= $rule_config->{"port"}
ABresult
1X1
011
000
259 0 ▶ $rule_config->{dapp} ||= $rule_config->{app};
Condition0%0/3
$rule_config->{"dapp"} ||= $rule_config->{"app"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rule_config->{"dapp"} ||= $rule_config->{"app"}
$rule_config->{"dapp"} $rule_config->{"app"}
Truth table0%0/3
A ||= B
A= $rule_config->{"dapp"}B= $rule_config->{"app"}
ABresult
1X1
011
000
260 0 ▶ $rule_config->{source} ||= "any";
Condition0%0/2
$rule_config->{"source"} ||= "any"
l!l
00
MC/DC0%0/1
$rule_config->{"source"} ||= "any"
$rule_config->{"source"}
Truth table0%0/2
A
A= $rule_config->{"source"}
Aresult
11
00
261 0 ▶ $rule_config->{destination} ||= "any";
Condition0%0/2
$rule_config->{"destination"} ||= "any"
l!l
00
MC/DC0%0/1
$rule_config->{"destination"} ||= "any"
$rule_config->{"destination"}
Truth table0%0/2
A
A= $rule_config->{"destination"}
Aresult
11
00
262  
263 0 my @ufw_params = ();
264 0 push @ufw_params, $action;
265  
266 push( @ufw_params, "proto", $rule_config->{proto} )
267 0 ▶ if ( defined $rule_config->{proto} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"proto"} 0 0 0
268  
269 push( @ufw_params, "from", $rule_config->{source} )
270 0 ▶ if ( defined $rule_config->{source} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"source"} 0 0 0
271  
272 push( @ufw_params, "port", $rule_config->{sport} )
273 0 ▶ if ( defined $rule_config->{sport} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"sport"} 0 0 0
274  
275 push( @ufw_params, "app", qq("$rule_config->{sapp}") )
276 0 ▶ if ( defined $rule_config->{sapp} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"sapp"} 0 0 0
277  
278 push( @ufw_params, "to", $rule_config->{destination} )
279 0 ▶ if ( defined $rule_config->{destination} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"destination"} 0 0 0
280  
281 push( @ufw_params, "port", $rule_config->{dport} )
282 0 ▶ if ( defined $rule_config->{dport} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"dport"} 0 0 0
283  
284 push( @ufw_params, "app", qq("$rule_config->{dapp}") )
285 0 ▶ if ( defined $rule_config->{dapp} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"dapp"} 0 0 0
286  
287 push( @ufw_params, "direction", "in" )
288 0 ▶ if ( defined $rule_config->{iniface} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"iniface"} 0 0 0
289  
290 push( @ufw_params, "on", $rule_config->{iniface} )
291 0 ▶ if ( defined $rule_config->{iniface} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"iniface"} 0 0 0
292  
293 push( @ufw_params, "log", $rule_config->{log} )
294 0 ▶ if ( defined $rule_config->{log} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"log"} 0 0 0
295  
296 push( @ufw_params, "delete", $rule_config->{delete} )
297 0 ▶ if ( defined $rule_config->{delete} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"delete"} 0 0 0
298  
299 0 return @ufw_params;
300 }
301  
302 1 1;
« lib/Rex/Commands/Service.pm ↑ Summary lib/Rex/Interface/Connection/OpenSSH.pm »