Skip to content

Commit

Permalink
nixos/prometheus: Harden alertmanager/webhook-logger/pushgateway syst…
Browse files Browse the repository at this point in the history
…emd definitions
  • Loading branch information
jpds committed Jul 6, 2024
1 parent 7ce4fe5 commit 008ea18
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ in
${escapeShellArgs cfg.extraFlags}
'';

CapabilityBoundingSet = [ "" ];
DeviceAllow = [ "" ];
DynamicUser = true;
NoNewPrivileges = true;

MemoryDenyWriteExecute = true;

LockPersonality = true;

ProtectProc = "invisible";
ProtectSystem = "strict";
ProtectHome = "tmpfs";
Expand All @@ -43,14 +49,19 @@ in
PrivateDevices = true;
PrivateIPC = true;

ProcSubset = "pid";

ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;

Restart = "on-failure";

RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;

Expand Down
52 changes: 47 additions & 5 deletions nixos/modules/services/monitoring/prometheus/alertmanager.nix
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,57 @@ in {
-i "${alertmanagerYml}"
'';
serviceConfig = {
Restart = "always";
StateDirectory = "alertmanager";
DynamicUser = true; # implies PrivateTmp
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
WorkingDirectory = "/tmp";
ExecStart = "${cfg.package}/bin/alertmanager" +
optionalString (length cmdlineArgs != 0) (" \\\n " +
concatStringsSep " \\\n " cmdlineArgs);
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";

EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;

CapabilityBoundingSet = [ "" ];
DeviceAllow = [ "" ];
DynamicUser = true;
NoNewPrivileges = true;

MemoryDenyWriteExecute = true;

LockPersonality = true;

ProtectProc = "invisible";
ProtectSystem = "strict";
ProtectHome = "tmpfs";

PrivateTmp = true;
PrivateDevices = true;
PrivateIPC = true;

ProcSubset = "pid";

ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;

Restart = "always";

RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_NETLINK" ];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;

StateDirectory = "alertmanager";
SystemCallFilter = [
"@system-service"
"~@cpu-emulation"
"~@privileged"
"~@reboot"
"~@setuid"
"~@swap"
];

WorkingDirectory = "/tmp";
};
};
})
Expand Down
44 changes: 42 additions & 2 deletions nixos/modules/services/monitoring/prometheus/pushgateway.nix
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,52 @@ in {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
Restart = "always";
DynamicUser = true;
ExecStart = "${cfg.package}/bin/pushgateway" +
optionalString (length cmdlineArgs != 0) (" \\\n " +
concatStringsSep " \\\n " cmdlineArgs);

CapabilityBoundingSet = [ "" ];
DeviceAllow = [ "" ];
DynamicUser = true;
NoNewPrivileges = true;

MemoryDenyWriteExecute = true;

LockPersonality = true;

ProtectProc = "invisible";
ProtectSystem = "strict";
ProtectHome = "tmpfs";

PrivateTmp = true;
PrivateDevices = true;
PrivateIPC = true;

ProcSubset = "pid";

ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;

Restart = "always";

RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;

StateDirectory = if cfg.persistMetrics then cfg.stateDir else null;
SystemCallFilter = [
"@system-service"
"~@cpu-emulation"
"~@privileged"
"~@reboot"
"~@setuid"
"~@swap"
];
};
};
};
Expand Down

0 comments on commit 008ea18

Please sign in to comment.