Class: TCellAgent::Rust::NativeAgent

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

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_ptr) ⇒ NativeAgent

Returns a new instance of NativeAgent.



96
97
98
# File 'lib/tcell_agent/rust/native_agent.rb', line 96

def initialize(agent_ptr)
  @agent_ptr = agent_ptr
end

Instance Attribute Details

#agent_ptrObject (readonly)

Returns the value of attribute agent_ptr.



94
95
96
# File 'lib/tcell_agent/rust/native_agent.rb', line 94

def agent_ptr
  @agent_ptr
end

Class Method Details

.create_agent(config) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tcell_agent/rust/native_agent.rb', line 70

def self.create_agent(config)
  return nil unless TCellAgent::Rust::NativeLibrary.common_lib_available?

  agent_config = TCellAgent::Rust::AgentConfig.new(config)
  config_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(agent_config)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # config_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.create_agent(
    config_pointer, config_pointer.size - 1, buf, buf.size
  )

  response = JSON.parse(buf.get_string(0, result_size))
  if response['error']
    logger = TCellAgent::ModuleLogger.new(TCellAgent::RubyLogger.new, name)
    logger.error("Error creating native agent: #{response['error']}")
    return nil
  end

  NativeAgent.new(response['agent_ptr'])
end

.free_agent(agent_ptr) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/tcell_agent/rust/native_agent.rb', line 61

def self.free_agent(agent_ptr)
  if TCellAgent::Rust::NativeLibrary.common_lib_available? &&
     agent_ptr
    TCellAgent::Rust::NativeLibrary.free_agent(
      FFI::Pointer.new(agent_ptr)
    )
  end
end

.test_event_sender(events) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tcell_agent/rust/native_agent.rb', line 12

def self.test_event_sender(events)
  config = TCellAgent.configuration
  event_sender = {
    :uuid =>            config.uuid,
    :hostname =>        config.host_identifier,
    :agent_type =>      'Ruby',
    :agent_version =>   TCellAgent::VERSION,
    :app_id =>          config.app_id,
    :api_key =>         config.api_key,
    :tcell_input_url => config.tcell_input_url,
    :events =>          events
  }
  event_sender_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(event_sender)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # config_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.test_event_sender(
    event_sender_pointer, event_sender_pointer.size - 1, buf, buf.size
  )

  response = NativeAgentResponse.new('test_event_sender', buf, result_size)

  response.errors
end

.test_policiesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tcell_agent/rust/native_agent.rb', line 39

def self.test_policies
  config = TCellAgent.configuration
  policies_info = {
    :app_id =>        config.app_id,
    :api_key =>       config.api_key,
    :tcell_api_url => config.tcell_api_url
  }
  policies_info_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(policies_info)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # config_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.test_policies(
    policies_info_pointer, policies_info_pointer.size - 1, buf, buf.size
  )

  response = NativeAgentResponse.new('test_event_sender', buf, result_size)

  response.errors
end

Instance Method Details

#apply_appfirewall(appsensor_meta) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/tcell_agent/rust/native_agent.rb', line 100

def apply_appfirewall(appsensor_meta)
  return {} unless appsensor_meta

  post_params = Models.convert_params(appsensor_meta.flattened_post_dict)
  query_params = Models.convert_params(appsensor_meta.flattened_get_dict)
  header_params = Models.convert_params(appsensor_meta.flattened_headers_dict)
  cookie_params = Models.convert_params(appsensor_meta.flattened_cookie_dict)
  path_params = Models.convert_params(appsensor_meta.flattened_path_parameters)

  request_response_json = {
    :method => appsensor_meta.method,
    :status_code => appsensor_meta.response_code.to_i,
    :route_id => appsensor_meta.route_id,
    :path => appsensor_meta.path,
    :query_params => query_params,
    :post_params => post_params,
    :headers => header_params,
    :cookies => cookie_params,
    :path_params => path_params,
    :remote_address => appsensor_meta.remote_address,
    :full_uri => appsensor_meta.location,
    :session_id => appsensor_meta.session_id,
    :user_id => appsensor_meta.user_id,
    :user_agent => appsensor_meta.user_agent,
    :request_bytes_length => appsensor_meta.request_content_bytes_len,
    :response_bytes_length => appsensor_meta.response_content_bytes_len,
    :content_type => appsensor_meta.content_type,
    :request_body => appsensor_meta.raw_request_body
  }

  request_response_json[:sql_exceptions] = appsensor_meta.sql_exceptions if appsensor_meta.sql_exceptions
  request_response_json[:database_result_sizes] = appsensor_meta.database_result_sizes if appsensor_meta.database_result_sizes

  if TCellAgent::Utils::Strings.present?(appsensor_meta.csrf_exception_name)
    request_response_json[:csrf_exception] = {
      :exception_name => appsensor_meta.csrf_exception_name
    }
  end

  request_response_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(request_response_json)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # request_response_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.appfirewall_apply(
    FFI::Pointer.new(@agent_ptr),
    request_response_pointer,
    request_response_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('apply_appfirewall', buf, result_size)
  log_response_errors(response.errors)
  response.response
end

#apply_cmdi(command, tcell_context) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/tcell_agent/rust/native_agent.rb', line 199

def apply_cmdi(command, tcell_context)
  return unless TCellAgent::Utils::Strings.present?(command)

  command_info = {
    :command => command,
    :method => tcell_context.request_method,
    :path => tcell_context.path,
    :remote_address => tcell_context.remote_address,
    :route_id => tcell_context.route_id,
    :session_id => tcell_context.session_id,
    :user_id => tcell_context.user_id,
    :full_uri => tcell_context.uri
  }
  command_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(command_info)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # command_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.cmdi_apply(
    FFI::Pointer.new(@agent_ptr),
    command_pointer,
    command_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('apply_cmdi', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#apply_patches(appsensor_meta) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/tcell_agent/rust/native_agent.rb', line 158

def apply_patches(appsensor_meta)
  return {} unless appsensor_meta

  post_params = Models.convert_params(appsensor_meta.flattened_post_dict)
  query_params = Models.convert_params(appsensor_meta.flattened_get_dict)
  header_params = Models.convert_params(appsensor_meta.flattened_headers_dict)
  cookie_params = Models.convert_params(appsensor_meta.flattened_cookie_dict)

  patches_request_json = {
    :method => appsensor_meta.method,
    :path => appsensor_meta.path,
    :remote_address => appsensor_meta.remote_address,
    :request_bytes_length => appsensor_meta.request_content_bytes_len,
    :query_params => query_params,
    :post_params =>  post_params,
    :headers => header_params,
    :cookies => cookie_params,
    :content_type => appsensor_meta.content_type,
    :full_uri => appsensor_meta.location
  }

  patches_request_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(patches_request_json)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # patches_request_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.patches_apply(
    FFI::Pointer.new(@agent_ptr),
    patches_request_pointer,
    patches_request_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('apply_patches', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#check_http_redirect(location_header, from_domain, status_code, tcell_context) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/tcell_agent/rust/native_agent.rb', line 261

def check_http_redirect(location_header,
                        from_domain,
                        status_code,
                        tcell_context)
  return {} unless tcell_context

  http_redirect_request = {
    :location_header => location_header,
    :local_server => from_domain,
    :status_code => status_code,
    :method => tcell_context.request_method,
    :path => tcell_context.path,
    :remote_addr => tcell_context.remote_address,
    :full_uri => tcell_context.fullpath,
    :route_id => tcell_context.route_id,
    :session_id => tcell_context.session_id,
    :user_id => tcell_context.user_id
  }
  http_redirect_request_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(http_redirect_request)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # http_redirect_request_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.check_http_redirect(
    FFI::Pointer.new(@agent_ptr),
    http_redirect_request_pointer,
    http_redirect_request_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('check_http_redirect', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#file_access_apply(file_path, mode, tcell_context) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/tcell_agent/rust/native_agent.rb', line 367

def file_access_apply(file_path,
                      mode,
                      tcell_context)

  file_access_info = {
    :dir_classification => 'Unknown',
    :file_path => file_path,
    :mode => mode
  }

  if tcell_context
    file_access_info = file_access_info.merge(
      {
        :full_uri => tcell_context.fullpath,
        :remote_address => tcell_context.remote_address,
        :route_id => tcell_context.route_id,
        :session_id => tcell_context.session_id,
        :user_id => tcell_context.user_id
      }
    )
  end

  file_access_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(file_access_info)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # login_info_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.file_access_apply(
    FFI::Pointer.new(@agent_ptr),
    file_access_pointer,
    file_access_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('file_access_apply', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#get_headers(tcell_context) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/tcell_agent/rust/native_agent.rb', line 232

def get_headers(tcell_context)
  return unless tcell_context

  headers_request = {
    :method => tcell_context.request_method,
    :path => tcell_context.path,
    :route_id => tcell_context.route_id.to_s,
    :session_id => tcell_context.session_id.to_s
  }
  headers_request_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(headers_request)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 16)
  # headers_request_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.get_headers(
    FFI::Pointer.new(@agent_ptr),
    headers_request_pointer,
    headers_request_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('get_headers', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#get_js_agent_script_tag(tcell_context) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/tcell_agent/rust/native_agent.rb', line 299

def get_js_agent_script_tag(tcell_context)
  return {} unless tcell_context

  jsagent_request = {
    :method => tcell_context.request_method,
    :path => tcell_context.path
  }
  jsagent_request_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(jsagent_request)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # jsagent_request_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.get_js_agent_script_tag(
    FFI::Pointer.new(@agent_ptr),
    jsagent_request_pointer,
    jsagent_request_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('get_js_agent_script_tag', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#log_message(level, message, thread) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/tcell_agent/rust/native_agent.rb', line 478

def log_message(level, message, thread)
  return unless level && message

  message_json = {
    :level => level,
    :message => message,
    :thread => thread
  }
  message_json_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(message_json)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # message_json_pointer.size - 1: strips null terminator
  TCellAgent::Rust::NativeLibrary.log_message(
    FFI::Pointer.new(@agent_ptr),
    message_json_pointer,
    message_json_pointer.size - 1,
    buf,
    buf.size
  )
end

#log_response_errors(errors) ⇒ Object



501
502
503
504
505
# File 'lib/tcell_agent/rust/native_agent.rb', line 501

def log_response_errors(errors)
  errors.each do |error|
    log_message('error', error, self.class.name)
  end
end

#login_fraud_apply(success, user_id, password, headers, user_valid, tcell_context) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/tcell_agent/rust/native_agent.rb', line 326

def (success,
                      user_id,
                      password,
                      headers,
                      user_valid,
                      tcell_context)
  event_name =  success ? :Success : :Failure
  header_keys = TCellAgent::Utils::Headers.clean_keys(headers)
   = {
    :event_name => event_name,
    :user_id => user_id,
    :user_agent => tcell_context.user_agent,
    :remote_address => tcell_context.remote_address,
    :header_keys => header_keys,
    :passsword => password,
    :session_id => tcell_context.session_id,
    :full_uri => tcell_context.fullpath,
    :referrer => tcell_context.referrer,
    :user_valid => user_valid
  }

   = FFI::MemoryPointer.from_string(
    JSON.dump()
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # login_info_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.(
    FFI::Pointer.new(@agent_ptr),
    ,
    .size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('login_fraud_apply', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#poll_new_policiesObject



409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/tcell_agent/rust/native_agent.rb', line 409

def poll_new_policies
  buf = FFI::MemoryPointer.new(:uint8, 1024 * 32)
  result_size = TCellAgent::Rust::NativeLibrary.poll_new_policies(
    FFI::Pointer.new(@agent_ptr),
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('poll_new_policies', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#report_metrics(request_time, tcell_context) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/tcell_agent/rust/native_agent.rb', line 447

def report_metrics(request_time, tcell_context)
  return {} unless request_time

  message = {
    :elapsed_time => request_time,
    :route_id => tcell_context && tcell_context.route_id,
    :session_id => tcell_context && tcell_context.session_id,
    :user_id => tcell_context && tcell_context.user_id,
    :user_agent => tcell_context && tcell_context.user_agent,
    :remote_address => tcell_context && tcell_context.remote_address
  }
  message_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(message)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # message_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.report_metrics(
    FFI::Pointer.new(@agent_ptr),
    message_pointer,
    message_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('report_metrics', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#send_sanitized_events(events) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/tcell_agent/rust/native_agent.rb', line 423

def send_sanitized_events(events)
  return {} unless events

  events = { :events => events }
  events_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(events)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # events_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.send_sanitized_events(
    FFI::Pointer.new(@agent_ptr),
    events_pointer,
    events_pointer.size - 1,
    buf,
    buf.size
  )

  response = NativeAgentResponse.new('send_sanitized_events', buf, result_size)
  log_response_errors(response.errors)

  response.response
end

#update_policies(policies) ⇒ Object

Note: for tests



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/tcell_agent/rust/native_agent.rb', line 508

def update_policies(policies)
  return {} unless TCellAgent::Utils::Strings.present?(policies)

  policies_pointer = FFI::MemoryPointer.from_string(
    JSON.dump(policies)
  )

  buf = FFI::MemoryPointer.new(:uint8, 1024 * 8)
  # policies_pointer.size - 1: strips null terminator
  result_size = TCellAgent::Rust::NativeLibrary.update_policies(
    FFI::Pointer.new(agent_ptr),
    policies_pointer,
    policies_pointer.size - 1,
    buf,
    buf.size
  )

  NativeAgentResponse.new(
    'update_policies', buf, result_size
  ).response
end