forked from input-output-hk/cardano-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.nix
306 lines (270 loc) · 9.3 KB
/
metadata.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
304
305
306
pkgs: with pkgs; { nodes, name, config, ... }:
let
cfg = config.services.metadata-server;
hostAddr = getListenIp nodes.${name};
metadataServerPort = 8080;
metadataWebhookPort = 8081;
webhookKeys = import ../static/metadata-webhook-secrets.nix;
# The maximum POST size allowed for a metadata/query body payload
maxPostSizeBodyKb = 64;
# The maximum cacheable POST size before varnish will disconnect and cause nginx to 502
maxPostSizeCachableKb = 100;
in {
environment = {
systemPackages = with pkgs; [
bat fd lsof netcat ncdu ripgrep tree vim
];
};
imports = [
cardano-ops.modules.common
cardano-ops.modules.cardano-postgres
(sourcePaths.offchain-metadata-tools + "/nix/nixos")
];
# Disallow metadata-server to restart more than 3 times within a 30 minute window
# This ensures the service stops and an alert will get sent if there is a persistent restart issue
# This also allows for some additional startup time before failure and restart
#
# If metadata-server fails and the service needs to be restarted manually before the 30 min window ends, run:
# systemctl reset-failed metadata-server && systemctl start metadata-server
#
# Same as above for metadata-webhook service
systemd.services.metadata-server.startLimitIntervalSec = 1800;
systemd.services.metadata-webhook.startLimitIntervalSec = 1800;
systemd.services.metadata-server.serviceConfig = {
Restart = "always";
RestartSec = "30s";
# Not yet available as an attribute for the Unit section in nixpkgs 20.09
StartLimitBurst = 3;
RuntimeMaxSec = 43200;
};
systemd.services.metadata-webhook.serviceConfig = {
Restart = "always";
RestartSec = "30s";
# Not yet available as an attribute for the Unit section in nixpkgs 20.09
StartLimitBurst = 3;
};
services.metadata-server = {
enable = true;
port = metadataServerPort;
};
services.metadata-webhook = {
enable = true;
port = metadataWebhookPort;
webHookSecret = webhookKeys.webHookSecret;
gitHubToken = webhookKeys.gitHubToken;
postgres = {
socketdir = config.services.metadata-server.postgres.socketdir;
port = config.services.metadata-server.postgres.port;
database = config.services.metadata-server.postgres.database;
table = config.services.metadata-server.postgres.table;
user = config.services.metadata-server.postgres.user;
numConnections = config.services.metadata-server.postgres.numConnections;
};
};
services.cardano-postgres = {
enable = true;
withHighCapacityPostgres = false;
};
services.postgresql = {
ensureDatabases = [ "${cfg.postgres.database}" ];
ensureUsers = [
{
name = "${cfg.postgres.user}";
ensurePermissions = {
"DATABASE ${cfg.postgres.database}" = "ALL PRIVILEGES";
};
}
];
identMap = ''
metadata-users root ${cfg.postgres.user}
metadata-users ${cfg.user} ${cfg.postgres.user}
metadata-users ${config.services.metadata-webhook.user} ${cfg.postgres.user}
metadata-users postgres postgres
'';
authentication = ''
local all all ident map=metadata-users
'';
};
security.acme = lib.mkIf (config.deployment.targetEnv != "libvirtd") {
email = "[email protected]";
acceptTerms = true; # https://letsencrypt.org/repository/
};
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.varnish = {
enable = true;
extraModules = [ pkgs.varnish-modules ];
config = ''
vcl 4.1;
import std;
import bodyaccess;
backend default {
.host = "127.0.0.1";
.port = "${toString metadataServerPort}";
}
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv {
unset req.http.X-Body-Len;
unset req.http.x-cache;
# Allow PURGE from localhost
if (req.method == "PURGE") {
if (!std.ip(req.http.X-Real-Ip, "0.0.0.0") ~ purge) {
return(synth(405,"Not Allowed"));
}
# The host is included as part of the object hash
# We need to match the public FQDN for the purge to be successful
set req.http.host = "${globals.metadataHostName}";
}
# Allow POST caching
# PURGE also needs to hash the body to obtain a correct object hash to purge
if (req.method == "POST" || req.method == "PURGE") {
# Caches the body which enables POST retries if needed
std.cache_req_body(${toString maxPostSizeCachableKb}KB);
set req.http.X-Body-Len = bodyaccess.len_req_body();
if ((std.integer(req.http.X-Body-Len, ${toString (1024 * maxPostSizeCachableKb)}) > ${toString (1024 * maxPostSizeBodyKb)}) ||
(req.http.X-Body-Len == "-1")) {
return(synth(413, "Payload Too Large"));
}
if (req.method == "PURGE") {
return(purge);
}
return(hash);
}
}
sub vcl_hash {
# For caching POSTs, hash the body also
if (req.http.X-Body-Len) {
bodyaccess.hash_req_body();
}
else {
hash_data("");
}
}
sub vcl_hit {
set req.http.x-cache = "hit";
}
sub vcl_miss {
set req.http.x-cache = "miss";
}
sub vcl_pass {
set req.http.x-cache = "pass";
}
sub vcl_pipe {
set req.http.x-cache = "pipe";
}
sub vcl_synth {
set req.http.x-cache = "synth synth";
set resp.http.x-cache = req.http.x-cache;
}
sub vcl_deliver {
if (obj.uncacheable) {
set req.http.x-cache = req.http.x-cache + " uncacheable";
}
else {
set req.http.x-cache = req.http.x-cache + " cached";
}
set resp.http.x-cache = req.http.x-cache;
}
sub vcl_backend_fetch {
if (bereq.http.X-Body-Len) {
set bereq.method = "POST";
}
}
sub vcl_backend_response {
set beresp.ttl = 4h;
if (beresp.status == 404) {
set beresp.ttl = 1h;
}
}
'';
};
# Ensure the worker processes don't hit TCP file descriptor limits
systemd.services.nginx.serviceConfig.LimitNOFILE = 65535;
services.nginx = {
enable = true;
package = nginxMetadataServer;
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
eventsConfig = ''
worker_connections 8192;
'';
commonHttpConfig = ''
log_format x-fwd '$remote_addr - $remote_user $sent_http_x_cache [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
access_log syslog:server=unix:/dev/log x-fwd if=$loggable;
limit_req_zone $binary_remote_addr zone=metadataQueryPerIP:100m rate=10r/s;
limit_req_status 429;
server_names_hash_bucket_size 128;
map $sent_http_x_cache $loggable_varnish {
default 1;
"hit cached" 0;
}
map $request_uri $loggable {
/status/format/prometheus 0;
default $loggable_varnish;
}
'';
virtualHosts = {
"${globals.metadataHostName}" = {
enableACME = config.deployment.targetEnv != "libvirtd";
forceSSL = globals.explorerForceSSL && (config.deployment.targetEnv != "libvirtd");
locations =
let
corsConfig = ''
add_header 'Vary' 'Origin' always;
add_header 'Access-Control-Allow-Methods' 'GET, PATCH, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'User-Agent,X-Requested-With,Content-Type' always;
if ($request_method = OPTIONS) {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
'';
serverEndpoints = [
"/metadata/query"
"/metadata"
];
webhookEndpoints = [
"/webhook"
];
in (lib.recursiveUpdate (lib.genAttrs serverEndpoints (p: {
proxyPass = "http://127.0.0.1:${toString metadataServerPort}${p}";
extraConfig = corsConfig;
})) {
# Add varnish caching to only the `/metadata/query` endpoint
"/metadata/query".proxyPass = "http://127.0.0.1:6081/metadata/query";
"/metadata/query".extraConfig = ''
limit_req zone=metadataQueryPerIP burst=20 nodelay;
${corsConfig}
'';
}) // (lib.genAttrs webhookEndpoints (p: {
proxyPass = "http://127.0.0.1:${toString metadataWebhookPort}${p}";
extraConfig = corsConfig;
}));
};
"metadata-ip" = {
locations = {
# TODO if metadata server offers metrics
#"/metrics/metadata" = {
# proxyPass = "http://127.0.0.1:8080/";
#};
};
};
};
};
# Avoid flooding (and rotating too quicky) default journal with nginx logs.
# nginx logs: journalctl --namespace nginx
systemd.services.nginx.serviceConfig.LogNamespace = "nginx";
services.monitoring-exporters.extraPrometheusExporters = [
#{
# job_name = "metadata";
# scrape_interval = "10s";
# metrics_path = "/metrics/metadata";
#}
];
}