Skip to content

Commit 393ca37

Browse files
committed
chore: add test for read-only mount
1 parent 0012206 commit 393ca37

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

ansible/files/permission_check.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
],
9696
}
9797

98+
# postgresql.service is expected to mount /etc as read-only
99+
expected_mount = "/etc ro"
98100

99101
# This program depends on osquery being installed on the system
100102
# Function to run osquery
@@ -151,6 +153,33 @@ def check_nixbld_users():
151153

152154
print("All nixbld users are in the 'nixbld' group.")
153155

156+
def check_postgresql_mount():
157+
# processes table has the nix .postgres-wrapped path as the
158+
# binary path, rather than /usr/lib/postgresql/bin/postgres which
159+
# is a symlink to /var/lib/postgresql/.nix-profile/bin/postgres, a script
160+
# that ultimately calls /nix/store/...-postgresql-and-plugins-15.8/bin/.postgres-wrapped
161+
query = """
162+
SELECT pid
163+
FROM processes
164+
WHERE path LIKE '%.postgres-wrapped%'
165+
AND cmdline LIKE '%-D /etc/postgresql%';
166+
"""
167+
query_result = run_osquery(query)
168+
parsed_result = parse_json(query_result)
169+
170+
pid = parsed_result[0].get("pid")
171+
172+
# get the mounts for the process
173+
with open(f"/proc/{pid}/mounts", "r") as o:
174+
lines = [line for line in o if "/etc" in line and "ro," in line]
175+
if len(lines) == 0:
176+
print(f"Expected exactly 1 match, got 0")
177+
sys.exit(1)
178+
if len(lines) != 1:
179+
print(f"Expected exactly 1 match, got {len(lines)}: {';'.join(lines)}")
180+
sys.exit(1)
181+
182+
print("postgresql.service mounts /etc as read-only.")
154183

155184
def main():
156185
parser = argparse.ArgumentParser(
@@ -218,6 +247,8 @@ def main():
218247
# Check if all nixbld users are in the nixbld group
219248
check_nixbld_users()
220249

250+
# Check if postgresql.service is using a read-only mount for /etc
251+
check_postgresql_mount()
221252

222253
if __name__ == "__main__":
223254
main()

0 commit comments

Comments
 (0)