-
Notifications
You must be signed in to change notification settings - Fork 26
/
test.nix
303 lines (266 loc) · 9.07 KB
/
test.nix
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
298
299
300
301
302
303
{ pkgs, nixpkgs, ... }:
let
ws-go-server = pkgs.buildGoPackage rec {
name = "ws-go-server-${version}";
version = "0.0.1";
unpackPhase = ''
mkdir -p src/src/github.com/ws-go-server/ws-go-server/
cp ${ws-go-server-src} src/src/github.com/ws-go-server/ws-go-server/main.go
# setting the weird sourceRoot and the src/src... WTH?
sourceRoot=`pwd`/src
'';
goPackagePath = "github.com/ws-go-server/ws-go-server/";
goDeps = ws-go-deps;
};
ws-go-client = pkgs.buildGoPackage rec {
name = "ws-go-client-${version}";
version = "0.0.1";
unpackPhase = ''
mkdir -p src/src/github.com/ws-go-client/ws-go-client/
cp ${ws-go-client-src} src/src/github.com/ws-go-client/ws-go-client/main.go
# setting the weird sourceRoot and the src/src... WTH?
sourceRoot=`pwd`/src
'';
goPackagePath = "github.com/ws-go-client/ws-go-client/";
goDeps = ws-go-deps;
};
# thanks to https://github.com/golang-samples/websocket/blob/master/simple/main.go
ws-go-server-src = pkgs.writeText "main.go" ''
package main
import (
"io"
"fmt"
"net/http"
"golang.org/x/net/websocket"
)
func echoHandler(ws *websocket.Conn) {
io.Copy(ws, ws)
}
func main() {
fmt.Println("GO websocket server running...")
http.Handle("/myapp/ws", websocket.Handler(echoHandler))
http.Handle("/", http.FileServer(http.Dir(".")))
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
'';
ws-go-client-src = pkgs.writeText "main.go" ''
package main
import (
"fmt"
"log"
"golang.org/x/net/websocket"
)
var origin = "http://localhost/"
var url = "ws://example.ws:80/myapp/ws"
func main() {
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
message := []byte("hello, world!")
_, err = ws.Write(message)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Send: %s\n", message)
var msg = make([]byte, 512)
_, err = ws.Read(msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Receive: %s\n", msg)
}
'';
ws-go-deps = pkgs.writeText "ws-go-server-deps.nix" ''
[{
goPackagePath = "golang.org/x/net";
fetch = {
type = "git";
url = "https://go.googlesource.com/net";
rev = "07b51741c1d6423d4a6abab1c49940ec09cb1aaf";
sha256 = "12lvdj0k2gww4hw5f79qb9yswqpy4i3bgv1likmf3mllgdxfm20w";
};
}]
'';
in
{
name = "reverse-proxy";
machine = { pkgs, lib, ... }: {
nix.nixPath = [ "nixpkgs=${nixpkgs}" "nixos-config=/etc/nixos/configuration.nix" ];
nixcloud.TLS.certs = lib.mkForce {
"stupid.io".mode = "selfsigned";
"ext.ra".mode = "selfsigned";
"exclusive.ws1".mode = "selfsigned";
"exclusive.ws2".mode = "selfsigned";
"flags.io".mode = "selfsigned";
"example.com".mode = "selfsigned";
"example.ws".mode = "selfsigned";
"flubb.com".mode = "selfsigned";
};
nixcloud.reverse-proxy = {
enable = true;
extendEtcHosts = true;
extraMappings = [
{
domain = "stupid.io";
path = "/";
port = 8383;
http.mode = "off";
https.mode = "off";
websockets = {
ws = {
subpath = "/websocket";
http.mode = "off";
https.mode = "off";
};
};
}
{
domain = "ext.ra";
path = "/location";
port = 60000;
# http.mode = "off";
# https.mode = "off";
http.mode = "on";
https.mode = "off";
extraLocations = {
test = {
http.mode = "off";
https.mode = "on";
port = 60000;
subpath = "/test";
};
extraTest = {
http.mode = "on";
https.mode = "on";
port = 60000;
subpath = "/extra/test";
};
fail = {
http.mode = "on";
https.mode = "on";
port = 12345;
subpath = "/fail";
};
};
}
{
domain = "exclusive.ws1";
path = "/tt";
port = 8383;
http.mode = "off";
https.mode = "off";
websockets = {
ws = {
subpath = "/websocket";
http.mode = "on";
https.mode = "on";
};
};
}
{
domain = "exclusive.ws2";
#path = "/";
port = 8484;
http.mode = "off";
https.mode = "off";
websockets = {
ws = {
subpath = "/websocket";
http.mode = "on";
https.mode = "on";
};
};
}
{
domain = "flags.io";
path = "/";
port = 60000;
http.mode = "on";
http.extraFlags = ''
add_header Strict-Transport-Security max-age=345678;
'';
https.mode = "on";
https.extraFlags = ''
add_header Strict-Transport-Security max-age=345678;
'';
websockets = {
ws = {
subpath = "/websocket";
http.mode = "off";
https.mode = "off";
};
};
}
];
};
# including additional extraConfigs which are used for LXC based webservices
imports = [ ./test ];
services.httpd = {
enable = true;
virtualHosts = {
"example.com" = {
listen = [{port = 60000; ip = "*";}];
};
};
adminAddr="example@example.com";
};
# Needed so that we have all dependencies available for building the
# container config within the VM.
virtualisation.pathsInNixDB = let
emptyClosure = (import <nixpkgs/nixos/lib/eval-config.nix> {
modules = lib.singleton { boot.isContainer = true; };
}).config.system.build.toplevel;
in [ pkgs.stdenv emptyClosure ];
};
testScript = ''
$machine->waitForUnit('multi-user.target');
$machine->waitForOpenPort(80);
$machine->waitForOpenPort(443);
# 8383 is actually not a webserve, we only use it to generate a reverse-proxy mapping to test the reverse-proxy
#$machine->waitForOpenPort(8383);
# 8484 is actually not a webserve, we only use it to generate a reverse-proxy mapping to test the reverse-proxy
#$machine->waitForOpenPort(8484);
$machine->waitForOpenPort(60000);
# make sure both are answered by apache
$machine->succeed('curl http://example.com/wiki | grep "<span>Apache" >&2');
$machine->succeed('curl -k https://example.com/wiki | grep "<span>Apache" >&2');
#check if extraLocations work
$machine->succeed('curl http://ext.ra/location | grep "<span>Apache" >&2');
$machine->fail('curl -k https://ext.ra/location | grep "<span>Apache" >&2');
$machine->succeed('curl -k https://ext.ra/location/test | grep "<span>Apache" >&2');
$machine->succeed('curl http://ext.ra/location/extra/test | grep "<span>Apache" >&2');
$machine->succeed('curl -k https://ext.ra/location/extra/test | grep "<span>Apache" >&2');
$machine->fail('curl https://ext.ra/location/fail | grep "<span>Apache" >&2');
$machine->fail('curl -k https://ext.ra/location/fail | grep "<span>Apache" >&2');
# make sure for both there is no entry in example.com/blog
$machine->succeed('curl http://example.com/blog | grep 404 >&2');
$machine->succeed('curl -k https://example.com/blog | grep 404 >&2');
# make sure http redirects to https and https works for flubb.com
# BUG: this test also uses the same port and this should bail out on nix evaluation...
$machine->succeed('curl http://flubb.com/blog | grep -i "301 Moved Permanently" >&2');
$machine->succeed('curl -k https://flubb.com/blog | grep "<span>Apache" >&2');
# test basicauth
$machine->succeed('curl http://example.com/basicauth --user "foo:bar1" | grep "<span>Apache" >&2');
$machine->succeed('curl http://example.com/basicauth | grep "401 Auth" >&2');
$machine->succeed('curl -k https://example.com/basicauth --user "foo:bar2" | grep "<span>Apache" >&2');
$machine->succeed('curl -k https://example.com/basicauth | grep "401 Auth" >&2');
# ipv4/ipv6 tests
$machine->succeed('cat /etc/hosts >&2');
$machine->succeed('curl -4 http://example.com/wiki | grep "<span>Apache" >&2');
$machine->succeed('curl -4 -k https://example.com/wiki | grep "<span>Apache" >&2');
$machine->succeed('curl -6 http://example.com/wiki | grep "<span>Apache" >&2');
$machine->succeed('curl -6 -k https://example.com/wiki | grep "<span>Apache" >&2');
# test HSTS
$machine->succeed('curl -k -s -D- http://flags.io | grep Strict >&2');
$machine->succeed('curl -k -s -D- https://flags.io | grep Strict >&2');
# test websockets
$machine->succeed('curl http://example.ws/myapp/ws >&2');
$machine->succeed('${ws-go-server}/bin/ws-go-server & >&2');
$machine->waitForOpenPort(8080);
$machine->succeed('${ws-go-client}/bin/ws-go-client >&2');
'';
}