Class: TCellAgent::Agent

Inherits:
Object
  • Object
show all
Includes:
ModuleLoggerAccess
Defined in:
lib/tcell_agent/agent.rb,
lib/tcell_agent/agent/route_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModuleLoggerAccess

#module_logger

Constructor Details

#initializeAgent

Returns a new instance of Agent.



29
30
31
32
33
34
# File 'lib/tcell_agent/agent.rb', line 29

def initialize
  @stop_agent = false
  @native_agent = nil
  @route_table = TCellAgent::Routes::RouteTable.new
  @policies_manager = PoliciesManager.new(nil)
end

Instance Attribute Details

#route_tableObject

Returns the value of attribute route_table.



26
27
28
# File 'lib/tcell_agent/agent.rb', line 26

def route_table
  @route_table
end

#stop_agentObject

Returns the value of attribute stop_agent.



26
27
28
# File 'lib/tcell_agent/agent.rb', line 26

def stop_agent
  @stop_agent
end

Class Method Details

.get_database_discovery_identifier(database, schema, table, fields) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/tcell_agent/agent/route_manager.rb', line 8

def self.get_database_discovery_identifier(database, schema, table, fields)
  [
    database,
    schema,
    table,
    fields.join(',')
  ].join(',').hash
end

Instance Method Details

#discover_database_fields(route_id, database, schema, table, fields) ⇒ Object



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/agent/route_manager.rb', line 17

def discover_database_fields(route_id, database, schema, table, fields)
  return if route_id.nil? || database.nil? || schema.nil? || table.nil? || fields.nil?

  query_hash = TCellAgent::Agent.get_database_discovery_identifier(database, schema, table, fields)

  return if @route_table.routes[route_id].database_queries_discovered.fetch(query_hash, false)

  @route_table.routes[route_id].database_queries_discovered[query_hash] = true
  event = TCellAgent::SensorEvents::DiscoveryEvent.new(route_id).for_database_fields(database, schema, table, fields)
  TCellAgent.send_event(event)
  # discovered_fields = fields.select { |field|
  #    @route_table.routes[route_id].database_queries_discoverd[database][schema][table][field].discovered != true
  # }
  # if (discovered_fields.length > 0)
  #  discovered_fields.each { |field|
  #  @route_table.routes[route_id].database[database][schema][table][field].discovered = true
  #  }
  #  event = (TCellAgent::SensorEvents::DiscoveryEvent.new(route_id)).for_database_fields(database, schema, table, fields)
  #  TCellAgent.send_event(event)
  # end
end

#instrument_auth_frameworksObject



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

def instrument_auth_frameworks
  if defined?(Devise) && TCellAgent.configuration.should_instrument?('devise')
    module_logger.info('Instrumenting Devise authentication framework')
    require 'tcell_agent/rails/auth/devise'
    require 'tcell_agent/rails/auth/devise_helper'
  end

  if defined?(Authlogic) && TCellAgent.configuration.should_instrument?('authlogic')
    module_logger.info('Instrumenting Authlogic authentication framework')
    require 'tcell_agent/rails/auth/authlogic'
    require 'tcell_agent/rails/auth/authlogic_helper'
  end

  if defined?(Doorkeeper) && TCellAgent.configuration.should_instrument?('doorkeeper') # rubocop:disable Style/GuardClause
    module_logger.info('Instrumenting Doorkeeper authentication framework')
    require 'tcell_agent/rails/auth/doorkeeper'
  end
end

#instrument_built_insObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tcell_agent/agent.rb', line 55

def instrument_built_ins
  require 'tcell_agent/instrumentation/cmdi'
  require 'tcell_agent/instrumentation/lfi'

  variant = if RUBY_VERSION.start_with?('3')
              'ruby_3'
            else
              'ruby_2'
            end

  if TCellAgent.configuration.should_instrument?('io')
    module_logger.info('Instrumenting Ruby Class: IO')
    require "tcell_agent/instrumentation/monkey_patches/#{variant}/io"
  end

  if TCellAgent.configuration.should_instrument?('file')
    module_logger.info('Instrumenting Ruby Class: File')
    require "tcell_agent/instrumentation/monkey_patches/#{variant}/file"
  end

  if TCellAgent.configuration.should_instrument?('kernel') # rubocop:disable Style/GuardClause
    module_logger.info('Instrumenting Ruby Module: Kernel')
    require "tcell_agent/instrumentation/monkey_patches/#{variant}/kernel"
  end
end

#manage_policiesObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tcell_agent/agent.rb', line 81

def manage_policies
  @policies_manager = PoliciesManager.new(@native_agent)

  result = {}
  unless TCellAgent.configuration.should_start_policy_poll?
    result = @native_agent.poll_new_policies
  end

  policies_and_enablements = result['new_policies_and_enablements'] || {}

  @policies_manager.process_policy_json(
    policies_and_enablements['enablements'],
    policies_and_enablements['policies']
  )

  @policy_polling = PolicyPolling.new(@policies_manager, @native_agent)
end

#policiesObject



99
100
101
# File 'lib/tcell_agent/agent.rb', line 99

def policies
  @policies_manager.policies
end

#queue_sensor_event(event) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/tcell_agent/agent.rb', line 103

def queue_sensor_event(event)
  return unless @native_agent

  @native_agent.send_sanitized_events(
    [event]
  )
rescue StandardError => e
  module_logger.error("Error sending event: (#{e.class}) #{e.message}")
  module_logger.exception(e)
end

#report_metrics(request_time, tcell_context) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/tcell_agent/agent.rb', line 114

def report_metrics(request_time, tcell_context)
  @native_agent.report_metrics(
    request_time, tcell_context
  )
rescue StandardError => e
  module_logger.error("Error reporting metric: (#{e.class}) #{e.message}")
  module_logger.exception(e)
end

#start(server_name) ⇒ Object



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
# File 'lib/tcell_agent/agent.rb', line 123

def start(server_name)
  @native_agent = TCellAgent::Rust::NativeAgent.create_agent(
    TCellAgent.initializer_configuration ||
    TCellAgent.configuration
  )

  if @native_agent.nil?
    TCellAgent.configuration.enabled = false
    return
  end

  TCellAgent.native_logger = @native_agent

  module_logger.info('Rails initializer overriding default agent configuration') unless TCellAgent.initializer_configuration.nil?

  instrument_auth_frameworks
  instrument_built_ins
  manage_policies

  module_logger.info("Started thread agent: #{server_name}")
  TCellAgent.report_settings
  TCellAgent::Instrumentation::Rails.send_settings
rescue StandardError => e
  TCellAgent.configuration.enabled = false
  module_logger.error("Error starting agent: (#{e.class}) #{e.message}")
  module_logger.exception(e)
end