Skip to content

Commit aa4e52c

Browse files
samrosestaaldraad
authored andcommitted
test: some sanity tests for jit pam module
1 parent dd6c5d8 commit aa4e52c

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

testinfra/test_ami_nix.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,140 @@ def test_libpq5_version(host):
628628
print("✓ libpq5 version is >= 14")
629629

630630

631+
def test_jit_pam_module_installed(host):
632+
"""Test that the JIT PAM module (pam_jit_pg.so) is properly installed."""
633+
# Check if gatekeeper is installed via Nix
634+
result = run_ssh_command(host['ssh'], "sudo -u postgres ls -la /var/lib/postgresql/.nix-profile/lib/security/pam_jit_pg.so 2>/dev/null")
635+
if result['succeeded']:
636+
print(f"\nJIT PAM module found in Nix profile:\n{result['stdout']}")
637+
else:
638+
print("\nJIT PAM module not found in postgres user's Nix profile")
639+
assert False, "JIT PAM module (pam_jit_pg.so) not found in expected location"
640+
641+
# Check if the symlink exists in the Linux PAM security directory
642+
result = run_ssh_command(host['ssh'], "find /nix/store -type f -path '*/lib/security/pam_jit_pg.so' 2>/dev/null | head -5")
643+
if result['succeeded'] and result['stdout'].strip():
644+
print(f"\nJIT PAM module symlinks found:\n{result['stdout']}")
645+
else:
646+
print("\nNo JIT PAM module symlinks found in /nix/store")
647+
648+
# Verify the module is a valid shared library
649+
result = run_ssh_command(host['ssh'], "file /var/lib/postgresql/.nix-profile/lib/security/pam_jit_pg.so")
650+
if result['succeeded']:
651+
print(f"\nJIT PAM module file type:\n{result['stdout']}")
652+
assert "shared object" in result['stdout'].lower() or "dynamically linked" in result['stdout'].lower(), \
653+
"JIT PAM module is not a valid shared library"
654+
655+
print("✓ JIT PAM module is properly installed")
656+
657+
658+
def test_pam_postgresql_config(host):
659+
"""Test that the PAM configuration for PostgreSQL exists and is properly configured."""
660+
# Check PostgreSQL version to determine if PAM config should exist
661+
result = run_ssh_command(host['ssh'], "sudo -u postgres psql --version | grep -oE '[0-9]+' | head -1")
662+
pg_major_version = 15 # Default
663+
if result['succeeded'] and result['stdout'].strip():
664+
try:
665+
pg_major_version = int(result['stdout'].strip())
666+
except ValueError:
667+
pass
668+
669+
print(f"\nPostgreSQL major version: {pg_major_version}")
670+
671+
# PAM config should exist for non-PostgreSQL 15 versions
672+
if pg_major_version != 15:
673+
# Check if PAM config file exists
674+
result = run_ssh_command(host['ssh'], "ls -la /etc/pam.d/postgresql")
675+
if result['succeeded']:
676+
print(f"\nPAM config file found:\n{result['stdout']}")
677+
678+
# Check file permissions
679+
result = run_ssh_command(host['ssh'], "stat -c '%a %U %G' /etc/pam.d/postgresql")
680+
if result['succeeded']:
681+
perms = result['stdout'].strip()
682+
print(f"PAM config permissions: {perms}")
683+
# Should be owned by postgres:postgres with 664 permissions
684+
assert "postgres postgres" in perms, "PAM config not owned by postgres:postgres"
685+
else:
686+
print("\nPAM config file not found")
687+
assert False, "PAM configuration file /etc/pam.d/postgresql not found"
688+
else:
689+
print("\nSkipping PAM config check for PostgreSQL 15")
690+
# For PostgreSQL 15, the PAM config should NOT exist
691+
result = run_ssh_command(host['ssh'], "test -f /etc/pam.d/postgresql")
692+
if result['succeeded']:
693+
print("\nWARNING: PAM config exists for PostgreSQL 15 (not expected)")
694+
695+
print("✓ PAM configuration is properly set up")
696+
697+
698+
def test_jit_pam_gatekeeper_profile(host):
699+
"""Test that the gatekeeper package is properly installed in the postgres user's Nix profile."""
700+
# Check if gatekeeper is in the postgres user's Nix profile
701+
result = run_ssh_command(host['ssh'], "sudo -u postgres nix profile list 2>/dev/null | grep -i gatekeeper")
702+
if result['succeeded'] and result['stdout'].strip():
703+
print(f"\nGatekeeper found in Nix profile:\n{result['stdout']}")
704+
else:
705+
# Try alternative check
706+
result = run_ssh_command(host['ssh'], "sudo -u postgres ls -la /var/lib/postgresql/.nix-profile/ | grep -i gate")
707+
if result['succeeded'] and result['stdout'].strip():
708+
print(f"\nGatekeeper-related files in profile:\n{result['stdout']}")
709+
else:
710+
print("\nGatekeeper not found in postgres user's Nix profile")
711+
# This might be expected if it's installed system-wide instead
712+
713+
# Check if we can find the gatekeeper derivation
714+
result = run_ssh_command(host['ssh'], "find /nix/store -maxdepth 1 -type d -name '*gatekeeper*' 2>/dev/null | head -5")
715+
if result['succeeded'] and result['stdout'].strip():
716+
print(f"\nGatekeeper derivations found:\n{result['stdout']}")
717+
else:
718+
print("\nNo gatekeeper derivations found in /nix/store")
719+
720+
print("✓ Gatekeeper package installation check completed")
721+
722+
723+
def test_jit_pam_module_dependencies(host):
724+
"""Test that the JIT PAM module has all required dependencies."""
725+
# Check dependencies of the PAM module
726+
result = run_ssh_command(host['ssh'], "ldd /var/lib/postgresql/.nix-profile/lib/security/pam_jit_pg.so 2>/dev/null")
727+
if result['succeeded']:
728+
print(f"\nJIT PAM module dependencies:\n{result['stdout']}")
729+
730+
# Check for required libraries
731+
required_libs = ["libpam", "libc"]
732+
for lib in required_libs:
733+
if lib not in result['stdout'].lower():
734+
print(f"WARNING: Required library {lib} not found in dependencies")
735+
736+
# Check for any missing dependencies
737+
if "not found" in result['stdout'].lower():
738+
assert False, "JIT PAM module has missing dependencies"
739+
else:
740+
print("\nCould not check JIT PAM module dependencies")
741+
742+
print("✓ JIT PAM module dependencies are satisfied")
743+
744+
745+
def test_jit_pam_postgresql_integration(host):
746+
"""Test that PostgreSQL can be configured to use PAM authentication."""
747+
# Check if PAM is available as an authentication method in PostgreSQL
748+
result = run_ssh_command(host['ssh'], "sudo -u postgres psql -c \"SELECT name, setting FROM pg_settings WHERE name LIKE '%pam%';\" 2>/dev/null")
749+
if result['succeeded']:
750+
print(f"\nPostgreSQL PAM-related settings:\n{result['stdout']}")
751+
752+
# Check pg_hba.conf for potential PAM entries (even if not currently active)
753+
result = run_ssh_command(host['ssh'], "grep -i pam /etc/postgresql/pg_hba.conf 2>/dev/null || echo 'No PAM entries in pg_hba.conf'")
754+
if result['succeeded']:
755+
print(f"\nPAM entries in pg_hba.conf:\n{result['stdout']}")
756+
757+
# Verify PostgreSQL was compiled with PAM support
758+
result = run_ssh_command(host['ssh'], "sudo -u postgres pg_config --configure 2>/dev/null | grep -i pam || echo 'PAM compile flag not found'")
759+
if result['succeeded']:
760+
print(f"\nPostgreSQL PAM compile flags:\n{result['stdout']}")
761+
762+
print("✓ PostgreSQL PAM integration check completed")
763+
764+
631765
def test_postgrest_read_only_session_attrs(host):
632766
"""Test PostgREST with target_session_attrs=read-only and check for session errors."""
633767
# First, check if PostgreSQL is configured for read-only mode

0 commit comments

Comments
 (0)