Commit 63caf38e authored by Ivan Trubach's avatar Ivan Trubach
Browse files

nixos/prometheus.exporters.pgbouncer: fix escaping connectionStringFile in shell arguments

parent 6008ed89
Loading
Loading
Loading
Loading
+33 −32
Original line number Diff line number Diff line
@@ -4,15 +4,21 @@ let
  cfg = config.services.prometheus.exporters.pgbouncer;
  inherit (lib)
    mkOption
    mkPackageOption
    types
    optionals
    optionalString
    getExe
    getExe'
    escapeShellArg
    escapeShellArgs
    concatStringsSep
    ;
in
{
  port = 9127;
  extraOpts = {
    package = mkPackageOption pkgs "prometheus-pgbouncer-exporter" { };

    telemetryPath = mkOption {
      type = types.str;
@@ -120,35 +126,30 @@ in

  serviceOpts = {
    after = [ "pgbouncer.service" ];
      serviceConfig = let
      startScript = pkgs.writeShellScriptBin "pgbouncer-start" "${concatStringsSep " " ([
            "${pkgs.prometheus-pgbouncer-exporter}/bin/pgbouncer_exporter"
    script = optionalString (cfg.connectionStringFile != null) ''
      connectionString=$(${escapeShellArgs [
        (getExe' pkgs.coreutils "cat") "--" cfg.connectionStringFile
      ]})
    '' + concatStringsSep " " ([
      "exec -- ${escapeShellArg (getExe cfg.package)}"
      "--web.listen-address ${cfg.listenAddress}:${toString cfg.port}"
            "--pgBouncer.connectionString ${if cfg.connectionStringFile != null then
            "$(head -n1 ${cfg.connectionStringFile})" else "${escapeShellArg cfg.connectionString}"}"
          ]
            ++ optionals (cfg.telemetryPath != null) [
      "--pgBouncer.connectionString ${if cfg.connectionStringFile != null
          then "\"$connectionString\""
          else "${escapeShellArg cfg.connectionString}"}"
    ] ++ optionals (cfg.telemetryPath != null) [
      "--web.telemetry-path ${escapeShellArg cfg.telemetryPath}"
          ]
            ++ optionals (cfg.pidFile != null) [
            "--pgBouncer.pid-file= ${escapeShellArg cfg.pidFile}"
          ]
            ++ optionals (cfg.logLevel != null) [
    ] ++ optionals (cfg.pidFile != null) [
      "--pgBouncer.pid-file ${escapeShellArg cfg.pidFile}"
    ] ++ optionals (cfg.logLevel != null) [
      "--log.level ${escapeShellArg cfg.logLevel}"
          ]
            ++ optionals (cfg.logFormat != null) [
    ] ++ optionals (cfg.logFormat != null) [
      "--log.format ${escapeShellArg cfg.logFormat}"
          ]
            ++ optionals (cfg.webSystemdSocket != false) [
    ] ++ optionals (cfg.webSystemdSocket != false) [
      "--web.systemd-socket ${escapeShellArg cfg.webSystemdSocket}"
          ]
            ++ optionals (cfg.webConfigFile != null) [
    ] ++ optionals (cfg.webConfigFile != null) [
      "--web.config.file ${escapeShellArg cfg.webConfigFile}"
          ]
            ++ cfg.extraFlags)}";
      in
      {
        ExecStart = "${startScript}/bin/pgbouncer-start";
      };
    ] ++ cfg.extraFlags);

    serviceConfig.RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
  };
}