lib/Rex/Resource/firewall.pm

filter: Statementstmt 54.5% 24 / 44 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 41.8% 31 / 74 CC 12 SCAR 45.9
CC
12
Cov
36%
__ANON__
156.0
BEGIN
1.0
BEGIN
1.0
CRAP 98.9
« lib/Rex/Commands/Mkfs.pm ↑ Summary lib/Rex/Virtualization/LibVirt/create.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Resource::firewall - Firewall functions
8  
9 =head1 DESCRIPTION
10  
11 With this module it is easy to manage different firewall systems.
12  
13 =head1 SYNOPSIS
14  
15 # Configure a particular rule
16 task "configure_firewall", "server01", sub {
17 firewall "some-name",
18 ensure => "present",
19 proto => "tcp",
20 action => "accept",
21 source => "192.168.178.0/24",
22 destination => "192.168.1.0/24",
23 sport => 80,
24 sapp => 'www', # source application, if provider supports it
25 port => 80, # same as dport
26 dport => 80,
27 app => 'www', # same as dapp, destination application, if provider supports it
28 dapp => 'www', # destination application, if provider supports it
29 tcp_flags => ["FIN", "SYN", "RST"],
30 chain => "INPUT",
31 table => "nat",
32 jump => "LOG",
33 iniface => "eth0",
34 outiface => "eth1",
35 reject_with => "icmp-host-prohibited",
36 log => "new|all", # if provider supports it
37 log_level => "", # if provider supports it
38 log_prefix => "FW:", # if provider supports it
39 state => "NEW",
40 ip_version => -4; # for iptables provider. valid options -4 and -6
41 };
42  
43 # Add overall logging (if provider supports)
44 firewall "some-name",
45 provider => 'ufw',
46 logging => "medium";
47  
48 =head1 EXPORTED RESOURCES
49  
50 =over 4
51  
52 =cut
53  
54 package Rex::Resource::firewall;
55  
56 32 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 32 our $VERSION = '1.16.1'; # VERSION
60  
61 32 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 32 ▶use Rex -minimal;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 32 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 32 ▶use Rex::Resource::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 32 my $__provider = { default => "Rex::Resource::firewall::Provider::iptables", };
71  
72 =item firewall($name, %params)
73  
74 =cut
75  
76 0 ▶resource "firewall", { export => 1 }, sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
77 0 my $rule_name = resource_name;
78  
79 0 my $rule_config = {
80 action => param_lookup("action"),
81 ensure => param_lookup( "ensure", "present" ),
82 proto => param_lookup( "proto", undef ),
83 source => param_lookup( "source", undef ),
84 destination => param_lookup( "destination", undef ),
85 port => param_lookup( "port", undef ),
86 app => param_lookup( "app", undef ),
87 sport => param_lookup( "sport", undef ),
88 sapp => param_lookup( "sapp", undef ),
89 dport => param_lookup( "dport", undef ),
90 dapp => param_lookup( "dapp", undef ),
91 tcp_flags => param_lookup( "tcp_falgs", undef ),
92 chain => param_lookup( "chain", "input" ),
93 table => param_lookup( "table", "filter" ),
94 iniface => param_lookup( "iniface", undef ),
95 outiface => param_lookup( "outiface", undef ),
96 reject_with => param_lookup( "reject_with", undef ),
97 logging => param_lookup( "logging", undef ), # overall logging
98 log => param_lookup( "log", undef ), # logging for rule
99 log_level => param_lookup( "log_level", undef ), # logging for rule
100 log_prefix => param_lookup( "log_prefix", undef ),
101 state => param_lookup( "state", undef ),
102 ip_version => param_lookup( "ip_version", -4 ),
103 };
104  
105 0 my $provider =
106 param_lookup( "provider", case ( lc(operating_system), $__provider ) );
107  
108 0 ▶ if ( $provider !~ m/::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($provider =~ /::/u) 0 0 0
109 0 $provider = "Rex::Resource::firewall::Provider::$provider";
110 }
111  
112 0 $provider->require;
113 0 my $provider_o = $provider->new();
114  
115 0 my $changed = 0;
116 0 ▶ if ( my $logging = $rule_config->{logging} ) {
Branch: 5 branches
BranchTrueFalseTotal
if (my $logging = $rule_config->{'logging'}) { } 0 0 0
elsif ($rule_config->{'ensure'} eq 'present') { } 0 0 0
elsif ($rule_config->{'ensure'} eq 'absent') { } 0 0 0
elsif ($rule_config->{'ensure'} eq 'disabled') { } 0 0 0
elsif ($rule_config->{'ensure'} eq 'enabled') { } 0 0 0
117 0 ▶ if ( $provider_o->logging($logging) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider_o->logging($logging)) 0 0 0
118 0 emit changed, "Firewall logging updated.";
119 }
120 }
121 elsif ( $rule_config->{ensure} eq "present" ) {
122 0 ▶ if ( $provider_o->present($rule_config) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider_o->present($rule_config)) 0 0 0
123 0 emit created, "Firewall rule created.";
124 }
125 }
126 elsif ( $rule_config->{ensure} eq "absent" ) {
127 0 ▶ if ( $provider_o->absent($rule_config) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider_o->absent($rule_config)) 0 0 0
128 0 emit removed, "Firewall rule removed.";
129 }
130 }
131 elsif ( $rule_config->{ensure} eq "disabled" ) {
132 0 ▶ if ( $provider_o->disable($rule_config) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider_o->disable($rule_config)) 0 0 0
133 0 emit changed, "Firewall disabled.";
134 }
135 }
136 elsif ( $rule_config->{ensure} eq "enabled" ) {
137 0 ▶ if ( $provider_o->enable($rule_config) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider_o->enable($rule_config)) 0 0 0
138 0 emit changed, "Firewall enabled.";
139 }
140 }
141 else {
142 0 die "Error: $rule_config->{ensure} not a valid option for 'ensure'.";
143 }
144  
145 32 };
146  
147 =back
148  
149 =cut
150  
151 32 1;
« lib/Rex/Commands/Mkfs.pm ↑ Summary lib/Rex/Virtualization/LibVirt/create.pm »