92_ports.t 42.1 KB
Newer Older
Francesc Guasch's avatar
Francesc Guasch committed
1
2
3
4
5
use warnings;
use strict;

use Carp qw(confess);
use Data::Dumper;
6
use IPC::Run3;
Francesc Guasch's avatar
Francesc Guasch committed
7
8
9
10
11
12
13
14
15
16
17
18
use JSON::XS;
use Test::More;
use IPTables::ChainMgr;

use lib 't/lib';
use Test::Ravada;

no warnings "experimental::signatures";
use feature qw(signatures);

use_ok('Ravada');

19
20
21
my $BASE_NAME = "zz-test-base-alpine";
my $BASE;

22
23
24
25
sub test_no_dupe($vm) {

    flush_rules($vm);

26
    my $domain= $BASE->clone(name => new_domain_name, user => user_admin);
27
28
29
30
31
32
33

    my $remote_ip = '10.0.0.1';
    my $local_ip = $vm->ip;

    my ($internal_port, $name_port) = (22, 'ssh');

    my ($in, $out, $err);
34
35
    run3(['iptables','-t','nat','-L','PREROUTING','-n'],\($in, $out, $err));
    die $err if $err;
36
37
38
    my @out = split /\n/,$out;
    is(grep(/^DNAT.*/,@out),0);

39
40
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
41
42
43
44
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*192.168.\d+\.0/24\sstate NEW},@out),0);

    $domain->start(user => user_admin, remote_ip => $remote_ip);
45
    my @request = grep { $_->command ne 'set_time'} $domain->list_requests();
46
47
48
49
50
51
52
53
54

    # No requests because no ports exposed
    is(scalar @request,0) or exit;
    delete_request('enforce_limits');
    wait_request(debug => 0, background => 0);

    my $client_ip = $domain->remote_ip();
    is($client_ip, $remote_ip);
    my $public_port;
55
    my $internal_ip = _wait_ip2($vm->type, $domain) or die "Error: no ip for ".$domain->name;
56
57
58
59
60
61
62
63
64
65
66
    my $internal_net = $internal_ip;
    $internal_net =~ s{(.*)\.\d+$}{$1.0/24};

    ($public_port) = $domain->expose(
        port => $internal_port
        , name => $name_port
        , restricted => 1
    );
    delete_request('enforce_limits');
    wait_request(background => 0, debug => 0);

67
    run3(['iptables','-t','nat','-L','PREROUTING','-n'],\($in, $out, $err));
68
69
70
    @out = split /\n/,$out;
    is(grep(/^DNAT.*$local_ip.*dpt:$public_port to:$internal_ip:$internal_port/,@out),1);

71
72
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
73
74
75
76
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*$internal_net\s+state NEW},@out),1,"Expecting rule for $internal_net")
        or die $out;

77
78
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
79
80
81
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*$remote_ip\s+$internal_ip.*dpt:$internal_port},@out),1) or die $out;
    is(grep(m{^DROP.*0.0.0.0.+$internal_ip.*dpt:$internal_port},@out),1) or die $out;
Francesc Guasch's avatar
Francesc Guasch committed
82

83
84
85
86
87
88
    ##########################################################################
    #
    # start again check only one instance of each
    #
    $domain->start(user => user_admin, remote_ip => $remote_ip);
    is($domain->is_active,1);
89
90
    run3(['iptables','-t','nat','-L','PREROUTING','-n'],\($in, $out, $err));
    die $err if $err;
91
92
93
    @out = split /\n/,$out;
    is(grep(/^DNAT.*$local_ip.*dpt:$public_port to:$internal_ip:$internal_port/,@out),1);

94
95
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
96
97
98
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*$internal_net\s+state NEW},@out),1) or die $out;

99
100
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*$remote_ip\s+$internal_ip.*dpt:$internal_port},@out),1) or die $out;
    is(grep(m{^DROP.*0.0.0.0.+$internal_ip.*dpt:$internal_port},@out),1) or die $out;

    test_hibernate($domain, $local_ip, $public_port, $internal_ip, $internal_port,$remote_ip);
    test_start_after_hibernate($domain, $local_ip, $public_port, $internal_ip, $internal_port,$remote_ip);
    $domain->remove(user_admin);

    flush_rules($vm);
}

sub test_hibernate($domain
        ,$local_ip, $public_port, $internal_ip, $internal_port, $remote_ip) {
    $domain->hibernate(user_admin);
    is($domain->is_hibernated,1);

    my ($in,$out,$err);
118
119
    run3(['iptables','-t','nat','-L','PREROUTING','-n'],\($in, $out, $err));
    die $err if $err;
120
121
122
    my @out = split /\n/,$out;
    is(grep(/^DNAT.*$local_ip.*dpt:$public_port to:$internal_ip:$internal_port/,@out),0);

123
124
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
125
126
127
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*192.168.\d+\.0/24\sstate NEW},@out),0);

128
129
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*$remote_ip\s+$internal_ip.*dpt:$internal_port},@out),0) or die $out;
    is(grep(m{^DROP.*0.0.0.0.+$internal_ip.*dpt:$internal_port},@out),0) or die $out;

}

sub test_start_after_hibernate($domain
        ,$local_ip, $public_port, $internal_ip, $internal_port, $remote_ip) {

    my $internal_net = $internal_ip;
    $internal_net =~ s{(.*)\.\d+$}{$1.0/24};

    $domain->start(user => user_admin, remote_ip => $remote_ip);

    delete_request('enforce_limits');
    wait_request(debug => 0, background => 0);

    my ($in,$out,$err);
148
149
    run3(['iptables','-t','nat','-L','PREROUTING','-n'],\($in, $out, $err));
    die $err if $err;
150
151
152
    my @out = split /\n/,$out;
    is(grep(/^DNAT.*$local_ip.*dpt:$public_port to:$internal_ip:$internal_port/,@out),1);

153
154
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
155
156
157
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*$internal_net\s+state NEW},@out),1) or die $out;

158
159
    run3(['iptables','-L','FORWARD','-n'],\($in, $out, $err));
    die $err if $err;
160
161
162
163
164
165
166
167
    @out = split /\n/,$out;
    is(grep(m{^ACCEPT.*$remote_ip\s+$internal_ip.*dpt:$internal_port},@out),1) or die $out;
    is(grep(m{^DROP.*0.0.0.0.+$internal_ip.*dpt:$internal_port},@out),1) or die $out;

}


##############################################################
Francesc Guasch's avatar
Francesc Guasch committed
168
169
170
171
172
# Forward one port
sub test_one_port($vm) {

    flush_rules();

173
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

    my $remote_ip = '10.0.0.1';
    my $local_ip = $vm->ip;

    $domain->start(user => user_admin, remote_ip => $remote_ip);

    my $client_ip = $domain->remote_ip();
    is($client_ip, $remote_ip);

    _wait_ip($vm->type, $domain);

    my $domain_ip = $domain->ip;
    ok($domain_ip,"[".$vm->type."] Expecting an IP for domain ".$domain->name.", got ".($domain_ip or '')) or return;
    is(scalar $domain->list_ports,0);

    my ($internal_port, $name_port) = (22, 'ssh');
    my $public_port;
    eval {
       ($public_port) = $domain->expose(port => $internal_port, name => $name_port);
    };
    is($@,'',"[".$vm->type."] export port $internal_port");

Francesc Guasch's avatar
Francesc Guasch committed
196
    my $port_info_no = $domain->exposed_port(456);
197
    is($port_info_no,undef);
Francesc Guasch's avatar
Francesc Guasch committed
198
199

    $port_info_no = $domain->exposed_port('no');
200
    is($port_info_no,undef);
Francesc Guasch's avatar
Francesc Guasch committed
201
202
203
204
205
206
207
208
209
210
211

    my $port_info = $domain->exposed_port($name_port);
    ok($port_info) && do {
        is($port_info->{name}, $name_port);
        is($port_info->{internal_port}, $internal_port);
    };

    my $port_info2 = $domain->exposed_port($internal_port);
    ok($port_info2);
    is_deeply($port_info2, $port_info);

Francesc Guasch's avatar
Francesc Guasch committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    my @list_ports = $domain->list_ports();
    is(scalar @list_ports,1);

    my $info = $domain->info(user_admin);
    ok($info->{ports});
    is($info->{ports}->[0]->{internal_port}, $internal_port);
    is($info->{ports}->[0]->{public_port}, $public_port);
    is($info->{ports}->[0]->{name}, $name_port);

    my ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
            , 'to-destination' => $domain->ip.":".$internal_port
    );

    ok($n_rule,"Expecting rule for -> $local_ip:$public_port") or exit;

233

Francesc Guasch's avatar
Francesc Guasch committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    #################################################################
    #
    # shutdown
    local $@ = undef;
    eval { $domain->shutdown_now(user_admin) };
    is($@, '');

    ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
    );

    ok(!$n_rule,"Expecting no rule for -> $local_ip:$public_port") or exit;
Francesc Guasch's avatar
Francesc Guasch committed
251
    wait_request();
Francesc Guasch's avatar
Francesc Guasch committed
252
253
254
255
256

    #################################################################
    # start
    #
    $domain->start(user => user_admin, remote_ip => $remote_ip);
257
258
    delete_request('enforce_limits');
    wait_request(debug => 0, background => 0);
Francesc Guasch's avatar
Francesc Guasch committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

    ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
            , 'to-destination' => $domain->ip.":".$internal_port
    );

    ok($n_rule,"Expecting rule for -> $local_ip:$public_port") or exit;

    #################################################################
    #
    # remove
    local $@ = undef;
    eval { $domain->remove(user_admin) };
    is($@, '');
    ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
    );

    ok(!$n_rule,"Expecting no rule for -> $local_ip:$public_port") or exit;

}

# Remove expose port
sub test_remove_expose {
    my $vm_name = shift;
    my $request = shift;

    my $vm = rvd_back->search_vm($vm_name);

298
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

    my $remote_ip = '10.0.0.1';
    my $local_ip = $vm->ip;

    $domain->start(user => user_admin, remote_ip => $remote_ip);

    my $client_ip = $domain->remote_ip();
    is($client_ip, $remote_ip);

    #    my $client_user = $domain->remote_user();
    # is($client_user->id, user_admin->id);

    _wait_ip($vm_name, $domain);

    my $domain_ip = $domain->ip;
    ok($domain_ip,"[$vm_name] Expecting an IP for domain ".$domain->name.", got ".($domain_ip or '')) or return;

    my $internal_port = 22;
    my ($public_port0) = $domain->expose($internal_port);
    ok($public_port0,"Expecting a public port") or exit;

    is(scalar $domain->list_ports,1);

    #    my ($public_ip, $public_port) = $domain->public_address($internal_port);
    #    is($public_ip, $public_ip0);
    #    is($public_port, $public_port0);
    my $public_port = $public_port0;

    my ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
    );

    ok($n_rule,"Expecting rule for -> $local_ip:$public_port") or exit;

    #################################################################
    #
    # remove expose
    if (!$request) {
        local $@ = undef;
        eval { $domain->remove_expose($internal_port) };
        is($@, '');
    } else {
        my $req = Ravada::Request->remove_expose(
                   uid => user_admin->id
                 ,port => $internal_port
            ,id_domain => $domain->id
        );
        rvd_back->_process_all_requests_dont_fork();

        is($req->status(),'done');
        is($req->error(),'');
    }
    is(scalar $domain->list_ports,0) or exit;
    ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
    );

    ok(!$n_rule,"Expecting no rule for -> $local_ip:$public_port") or exit;

    $domain->shutdown_now(user_admin);
    ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
    );

    ok(!$n_rule,"Expecting no rule for -> $local_ip:$public_port") or exit;
}

sub test_req_remove_expose {
    flush_rules();
    test_remove_expose(@_,'request');
}

# Remove crash a domain and see if ports are closed after cleanup
sub test_crash_domain($vm_name) {
    flush_rules();

    my $vm = rvd_back->search_vm($vm_name);

392
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428

    my $remote_ip = '10.0.0.1';
    my $local_ip = $vm->ip;

    $domain->start(user => user_admin, remote_ip => $remote_ip);

    my $client_ip = $domain->remote_ip();
    is($client_ip, $remote_ip);

    _wait_ip($vm_name, $domain);

    my $domain_ip = $domain->ip or do {
        diag("[$vm_name] Expecting an IP for domain ".$domain->name);
        return;
    };

    my $internal_port = 22;
    my $public_port = $domain->expose($internal_port);

    is(scalar $domain->list_ports,1);
    my ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
    );

    is($n_rule,1,"Expecting rule for $remote_ip -> $local_ip:$public_port") or exit;

    #################################################################
    #
    # shutdown forced
    shutdown_domain_internal($domain);

429
    my $domain2 = $BASE->clone(name => new_domain_name, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
430
431
432
433
434
435
436
437
438
    $domain2->start(user => user_admin) if !$domain2->is_active;

    $domain2->remove(user_admin);
}

sub test_two_ports($vm) {

    flush_rules();

439
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

    my $remote_ip = '10.0.0.1';
    my $local_ip = $vm->ip;

    $domain->start(user => user_admin, remote_ip => $remote_ip);

    my $client_ip = $domain->remote_ip();
    is($client_ip, $remote_ip);

    _wait_ip($vm->type, $domain);

    my $domain_ip = $domain->ip;
    ok($domain_ip,"[".$vm->type
        ."] Expecting an IP for domain ".$domain->name.", got ".($domain_ip or '')) or return;

    my $internal_port1 = 10;
    my $public_port1 = $domain->expose($internal_port1);

    my $internal_port2 = 20;
    my $public_port2 = $domain->expose($internal_port2);

    ok($public_port1 ne $public_port2,"Expecting two different ports "
        ." $public_port1 $public_port2 ");

    for my $public_port ( $public_port1, $public_port2 ) {
        my ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
        );

        ok($n_rule,"Expecting rule for -> $local_ip:$public_port") or exit;
    }

    local $@ = undef;
    eval { $domain->shutdown_now(user_admin) };
    is($@, '');

    for my $public_port ( $public_port1, $public_port2 ) {
        my ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
        );

        ok(!$n_rule,"Expecting no rule for -> $local_ip:$public_port") or exit;
    }
}

495
496
497
sub test_clone_exports_spinoff($vm) {
    test_clone_exports($vm,1);
}
Francesc Guasch's avatar
Francesc Guasch committed
498

499
sub test_clone_exports($vm, $spinoff=0) {
Francesc Guasch's avatar
Francesc Guasch committed
500

501
502
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);
    $base->spinoff() if $spinoff;
Francesc Guasch's avatar
Francesc Guasch committed
503
504
505
    $base->expose(port => 22, name => "ssh");

    my @base_ports = $base->list_ports();
506
    is(scalar @base_ports,1);
Francesc Guasch's avatar
Francesc Guasch committed
507
508
509
510

    my $clone = $base->clone(name => new_domain_name, user => user_admin);

    my @clone_ports = $clone->list_ports();
511
    is(scalar @clone_ports,1, "Expecting ports listed spinoff=$spinoff" );
Francesc Guasch's avatar
Francesc Guasch committed
512
513
514
515
516
517
518
519
520

    is($base_ports[0]->{internal_port}, $clone_ports[0]->{internal_port});
    isnt($base_ports[0]->{public_port}, $clone_ports[0]->{public_port});
    is($base_ports[0]->{name}, $clone_ports[0]->{name});

    $clone->remove(user_admin);
    $base->remove(user_admin);
}

Francesc Guasch's avatar
Francesc Guasch committed
521
sub test_routing_hibernated($vm) {
Francesc Guasch's avatar
Francesc Guasch committed
522
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);
frankiejol's avatar
frankiejol committed
523
524
    my $internal_port = 22;

Francesc Guasch's avatar
Francesc Guasch committed
525
    $base->expose(port => $internal_port, name => "ssh");
Francesc Guasch's avatar
Francesc Guasch committed
526
527
528
529
530
531
532
533

    my @base_ports0 = $base->list_ports();
    my $public_port0 = $base_ports0[0]->{public_port};

    my $remote_ip = '4.4.4.4';
    $base->start(remote_ip => $remote_ip,  user => user_admin);

    _wait_ip($vm, $base);
frankiejol's avatar
frankiejol committed
534
    my $internal_ip = $base->ip;
Francesc Guasch's avatar
Francesc Guasch committed
535
536
537
538
539
540
541
542
    wait_request( debug => 0 );

    my @base_ports1 = $base->list_ports();

    my $public_port1 = $base_ports1[0]->{public_port};

    is($public_port1, $public_port0) or exit;

frankiejol's avatar
frankiejol committed
543
544
545
546
547
548
549
550
551
552
    my ($out, $err) = $vm->run_command("iptables-save");
    my @lines = grep(/$internal_ip:$internal_port/, split /\n/,$out);
    is (scalar @lines,1) or die Dumper(\@lines);

    my @lines0 = grep(/-A FORWARD .*ACCEPT$/, split/\n/,$out);
    @lines = grep(m{d $internal_ip/32 .*dport $internal_port}, @lines0);
    is (scalar @lines,1,"Expecting 1 line $internal_ip .*dport $internal_port")
        or die Dumper($internal_ip,\@lines0);


Francesc Guasch's avatar
Francesc Guasch committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    hibernate_domain_internal($base);

    $base->start(remote_ip => $remote_ip,  user => user_admin);

    _wait_ip($vm, $base);
    wait_request( debug => 0 );

    my @base_ports2 = $base->list_ports();

    my $public_port2 = $base_ports2[0]->{public_port};

    is($public_port2, $public_port0) or exit;
    is($public_port2, $public_port1) or exit;

frankiejol's avatar
frankiejol committed
567
568
569
570
571
572
573
574
575
    ($out, $err) = $vm->run_command("iptables-save");
    @lines = grep(/$internal_ip:$internal_port/, split /\n/,$out);
    is (scalar @lines,1) or die Dumper(\@lines);

    @lines0 = grep(/-A FORWARD .*ACCEPT$/, split/\n/,$out);
    @lines = grep(m{d $internal_ip/32 .*dport $internal_port}, @lines0);
    is (scalar @lines,1,"Expecting 1 line $internal_ip .*dport $internal_port")
        or die Dumper($internal_ip,\@lines0);

Francesc Guasch's avatar
Francesc Guasch committed
576
577
578
    $base->remove(user_admin);
}

frankiejol's avatar
frankiejol committed
579
sub test_routing_already_used($vm, $source=0, $restricted=0) {
Francesc Guasch's avatar
Francesc Guasch committed
580
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);
frankiejol's avatar
frankiejol committed
581
582
    my $internal_port = 22;
    $base->expose(port => $internal_port, name => "ssh", restricted => $restricted);
Francesc Guasch's avatar
Francesc Guasch committed
583
584
585
586
    my @base_ports0 = $base->list_ports();

    my $public_port0 = $base_ports0[0]->{public_port};

frankiejol's avatar
frankiejol committed
587
588
    my @source;
    @source = ( 's' => '0.0.0.0/0');
Francesc Guasch's avatar
Francesc Guasch committed
589
590
591
592
593
594
595
    $vm->iptables_unique(
            t => 'nat'
            ,A => 'PREROUTING'
            ,p => 'tcp'
            ,dport => $public_port0
            ,j => 'DNAT'
            ,'to-destination' => "1.2.3.4:1111"
frankiejol's avatar
frankiejol committed
596
            ,@source
Francesc Guasch's avatar
Francesc Guasch committed
597
598
599
600
601
602
603
    );

    my @iptables0 = _iptables_save($vm,'nat','PREROUTING');
    my $remote_ip = '3.3.3.3';
    $base->start(remote_ip => $remote_ip,  user => user_admin);

    _wait_ip($vm, $base);
frankiejol's avatar
frankiejol committed
604
    my $internal_ip = $base->ip;
Francesc Guasch's avatar
Francesc Guasch committed
605
606
607
608
609
610
611
612
613
614
615
    wait_request( debug => 0 );

    my @base_ports1 = $base->list_ports();

    my $public_port1 = $base_ports1[0]->{public_port};

    isnt($public_port1, $public_port0) or exit;
    my @iptables1 = _iptables_save($vm,'nat' ,'PREROUTING');
    is(scalar(@iptables1),scalar(@iptables0)+1,"Expecting 1 chain more "
    .Dumper(\@iptables0,\@iptables1)) or exit;

frankiejol's avatar
frankiejol committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
    my @lines0 = grep(/-A FORWARD .*ACCEPT$/, _iptables_save($vm));
    my @lines = grep(m{d $internal_ip/32 .*dport $internal_port}, @lines0);
    is (scalar @lines,1,"Expecting 1 line $internal_ip .*dport $internal_port")
        or die Dumper($internal_ip,\@lines0);

    # start again the machine, nothing should change
    for ( 1 .. 3 ) {
        my $req = Ravada::Request->start_domain(
            uid => user_admin->id
            ,id_domain => $base->id
            ,remote_ip => $remote_ip
        );
        wait_request(debug => 0);
        is($req->status,'done');
        is($req->error, '');

        my @base_ports2 = $base->list_ports();

        my $public_port2 = $base_ports2[0]->{public_port};

        isnt($public_port2, $public_port0) or exit;
        is($public_port2, $public_port1) or exit;

        my @iptables2 = _iptables_save($vm,'nat' ,'PREROUTING');
        is(scalar(@iptables1),scalar(@iptables2)) or die Dumper(\@iptables1,\@iptables2);

        my ($out, $err) = $vm->run_command("iptables-save");
        my @lines = grep(/$internal_ip:$internal_port/, split /\n/,$out);
        is (scalar @lines,1) or die Dumper(\@lines);

        my @lines0 = grep(/-A FORWARD /, split/\n/,$out);
        @lines = grep(m{d $internal_ip/32 .*dport $internal_port -j ACCEPT}, @lines0);
        is (scalar @lines,1,"Expecting 1 line $internal_ip .*dport $internal_port")
        or die Dumper($internal_ip,\@lines0);

        if ($restricted) {
            @lines = grep(m{d $internal_ip/32 .*dport 22 -j DROP}, @lines0);
            is (scalar @lines,1,"Expecting 1 $internal_ip .*dport $internal_port -j DROP")
            or die Dumper($internal_ip,\@lines0);
        }

    }

Francesc Guasch's avatar
Francesc Guasch committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
    # open again the ports, nothing should change
    for ( 1 .. 3 ) {
        my $req = Ravada::Request->open_iptables(
            uid => user_admin->id
            ,id_domain => $base->id
            ,remote_ip => $remote_ip
        );
        wait_request(debug => 0);
        is($req->status,'done');
        is($req->error, '');

        my @base_ports2 = $base->list_ports();

        my $public_port2 = $base_ports2[0]->{public_port};

        isnt($public_port2, $public_port0) or exit;
        is($public_port2, $public_port1) or exit;

        my @iptables2 = _iptables_save($vm,'nat' ,'PREROUTING');
        is(scalar(@iptables1),scalar(@iptables2)) or die Dumper(\@iptables1,\@iptables2);
frankiejol's avatar
frankiejol committed
679
680
681
682
683
684
685
686
687

        my ($out, $err) = $vm->run_command("iptables-save");
        my @lines = grep(/$internal_ip:$internal_port/, split /\n/,$out);
        is (scalar @lines,1) or die Dumper(\@lines);

        my @lines0 = grep(/-A FORWARD .*ACCEPT$/, split/\n/,$out);
        @lines = grep(m{d $internal_ip/32 .*dport $internal_port}, @lines0);
        is (scalar @lines,1,"Expecting 1 line $internal_ip .*dport $internal_port")
        or die Dumper($internal_ip,\@lines0);
Francesc Guasch's avatar
Francesc Guasch committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
    }

    $base->remove(user_admin);
}

sub _iptables_save($vm,$table=undef,$chain=undef) {
    my @cmd = ("iptables-save");
    push @cmd,("-t",$table) if $table;

    my ($out,$err) = $vm->run_command(@cmd);

    my @out;
    for my $line (split /\n/,$out) {
        next if $chain && $line !~ /^-A $chain/;
        push @out,($line);
    }
    return @out;
}

707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
sub test_interfaces($vm) {
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
    $domain->start( remote_ip => '10.1.1.2', user => user_admin);

    _wait_ip2($vm, $domain);
    my $info = $domain->info(user_admin);

    my $domain_f = Ravada::Front::Domain->open($domain->id);
    my $info_f = $domain_f->info(user_admin);
    ok(exists $info_f->{ip},"Expecting ip in front domain info");
    is($info_f->{ip}, $domain->ip);

    ok($info_f->{interfaces},"Expecting interfaces on ".$domain->_vm->type)
        and isa_ok($info_f->{interfaces},"ARRAY","Expecting mac address is a list")
        and do {
            my $found = 0;
            for my $if (@{$info_f->{interfaces}}) {
                $found++;
                like($if->{hwaddr}, qr/^[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]/);
            }
            ok($found,"Expecting some interfaces, found=$found") or exit;
        };

    $domain->remove(user_admin);
}

Francesc Guasch's avatar
Francesc Guasch committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
sub test_redirect_ip_duplicated($vm) {
    diag("Test redirect ip duplicated ".$vm->type);
    my $internal_port = 22;
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
    $domain->expose(port => $internal_port, name => "ssh");
    $domain->start( remote_ip => '10.1.1.2', user => user_admin);
    my $ip = _wait_ip2($vm, $domain);
    wait_request(debug => 1);

    my @ports0 = $domain->list_ports();
    my ($public_port) = $ports0[0]->{public_port};
    $vm->iptables(t => 'nat'
        , A => 'PREROUTING'
        , p => 'tcp'
        , d => $vm->ip
        , dport => $public_port+10
        , j => 'DNAT'
        , 'to-destination' => "$ip:$internal_port"
    );
    my @out = split /\n/, `iptables-save -t nat`;
    my @open = (grep /--to-destination $ip/, @out);
    is(scalar(@open),2) or die Dumper(\@open);

    $domain->start( remote_ip => '10.1.1.2', user => user_admin);
    wait_request(debug => 1);

    @out = split /\n/, `iptables-save -t nat`;
    @open = (grep /--to-destination $ip/, @out);
    is(scalar(@open),1) or die Dumper(\@open);

    $domain->remove(user_admin);
}

sub test_redirect_ip_duplicated_refresh($vm) {
    diag("Test redirect ip duplicated refresh".$vm->type);
    my $internal_port = 22;
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
    $domain->expose(port => $internal_port, name => "ssh");
    $domain->start( remote_ip => '10.1.1.2', user => user_admin);
    my $ip = _wait_ip2($vm, $domain);
    wait_request(debug => 1);

    my @ports0 = $domain->list_ports();
    my ($public_port) = $ports0[0]->{public_port};
    $vm->iptables(t => 'nat'
        , A => 'PREROUTING'
        , p => 'tcp'
        , d => $vm->ip
        , dport => $public_port+10
        , j => 'DNAT'
        , 'to-destination' => "$ip:$internal_port"
    );
    my @out = split /\n/, `iptables-save -t nat`;
    my @open = (grep /--to-destination $ip/, @out);
    is(scalar(@open),2) or die Dumper(\@open);

    my $req = Ravada::Request->refresh_vms();
    wait_request();
    is($req->status,'done');
    is($req->error, '');

    @out = split /\n/, `iptables-save -t nat`;
    @open = (grep /--to-destination $ip/, @out);
    is(scalar(@open),1) or die Dumper(\@open);

    $domain->remove(user_admin);
}


802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
sub test_open_port_duplicated($vm) {
    diag("Test open port duplicated ".$vm->type);
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);
    $base->expose(port => 22, name => "ssh");
    my @base_ports0 = $base->list_ports();

    $base->prepare_base(user => user_admin);

    my $clone = $base->clone(name => new_domain_name, user => user_admin);

    $clone->start(remote_ip => '10.1.1.1', user => user_admin);
    _wait_ip2($vm, $clone);

    wait_request();

    my @out = split /\n/, `iptables-save -t nat`;
    my @open = (grep /--to-destination [\d\.]+:22/, @out);
    is(scalar(@open),1);
    my ($public_port) = $open[0] =~ /--dport (\d+)/;
    die "Error: no public port in $open[0]" if !$public_port;
    $vm->iptables( t => 'nat'
        , A => 'PREROUTING'
        , p => 'tcp'
        , d => '192.0.2.3'
        , dport => $public_port
        , j => 'DNAT'
        , 'to-destination' => '127.0.0.1:23'
    );
    my @out2 = split /\n/, `iptables-save -t nat`;
    my @open2 = (grep /--dport $public_port/, @out2);
    is(scalar(@open2),2) or die Dumper(\@open2);

    my $req = Ravada::Request->refresh_vms();
    wait_request();
    is($req->status,'done');
    is($req->error, '');

    my @out3 = split /\n/, `iptables-save -t nat`;
    my @open3 = (grep /--dport $public_port/, @out3);
    is(scalar(@open3),1) or die Dumper(\@open3);

    $clone->remove(user_admin);
    $base->remove(user_admin);
}

sub test_close_port($vm) {
    diag("Test close port ".$vm->type);
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);
    $base->expose(port => 22, name => "ssh");
    my @base_ports0 = $base->list_ports();

    $base->prepare_base(user => user_admin);

    my $clone = $base->clone(name => new_domain_name, user => user_admin);

    $clone->start(remote_ip => '10.1.1.1', user => user_admin);
    _wait_ip2($vm, $clone);

    wait_request();

    my @out = split /\n/, `iptables-save -t nat`;
    my @open = (grep /--to-destination [\d\.]+:22/, @out);
    is(scalar(@open),1);
    my ($public_port) = $open[0] =~ /--dport (\d+)/;
    die "Error: no public port in $open[0]" if !$public_port;
    $vm->iptables( t => 'nat'
        , A => 'PREROUTING'
        , p => 'tcp'
        , d => '192.0.2.3'
        , dport => $public_port
        , j => 'DNAT'
        , 'to-destination' => '127.0.0.1:23'
    );
    my @out2 = split /\n/, `iptables-save -t nat`;
    my @open2 = (grep /--dport $public_port/, @out2);
    is(scalar(@open2),2) or die Dumper(\@open);
    $clone->shutdown_now(user_admin);
    wait_request();

    my @out3 = split /\n/, `iptables-save -t nat`;
    my @open3 = (grep /--to-destination [\d\.]+:22/, @out3);
    is(scalar(@open3),0, Dumper(\@open3));

    $clone->remove(user_admin);
    $base->remove(user_admin);
}

889
890
sub test_clone_exports_add_ports($vm) {

891
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);
892
893
894
895
896
897
898
899
900
901
    $base->expose(port => 22, name => "ssh");
    my @base_ports0 = $base->list_ports();

    $base->prepare_base(user => user_admin, with_cd => 1);

    my $clone = $base->clone(name => new_domain_name, user => user_admin);
    $base->expose(port => 80, name => "web");
    my @base_ports = $base->list_ports();
    is(scalar @base_ports, scalar @base_ports0 + 1);

902
903
904
905
    my $clone_f = Ravada::Front::Domain->open($clone->id);
    eval { my $info = $clone_f->info(user_admin) };
    is($@,'');

906
907
908
909
    $clone->start(remote_ip => '10.1.1.1', user => user_admin);
    my @clone_ports = $clone->list_ports();
    is(scalar @clone_ports,2 );

910
    my @req = $clone->list_requests;
911
    is(scalar(@req) , 2);
912

913
914
915
916
917
    for my $n ( 0 .. 1 ) {
        is($base_ports[$n]->{internal_port}, $clone_ports[$n]->{internal_port});
        isnt($base_ports[$n]->{public_port}, $clone_ports[$n]->{public_port},"Same public port in clone and base for ".$base_ports[$n]->{internal_port});
        is($base_ports[$n]->{name}, $clone_ports[$n]->{name});
    }
918
    _wait_ip2($vm, $clone);
919
920
    wait_request( debug => 0, request => \@req );
    for (@req) {
921
        next if $_->command eq 'set_time';
922
923
924
        is($_->status,'done')   or exit;
        is($_->error,'')        or exit;
    }
925
926
927
928
929
930
931
932
    my @out = split /\n/, `iptables -t nat -L PREROUTING -n`;
    ok(grep /dpt:\d+.*\d+:22/, @out);
    ok(grep /dpt:\d+.*\d+:80/, @out);

    $clone->remove(user_admin);
    $base->remove(user_admin);
}

933
sub _wait_ip2($vm_name, $domain) {
934
    $domain->start(user => user_admin, remote_ip => '1.2.3.5') unless $domain->is_active();
935
936
937
938
939
940
941
942
    for ( 1 .. 22 ) {
        return $domain->ip if $domain->ip;
        diag("Waiting for ".$domain->name. " ip") if !(time % 10);
        sleep 1;
    }
    confess;
}

Francesc Guasch's avatar
Francesc Guasch committed
943
sub _wait_ip {
944
    return _wait_ip2(@_);
Francesc Guasch's avatar
Francesc Guasch committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
}

sub add_network_10 {
    my $requires_password = shift;
    $requires_password = 1 if !defined $requires_password;

    my $sth = connector->dbh->prepare(
        "DELETE FROM networks where address='10.0.0.0/24'"
    );
    $sth->execute;
        $sth = connector->dbh->prepare(
        "INSERT INTO networks (name,address,all_domains,requires_password)"
        ."VALUES('10','10.0.0.0/24',1,?)"
    );
    $sth->execute($requires_password);
}


# expose a port when the host is down
sub test_host_down {
    my $vm_name = shift;

    flush_rules();

    my $vm = rvd_back->search_vm($vm_name);

971
    my $domain = $BASE->clone(name => new_domain_name, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
972
973
974
975
976
977
978
979
980
981
982
983
984

    my $remote_ip = '10.0.0.1';
    my $local_ip = $vm->ip;

    $domain->shutdown_now(user_admin)    if $domain->is_active;

    my $internal_port = 22;
    my ($public_port);
    eval { ($public_port) = $domain->expose($internal_port) };
    is($@,'') or return;

    $domain->start(user => user_admin, remote_ip => $remote_ip);

985
    _wait_requests($domain);
986
    wait_request(debug => 0, background => 0);
Francesc Guasch's avatar
Francesc Guasch committed
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

    my $domain_ip = $domain->ip;
    ok($domain_ip,"[$vm_name] Expecting an IP for domain ".$domain->name.", got ".($domain_ip or '')) or return;

    is(scalar $domain->list_ports,1);

    my ($n_rule)
        = search_iptable_remote(local_ip => "$local_ip/32"
            , local_port => $public_port
            , table => 'nat'
            , chain => 'PREROUTING'
            , node => $vm
            , jump => 'DNAT'
    );
For faster browsing, not all history is shown. View entire blame