Fix duplicate port check

This commit is contained in:
Jakob Lechner 2025-04-10 01:13:11 +02:00
parent 48e71f75f8
commit 6f01431032
39 changed files with 132 additions and 158 deletions

View file

@ -1,33 +1,5 @@
{ lib, ... }:
let
filterPort = pm: port: (
lib.attrsets.catAttrs port (
lib.attrsets.attrValues (
lib.attrsets.filterAttrs (_: v: v ? "${port}") pm
)
)
);
onlyUniqueItemsInList = x: lib.lists.length x == lib.lists.length (lib.lists.unique x);
protocols = x: lib.lists.unique (lib.flatten (map builtins.attrNames (lib.attrValues x)));
mkRange = x: lib.lists.range (builtins.elemAt x 0) (builtins.elemAt x 1);
validateList = allowed: builtins.all (x: builtins.elem x allowed);
in
{
validatePortAttrset = portmap:
if ! onlyUniqueItemsInList (lib.flatten (map
(x:
if lib.isInt x then x
else if lib.isList x then x
else if lib.isAttrs x then
(
if ! validateList [ "range" ] (builtins.attrNames x) then builtins.abort "found invalid attribute name"
else if x ? "range" then if lib.lists.length x.range == 2 then mkRange x.range else builtins.abort "range needs a list with exactly two items"
else builtins.abort "found invalid attrset"
)
else builtins.abort "found invalid entry in portmap"
)
(filterPort portmap "udp"))) then builtins.abort "Found duplicate ports."
else if ! validateList [ "tcp" "udp" ] (protocols portmap) then builtins.abort "Found invalid protocol."
else portmap;
validatePortAttrset = import ./ports.nix { inherit lib; };
}

View file

@ -9,25 +9,25 @@ let
)
);
onlyUniqueItemsInList = x: lib.lists.length x == lib.lists.length (lib.lists.unique x);
protocols = x: lib.lists.unique (lib.flatten (map builtins.attrNames (lib.attrValues x)));
mkRange = x: lib.lists.range (builtins.elemAt x 0) (builtins.elemAt x 1);
validateList = allowed: builtins.all (x: builtins.elem x allowed);
mkRange = { from, to }: (lib.lists.range from to);
in
{
validatePortAttrset = portmap:
if ! onlyUniqueItemsInList (lib.flatten (map
(x:
if lib.isInt x then x
else if lib.isList x then x
else if lib.isAttrs x then
(
if ! validateList [ "range" ] (builtins.attrNames x) then builtins.abort "found invalid attribute name"
else if x ? "range" then if lib.lists.length x.range == 2 then mkRange x.range else builtins.abort "range needs a list with exactly two items"
else builtins.abort "found invalid attrset"
)
else builtins.abort "found invalid entry in portmap"
)
(filterPort portmap "udp"))) then builtins.abort "Found duplicate ports."
else if ! validateList [ "tcp" "udp" ] (protocols portmap) then builtins.abort "Found invalid protocol."
else portmap;
}
portmap:
if builtins.all
(
proto:
if onlyUniqueItemsInList
(
lib.flatten (
map
(x:
if lib.isInt x then x
else if lib.isList x then x
else if lib.isAttrs x then mkRange x
else builtins.abort "found invalid entry in portmap"
)
(filterPort portmap proto)
)
) then true else builtins.abort "Found duplicate ${proto} ports."
) [ "tcp" "udp" ]
then portmap
else builtins.abort "Found duplicate ports."