Watch
0
0
Fork
You've already forked hyperhive
0

gateway: answer 503 for a store outage and 429 for the rate limit, and retry both in swarm-logs and swarm-metrics

This commit is contained in:
atlas 2026-11-30 15:25:53 +01:00 • committed by mara
commit 790cdd43c8
4 changed files with 709 additions and 125 deletions

View file

@ -257,7 +257,7 @@ in
so every agent on one host shares one budget. The default covers about a
hundred agents on a host cold-starting at once, each costing one store
login and read for its first query. Requests over the rate and its burst
are answered 403.
are answered 429.
'';
};
@ -267,7 +267,7 @@ in
description = ''
Requests a client address may make above
{option}`services.hyperhive.deploy.victorialogs.authRate` before nginx
answers 403, served without delay.
answers 429, served without delay.
'';
};
@ -331,7 +331,11 @@ in
"~^Bearer [0-9a-f]{64}$" /__swarm_logs_grafana_auth;
default /__hive_authelia;
}
limit_req_zone $binary_remote_addr zone=swarm_logs_agent_auth:1m rate=${deployCfg.victorialogs.authRate};
map $http_authorization $swarm_logs_agent_limit_key {
"~^Bearer swarm-agent\." $binary_remote_addr;
default "";
}
limit_req_zone $swarm_logs_agent_limit_key zone=swarm_logs_agent_auth:1m rate=${deployCfg.victorialogs.authRate};
map $http_authorization $swarm_logs_ingest_nocache {
"" 1;
default 0;
@ -420,12 +424,36 @@ in
# Three checks: an agent's own `swarm-agent.` bearer goes to
# `swarm-logs-auth`, Grafana's 64-hex bearer to `swarm-metrics-auth`,
# anything else to authelia (`appendHttpConfig` above picks).
#
# The agents' rate limit and the 503 hand-on are set on this location;
# see `@swarm_logs_auth_failed` below.
"^~ /select/logsql/" = {
# No URI part, so the request path and its query string reach
# the store as the caller sent them.
proxyPass = upstream;
extraConfig = ''
limit_req zone=swarm_logs_agent_auth burst=${toString deployCfg.victorialogs.authBurst} nodelay;
limit_req_status 429;
auth_request /__swarm_logs_auth;
auth_request_set $swarm_logs_auth_status $upstream_status;
error_page 500 = @swarm_logs_auth_failed;
'';
};
# Where a failed `auth_request` lands: the 503 the responder answered
# when its store could not, and a 500 for any other status.
# `auth_request` turns every subrequest status but 2xx, 401 and 403
# into a 500, so this is how a 503 reaches the caller. For the same
# reason the rate limit is on the query location and not in the agent
# subrequest: there a 429 would become a 500. `limit_req` runs before
# `auth_request` and skips a request whose key is empty, which
# `swarm_logs_agent_limit_key` makes of every non-agent request.
"@swarm_logs_auth_failed" = {
extraConfig = ''
if ($swarm_logs_auth_status = 503) {
return 503;
}
return 500;
'';
};
@ -458,16 +486,12 @@ in
# The `Authorization` header rides the subrequest unchanged.
#
# Any well-formed agent bearer the responder has not recently granted
# costs a secret store login and read, so the rate per client address
# is capped here, ahead of the responder. 403 when over: the only
# statuses `auth_request` passes through are 401 and 403, and
# `swarm-logs` retries a 401.
# costs a secret store login and read; the rate per client address is
# capped on the query location, ahead of this subrequest.
"= /__swarm_logs_agent_auth" = {
proxyPass = "http://unix:${logsAuthSocket}:/";
extraConfig = ''
internal;
limit_req zone=swarm_logs_agent_auth burst=${toString deployCfg.victorialogs.authBurst} nodelay;
limit_req_status 403;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
'';

View file

@ -96,8 +96,20 @@ let
# `error_page 401 =302` follows it and reads authelia's login page as a 200
# holding no series. `/__metrics_read_auth` picks the check from the
# caller's `Authorization` (`appendHttpConfig` below).
#
# The agents' rate limit is here and not in the agent subrequest:
# `auth_request` turns any subrequest status but 2xx, 401 and 403 into a 500,
# so a limit hit there could not reach the caller as a 429. `limit_req` runs
# before `auth_request` and does not count a request whose key is empty,
# which `swarm_metrics_agent_limit_key` makes of every request that is not
# an agent's. A responder answering 503 (its store cannot answer) is the one
# other subrequest status handed on, by `@swarm_metrics_auth_failed`.
readAuth = ''
limit_req zone=swarm_metrics_agent_auth burst=${toString deployCfg.victoriametrics.agentAuthBurst} nodelay;
limit_req_status 429;
auth_request /__metrics_read_auth;
auth_request_set $swarm_metrics_auth_status $upstream_status;
error_page 500 = @swarm_metrics_auth_failed;
'';
readLocations =
let
@ -336,7 +348,7 @@ in
costs a secret store login and read, whether the bearer is right or not.
Keyed by client address, so every agent on one host shares one budget.
Requests over the rate and its burst are answered 403.
Requests over the rate and its burst are answered 429.
'';
};
@ -346,7 +358,7 @@ in
description = ''
Requests a client address may make above
{option}`services.hyperhive.deploy.victoriametrics.agentAuthRate` before
nginx answers 403, served without delay.
nginx answers 429, served without delay.
'';
};
@ -418,7 +430,11 @@ in
"~^Bearer " /__metrics_grafana_auth;
default /__metrics_authelia;
}
limit_req_zone $binary_remote_addr zone=swarm_metrics_agent_auth:1m rate=${deployCfg.victoriametrics.agentAuthRate};
map $http_authorization $swarm_metrics_agent_limit_key {
"~^Bearer swarm-agent\." $binary_remote_addr;
default "";
}
limit_req_zone $swarm_metrics_agent_limit_key zone=swarm_metrics_agent_auth:1m rate=${deployCfg.victoriametrics.agentAuthRate};
map $http_authorization $swarm_metrics_ingest_nocache {
"" 1;
default 0;
@ -449,6 +465,16 @@ in
# its queries, and authenticates none of them.
"/".return = "403";
# Where a failed read-location `auth_request` lands: the 503 the
# responder answered when its store could not, and a 500 for any
# other status.
"@swarm_metrics_auth_failed".extraConfig = ''
if ($swarm_metrics_auth_status = 503) {
return 503;
}
return 500;
'';
# The swarm UI's quick link opens the bare domain. The store's own
# index page links mostly to paths refused above, so send it to vmui.
"= /".return = "302 /vmui/";
@ -508,14 +534,11 @@ in
# An agent's bearer, checked by `swarm-metrics-agent-auth`. Any
# well-formed agent bearer the responder has not recently granted
# costs a store login and read, so the rate per client address is
# capped here. 403 when over: `auth_request` passes only 401 and 403
# through, and `swarm-metrics` retries a 401.
# capped on the read locations (`readAuth`), ahead of this subrequest.
"= /__metrics_agent_auth" = {
proxyPass = "http://unix:${agentAuthSocket}:/";
extraConfig = ''
internal;
limit_req zone=swarm_metrics_agent_auth burst=${toString deployCfg.victoriametrics.agentAuthBurst} nodelay;
limit_req_status 403;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
'';

View file

@ -1276,7 +1276,11 @@ let
&& lib.all (
r:
lib.hasInfix "auth_request /__metrics_read_auth;" r.extraConfig
&& !(lib.hasInfix "error_page" r.extraConfig)
&& !(lib.hasInfix "error_page 401" r.extraConfig)
&& lib.hasInfix "limit_req zone=swarm_metrics_agent_auth burst=${toString v.agentAuthBurst} nodelay;" r.extraConfig
&& lib.hasInfix "limit_req_status 429;" r.extraConfig
&& lib.hasInfix "auth_request_set $swarm_metrics_auth_status $upstream_status;" r.extraConfig
&& lib.hasInfix "error_page 500 = @swarm_metrics_auth_failed;" r.extraConfig
) (lib.attrValues reads)
&& lib.hasInfix "map \$http_authorization \$swarm_metrics_read_auth_target" http
&& lib.hasInfix agentArm http
@ -1299,10 +1303,12 @@ let
&& lib.hasInfix "--surface metrics" agentUnit.serviceConfig.ExecStart
&& lib.hasInfix "--verdict-ttl ${toString v.agentAuthVerdictTtl}" agentUnit.serviceConfig.ExecStart
&& agentUnit.serviceConfig.LoadCredential == unit.serviceConfig.LoadCredential
&& lib.hasInfix "limit_req_zone $binary_remote_addr zone=swarm_metrics_agent_auth:1m rate=${v.agentAuthRate};" http
&&
lib.hasInfix "limit_req zone=swarm_metrics_agent_auth burst=${toString v.agentAuthBurst} nodelay;"
l."= /__metrics_agent_auth".extraConfig
&& lib.hasInfix "limit_req_zone $swarm_metrics_agent_limit_key zone=swarm_metrics_agent_auth:1m rate=${v.agentAuthRate};" http
&& lib.hasInfix "\"~^Bearer swarm-agent\\.\" $binary_remote_addr;" http
&& !(lib.hasInfix "limit_req" l."= /__metrics_agent_auth".extraConfig)
&& lib.hasInfix "if ($swarm_metrics_auth_status = 503) {" l."@swarm_metrics_auth_failed".extraConfig
&& lib.hasInfix "return 503;" l."@swarm_metrics_auth_failed".extraConfig
&& lib.hasInfix "return 500;" l."@swarm_metrics_auth_failed".extraConfig
&& !(lib.hasInfix "limit_req" l."= /__metrics_grafana_auth".extraConfig)
&& lib.hasInfix "auth_request /__metrics_authelia;" l."^~ /vmui/".extraConfig
&& lib.hasInfix "error_page 401 =302" l."^~ /vmui/".extraConfig
@ -1416,9 +1422,9 @@ let
q = v."logs.t.local".locations."^~ /select/logsql/".extraConfig;
browser = v."logs.t.local".locations."/".extraConfig;
in
!(lib.hasInfix "error_page" q)
!(lib.hasInfix "error_page 401" q)
&& !(lib.hasInfix "auth.t.local" q)
&& lib.hasInfix "error_page" browser
&& lib.hasInfix "error_page 401" browser
&& lib.hasInfix "auth.t.local" browser;
}
{
@ -1526,9 +1532,11 @@ let
}
{
# Every agent bearer the responder has not recently granted costs a
# store login and read, so its location carries the per-address limit.
# The responder gets the grant TTL from the option. The query and
# authelia locations carry no limit of their own.
# store login and read, so the query location limits per address, as a
# 429 the caller sees, and only for an agent's bearer. A responder 503
# is handed on by the named location. The responder gets the grant TTL
# from the option. The agent subrequest and the authelia location carry
# no limit of their own.
name = "agent bearer checks are rate-limited per address and granted for a TTL";
ok =
let
@ -1537,12 +1545,20 @@ let
unit = allLocal.systemd.services.swarm-logs-auth;
v = allLocal.services.hyperhive.deploy.victorialogs;
in
lib.hasInfix "limit_req_zone $binary_remote_addr zone=swarm_logs_agent_auth:1m rate=${v.authRate};" http
lib.hasInfix "limit_req_zone $swarm_logs_agent_limit_key zone=swarm_logs_agent_auth:1m rate=${v.authRate};" http
&& lib.hasInfix "\"~^Bearer swarm-agent\\.\" $binary_remote_addr;" http
&&
lib.hasInfix "limit_req zone=swarm_logs_agent_auth burst=${toString v.authBurst} nodelay;"
l."= /__swarm_logs_agent_auth".extraConfig
&& lib.hasInfix "limit_req_status 403;" l."= /__swarm_logs_agent_auth".extraConfig
&& !(lib.hasInfix "limit_req" l."^~ /select/logsql/".extraConfig)
l."^~ /select/logsql/".extraConfig
&& lib.hasInfix "limit_req_status 429;" l."^~ /select/logsql/".extraConfig
&& !(lib.hasInfix "limit_req" l."= /__swarm_logs_agent_auth".extraConfig)
&&
lib.hasInfix "auth_request_set $swarm_logs_auth_status $upstream_status;"
l."^~ /select/logsql/".extraConfig
&& lib.hasInfix "error_page 500 = @swarm_logs_auth_failed;" l."^~ /select/logsql/".extraConfig
&& lib.hasInfix "if ($swarm_logs_auth_status = 503) {" l."@swarm_logs_auth_failed".extraConfig
&& lib.hasInfix "return 503;" l."@swarm_logs_auth_failed".extraConfig
&& lib.hasInfix "return 500;" l."@swarm_logs_auth_failed".extraConfig
&& !(lib.hasInfix "limit_req" l."= /__hive_authelia".extraConfig)
&& lib.hasInfix "--verdict-ttl 60" unit.serviceConfig.ExecStart;
}

View file

@ -507,7 +507,7 @@ let
let
h = allLocal.services.hyperhive;
locs = allLocal.services.nginx.virtualHosts.${h.swarm.victorialogs.domain}.locations;
checksOf = l: lib.filter (lib.hasPrefix "auth_request") (lib.splitString "\n" l.extraConfig);
checksOf = l: lib.filter (lib.hasPrefix "auth_request /") (lib.splitString "\n" l.extraConfig);
in
h.deploy.victorialogs.enable
&& locs ? "= /metrics"