How to disable "Cannot Render Console from..." on Rails

I'm using Ubuntu/vagrant as my development environment. I'm getting these messages on rails console:

Started GET "/assets/home-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for 10.0.2.2 at 2015-04-02 15:48:31 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

Is it possible to disable those "cannot render..." messages or allow them in any way?


Solution 1:

You need to specifically allow the 10.0.2.2 network space in the Web Console config.

So you'll want something like this:

class Application < Rails::Application
  config.web_console.permissions = '10.0.2.2'
end

Read here for more information.

As pointed out by pguardiario, this wants to go into config/environments/development.rb rather than config/application.rb so it is only applied in your development environment.

Solution 2:

You can whitelist single IP's or whole networks.

Say you want to share your console with 192.168.0.100. You can do this:

class Application < Rails::Application
  config.web_console.whitelisted_ips = '192.168.0.100'
end

If you want to whitelist the whole private network, you can do:

class Application < Rails::Application
  config.web_console.whitelisted_ips = '192.168.0.0/16'
end

If you don't wanna see this message anymore, set this option to false:

class Application < Rails::Application
  config.web_console.whiny_requests = false
end

Be careful what you wish for, 'cause you might just get it all

This is probably only for development purposes so you might prefer to place it under config/environments/development.rb instead of config/application.rb.

Solution 3:

Hardcoding an IP into a configuration file isn't good. What about other devs? What if the ip changes?

Docker-related config should not leak into the rails app whenever possible. That's why you should use env vars in the config/environments/development.rb file:

class Application < Rails::Application
  # Check if we use Docker to allow docker ip through web-console
  if ENV['DOCKERIZED'] == 'true'
    config.web_console.whitelisted_ips = ENV['DOCKER_HOST_IP']
  end
end

You should set correct env vars in a .env file, not tracked into version control.

In docker-compose.yml you can inject env vars from this file with env_file:

app:
  build: .
  ports:
   - "3000:3000"
  volumes:
    - .:/app
  links:
    - db
  environment:
    - DOCKERIZED=true
  env_file:
    - ".env"

Based on the feebdack received in comments, we can also build a solution without environment variables:

class Application < Rails::Application
  # Check if we use Docker to allow docker ip through web-console
  if File.file?('/.dockerenv') == true
    host_ip = `/sbin/ip route|awk '/default/ { print $3 }'`.strip
    config.web_console.whitelisted_ips << host_ip
  end
end

I'll leave the solutions with env var for learning purposes.

Solution 4:

Auto discovery within your config/development.rb

config.web_console.whitelisted_ips = Socket.ip_address_list.reduce([]) do |res, addrinfo|
    addrinfo.ipv4? ? res << IPAddr.new(addrinfo.ip_address).mask(24) : res
end

Of course might need to add

require 'socket'
require 'ipaddr'

Within your file.

Solution 5:

Anyone on any of my private networks is welcome.

I run in a docker container and I don't care which network it wants to use this week.

config/environments/development.rb add line

config.web_console.whitelisted_ips = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']