prometheus-plugs icon indicating copy to clipboard operation
prometheus-plugs copied to clipboard

Configuring basic auth in Distillery

Open acrogenesis opened this issue 5 years ago • 9 comments

In my rel/config/config.exs I set

config :prometheus, ValiotApp.PrometheusExporter,
  auth: {:basic, System.get_env("METRICS_USER"), System.get_env("METRICS_PASS")}

But PrometheusExporter isn't using this configuration. If I instead set it on config/prod.exs it uses the configuration but the env variables aren't set at build time so it has an empty user and password.

I believe it might by related to #25

acrogenesis avatar Apr 21 '19 15:04 acrogenesis

So the problem is evaluated at compile time plug_exporter.ex#L74 I think we could how that particular config is handled, or we could change how deadtrickster/prometheus.ex/lib/prometheus/config.ex#L48 works. The latter seems a better option but a lot more complicated.

acrogenesis avatar Apr 21 '19 16:04 acrogenesis

maybe add a switch for an option, like :runtime t

deadtrickster avatar Apr 21 '19 17:04 deadtrickster

I'm trying something along those lines but I'm having trouble with the metaprogramming 🤯

acrogenesis avatar Apr 21 '19 17:04 acrogenesis

Having the same problem. Distillery's REPLACE_OS_VARS=true does not work because of the tuple structure:

config :prometheus, MyApp.Metrics.Plug,
  auth: {:basic, "prometheus", "${PROMETHEUS_PASSWORD}"}

philipgiuliani avatar May 10 '19 10:05 philipgiuliani

Im the meanwhile, I was able to workaround this problem by adding https://github.com/CultivateHQ/basic_auth and handling authentication myself.

config.exs

config :prometheus, MyApp.Metrics.Plug,
  path: "/"

config :myapp, prometheus_basic_auth: [
  username: "prometheus",
  password: "${PROMETHEUS_PASSWORD}"
]

router.ex

  pipeline :metrics do
    plug BasicAuth, use_config: {:myapp, :prometheus_basic_auth}
  end

  scope "/metrics" do
    pipe_through :metrics

    forward "/", MyApp.Metrics.Plug
  end

philipgiuliani avatar May 16 '19 07:05 philipgiuliani

Nice idea @philipgiuliani thanks!

acrogenesis avatar May 16 '19 15:05 acrogenesis

Use https://github.com/azohra/ptolemy to get your secrets during runtime and have an external system like vault to manage your secrets.

ssajnani avatar Oct 29 '19 20:10 ssajnani

note: you need deleteplug MyApp.MetricsExporter from endpoint.ex if you define you own basic auth plug.

xdays avatar Apr 21 '20 09:04 xdays

I maintain an Elixir library that I use to conditionally execute Plugs at runtime: https://github.com/akoutmos/unplug

We wrote our own predicate for Unplug and then just conditionally execute the PlugExporter at runtime based on the current request.

akoutmos avatar May 09 '20 15:05 akoutmos