2021-04-26 01:17:48 +01:00
|
|
|
import ./make-test-python.nix ({ pkgs, lib, ... }:
|
2018-01-26 00:41:36 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
configDir = "/var/lib/foobar";
|
|
|
|
in {
|
|
|
|
name = "home-assistant";
|
2021-04-26 01:17:48 +01:00
|
|
|
meta.maintainers = lib.teams.home-assistant.members;
|
2018-01-26 00:41:36 +00:00
|
|
|
|
2020-06-20 11:05:30 +01:00
|
|
|
nodes.hass = { pkgs, ... }: {
|
2022-02-01 20:03:51 +00:00
|
|
|
services.postgresql = {
|
|
|
|
enable = true;
|
nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15
Closes #216989
First of all, a bit of context: in PostgreSQL, newly created users don't
have the CREATE privilege on the public schema of a database even with
`ALL PRIVILEGES` granted via `ensurePermissions` which is how most of
the DB users are currently set up "declaratively"[1]. This means e.g. a
freshly deployed Nextcloud service will break early because Nextcloud
itself cannot CREATE any tables in the public schema anymore.
The other issue here is that `ensurePermissions` is a mere hack. It's
effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how
a value is substituted in a query. You'd have to parse a subset of SQL
to actually know which object are permissions granted to for a user).
After analyzing the existing modules I realized that in every case with
a single exception[2] the UNIX system user is equal to the db user is
equal to the db name and I don't see a compelling reason why people
would change that in 99% of the cases. In fact, some modules would even
break if you'd change that because the declarations of the system user &
the db user are mixed up[3].
So I decided to go with something new which restricts the ways to use
`ensure*` options rather than expanding those[4]. Effectively this means
that
* The DB user _must_ be equal to the DB name.
* Permissions are granted via `ensureDBOwnerhip` for an attribute-set in
`ensureUsers`. That way, the user is actually the owner and can
perform `CREATE`.
* For such a postgres user, a database must be declared in
`ensureDatabases`.
For anything else, a custom state management should be implemented. This
can either be `initialScript`, doing it manual, outside of the module or
by implementing proper state management for postgresql[5], but the
current state of `ensure*` isn't even declarative, but a convergent tool
which is what Nix actually claims to _not_ do.
Regarding existing setups: there are effectively two options:
* Leave everything as-is (assuming that system user == db user == db
name): then the DB user will automatically become the DB owner and
everything else stays the same.
* Drop the `createDatabase = true;` declarations: nothing will change
because a removal of `ensure*` statements is ignored, so it doesn't
matter at all whether this option is kept after the first deploy (and
later on you'd usually restore from backups anyways).
The DB user isn't the owner of the DB then, but for an existing setup
this is irrelevant because CREATE on the public schema isn't revoked
from existing users (only not granted for new users).
[1] not really declarative though because removals of these statements
are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467
[2] `services.invidious`: I removed the `ensure*` part temporarily
because it IMHO falls into the category "manage the state on your
own" (see the commit message). See also
https://github.com/NixOS/nixpkgs/pull/265857
[3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";`
[4] As opposed to other changes that are considered a potential fix, but
also add more things like collation for DBs or passwords that are
_never_ touched again when changing those.
[5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-08 11:50:09 +00:00
|
|
|
ensureDatabases = [ "hass" ];
|
|
|
|
ensureUsers = [{
|
|
|
|
name = "hass";
|
|
|
|
ensureDBOwnership = true;
|
|
|
|
}];
|
2022-02-01 20:03:51 +00:00
|
|
|
};
|
|
|
|
|
2020-06-20 11:05:30 +01:00
|
|
|
services.home-assistant = {
|
|
|
|
enable = true;
|
2022-01-30 01:29:45 +00:00
|
|
|
inherit configDir;
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
# provide dependencies through package overrides
|
2021-12-01 00:19:01 +00:00
|
|
|
package = (pkgs.home-assistant.override {
|
2022-02-14 01:10:24 +00:00
|
|
|
extraPackages = ps: with ps; [
|
|
|
|
colorama
|
|
|
|
];
|
2023-02-20 15:43:25 +00:00
|
|
|
extraComponents = [
|
|
|
|
# test char-tty device allow propagation into the service
|
|
|
|
"zha"
|
|
|
|
];
|
2021-12-01 00:19:01 +00:00
|
|
|
});
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
# provide component dependencies explicitly from the module
|
2022-01-30 01:30:51 +00:00
|
|
|
extraComponents = [
|
2023-02-20 15:43:25 +00:00
|
|
|
"mqtt"
|
2022-01-30 01:30:51 +00:00
|
|
|
];
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
# provide package for postgresql support
|
2022-02-01 20:03:51 +00:00
|
|
|
extraPackages = python3Packages: with python3Packages; [
|
|
|
|
psycopg2
|
|
|
|
];
|
|
|
|
|
2022-02-01 02:10:44 +00:00
|
|
|
# test loading custom components
|
|
|
|
customComponents = with pkgs.home-assistant-custom-components; [
|
2023-11-25 15:32:41 +00:00
|
|
|
prometheus_sensor
|
2022-02-01 02:10:44 +00:00
|
|
|
];
|
|
|
|
|
2022-01-31 01:32:39 +00:00
|
|
|
# test loading lovelace modules
|
|
|
|
customLovelaceModules = with pkgs.home-assistant-custom-lovelace-modules; [
|
|
|
|
mini-graph-card
|
|
|
|
];
|
|
|
|
|
2020-06-20 11:05:30 +01:00
|
|
|
config = {
|
|
|
|
homeassistant = {
|
|
|
|
name = "Home";
|
|
|
|
time_zone = "UTC";
|
|
|
|
latitude = "0.0";
|
|
|
|
longitude = "0.0";
|
|
|
|
elevation = 0;
|
2018-01-26 00:41:36 +00:00
|
|
|
};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2022-02-01 20:03:51 +00:00
|
|
|
# configure the recorder component to use the postgresql db
|
|
|
|
recorder.db_url = "postgresql://@/hass";
|
|
|
|
|
2022-01-30 01:29:45 +00:00
|
|
|
# we can't load default_config, because the updater requires
|
|
|
|
# network access and would cause an error, so load frontend
|
|
|
|
# here explicitly.
|
|
|
|
# https://www.home-assistant.io/integrations/frontend/
|
2020-06-20 11:05:30 +01:00
|
|
|
frontend = {};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2023-04-28 20:36:30 +01:00
|
|
|
# include some popular integrations, that absolutely shouldn't break
|
|
|
|
knx = {};
|
|
|
|
shelly = {};
|
|
|
|
zha = {};
|
|
|
|
|
2022-01-30 01:29:45 +00:00
|
|
|
# set up a wake-on-lan switch to test capset capability required
|
|
|
|
# for the ping suid wrapper
|
|
|
|
# https://www.home-assistant.io/integrations/wake_on_lan/
|
|
|
|
switch = [ {
|
2022-01-25 17:30:43 +00:00
|
|
|
platform = "wake_on_lan";
|
|
|
|
mac = "00:11:22:33:44:55";
|
|
|
|
host = "127.0.0.1";
|
2022-01-30 01:29:45 +00:00
|
|
|
} ];
|
|
|
|
|
|
|
|
# test component-based capability assignment (CAP_NET_BIND_SERVICE)
|
|
|
|
# https://www.home-assistant.io/integrations/emulated_hue/
|
2021-04-25 13:44:51 +01:00
|
|
|
emulated_hue = {
|
|
|
|
host_ip = "127.0.0.1";
|
|
|
|
listen_port = 80;
|
|
|
|
};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
|
|
|
# https://www.home-assistant.io/integrations/logger/
|
2020-06-20 11:05:30 +01:00
|
|
|
logger = {
|
|
|
|
default = "info";
|
|
|
|
};
|
|
|
|
};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
|
|
|
# configure the sample lovelace dashboard
|
2020-06-20 11:05:30 +01:00
|
|
|
lovelaceConfig = {
|
|
|
|
title = "My Awesome Home";
|
|
|
|
views = [{
|
|
|
|
title = "Example";
|
|
|
|
cards = [{
|
|
|
|
type = "markdown";
|
|
|
|
title = "Lovelace";
|
|
|
|
content = "Welcome to your **Lovelace UI**.";
|
|
|
|
}];
|
|
|
|
}];
|
2018-01-26 00:41:36 +00:00
|
|
|
};
|
2020-06-20 11:05:30 +01:00
|
|
|
lovelaceConfigWritable = true;
|
|
|
|
};
|
2021-12-18 22:01:33 +00:00
|
|
|
|
|
|
|
# Cause a configuration change inside `configuration.yml` and verify that the process is being reloaded.
|
|
|
|
specialisation.differentName = {
|
|
|
|
inheritParentConfig = true;
|
|
|
|
configuration.services.home-assistant.config.homeassistant.name = lib.mkForce "Test Home";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Cause a configuration change that requires a service restart as we added a new runtime dependency
|
|
|
|
specialisation.newFeature = {
|
|
|
|
inheritParentConfig = true;
|
2023-04-28 20:36:30 +01:00
|
|
|
configuration.services.home-assistant.config.backup = {};
|
2021-12-18 22:01:33 +00:00
|
|
|
};
|
2023-04-17 22:49:13 +01:00
|
|
|
|
|
|
|
specialisation.removeCustomThings = {
|
|
|
|
inheritParentConfig = true;
|
|
|
|
configuration.services.home-assistant = {
|
|
|
|
customComponents = lib.mkForce [];
|
|
|
|
customLovelaceModules = lib.mkForce [];
|
|
|
|
};
|
|
|
|
};
|
2018-02-01 12:42:07 +00:00
|
|
|
};
|
2018-01-26 00:41:36 +00:00
|
|
|
|
2021-12-18 22:01:33 +00:00
|
|
|
testScript = { nodes, ... }: let
|
2023-02-20 16:59:50 +00:00
|
|
|
system = nodes.hass.system.build.toplevel;
|
2021-12-18 22:01:33 +00:00
|
|
|
in
|
|
|
|
''
|
2022-07-14 00:41:58 +01:00
|
|
|
import json
|
2022-02-14 01:10:24 +00:00
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
start_all()
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2022-02-14 01:10:24 +00:00
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
def get_journal_cursor() -> str:
|
|
|
|
exit, out = hass.execute("journalctl -u home-assistant.service -n1 -o json-pretty --output-fields=__CURSOR")
|
2022-07-14 00:41:58 +01:00
|
|
|
assert exit == 0
|
|
|
|
return json.loads(out)["__CURSOR"]
|
|
|
|
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
def get_journal_since(cursor) -> str:
|
|
|
|
exit, out = hass.execute(f"journalctl --after-cursor='{cursor}' -u home-assistant.service")
|
|
|
|
assert exit == 0
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2023-02-20 16:01:02 +00:00
|
|
|
def get_unit_property(property) -> str:
|
|
|
|
exit, out = hass.execute(f"systemctl show --property={property} home-assistant.service")
|
|
|
|
assert exit == 0
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
def wait_for_homeassistant(cursor):
|
|
|
|
hass.wait_until_succeeds(f"journalctl --after-cursor='{cursor}' -u home-assistant.service | grep -q 'Home Assistant initialized in'")
|
2022-07-14 00:41:58 +01:00
|
|
|
|
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
hass.wait_for_unit("home-assistant.service")
|
2023-02-20 15:43:25 +00:00
|
|
|
cursor = get_journal_cursor()
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that YAML configuration file is in place"):
|
|
|
|
hass.succeed("test -L ${configDir}/configuration.yaml")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
|
|
|
with subtest("Check the lovelace config is copied because lovelaceConfigWritable = true"):
|
2020-01-09 10:19:54 +00:00
|
|
|
hass.succeed("test -f ${configDir}/ui-lovelace.yaml")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that Home Assistant's web interface and API can be reached"):
|
2023-02-20 15:43:25 +00:00
|
|
|
wait_for_homeassistant(cursor)
|
2020-01-09 10:19:54 +00:00
|
|
|
hass.wait_for_open_port(8123)
|
2020-05-20 22:41:42 +01:00
|
|
|
hass.succeed("curl --fail http://localhost:8123/lovelace")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2022-02-01 02:10:44 +00:00
|
|
|
with subtest("Check that custom components get installed"):
|
|
|
|
hass.succeed("test -f ${configDir}/custom_components/prometheus_sensor/manifest.json")
|
|
|
|
hass.wait_until_succeeds("journalctl -u home-assistant.service | grep -q 'We found a custom integration prometheus_sensor which has not been tested by Home Assistant'")
|
|
|
|
|
2022-01-31 01:32:39 +00:00
|
|
|
with subtest("Check that lovelace modules are referenced and fetchable"):
|
2023-12-07 15:29:33 +00:00
|
|
|
hass.succeed("grep -q 'mini-graph-card-bundle.js' '${configDir}/configuration.yaml'")
|
2022-01-31 01:32:39 +00:00
|
|
|
hass.succeed("curl --fail http://localhost:8123/local/nixos-lovelace-modules/mini-graph-card-bundle.js")
|
|
|
|
|
2023-02-20 16:01:02 +00:00
|
|
|
with subtest("Check that optional dependencies are in the PYTHONPATH"):
|
|
|
|
env = get_unit_property("Environment")
|
|
|
|
python_path = env.split("PYTHONPATH=")[1].split()[0]
|
|
|
|
for package in ["colorama", "paho-mqtt", "psycopg2"]:
|
|
|
|
assert package in python_path, f"{package} not in PYTHONPATH"
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
with subtest("Check that declaratively configured components get setup"):
|
|
|
|
journal = get_journal_since(cursor)
|
|
|
|
for domain in ["emulated_hue", "wake_on_lan"]:
|
|
|
|
assert f"Setup of domain {domain} took" in journal, f"{domain} setup missing"
|
|
|
|
|
2021-04-25 13:44:51 +01:00
|
|
|
with subtest("Check that capabilities are passed for emulated_hue to bind to port 80"):
|
|
|
|
hass.wait_for_open_port(80)
|
|
|
|
hass.succeed("curl --fail http://localhost:80/description.xml")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2021-12-01 00:19:01 +00:00
|
|
|
with subtest("Check extra components are considered in systemd unit hardening"):
|
|
|
|
hass.succeed("systemctl show -p DeviceAllow home-assistant.service | grep -q char-ttyUSB")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2021-12-18 22:01:33 +00:00
|
|
|
with subtest("Check service reloads when configuration changes"):
|
2023-02-20 15:43:25 +00:00
|
|
|
pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
|
|
|
|
cursor = get_journal_cursor()
|
|
|
|
hass.succeed("${system}/specialisation/differentName/bin/switch-to-configuration test")
|
|
|
|
new_pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
|
|
|
|
assert pid == new_pid, "The PID of the process should not change between process reloads"
|
|
|
|
wait_for_homeassistant(cursor)
|
|
|
|
|
|
|
|
with subtest("Check service restarts when dependencies change"):
|
|
|
|
pid = new_pid
|
|
|
|
cursor = get_journal_cursor()
|
|
|
|
hass.succeed("${system}/specialisation/newFeature/bin/switch-to-configuration test")
|
|
|
|
new_pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
|
|
|
|
assert pid != new_pid, "The PID of the process should change when its PYTHONPATH changess"
|
|
|
|
wait_for_homeassistant(cursor)
|
|
|
|
|
|
|
|
with subtest("Check that new components get setup after restart"):
|
|
|
|
journal = get_journal_since(cursor)
|
2023-06-07 22:55:51 +01:00
|
|
|
for domain in ["backup"]:
|
2023-02-20 15:43:25 +00:00
|
|
|
assert f"Setup of domain {domain} took" in journal, f"{domain} setup missing"
|
2021-12-18 22:01:33 +00:00
|
|
|
|
2023-04-17 22:49:13 +01:00
|
|
|
with subtest("Check custom components and custom lovelace modules get removed"):
|
|
|
|
cursor = get_journal_cursor()
|
|
|
|
hass.succeed("${system}/specialisation/removeCustomThings/bin/switch-to-configuration test")
|
|
|
|
hass.fail("grep -q 'mini-graph-card-bundle.js' '${configDir}/ui-lovelace.yaml'")
|
|
|
|
hass.fail("test -f ${configDir}/custom_components/prometheus_sensor/manifest.json")
|
|
|
|
wait_for_homeassistant(cursor)
|
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that no errors were logged"):
|
2023-02-20 15:43:25 +00:00
|
|
|
hass.fail("journalctl -u home-assistant -o cat | grep -q ERROR")
|
2020-06-20 11:05:30 +01:00
|
|
|
|
2021-04-25 13:44:51 +01:00
|
|
|
with subtest("Check systemd unit hardening"):
|
2022-01-30 01:29:45 +00:00
|
|
|
hass.log(hass.succeed("systemctl cat home-assistant.service"))
|
2021-04-25 13:44:51 +01:00
|
|
|
hass.log(hass.succeed("systemd-analyze security home-assistant.service"))
|
2018-01-26 00:41:36 +00:00
|
|
|
'';
|
|
|
|
})
|