-
Notifications
You must be signed in to change notification settings - Fork 485
Safeguard against accidentally using a remote DATABASE_URL #521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce5ef8a
safeguard against accidentally using a remote DATABASE_URL
svenfuchs 25748f3
add DatabaseCleaner safeguard config accessors
svenfuchs 761519c
add safeguard for running in production, namespace errors, add error …
svenfuchs 1a9704c
document safeguards
svenfuchs 7ec0956
add to History.rdoc
svenfuchs 148420d
update Gemfile.lock to database_cleaner 1.6.3
svenfuchs 9f06d59
stop running cucumber, xit out mongo truncation specs
svenfuchs 8b46dfc
allow 127.0.0.1 in DATABASE_URL
svenfuchs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| PATH | ||
| remote: . | ||
| specs: | ||
| database_cleaner (1.6.2) | ||
| database_cleaner (1.6.3) | ||
|
|
||
| GEM | ||
| remote: https://rubygems.org/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| module DatabaseCleaner | ||
| class Safeguard | ||
| class Error < Exception | ||
| class RemoteDatabaseUrl < Error | ||
| def initialize | ||
| super("ENV['DATABASE_URL'] is set to a remote URL. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards") | ||
| end | ||
| end | ||
|
|
||
| class ProductionEnv < Error | ||
| def initialize(env) | ||
| super("ENV['#{env}'] is set to production. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| class RemoteDatabaseUrl | ||
| LOCAL = %w(localhost 127.0.0.1) | ||
|
|
||
| def run | ||
| raise Error::RemoteDatabaseUrl if !skip? && given? | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def given? | ||
| remote?(ENV['DATABASE_URL']) | ||
| end | ||
|
|
||
| def remote?(url) | ||
| url && !LOCAL.any? { |str| url.include?(str) } | ||
| end | ||
|
|
||
| def skip? | ||
| ENV['DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL'] || | ||
| DatabaseCleaner.allow_remote_database_url | ||
| end | ||
| end | ||
|
|
||
| class Production | ||
| KEYS = %w(ENV RACK_ENV RAILS_ENV) | ||
|
|
||
| def run | ||
| raise Error::ProductionEnv.new(key) if !skip? && given? | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def given? | ||
| !!key | ||
| end | ||
|
|
||
| def key | ||
| @key ||= KEYS.detect { |key| ENV[key] == 'production' } | ||
| end | ||
|
|
||
| def skip? | ||
| ENV['DATABASE_CLEANER_ALLOW_PRODUCTION'] || | ||
| DatabaseCleaner.allow_production | ||
| end | ||
| end | ||
|
|
||
| CHECKS = [ | ||
| RemoteDatabaseUrl, | ||
| Production | ||
| ] | ||
|
|
||
| def run | ||
| CHECKS.each { |const| const.new.run } | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| require 'spec_helper' | ||
| require 'support/env' | ||
| require 'active_record' | ||
| require 'database_cleaner/active_record/transaction' | ||
|
|
||
| module DatabaseCleaner | ||
| describe Safeguard do | ||
| include Support::Env | ||
|
|
||
| let(:strategy) { DatabaseCleaner::ActiveRecord::Transaction } | ||
| let(:cleaner) { Base.new(:autodetect) } | ||
|
|
||
| before { allow_any_instance_of(strategy).to receive(:start) } | ||
|
|
||
| describe 'DATABASE_URL is set' do | ||
| describe 'to any value' do | ||
| env DATABASE_URL: 'postgres://remote.host' | ||
|
|
||
| it 'raises' do | ||
| expect { cleaner.start }.to raise_error(Safeguard::Error::RemoteDatabaseUrl) | ||
| end | ||
| end | ||
|
|
||
| describe 'to a localhost url' do | ||
| env DATABASE_URL: 'postgres://localhost' | ||
|
|
||
| it 'does not raise' do | ||
| expect { cleaner.start }.to_not raise_error | ||
| end | ||
| end | ||
|
|
||
| describe 'to a 127.0.0.1 url' do | ||
| env DATABASE_URL: 'postgres://127.0.0.1' | ||
|
|
||
| it 'does not raise' do | ||
| expect { cleaner.start }.to_not raise_error | ||
| end | ||
| end | ||
|
|
||
| describe 'DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL is set' do | ||
| env DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: true | ||
|
|
||
| it 'does not raise' do | ||
| expect { cleaner.start }.to_not raise_error | ||
| end | ||
| end | ||
|
|
||
| describe 'DatabaseCleaner.allow_remote_database_url is true' do | ||
| before { DatabaseCleaner.allow_remote_database_url = true } | ||
| after { DatabaseCleaner.allow_remote_database_url = nil } | ||
|
|
||
| it 'does not raise' do | ||
| expect { cleaner.start }.to_not raise_error | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'ENV is set to production' do | ||
| %w(ENV RACK_ENV RAILS_ENV).each do |key| | ||
| describe "on #{key}" do | ||
| env key => 'production' | ||
|
|
||
| it 'raises' do | ||
| expect { cleaner.start }.to raise_error(Safeguard::Error::ProductionEnv) | ||
| end | ||
| end | ||
|
|
||
| describe 'DATABASE_CLEANER_ALLOW_PRODUCTION is set' do | ||
| env DATABASE_CLEANER_ALLOW_PRODUCTION: true | ||
|
|
||
| it 'does not raise' do | ||
| expect { cleaner.start }.to_not raise_error | ||
| end | ||
| end | ||
|
|
||
| describe 'DatabaseCleaner.allow_production is true' do | ||
| before { DatabaseCleaner.allow_production = true } | ||
| after { DatabaseCleaner.allow_production = nil } | ||
|
|
||
| it 'does not raise' do | ||
| expect { cleaner.start }.to_not raise_error | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| module Support | ||
| module Env | ||
| def self.included(base) | ||
| base.send(:extend, ClassMethods) | ||
| end | ||
|
|
||
| module ClassMethods | ||
| def env(vars) | ||
| before { define_env(vars) } | ||
| after { undefine_env(vars) } | ||
| end | ||
| end | ||
|
|
||
| def define_env(vars) | ||
| vars.each { |key, value| ENV[key.to_s.upcase] = value.to_s } | ||
| end | ||
|
|
||
| def undefine_env(vars) | ||
| vars.each { |key, _| ENV.delete(key.to_s) } | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's a good idea to also include
::1for IPv6?(also, all
127.0.0.0/8addresses are local, although not sure if it's worth the effort adding support for that, as it's not often used)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure PRs are welcome :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely. However, I'd encourage you to file an issue first. In my opinion, it has to be a concrete issue first in order to get patched later.