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.



113
114
115
# File 'lib/tcell_agent/rust/native_agent.rb', line 113

def initialize(agent_ptr)
  @agent_ptr = agent_ptr
end

Instance Attribute Details

#agent_ptrObject (readonly)

Returns the value of attribute agent_ptr.



111
112
113
# File 'lib/tcell_agent/rust/native_agent.rb', line 111

def agent_ptr
  @agent_ptr
end

Class Method Details

.create_agent(config) ⇒ Object



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
# File 'lib/tcell_agent/rust/native_agent.rb', line 84

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

  return unless response['config'] && response['agent_enabled']

  TCellAgent.configuration.populate_configuration(response['config'])
  NativeAgent.new(response['agent_ptr'])
end

.free_agent(agent_ptr) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/tcell_agent/rust/native_agent.rb', line 75

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_agent(config) ⇒ Object



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

def self.test_agent(config)
  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
  TCellAgent::Rust::NativeLibrary.test_agent(
    config_pointer, config_pointer.size - 1, buf, buf.size
  )
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



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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/tcell_agent/rust/native_agent.rb', line 117

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/tcell_agent/rust/native_agent.rb', line 216

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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/tcell_agent/rust/native_agent.rb', line 175

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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/tcell_agent/rust/native_agent.rb', line 279

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



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/tcell_agent/rust/native_agent.rb', line 385

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



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/tcell_agent/rust/native_agent.rb', line 250

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



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/tcell_agent/rust/native_agent.rb', line 317

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



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/tcell_agent/rust/native_agent.rb', line 496

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



519
520
521
522
523
# File 'lib/tcell_agent/rust/native_agent.rb', line 519

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/tcell_agent/rust/native_agent.rb', line 344

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



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/tcell_agent/rust/native_agent.rb', line 427

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



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/tcell_agent/rust/native_agent.rb', line 465

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



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/tcell_agent/rust/native_agent.rb', line 441

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



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/tcell_agent/rust/native_agent.rb', line 526

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