You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
6.8 KiB
Nix

1 year ago
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
{lib, ...}: v1_acl:
Squashed commit of the following: commit 014fa13262f264d61de6ae5cd49b67bc02c25bd0 Author: Tilmann Meyer <me@atiltedtree.dev> Date: Sun Mar 24 23:09:00 2024 +0100 Fixed persistent keepalive for networkd configurer commit 37453982ab4c65848c9849aec54806b829072e91 Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:08:06 2024 -0400 update flake commit 68a0496bc7fa0770427d22bd25c6910b9144e9ab Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:07:59 2024 -0400 Update tests to use `subnets` in all connections commit 8c7f741b7f0549387abd96482010bc06b731a55c Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:07:31 2024 -0400 Add error message to parser for incorrectly configured subnets in connections commit 3e3a37fc0f5b232da4c4744d81981d68ccd93ffc Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:05:27 2024 -0400 Update disjoint test commit 1236e4e8f27c22e3e9d936dbbdcb1913b26b8c9c Author: Adam Stephens <adam@valkor.net> Date: Sun Mar 24 20:59:26 2024 -0400 Add disjointed meshes tests commit dadd5bf7205eddb0a17330426bcabdccd6923615 Author: Matthew Salerno <m@salernosection.com> Date: Wed Dec 13 22:01:16 2023 -0500 add tags to agenix-rekey provider commit 1d3184639a5a83f0633f2908cd3b66293a24d1d8 Author: Matthew Salerno <m@salernosection.com> Date: Mon Dec 11 22:09:45 2023 -0500 sneaky systemd issues with dev only commit c8fb5affe5802c133abe10a51b228bfd76e03147 Author: Matthew Salerno <m@salernosection.com> Date: Tue Dec 5 20:51:30 2023 -0500 wnlib is back commit 1e697eb859788c592a5e3606086180b72fd1e9f1 Author: Matthew Salerno <m@salernosection.com> Date: Sat Nov 11 22:09:26 2023 -0500 allow multiple devs for same subnet commit 45b70c90636eee57d49ebac348e345912be226da Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 18 16:04:49 2023 -0400 added manual ip tests and resulting fixes commit 3d49ebff29894fe58de97a07a02035bd6d9a2752 Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 18 11:49:58 2023 -0400 Added manual IP assignment tests commit bd52d85d2dc56b0933d958aa3ef4faa3113e356c Author: Matthew Salerno <m@salernosection.com> Date: Thu Sep 14 16:08:00 2023 -0400 Generalized ip assignment to take cidr or IP commit fd2b9ce77cf1492463413e8bc0a9797226f9ddd0 Author: Matthew Salerno <m@salernosection.com> Date: Thu Sep 14 13:50:11 2023 -0400 Generalized ip assignment to take cidr or IP commit 57f8e0e97463dec3e0da7f4b995ee89b92d36687 Author: Matthew Salerno <m@salernosection.com> Date: Wed Sep 13 18:38:42 2023 -0400 Fixed manual ipv4 assignment issue commit a24fffa753745d1d6285ae588ff29b374ea1f8d2 Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 11 13:44:11 2023 -0400 Update README.md with link to self commit dd9de47a84060004ec9c76ceb7e7ee4086438f73 Author: Matthew Salerno <m@salernosection.com> Date: Sat Sep 2 19:55:17 2023 -0400 fixed missing link commit e761330e9189d194018ec2e3bd314d015b3f5f38 Merge: bb8636d b658653 Author: Matthew Salerno <m@salernosection.com> Date: Sat Sep 2 19:54:29 2023 -0400 fixed missing link commit bb8636dd8d5841747cdb7e8da171aeb44534e4a6 Author: Matthew Salerno <m@salernosection.com> Date: Thu Aug 31 20:45:14 2023 -0400 Readme moved to wiki commit 86e300428b905843129d2785d01fdf4313334b93 Author: Matthew Salerno <m@salernosection.com> Date: Tue Aug 22 21:07:34 2023 -0400 Fixed a bug in allGroupEndpoints logic commit 9a5c773355c571b6dc005dd70b083775b00f106d Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 22:03:19 2023 -0400 oops, delete net.nix commit b97760e456dc471e33bab3844ab9d3dbd5cc5a73 Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:59:30 2023 -0400 fixup additional_ settings commit 753c0726630304113e5a6af75d97b4fcf0b45a90 Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:30:40 2023 -0400 change null test to test enable = false commit 690e13e902ef6515946baf882a470f00b54adefa Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:28:29 2023 -0400 add mailing list to readme
7 months ago
let wnlib = import ../lib.nix {inherit lib;}; in
with wnlib;
with lib;
let
/** parsePeer :: acl_peer -> ic_peer */
parsePeer = acl_peer: {
1 year ago
subnetConnections = listOfSetsToSetByKey "name" (pipeMap [subnetFromName (getSubnetConnectionAndName acl_peer)] (attrNames acl_peer.subnets));
}
// mergeIf acl_peer "extraArgs"
// mergeIf acl_peer "publicKey"
// mergeIf acl_peer "privateKeyFile"
1 year ago
// mergeIf acl_peer "privateKey"
// (if acl_peer ? groups then {groups = map groupFromName acl_peer.groups;} else {groups = {};});
/** parseGroup :: acl_group -> ic_group */
parseGroup = acl_group: {
1 year ago
peers = mapListOfSetsToSetByKey "name" parsePeer (selectPeers [{type="group"; rule="is"; value="${acl_group.name}";}]);
}
// mergeIf acl_group "extraArgs";
/** parseSubnet :: acl_subnet -> ic_subnet */
parseSubnet = acl_subnet: {
1 year ago
peers = mapListOfSetsToSetByKey "name" parsePeer (selectPeers [{type="subnet"; rule="is"; value="${acl_subnet.name}";}]);
}
// mergeIf acl_subnet "extraArgs"
// mergeIf acl_subnet "presharedKeyFile";
/** getSubnetConnection :: acl_peer -> acl_subnet -> (subnetConnection // {name}) */
getSubnetConnectionAndName = acl_peer: acl_subnet: {
name = acl_subnet.name; # name gets removed shortly after, name is not in the actual subnetConnection object
subnet = parseSubnet acl_subnet;
ipAddresses = getIpAddresses acl_subnet acl_peer;
listenPort = acl_peer.subnets."${acl_subnet.name}".listenPort;
peerConnections = getPeerConnections acl_peer acl_subnet;
}
// mergeIf (getAttr acl_subnet.name acl_peer.subnets) "extraArgs";
/** getIpAddresses :: acl_peer -> acl_subnet -> [str] */
getIpAddresses = acl_subnet: acl_peer:
Squashed commit of the following: commit 014fa13262f264d61de6ae5cd49b67bc02c25bd0 Author: Tilmann Meyer <me@atiltedtree.dev> Date: Sun Mar 24 23:09:00 2024 +0100 Fixed persistent keepalive for networkd configurer commit 37453982ab4c65848c9849aec54806b829072e91 Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:08:06 2024 -0400 update flake commit 68a0496bc7fa0770427d22bd25c6910b9144e9ab Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:07:59 2024 -0400 Update tests to use `subnets` in all connections commit 8c7f741b7f0549387abd96482010bc06b731a55c Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:07:31 2024 -0400 Add error message to parser for incorrectly configured subnets in connections commit 3e3a37fc0f5b232da4c4744d81981d68ccd93ffc Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:05:27 2024 -0400 Update disjoint test commit 1236e4e8f27c22e3e9d936dbbdcb1913b26b8c9c Author: Adam Stephens <adam@valkor.net> Date: Sun Mar 24 20:59:26 2024 -0400 Add disjointed meshes tests commit dadd5bf7205eddb0a17330426bcabdccd6923615 Author: Matthew Salerno <m@salernosection.com> Date: Wed Dec 13 22:01:16 2023 -0500 add tags to agenix-rekey provider commit 1d3184639a5a83f0633f2908cd3b66293a24d1d8 Author: Matthew Salerno <m@salernosection.com> Date: Mon Dec 11 22:09:45 2023 -0500 sneaky systemd issues with dev only commit c8fb5affe5802c133abe10a51b228bfd76e03147 Author: Matthew Salerno <m@salernosection.com> Date: Tue Dec 5 20:51:30 2023 -0500 wnlib is back commit 1e697eb859788c592a5e3606086180b72fd1e9f1 Author: Matthew Salerno <m@salernosection.com> Date: Sat Nov 11 22:09:26 2023 -0500 allow multiple devs for same subnet commit 45b70c90636eee57d49ebac348e345912be226da Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 18 16:04:49 2023 -0400 added manual ip tests and resulting fixes commit 3d49ebff29894fe58de97a07a02035bd6d9a2752 Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 18 11:49:58 2023 -0400 Added manual IP assignment tests commit bd52d85d2dc56b0933d958aa3ef4faa3113e356c Author: Matthew Salerno <m@salernosection.com> Date: Thu Sep 14 16:08:00 2023 -0400 Generalized ip assignment to take cidr or IP commit fd2b9ce77cf1492463413e8bc0a9797226f9ddd0 Author: Matthew Salerno <m@salernosection.com> Date: Thu Sep 14 13:50:11 2023 -0400 Generalized ip assignment to take cidr or IP commit 57f8e0e97463dec3e0da7f4b995ee89b92d36687 Author: Matthew Salerno <m@salernosection.com> Date: Wed Sep 13 18:38:42 2023 -0400 Fixed manual ipv4 assignment issue commit a24fffa753745d1d6285ae588ff29b374ea1f8d2 Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 11 13:44:11 2023 -0400 Update README.md with link to self commit dd9de47a84060004ec9c76ceb7e7ee4086438f73 Author: Matthew Salerno <m@salernosection.com> Date: Sat Sep 2 19:55:17 2023 -0400 fixed missing link commit e761330e9189d194018ec2e3bd314d015b3f5f38 Merge: bb8636d b658653 Author: Matthew Salerno <m@salernosection.com> Date: Sat Sep 2 19:54:29 2023 -0400 fixed missing link commit bb8636dd8d5841747cdb7e8da171aeb44534e4a6 Author: Matthew Salerno <m@salernosection.com> Date: Thu Aug 31 20:45:14 2023 -0400 Readme moved to wiki commit 86e300428b905843129d2785d01fdf4313334b93 Author: Matthew Salerno <m@salernosection.com> Date: Tue Aug 22 21:07:34 2023 -0400 Fixed a bug in allGroupEndpoints logic commit 9a5c773355c571b6dc005dd70b083775b00f106d Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 22:03:19 2023 -0400 oops, delete net.nix commit b97760e456dc471e33bab3844ab9d3dbd5cc5a73 Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:59:30 2023 -0400 fixup additional_ settings commit 753c0726630304113e5a6af75d97b4fcf0b45a90 Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:30:40 2023 -0400 change null test to test enable = false commit 690e13e902ef6515946baf882a470f00b54adefa Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:28:29 2023 -0400 add mailing list to readme
7 months ago
lib.throwIfNot (builtins.hasAttr acl_subnet.name acl_peer.subnets) "Tried getting the IP address for ${acl_peer.name} on subnet ${acl_subnet.name}. However, ${acl_peer.name} is not a member of ${acl_subnet.name}. It may be that you forgot to add ${acl_subnet.name} to `connections.*.subnets` in your ACL." (
if (acl_peer.subnets."${acl_subnet.name}" ? ipAddresses) then (
if (elem "auto" acl_peer.subnets."${acl_subnet.name}".ipAddresses) then (
(remove "auto" acl_peer.subnets."${acl_subnet.name}".ipAddresses) ++ (singleton (generateIPv6Address acl_subnet.name acl_peer.name))
) else acl_peer.subnets."${acl_subnet.name}".ipAddresses
) else (singleton (generateIPv6Address acl_subnet.name acl_peer.name))
);
/** getPeerConnections :: acl_peer -> acl_subnet -> str -> peerConnection */
getPeerConnections = acl_peerFrom: acl_subnet:
let
1 year ago
filterSubnets = connection: !(connection ? subnets) || elem acl_subnet.name connection.subnets;
filterPeer = key: acl_peer: connection: elem acl_peer.name (catAttrs "name" (selectPeers connection."${key}"));
getConnectionsX = key: filter (connection: all (x: x connection) [filterSubnets (filterPeer key acl_peerFrom)]) v1_acl.connections;
getConnectionsA = getConnectionsX "a";
getConnectionsB = getConnectionsX "b";
allPeers = unique ((concatMap (connection: selectPeers connection.b) getConnectionsA) ++ (concatMap (connection: selectPeers connection.a) getConnectionsB));
allOtherPeers = remove acl_peerFrom allPeers;
getExtraArgs = acl_peerTo:
let
connections = (filter (filterPeer "a" acl_peerTo) getConnectionsB) ++ (filter (filterPeer "b" acl_peerTo) getConnectionsA);
extraArgsList = catAttrs "extraArgs" connections;
in
foldl' mergeAttrs {} extraArgsList;
in
listOfSetsToSetByKey "name" (map (acl_peerTo:
1 year ago
let
extraArgs = getExtraArgs acl_peerTo;
in
{
name = acl_peerTo.name;
peer = parsePeer acl_peerTo;
ipAddresses = getIpAddresses acl_subnet acl_peerTo;
1 year ago
endpoint = getEndpoint acl_subnet acl_peerFrom acl_peerTo;
} // (if extraArgs == {} then {} else {inherit extraArgs;})
) allOtherPeers);
/** getEndpoint :: acl_peer -> acl_peer -> ic_endpoint */
1 year ago
getEndpoint = acl_subnet: acl_peerFrom: acl_peerTo:
let
peersForEndpoint = endpoint: catAttrs "name" (selectPeers (attrByPath ["match"] [] endpoint));
1 year ago
allPeerEndpoints = if acl_peerTo ? endpoints then
(filter (endpoint: elem acl_peerFrom.name (peersForEndpoint endpoint)) acl_peerTo.endpoints)
else [];
allGroupEndpoints = concatMap (acl_group: attrByPath ["endpoints"] [] (groupFromName acl_group)) (intersectLists
(attrByPath ["groups"] [] acl_peerTo)
(attrByPath ["groups"] [] acl_peerFrom)
1 year ago
);
allSubnetEndpoints = acl_subnet.endpoints;
allEndpointMatches = allSubnetEndpoints ++ allGroupEndpoints ++ allPeerEndpoints;
in
1 year ago
removeAttrs (foldl' mergeAttrs {} allEndpointMatches) [ "match" ];
/** selectPeers :: [acl_filters] -> str -> [acl_peer]
* (str -> ic_peer) means it returns an attrset of peers keyed by name, typescript syntax:
* selectPeers(acl: acl, acl_filters: acl_filter[]): {[peerName: string]: ic_peer};
*/
selectPeers = acl_filters:
if length acl_filters == 0
then
v1_acl.peers
else
1 year ago
foldl' intersectLists (selectPeersSingleFilter (head acl_filters)) (map selectPeersSingleFilter acl_filters);
/** selectPeersSingleFilter :: acl_filter -> [acl_peer] */
selectPeersSingleFilter = acl_filter:
with acl_filter;
let
applyRule = comparison: if rule == "is" then comparison else if rule == "not" then !comparison else throw ("Unknown filter rule " + rule);
in
if type == "peer" then
(filter (acl_peer: applyRule (acl_peer.name == value)) v1_acl.peers)
else if type == "group" then
(filter (acl_peer: applyRule (elem value acl_peer.groups)) v1_acl.peers)
else if type == "subnet" then
(filter (acl_peer: applyRule (elem value (attrNames acl_peer.subnets))) v1_acl.peers)
else throw ("Unknown filter type " + type);
groupFromName = groupName: findSingle
(group: group.name == groupName)
(throw "No group " + groupName)
(throw "Multiply defined group " + groupName)
v1_acl.groups;
subnetFromName = subnetName: findSingle
(subnet: subnet.name == subnetName)
Squashed commit of the following: commit 014fa13262f264d61de6ae5cd49b67bc02c25bd0 Author: Tilmann Meyer <me@atiltedtree.dev> Date: Sun Mar 24 23:09:00 2024 +0100 Fixed persistent keepalive for networkd configurer commit 37453982ab4c65848c9849aec54806b829072e91 Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:08:06 2024 -0400 update flake commit 68a0496bc7fa0770427d22bd25c6910b9144e9ab Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:07:59 2024 -0400 Update tests to use `subnets` in all connections commit 8c7f741b7f0549387abd96482010bc06b731a55c Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:07:31 2024 -0400 Add error message to parser for incorrectly configured subnets in connections commit 3e3a37fc0f5b232da4c4744d81981d68ccd93ffc Author: Matthew Salerno <m@salernosection.com> Date: Sun Mar 24 21:05:27 2024 -0400 Update disjoint test commit 1236e4e8f27c22e3e9d936dbbdcb1913b26b8c9c Author: Adam Stephens <adam@valkor.net> Date: Sun Mar 24 20:59:26 2024 -0400 Add disjointed meshes tests commit dadd5bf7205eddb0a17330426bcabdccd6923615 Author: Matthew Salerno <m@salernosection.com> Date: Wed Dec 13 22:01:16 2023 -0500 add tags to agenix-rekey provider commit 1d3184639a5a83f0633f2908cd3b66293a24d1d8 Author: Matthew Salerno <m@salernosection.com> Date: Mon Dec 11 22:09:45 2023 -0500 sneaky systemd issues with dev only commit c8fb5affe5802c133abe10a51b228bfd76e03147 Author: Matthew Salerno <m@salernosection.com> Date: Tue Dec 5 20:51:30 2023 -0500 wnlib is back commit 1e697eb859788c592a5e3606086180b72fd1e9f1 Author: Matthew Salerno <m@salernosection.com> Date: Sat Nov 11 22:09:26 2023 -0500 allow multiple devs for same subnet commit 45b70c90636eee57d49ebac348e345912be226da Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 18 16:04:49 2023 -0400 added manual ip tests and resulting fixes commit 3d49ebff29894fe58de97a07a02035bd6d9a2752 Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 18 11:49:58 2023 -0400 Added manual IP assignment tests commit bd52d85d2dc56b0933d958aa3ef4faa3113e356c Author: Matthew Salerno <m@salernosection.com> Date: Thu Sep 14 16:08:00 2023 -0400 Generalized ip assignment to take cidr or IP commit fd2b9ce77cf1492463413e8bc0a9797226f9ddd0 Author: Matthew Salerno <m@salernosection.com> Date: Thu Sep 14 13:50:11 2023 -0400 Generalized ip assignment to take cidr or IP commit 57f8e0e97463dec3e0da7f4b995ee89b92d36687 Author: Matthew Salerno <m@salernosection.com> Date: Wed Sep 13 18:38:42 2023 -0400 Fixed manual ipv4 assignment issue commit a24fffa753745d1d6285ae588ff29b374ea1f8d2 Author: Matthew Salerno <m@salernosection.com> Date: Mon Sep 11 13:44:11 2023 -0400 Update README.md with link to self commit dd9de47a84060004ec9c76ceb7e7ee4086438f73 Author: Matthew Salerno <m@salernosection.com> Date: Sat Sep 2 19:55:17 2023 -0400 fixed missing link commit e761330e9189d194018ec2e3bd314d015b3f5f38 Merge: bb8636d b658653 Author: Matthew Salerno <m@salernosection.com> Date: Sat Sep 2 19:54:29 2023 -0400 fixed missing link commit bb8636dd8d5841747cdb7e8da171aeb44534e4a6 Author: Matthew Salerno <m@salernosection.com> Date: Thu Aug 31 20:45:14 2023 -0400 Readme moved to wiki commit 86e300428b905843129d2785d01fdf4313334b93 Author: Matthew Salerno <m@salernosection.com> Date: Tue Aug 22 21:07:34 2023 -0400 Fixed a bug in allGroupEndpoints logic commit 9a5c773355c571b6dc005dd70b083775b00f106d Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 22:03:19 2023 -0400 oops, delete net.nix commit b97760e456dc471e33bab3844ab9d3dbd5cc5a73 Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:59:30 2023 -0400 fixup additional_ settings commit 753c0726630304113e5a6af75d97b4fcf0b45a90 Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:30:40 2023 -0400 change null test to test enable = false commit 690e13e902ef6515946baf882a470f00b54adefa Author: Matthew Salerno <m@salernosection.com> Date: Mon Aug 21 21:28:29 2023 -0400 add mailing list to readme
7 months ago
(throw "No subnet " + subnetName + " when processing peer " + acl_peer)
(throw "Multiply defined subnet " + subnetName + " when processing peer " + acl_peer)
v1_acl.subnets;
in
{
1 year ago
peers = mapListOfSetsToSetByKey "name" parsePeer v1_acl.peers;
subnets = mapListOfSetsToSetByKey "name" parseSubnet v1_acl.subnets;
groups = mapListOfSetsToSetByKey "name" parseGroup v1_acl.groups;
}
// mergeIf v1_acl "extraArgs"