From 00f7dd2955d61982c107465d8ff3b01fbdc2e946 Mon Sep 17 00:00:00 2001 From: Sveske_Juice Date: Mon, 7 Apr 2025 16:41:37 +0200 Subject: [PATCH] Assertions for users --- README.md | 11 +++++-- flake.nix | 4 +-- modules/config.nix | 44 ++++++++++++++++++++++++++++ modules/options/users.nix | 4 +-- tests/{ => autorun}/create_users.nix | 6 +++- tests/{ => autorun}/minimal.nix | 2 +- tests/{ => autorun}/xml.nix | 4 +-- tests/example_hash.txt | 1 + 8 files changed, 65 insertions(+), 11 deletions(-) rename tests/{ => autorun}/create_users.nix (88%) rename tests/{ => autorun}/minimal.nix (91%) rename tests/{ => autorun}/xml.nix (98%) create mode 100644 tests/example_hash.txt diff --git a/README.md b/README.md index 586e591..eb77281 100644 --- a/README.md +++ b/README.md @@ -6,19 +6,24 @@ Users, Libraries, Plugins, Settings, etc. # Progress - [x] Creating .xml files -- [ ] Modifying databate +- [x] Modifying databate - [ ] Test cases - [x] XML Generation - [x] Networking - [ ] Encoding - [ ] System - [ ] Users + - [x] Create users + - [ ] Delete users + - [ ] Insert users in middle (test InternalId) + - [ ] MustUpdatePassword test - [ ] Libraries - [ ] Connecting - [ ] Fetching files -- [ ] Users - - [ ] Hashed passwords +- [x] Users + - [x] Hashed passwords + - [ ] Mutable users (override Users table with configured users table if false, like in nixpkgs user-groups.nix) - [ ] Libraries - [ ] Plugins - [ ] Settings diff --git a/flake.nix b/flake.nix index 06e3e24..7250171 100644 --- a/flake.nix +++ b/flake.nix @@ -19,7 +19,7 @@ tests = system: builtins.listToAttrs (builtins.map (x: let - test = import (./tests + "/${x}") { + test = import (./tests/autorun + "/${x}") { pkgs = import nixpkgs {inherit system;}; }; in { @@ -31,7 +31,7 @@ if value == "regular" then name else null)) - (builtins.readDir ./tests)) + (builtins.readDir ./tests/autorun)) )); in { formatter = forAllSystems ( diff --git a/modules/config.nix b/modules/config.nix index c88d441..52998f5 100644 --- a/modules/config.nix +++ b/modules/config.nix @@ -201,5 +201,49 @@ in { '' ); }; + assertions = [ + # NOTE: probably a retarded way of looping over users + # im not a functional mf + + # Make sure that either Password or HashPasswordFile is provided + { + assertion = + lib.lists.findFirst + (x: x == false) + true + ( + map + (user: user.HashedPasswordFile != null || user.Password != null) + cfg.Users + ); + message = "Must Provide either Password or HashedPasswordFile"; + } + # Make sure not both Password and HashPasswordFile is set + { + assertion = + lib.lists.findFirst + (x: x == false) + true + ( + map + (user: !(user.HashedPasswordFile != null && user.Password != null)) + cfg.Users + ); + message = "Can not set both Password and HashedPasswordFile"; + } + # Check if username provided + { + assertion = + lib.lists.findFirst + (x: x == false) + true + ( + map + (user: !(isNull user.Username)) + cfg.Users + ); + message = "Must set a username for user"; + } + ]; }; } diff --git a/modules/options/users.nix b/modules/options/users.nix index 6be56b8..a28b2ab 100644 --- a/modules/options/users.nix +++ b/modules/options/users.nix @@ -89,8 +89,8 @@ with lib; { default = null; }; Password = mkOption { - type = types.str; - default = ""; + type = with types; nullOr str; + default = null; }; HashedPasswordFile = mkOption { type = types.nullOr types.path; diff --git a/tests/create_users.nix b/tests/autorun/create_users.nix similarity index 88% rename from tests/create_users.nix rename to tests/autorun/create_users.nix index 9336f72..8d82281 100644 --- a/tests/create_users.nix +++ b/tests/autorun/create_users.nix @@ -11,7 +11,7 @@ in { ... }: { imports = [ - ../modules/default.nix + ../../modules/default.nix ]; virtualisation.memorySize = 1024; @@ -28,6 +28,10 @@ in { Username = "admin"; Password = "123"; } + { + Username = "other wierd user"; + HashedPasswordFile = ../example_hash.txt; + } ]; }; }; diff --git a/tests/minimal.nix b/tests/autorun/minimal.nix similarity index 91% rename from tests/minimal.nix rename to tests/autorun/minimal.nix index 7fb8420..0ab22eb 100644 --- a/tests/minimal.nix +++ b/tests/autorun/minimal.nix @@ -11,7 +11,7 @@ in { ... }: { imports = [ - ../modules/default.nix + ../../modules/default.nix ]; virtualisation.memorySize = 1024; diff --git a/tests/xml.nix b/tests/autorun/xml.nix similarity index 98% rename from tests/xml.nix rename to tests/autorun/xml.nix index d8c47c6..72f1df0 100644 --- a/tests/xml.nix +++ b/tests/autorun/xml.nix @@ -11,7 +11,7 @@ in { ... }: { imports = [ - ../modules/default.nix + ../../modules/default.nix ]; # assertions = let @@ -37,7 +37,7 @@ in { assertion = expected == got; message = "[Test: ${name}] Generated XML is incorrect!\nExpected \n\n${expected}\n but got \n\n${got}"; }; - toXml = (import ../lib {nixpkgs = pkgs;}).toXMLGeneric; + toXml = (import ../../lib {nixpkgs = pkgs;}).toXMLGeneric; in [ ( genTest "Single tag" diff --git a/tests/example_hash.txt b/tests/example_hash.txt new file mode 100644 index 0000000..a67b007 --- /dev/null +++ b/tests/example_hash.txt @@ -0,0 +1 @@ +$PBKDF2-SHA512$iterations=210000$1268EF06B20D1E0BB64158C39C7C22DA$e6ae3c2a2dac9ee0935636b08c54b69fc20b6fb6e5554d2a35d984abc7c0202b639110811a7ffefa58eedae95c5b0b210a1cfcdda877614ab84b9b914d3b881a