-
-
Notifications
You must be signed in to change notification settings - Fork 85
feat: periodically check auth_query creds #464
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| defmodule Supavisor.SecretChecker do | ||
| @moduledoc false | ||
|
|
||
| use GenServer | ||
| require Logger | ||
|
|
||
| alias Supavisor.Helpers | ||
|
|
||
| @interval :timer.seconds(15) | ||
|
|
||
| def start_link(args) do | ||
| name = {:via, Registry, {Supavisor.Registry.Tenants, {:secret_checker, args.id}}} | ||
|
|
||
| GenServer.start_link(__MODULE__, args, name: name) | ||
| end | ||
|
|
||
| def init(args) do | ||
| Logger.debug("SecretChecker: Starting secret checker") | ||
| tenant = Supavisor.tenant(args.id) | ||
|
|
||
| %{auth: auth, user: user} = Enum.find(args.replicas, fn e -> e.replica_type == :write end) | ||
|
|
||
| state = %{ | ||
| tenant: tenant, | ||
| auth: auth, | ||
| user: user, | ||
| key: {:secrets, tenant, user}, | ||
| ttl: args[:ttl] || :timer.hours(24), | ||
| conn: nil, | ||
| check_ref: check() | ||
| } | ||
|
|
||
| Logger.metadata(project: tenant, user: user) | ||
| {:ok, state, {:continue, :init_conn}} | ||
| end | ||
|
|
||
| def handle_continue(:init_conn, %{auth: auth} = state) do | ||
| ssl_opts = | ||
| if auth.upstream_ssl and auth.upstream_verify == "peer" do | ||
| [ | ||
| {:verify, :verify_peer}, | ||
| {:cacerts, [Helpers.upstream_cert(auth.upstream_tls_ca)]}, | ||
| {:server_name_indication, auth.host}, | ||
| {:customize_hostname_check, [{:match_fun, fn _, _ -> true end}]} | ||
| ] | ||
| end | ||
|
|
||
| {:ok, conn} = | ||
| Postgrex.start_link( | ||
| hostname: auth.host, | ||
| port: auth.port, | ||
| database: auth.database, | ||
| password: auth.password.(), | ||
| username: auth.user, | ||
| parameters: [application_name: "Supavisor auth_query"], | ||
| ssl: auth.upstream_ssl, | ||
| socket_options: [ | ||
| auth.ip_version | ||
| ], | ||
| queue_target: 1_000, | ||
| queue_interval: 5_000, | ||
| ssl_opts: ssl_opts || [] | ||
| ) | ||
|
|
||
| # kill the postgrex connection if the current process exits unexpectedly | ||
| Process.link(conn) | ||
| {:noreply, %{state | conn: conn}} | ||
| end | ||
|
|
||
| def handle_info(:check, state) do | ||
| Logger.debug("Checking secrets") | ||
| check_secrets(state) | ||
| Logger.debug("Secrets checked") | ||
| {:noreply, %{state | check_ref: check()}} | ||
| end | ||
|
|
||
| def handle_info(msg, state) do | ||
| Logger.error("Unexpected message: #{inspect(msg)}") | ||
| {:noreply, state} | ||
| end | ||
|
|
||
| def terminate(_, state) do | ||
| :gen_statem.stop(state.conn) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I've been wanting to add this PR to Postgrex for a long time |
||
| :ok | ||
| end | ||
|
|
||
| def check(interval \\ @interval), | ||
| do: Process.send_after(self(), :check, interval) | ||
|
|
||
| def check_secrets(%{auth: auth, user: user, conn: conn} = state) do | ||
| case Helpers.get_user_secret(conn, auth.auth_query, user) do | ||
| {:ok, secret} -> | ||
| method = if secret.digest == :md5, do: :auth_query_md5, else: :auth_query | ||
| secrets = Map.put(secret, :alias, auth.alias) | ||
|
|
||
| update_cache = | ||
| case Cachex.get(Supavisor.Cache, state.key) do | ||
| {:ok, {:cached, {_, {old_method, old_secrets}}}} -> | ||
| method != old_method or secrets != old_secrets.() | ||
|
|
||
| other -> | ||
| Logger.error("Failed to get cache: #{inspect(other)}") | ||
| true | ||
| end | ||
|
|
||
| if update_cache do | ||
| Logger.info("Secrets changed or not present, updating cache") | ||
| value = {:ok, {method, fn -> secrets end}} | ||
| Cachex.put(Supavisor.Cache, state.key, {:cached, value}, expire: :timer.hours(24)) | ||
| end | ||
|
|
||
| other -> | ||
| Logger.error("Failed to get secret: #{inspect(other)}") | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need that registry at all? It doesn't seem to be used for anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, it can be very useful during debugging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not 100% sure if it is good approach. Maybe we could use
Process.set_label/1in the future instead?