forked from UPC/ravada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n10_nat.t
205 lines (151 loc) · 6.38 KB
/
n10_nat.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use warnings;
use strict;
use Carp qw(confess);
use Data::Dumper;
use Test::More;
use YAML qw(DumpFile);
use lib 't/lib';
use Test::Ravada;
no warnings "experimental::signatures";
use feature qw(signatures);
use_ok('Ravada');
my $FILE_CONFIG = "t/etc/ravada.conf";
init( );
my $NAT_IP = '2.2.2.2';
my $REMOTE_IP = '9.9.9.9';
my $CHAIN = 'RAVADA';
##################################################################################
sub _search_other_ip($ip) {
my $out = `ifconfig`;
for my $line ( split /\n/, $out ) {
my ($ip2) = $line =~ /inet.(\d+\.\d+\.\d+\.\d+) /;
return $ip2 if $ip2
&& $ip2 ne $ip
&& $ip2 ne '127.0.0.1';
}
die "I can't find another ip address here";
}
sub test_nat($vm_name) {
my $domain = create_domain($vm_name);
$domain->shutdown_now() if $domain->is_active;
$domain->start(user => user_admin, remote_ip => $REMOTE_IP );
#-------------------------------------------------------------------------------
# Test Display
#
my $display;
eval { $display = $domain->display(user_admin)};
is($@,'');
ok($display,"Expecting a display URI, got '".($display or '')."'") or return;
my ($ip, $port) = $display =~ m{^\w+://(.*):(\d+)} if defined $display;
ok($ip, "Expecting an IP , got ''") or return;
test_chain($vm_name, local_ip => $ip, local_port => $port, remote_ip => $REMOTE_IP);
test_chain($vm_name, local_ip => $ip, local_port => $port, remote_ip => '0.0.0.0/0'
, jump => 'DROP');
isnt($ip , '127.0.0.1', "[$vm_name] Expecting IP no '127.0.0.1', got '$ip'")
or exit;
$domain->shutdown_now(user_admin) if $domain->is_active;
test_chain($vm_name, local_ip => $ip, local_port => $port, remote_ip => $REMOTE_IP
, enabled => 0);
test_chain($vm_name, local_ip => $ip, local_port => $port, remote_ip => '0.0.0.0/0'
, jump => 'DROP', enabled => 0);
#--------------------------------------------------------------------------------
# Test forcing display ip
#
my $display_ip = _search_other_ip( $ip );
isnt($display_ip, $ip);
like($display_ip, qr{\d+\.\d+\.\d+\.\d+});
my $file_config = "/tmp/config_display.yml";
DumpFile($file_config,{ display_ip => $display_ip, vm => ['Void', 'KVM'] });
my $rvd_back = Ravada->new(
connector => connector()
, config => $file_config
);
is($rvd_back->display_ip, $display_ip);
is($rvd_back->search_vm($vm_name)->ip, $display_ip);
$domain = $rvd_back->search_domain($domain->name);
$domain->start(user => user_admin, remote_ip => $REMOTE_IP );
eval { $display = $domain->display(user_admin)};
is($@,'');
my ($ip2) = $display =~ m{^\w+://(.*):\d+} if defined $display;
is($ip2, $display_ip);
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => $REMOTE_IP
, msg => "display");
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => '0.0.0.0/0'
, jump => 'DROP');
$domain->shutdown_now(user_admin) if $domain->is_active;
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => $REMOTE_IP
, enabled => 0);
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => '0.0.0.0/0'
, jump => 'DROP', enabled => 0);
#--------------------------------------------------------------------------------
# Now with Nat
#
DumpFile($file_config,{ display_ip => $display_ip, nat_ip => $NAT_IP, vm => ['Void', 'KVM'] });
$rvd_back = Ravada->new(
connector => connector()
, config => $file_config
);
is($rvd_back->nat_ip, $NAT_IP);
is($rvd_back->search_vm($vm_name)->nat_ip, $NAT_IP);
$domain = $rvd_back->search_domain($domain->name);
$domain->start(user => user_admin, remote_ip => $REMOTE_IP );
eval { $display = $domain->display(user_admin)};
my ($ip3) = $display =~ m{^\w+://(.*):\d+} if defined $display;
is($ip3, $NAT_IP,"[$vm_name] Expecting NAT_IP in display");
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => $REMOTE_IP
, msg => 'nat');
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => '0.0.0.0/0'
, jump => 'DROP', msg => 'nat');
$domain->shutdown_now(user_admin) if $domain->is_active;
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => $REMOTE_IP
, enabled => 0, msg => 'nat');
test_chain($vm_name, local_ip => $display_ip, local_port => $port, remote_ip => '0.0.0.0/0'
, jump => 'DROP', enabled => 0, msg => 'nat');
unlink($file_config);
rvd_back($FILE_CONFIG);
}
sub test_chain($vm_name, %args) {
SKIP: {
skip("iptables test must be run from root user",2);
my $jump = (delete $args{jump} or 'ACCEPT');
my $local_ip = delete $args{local_ip} or confess "Missing local_ip";
my $remote_ip = delete $args{remote_ip} or confess "Missing remote_ip";
my $local_port= delete $args{local_port} or confess "Missing local_port";
my $enabled = delete $args{enabled};
$enabled = 1 if !defined $enabled;
my $msg = ( delete $args{msg} or '' );
confess "Unknown args ".join(",",sort keys %args) if keys %args;
my $ipt = open_ipt();
my ($rule_num , $chain_rules)
= $ipt->find_ip_rule($remote_ip, $local_ip,'filter', $CHAIN, $jump
, {normalize => 1 , d_port => $local_port });
my $msg2 = "[$vm_name]";
$msg2 = "[$vm_name - $msg]" if $msg;
ok($rule_num,"$msg2 Expecting rule for $remote_ip -> $local_ip: $local_port $jump")
or exit
if $enabled;
ok(!$rule_num,"$msg2 Expecting no rule for $remote_ip -> $local_ip: $local_port"
.", found at $rule_num ")
if !$enabled;
}
}
##################################################################################
clean();
flush_rules();
for my $vm_name ( 'Void', 'KVM' ) {
my $vm;
eval { $vm = rvd_back->search_vm($vm_name) };
SKIP: {
my $msg = "SKIPPED test: No $vm_name VM found ";
if ($vm && $vm_name =~ /kvm/i && $>) {
$msg = "SKIPPED: Test must run as root";
$vm = undef;
}
diag($msg) if !$vm;
skip $msg,10 if !$vm;
test_nat($vm_name);
}
}
clean();
flush_rules();
done_testing();