Add comprehensive smoke and feature tests for Gateway API
- Introduced LtdFeatureTests to validate BACnet and OPC UA point updates, trend logging, and alarm rule functionality. - Added OpcUaClientTests to ensure proper OPC UA client configuration, point value synchronization, and session management. - Created OpcUaFeatureTests to test OPC UA server capabilities, including browsing, reading, writing, and subscription notifications. - Implemented native_bacnet_test.py for isolated BACnet wire testing, covering discovery, read/write operations, and command processing. - Added ui-theme-tabs.test.cjs to verify theme switching, tab interactions, and protocol form accessibility in the UI.
This commit is contained in:
1 parent
d9165a60ff
commit
98c48d9d78
80 files changed
+11120
-315
No files matched your search
+164
-19
@@ -6,6 +6,12 @@ let
|
||||
cfg = config.services.seiugw;
|
||||
hostProvisioningRequest = "${cfg.dataDir}/provisioning/host.json";
|
||||
komariInstalledMarker = "${cfg.dataDir}/provisioning/komari-installed";
|
||||
komariRuntimeToken = "/run/seiugw-komari/token";
|
||||
bacnetStackServer = pkgs.callPackage ./bacnet.nix { };
|
||||
manageHostNetwork = config.networking.useDHCP && config.networking.dhcpcd.enable
|
||||
&& !config.networking.useNetworkd && !config.networking.networkmanager.enable;
|
||||
dhcpcdBase = builtins.head (builtins.match ".* --config ([^ ]+).*" config.systemd.services.dhcpcd.serviceConfig.ExecStart);
|
||||
networkHelper = "${pkgs.python3}/bin/python3 ${./host-network.py}";
|
||||
in
|
||||
{
|
||||
options.services.seiugw = {
|
||||
@@ -67,6 +73,12 @@ in
|
||||
description = "Reserved OPC UA TCP port passed to the gateway.";
|
||||
};
|
||||
|
||||
modbusTcpPorts = mkOption {
|
||||
type = types.listOf types.port;
|
||||
default = [ 502 ];
|
||||
description = "Modbus-TCP ports opened by the NixOS firewall. Add every non-standard port used by a server instance.";
|
||||
};
|
||||
|
||||
trends = {
|
||||
enable = mkOption {
|
||||
type = types.bool;
|
||||
@@ -97,7 +109,7 @@ in
|
||||
enrollmentTokenFile = mkOption {
|
||||
type = types.str;
|
||||
default = "${cfg.dataDir}/komari-enrollment-token";
|
||||
description = "Root-readable file with the Komari one-time enrollment token. It must not be stored in Nix or Git.";
|
||||
description = "Root-only import file for a replacement Komari token. Imported into encrypted gateway storage, then removed. Never put plaintext tokens into Nix or Git.";
|
||||
};
|
||||
|
||||
installDir = mkOption {
|
||||
@@ -122,9 +134,11 @@ in
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
users.groups.seiugw = { };
|
||||
users.groups.dialout = { };
|
||||
users.users.seiugw = {
|
||||
isSystemUser = true;
|
||||
group = "seiugw";
|
||||
extraGroups = [ "dialout" ];
|
||||
home = cfg.dataDir;
|
||||
createHome = true;
|
||||
};
|
||||
@@ -132,15 +146,62 @@ in
|
||||
systemd.tmpfiles.rules = [
|
||||
"d ${cfg.dataDir} 0750 seiugw seiugw -"
|
||||
"d ${cfg.dataDir}/provisioning 0750 seiugw seiugw -"
|
||||
] ++ optionals manageHostNetwork [
|
||||
"d /run/seiugw-network 0750 root seiugw -"
|
||||
"d /var/lib/seiugw-network 0700 root root -"
|
||||
] ++ optionals cfg.komari.enable [
|
||||
"d ${cfg.komari.installDir} 0750 root root -"
|
||||
];
|
||||
|
||||
networking.firewall = mkIf cfg.openFirewall {
|
||||
allowedTCPPorts = [ cfg.webPort ];
|
||||
allowedTCPPorts = [ cfg.webPort cfg.opcUa.port ] ++ cfg.modbusTcpPorts;
|
||||
allowedUDPPorts = [ cfg.bacnet.ipPort ];
|
||||
};
|
||||
|
||||
# dhcpcd sees the composed file only in its own mount namespace.
|
||||
# The NixOS base config and options remain the source for unmanaged cards.
|
||||
systemd.services.dhcpcd = mkIf manageHostNetwork {
|
||||
requires = [ "seiugw-network-prepare.service" ];
|
||||
after = [ "seiugw-network-prepare.service" ];
|
||||
serviceConfig.BindReadOnlyPaths = [ "/run/seiugw-network/dhcpcd.conf:${dhcpcdBase}" ];
|
||||
};
|
||||
|
||||
systemd.services.seiugw-network-prepare = mkIf manageHostNetwork {
|
||||
description = "Load confirmed SE-Inno IPv4 profiles";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = "${networkHelper} prepare ${dhcpcdBase} ${pkgs.iproute2}/bin/ip ${pkgs.systemd}/bin/systemctl";
|
||||
};
|
||||
};
|
||||
|
||||
systemd.sockets.seiugw-network = mkIf manageHostNetwork {
|
||||
wantedBy = [ "sockets.target" ];
|
||||
socketConfig = {
|
||||
ListenStream = "/run/seiugw-network/control.sock";
|
||||
SocketMode = "0660";
|
||||
SocketGroup = "seiugw";
|
||||
RemoveOnStop = true;
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services.seiugw-network = mkIf manageHostNetwork {
|
||||
description = "SE-Inno host IPv4 control and rollback";
|
||||
after = [ "seiugw-network-prepare.service" ];
|
||||
requires = [ "seiugw-network-prepare.service" ];
|
||||
serviceConfig = {
|
||||
ExecStart = "${networkHelper} serve ${dhcpcdBase} ${pkgs.iproute2}/bin/ip ${pkgs.systemd}/bin/systemctl";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "1s";
|
||||
UMask = "0077";
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
NoNewPrivileges = true;
|
||||
ReadWritePaths = [ "/run/seiugw-network" "/var/lib/seiugw-network" ];
|
||||
};
|
||||
};
|
||||
|
||||
# Keep MariaDB local. The gateway process receives socket-authenticated access;
|
||||
# no database password or network-exposed database account is created.
|
||||
services.mysql = mkIf cfg.trends.enable {
|
||||
@@ -174,6 +235,7 @@ in
|
||||
environment = {
|
||||
ASPNETCORE_URLS = "http://${cfg.listenAddress}:${toString cfg.webPort}";
|
||||
Gateway__DataDirectory = cfg.dataDir;
|
||||
Gateway__ProtocolDataPlane = "native-bacnet-ip";
|
||||
Gateway__Bacnet__Interface = cfg.bacnet.interface;
|
||||
Gateway__Bacnet__DeviceInstance = toString cfg.bacnet.deviceInstance;
|
||||
Gateway__Bacnet__IpPort = toString cfg.bacnet.ipPort;
|
||||
@@ -188,6 +250,56 @@ in
|
||||
UMask = "0077";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
|
||||
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
|
||||
NoNewPrivileges = true;
|
||||
PrivateTmp = true;
|
||||
ProtectHome = true;
|
||||
ProtectSystem = "strict";
|
||||
ReadWritePaths = [ cfg.dataDir ];
|
||||
};
|
||||
};
|
||||
|
||||
# Native BACnet/IP device. This is a separate, supervised C process from
|
||||
# the upstream BACnet stack; it owns UDP/47808 rather than relying on a
|
||||
# web process or Docker networking. Objects and durable priority slots
|
||||
# are materialised from SQLite by the native bridge.
|
||||
systemd.services.seiugw-bacnet = {
|
||||
description = "SEIU native BACnet/IP device";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
wants = [ "network-online.target" "seiugw.service" ];
|
||||
after = [ "network-online.target" "seiugw.service" ];
|
||||
|
||||
# The web unit is Type=simple: ordering alone does not mean its schema
|
||||
# migration has finished. Wait for the bridge tables on a first upgrade.
|
||||
preStart = ''
|
||||
for attempt in {1..30}; do
|
||||
if [ -f ${escapeShellArg "${cfg.dataDir}/seiugw.db"} ] && \
|
||||
[ "$(${pkgs.sqlite}/bin/sqlite3 ${escapeShellArg "${cfg.dataDir}/seiugw.db"} "SELECT (SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name IN ('bacnet_commands','bacnet_properties','bacnet_observations','bacnet_runtime','bacnet_discovery','bacnet_discovered_devices'))+(SELECT COUNT(*) FROM pragma_table_info('bacnet_discovered_devices') WHERE name IN ('network','mac'))+(SELECT COUNT(*) FROM pragma_table_info('points') WHERE name='deleted_at_utc');" 2>/dev/null)" = 9 ]; then
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
echo "Gateway database schema is not ready" >&2
|
||||
exit 1
|
||||
'';
|
||||
|
||||
environment = {
|
||||
BACNET_DATALINK = "bip";
|
||||
SEIUGW_DATABASE = "${cfg.dataDir}/seiugw.db";
|
||||
BACNET_IFACE = cfg.bacnet.interface;
|
||||
BACNET_IP_PORT = toString cfg.bacnet.ipPort;
|
||||
BACNET_IP_BROADCAST_PORT = toString cfg.bacnet.ipPort;
|
||||
};
|
||||
|
||||
serviceConfig = {
|
||||
ExecStart = "${bacnetStackServer}/bin/bacserv ${toString cfg.bacnet.deviceInstance} SEIU-Gateway";
|
||||
User = "seiugw";
|
||||
Group = "seiugw";
|
||||
WorkingDirectory = cfg.dataDir;
|
||||
UMask = "0077";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
NoNewPrivileges = true;
|
||||
PrivateTmp = true;
|
||||
ProtectHome = true;
|
||||
@@ -202,9 +314,12 @@ in
|
||||
# re-applied after every later boot without changing NixOS configuration.
|
||||
systemd.services.seiugw-apply-hostname = {
|
||||
description = "Apply SEIU Gateway commissioning hostname";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
serviceConfig.Type = "oneshot";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
};
|
||||
path = [ pkgs.coreutils pkgs.gnugrep pkgs.hostname pkgs.jq ];
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
@@ -225,6 +340,7 @@ in
|
||||
|
||||
host_name="SE-UGW-$project-ISP$(printf '%02d' "$isp")"
|
||||
[ "''${#host_name}" -le 63 ] || exit 0
|
||||
[ "$(${pkgs.hostname}/bin/hostname)" != "$host_name" ] || exit 0
|
||||
${pkgs.hostname}/bin/hostname "$host_name"
|
||||
'';
|
||||
};
|
||||
@@ -232,19 +348,57 @@ in
|
||||
systemd.paths.seiugw-apply-hostname = {
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
pathConfig = {
|
||||
PathExists = hostProvisioningRequest;
|
||||
PathChanged = hostProvisioningRequest;
|
||||
Unit = "seiugw-apply-hostname.service";
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services.seiugw-komari-token = mkIf cfg.komari.enable {
|
||||
description = "Import and materialize encrypted Komari credential";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "seiugw.service" ];
|
||||
requires = [ "seiugw.service" ];
|
||||
environment.Gateway__DataDirectory = cfg.dataDir;
|
||||
serviceConfig = { Type = "oneshot"; UMask = "0077"; Restart = "on-failure"; RestartSec = "10s"; };
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
${pkgs.coreutils}/bin/install -d -m 0700 -o seiugw -g seiugw /run/seiugw-komari
|
||||
# Stage root's input for the unprivileged database owner. Never print it.
|
||||
if [ -s ${escapeShellArg cfg.komari.enrollmentTokenFile} ]; then
|
||||
${pkgs.coreutils}/bin/install -m 0600 -o seiugw -g seiugw ${escapeShellArg cfg.komari.enrollmentTokenFile} /run/seiugw-komari/import
|
||||
${pkgs.util-linux}/bin/runuser -u seiugw -- ${cfg.package}/bin/Gateway.Api --import-komari-token /run/seiugw-komari/import
|
||||
${pkgs.coreutils}/bin/rm -f /run/seiugw-komari/import ${escapeShellArg cfg.komari.enrollmentTokenFile}
|
||||
fi
|
||||
if ${pkgs.util-linux}/bin/runuser -u seiugw -- ${cfg.package}/bin/Gateway.Api --materialize-komari-token /run/seiugw-komari/next; then
|
||||
if ! ${pkgs.diffutils}/bin/cmp -s /run/seiugw-komari/next ${komariRuntimeToken}; then
|
||||
${pkgs.coreutils}/bin/mv /run/seiugw-komari/next ${komariRuntimeToken}
|
||||
${pkgs.systemd}/bin/systemctl --no-block try-restart seiugw-komari-agent.service
|
||||
${pkgs.systemd}/bin/systemctl --no-block restart seiugw-komari-bootstrap.service
|
||||
else
|
||||
${pkgs.coreutils}/bin/rm -f /run/seiugw-komari/next
|
||||
fi
|
||||
fi
|
||||
'';
|
||||
};
|
||||
systemd.paths.seiugw-komari-token = mkIf cfg.komari.enable {
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
pathConfig = {
|
||||
PathChanged = [ "${cfg.dataDir}/provisioning/komari-token.request" cfg.komari.enrollmentTokenFile ];
|
||||
Unit = "seiugw-komari-token.service";
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services.seiugw-komari-bootstrap = mkIf cfg.komari.enable {
|
||||
description = "One-time Komari agent installer for SEIU Gateway";
|
||||
after = [ "network-online.target" "seiugw-apply-hostname.service" ];
|
||||
requires = [ "seiugw-apply-hostname.service" ];
|
||||
after = [ "network-online.target" "seiugw-apply-hostname.service" "seiugw-komari-token.service" ];
|
||||
requires = [ "seiugw-apply-hostname.service" "seiugw-komari-token.service" ];
|
||||
wants = [ "network-online.target" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
# Both prerequisites are already watched by the commissioning path.
|
||||
# A completed installer must remain active; otherwise PathExists can
|
||||
# repeatedly start this successful no-op and hit systemd's start limit.
|
||||
RemainAfterExit = true;
|
||||
Restart = "on-failure";
|
||||
RestartSec = "30s";
|
||||
};
|
||||
@@ -252,7 +406,7 @@ in
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
marker=${escapeShellArg komariInstalledMarker}
|
||||
token_file=${escapeShellArg cfg.komari.enrollmentTokenFile}
|
||||
token_file=${escapeShellArg komariRuntimeToken}
|
||||
install_dir=${escapeShellArg cfg.komari.installDir}
|
||||
agent="$install_dir/agent"
|
||||
[ -e "$marker" ] && exit 0
|
||||
@@ -281,23 +435,14 @@ in
|
||||
};
|
||||
};
|
||||
|
||||
systemd.paths.seiugw-komari-token = mkIf cfg.komari.enable {
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
pathConfig = {
|
||||
PathExists = cfg.komari.enrollmentTokenFile;
|
||||
PathChanged = cfg.komari.enrollmentTokenFile;
|
||||
Unit = "seiugw-komari-bootstrap.service";
|
||||
};
|
||||
};
|
||||
|
||||
# The upstream installer deliberately does not create a NixOS unit. NixOS
|
||||
# owns this unit instead, while the binary remains in the upstream mutable
|
||||
# install directory. The enrollment token is read at service start and is
|
||||
# never placed in the Nix store.
|
||||
systemd.services.seiugw-komari-agent = mkIf cfg.komari.enable {
|
||||
description = "Komari monitoring agent";
|
||||
after = [ "network-online.target" "seiugw-apply-hostname.service" ];
|
||||
wants = [ "network-online.target" ];
|
||||
after = [ "network-online.target" "seiugw-apply-hostname.service" "seiugw-komari-token.service" ];
|
||||
wants = [ "network-online.target" "seiugw-komari-token.service" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
unitConfig.ConditionPathExists = "${cfg.komari.installDir}/agent";
|
||||
serviceConfig = {
|
||||
@@ -309,7 +454,7 @@ in
|
||||
path = [ pkgs.bash pkgs.coreutils ];
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
token="$(${pkgs.coreutils}/bin/cat ${escapeShellArg cfg.komari.enrollmentTokenFile})"
|
||||
token="$(${pkgs.coreutils}/bin/cat ${escapeShellArg komariRuntimeToken})"
|
||||
[ -n "$token" ]
|
||||
exec ${escapeShellArg "${cfg.komari.installDir}/agent"} -e ${escapeShellArg cfg.komari.endpoint} --auto-discovery "$token" ${lib.optionalString cfg.komari.ignoreUnsafeCertificateErrors "--ignore-unsafe-cert"}
|
||||
'';
|
||||
|
||||
Reference in new issue
Block a user