2010-05-20 23:07:32 +02:00
|
|
|
# This is a simple distributed test involving a topology with two
|
|
|
|
# separate virtual networks - the "inside" and the "outside" - with a
|
|
|
|
# client on the inside network, a server on the outside network, and a
|
|
|
|
# router connected to both that performs Network Address Translation
|
|
|
|
# for the client.
|
2017-01-22 19:42:59 +01:00
|
|
|
import ./make-test.nix ({ pkgs, lib, withFirewall, withConntrackHelpers ? false, ... }:
|
2014-09-18 22:34:29 +02:00
|
|
|
let
|
|
|
|
unit = if withFirewall then "firewall" else "nat";
|
|
|
|
in
|
|
|
|
{
|
2017-01-22 19:42:59 +01:00
|
|
|
name = "nat" + (if withFirewall then "WithFirewall" else "Standalone")
|
|
|
|
+ (lib.optionalString withConntrackHelpers "withConntrackHelpers");
|
|
|
|
meta = with pkgs.stdenv.lib.maintainers; {
|
2015-07-12 12:09:40 +02:00
|
|
|
maintainers = [ eelco chaoflow rob wkennington ];
|
|
|
|
};
|
2010-05-20 23:07:32 +02:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
nodes =
|
|
|
|
{ client =
|
|
|
|
{ config, pkgs, nodes, ... }:
|
2017-01-22 19:42:59 +01:00
|
|
|
lib.mkMerge [
|
|
|
|
{ virtualisation.vlans = [ 1 ];
|
|
|
|
networking.firewall.allowPing = true;
|
|
|
|
networking.defaultGateway =
|
|
|
|
(pkgs.lib.head nodes.router.config.networking.interfaces.eth2.ip4).address;
|
|
|
|
}
|
|
|
|
(lib.optionalAttrs withConntrackHelpers {
|
|
|
|
networking.firewall.connectionTrackingModules = [ "ftp" ];
|
|
|
|
networking.firewall.autoLoadConntrackHelpers = true;
|
|
|
|
})
|
|
|
|
];
|
2010-05-20 23:07:32 +02:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
router =
|
|
|
|
{ config, pkgs, ... }:
|
2017-01-22 19:42:59 +01:00
|
|
|
lib.mkMerge [
|
|
|
|
{ virtualisation.vlans = [ 2 1 ];
|
|
|
|
networking.firewall.enable = withFirewall;
|
|
|
|
networking.firewall.allowPing = true;
|
|
|
|
networking.nat.enable = true;
|
|
|
|
networking.nat.internalIPs = [ "192.168.1.0/24" ];
|
|
|
|
networking.nat.externalInterface = "eth1";
|
|
|
|
}
|
|
|
|
(lib.optionalAttrs withConntrackHelpers {
|
|
|
|
networking.firewall.connectionTrackingModules = [ "ftp" ];
|
|
|
|
networking.firewall.autoLoadConntrackHelpers = true;
|
|
|
|
})
|
|
|
|
];
|
2010-05-20 23:07:32 +02:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
server =
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{ virtualisation.vlans = [ 2 ];
|
|
|
|
networking.firewall.enable = false;
|
|
|
|
services.httpd.enable = true;
|
|
|
|
services.httpd.adminAddr = "foo@example.org";
|
|
|
|
services.vsftpd.enable = true;
|
|
|
|
services.vsftpd.anonymousUser = true;
|
|
|
|
};
|
|
|
|
};
|
2010-05-20 23:07:32 +02:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
testScript =
|
|
|
|
{ nodes, ... }:
|
|
|
|
''
|
|
|
|
startAll;
|
2010-05-20 23:07:32 +02:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
# The router should have access to the server.
|
|
|
|
$server->waitForUnit("network.target");
|
|
|
|
$server->waitForUnit("httpd");
|
|
|
|
$router->waitForUnit("network.target");
|
|
|
|
$router->succeed("curl --fail http://server/ >&2");
|
2010-05-20 23:07:32 +02:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
# The client should be also able to connect via the NAT router.
|
|
|
|
$router->waitForUnit("${unit}");
|
|
|
|
$client->waitForUnit("network.target");
|
|
|
|
$client->succeed("curl --fail http://server/ >&2");
|
|
|
|
$client->succeed("ping -c 1 server >&2");
|
2011-03-10 13:08:39 +01:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
# Test whether passive FTP works.
|
|
|
|
$server->waitForUnit("vsftpd");
|
|
|
|
$server->succeed("echo Hello World > /home/ftp/foo.txt");
|
|
|
|
$client->succeed("curl -v ftp://server/foo.txt >&2");
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2017-01-22 19:42:59 +01:00
|
|
|
# Test whether active FTP works.
|
|
|
|
$client->${if withConntrackHelpers then "succeed" else "fail"}(
|
|
|
|
"curl -v -P - ftp://server/foo.txt >&2");
|
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
# Test ICMP.
|
|
|
|
$client->succeed("ping -c 1 router >&2");
|
|
|
|
$router->succeed("ping -c 1 client >&2");
|
2011-03-10 14:03:47 +01:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
# If we turn off NAT, the client shouldn't be able to reach the server.
|
|
|
|
$router->succeed("iptables -t nat -D PREROUTING -j nixos-nat-pre");
|
|
|
|
$router->succeed("iptables -t nat -D POSTROUTING -j nixos-nat-post");
|
|
|
|
$client->fail("curl --fail --connect-timeout 5 http://server/ >&2");
|
|
|
|
$client->fail("ping -c 1 server >&2");
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2014-09-18 22:34:29 +02:00
|
|
|
# And make sure that reloading the NAT job works.
|
|
|
|
$router->succeed("systemctl restart ${unit}");
|
|
|
|
$client->succeed("curl --fail http://server/ >&2");
|
|
|
|
$client->succeed("ping -c 1 server >&2");
|
|
|
|
'';
|
|
|
|
})
|