Class: TCellAgent::MetaData

Inherits:
Object
  • Object
show all
Defined in:
lib/tcell_agent/tcell_context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, remote_address, route_id, session_id, user_id, transaction_id, location, reverse_proxy_header_value) ⇒ MetaData

Returns a new instance of MetaData.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tcell_agent/tcell_context.rb', line 84

def initialize(method,
               remote_address,
               route_id,
               session_id,
               user_id,
               transaction_id,
               location,
               reverse_proxy_header_value)
  @method = method
  @remote_address = remote_address
  @route_id = route_id
  @session_id = session_id
  @user_id = user_id
  @transaction_id = transaction_id
  @location = location
  @reverse_proxy_header_value = reverse_proxy_header_value

  @flattened_get_dict = {}
  @flattened_cookie_dict = {}
  @flattened_path_parameters = {}

  # POST/body dict can contain files, so always flatten it
  # to get rid of those and to make this model serializable
  @flattened_post_dict = {}
  @flattened_body_dict = {}

  @request_content_bytes_len = 0
  @response_content_bytes_len = 0
  @user_agent = nil
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def content_type
  @content_type
end

#csrf_exception_nameObject

Returns the value of attribute csrf_exception_name.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def csrf_exception_name
  @csrf_exception_name
end

#database_result_sizesObject

Returns the value of attribute database_result_sizes.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def database_result_sizes
  @database_result_sizes
end

#flattened_body_dictObject (readonly)

Returns the value of attribute flattened_body_dict.



77
78
79
# File 'lib/tcell_agent/tcell_context.rb', line 77

def flattened_body_dict
  @flattened_body_dict
end

Returns the value of attribute flattened_cookie_dict.



77
78
79
# File 'lib/tcell_agent/tcell_context.rb', line 77

def flattened_cookie_dict
  @flattened_cookie_dict
end

#flattened_get_dictObject (readonly)

Returns the value of attribute flattened_get_dict.



77
78
79
# File 'lib/tcell_agent/tcell_context.rb', line 77

def flattened_get_dict
  @flattened_get_dict
end

#flattened_headers_dictObject (readonly)

Returns the value of attribute flattened_headers_dict.



77
78
79
# File 'lib/tcell_agent/tcell_context.rb', line 77

def flattened_headers_dict
  @flattened_headers_dict
end

#flattened_path_parametersObject (readonly)

Returns the value of attribute flattened_path_parameters.



77
78
79
# File 'lib/tcell_agent/tcell_context.rb', line 77

def flattened_path_parameters
  @flattened_path_parameters
end

#flattened_post_dictObject (readonly)

Returns the value of attribute flattened_post_dict.



77
78
79
# File 'lib/tcell_agent/tcell_context.rb', line 77

def flattened_post_dict
  @flattened_post_dict
end

#locationObject

Returns the value of attribute location.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def location
  @location
end

#methodObject

Returns the value of attribute method.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def method
  @method
end

#pathObject

Returns the value of attribute path.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def path
  @path
end

#raw_request_bodyObject

Returns the value of attribute raw_request_body.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def raw_request_body
  @raw_request_body
end

#remote_addressObject

Returns the value of attribute remote_address.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def remote_address
  @remote_address
end

#request_content_bytes_lenObject

Returns the value of attribute request_content_bytes_len.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def request_content_bytes_len
  @request_content_bytes_len
end

#response_codeObject

Returns the value of attribute response_code.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def response_code
  @response_code
end

#response_content_bytes_lenObject

Returns the value of attribute response_content_bytes_len.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def response_content_bytes_len
  @response_content_bytes_len
end

#response_headersObject

Returns the value of attribute response_headers.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def response_headers
  @response_headers
end

#reverse_proxy_header_valueObject

Returns the value of attribute reverse_proxy_header_value.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def reverse_proxy_header_value
  @reverse_proxy_header_value
end

#route_idObject

Returns the value of attribute route_id.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def route_id
  @route_id
end

#session_idObject

Returns the value of attribute session_id.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def session_id
  @session_id
end

#sql_exceptionsObject

Returns the value of attribute sql_exceptions.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def sql_exceptions
  @sql_exceptions
end

#transaction_idObject

Returns the value of attribute transaction_id.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def transaction_id
  @transaction_id
end

#user_agentObject

Returns the value of attribute user_agent.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def user_agent
  @user_agent
end

#user_idObject

Returns the value of attribute user_id.



57
58
59
# File 'lib/tcell_agent/tcell_context.rb', line 57

def user_id
  @user_id
end

Class Method Details

.for_appfirewall(request, response_content_length, response_code, response_headers) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tcell_agent/tcell_context.rb', line 15

def for_appfirewall(request, response_content_length, response_code, response_headers)
   = TCellAgent::MetaData.for_patches(request)

  tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID]
  .csrf_exception_name = tcell_context.csrf_exception_name
  .user_agent = tcell_context.user_agent
  .path_parameters = tcell_context.path_parameters
  .sql_exceptions = tcell_context.sql_exceptions
  .database_result_sizes = tcell_context.database_result_sizes

  .response_content_bytes_len = response_content_length

  .response_code = response_code
  .response_headers = response_headers

  
end

.for_patches(request) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tcell_agent/tcell_context.rb', line 33

def for_patches(request)
  tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID]
  # use uri stored in tcell_context because
  # rails modifies original request.url
  # to always return /404 (or whatever error code
  # it encountered)
   = MetaData.new(
    tcell_context.request_method,
    tcell_context.remote_address,
    tcell_context.route_id,
    tcell_context.session_id,
    tcell_context.user_id,
    tcell_context.transaction_id,
    tcell_context.uri,
    tcell_context.reverse_proxy_header_value
  )
  .path = tcell_context.path

  .set_parameter_dicts(request)

  
end

Instance Method Details

#charsetObject



143
144
145
146
147
# File 'lib/tcell_agent/tcell_context.rb', line 143

def charset
  Rack::MediaType.params(@content_type)['charset'] || Encoding.default_external
rescue StandardError
  Encoding.default_external
end


119
120
121
# File 'lib/tcell_agent/tcell_context.rb', line 119

def cookie_dict=(value)
  @flattened_cookie_dict = TCellAgent::Utils::Params.flatten(value)
end

#get_dict=(value) ⇒ Object



115
116
117
# File 'lib/tcell_agent/tcell_context.rb', line 115

def get_dict=(value)
  @flattened_get_dict = TCellAgent::Utils::Params.flatten(value)
end

#get_raw_post_data(request) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/tcell_agent/tcell_context.rb', line 149

def get_raw_post_data(request)
  content_length = request.content_length.to_i if request.content_length
  if !content_length.nil? && content_length > TCELL_MAX_BODY_LENGTH || request.content_type.nil?
    return nil
  end

  raw_post_data = nil
  # Positions strio to the beginning of input, resetting lineno to zero.
  # rails 4.1 seems to read the stringIO directly and so body.gets is empty
  # this is called

  body = request.body
  body.rewind if body.respond_to?(:rewind)
  raw_post_data = body.read(request.content_length.to_i) if request.content_length
  body.rewind if body.respond_to?(:rewind)

  raw_post_data.force_encoding(charset) unless raw_post_data.nil?
end

#headers_dict=(value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/tcell_agent/tcell_context.rb', line 123

def headers_dict=(value)
  headers_dict = value.select do |header_key, _v|
    (header_key != 'HTTP_COOKIE' && header_key.start_with?('HTTP_')) ||
      %w[CONTENT_TYPE CONTENT_LENGTH].include?(header_key)
  end

  headers_dict = headers_dict.each_with_object({}) do |(k, v), memo|
    memo[k.sub(/^HTTP_/, '').tr('_', '-').downcase] = v
  end
  @flattened_headers_dict = TCellAgent::Utils::Params.flatten(headers_dict)
end

#path_parameters=(value) ⇒ Object



139
140
141
# File 'lib/tcell_agent/tcell_context.rb', line 139

def path_parameters=(value)
  @flattened_path_parameters = TCellAgent::Utils::Params.flatten(value)
end

#post_dict=(value) ⇒ Object



135
136
137
# File 'lib/tcell_agent/tcell_context.rb', line 135

def post_dict=(value)
  @flattened_post_dict = TCellAgent::Utils::Params.flatten(value)
end

#set_parameter_dicts(request) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/tcell_agent/tcell_context.rb', line 168

def set_parameter_dicts(request)
  @flattened_body_dict = {} # deprecated
  @content_type = request.content_type || ''
  @raw_request_body = get_raw_post_data(request) unless @content_type.start_with?('application/octet-stream',
                                                                                  'multipart/form-data')
  @request_content_bytes_len = (request.content_length || 0).to_i

  self.get_dict = request.GET
  self.cookie_dict = request.cookies

  self.post_dict = if @content_type.start_with?('application/json', 'application/xml') ||
                      (@content_type.start_with?('multipart/form-data') && @request_content_bytes_len == 0)
                     {}
                   else
                     begin
                       request.POST
                     rescue EOFError
                       {}
                     end
                   end

  self.headers_dict = request.env
end