init-relay-env.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python3
  2. """Initialize private deployment secrets without printing them or overwriting."""
  3. import argparse
  4. import os
  5. import secrets
  6. from pathlib import Path
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument('--output', type=Path, default=Path(__file__).with_name('.env'))
  9. parser.add_argument('--bind', default='127.0.0.1')
  10. parser.add_argument('--port', type=int, default=8080)
  11. parser.add_argument('--admin-email', default='admin@relay.local')
  12. args = parser.parse_args()
  13. if not 1 <= args.port <= 65535:
  14. parser.error('port must be between 1 and 65535')
  15. for value in (args.bind, args.admin_email):
  16. if any(c in value for c in '\r\n\x00'):
  17. parser.error('values must be single lines')
  18. values = {
  19. 'RELAY_VERSION': 'relay-v3-dev',
  20. 'BIND_HOST': args.bind,
  21. 'SERVER_PORT': str(args.port),
  22. 'ADMIN_EMAIL': args.admin_email,
  23. 'ADMIN_PASSWORD': secrets.token_urlsafe(24),
  24. 'POSTGRES_PASSWORD': secrets.token_hex(32),
  25. 'REDIS_PASSWORD': secrets.token_hex(32),
  26. 'JWT_SECRET': secrets.token_hex(32),
  27. 'TOTP_ENCRYPTION_KEY': secrets.token_hex(32),
  28. }
  29. fd = os.open(args.output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
  30. with os.fdopen(fd, 'w', encoding='utf-8', newline='\n') as output:
  31. for key, value in values.items():
  32. output.write(f'{key}={value}\n')
  33. print(f'Created private deployment config: {args.output}')