2023-11-23 10:03:33 +01:00
|
|
|
{ pkgs, lib, ... }:
|
2024-03-20 06:50:41 +01:00
|
|
|
let
|
|
|
|
pkgs' = pkgs;
|
|
|
|
in
|
2023-11-23 10:03:33 +01:00
|
|
|
{
|
2024-03-20 06:50:41 +01:00
|
|
|
peerWithoutUser = pkgs.testers.runNixOSTest {
|
2023-11-23 10:03:33 +01:00
|
|
|
name = "postgresql-peerWithoutUser";
|
|
|
|
|
|
|
|
nodes.machine = { config, pkgs, ... }: {
|
|
|
|
imports = [
|
2024-03-20 06:50:41 +01:00
|
|
|
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
|
|
|
|
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
|
2023-11-23 10:03:33 +01:00
|
|
|
../../modules/blocks/postgresql.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
shb.postgresql.ensures = [
|
|
|
|
{
|
|
|
|
username = "me";
|
2023-12-04 08:33:55 +01:00
|
|
|
database = "me";
|
2023-11-23 10:03:33 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = { nodes, ... }: ''
|
|
|
|
start_all()
|
|
|
|
machine.wait_for_unit("postgresql.service")
|
2023-12-04 08:33:39 +01:00
|
|
|
machine.wait_for_open_port(5432)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
def peer_cmd(user, database):
|
|
|
|
return "sudo -u me psql -U {user} {db} --command \"\"".format(user=user, db=database)
|
|
|
|
|
|
|
|
with subtest("cannot login because of missing user"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.fail(peer_cmd("me", "me"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
with subtest("cannot login with unknown user"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.fail(peer_cmd("notme", "me"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
with subtest("cannot login to unknown database"):
|
|
|
|
machine.fail(peer_cmd("me", "notmine"), timeout=10)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-20 06:50:41 +01:00
|
|
|
peerAuth = pkgs.testers.runNixOSTest {
|
2023-11-23 10:03:33 +01:00
|
|
|
name = "postgresql-peerAuth";
|
|
|
|
|
|
|
|
nodes.machine = { config, pkgs, ... }: {
|
|
|
|
imports = [
|
2024-03-20 06:50:41 +01:00
|
|
|
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
|
|
|
|
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
|
2023-11-23 10:03:33 +01:00
|
|
|
../../modules/blocks/postgresql.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
users.users.me = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "me";
|
|
|
|
extraGroups = [ "sudoers" ];
|
|
|
|
};
|
|
|
|
users.groups.me = {};
|
|
|
|
|
|
|
|
shb.postgresql.ensures = [
|
|
|
|
{
|
|
|
|
username = "me";
|
2023-12-04 08:33:55 +01:00
|
|
|
database = "me";
|
2023-11-23 10:03:33 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = { nodes, ... }: ''
|
|
|
|
start_all()
|
|
|
|
machine.wait_for_unit("postgresql.service")
|
2023-12-04 08:33:39 +01:00
|
|
|
machine.wait_for_open_port(5432)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
def peer_cmd(user, database):
|
|
|
|
return "sudo -u me psql -U {user} {db} --command \"\"".format(user=user, db=database)
|
|
|
|
|
|
|
|
def tcpip_cmd(user, database, port):
|
|
|
|
return "psql -h 127.0.0.1 -p {port} -U {user} {db} --command \"\"".format(user=user, db=database, port=port)
|
|
|
|
|
|
|
|
with subtest("can login with provisioned user and database"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.succeed(peer_cmd("me", "me"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
with subtest("cannot login with unknown user"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.fail(peer_cmd("notme", "me"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
with subtest("cannot login to unknown database"):
|
|
|
|
machine.fail(peer_cmd("me", "notmine"), timeout=10)
|
|
|
|
|
|
|
|
with subtest("cannot login with tcpip"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.fail(tcpip_cmd("me", "me", "5432"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-20 06:50:41 +01:00
|
|
|
tcpIPWithoutPasswordAuth = pkgs.testers.runNixOSTest {
|
2023-11-23 10:03:33 +01:00
|
|
|
name = "postgresql-tcpIpWithoutPasswordAuth";
|
|
|
|
|
|
|
|
nodes.machine = { config, pkgs, ... }: {
|
|
|
|
imports = [
|
2024-03-20 06:50:41 +01:00
|
|
|
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
|
|
|
|
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
|
2023-11-23 10:03:33 +01:00
|
|
|
../../modules/blocks/postgresql.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
shb.postgresql.enableTCPIP = true;
|
|
|
|
shb.postgresql.ensures = [
|
|
|
|
{
|
|
|
|
username = "me";
|
2023-12-04 08:33:55 +01:00
|
|
|
database = "me";
|
2023-11-23 10:03:33 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = { nodes, ... }: ''
|
|
|
|
start_all()
|
|
|
|
machine.wait_for_unit("postgresql.service")
|
2023-12-04 08:33:39 +01:00
|
|
|
machine.wait_for_open_port(5432)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
def peer_cmd(user, database):
|
|
|
|
return "sudo -u me psql -U {user} {db} --command \"\"".format(user=user, db=database)
|
|
|
|
|
|
|
|
def tcpip_cmd(user, database, port):
|
|
|
|
return "psql -h 127.0.0.1 -p {port} -U {user} {db} --command \"\"".format(user=user, db=database, port=port)
|
|
|
|
|
|
|
|
with subtest("cannot login without existing user"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.fail(peer_cmd("me", "me"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
with subtest("cannot login with user without password"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.fail(tcpip_cmd("me", "me", "5432"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-20 06:50:41 +01:00
|
|
|
tcpIPPasswordAuth = pkgs.testers.runNixOSTest {
|
2023-11-23 10:03:33 +01:00
|
|
|
name = "postgresql-tcpIPPasswordAuth";
|
|
|
|
|
|
|
|
nodes.machine = { config, pkgs, ... }: {
|
|
|
|
imports = [
|
2024-03-20 06:50:41 +01:00
|
|
|
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
|
|
|
|
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
|
2023-11-23 10:03:33 +01:00
|
|
|
../../modules/blocks/postgresql.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
users.users.me = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "me";
|
|
|
|
extraGroups = [ "sudoers" ];
|
|
|
|
};
|
|
|
|
users.groups.me = {};
|
|
|
|
|
|
|
|
system.activationScripts.secret = ''
|
|
|
|
echo secretpw > /run/dbsecret
|
|
|
|
'';
|
|
|
|
shb.postgresql.enableTCPIP = true;
|
|
|
|
shb.postgresql.ensures = [
|
|
|
|
{
|
|
|
|
username = "me";
|
2023-12-04 08:33:55 +01:00
|
|
|
database = "me";
|
2023-11-23 10:03:33 +01:00
|
|
|
passwordFile = "/run/dbsecret";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = { nodes, ... }: ''
|
|
|
|
start_all()
|
|
|
|
machine.wait_for_unit("postgresql.service")
|
2023-12-04 08:33:39 +01:00
|
|
|
machine.wait_for_open_port(5432)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
def peer_cmd(user, database):
|
|
|
|
return "sudo -u me psql -U {user} {db} --command \"\"".format(user=user, db=database)
|
|
|
|
|
|
|
|
def tcpip_cmd(user, database, port, password):
|
|
|
|
return "PGPASSWORD={password} psql -h 127.0.0.1 -p {port} -U {user} {db} --command \"\"".format(user=user, db=database, port=port, password=password)
|
|
|
|
|
|
|
|
with subtest("can peer login with provisioned user and database"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.succeed(peer_cmd("me", "me"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
with subtest("can tcpip login with provisioned user and database"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.succeed(tcpip_cmd("me", "me", "5432", "secretpw"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
|
|
|
|
with subtest("cannot tcpip login with wrong password"):
|
2023-12-04 08:33:55 +01:00
|
|
|
machine.fail(tcpip_cmd("me", "me", "5432", "oops"), timeout=10)
|
2023-11-23 10:03:33 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|