blob: 8dcc3ef496c76c176a66b2d4a96ef098d26384f5 (
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
|
{ lib, ... }:
{
name = "anubis";
meta.maintainers = with lib.maintainers; [
soopyc
nullcube
ryand56
];
nodes.machine =
{ config, pkgs, ... }:
{
environment.etc."anubis-botPolicy.json".text = lib.generators.toJSON { } {
bots = [
{
name = "allow-all";
user_agent_regex = ".*";
action = "ALLOW";
}
];
};
services.anubis = {
defaultOptions = {
settings = {
DIFFICULTY = 3;
USER_DEFINED_DEFAULT = true;
};
};
instances."".settings = {
TARGET = "http://localhost:8080";
DIFFICULTY = 5;
USER_DEFINED_INSTANCE = true;
};
instances."tcp" = {
user = "anubis-tcp";
group = "anubis-tcp";
settings = {
TARGET = "http://localhost:8080";
BIND = "127.0.0.1:9000";
BIND_NETWORK = "tcp";
METRICS_BIND = "127.0.0.1:9001";
METRICS_BIND_NETWORK = "tcp";
};
};
instances."another-unix-listen" = {
settings = {
TARGET = "http://localhost:8080";
};
};
instances."unix-upstream" = {
group = "nginx";
settings = {
TARGET = "unix:///run/nginx/nginx.sock";
};
};
instances."policy-default" = {
settings = {
TARGET = "http://localhost:8080";
};
};
instances."policy-custom" = {
policy = {
extraBots = [
{
name = "custom-allow";
user_agent_regex = "CustomBot/.*";
action = "ALLOW";
}
];
settings.dnsbl = false;
};
settings = {
TARGET = "http://localhost:8080";
};
};
instances."policy-file" = {
settings = {
TARGET = "http://localhost:8080";
POLICY_FNAME = "/etc/anubis-botPolicy.json";
};
};
};
# support
users.users.nginx.extraGroups = [ config.users.groups.anubis.name ];
services.nginx = {
enable = true;
recommendedProxySettings = true;
virtualHosts."basic.localhost".locations = {
"/".proxyPass = "http://unix:${config.services.anubis.instances."".settings.BIND}";
"/metrics".proxyPass = "http://unix:${config.services.anubis.instances."".settings.METRICS_BIND}";
};
virtualHosts."tcp.localhost".locations = {
"/".proxyPass = "http://${config.services.anubis.instances."tcp".settings.BIND}";
"/metrics".proxyPass = "http://${config.services.anubis.instances."tcp".settings.METRICS_BIND}";
};
virtualHosts."another-unix-listen".locations = {
"/".proxyPass = "http://unix:${
config.services.anubis.instances."another-unix-listen".settings.BIND
}";
"/metrics".proxyPass = "http://unix:${
config.services.anubis.instances."another-unix-listen".settings.METRICS_BIND
}";
};
virtualHosts."unix.localhost".locations = {
"/".proxyPass = "http://unix:${config.services.anubis.instances."unix-upstream".settings.BIND}";
};
# emulate an upstream with nginx, listening on tcp and unix sockets.
virtualHosts."upstream.localhost" = {
default = true; # make nginx match this vhost for `localhost`
listen = [
{ addr = "unix:/run/nginx/nginx.sock"; }
{
addr = "localhost";
port = 8080;
}
];
locations."/" = {
tryFiles = "$uri $uri/index.html =404";
root = pkgs.runCommand "anubis-test-upstream" { } ''
mkdir $out
echo "it works" >> $out/index.html
'';
};
};
};
};
testScript = ''
for unit in ["nginx", "anubis", "anubis-another-unix-listen", "anubis-tcp", "anubis-unix-upstream"]:
machine.wait_for_unit(unit + ".service")
for port in [9000, 9001]:
machine.wait_for_open_port(port)
machine.wait_for_open_unix_socket("/run/anubis/anubis/anubis.sock")
machine.wait_for_open_unix_socket("/run/anubis/anubis/anubis-metrics.sock")
for instance in ["another-unix-listen", "unix-upstream"]:
machine.wait_for_open_unix_socket(f"/run/anubis/anubis-{instance}/anubis.sock")
machine.wait_for_open_unix_socket(f"/run/anubis/anubis-{instance}/anubis-metrics.sock")
machine.succeed('curl -f http://basic.localhost | grep "it works"')
machine.succeed('curl -f http://basic.localhost -H "User-Agent: Mozilla" | grep anubis')
machine.succeed('curl -f http://basic.localhost/metrics | grep anubis_challenges_issued')
machine.succeed('curl -f http://another-unix-listen.localhost -H "User-Agent: Mozilla" | grep anubis')
machine.succeed('curl -f http://another-unix-listen.localhost/metrics | grep anubis_challenges_issued')
# Upstream is a unix socket mode.
machine.succeed('curl -f http://unix.localhost/index.html | grep "it works"')
# Default user-defined environment variables.
machine.succeed('cat /run/current-system/etc/systemd/system/anubis.service | grep "USER_DEFINED_DEFAULT"')
machine.succeed('cat /run/current-system/etc/systemd/system/anubis-tcp.service | grep "USER_DEFINED_DEFAULT"')
# Instance-specific user-specified environment variables.
machine.succeed('cat /run/current-system/etc/systemd/system/anubis.service | grep "USER_DEFINED_INSTANCE"')
machine.fail('cat /run/current-system/etc/systemd/system/anubis-tcp.service | grep "USER_DEFINED_INSTANCE"')
# Make sure defaults don't overwrite themselves.
machine.succeed('cat /run/current-system/etc/systemd/system/anubis.service | grep "DIFFICULTY=5"')
machine.succeed('cat /run/current-system/etc/systemd/system/anubis-tcp.service | grep "DIFFICULTY=3"')
# Check correct policy settings are applied.
machine.fail('cat /run/current-system/etc/systemd/system/anubis.service | grep "POLICY_FNAME="')
machine.fail('cat /run/current-system/etc/systemd/system/anubis-policy-default.service | grep "POLICY_FNAME="')
machine.succeed('cat /run/current-system/etc/systemd/system/anubis-policy-custom.service | grep "POLICY_FNAME=/nix/store"')
machine.succeed('cat /run/current-system/etc/systemd/system/anubis-policy-file.service | grep "POLICY_FNAME=/etc/anubis-botPolicy.json"')
'';
}
|