-
Notifications
You must be signed in to change notification settings - Fork 151
Closed
Description
the metrics endpoint is opened and have no authentication.
I use a block to auth this block to solve this, and in my Rails app, it used like this
config.middleware.use Prometheus::Middleware::Exporter, authentication: ->(env) do
ActiveSupport::SecurityUtils.secure_compare(
Rack::Request.new(env).params['secret'].to_s,
YOUR_SECRET
)
endmodule Prometheus
module Middleware
class Exporter
attr_reader :app, :registry, :path
FORMATS = [Client::Formats::Text].freeze
FALLBACK = Client::Formats::Text
DEFAULT_AUTHENTICATION = ->(_) { true }
def initialize(app, options = {})
@app = app
@registry = options[:registry] || Client.registry
@path = options[:path] || '/metrics'
@acceptable = build_dictionary(FORMATS, FALLBACK)
@authentication = options[:authentication] || DEFAULT_AUTHENTICATION
end
def call(env)
if env['PATH_INFO'] == @path
if !!@authentication.call(env)
format = negotiate(env, @acceptable)
format ? respond_with(format) : not_acceptable(FORMATS)
else
authentication_failed!
end
else
@app.call(env)
end
end
private
def authentication_failed!
[ 401,
{ 'Content-Type' => 'text/plain' },
["Authentication Failed"]
]
end
end
end
endReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels