kong-entrypoint.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. # Custom entrypoint for Kong that builds Lua expressions for request-transformer
  3. # and performs environment variable substitution in the declarative config.
  4. # Build Lua expressions for translating opaque API keys to asymmetric JWTs.
  5. # When opaque keys are not configured (empty env vars), expressions fall through
  6. # to legacy-only behavior - just passing apikey as-is.
  7. #
  8. # Full expression logic (when opaque keys are configured):
  9. # 1. If Authorization header exists and is NOT an sb_ key -> pass through (user session JWT)
  10. # 2. If apikey matches secret key -> set service_role asymmetric JWT internal "API key"
  11. # 3. If apikey matches publishable key -> set anon asymmetric JWT internal "API key"
  12. # 4. Fallback: pass apikey as-is (legacy HS256 JWT)
  13. if [ -n "$BRIVEN_SECRET_KEY" ] && [ -n "$BRIVEN_PUBLISHABLE_KEY" ]; then
  14. # Opaque keys configured -> full translation expressions
  15. export LUA_AUTH_EXPR="\$((headers.authorization ~= nil and headers.authorization:sub(1, 10) ~= 'Bearer sb_' and headers.authorization) or (headers.apikey == '$BRIVEN_SECRET_KEY' and 'Bearer $SERVICE_ROLE_KEY_ASYMMETRIC') or (headers.apikey == '$BRIVEN_PUBLISHABLE_KEY' and 'Bearer $ANON_KEY_ASYMMETRIC') or headers.apikey)"
  16. # Realtime WebSocket: reads from query_params.apikey (briven-js sends apikey
  17. # via query string), outputs to x-api-key header which Realtime checks first.
  18. export LUA_RT_WS_EXPR="\$((query_params.apikey == '$BRIVEN_SECRET_KEY' and '$SERVICE_ROLE_KEY_ASYMMETRIC') or (query_params.apikey == '$BRIVEN_PUBLISHABLE_KEY' and '$ANON_KEY_ASYMMETRIC') or query_params.apikey)"
  19. else
  20. # Legacy API keys, not sb_ API keys -> pass apikey through unchanged
  21. export LUA_AUTH_EXPR="\$((headers.authorization ~= nil and headers.authorization:sub(1, 10) ~= 'Bearer sb_' and headers.authorization) or headers.apikey)"
  22. export LUA_RT_WS_EXPR="\$(query_params.apikey)"
  23. fi
  24. # Substitute environment variables in the Kong declarative config.
  25. # Uses awk instead of eval/echo to preserve YAML quoting (eval strips double
  26. # quotes, breaking "Header: value" patterns that YAML parses as mappings).
  27. awk '{
  28. result = ""
  29. rest = $0
  30. while (match(rest, /\$[A-Za-z_][A-Za-z_0-9]*/)) {
  31. varname = substr(rest, RSTART + 1, RLENGTH - 1)
  32. if (varname in ENVIRON) {
  33. result = result substr(rest, 1, RSTART - 1) ENVIRON[varname]
  34. } else {
  35. result = result substr(rest, 1, RSTART + RLENGTH - 1)
  36. }
  37. rest = substr(rest, RSTART + RLENGTH)
  38. }
  39. print result rest
  40. }' /home/kong/temp.yml > "$KONG_DECLARATIVE_CONFIG"
  41. # Remove empty key-auth credentials (unconfigured opaque keys)
  42. sed -i '/^[[:space:]]*- key:[[:space:]]*$/d' "$KONG_DECLARATIVE_CONFIG"
  43. exec /entrypoint.sh kong docker-start