Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OPS-1161] Harden systemd services #781

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
in pkgs-darwin.lib.recursiveUpdate
{
nixosModules = {
tezos-node = import ./nix/modules/tezos-node.nix;
tezos-accuser = import ./nix/modules/tezos-accuser.nix;
tezos-baker = import ./nix/modules/tezos-baker.nix;
tezos-signer = import ./nix/modules/tezos-signer.nix;
tezos-node = import ./nix/modules/tezos-node.nix { inherit inputs; };
tezos-accuser = import ./nix/modules/tezos-accuser.nix { inherit inputs; };
tezos-baker = import ./nix/modules/tezos-baker.nix { inherit inputs; };
tezos-signer = import ./nix/modules/tezos-signer.nix { inherit inputs; };
};

devShells."aarch64-darwin".autorelease-macos =
Expand Down
20 changes: 9 additions & 11 deletions nix/modules/common.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# SPDX-FileCopyrightText: 2021 Oxhead Alpha
# SPDX-License-Identifier: LicenseRef-MIT-OA

{ lib, pkgs, ... }:
{ lib, pkgs, inputs, ... }:

with lib;
rec {
let
inherit (inputs.serokell-nix.lib.systemd) hardeningProfiles withHardeningProfile;
in rec {
sharedOptions = {

logVerbosity = mkOption {
Expand Down Expand Up @@ -46,14 +48,14 @@ rec {
};
};

genDaemonConfig = { instancesCfg, service-name, service-pkgs, service-start-script, service-prestart-script ? (_: "")}:
genDaemonConfig = { instancesCfg, service-name, service-pkgs, service-start-script, service-prestart-script ? (_: ""), extraServiceConfig ? {}}:
mkIf (instancesCfg != {}) {
users = mkMerge (flip mapAttrsToList instancesCfg (node-name: node-cfg: genUsers node-name ));
systemd = mkMerge (flip mapAttrsToList instancesCfg (node-name: node-cfg:
let octez-client = "${pkgs.octezPackages.octez-client}/bin/octez-client";
passwordFilenameArg = if node-cfg.passwordFilename != null then "-f ${node-cfg.passwordFilename}" else "";
in {
services."tezos-${node-name}-octez-${service-name}" = lib.recursiveUpdate (genSystemdService node-name node-cfg service-name) rec {
services."tezos-${node-name}-octez-${service-name}" = lib.recursiveUpdate (genSystemdService node-name node-cfg service-name (extraServiceConfig // {Type = "forking";})) rec {
bindsTo = [ "network.target" "tezos-${node-name}-octez-node.service" ];
after = bindsTo;
path = with pkgs; [ curl ];
Expand All @@ -77,9 +79,6 @@ rec {
fi
'' + service-prestart-script node-cfg;
script = service-start-script node-cfg;
serviceConfig = {
Type = "forking";
};
};
}));
};
Expand All @@ -89,20 +88,19 @@ rec {
users."tezos-${node-name}" = { group = "tezos-${node-name}"; isNormalUser = true; };
};

genSystemdService = node-name: node-cfg: service-name: {
genSystemdService = node-name: node-cfg: service-name: extraServiceConfig: {
inherit (node-cfg) enable;
wantedBy = [ "multi-user.target" ];
description = "Octez ${service-name}";
environment = {
OCTEZ_LOG = "* -> ${node-cfg.logVerbosity}";
};
serviceConfig = {
serviceConfig = withHardeningProfile hardeningProfiles.backend ({
User = "tezos-${node-name}";
Group = "tezos-${node-name}";
StateDirectory = "tezos-${node-name}";
Restart = "always";
RestartSec = "10";
};
} // extraServiceConfig);
};

}
3 changes: 2 additions & 1 deletion nix/modules/tezos-accuser.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2021 Oxhead Alpha
# SPDX-License-Identifier: LicenseRef-MIT-OA

{inputs}:
{config, lib, pkgs, ...}:

with lib;
Expand All @@ -13,7 +14,7 @@ let
"${pkgs.octezPackages.octez-accuser-Proxford}/bin/octez-accuser-Proxford";
};
cfg = config.services.octez-accuser;
common = import ./common.nix { inherit lib; inherit pkgs; };
common = import ./common.nix { inherit lib pkgs inputs; };
instanceOptions = types.submodule ( {...} : {
options = common.daemonOptions // {

Expand Down
6 changes: 5 additions & 1 deletion nix/modules/tezos-baker.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2021 Oxhead Alpha
# SPDX-License-Identifier: LicenseRef-MIT-OA

{inputs}:
{config, lib, pkgs, ...}:

with lib;
Expand All @@ -14,7 +15,7 @@ let
};
octez-client = "${pkgs.octezPackages.octez-client}/bin/octez-client";
cfg = config.services.octez-baker;
common = import ./common.nix { inherit lib; inherit pkgs; };
common = import ./common.nix { inherit lib pkgs inputs; };
instanceOptions = types.submodule ( {...} : {
options = common.daemonOptions // {

Expand Down Expand Up @@ -76,5 +77,8 @@ in {
service-pkgs = octez-baker-pkgs;
service-start-script = baker-start-script;
service-prestart-script = baker-prestart-script;
extraServiceConfig = {
PrivateDevices = "no";
};
};
}
12 changes: 10 additions & 2 deletions nix/modules/tezos-node.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2021 Oxhead Alpha
# SPDX-License-Identifier: LicenseRef-MIT-OA

{inputs}:
{ config, lib, pkgs, ... }:

with lib;
Expand All @@ -15,7 +16,14 @@ let
--net-addr ":${toString netPort}" \
--network "${network}" ${builtins.concatStringsSep " " options}
'';
common = import ./common.nix { inherit lib; inherit pkgs; };
common = import ./common.nix { inherit lib pkgs inputs; };
extraServiceConfig = {
RestrictAddressFamilies = [
"AF_INET"
"AF_UNIX"
"AF_INET6"
];
};
instanceOptions = types.submodule ( {...} : {
options = common.sharedOptions // {
enable = mkEnableOption "Octez node service";
Expand Down Expand Up @@ -92,7 +100,7 @@ in {
config = mkIf (cfg.instances != {}) {
users = mkMerge (flip mapAttrsToList cfg.instances (node-name: node-cfg: common.genUsers node-name ));
systemd = mkMerge (flip mapAttrsToList cfg.instances (node-name: node-cfg: {
services."tezos-${node-name}-octez-node" = common.genSystemdService node-name node-cfg "node" // {
services."tezos-${node-name}-octez-node" = common.genSystemdService node-name node-cfg "node" extraServiceConfig // {
after = [ "network.target" ];
preStart =
''
Expand Down
5 changes: 3 additions & 2 deletions nix/modules/tezos-signer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#
# SPDX-License-Identifier: LicenseRef-MIT-TQ

{inputs}:
{config, lib, pkgs, ...}:

with lib;

let
octez-signer-launch = "${pkgs.octezPackages.octez-signer}/bin/octez-signer launch";
common = import ./common.nix { inherit lib; inherit pkgs; };
common = import ./common.nix { inherit lib pkgs inputs; };
cfg = config.services.octez-signer;
instanceOptions = types.submodule ( {...} : {
options = common.sharedOptions // {
Expand Down Expand Up @@ -98,7 +99,7 @@ in {
"${octez-signer-launch} local signer --socket ${node-cfg.unixSocket}";
};
in {
services."tezos-${node-name}-octez-signer" = common.genSystemdService node-name node-cfg "signer" // {
services."tezos-${node-name}-octez-signer" = common.genSystemdService node-name node-cfg "signer" {} // {
after = [ "network.target" ];
script = ''
${octez-signers.${node-cfg.networkProtocol}}
Expand Down