Sha256: 9b172e496f9116926c8c0f6b5774d70ca214d5ca63d646befb35e683a6ffe5a7

Contents?: true

Size: 1.99 KB

Versions: 12

Compression:

Stored size: 1.99 KB

Contents

# Report environment data on the first request.
#
# XXX: Completely untested outside of MRI (official Ruby runtime) 1.9.3 and up!

require 'socket'
module Immunio
  class EnvironmentReporter
    def initialize(app)
      @app = app

      # No need for mutex, reporting environment is idempotent
      @reported = false
    end

    def call(env)
      response = @app.call(env)

      # Report gem usage at the end so that everything as been loaded.
      Request.time "plugin", "#{Module.nesting[0]}::#{__method__}" do
        report if !@reported
      end

      response
    end

    def runtime_name
      if Object.const_defined?(:RUBY_DESCRIPTION) && RUBY_DESCRIPTION =~ /Enterprise/
        'ree'
      elsif Object.const_defined? :RUBY_ENGINE
        RUBY_ENGINE.to_s
      else
        'unknown'
      end
    end

    def runtime_version
      case runtime_name
        when 'ruby' then RUBY_VERSION
        when 'jruby' then JRUBY_VERSION
        when 'rbx' then Rubinius::VERSION
        else 'unknown'
      end
    end

    def ips
      ips = Socket.ip_address_list.map(&:ip_address)
      ips.reject { |ip| ['::1', '127.0.0.1'].include?(ip) }
    end

    def hostname
      Socket.gethostname rescue SocketError
    end

    def hostname_ip
      # Return nil if we can't fetch the hostname with `Socket.gethostname`,
      # for example if there is no public IPV4 interface.
      Addrinfo.getaddrinfo(hostname, nil).first.ip_address rescue nil
    end

    def plugins
      Immunio::Plugin.registered
    end

    def report
      @reported = true

      info = {
        runtime: {
          name: runtime_name,
          version: runtime_version
        },
        language: {
          name: 'ruby',
          version: RUBY_VERSION
        },
        platform: {
          description: RUBY_PLATFORM
        },
        host: {
          hostname: hostname,
          hostname_ip: hostname_ip,
          ips: ips
        },
        plugins: plugins
      }

      Immunio.agent.environment = info
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
immunio-2.0.4 lib/immunio/plugins/environment_reporter.rb
immunio-2.0.3 lib/immunio/plugins/environment_reporter.rb
immunio-2.0.2 lib/immunio/plugins/environment_reporter.rb
immunio-1.2.1 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.19 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.18 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.16 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.15 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.13 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.11 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.10 lib/immunio/plugins/environment_reporter.rb
immunio-1.1.7 lib/immunio/plugins/environment_reporter.rb