Popularity
1.5
Growing
Activity
0.0
Declining
46
2
5

Description

Build a highly secure, multi-tenant rails app without data leak.

Keep your data secure with multi-tenant-support. Prevent most ActiveRecord CRUD methods to action across tenant, ensuring no one can accidentally or intentionally access other tenants' data. This can be crucial for applications handling sensitive information like financial information, intellectual property, and so forth.

Programming language: Ruby
License: MIT License
Latest version: v1.5.0

MultiTenantSupport alternatives and similar gems

Based on the "Multi-tenancy" category.
Alternatively, view multi-tenant-support alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of MultiTenantSupport or a related project?

Add another 'Multi-tenancy' Gem

README

MultiTenantSupport

Test Gem Version

Build a highly secure, multi-tenant rails app without data leak.

Keep your data secure with multi-tenant-support. Prevent most ActiveRecord CRUD methods to action across tenant, ensuring no one can accidentally or intentionally access other tenants' data. This can be crucial for applications handling sensitive information like financial information, intellectual property, and so forth.

  • Prevent most ActiveRecord CRUD methods from acting across tenants.
  • Support Row-level Multitenancy
  • Build on ActiveSupport::CurrentAttributes offered by rails
  • Auto set current tenant through subdomain and domain in controller (overrideable)
  • Support ActiveJob and Sidekiq

This gem was inspired much from acts_as_tenant, multitenant, multitenancy, rails-multitenant, activerecord-firewall, milia.

But it does more than them, and highly focuses on ActiveRecord data leak protection.

What make it differnce on details

It protects data in every scenario in great detail. Currently, you can't find any multi-tenant gems doing a full data leak protect on ActiveRecord. But this gem does it.

Our protection code mainly focus on 5 scenarios:

  • Action by tenant
    • CurrentTenantSupport.current_tenant exists
    • CurrentTenantSupport.allow_read_across_tenant is false (default)
  • Action by wrong tenant
    • CurrentTenantSupport.current_tenant does not match target_record.account
    • CurrentTenantSupport.allow_read_across_tenant is false (default)
  • Action when missing tenant
    • CurrentTenantSupport.current_tenant is nil
    • CurrentTenantSupport.allow_read_across_tenant is false (default)
  • Action by super admin but readonly
    • CurrentTenantSupport.current_tenant is nil
    • CurrentTenantSupport.allow_read_across_tenant is true
  • Action by super admin but want modify on a specific tenant
    • CurrentTenantSupport.current_tenant is nil
    • CurrentTenantSupport.allow_read_across_tenant is true
    • Run code in the block of CurrentTenantSupport.under_tenant

Below are the behaviour of all ActiveRecord CRUD methods under abvove scenarios:

Protect on read

Read By tenant missing tenant super admin super admin(modify on a specific tenant)
count ๐Ÿ• ๐Ÿšซ ๐ŸŒŽ ๐Ÿ•
first ๐Ÿ• ๐Ÿšซ ๐ŸŒŽ ๐Ÿ•
last ๐Ÿ• ๐Ÿšซ ๐ŸŒŽ ๐Ÿ•
where ๐Ÿ• ๐Ÿšซ ๐ŸŒŽ ๐Ÿ•
find_by ๐Ÿ• ๐Ÿšซ ๐ŸŒŽ ๐Ÿ•
unscoped ๐Ÿ• ๐Ÿšซ ๐ŸŒŽ ๐Ÿ•

๐Ÿ• scoped ​ ​ ​ ๐ŸŒŽ ​ unscoped ​ ​ ​ โœ… ​ allow ​ ​ ​ ๐Ÿšซ ​ disallow ​ ​ ​ โš ๏ธ ​ Not protected

Protect on initialize

Initialize by tenant wrong tenant missing tenant super admin super admin(modify on a specific tenant)
new โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
build โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
reload โœ… ๐Ÿšซ ๐Ÿšซ โœ… โœ…

๐Ÿ• scoped ​ ​ ​ ๐ŸŒŽ ​ unscoped ​ ​ ​ โœ… ​ allow ​ ​ ​ ๐Ÿšซ ​ disallow ​ ​ ​ โš ๏ธ ​ Not protected

Protect on create

create by tenant wrong tenant missing tenant super admin super admin(modify on a specific tenant)
save โœ… ​ ๐Ÿ• ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
save! โœ… ​ ๐Ÿ• ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
create โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
create! โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
insert โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
insert! โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
insert_all โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
insert_all! โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•

๐Ÿ• scoped ​ ​ ​ ๐ŸŒŽ ​ unscoped ​ ​ ​ โœ… ​ allow ​ ​ ​ ๐Ÿšซ ​ disallow ​ ​ ​ โš ๏ธ ​ Not protected

Protect on tenant assign

Manual assign or update tenant by tenant missing tenant super admin super admin(modify on a specific tenant)
account= ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ
account_id= ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ
update(account:) ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ
update(account_id:) ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ

๐Ÿ• scoped ​ ​ ​ ๐ŸŒŽ ​ unscoped ​ ​ ​ โœ… ​ allow ​ ​ ​ ๐Ÿšซ ​ disallow ​ ​ ​ โš ๏ธ ​ Not protected

Protect on update

Update by tenant wrong tenant missing tenant super admin super admin(modify on a specific tenant)
save โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
save! โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
update โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
update_all โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
update_attribute โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
update_columns โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
update_column โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
upsert_all โš ๏ธ - ๐Ÿšซ โš ๏ธ โš ๏ธ
upsert โš ๏ธ - ๐Ÿšซ โš ๏ธ โš ๏ธ

๐Ÿ• scoped ​ ​ ​ ๐ŸŒŽ ​ unscoped ​ ​ ​ โœ… ​ allow ​ ​ ​ ๐Ÿšซ ​ disallow ​ ​ ​ โš ๏ธ ​ Not protected

Protect on delete

Delete by tenant wrong tenant missing tenant super admin super admin(modify on a specific tenant)
destroy โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
destroy! โœ… ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ โœ…
destroy_all โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
destroy_by โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
delete_all โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•
delete_by โœ… ​ ๐Ÿ• - ๐Ÿšซ ๐Ÿšซ โœ… ​ ๐Ÿ•

๐Ÿ• scoped ​ ​ ​ ๐ŸŒŽ ​ unscoped ​ ​ ​ โœ… ​ allow ​ ​ ​ ๐Ÿšซ ​ disallow ​ ​ ​ โš ๏ธ ​ Not protected

Installation

  1. Add this line to your application's Gemfile:

    gem 'multi-tenant-support'
    
  2. And then execute:

    bundle install
    
  3. Add domain and subdomain to your tenant account table (Skip if your rails app already did this)

    rails generate multi_tenant_support:migration YOUR_TENANT_ACCOUNT_TABLE_OR_MODEL_NAME
    
    # Say your tenant account table is "accounts"
    rails generate multi_tenant_support:migration accounts
    
    # You can also run it with the tenant account model name
    # rails generate multi_tenant_support:migration Account
    
    rails db:migrate
    
  4. Create an initializer

    rails generate multi_tenant_support:initializer
    
  5. Set tenant_account_class_name to your tenant account model name in multi_tenant_support.rb

    - config.tenant_account_class_name = 'REPLACE_ME'
    + config.tenant_account_class_name = 'Account'
    
  6. Set host to your app's domain in multi_tenant_support.rb

    - config.host = 'REPLACE.ME'
    + config.host = 'your-app-domain.com'
    
  7. Setup for ActiveJob or Sidekiq

    If you are using ActiveJob

    - # require 'multi_tenant_support/active_job'
    + require 'multi_tenant_support/active_job'
    

    If you are using sidekiq without ActiveJob

    - # require 'multi_tenant_support/sidekiq'
    + require 'multi_tenant_support/sidekiq'
    
  8. Add belongs_to_tenant to all models which you want to scope under tenant

    class User < ApplicationRecord
      belongs_to_tenant :account
    end
    

Usage

Get current

Get current tenant through:

MultiTenantSupport.current_tenant

Switch tenant

You can switch to another tenant temporary through:

MultiTenantSupport.under_tenant amazon do
  # Do things under amazon account
end

Set current tenant global

MultiTenantSupport.set_tenant_account(account)

Temp set current tenant to nil

MultiTenantSupport.without_current_tenant do
  # ...
end

3 protection states

  1. MultiTenantSupport.full_protected?
  2. MultiTenantSupport.allow_read_across_tenant?
  3. MultiTenantSupport.unprotected?

Full protection(default)

The default state is full protection. This gem disallow modify record across tenant by default.

If MultiTenantSupport.current_tenant exist, you can only modify those records under this tenant, otherwise, you will get some errors like:

  • MultiTenantSupport::MissingTenantError
  • MultiTenantSupport::ImmutableTenantError
  • MultiTenantSupport::NilTenantError
  • MultiTenantSupport::InvalidTenantAccess
  • ActiveRecord::RecordNotFound

If MultiTenantSupport.current_tenant is missing, you cannot modify or create any tenanted records.

If you switched to other state, you can switch back through:

MultiTenantSupport.turn_on_full_protection

# Or
MultiTenantSupport.turn_on_full_protection do
  # ...
end

Allow read across tenant for super admin

You can turn on the permission to read records across tenant through:

MultiTenantSupport.allow_read_across_tenant

# Or
MultiTenantSupport.allow_read_across_tenant do
  # ...
end

You can put it in a before action in SuperAdmin's controllers

Turn off protection

Sometimes, as a super admin, we need to execute certain maintenatn operations over all tenant records. You can do this through:

MultiTenantSupport.turn_off_protection

# Or
MultiTenantSupport.turn_off_protection do
  # ...
end

Set current tenant acccount in controller by default

This gem has set a before action set_current_tenant_account on ActionController. It search tenant by subdomain or domain. Do remember to skip_before_action :set_current_tenant_account in super admin controllers.

Feel free to override it, if the finder behaviour is not what you want.

Override current tenant finder method if domain/subdomain is not the way you want

You can override find_current_tenant_account in any controller with your own tenant finding strategy. Just make sure this method return the tenat account record or nil.

For example, say you only want to find tenant with domain not subdomain. It's very simple:

class ApplicationController < ActionController::Base
  private

  def find_current_tenant_account
    Account.find_by(domain: request.domain)
  end
end

Then your tenant finding strategy has changed from domain/subdomain to domain only.

upsert_all

Currently, we don't have a good way to protect this method. So please use upser_all carefully.

Unscoped

This gem has override unscoped to prevent the default tenant scope be scoped out. But if you really want to scope out the default tenant scope, you can use unscope_tenant.

Console

Console does not allow read across tenant by default. But you have several ways to change that:

  1. Set allow_read_across_tenant_by_default in the initialize file

    console do |config|
      config.allow_read_across_tenant_by_default = true
    end
    
  2. Set the environment variable ALLOW_READ_ACROSS_TENANT when call consoel command

    ALLOW_READ_ACROSS_TENANT=1 rails console
    
  3. Manual change it in console

    $ rails c
    $ irb(main):001:0> MultiTenantSupport.allow_read_across_tenant
    

Testing

Minitest (Rails default)

# test/test_helper.rb
require 'multi_tenant_support/minitet'

RSpec (with Capybara)

# spec/rails_helper.rb or spec/spec_helper.rb
require 'multi_tenant_support/rspec'

Above code will make sure the MultiTenantSupport.current_tenant won't accidentally be reset during integration and system tests. For example:

With above testing requre code

# Integration test
test "a integration test" do
  host! "apple.example.com"

  assert_no_changes "MultiTenantSupport.current_tenant" do
    get users_path
  end
end

# System test
test "a system test" do
  Capybara.app_host = "http://apple.example.com"

  assert_no_changes "MultiTenantSupport.current_tenant" do
    visit users_path
  end
end

Code Example

Database Schema

create_table "accounts", force: :cascade do |t|
  t.bigint "domain"
  t.bigint "subdomain"
end

create_table "users", force: :cascade do |t|
  t.bigint "account_id"
end

Initializer

# config/initializers/multi_tenant_support.rb

MultiTenantSupport.configure do
  model do |config|
    config.tenant_account_class_name = 'Account'
    config.tenant_account_primary_key = :id
  end

  controller do |config|
    config.current_tenant_account_method = :current_tenant_account
  end

  app do |config|
    config.excluded_subdomains = ['www']
    config.host = 'example.com'
  end

  console do |config|
    config.allow_read_across_tenant_by_default = false
  end
end

Model

class Account < AppplicationRecord
  has_many :users
end

class User < ApplicationRecord
  belongs_to_tenant :account
end

Controler

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id]) # This result is already scope under current_tenant_account
    @you_can_get_account = current_tenant_account
  end
end

ActiveRecord proteced methods

ActiveRecord proteced methods count ๐Ÿ”’ save ๐Ÿ”’ account= ๐Ÿ”’ upsert โš ๏ธ (Partial) first ๐Ÿ”’ save! ๐Ÿ”’ account_id= ๐Ÿ”’ destroy ๐Ÿ”’ last ๐Ÿ”’ create ๐Ÿ”’ update ๐Ÿ”’ destroy! ๐Ÿ”’ where ๐Ÿ”’ create! ๐Ÿ”’ update_all ๐Ÿ”’ destroy_all ๐Ÿ”’ find_by ๐Ÿ”’ insert ๐Ÿ”’ update_attribute ๐Ÿ”’ destroy_by ๐Ÿ”’ reload ๐Ÿ”’ insert! ๐Ÿ”’ update_columns ๐Ÿ”’ delete_all ๐Ÿ”’ new ๐Ÿ”’ insert_all ๐Ÿ”’ update_column ๐Ÿ”’ delete_by ๐Ÿ”’ build ๐Ÿ”’ insert_all! ๐Ÿ”’ upsert_all โš ๏ธ (Partial) unscoped ๐Ÿ”’

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hoppergee/multi_tenant_support.

License

The gem is available as open source under the terms of the MIT License.


*Note that all licence references and agreements mentioned in the MultiTenantSupport README section above are relevant to that project's source code only.