Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/postgresql/citus: fix syscall filter and add test #379769

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions nixos/modules/services/databases/postgresql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,16 @@ in
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
SystemCallFilter = [
"@system-service"
"~@privileged @resources"
] ++ lib.optionals (any extensionInstalled [ "plv8" ]) [ "@pkey" ];
SystemCallFilter =
[
"@system-service"
"~@privileged @resources"
]
++ lib.optionals (any extensionInstalled [ "plv8" ]) [ "@pkey" ]
++ lib.optionals (any extensionInstalled [ "citus" ]) [
"getpriority"
"setpriority"
];
UMask = if groupAccessAvailable then "0027" else "0077";
}
(mkIf (cfg.dataDir != "/var/lib/postgresql/${cfg.package.psqlSchema}") {
Expand Down
73 changes: 73 additions & 0 deletions nixos/tests/postgresql/citus.nix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, in #355010 we didn't add a whole new test on purpose - because this won't scale for many extensions. We can't add a new VM test for each of them. But I think we also can't test them together, otherwise the granted exceptions here might overlay each other.

At the same time - we should really test this. So we need a VM test.

I don't really have a solution, but I do think that a VM test for citus is generally a good idea, especially if we consider that this test could be extended to test multiple machines setting up a citus cluster in the future.

TLDR: I'm OK with adding another test here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right I agree with a multi-node test, but would rather do that on top of master when staging is merged

Copy link
Contributor Author

@jflanglois jflanglois Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also makes me think: I wonder if the hardening should be made more configurable in the module? It seems like extensions could easily hit a blocked syscall after an update and this was difficult to debug for me because there were no logs—I only looked at hardening options because postgresql started fine when run manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like extensions could easily hit a blocked syscall after an update and this was difficult to debug for me because there were no logs

Aren't these logged to the kernel log?
I don't recall where exactly, but I'm pretty sure it's written somewhere - for syscalls at least.

I wonder if the hardening should be made more configurable in the module

See #344925 (comment) for my rationale on why it's implemented the way it currently is.

So, in #355010 we didn't add a whole new test on purpose - because this won't scale for many extensions. We can't add a new VM test for each of them. But I think we also can't test them together, otherwise the granted exceptions here might overlay each other.

Agreed. For the time being, my suggestion would be to add VM tests when the need arises (i.e. when we have to adjust the hardening for instance).

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
pkgs,
makeTest,
genTests,
}:

let
inherit (pkgs) lib;

test-sql = pkgs.writeText "postgresql-test" ''
CREATE EXTENSION citus;

CREATE TABLE examples (
id bigserial,
shard_key int,
PRIMARY KEY (id, shard_key)
);

SELECT create_distributed_table('examples', 'shard_key');

INSERT INTO examples (shard_key) SELECT shard % 10 FROM generate_series(1,1000) shard;
'';

makeTestFor =
package:
makeTest {
name = "citus-${package.name}";
meta = with lib.maintainers; {
maintainers = [ typetetris ];
};

nodes.machine =
{ ... }:
{
services.postgresql = {
inherit package;
enable = true;
enableJIT = lib.hasInfix "-jit-" package.name;
extensions =
ps: with ps; [
citus
];
settings = {
shared_preload_libraries = "citus";
};
};
};

testScript = ''
def check_count(statement, lines):
return 'test $(sudo -u postgres psql postgres -tAc "{}") -eq {}'.format(
statement, lines
)


machine.start()
machine.wait_for_unit("postgresql")

with subtest("Postgresql with extension citus is available just after unit start"):
machine.succeed(
"sudo -u postgres psql -f ${test-sql}"
)

machine.succeed(check_count("SELECT count(*) FROM examples;", 1000))

machine.shutdown()
'';
};
in
genTests {
inherit makeTestFor;
filter = _: p: !p.pkgs.citus.meta.broken;
}
1 change: 1 addition & 0 deletions nixos/tests/postgresql/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ in

# extensions
anonymizer = importWithArgs ./anonymizer.nix;
citus = importWithArgs ./citus.nix;
pgjwt = importWithArgs ./pgjwt.nix;
pgvecto-rs = importWithArgs ./pgvecto-rs.nix;
timescaledb = importWithArgs ./timescaledb.nix;
Expand Down