Class: Immunio::EnvironmentReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/immunio/plugins/environment_reporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ EnvironmentReporter

Returns a new instance of EnvironmentReporter.



8
9
10
11
12
13
# File 'lib/immunio/plugins/environment_reporter.rb', line 8

def initialize(app)
  @app = app

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

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/immunio/plugins/environment_reporter.rb', line 15

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

#hostnameObject



50
51
52
# File 'lib/immunio/plugins/environment_reporter.rb', line 50

def hostname
  Socket.gethostname rescue SocketError
end

#hostname_ipObject



54
55
56
57
58
# File 'lib/immunio/plugins/environment_reporter.rb', line 54

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

#ipsObject



45
46
47
48
# File 'lib/immunio/plugins/environment_reporter.rb', line 45

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

#pluginsObject



60
61
62
# File 'lib/immunio/plugins/environment_reporter.rb', line 60

def plugins
  Immunio::Plugin.registered
end

#reportObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/immunio/plugins/environment_reporter.rb', line 64

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

#runtime_nameObject



26
27
28
29
30
31
32
33
34
# File 'lib/immunio/plugins/environment_reporter.rb', line 26

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

#runtime_versionObject



36
37
38
39
40
41
42
43
# File 'lib/immunio/plugins/environment_reporter.rb', line 36

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