Class: TCellAgent::Rust::NativeAgent
- Inherits:
-
Object
- Object
- TCellAgent::Rust::NativeAgent
- Defined in:
- lib/tcell_agent/rust/native_agent.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#agent_ptr ⇒ Object
readonly
Returns the value of attribute agent_ptr.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_appfirewall(appsensor_meta) ⇒ Object
- #apply_cmdi(command, tcell_context) ⇒ Object
- #apply_patches(appsensor_meta) ⇒ Object
- #apply_suspicious_quick_check(appsensor_meta) ⇒ Object
- #check_http_redirect(location_header, from_domain, status_code, tcell_context) ⇒ Object
- #file_access_apply(file_path, mode, tcell_context) ⇒ Object
- #get_headers(content_type, tcell_context) ⇒ Object
- #get_js_agent_script_tag(tcell_context) ⇒ Object
-
#initialize(agent_ptr) ⇒ NativeAgent
constructor
A new instance of NativeAgent.
- #log_message(level, message, thread) ⇒ Object
- #log_response_errors(errors) ⇒ Object
- #login_fraud_apply(success, user_id, password, headers, user_valid, tcell_context) ⇒ Object
- #poll_new_policies ⇒ Object
- #report_metrics(request_time, tcell_context) ⇒ Object
- #send_sanitized_events(events) ⇒ Object
-
#update_policies(policies) ⇒ Object
NOTE: for tests.
Constructor Details
#initialize(agent_ptr) ⇒ NativeAgent
Returns a new instance of NativeAgent.
64 65 66 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 64 def initialize(agent_ptr) @agent_ptr = agent_ptr end |
Instance Attribute Details
#agent_ptr ⇒ Object (readonly)
Returns the value of attribute agent_ptr.
62 63 64 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 62 def agent_ptr @agent_ptr end |
Class Method Details
.create_agent(config) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 35 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
26 27 28 29 30 31 32 33 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 26 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
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 12 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 |
Instance Method Details
#apply_appfirewall(appsensor_meta) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 68 def apply_appfirewall() return {} unless post_params = Models.convert_params(.flattened_post_dict) query_params = Models.convert_params(.flattened_get_dict) header_params = Models.convert_params(.flattened_headers_dict) = Models.convert_params(.) path_params = Models.convert_params(.flattened_path_parameters) request_response_json = { :method => .method, :status_code => .response_code.to_i, :route_id => .route_id, :path => .path, :query_params => query_params, :post_params => post_params, :headers => header_params, :cookies => , :path_params => path_params, :reverse_proxy_header_value => .reverse_proxy_header_value, :remote_address => .remote_address, :full_uri => .location, :session_id => .session_id, :user_id => .user_id, :user_agent => .user_agent, :request_bytes_length => .request_content_bytes_len, :response_bytes_length => .response_content_bytes_len, :content_type => .content_type, :request_body => .raw_request_body } request_response_json[:sql_exceptions] = .sql_exceptions if .sql_exceptions request_response_json[:database_result_sizes] = .database_result_sizes if .database_result_sizes if TCellAgent::Utils::Strings.present?(.csrf_exception_name) request_response_json[:csrf_exception] = { :exception_name => .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
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 231 232 233 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 200 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, :reverse_proxy_header_value => tcell_context.reverse_proxy_header_value, :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
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 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 126 def apply_patches() return {} unless post_params = Models.convert_params(.flattened_post_dict) query_params = Models.convert_params(.flattened_get_dict) header_params = Models.convert_params(.flattened_headers_dict) = Models.convert_params(.) patches_request_json = { :method => .method, :path => .path, :remote_address => .remote_address, :reverse_proxy_header_value => .reverse_proxy_header_value, :request_bytes_length => .request_content_bytes_len, :query_params => query_params, :post_params => post_params, :headers => header_params, :cookies => , :content_type => .content_type, :full_uri => .location, :request_body => .raw_request_body } 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 |
#apply_suspicious_quick_check(appsensor_meta) ⇒ Object
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 198 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 169 def apply_suspicious_quick_check() return {} unless sus_quick_check_request_json = { :reverse_proxy_header_value => .reverse_proxy_header_value, :method => .method, :path => .path, :full_uri => .location, :request_bytes_length => .request_content_bytes_len } if .reverse_proxy_header_value.nil? sus_quick_check_request_json.merge( { :client_ip_override => .remote_address } ) end sus_quick_check_request_ptr = FFI::MemoryPointer.from_string( JSON.dump(sus_quick_check_request_json) ) # sus_quick_check_request_ptr.size - 1: strips null terminator TCellAgent::Rust::NativeLibrary.suspicious_quick_check_apply( FFI::Pointer.new(@agent_ptr), sus_quick_check_request_ptr, sus_quick_check_request_ptr.size - 1 ) end |
#check_http_redirect(location_header, from_domain, status_code, tcell_context) ⇒ Object
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 298 299 300 301 302 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 265 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, :reverse_proxy_header_value => tcell_context.reverse_proxy_header_value, :remote_addr => tcell_context.remote_address, :full_uri => tcell_context.uri, :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
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 408 409 410 411 412 413 414 415 416 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 373 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.uri, :reverse_proxy_header_value => tcell_context.reverse_proxy_header_value, :remote_address => tcell_context.remote_address, :route_id => tcell_context.route_id, :session_id => tcell_context.session_id, :user_id => tcell_context.user_id, :method => tcell_context.request_method, :request_path => tcell_context.path } ) 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(content_type, tcell_context) ⇒ Object
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 260 261 262 263 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 235 def get_headers(content_type, tcell_context) return unless tcell_context headers_request = { :content_type => content_type, :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
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 304 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
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 488 def (level, , thread) return unless level && = { :level => level, :message => , :thread => thread } = FFI::MemoryPointer.from_string( JSON.dump() ) buf = FFI::MemoryPointer.new(:uint8, 1024 * 8) # message_json_pointer.size - 1: strips null terminator TCellAgent::Rust::NativeLibrary.( FFI::Pointer.new(@agent_ptr), , .size - 1, buf, buf.size ) end |
#log_response_errors(errors) ⇒ Object
511 512 513 514 515 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 511 def log_response_errors(errors) errors.each do |error| ('error', error, self.class.name) end end |
#login_fraud_apply(success, user_id, password, headers, user_valid, tcell_context) ⇒ Object
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 366 367 368 369 370 371 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 331 def login_fraud_apply(success, user_id, password, headers, user_valid, tcell_context) event_name = success ? :Success : :Failure header_keys = TCellAgent::Utils::Headers.clean_keys(headers) login_info = { :event_name => event_name, :user_id => user_id, :user_agent => tcell_context.user_agent, :reverse_proxy_header_value => tcell_context.reverse_proxy_header_value, :remote_address => tcell_context.remote_address, :header_keys => header_keys, :password => password, :session_id => tcell_context.session_id, :full_uri => tcell_context.uri, :referrer => tcell_context.referrer, :user_valid => user_valid } login_info_pointer = FFI::MemoryPointer.from_string( JSON.dump(login_info) ) buf = FFI::MemoryPointer.new(:uint8, 1024 * 8) # login_info_pointer.size - 1: strips null terminator result_size = TCellAgent::Rust::NativeLibrary.login_fraud_apply( FFI::Pointer.new(@agent_ptr), login_info_pointer, login_info_pointer.size - 1, buf, buf.size ) response = NativeAgentResponse.new('login_fraud_apply', buf, result_size) log_response_errors(response.errors) response.response end |
#poll_new_policies ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 418 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
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 456 def report_metrics(request_time, tcell_context) return {} unless request_time = { :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, :reverse_proxy_header_value => tcell_context.reverse_proxy_header_value, :remote_address => tcell_context && tcell_context.remote_address } = FFI::MemoryPointer.from_string( JSON.dump() ) 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), , .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
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 432 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
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/tcell_agent/rust/native_agent.rb', line 518 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 |