tcell_agent 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/bin/tcell_agent +10 -2
  3. data/lib/tcell_agent.rb +3 -3
  4. data/lib/tcell_agent/agent.rb +42 -52
  5. data/lib/tcell_agent/agent/event_processor.rb +129 -162
  6. data/lib/tcell_agent/agent/fork_pipe_manager.rb +57 -62
  7. data/lib/tcell_agent/agent/policy_manager.rb +83 -104
  8. data/lib/tcell_agent/agent/policy_types.rb +24 -29
  9. data/lib/tcell_agent/agent/route_manager.rb +36 -46
  10. data/lib/tcell_agent/agent/static_agent.rb +19 -21
  11. data/lib/tcell_agent/api.rb +23 -28
  12. data/lib/tcell_agent/appsensor/injections_reporter.rb +7 -11
  13. data/lib/tcell_agent/authlogic.rb +7 -7
  14. data/lib/tcell_agent/cmdi.rb +22 -23
  15. data/lib/tcell_agent/config/unknown_options.rb +71 -69
  16. data/lib/tcell_agent/configuration.rb +187 -191
  17. data/lib/tcell_agent/devise.rb +13 -15
  18. data/lib/tcell_agent/hooks/login_fraud.rb +1 -1
  19. data/lib/tcell_agent/instrumentation.rb +120 -124
  20. data/lib/tcell_agent/logger.rb +29 -45
  21. data/lib/tcell_agent/patches.rb +5 -5
  22. data/lib/tcell_agent/policies/dataloss_policy.rb +263 -288
  23. data/lib/tcell_agent/policies/http_redirect_policy.rb +25 -37
  24. data/lib/tcell_agent/policies/http_tx_policy.rb +48 -52
  25. data/lib/tcell_agent/policies/login_fraud_policy.rb +15 -20
  26. data/lib/tcell_agent/policies/policy.rb +0 -2
  27. data/lib/tcell_agent/policies/rust_policies.rb +24 -29
  28. data/lib/tcell_agent/rails.rb +2 -3
  29. data/lib/tcell_agent/rails/auth/authlogic.rb +2 -2
  30. data/lib/tcell_agent/rails/auth/devise.rb +2 -2
  31. data/lib/tcell_agent/rails/auth/doorkeeper.rb +2 -2
  32. data/lib/tcell_agent/rails/better_ip.rb +12 -16
  33. data/lib/tcell_agent/rails/csrf_exception.rb +4 -7
  34. data/lib/tcell_agent/rails/dlp.rb +208 -107
  35. data/lib/tcell_agent/rails/dlp/process_request.rb +37 -47
  36. data/lib/tcell_agent/rails/dlp_handler.rb +9 -11
  37. data/lib/tcell_agent/rails/js_agent_insert.rb +11 -14
  38. data/lib/tcell_agent/rails/middleware/body_filter_middleware.rb +8 -7
  39. data/lib/tcell_agent/rails/middleware/context_middleware.rb +4 -5
  40. data/lib/tcell_agent/rails/middleware/global_middleware.rb +5 -8
  41. data/lib/tcell_agent/rails/middleware/headers_middleware.rb +24 -27
  42. data/lib/tcell_agent/rails/on_start.rb +5 -5
  43. data/lib/tcell_agent/rails/responses.rb +7 -9
  44. data/lib/tcell_agent/rails/routes.rb +62 -81
  45. data/lib/tcell_agent/rails/routes/grape.rb +25 -30
  46. data/lib/tcell_agent/rails/routes/route_id.rb +9 -14
  47. data/lib/tcell_agent/rails/settings_reporter.rb +44 -33
  48. data/lib/tcell_agent/rails/tcell_body_proxy.rb +15 -18
  49. data/lib/tcell_agent/routes/table.rb +31 -33
  50. data/lib/tcell_agent/rust/{libtcellagent-1.3.0.dylib → libtcellagent-1.3.1.dylib} +0 -0
  51. data/lib/tcell_agent/rust/{libtcellagent-1.3.0.so → libtcellagent-1.3.1.so} +0 -0
  52. data/lib/tcell_agent/rust/{libtcellagent-alpine-1.3.0.so → libtcellagent-alpine-1.3.1.so} +0 -0
  53. data/lib/tcell_agent/rust/models.rb +32 -37
  54. data/lib/tcell_agent/rust/tcellagent-1.3.1.dll +0 -0
  55. data/lib/tcell_agent/rust/whisperer.rb +101 -104
  56. data/lib/tcell_agent/sensor_events/app_config.rb +7 -7
  57. data/lib/tcell_agent/sensor_events/appsensor_event.rb +26 -27
  58. data/lib/tcell_agent/sensor_events/appsensor_meta_event.rb +20 -88
  59. data/lib/tcell_agent/sensor_events/command_injection.rb +52 -80
  60. data/lib/tcell_agent/sensor_events/discovery.rb +27 -27
  61. data/lib/tcell_agent/sensor_events/dlp.rb +50 -56
  62. data/lib/tcell_agent/sensor_events/honeytokens.rb +9 -9
  63. data/lib/tcell_agent/sensor_events/metrics.rb +20 -21
  64. data/lib/tcell_agent/sensor_events/patches.rb +10 -12
  65. data/lib/tcell_agent/sensor_events/sensor.rb +32 -36
  66. data/lib/tcell_agent/sensor_events/server_agent.rb +130 -127
  67. data/lib/tcell_agent/sensor_events/util/sanitizer_utilities.rb +60 -80
  68. data/lib/tcell_agent/sensor_events/util/utils.rb +3 -5
  69. data/lib/tcell_agent/servers/passenger.rb +5 -9
  70. data/lib/tcell_agent/servers/puma.rb +18 -27
  71. data/lib/tcell_agent/servers/rails_server.rb +5 -9
  72. data/lib/tcell_agent/servers/thin.rb +2 -4
  73. data/lib/tcell_agent/servers/unicorn.rb +18 -27
  74. data/lib/tcell_agent/servers/webrick.rb +2 -4
  75. data/lib/tcell_agent/settings_reporter.rb +126 -0
  76. data/lib/tcell_agent/sinatra.rb +24 -26
  77. data/lib/tcell_agent/start_background_thread.rb +21 -142
  78. data/lib/tcell_agent/system_info.rb +4 -3
  79. data/lib/tcell_agent/tcell_context.rb +150 -0
  80. data/lib/tcell_agent/userinfo.rb +3 -3
  81. data/lib/tcell_agent/utils/io.rb +19 -24
  82. data/lib/tcell_agent/utils/params.rb +9 -15
  83. data/lib/tcell_agent/utils/queue_with_timeout.rb +26 -32
  84. data/lib/tcell_agent/utils/strings.rb +4 -6
  85. data/lib/tcell_agent/version.rb +1 -1
  86. data/spec/lib/tcell_agent/agent/policy_manager_spec.rb +5 -5
  87. data/spec/lib/tcell_agent/agent/static_agent_spec.rb +7 -7
  88. data/spec/lib/tcell_agent/cmdi_spec.rb +21 -21
  89. data/spec/lib/tcell_agent/hooks/login_fraud_spec.rb +29 -24
  90. data/spec/lib/tcell_agent/instrumentation_spec.rb +4 -4
  91. data/spec/lib/tcell_agent/patches_spec.rb +8 -8
  92. data/spec/lib/tcell_agent/policies/appsensor_policy_spec.rb +23 -23
  93. data/spec/lib/tcell_agent/policies/patches_policy_spec.rb +2 -2
  94. data/spec/lib/tcell_agent/rails/csrf_exception_spec.rb +69 -0
  95. data/spec/lib/tcell_agent/rails/dlp_spec.rb +1039 -0
  96. data/spec/lib/tcell_agent/rails/js_agent_insert_spec.rb +271 -0
  97. data/spec/lib/tcell_agent/rails/logger_spec.rb +5 -5
  98. data/spec/lib/tcell_agent/rails/middleware/appsensor_middleware_spec.rb +3 -3
  99. data/spec/lib/tcell_agent/rails/middleware/dlp_middleware_spec.rb +4 -4
  100. data/spec/lib/tcell_agent/rails/middleware/global_middleware_spec.rb +5 -5
  101. data/spec/lib/tcell_agent/rails/middleware/redirect_middleware_spec.rb +1 -1
  102. data/spec/lib/tcell_agent/rails/middleware/tcell_body_proxy_spec.rb +11 -8
  103. data/spec/lib/tcell_agent/rails/responses_spec.rb +2 -2
  104. data/spec/lib/tcell_agent/rails/routes/grape_spec.rb +2 -2
  105. data/spec/lib/tcell_agent/rails/routes/route_id_spec.rb +1 -1
  106. data/spec/lib/tcell_agent/rails/routes/routes_spec.rb +4 -4
  107. data/spec/lib/tcell_agent/rust/models_spec.rb +83 -75
  108. data/spec/lib/tcell_agent/rust/whisperer_spec.rb +14 -14
  109. data/spec/lib/tcell_agent/sensor_events/appsensor_meta_event_spec.rb +19 -70
  110. data/spec/lib/tcell_agent/sensor_events/sessions_metric_spec.rb +1 -1
  111. data/spec/lib/tcell_agent/settings_reporter_spec.rb +162 -0
  112. data/spec/lib/tcell_agent/tcell_context_spec.rb +154 -0
  113. data/spec/spec_helper.rb +5 -0
  114. metadata +18 -10
  115. data/lib/tcell_agent/appsensor/meta_data.rb +0 -132
  116. data/lib/tcell_agent/patches/meta_data.rb +0 -59
  117. data/lib/tcell_agent/rust/tcellagent-1.3.0.dll +0 -0
  118. data/spec/lib/tcell_agent/appsensor/meta_data_spec.rb +0 -71
@@ -1,12 +1,10 @@
1
1
  Rack::Handler::WEBrick.class_eval do
2
-
3
2
  class << self
4
3
  alias_method :original_run, :run
5
- def run(app, options={})
6
- TCellAgent.run_instrumentation("WEBrick")
4
+ def run(app, options = {})
5
+ TCellAgent.run_instrumentation('WEBrick')
7
6
 
8
7
  original_run(app, options)
9
8
  end
10
9
  end
11
-
12
10
  end
@@ -0,0 +1,126 @@
1
+ require 'tcell_agent/agent'
2
+ require 'tcell_agent/configuration'
3
+ require 'tcell_agent/sensor_events/app_config'
4
+ require 'tcell_agent/sensor_events/server_agent'
5
+ require 'thread'
6
+
7
+ module TCellAgent
8
+ def self.report_settings(send_startup_events)
9
+ return unless send_startup_events && TCellAgent.configuration.should_instrument?
10
+
11
+ Thread.new do
12
+ TCellAgent::Instrumentation.safe_block('Instrumenting Agent Details') do
13
+ event = TCellAgent::SensorEvents::ServerAgentDetailsSensorEvent.new
14
+ TCellAgent.send_event(event)
15
+ end
16
+
17
+ TCellAgent::Instrumentation.safe_block('Instrumenting Server Packages') do
18
+ event = TCellAgent::SensorEvents::ServerAgentPackagesSensorEvent.new
19
+ TCellAgent.send_event(event)
20
+ end
21
+
22
+ TCellAgent::Instrumentation.safe_block('Instrumenting Native Lib Status') do
23
+ require 'tcell_agent/rust/whisperer'
24
+
25
+ TCellAgent.send_event(
26
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
27
+ 'native_lib_loaded',
28
+ TCellAgent::Rust::Wrapper.common_lib_available?.to_s
29
+ )
30
+ )
31
+ end
32
+
33
+ TCellAgent::Instrumentation.safe_block('Instrumenting Initial Config') do
34
+ TCellAgent.send_event(
35
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
36
+ 'allow_payloads',
37
+ (!!TCellAgent.configuration.allow_payloads).to_s # rubocop:disable Style/DoubleNegation
38
+ )
39
+ )
40
+
41
+ TCellAgent.send_event(
42
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
43
+ 'reverse_proxy',
44
+ (!!TCellAgent.configuration.reverse_proxy).to_s # rubocop:disable Style/DoubleNegation
45
+ )
46
+ )
47
+
48
+ # Because of all the diff ways to initialize the agent
49
+ # some some of the following vars might not be set until
50
+ # we call this method, so call this method to set all
51
+ # the variables
52
+ TCellAgent.configuration.log_filename
53
+
54
+ TCellAgent.send_event(
55
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
56
+ 'config_filename',
57
+ TCellAgent.configuration.config_filename
58
+ )
59
+ )
60
+ TCellAgent.send_event(
61
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
62
+ 'logging_directory',
63
+ TCellAgent.configuration.agent_log_dir
64
+ )
65
+ )
66
+
67
+ TCellAgent.send_event(
68
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
69
+ 'agent_home_directory',
70
+ TCellAgent.configuration.agent_home_dir
71
+ )
72
+ )
73
+
74
+ TCellAgent.send_event(
75
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
76
+ 'agent_home_owner',
77
+ TCellAgent.configuration.agent_home_owner
78
+ )
79
+ )
80
+
81
+ logging_options = TCellAgent.configuration.logging_options || {}
82
+ use_default_setting = !logging_options.key?(:enabled) && !logging_options.key?('enabled')
83
+ if use_default_setting || logging_options[:enabled] || logging_options['enabled']
84
+ TCellAgent.send_event(
85
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new('logging_enabled', 'true')
86
+ )
87
+
88
+ TCellAgent.send_event(
89
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
90
+ 'logging_level',
91
+ logging_options[:level] || logging_options['level'] || 'INFO'
92
+ )
93
+ )
94
+ else
95
+ TCellAgent.send_event(
96
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new('logging_enabled', 'false')
97
+ )
98
+ end
99
+
100
+ if TCellAgent.configuration.hmac_key
101
+ TCellAgent.send_event(
102
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
103
+ 'hmac_key_present',
104
+ (!!TCellAgent.configuration.hmac_key).to_s # rubocop:disable Style/DoubleNegation
105
+ )
106
+ )
107
+ end
108
+
109
+ if TCellAgent.configuration.reverse_proxy
110
+ TCellAgent.send_event(
111
+ TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
112
+ 'reverse_proxy_ip_address_header',
113
+ TCellAgent.configuration.reverse_proxy_ip_address_header
114
+ )
115
+ )
116
+ end
117
+ end
118
+
119
+ if defined?(::Rails)
120
+ TCellAgent::Instrumentation.safe_block('Instrumenting routes') do
121
+ TCellAgent::Instrumentation::Rails.instrument_routes
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -6,36 +6,34 @@ require 'tcell_agent/agent'
6
6
  require 'tcell_agent/instrumentation'
7
7
 
8
8
  module TCellAgent
9
- class Sinatra::Response
10
- include Sinatra
9
+ class Sinatra::Response # rubocop:disable Style/ClassAndModuleChildren
10
+ include Sinatra
11
11
 
12
- alias_method :original_finish, :finish
13
- def finish
14
- status, headers, response = original_finish
12
+ alias_method :original_finish, :finish
13
+ def finish
14
+ status, headers, response = original_finish
15
15
 
16
- TCellAgent::Instrumentation.safe_block("Setting Headers") do
17
- rust_policies = TCellAgent.policy(TCellAgent::PolicyTypes::Rust)
18
- if rust_policies
19
- policy_headers = rust_policies.get_headers(
20
- request.env[TCellAgent::Instrumentation::TCELL_ID]
21
- )
22
- policy_headers.each do |header_info|
23
- header_name = header_info['name']
24
- header_value = header_info['value']
25
- existing_header_value = headers[header_name]
26
- if existing_header_value
27
- headers[header_name] = "#{existing_header_value}, #{header_value}"
28
- else
29
- headers[header_name] = header_value
30
- end
31
- end
32
- response = [status, headers, active_response]
16
+ TCellAgent::Instrumentation.safe_block('Setting Headers') do
17
+ rust_policies = TCellAgent.policy(TCellAgent::PolicyTypes::RUST)
18
+ if rust_policies
19
+ policy_headers = rust_policies.get_headers(
20
+ request.env[TCellAgent::Instrumentation::TCELL_ID]
21
+ )
22
+ policy_headers.each do |header_info|
23
+ header_name = header_info['name']
24
+ header_value = header_info['value']
25
+ existing_header_value = headers[header_name]
26
+ headers[header_name] = if existing_header_value
27
+ "#{existing_header_value}, #{header_value}"
28
+ else
29
+ header_value
30
+ end
33
31
  end
32
+ response = [status, headers, active_response]
34
33
  end
35
-
36
- [status, headers, response]
37
34
  end
35
+
36
+ [status, headers, response]
38
37
  end
38
+ end
39
39
  end
40
-
41
-
@@ -1,176 +1,55 @@
1
1
  # See the file "LICENSE" for the full license governing this code.
2
2
  require 'tcell_agent/configuration'
3
3
 
4
- if (TCellAgent.configuration.disable_all == false)
4
+ unless TCellAgent.configuration.disable_all
5
5
  require 'tcell_agent/logger'
6
6
  require 'tcell_agent/agent'
7
7
  require 'thread'
8
+
8
9
  module TCellAgent
9
- #require 'tcell_agent/sinatra' if defined?(Sinatra)
10
+ # require 'tcell_agent/sinatra' if defined?(Sinatra)
10
11
  require 'tcell_agent/rails' if defined?(Rails)
11
12
  require 'tcell_agent/cmdi'
12
13
 
13
- def self.run_instrumentation(server_name, send_startup_events=true)
14
-
14
+ def self.run_instrumentation(server_name, send_startup_events = true)
15
15
  require 'tcell_agent/hooks/login_fraud'
16
16
  require 'tcell_agent/rails/on_start' if defined?(Rails)
17
+ require 'tcell_agent/settings_reporter'
17
18
 
18
- TCellAgent::Instrumentation.safe_block("Starting thread agent") do
19
+ TCellAgent::Instrumentation.safe_block('Starting thread agent') do
19
20
  TCellAgent.logger.debug("Instrumenting: #{server_name}")
20
21
  TCellAgent.thread_agent.start
21
22
  end
22
23
 
23
- if send_startup_events && TCellAgent.configuration.should_instrument?
24
- Thread.new do
25
-
26
- TCellAgent::Instrumentation.safe_block("Instrumenting Agent Details") do
27
- event = TCellAgent::SensorEvents::ServerAgentDetailsSensorEvent.new
28
- TCellAgent.send_event(event)
29
- end
30
-
31
- TCellAgent::Instrumentation.safe_block("Instrumenting Server Packages") do
32
- event = TCellAgent::SensorEvents::ServerAgentPackagesSensorEvent.new
33
- TCellAgent.send_event(event)
34
- end
35
-
36
- TCellAgent::Instrumentation.safe_block("Instrumenting Initial Config") do
37
- require 'tcell_agent/rust/whisperer'
38
-
39
- TCellAgent.send_event(
40
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
41
- "native_lib_loaded",
42
- TCellAgent::Rust::Wrapper.common_lib_available?.to_s)
43
- )
44
-
45
- TCellAgent.send_event(
46
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
47
- "allow_payloads",
48
- (!!TCellAgent.configuration.allow_payloads).to_s)
49
- )
50
-
51
- TCellAgent.send_event(
52
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
53
- "reverse_proxy",
54
- (!!TCellAgent.configuration.reverse_proxy).to_s)
55
- )
56
-
57
- # Because of all the diff ways to initialize the agent
58
- # some some of the following vars might not be set until
59
- # we call this method, so call this method to set all
60
- # the variables
61
- TCellAgent.configuration.log_filename
62
-
63
- TCellAgent.send_event(
64
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
65
- "config_filename",
66
- TCellAgent.configuration.config_filename)
67
- )
68
- TCellAgent.send_event(
69
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
70
- "logging_directory",
71
- TCellAgent.configuration.agent_log_dir)
72
- )
73
-
74
- TCellAgent.send_event(
75
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
76
- "agent_home_directory",
77
- TCellAgent.configuration.agent_home_dir)
78
- )
79
-
80
- TCellAgent.send_event(
81
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
82
- "agent_home_owner",
83
- TCellAgent.configuration.agent_home_owner)
84
- )
85
-
86
- logging_options = TCellAgent.configuration.logging_options || {}
87
- use_default_setting = !logging_options.has_key?(:enabled) && !logging_options.has_key?("enabled")
88
- if use_default_setting || logging_options[:enabled] || logging_options["enabled"]
89
- TCellAgent.send_event(
90
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new("logging_enabled", "true")
91
- )
92
-
93
- TCellAgent.send_event(
94
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
95
- "logging_level",
96
- logging_options[:level] || logging_options["level"] || "INFO"
97
- )
98
- )
99
-
100
- # Deprecated: this is so we can ensure no one is using this setting so we can remove
101
- # the code altogether in the future
102
- if logging_options.has_key?(:log_dir) || logging_options.has_key?("log_dir")
103
- TCellAgent.send_event(
104
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
105
- "deprecated_log_dir",
106
- logging_options[:log_dir] || logging_options["log_dir"]
107
- )
108
- )
109
- end
110
- else
111
- TCellAgent.send_event(
112
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new("logging_enabled", "false")
113
- )
114
- end
115
-
116
- if TCellAgent.configuration.hmac_key
117
- TCellAgent.send_event(
118
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
119
- "hmac_key_present",
120
- (!!TCellAgent.configuration.hmac_key).to_s
121
- )
122
- )
123
- end
124
-
125
- if TCellAgent.configuration.reverse_proxy
126
- TCellAgent.send_event(
127
- TCellAgent::SensorEvents::TCellAgentSettingEvent.new(
128
- "reverse_proxy_ip_address_header",
129
- TCellAgent.configuration.reverse_proxy_ip_address_header)
130
- )
131
- end
132
- end
133
-
134
- if defined?(Rails)
135
- TCellAgent::Instrumentation.safe_block("Instrumenting routes") do
136
- TCellAgent::Instrumentation::Rails.instrument_routes
137
- end
138
- end
139
- end
140
- end
141
-
24
+ report_settings(send_startup_events)
142
25
  end
143
-
144
26
  end
145
27
 
146
- tcell_server = ENV["TCELL_AGENT_SERVER"]
28
+ tcell_server = ENV['TCELL_AGENT_SERVER']
147
29
 
148
30
  if TCellAgent.configuration.should_instrument?
149
- if (!(tcell_server && tcell_server == "mock"))
31
+ unless tcell_server && tcell_server == 'mock'
150
32
 
151
- if (tcell_server && tcell_server == "webrick") || defined?(Rails::Server)
152
- require("tcell_agent/servers/rails_server")
33
+ if (tcell_server && tcell_server == 'webrick') || defined?(Rails::Server)
34
+ require('tcell_agent/servers/rails_server')
153
35
 
154
- elsif (tcell_server && tcell_server == "thin") || defined?(Thin)
155
- require("tcell_agent/servers/thin")
36
+ elsif (tcell_server && tcell_server == 'thin') || defined?(Thin)
37
+ require('tcell_agent/servers/thin')
156
38
 
157
- elsif (tcell_server && tcell_server == "puma") || defined?(Puma)
158
- require("tcell_agent/servers/puma")
39
+ elsif (tcell_server && tcell_server == 'puma') || defined?(Puma)
40
+ require('tcell_agent/servers/puma')
159
41
 
160
- elsif (tcell_server && tcell_server == "unicorn") || defined?(Unicorn)
161
- require("tcell_agent/servers/unicorn")
42
+ elsif (tcell_server && tcell_server == 'unicorn') || defined?(Unicorn)
43
+ require('tcell_agent/servers/unicorn')
162
44
 
163
- elsif (tcell_server && tcell_server == "passenger") || defined?(PhusionPassenger)
164
- require("tcell_agent/servers/passenger")
45
+ elsif (tcell_server && tcell_server == 'passenger') || defined?(PhusionPassenger)
46
+ require('tcell_agent/servers/passenger')
165
47
 
166
48
  end
167
49
  end
168
50
 
169
- else
170
-
51
+ elsif (tcell_server && tcell_server == 'unicorn') || defined?(Unicorn)
171
52
  # unicorn is always instrumented to support rolling restarts
172
- if (tcell_server && tcell_server == "unicorn") || defined?(Unicorn)
173
- require("tcell_agent/servers/unicorn")
174
- end
53
+ require('tcell_agent/servers/unicorn')
175
54
  end
176
55
  end
@@ -1,10 +1,11 @@
1
1
  module TCellAgent
2
2
  module SystemInfo
3
3
  def self.get_language_version
4
- return RUBY_VERSION
4
+ RUBY_VERSION
5
5
  end
6
+
6
7
  def self.get_language
7
- return "Ruby"
8
+ 'Ruby'
8
9
  end
9
10
  end
10
- end
11
+ end
@@ -0,0 +1,150 @@
1
+ require 'tcell_agent/logger'
2
+ require 'tcell_agent/utils/params'
3
+
4
+ # TODO(ralba): move TCellData from instrumentation.rb here
5
+ # and merge both models into one and drop usage of MetaData.
6
+ # TCellData needs to be serializable in order to do that
7
+ module TCellAgent
8
+ class MetaData
9
+ class << self
10
+ def from_request(request)
11
+ tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID]
12
+ # use uri stored in tcell_context because
13
+ # rails modifies original request.url
14
+ # to always return /404 (or whatever error code
15
+ # it encountered)
16
+ meta_event = MetaData.new(
17
+ tcell_context.request_method,
18
+ tcell_context.ip_address,
19
+ tcell_context.route_id,
20
+ tcell_context.hmac_session_id,
21
+ tcell_context.user_id,
22
+ tcell_context.transaction_id,
23
+ tcell_context.uri
24
+ )
25
+ meta_event.path = tcell_context.path
26
+
27
+ meta_event.set_parameter_dicts(request)
28
+
29
+ meta_event
30
+ end
31
+ end
32
+
33
+ attr_accessor :remote_address,
34
+ :method,
35
+ :route_id,
36
+ :session_id,
37
+ :user_id,
38
+ :transaction_id,
39
+ :location,
40
+ :path,
41
+ :request_content_bytes_len,
42
+ :response_content_bytes_len,
43
+ :response_code,
44
+ :user_agent,
45
+ :response_headers,
46
+ :csrf_exception_name,
47
+ :sql_exceptions,
48
+ :database_result_sizes
49
+
50
+ attr_reader :flattened_get_dict,
51
+ :flattened_cookie_dict,
52
+ :flattened_headers_dict,
53
+ :flattened_post_dict,
54
+ :flattened_body_dict,
55
+ :flattened_path_parameters
56
+
57
+ def initialize(method,
58
+ remote_address,
59
+ route_id,
60
+ session_id,
61
+ user_id,
62
+ transaction_id,
63
+ location)
64
+ @method = method
65
+ @remote_address = remote_address
66
+ @route_id = route_id
67
+ @session_id = session_id
68
+ @user_id = user_id
69
+ @transaction_id = transaction_id
70
+ @location = location
71
+ @path = path
72
+
73
+ @flattened_get_dict = {}
74
+ @flattened_cookie_dict = {}
75
+ @flattened_path_parameters = {}
76
+
77
+ # POST/body dict can contain files, so always flatten it
78
+ # to get rid of those and to make this model serializable
79
+ @flattened_post_dict = {}
80
+ @flattened_body_dict = {}
81
+
82
+ @request_content_bytes_len = 0
83
+ @response_content_bytes_len = 0
84
+ @user_agent = nil
85
+ end
86
+
87
+ def get_dict=(value)
88
+ @flattened_get_dict = TCellAgent::Utils::Params.flatten(value)
89
+ end
90
+
91
+ def cookie_dict=(value)
92
+ @flattened_cookie_dict = TCellAgent::Utils::Params.flatten(value)
93
+ end
94
+
95
+ def headers_dict=(value)
96
+ headers_dict = value.select do |k, _v|
97
+ header_downcased = k.downcase
98
+ (header_downcased != 'http_cookie' && header_downcased.start_with?('http_')) ||
99
+ %w[content_type content_length].include?(header_downcased)
100
+ end
101
+
102
+ headers_dict = headers_dict.each_with_object({}) do |(k, v), memo|
103
+ memo[k.downcase.sub(/^http_/, '').tr('_', '-')] = v
104
+ end
105
+ @flattened_headers_dict = TCellAgent::Utils::Params.flatten(headers_dict)
106
+ end
107
+
108
+ def post_dict=(value)
109
+ @flattened_post_dict = TCellAgent::Utils::Params.flatten(value)
110
+ end
111
+
112
+ def path_parameters=(value)
113
+ @flattened_path_parameters = TCellAgent::Utils::Params.flatten(value)
114
+ end
115
+
116
+ def set_parameter_dicts(request)
117
+ self.get_dict = request.GET
118
+ self.cookie_dict = request.cookies
119
+ self.post_dict = request.POST
120
+ self.headers_dict = request.env
121
+
122
+ # Positions strio to the beginning of input, resetting lineno to zero.
123
+ # rails 4.1 seems to read the stringIO directly and so body.gets is empty
124
+ # this is called
125
+ request.body.rewind
126
+
127
+ @request_content_bytes_len = (request.content_length || 0).to_i
128
+ set_body_dict(
129
+ @request_content_bytes_len,
130
+ request.content_type,
131
+ request.body.gets
132
+ )
133
+ end
134
+
135
+ def set_body_dict(request_content_bytes_len, request_content_type, request_body)
136
+ @flattened_body_dict = {}
137
+
138
+ return if request_content_bytes_len > 2_000_000
139
+
140
+ return unless request_body && (request_content_type =~ %r{application/json}i)
141
+
142
+ begin
143
+ # don't enqueue parameter values of unknown type to avoid any serialization issues
144
+ @flattened_body_dict = TCellAgent::Utils::Params.flatten(JSON.parse(request_body))
145
+ rescue JSON::ParserError
146
+ TCellAgent.logger.debug('JSON body parameter parsing failed')
147
+ end
148
+ end
149
+ end
150
+ end