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) ⇒ MetaData

Returns a new instance of MetaData.



82
83
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
# File 'lib/tcell_agent/tcell_context.rb', line 82

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

  @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.



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

def content_type
  @content_type
end

#csrf_exception_nameObject

Returns the value of attribute csrf_exception_name.



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

def csrf_exception_name
  @csrf_exception_name
end

#database_result_sizesObject

Returns the value of attribute database_result_sizes.



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

def database_result_sizes
  @database_result_sizes
end

#flattened_body_dictObject (readonly)

Returns the value of attribute flattened_body_dict.



75
76
77
# File 'lib/tcell_agent/tcell_context.rb', line 75

def flattened_body_dict
  @flattened_body_dict
end

Returns the value of attribute flattened_cookie_dict.



75
76
77
# File 'lib/tcell_agent/tcell_context.rb', line 75

def flattened_cookie_dict
  @flattened_cookie_dict
end

#flattened_get_dictObject (readonly)

Returns the value of attribute flattened_get_dict.



75
76
77
# File 'lib/tcell_agent/tcell_context.rb', line 75

def flattened_get_dict
  @flattened_get_dict
end

#flattened_headers_dictObject (readonly)

Returns the value of attribute flattened_headers_dict.



75
76
77
# File 'lib/tcell_agent/tcell_context.rb', line 75

def flattened_headers_dict
  @flattened_headers_dict
end

#flattened_path_parametersObject (readonly)

Returns the value of attribute flattened_path_parameters.



75
76
77
# File 'lib/tcell_agent/tcell_context.rb', line 75

def flattened_path_parameters
  @flattened_path_parameters
end

#flattened_post_dictObject (readonly)

Returns the value of attribute flattened_post_dict.



75
76
77
# File 'lib/tcell_agent/tcell_context.rb', line 75

def flattened_post_dict
  @flattened_post_dict
end

#locationObject

Returns the value of attribute location.



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

def location
  @location
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#raw_request_bodyObject

Returns the value of attribute raw_request_body.



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

def raw_request_body
  @raw_request_body
end

#remote_addressObject

Returns the value of attribute remote_address.



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

def remote_address
  @remote_address
end

#request_content_bytes_lenObject

Returns the value of attribute request_content_bytes_len.



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

def request_content_bytes_len
  @request_content_bytes_len
end

#response_codeObject

Returns the value of attribute response_code.



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

def response_code
  @response_code
end

#response_content_bytes_lenObject

Returns the value of attribute response_content_bytes_len.



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

def response_content_bytes_len
  @response_content_bytes_len
end

#response_headersObject

Returns the value of attribute response_headers.



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

def response_headers
  @response_headers
end

#route_idObject

Returns the value of attribute route_id.



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

def route_id
  @route_id
end

#session_idObject

Returns the value of attribute session_id.



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

def session_id
  @session_id
end

#sql_exceptionsObject

Returns the value of attribute sql_exceptions.



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

def sql_exceptions
  @sql_exceptions
end

#transaction_idObject

Returns the value of attribute transaction_id.



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

def transaction_id
  @transaction_id
end

#user_agentObject

Returns the value of attribute user_agent.



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

def user_agent
  @user_agent
end

#user_idObject

Returns the value of attribute user_id.



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

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
# 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.hmac_session_id,
    tcell_context.user_id,
    tcell_context.transaction_id,
    tcell_context.uri
  )
  .path = tcell_context.path

  .set_parameter_dicts(request)

  
end

Instance Method Details

#charsetObject



140
141
142
143
144
# File 'lib/tcell_agent/tcell_context.rb', line 140

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


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

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

#get_dict=(value) ⇒ Object



112
113
114
# File 'lib/tcell_agent/tcell_context.rb', line 112

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

#get_raw_post_data(request) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/tcell_agent/tcell_context.rb', line 146

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



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/tcell_agent/tcell_context.rb', line 120

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



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

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

#post_dict=(value) ⇒ Object



132
133
134
# File 'lib/tcell_agent/tcell_context.rb', line 132

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

#set_parameter_dicts(request) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tcell_agent/tcell_context.rb', line 164

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')
                     {}
                   else
                     request.POST
                   end

  self.headers_dict = request.env
end