Skip to content

please add a authentication in exporter #61

@xuanyu-h

Description

@xuanyu-h

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
      )
    end
module 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
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions