Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.


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.


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.


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.


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']

For development environment: Detect if it's docker, then determine the IP address and whitelist it

# config/environments/development.rb
require 'socket'
require 'ipaddr'

Rails.application.configure do
  ...

  # When inside a docker container
  if File.file?('/.dockerenv')
    # Whitelist docker ip for web console
    # Cannot render console from 172.27.0.1! Allowed networks: 127.0.0.1
    Socket.ip_address_list.each do |addrinfo|
      next unless addrinfo.ipv4?
      next if addrinfo.ip_address == "127.0.0.1" # Already whitelisted

      ip = IPAddr.new(addrinfo.ip_address).mask(24)

      Logger.new(STDOUT).info "Adding #{ip.inspect} to config.web_console.whitelisted_ips"

      config.web_console.whitelisted_ips << ip
    end
  end
end

For me this prints the following and the warning goes away 🎉

Adding 172.27.0.0 to config.web_console.whitelisted_ips
Adding 172.18.0.0 to config.web_console.whitelisted_ips

My solution was to combine

  • the answer from user2481743 ⭐️ https://stackoverflow.com/a/42142563/2037928
  • the comment from jottr ⭐️ How to disable "Cannot Render Console from..." on Rails

For me, whitelisted_ips didn't seem to work in a new project. The Readme states the corresponding configuration entry is supposed to be permissions now:

Rails.application.configure do
  config.web_console.permissions = '192.168.0.0/16'
end

https://github.com/rails/web-console/blob/master/README.markdown