blob: ab488683dc7fc7f8780919c564959e13964b0b4a (
plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
{ lib, pkgs, ... }:
let
# Hashes for the test files we serve. We have multiple of them to force nix
# to download the files instead of using the already-downloaded variant
hashes = {
a = "sha256-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=";
b = "sha256-PiPoFgA5WUoziU9lZOGxNIu9egCI1CxKy3PurtWcAJ0=";
c = "sha256-Ln0sA6lQeuJl7PW1NWiFpTOTogKdJBOUmXJloaJa78Y=";
};
# Builds a .nix file that can download a file from the test server
mkFetcher =
{
name,
url,
hash,
}:
pkgs.writeText "${name}.nix" ''
derivation {
name = "${name}-fetch";
builder = "builtin:fetchurl";
system = "builtin";
preferLocalBuild = true;
url = "${url}";
outputHash = "${hash}";
outputHashMode = "flat";
}
'';
# Fetches a file from the public IP, port 80
publicFetcher = mkFetcher {
name = "public";
url = "http://1.0.0.1/a";
hash = hashes.a;
};
# Fetches a file from the private IP, port 80
privateFetcher = mkFetcher {
name = "private";
url = "http://192.168.0.1/b";
hash = hashes.b;
};
# Fetches a file from the public IP, port 81
publicFetcherOtherPort = mkFetcher {
name = "public";
url = "http://192.168.0.1:81/c";
hash = hashes.c;
};
# Fetches a file from localhost
localhostFetcher = mkFetcher {
name = "localhost";
url = "http://127.0.0.1/a";
hash = hashes.a;
};
# Generates a file but tries to resolve via DNS first
resolver = pkgs.writeText "pinger.nix" ''
derivation {
name = "resolver";
system = builtins.currentSystem;
preferLocalBuild = true;
builder = "/bin/sh";
args = [ "-c" "${pkgs.pkgsStatic.busybox}/bin/nslookup . && echo hello > $out" ];
outputHash = "sha256-WJG1tSLV3whtD/CxEPvZ0hu0/HFjrzTQgoai6Eb2vgM=";
outputHashMode = "flat";
}
'';
# Base system for all fetchers
baseNixSystem = ipSuffix: {
networking = {
useDHCP = false;
interfaces.eth1 = {
ipv4.addresses = [
{
address = "192.168.0.${toString ipSuffix}";
prefixLength = 24;
}
{
address = "1.0.0.${toString ipSuffix}";
prefixLength = 24;
}
];
};
nftables = {
enable = true;
flushRuleset = false;
};
};
nix.settings = {
# Gets rid of substitution warnings
substituters = lib.mkForce [ ];
# Gives us access inside the nix sandbox
extra-sandbox-paths = [ "${pkgs.pkgsStatic.busybox}" ];
};
nix.enable = true; # disabled by default. See all-tests.nix / tag(no-nix-by-default)
# Easy way to get files to the system
environment.etc = {
"fetch-public.nix".source = publicFetcher;
"fetch-private.nix".source = privateFetcher;
"fetch-public-other-port.nix".source = publicFetcherOtherPort;
"fetch-localhost.nix".source = localhostFetcher;
"resolver.nix".source = resolver;
};
# For localhost tests
services.nginx = {
enable = true;
virtualHosts.default.locations."/a".return = "200 \"a\"";
};
};
in
{
name = "nix-daemon-firewall";
meta.maintainers = with lib.maintainers; [ das_j ];
nodes = {
httpServer = {
services.nginx = {
enable = true;
virtualHosts.default = {
listen = [
{
addr = "0.0.0.0";
port = 80;
}
{
addr = "0.0.0.0";
port = 81;
}
];
locations = {
"/a".return = "200 \"a\"";
"/b".return = "200 \"b\"";
"/c".return = "200 \"c\"";
};
};
};
networking = {
useDHCP = false;
interfaces.eth1 = {
ipv4.addresses = [
{
address = "192.168.0.1";
prefixLength = 24;
}
{
address = "1.0.0.1";
prefixLength = 24;
}
];
};
firewall.allowedTCPPorts = [
80
81
];
};
};
unfirewalled = {
imports = [ (baseNixSystem 2) ];
};
everythingAllowed = {
imports = [ (baseNixSystem 3) ];
nix.firewall = {
enable = true;
allowLoopback = true;
allowNonTCPUDP = true;
};
};
noLocalNets = {
imports = [ (baseNixSystem 4) ];
nix.firewall = {
enable = true;
allowPrivateNetworks = false;
};
};
onlyPort80 = {
imports = [ (baseNixSystem 5) ];
nix.firewall = {
enable = true;
allowedTCPPorts = [ 80 ];
};
};
resolved = {
imports = [ (baseNixSystem 6) ];
nix.firewall = {
enable = true;
};
services.resolved.enable = true;
};
};
testScript = ''
# Startup
start_all()
httpServer.wait_for_open_port(80)
# No firewall
unfirewalled.wait_for_unit("multi-user.target")
unfirewalled.fail("nft list ruleset | grep nix_daemon_traffic")
# Everything allowed
everythingAllowed.wait_for_unit("multi-user.target")
everythingAllowed.succeed("nft list ruleset | grep nix_daemon_traffic")
everythingAllowed.succeed("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-public.nix")
everythingAllowed.succeed("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-private.nix")
everythingAllowed.succeed("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-public-other-port.nix")
everythingAllowed.succeed("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-localhost.nix")
# No local networks and no localhost
noLocalNets.wait_for_unit("multi-user.target")
noLocalNets.succeed("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-public.nix")
noLocalNets.fail("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-private.nix")
noLocalNets.fail("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-localhost.nix")
# Ports
onlyPort80.wait_for_unit("multi-user.target")
onlyPort80.succeed("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-public.nix")
onlyPort80.fail("NIX_REMOTE=daemon nix-build --timeout 5 /etc/fetch-public-other-port.nix")
# systemd-resolved
resolved.wait_for_unit("multi-user.target")
resolved.succeed("NIX_REMOTE=daemon nix-build --timeout 5 /etc/resolver.nix")
'';
}
|