Search code examples
ruby-on-railsdevisesidekiq

Rails 5 Protect Sidekiq UI


I'm trying to protect the Sidekiq UI with Devise. I'm running Rails 5.1, Devise 4.3 and Sidekiq 5.0. I tried the following post:

How can I password-protect my /sidekiq route (i.e. require authentication for the Sidekiq::Web tool)?

but I get the following error:

Undefined method failure_app for nil:NilClass

My routes file looks like this:

Rails.application.routes.draw do
  constraint = lambda do |request|
    request.env['warden'].authenticate!({ scope: :admin_user })
  end

  constraints constraint do
    mount Sidekiq::Web => '/admin/sidekiq'
  end

  devise_for :users
  resources :users
end

How can I protect Sidekiq UI with Devise on Rails 5.1?


Solution

  • I stumbled across this page that outlines how to secure the Web UI with Devise:

    Devise
    
    Allow any authenticated User
    
    # Allow any user access
    authenticate :user do
      mount Sidekiq::Web => '/sidekiq'
    end
    
    # Allow only admin users
    authenticate :user, lambda { |u| u.admin? } do
      mount Sidekiq::Web => '/sidekiq'
    end
    

    I've confirmed that this works with Rails 5.1 and Devise 4.3.