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