How to disable “Cannot Render Console from…” on Rails

后端 未结 11 1282
心在旅途
心在旅途 2020-12-07 10:35

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

Started GET \"/assets/home-fcec5b5a277ac7c20cc9f45a209         


        
相关标签:
11条回答
  • 2020-12-07 10:42

    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.

    0 讨论(0)
  • 2020-12-07 10:42

    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.

    0 讨论(0)
  • 2020-12-07 10:49

    You need to whitelist 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.whitelisted_ips = '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.

    0 讨论(0)
  • 2020-12-07 10:53

    If you are using Docker most likely you don't want neither to introduce new ENV variables nor to hardcode your specific IP address.

    Instead you may want to check that you are in Docker using /proc/1/cgroup, and to allow your host IP (both for web_console and better_errors). Add to your config/environments/development.rb

      # https://stackoverflow.com/a/20012536/4862360
      if File.read('/proc/1/cgroup').include?('docker')
        # https://stackoverflow.com/a/24716645/4862360
        host_ip = `/sbin/ip route|awk '/default/ { print $3 }'`.strip
    
        BetterErrors::Middleware.allow_ip!(host_ip) if defined?(BetterErrors::Middleware)
        config.web_console.whitelisted_ips << host_ip
      end
    
    0 讨论(0)
  • 2020-12-07 10:56

    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

    0 讨论(0)
  • 2020-12-07 10:58

    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.

    0 讨论(0)
提交回复
热议问题