58 lines
2.6 KiB
Python
58 lines
2.6 KiB
Python
"""Verify effective MaxKB DB/cache connections without modifying business data."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
def verify(db, cache, pgdata):
|
|
config = db.settings_dict
|
|
redis_config = cache.connection_pool.connection_kwargs
|
|
if config.get('HOST') != '127.0.0.1' or int(config.get('PORT') or 5432) != 5432:
|
|
raise ValueError('MaxKB is not connected to its internal PostgreSQL')
|
|
if redis_config.get('host') != '127.0.0.1' or int(redis_config.get('port', 6379)) != 6379:
|
|
raise ValueError('MaxKB is not connected to its internal Redis')
|
|
with db.cursor() as cursor:
|
|
cursor.execute("SELECT current_setting('data_directory'), host(inet_server_addr()), inet_server_port(), current_setting('server_version')")
|
|
actual_dir, address, port, version = cursor.fetchone()
|
|
if actual_dir != pgdata or address != '127.0.0.1' or port != 5432:
|
|
raise ValueError('PostgreSQL server path or listener is outside the internal deployment')
|
|
if not str(version).startswith('17.'):
|
|
raise ValueError('Expected the internal PostgreSQL 17 runtime')
|
|
if not cache.ping():
|
|
raise ValueError('Internal Redis PING failed')
|
|
config_dir = cache.config_get('dir')
|
|
directory = config_dir.get('dir', config_dir.get(b'dir'))
|
|
if isinstance(directory, bytes):
|
|
directory = directory.decode()
|
|
if directory != '/opt/maxkb/data/redis':
|
|
raise ValueError('Redis data directory is not on the persistent MaxKB mount')
|
|
return {'postgresql': {'host': address, 'port': port, 'version': version, 'data_directory': actual_dir},
|
|
'redis': {'host': '127.0.0.1', 'port': 6379, 'version': cache.info('server')['redis_version'],
|
|
'data_directory': directory, 'ping': True}}
|
|
|
|
|
|
def main():
|
|
root = Path('/opt/maxkb-app')
|
|
os.chdir(root)
|
|
sys.path.insert(0, str(root / 'apps'))
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'maxkb.settings')
|
|
os.environ['SERVER_NAME'] = 'web'
|
|
import django
|
|
django.setup()
|
|
from django.db import connection
|
|
from django_redis import get_redis_connection
|
|
pgdata = os.environ.get('PGDATA', '')
|
|
if not Path(pgdata).is_relative_to('/opt/maxkb/data/postgresql'):
|
|
raise ValueError('PGDATA is outside the persistent MaxKB mount')
|
|
print(json.dumps(verify(connection, get_redis_connection('default'), pgdata)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
main()
|
|
except Exception:
|
|
# Connection exceptions can include credentials/DSNs; do not echo them.
|
|
print('Internal service verification failed; check DB/cache configuration privately.', file=sys.stderr)
|
|
sys.exit(1)
|