feat: add optional basic auth support for Loki client
Some Loki deployments (e.g., behind a reverse proxy or Grafana Cloud) require HTTP Basic Authentication. This adds optional --loki-username and --loki-password flags (and corresponding env vars) to the lab-monitoring server, along with NixOS module options for secure credential management via systemd LoadCredential. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -39,6 +39,18 @@ in
|
||||
description = "Loki base URL. When set, enables log query tools (query_logs, list_labels, list_label_values).";
|
||||
};
|
||||
|
||||
lokiUsername = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "Username for Loki basic authentication.";
|
||||
};
|
||||
|
||||
lokiPasswordFile = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.path;
|
||||
default = null;
|
||||
description = "Path to a file containing the password for Loki basic authentication. Recommended over storing secrets in the Nix store.";
|
||||
};
|
||||
|
||||
enableSilences = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
@@ -112,12 +124,17 @@ in
|
||||
ALERTMANAGER_URL = cfg.alertmanagerUrl;
|
||||
} // lib.optionalAttrs (cfg.lokiUrl != null) {
|
||||
LOKI_URL = cfg.lokiUrl;
|
||||
} // lib.optionalAttrs (cfg.lokiUsername != null) {
|
||||
LOKI_USERNAME = cfg.lokiUsername;
|
||||
};
|
||||
|
||||
script = let
|
||||
httpFlags = mkHttpFlags cfg.http;
|
||||
silenceFlag = lib.optionalString cfg.enableSilences "--enable-silences";
|
||||
in ''
|
||||
${lib.optionalString (cfg.lokiPasswordFile != null) ''
|
||||
export LOKI_PASSWORD="$(< "$CREDENTIALS_DIRECTORY/loki-password")"
|
||||
''}
|
||||
exec ${cfg.package}/bin/lab-monitoring serve ${httpFlags} ${silenceFlag}
|
||||
'';
|
||||
|
||||
@@ -126,7 +143,9 @@ in
|
||||
DynamicUser = true;
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
|
||||
} // lib.optionalAttrs (cfg.lokiPasswordFile != null) {
|
||||
LoadCredential = [ "loki-password:${cfg.lokiPasswordFile}" ];
|
||||
} // {
|
||||
# Hardening
|
||||
NoNewPrivileges = true;
|
||||
ProtectSystem = "strict";
|
||||
|
||||
Reference in New Issue
Block a user