DEV Community

David Turing
David Turing

Posted on

Add Authentication Behind Your Sidekiq Dashboard (or any route)

If you need to add authentication behind your Sidekiq dashboard, then you can add the following to your routes.rb file.

  scope :monitoring do
    Sidekiq::Web.use Rack::Auth::Basic do |username, password|
      ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest(ENV["ADMIN_USERNAME"])) &
        ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest(ENV["ADMIN_PASSWORD"]))

    mount Sidekiq::Web, at: '/sidekiq'

You can set the required username and password by updating the environment variables ADMIN_USERNAME and ADMIN_PASSWORD.

Then, whenever you visit localhost:3000/monitoring/sidekiq, you'll be able to see a nice dashboard with details about your background jobs. Woah, beautiful!

If you're using something like Devise, then it's even easier. Add the following to your routes.rb.

authenticate :user, lambda { |u| u.has_role? :site_admin } do
    mount Sidekiq::Web => '/sidekiq'
    get 'user_list' => 'pages#user_list'

As you can see above, it's straightforward to add any route behind the authentication.

Hope this pattern helps. Happy coding.

Discussion (0)