| 
									
										
										
										
											2010-05-20 21:07:32 +00: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. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  | import ./make-test-python.nix ({ pkgs, lib, withFirewall, withConntrackHelpers ? false, ... }: | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |   let | 
					
						
							|  |  |  |     unit = if withFirewall then "firewall" else "nat"; | 
					
						
							| 
									
										
										
										
											2017-08-03 17:57:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     routerBase = | 
					
						
							|  |  |  |       lib.mkMerge [ | 
					
						
							|  |  |  |         { virtualisation.vlans = [ 2 1 ]; | 
					
						
							|  |  |  |           networking.firewall.enable = withFirewall; | 
					
						
							|  |  |  |           networking.nat.internalIPs = [ "192.168.1.0/24" ]; | 
					
						
							|  |  |  |           networking.nat.externalInterface = "eth1"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         (lib.optionalAttrs withConntrackHelpers { | 
					
						
							|  |  |  |           networking.firewall.connectionTrackingModules = [ "ftp" ]; | 
					
						
							|  |  |  |           networking.firewall.autoLoadConntrackHelpers = true; | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ]; | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |   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; { | 
					
						
							| 
									
										
										
										
											2019-02-22 16:14:13 +01:00
										 |  |  |       maintainers = [ eelco rob ]; | 
					
						
							| 
									
										
										
										
											2015-07-12 12:09:40 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2010-05-20 21:07:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |     nodes = | 
					
						
							|  |  |  |       { client = | 
					
						
							| 
									
										
										
										
											2018-07-20 20:56:59 +00:00
										 |  |  |           { pkgs, nodes, ... }: | 
					
						
							| 
									
										
										
										
											2017-01-22 19:42:59 +01:00
										 |  |  |           lib.mkMerge [ | 
					
						
							|  |  |  |             { virtualisation.vlans = [ 1 ]; | 
					
						
							|  |  |  |               networking.defaultGateway = | 
					
						
							| 
									
										
										
										
											2017-12-03 05:14:54 +01:00
										 |  |  |                 (pkgs.lib.head nodes.router.config.networking.interfaces.eth2.ipv4.addresses).address; | 
					
						
							| 
									
										
										
										
											2017-01-22 19:42:59 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             (lib.optionalAttrs withConntrackHelpers { | 
					
						
							|  |  |  |               networking.firewall.connectionTrackingModules = [ "ftp" ]; | 
					
						
							|  |  |  |               networking.firewall.autoLoadConntrackHelpers = true; | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |           ]; | 
					
						
							| 
									
										
										
										
											2010-05-20 21:07:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         router = | 
					
						
							| 
									
										
										
										
											2018-07-20 20:56:59 +00:00
										 |  |  |         { ... }: lib.mkMerge [ | 
					
						
							| 
									
										
										
										
											2017-08-03 17:57:43 +00:00
										 |  |  |           routerBase | 
					
						
							|  |  |  |           { networking.nat.enable = true; } | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         routerDummyNoNat = | 
					
						
							| 
									
										
										
										
											2018-07-20 20:56:59 +00:00
										 |  |  |         { ... }: lib.mkMerge [ | 
					
						
							| 
									
										
										
										
											2017-08-03 17:57:43 +00:00
										 |  |  |           routerBase | 
					
						
							|  |  |  |           { networking.nat.enable = false; } | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2010-05-20 21:07:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         server = | 
					
						
							| 
									
										
										
										
											2018-07-20 20:56:59 +00:00
										 |  |  |           { ... }: | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |           { 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 21:07:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |     testScript = | 
					
						
							| 
									
										
										
										
											2017-08-03 17:57:43 +00:00
										 |  |  |       { nodes, ... }: let | 
					
						
							|  |  |  |         routerDummyNoNatClosure = nodes.routerDummyNoNat.config.system.build.toplevel; | 
					
						
							|  |  |  |         routerClosure = nodes.router.config.system.build.toplevel; | 
					
						
							|  |  |  |       in ''
 | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         client.start() | 
					
						
							|  |  |  |         router.start() | 
					
						
							|  |  |  |         server.start() | 
					
						
							| 
									
										
										
										
											2010-05-20 21:07:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         # The router should have access to the server. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         server.wait_for_unit("network.target") | 
					
						
							|  |  |  |         server.wait_for_unit("httpd") | 
					
						
							|  |  |  |         router.wait_for_unit("network.target") | 
					
						
							|  |  |  |         router.succeed("curl --fail http://server/ >&2") | 
					
						
							| 
									
										
										
										
											2010-05-20 21:07:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         # The client should be also able to connect via the NAT router. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         router.wait_for_unit("${unit}") | 
					
						
							|  |  |  |         client.wait_for_unit("network.target") | 
					
						
							|  |  |  |         client.succeed("curl --fail http://server/ >&2") | 
					
						
							|  |  |  |         client.succeed("ping -c 1 server >&2") | 
					
						
							| 
									
										
										
										
											2011-03-10 12:08:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         # Test whether passive FTP works. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         server.wait_for_unit("vsftpd") | 
					
						
							|  |  |  |         server.succeed("echo Hello World > /home/ftp/foo.txt") | 
					
						
							|  |  |  |         client.succeed("curl -v ftp://server/foo.txt >&2") | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 19:42:59 +01:00
										 |  |  |         # Test whether active FTP works. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         client.${if withConntrackHelpers then "succeed" else "fail"}("curl -v -P - ftp://server/foo.txt >&2") | 
					
						
							| 
									
										
										
										
											2017-01-22 19:42:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         # Test ICMP. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         client.succeed("ping -c 1 router >&2") | 
					
						
							|  |  |  |         router.succeed("ping -c 1 client >&2") | 
					
						
							| 
									
										
										
										
											2011-03-10 13:03:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         # If we turn off NAT, the client shouldn't be able to reach the server. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         router.succeed( | 
					
						
							|  |  |  |             "${routerDummyNoNatClosure}/bin/switch-to-configuration test 2>&1" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         client.fail("curl --fail --connect-timeout 5 http://server/ >&2") | 
					
						
							|  |  |  |         client.fail("ping -c 1 server >&2") | 
					
						
							| 
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |         # And make sure that reloading the NAT job works. | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         router.succeed( | 
					
						
							|  |  |  |             "${routerClosure}/bin/switch-to-configuration test 2>&1" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-08-04 17:52:42 +02:00
										 |  |  |         # FIXME: this should not be necessary, but nat.service is not started because | 
					
						
							|  |  |  |         #        network.target is not triggered | 
					
						
							|  |  |  |         #        (https://github.com/NixOS/nixpkgs/issues/16230#issuecomment-226408359) | 
					
						
							|  |  |  |         ${lib.optionalString (!withFirewall) ''
 | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |           router.succeed("systemctl start nat.service") | 
					
						
							| 
									
										
										
										
											2017-08-04 17:52:42 +02:00
										 |  |  |         ''}
 | 
					
						
							| 
									
										
										
										
											2019-12-01 00:20:28 +01:00
										 |  |  |         client.succeed("curl --fail http://server/ >&2") | 
					
						
							|  |  |  |         client.succeed("ping -c 1 server >&2") | 
					
						
							| 
									
										
										
										
											2014-09-18 13:34:29 -07:00
										 |  |  |       '';
 | 
					
						
							|  |  |  |   }) |