@@ -11,96 +11,97 @@ use warnings;
11
11
12
12
# VERSION
13
13
14
+ use Moose;
15
+
16
+ extends qw( Rex::Resource::firewall::Provider::base) ;
17
+ with qw( Rex::Resource::Role::Ensureable) ;
18
+
14
19
use Rex::Commands::Iptables;
15
20
use Rex::Helper::Run;
21
+ use Rex::Resource::Common;
22
+
16
23
use Data::Dumper;
17
- use base qw( Rex::Resource::firewall::Provider::base) ;
18
24
19
- sub new {
20
- my $that = shift ;
21
- my $proto = ref ($that ) || $that ;
22
- my $self = $proto -> SUPER::new(@_ );
25
+ sub test {
26
+ my ($self ) = @_ ;
27
+
28
+ my $rule_config = $self -> config;
29
+ my @iptables_rule = $self -> _build_iptables_array(" A" );
23
30
24
- bless ( $self , $proto );
31
+ my $exists =
32
+ Rex::Commands::Iptables::_rule_exists( $rule_config -> {ip_version },
33
+ @iptables_rule );
25
34
26
- return $self ;
35
+ if ( $self -> config-> {ensure } eq " absent" && $exists ) {
36
+ return 0;
37
+ }
38
+ elsif ( $self -> config-> {ensure } eq " present" && !$exists ) {
39
+ return 0;
40
+ }
41
+
42
+ return 1;
27
43
}
28
44
29
45
sub present {
30
- my ( $self , $rule_config ) = @_ ;
46
+ my ($self ) = @_ ;
31
47
32
- my @iptables_rule = ();
48
+ my @iptables_rule = $self -> _build_iptables_array(" A" );
49
+ my $exit_code = 0;
50
+ eval {
51
+ iptables( $self -> config-> {ip_version }, @iptables_rule );
52
+ 1;
53
+ } or do {
54
+ $exit_code = 1;
55
+ };
56
+
57
+ return {
58
+ value => " " ,
59
+ exit_code => $exit_code ,
60
+ changed => 1,
61
+ status => ( $exit_code == 0 ? state_changed : state_failed ),
62
+ };
63
+ }
33
64
34
- $rule_config -> {dport } ||= $rule_config -> {port };
35
- $rule_config -> {proto } ||= ' tcp' ;
36
- $rule_config -> {chain } ||= ' INPUT' ;
37
- $rule_config -> {ip_version } ||= -4;
65
+ sub absent {
66
+ my ($self ) = @_ ;
38
67
39
- if ( $rule_config -> {source }
40
- && $rule_config -> {source } !~ m /\/ (\d +)$ /
41
- && $self -> _version()-> [0] >= 1
42
- && $self -> _version()-> [1] >= 4 )
43
- {
44
- $rule_config -> {source } .= " /32" ;
45
- }
68
+ my @iptables_rule = $self -> _build_iptables_array(" D" );
69
+ my $exit_code = 0;
70
+ eval {
71
+ iptables( $self -> config-> {ip_version }, @iptables_rule );
72
+ 1;
73
+ } or do {
74
+ $exit_code = 1;
75
+ };
76
+
77
+ return {
78
+ value => " " ,
79
+ exit_code => $exit_code ,
80
+ changed => 1,
81
+ status => ( $exit_code == 0 ? state_changed : state_failed ),
82
+ };
83
+ }
46
84
47
- push ( @iptables_rule , t => $rule_config -> {table } )
48
- if ( defined $rule_config -> {table } );
49
- push ( @iptables_rule , A => uc ( $rule_config -> {chain } ) )
50
- if ( defined $rule_config -> {chain } );
51
- push ( @iptables_rule , p => $rule_config -> {proto } )
52
- if ( defined $rule_config -> {proto } );
53
- push ( @iptables_rule , m = > $rule_config ->{proto} )
54
- if ( defined $rule_config ->{proto} );
55
- push( @iptables_rule , s => $rule_config ->{source} )
56
- if ( defined $rule_config ->{source} );
57
- push( @iptables_rule , d => $rule_config ->{destination} )
58
- if ( defined $rule_config ->{destination} );
59
- push( @iptables_rule , sport => $rule_config ->{sport} )
60
- if ( defined $rule_config ->{sport} );
61
- push( @iptables_rule , dport => $rule_config ->{dport} )
62
- if ( defined $rule_config ->{dport} );
63
- push( @iptables_rule , "tcp-flags" => $rule_config ->{tcp_flags} )
64
- if ( defined $rule_config ->{tcp_flags} );
65
- push( @iptables_rule , "i" => $rule_config ->{iniface} )
66
- if ( defined $rule_config ->{iniface} );
67
- push( @iptables_rule , "o" => $rule_config ->{outiface} )
68
- if ( defined $rule_config ->{outiface} );
69
- push( @iptables_rule , "reject-with" => $rule_config ->{reject_with} )
70
- if ( defined $rule_config ->{reject_with} );
71
- push( @iptables_rule , "log-level" => $rule_config ->{log_level} )
72
- if ( defined $rule_config ->{log_level} );
73
- push( @iptables_rule , "log-prefix" => $rule_config ->{log_prefix} )
74
- if ( defined $rule_config ->{log_prefix} );
75
- push( @iptables_rule , "state" => $rule_config ->{state} )
76
- if ( defined $rule_config ->{state} );
77
- push( @iptables_rule , j => uc( $rule_config ->{action} ) )
78
- if ( defined $rule_config ->{action} );
85
+ sub _version {
86
+ my ($self ) = @_ ;
87
+ if ( exists $self -> {__version__ } ) { return $self -> {__version__ } }
79
88
80
- if (
81
- !Rex::Commands::Iptables::_rule_exists(
82
- $rule_config ->{ip_version},
83
- @iptables_rule
84
- )
85
- )
86
- {
87
- iptables( $rule_config ->{ip_version}, @iptables_rule );
88
- return 1;
89
- }
89
+ my $version = i_run " iptables --version" ;
90
+ $version =~ s / ^.*\s v(\d +\.\d +\.\d +)/ $1 / ;
90
91
91
- return 0;
92
- }
92
+ $self -> {__version__ } = [ split ( / \. / , $version ) ];
93
93
94
- sub absent {
95
- my ( $self , $rule_config ) = @_ ;
94
+ Rex::Logger::debug(
95
+ " Got iptables version: " . join ( " , " , @{ $self -> { __version__ } } ) ) ;
96
96
97
- my @iptables_rule = ();
97
+ return $self -> {__version__ };
98
+ }
98
99
99
- $rule_config -> { dport } ||= $rule_config -> { port };
100
- $rule_config -> { proto } ||= ' tcp ' ;
101
- $rule_config -> { chain } ||= ' INPUT ' ;
100
+ sub _build_iptables_array {
101
+ my ( $self , $type ) = @_ ;
102
+ my $rule_config = $self -> config ;
102
103
103
- $rule_config -> { ip_version } ||= -4 ;
104
+ my @iptables_rule = () ;
104
105
105
106
if ( $rule_config -> {source }
106
107
&& $rule_config -> {source } !~ m /\/ (\d +)$ /
@@ -112,14 +113,14 @@ sub absent {
112
113
113
114
push ( @iptables_rule , t => $rule_config -> {table } )
114
115
if ( defined $rule_config -> {table } );
115
- push ( @iptables_rule , D => uc ( $rule_config -> {chain } ) )
116
+ push ( @iptables_rule , $type => uc ( $rule_config -> {chain } ) )
116
117
if ( defined $rule_config -> {chain } );
117
- push ( @iptables_rule , s => $rule_config -> {source } )
118
- if ( defined $rule_config -> {source } );
119
118
push ( @iptables_rule , p => $rule_config -> {proto } )
120
119
if ( defined $rule_config -> {proto } );
121
120
push ( @iptables_rule , m = > $rule_config ->{proto} )
122
121
if ( defined $rule_config ->{proto} );
122
+ push( @iptables_rule , s => $rule_config ->{source} )
123
+ if ( defined $rule_config ->{source} );
123
124
push( @iptables_rule , d => $rule_config ->{destination} )
124
125
if ( defined $rule_config ->{destination} );
125
126
push( @iptables_rule , sport => $rule_config ->{sport} )
@@ -143,33 +144,7 @@ sub absent {
143
144
push( @iptables_rule , j => uc( $rule_config ->{action} ) )
144
145
if ( defined $rule_config ->{action} );
145
146
146
- if (
147
- Rex::Commands::Iptables::_rule_exists(
148
- $rule_config ->{ip_version},
149
- @iptables_rule
150
- )
151
- )
152
- {
153
- iptables( $rule_config ->{ip_version}, @iptables_rule );
154
- return 1;
155
- }
156
-
157
- return 0;
158
- }
159
-
160
- sub _version {
161
- my ($self ) = @_ ;
162
- if ( exists $self -> {__version__ } ) { return $self -> {__version__ } }
163
-
164
- my $version = i_run " iptables --version" , fail_ok => 1;
165
- $version =~ s / ^.*\s v(\d +\.\d +\.\d +)/ $1 / ;
166
-
167
- $self -> {__version__ } = [ split ( / \. / , $version ) ];
168
-
169
- Rex::Logger::debug(
170
- " Got iptables version: " . join ( " , " , @{ $self -> {__version__ } } ) );
171
-
172
- return $self -> {__version__ };
147
+ return @iptables_rule ;
173
148
}
174
149
175
150
1;
0 commit comments