nebula: Enable firewall and restrict ssh access by role

This commit is contained in:
SebastianStork 2026-01-03 00:41:13 +01:00
parent 382dae6cbb
commit 61f5c54196
Signed by: SebastianStork
SSH key fingerprint: SHA256:tRrGdjYOwgHxpSc/wTOZQZEjxcb15P0tyXRsbAfd+2Q
5 changed files with 69 additions and 46 deletions

View file

@ -33,6 +33,7 @@
nebula.node = { nebula.node = {
enable = true; enable = true;
address = "10.254.250.1"; address = "10.254.250.1";
isClient = true;
}; };
syncthing = { syncthing = {
enable = true; enable = true;

View file

@ -36,6 +36,7 @@
nebula.node = { nebula.node = {
enable = true; enable = true;
address = "10.254.250.3"; address = "10.254.250.3";
isClient = true;
}; };
syncthing = { syncthing = {
enable = true; enable = true;

View file

@ -34,8 +34,9 @@
nebula.node = { nebula.node = {
enable = true; enable = true;
address = "10.254.250.2"; address = "10.254.250.2";
isLighthouse = true;
routableAddress = "49.13.231.235"; routableAddress = "49.13.231.235";
isLighthouse = true;
isServer = true;
}; };
syncthing = { syncthing = {

View file

@ -6,32 +6,29 @@
}: }:
let let
cfg = config.custom.services.nebula.node; cfg = config.custom.services.nebula.node;
peers = config.custom.services.nebula.peers;
hostname = config.networking.hostName; hostname = config.networking.hostName;
nodes = lighthouses = peers |> lib.filter (node: node.isLighthouse);
self.nixosConfigurations
|> lib.filterAttrs (name: _: name != hostname)
|> lib.attrValues
|> lib.map (value: value.config.custom.services.nebula.node)
|> lib.filter (node: node.enable);
lighthouses = nodes |> lib.filter (node: node.isLighthouse); routablePeers = peers |> lib.filter (node: node.routableAddress != null);
routableNodes = nodes |> lib.filter (node: node.routableAddress != null);
in in
{ {
options.custom.services.nebula.node = { options.custom.services.nebula = {
node = {
enable = lib.mkEnableOption ""; enable = lib.mkEnableOption "";
name = lib.mkOption { name = lib.mkOption {
type = lib.types.nonEmptyStr; type = lib.types.nonEmptyStr;
default = config.networking.hostName; default = hostname;
}; };
address = lib.mkOption { address = lib.mkOption {
type = lib.types.nonEmptyStr; type = lib.types.nonEmptyStr;
default = ""; default = "";
}; };
isLighthouse = lib.mkEnableOption ""; isLighthouse = lib.mkEnableOption "";
isServer = lib.mkEnableOption "";
isClient = lib.mkEnableOption "";
routableAddress = lib.mkOption { routableAddress = lib.mkOption {
type = lib.types.nullOr lib.types.nonEmptyStr; type = lib.types.nullOr lib.types.nonEmptyStr;
@ -52,6 +49,18 @@ in
}; };
}; };
peers = lib.mkOption {
type = lib.types.anything;
default =
self.nixosConfigurations
|> lib.filterAttrs (name: _: name != hostname)
|> lib.attrValues
|> lib.map (value: value.config.custom.services.nebula.node)
|> lib.filter (node: node.enable);
readOnly = true;
};
};
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
meta.ports.udp = lib.optional (cfg.routablePort != null) cfg.routablePort; meta.ports.udp = lib.optional (cfg.routablePort != null) cfg.routablePort;
@ -80,7 +89,7 @@ in
); );
staticHostMap = staticHostMap =
routableNodes routablePeers
|> lib.map (lighthouse: { |> lib.map (lighthouse: {
name = lighthouse.address; name = lighthouse.address;
value = lib.singleton "${lighthouse.routableAddress}:${toString lighthouse.routablePort}"; value = lib.singleton "${lighthouse.routableAddress}:${toString lighthouse.routablePort}";
@ -89,14 +98,14 @@ in
firewall = { firewall = {
outbound = lib.singleton { outbound = lib.singleton {
host = "any";
port = "any"; port = "any";
proto = "any"; proto = "any";
host = "any";
}; };
inbound = lib.singleton { inbound = lib.singleton {
host = "any";
port = "any"; port = "any";
proto = "any"; proto = "icmp";
host = "any";
}; };
}; };

View file

@ -16,7 +16,8 @@ in
config = lib.mkIf (cfg.enable && cfg.sshd.enable) { config = lib.mkIf (cfg.enable && cfg.sshd.enable) {
meta.ports.tcp = [ cfg.sshd.port ]; meta.ports.tcp = [ cfg.sshd.port ];
services.openssh = { services = {
openssh = {
enable = true; enable = true;
openFirewall = false; openFirewall = false;
ports = [ ]; ports = [ ];
@ -26,6 +27,16 @@ in
}; };
}; };
nebula.networks.mesh.firewall.inbound =
config.custom.services.nebula.peers
|> lib.filter (node: node.isClient)
|> lib.map (nebula: {
port = "22";
proto = "tcp";
host = nebula.name;
});
};
systemd.services.sshd = { systemd.services.sshd = {
requires = [ "nebula@mesh.service" ]; requires = [ "nebula@mesh.service" ];
after = [ "nebula@mesh.service" ]; after = [ "nebula@mesh.service" ];