/
smash.nix
287 lines (256 loc) · 8.59 KB
/
smash.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
pkgs: with pkgs; { nodes, name, config, ... }:
let
nodeCfg = config.services.cardano-node;
cfg = config.services.smash;
inherit (import (sourcePaths.smash + "/nix") {}) smashHaskellPackages;
in {
environment = {
systemPackages = [
bat fd lsof netcat ncdu ripgrep tree vim cardano-cli
];
variables = {
SMASHPGPASSFILE = cfg.postgres.pgpass;
};
};
imports = [
cardano-ops.modules.base-service
cardano-ops.modules.cardano-postgres
(sourcePaths.smash+ "/nix/nixos")
];
services.cardano-node = {
allProducers = [ globals.relaysNew ];
package = smashHaskellPackages.cardano-node.components.exes.cardano-node;
totalMaxHeapSizeMbytes = 0.5 * config.node.memory * 1024;
};
# Disallow smash to restart more than 3 times within a 30 minute window
# This ensures the service stops and an alert will get sent if ledger state is corrupt
# This also allows for some additional startup time before failure and restart
#
# If smash fails and the service needs to be restarted manually before the 30 min window ends, run:
# systemctl reset-failed smash && systemctl start smash
#
systemd.services.smash.startLimitIntervalSec = 1800;
systemd.services.smash.serviceConfig = {
# Put cardano-db-sync in "cardano-node" group so that it can write socket file:
SupplementaryGroups = "cardano-node";
# FIXME: https://github.com/input-output-hk/cardano-db-sync/issues/102
Restart = "always";
RestartSec = "30s";
# Not yet available as an attribute for the Unit section in nixpkgs 20.09
StartLimitBurst = 3;
};
services.smash = {
enable = true;
inherit (globals) environmentName;
environment = globals.environmentConfig;
inherit (nodeCfg) socketPath;
logConfig = iohkNix.cardanoLib.defaultExplorerLogConfig // { PrometheusPort = globals.cardanoExplorerPrometheusExporterPort; };
};
services.cardano-postgres.enable = true;
services.postgresql = {
ensureDatabases = [ "${cfg.postgres.database}" ];
ensureUsers = [
{
name = "${cfg.postgres.user}";
ensurePermissions = {
"DATABASE ${cfg.postgres.database}" = "ALL PRIVILEGES";
};
}
];
identMap = ''
smash-users root ${cfg.postgres.user}
smash-users ${cfg.user} ${cfg.postgres.user}
smash-users postgres postgres
'';
authentication = ''
local all all ident map=smash-users
'';
};
security.acme = lib.mkIf (config.deployment.targetEnv != "libvirtd") {
email = "devops@iohk.io";
acceptTerms = true; # https://letsencrypt.org/repository/
};
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.varnish = {
enable = true;
config = ''
vcl 4.1;
import std;
backend default {
.host = "127.0.0.1";
.port = "3100";
}
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv {
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 = "smash.${globals.domain}";
return(purge);
}
}
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_response {
set beresp.ttl = 30d;
if (beresp.status == 404) {
set beresp.ttl = 1h;
}
# Default vcl_backend_response (without no-store):
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Surrogate-control ~ "no-store" ||
(!beresp.http.Surrogate-Control &&
beresp.http.Cache-Control ~ "no-cache|private") ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120s;
set beresp.uncacheable = true;
}
return (deliver);
}
'';
};
# Ensure the worker processes don't hit TCP file descriptor limits
systemd.services.nginx.serviceConfig.LimitNOFILE = 65535;
services.nginx = {
enable = true;
package = nginxSmash;
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
eventsConfig = ''
worker_connections 8192;
'';
commonHttpConfig = let
apiKeys = import ../static/smash-keys.nix;
allowedOrigins = lib.optionals (builtins.pathExists ../static/smash-allow-origins.nix) (import ../static/smash-allow-origins.nix);
in ''
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"';
map $arg_apiKey $api_client_name {
default "";
${lib.concatStringsSep "\n" (lib.mapAttrsToList (client: key: "\"${key}\" \"${client}\";") apiKeys)}
}
map $http_origin $origin_allowed {
default 0;
${lib.concatStringsSep "\n" (map (origin: "${origin} 1;") allowedOrigins)}
}
map $sent_http_x_cache $loggable_varnish {
"hit cached" 0;
default 1;
}
map $origin_allowed $origin {
default "";
1 $http_origin;
}
'';
virtualHosts = {
"smash.${globals.domain}" = {
enableACME = config.deployment.targetEnv != "libvirtd";
forceSSL = globals.explorerForceSSL && (config.deployment.targetEnv != "libvirtd");
locations =
let
apiKeyConfig = ''
if ($arg_apiKey = "") {
return 401; # Unauthorized (please authenticate)
}
if ($api_client_name = "") {
return 403; # Forbidden (invalid API key)
}
'';
corsConfig = ''
add_header 'Vary' 'Origin' always;
add_header 'Access-Control-Allow-Origin' $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;
}
'';
endpoints = [
"/swagger.json"
"/api/v1/metadata"
"/api/v1/errors"
"/api/v1/exists"
"/api/v1/enlist"
"/api/v1/delist"
"/api/v1/delisted"
"/api/v1/retired"
"/api/v1/status"
"/api/v1/tickers"
];
in lib.recursiveUpdate (lib.genAttrs endpoints (p: {
proxyPass = "http://127.0.0.1:6081${p}";
extraConfig = corsConfig;
})) {
"/api/v1/delist".extraConfig = ''
${corsConfig}
${apiKeyConfig}
'';
"/api/v1/enlist".extraConfig = ''
${corsConfig}
${apiKeyConfig}
'';
"/api/v1/metadata".extraConfig = ''
${corsConfig}
'';
"/api/v1/tickers".extraConfig = ''
${corsConfig}
if ($request_method = POST) {
${apiKeyConfig}
}
};
};
};
};
# 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 = "smash-exporter";
scrape_interval = "10s";
port = globals.cardanoExplorerPrometheusExporterPort;
metrics_path = "/";
labels = { alias = "smash-exporter"; };
}
];
}