-
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
Conversation
Not sure why exactly the tests are failing on Travis CI, but it seems as if there's been an issue with the |
@svenfuchs Thanks for submitting this! I think it's a good idea. I wonder if we could make it even better. Maybe even check if |
@etagwerker That's an interesting idea ... In our cases we've run the tests against the remote databases. The test tooling (rspec) and/or the spec_helper would set the current env to But yeah, I could see how |
@etagwerker also, forgot to ask ... could you advise on what style/s you'd prefer for opting out of this, error messages raised, or any other details that you'd like to see improved? I'd think we'd probably want to make opting out a Ruby config option such as |
@svenfuchs I like this format:
It'd be great if you could implement that in
Got it. I once saw this and I thought we could've stopped it if we had a safeguard for that: https://twitter.com/moubry/status/818867315970363392 - That's why I brought it up. |
@etagwerker i've made a few more changes:
I have also made these changes to fix Travis CI and cherrypicked them to this branch in an attempt to get the PR green, but there's an unstable spec that fails occasionally. Restarting this job should fix that. Also commented on that here. |
lib/database_cleaner/safeguard.rb
Outdated
end | ||
|
||
def remote?(url) | ||
url && !url.include?('localhost') |
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.
What about 127.0.0.1
? Should that also be covered?
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.
@rwrede good call, thanks for the pointer! i've addressed that in svenfuchs@8b46dfc
@svenfuchs This looks really good. I made a few small changes to the spec over here: https://github.com/DatabaseCleaner/database_cleaner/tree/safeguard If you approve I can go ahead and merge that branch into |
@etagwerker yes, that looks great, thanks a lot! |
@svenfuchs cool, just merged your commits. Thank you! 👍 |
thank you so much, @etagwerker ❤️ |
end | ||
|
||
class RemoteDatabaseUrl | ||
LOCAL = %w(localhost 127.0.0.1) |
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 ::1
for IPv6?
(also, all 127.0.0.0/8
addresses 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.
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.
@etagwerker Thanks for helping to get this in! Are you planning to cut a new version of the gem to deploy this? |
@etagwerker I suppose that there is a simpler solution than extra config variable, just run following: |
@soberstadt Yes, I'll try to cut a release this weekend. @lowang Interesting! That works well when you're using |
@soberstadt Just released a new version. 👍 |
I am getting
When doing CI on Heroku.
|
We seen remote databases wiped by accidentally running a test suite with
DATABASE_URL
being exported.The scenarios were:
DATABASE_URL
to the local environment. Then forgets to unset it and runs the test suite.DATABASE_URL
exported and runs a test suite.While technically this might not be the responsibility of the library
database_cleaner
to take care of I think it would be great if some basic protection was turned on by default.This PR adds a safeguard that looks for the environment variable
DATABASE_URL
and raises unless opted out.The implementation is simple and probably should be improved and documented. I'm opening this PR to see if you were interested in such a safety mechanism before I'd then put more work into it.