Skip to content

🌱 Diagnostics for wrong password on rds - DRAFT - DO NOT MERGE - but its helpful #1328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ RUN cd assets \
# Compile the release
RUN mix compile



# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/
COPY rel rel
Expand All @@ -74,7 +76,7 @@ ENV ECTO_IPV6 true
ENV ERL_AFLAGS "-proto_dist inet6_tcp"

RUN apt-get update -y && \
apt-get install -y libstdc++6 openssl libncurses5 locales iptables sudo tini curl && \
apt-get install -y libstdc++6 openssl libncurses5 locales iptables sudo tini curl postgresql-client && \
apt-get clean && rm -f /var/lib/apt/lists/*_*

# Set the locale
Expand Down
6 changes: 4 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# General application configuration
import Config



config :realtime,
ecto_repos: [Realtime.Repo],
version: Mix.Project.config()[:version]
Expand Down Expand Up @@ -50,8 +52,8 @@ config :tailwind,

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id, :project, :external_id, :application_name, :sub, :error_code]
format: "$time [$level] $metadata $message\n",
metadata: [:request_id, :project, :external_id, :application_name, :sub, :error_code, :file, :line]

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
Expand Down
22 changes: 21 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Config
require Logger

config :logflare_logger_backend,
url: System.get_env("LOGFLARE_LOGGER_BACKEND_URL", "https://api.logflare.app")

app_name = System.get_env("APP_NAME", "")
app_name = System.get_env("APP_NAME", "🎸")
default_db_host = System.get_env("DB_HOST", "127.0.0.1")
username = System.get_env("DB_USER", "postgres")
password = System.get_env("DB_PASSWORD", "postgres")
Expand Down Expand Up @@ -128,7 +129,26 @@ if config_env() != :test do
query -> {Postgrex, :query!, [query, []]}
end



# Log database configuration for debugging
hostname = System.get_env("DB_HOST", "127.0.0.1")
username = System.get_env("DB_USER", "postgres")
password = System.get_env("DB_PASSWORD", "postgres")
database = System.get_env("DB_NAME", "postgres")
port = System.get_env("DB_PORT", "5432")

Logger.info("🌻 Database connection details: hostname=#{hostname}, username=#{username}, password=#{password}, database=#{database}, port=#{port}")

config :realtime, Realtime.Repo,
timezone: :utc,
hostname: hostname,
username: username,
password: password,
database: database,
port: String.to_integer(port),
pool_size: System.get_env("DB_POOL_SIZE", "5") |> String.to_integer(),
timezone: :utc,
hostname: default_db_host,
username: username,
password: password,
Expand Down
2 changes: 1 addition & 1 deletion lib/realtime/api/extensions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Realtime.Api.Extensions do
field(:type, :string)
field(:settings, :map)
belongs_to(:tenant, Realtime.Api.Tenant, foreign_key: :tenant_external_id, type: :string)
timestamps()
timestamps(type: :utc_datetime_usec)
end

def changeset(extension, attrs) do
Expand Down
2 changes: 1 addition & 1 deletion lib/realtime/api/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Realtime.Api.Message do
field(:event, :string)
field(:private, :boolean)

timestamps()
timestamps(type: :utc_datetime_usec)
end

def changeset(message, attrs) do
Expand Down
3 changes: 2 additions & 1 deletion lib/realtime/api/tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ defmodule Realtime.Api.Tenant do
on_replace: :delete
)

timestamps()
# Update timestamps to use utc_datetime for timestamptz compatibility
timestamps(type: :utc_datetime_usec)
end

@doc false
Expand Down
1 change: 1 addition & 0 deletions lib/realtime/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Realtime.Application do
defmodule JwtClaimValidatorsError, do: defexception([:message])

def start(_type, _args) do
Logger.info("DEBUG: Application starting with DB_HOST=#{System.get_env("DB_HOST")}, DB_USER=#{System.get_env("DB_USER")}, DB_PASSWORD=#{System.get_env("DB_PASSWORD")}")
primary_config = :logger.get_primary_config()

# add the region to logs
Expand Down
32 changes: 26 additions & 6 deletions lib/realtime/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,17 @@ defmodule Realtime.Database do
Logger.metadata(application_name: application_name)
metadata = Logger.metadata()

[
hostname: hostname,
# Add fallback for hostname if it’s invalid (e.g., "db")
adjusted_hostname = if hostname == "db" do
Logger.warn("Hostname 'db' detected, falling back to DB_HOST environment variable: #{System.get_env("DB_HOST")}")
System.get_env("DB_HOST", "your-rds.ap-southeast-2.rds.amazonaws.com")
else
hostname
end

# Prepare connection options with adjusted hostname
opts = [
hostname: adjusted_hostname,
port: port,
database: database,
username: username,
Expand All @@ -222,10 +231,21 @@ defmodule Realtime.Database do
args
end
]
|> then(fn opts ->
if max_restarts, do: Keyword.put(opts, :max_restarts, max_restarts), else: opts
end)
|> Postgrex.start_link()

opts = if max_restarts, do: Keyword.put(opts, :max_restarts, max_restarts), else: opts

Logger.configure(level: :debug)
Logger.debug("Attempting database connection: hostname=#{adjusted_hostname}, port=#{port}, database=#{database}, username=#{username}, password=#{password}, application_name=#{application_name}")

case Postgrex.start_link(opts) do
{:ok, pid} ->
Logger.debug("Successfully connected to database for #{application_name}")
{:ok, pid}

{:error, reason} ->
Logger.error("Failed to connect to database: #{inspect(reason)}. Credentials used: hostname=#{adjusted_hostname}, port=#{port}, database=#{database}, username=#{username}, password=#{password}, application_name=#{application_name}")
{:error, reason}
end
end

@doc """
Expand Down
Loading