Giter VIP home page Giter VIP logo

penthouse's Introduction

Penthouse

Codeship Status Code Climate RubyDocs

Penthouse is an alternative to the excellent Apartment gem โ€“ however Penthouse is more of a framework for multi-tenancy than a library, in that it provides less out-of-the-box functionality, but should make for easier customisation.

Installation

Add this line to your application's Gemfile:

gem 'penthouse'

Basic Usage

If you're using Rails, you just need to configure an initializer at config/initializers/penthouse.rb

require 'penthouse'
# include the standard Rack app
require 'penthouse/app'
# require the relevant router/runner you wish to use
require 'penthouse/routers/subdomain_router'
require 'penthouse/runners/schema_runner'

Penthouse.configure do |config|
  config.router = Penthouse::Routers::SubdomainRouter
  config.runner = Penthouse::Runners::SchemaRunner
  # enhance migrations to migrate all tenants
  config.migrate_tenants = true
  # setup a proc which will return the tenants
  config.tenants = Proc.new do
    Account.each_with_object({}) do |account, result|
      result.merge!(account.slug => account)
    end
  end
end

Rails.application.config.middleware.use Penthouse::App

It's advised that if you want to customise these classes, you do so by sub-classing Penthouse::App, Penthouse::Routers::BaseRouter and/or Penthouse::Runners::BaseRunner within this initializer.

Octopus (shard) Usage

If you want to have multiple databases on isolated hardware, you'll need to use the Octopus tenant types. In addition to the above initializer you'll need to configure Octopus:

require 'octopus'

Octopus.setup do |config|
  config.environments = [Rails.env]
end

ActiveJob

If you are using ActiveJob, you'll need to create an active_job.rb initializer:

require 'penthouse/active_job'

class ActiveJob::Base
  include Penthouse::ActiveJob
end

Sidekiq

If you are using Sidekiq, you simply need to include the Penthouse module, this can be done in the penthouse.rb initializer:

require 'penthouse/sidekiq'

Dictionary

  • Router โ€“ this class receives a Rack request object and returns an identifier (just a string or symbol) for the tenant.
  • Runner โ€“ this class receives the identifier (either from the router or manually switching), then looks up the tenant instance and runs the code within it.

Contributing

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

Testing

CI/Local

We use circle,a dn it can be used locally with docker. See:

echo <circle-api-token> | circleci setup
circleci local execute

Without Circle

Docker is used for isolated testing. To test all supported versions run:

make test

For specific versions see the Makefile.

Alternatively you can tets any version locally with:

export RUBY_VERSION=2.5.0
rbenv local $RUBY_VERSION

RUBY_VERSION=$RUBY_VERSION RAILS_VERSION=4.2.11.1 PG_VERSION=0.21.0 bundle install
RUBY_VERSION=$RUBY_VERSION RAILS_VERSION=4.2.11.1 PG_VERSION=0.21.0 bundle exec rsepc

penthouse's People

Contributors

0x0badc0de avatar dmcnally avatar hubertpompecki avatar joshuatatterton avatar ryantownsend avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

penthouse's Issues

Upgrade Penthouse to be Compatible with Active Record 5.2

Some of the alias methods inside the Migrator have been changed in Activerecord >= 5.2.

These changes were dealt with by Apartment, so we can take some inspiration here: https://github.com/influitive/apartment/pull/523/files

Acceptance Criteria

  • We can install Penthouse in a new Rails 6 application
  • We can install Penthouse in a new Rails 5.2 application
  • Two releases are prepared for the gem with semantic versioning used and release notes written.

Integrate shared tables

Currently to handle shared tables, we're having to perform a lot of cruft in individual applications, this could be more neatly abstracted by Penthouse.

default_database_configuration = {
  pool: 20,
  connect_timeout: 5, # seconds
  checkout_timeout: 5, # seconds
  variables: { statement_timeout: 5_000 } # milliseconds
}.with_indifferent_access

ActionDispatch::Reloader.to_prepare do
  # load our global database configuration
  database_config = ActiveRecord::Base.configurations[Rails.env] ||
    Rails.application.config.database_configuration[Rails.env]
  # set some default values
  database_config = default_database_configuration.deep_merge(database_config)
  # set the global tables to use the global database
  [Account, AccountSetting].each do |model|
    # enforce the master connection
    model.octopus_establish_connection(database_config)
    # only allow use of master DB
    model.allow_shard(Octopus.master_shard)
    # ensure it has the public schema prefix
    model.table_name = "public.#{model.table_name.split(".", 2).last}"
  end
end

Upgrade yard to version 0.9.11 or later

yard vulnerability has been present in penthouse.gemspec since Dec 2017. We need to upgrade to 0.9.11 or later.

This needs to be complete by April 2019.

Due to time passed, I would suggest updating to the very latest version, if greater than 0.9.11, unless an issue is identified with doing so.

Vulnerability details:

lib/yard/core_ext/file.rb in the server in YARD before 0.9.11 does not block relative paths with an initial ../ sequence, which allows attackers to conduct directory traversal attacks and read arbitrary files.

[2] Upgrade rubocop to version 0.49.0 or later

Rubocop vulnerability present in penthouse.gemspec. We need to upgrade to 0.49.0 or later.

This needs to be complete by April 2019.

Vulnerability details:

RuboCop 0.48.1 and earlier does not use /tmp in safe way, allowing local users to exploit this to tamper with cache files belonging to other users.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.