Config.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\App;
  4. /**
  5. * Startup-time configuration validation.
  6. *
  7. * SPEC §M08.9: log a clear error and exit non-zero if mandatory env vars
  8. * are missing, or if both auth methods are disabled. Done at boot rather
  9. * than first request so misconfigurations crash on `docker compose up`,
  10. * not on the first user click.
  11. */
  12. final class Config
  13. {
  14. /**
  15. * @param array<string, mixed> $settings
  16. */
  17. public static function validateOrExit(array $settings): void
  18. {
  19. $errors = [];
  20. if (($settings['ui_service_token'] ?? '') === '') {
  21. $errors[] = 'UI_SERVICE_TOKEN is empty (required to call the api)';
  22. }
  23. if (($settings['api_base_url'] ?? '') === '') {
  24. $errors[] = 'API_BASE_URL is empty (e.g. http://api:8081)';
  25. }
  26. $oidcEnabled = (bool) ($settings['oidc_enabled'] ?? false);
  27. $localEnabled = (bool) ($settings['local_admin_enabled'] ?? false);
  28. if (!$oidcEnabled && !$localEnabled) {
  29. $errors[] = 'no auth method enabled — set OIDC_ENABLED=true or LOCAL_ADMIN_ENABLED=true';
  30. }
  31. if ($localEnabled) {
  32. if (($settings['local_admin_username'] ?? '') === '') {
  33. $errors[] = 'LOCAL_ADMIN_USERNAME is empty but LOCAL_ADMIN_ENABLED=true';
  34. }
  35. if (($settings['local_admin_password_hash'] ?? '') === '') {
  36. $errors[] = 'LOCAL_ADMIN_PASSWORD_HASH is empty but LOCAL_ADMIN_ENABLED=true';
  37. }
  38. }
  39. if ($oidcEnabled) {
  40. foreach (['oidc_issuer', 'oidc_client_id', 'oidc_client_secret', 'oidc_redirect_uri'] as $key) {
  41. if (($settings[$key] ?? '') === '') {
  42. $errors[] = sprintf('%s is empty but OIDC_ENABLED=true', strtoupper($key));
  43. }
  44. }
  45. }
  46. if ($errors === []) {
  47. return;
  48. }
  49. fwrite(STDERR, "[ui] startup configuration error(s):\n");
  50. foreach ($errors as $err) {
  51. fwrite(STDERR, " - {$err}\n");
  52. }
  53. exit(1);
  54. }
  55. }