
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Balrog is a lightweight authorization library for Ruby on Rails >= 5 written by Pixie Labs that can protect your routes. Balrog can be configured to authorize users using a simple password or single sign-on or both.
Add the gem to your Gemfile:
gem 'balrog'
Run the installer to generate an initializer:
$ bundle exec rails generate balrog:install
Enter New Password:
Confirm New Password:
create config/initializers/balrog.rb
$
If you need to create a new password, modify the hash in the Balrog initializer. You can generate a new hash with the provided Rake task:
$ bundle exec rails balrog:generate_hash
New password: *******
Type again: *******
$2a$04$8U/Yun3MZ5..FuT9yUJNK.F2uUuHagtvsD.CNc5lSZegzq9eJjwqu
Copy this hash into config/initializers/balrog.rb
class AdminController < ApplicationController
before_action :authenticate_with_balrog!
end
Use the .use
method to add Balrog to the 'stack'.
For example with Sidekiq::Web...
# Then we tell SideKiq to use Balrog::RoutesMiddleware
Sidekiq::Web.use Balrog::RoutesMiddleware
mount Sidekiq::Web => '/sidekiq'
N.B. If you are mounting Sidekiq Web, you need to disable Sidekiq Web's session in config/initializers/sidekiq.rb.
require 'sidekiq/web'
# In order to force sidekiq to use the rails app's session,
# we need to disable the Sidekiq's session.
Sidekiq::Web.disable(:sessions)
To add a logout button, you can call the balrog_logout_button
view helper
method and pass in a hash of HTML options to style it. After logout, the user
will be redirected to the root of the app.
For example, in your view:
<ul class='nav'>
<li>....</li>
<li><%= balrog_logout_button 'Admin Logout' %></li>
<li>....</li>
</ul>
Other usage examples:
<%= balrog_logout_button %>
<%= balrog_logout_button "Leave this place" %>
<%= balrog_logout_button "Click me", class: 'fancy-button--with-custom-text' %>
<%= balrog_logout_button class: 'fancy-button--with-default-text' %>
set_session_expiry
requires the user to login again after a period of time.
To customise this value, open config/initializers/balrog.rb
after running balrog:install
and change the argument being passed to set_session_expiry
.
The argument passed to set_session_expiry
can be any of the
Rails time extensions.
If you don't want sessions to expire, remove set_session_expiry
from the initializer completely.
Balrog::Middleware.setup do |config|
config.password_hash '$2a$12$BLz7XCFdG9YfwL64KlTgY.T3FY55aQk8SZEzHfpHfw15F2uN1kuSi'
config.set_session_expiry 30.minutes
end
We built Balrog to have a default view and stylesheet so that you can drop Balrog into your project and everything should “just work”. However, we don't want to be in your way if you needed to customise your Balrog gate view.
If you want to customise the Balrog view, you can run the balrog:view
generator, which will copy the required view and layout to your application:
$ rails generate balrog:view
After running the generator, you can now add elements and classes to the
views/balrog/gate.html.erb
, add styles to the
assets/stylesheets/application.css
and import the application stylesheet in
app/views/layouts/balrog.html.erb
. For an example, see the
dummy-rails-app in the spec folder.
To add single sign on you will need to add the omniauth gem to your gem file, along with the omniauth gem for your chosen provider.
In config/initializers/balrog.rb
, call config.set_omniauth
in the setup block.
.set_omniauth
takes the same arguments as the OmniAuth::Builder#provider
method,
a provider and any required keys.
To whitelist any email addresses with a specific domain, call
config.set_domain_whitelist
in the setup block and pass in the domain.
If you want to whitelist multiple domains, you can pass multiple domains
to the .set_domain_whitelist
.
Balrog does not require a password to be set if you wish to use single sign-on only.
Balrog::Middleware.setup do |config|
credentials = Rails.application.credentials
config.set_omniauth :google_oauth2, credentials.google_client_id, credentials.google_client_secret
config.set_domain_whitelist 'pixielabs.io', 'the_fellowship.com'
end
Please note: there is currently a CSRF vulnerability which affects OmniAuth (designated CVE-2015-9284) that requires mitigation at the application level. More details on how to do this can be found on the Omniauth Wiki.
To upgrade, you will need to change your Balrog initializer.
Instead of calling Rails.application.config.middleware.use Balrog::Middleware
, you will now need to call Balrog::Middleware.setup
.
Change the block you pass into these methods. #password_hash
and #set_session_expiry
now need to called on a block parameter, e.g set_session_expiry 30.minutes
needs to be changed to config.set_session_expiry 30.minutes
.
See below for code examples.
# Balrog 1.1
Rails.application.config.middleware.use Balrog::Middleware do
password_hash '$2a$12$I8Fp3e2GfSdM7KFyoMx56.BVdHeeyk9DQWKkdsxw7USvU/mC8a8.q'
set_session_expiry 30.minutes
end
# Balrog 2.0
Balrog::Middleware.setup do |config|
config.set_password_hash '$2a$12$9lquJW6mVYYS1pD1xYMGzulyC6sEDuLIUfkA/Y7F3RQ8psLNYyLeO'
config.set_session_expiry 30.minutes
end
Tests are part of the dummy Rails app within the spec folder. To run the tests:
$ cd spec/dummy-rails-app
$ bundle
$ rails generate active_record:session_migration
$ redis-server
Then in a different terminal:
$ cd spec/dummy-rails-app
$ rspec
Before contributing, please read the code of conduct.
routes.rb
FAQs
Unknown package
We found that balrog demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.