Class: NewRelic::Security::Agent::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/newrelic_security/agent/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/newrelic_security/agent/agent.rb', line 31

def initialize
  NewRelic::Security::Agent.config
  create_agent_home
  NewRelic::Security::Agent::Utils.enable_object_space_in_jruby
  NewRelic::Security::Agent.config.save_uuid
  @started = false
  @event_subscriber = NewRelic::Security::Agent::Control::EventSubscriber.new
  @started = true
  @route_map = ::Set.new
  @http_request_count = NewRelic::Security::Agent::Control::EventCounter.new
  @event_processed_count = NewRelic::Security::Agent::Control::EventCounter.new
  @event_sent_count = NewRelic::Security::Agent::Control::EventCounter.new
  @event_drop_count = NewRelic::Security::Agent::Control::EventCounter.new
  @iast_event_stats = NewRelic::Security::Agent::Control::EventStats.new
  @rasp_event_stats = NewRelic::Security::Agent::Control::EventStats.new
  @exit_event_stats = NewRelic::Security::Agent::Control::EventStats.new
  @error_reporting = NewRelic::Security::Agent::Control::ErrorReporting.new
  @scan_scheduler = NewRelic::Security::Agent::Control::ScanScheduler.new
end

Instance Attribute Details

#error_reportingObject

Returns the value of attribute error_reporting.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def error_reporting
  @error_reporting
end

#event_drop_countObject

Returns the value of attribute event_drop_count.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def event_drop_count
  @event_drop_count
end

#event_processed_countObject

Returns the value of attribute event_processed_count.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def event_processed_count
  @event_processed_count
end

#event_processorObject

Returns the value of attribute event_processor.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def event_processor
  @event_processor
end

#event_sent_countObject

Returns the value of attribute event_sent_count.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def event_sent_count
  @event_sent_count
end

#exit_event_statsObject

Returns the value of attribute exit_event_stats.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def exit_event_stats
  @exit_event_stats
end

#http_request_countObject

Returns the value of attribute http_request_count.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def http_request_count
  @http_request_count
end

#iast_clientObject

Returns the value of attribute iast_client.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def iast_client
  @iast_client
end

#iast_event_statsObject

Returns the value of attribute iast_event_stats.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def iast_event_stats
  @iast_event_stats
end

#rasp_event_statsObject

Returns the value of attribute rasp_event_stats.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def rasp_event_stats
  @rasp_event_stats
end

#route_mapObject

Returns the value of attribute route_map.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def route_map
  @route_map
end

#scan_schedulerObject

Returns the value of attribute scan_scheduler.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def scan_scheduler
  @scan_scheduler
end

#websocket_clientObject

Returns the value of attribute websocket_client.



29
30
31
# File 'lib/newrelic_security/agent/agent.rb', line 29

def websocket_client
  @websocket_client
end

Class Method Details

.configObject



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

def self.config
  ::NewRelic::Security::Agent.config
end

Instance Method Details

#add_instrumentationObject



103
104
105
# File 'lib/newrelic_security/agent/agent.rb', line 103

def add_instrumentation
  
end

#create_agent_homeObject



107
108
109
110
111
112
# File 'lib/newrelic_security/agent/agent.rb', line 107

def create_agent_home
  log_dir = ::File.join(NewRelic::Security::Agent.config[:log_file_path], SEC_HOME_PATH, LOGS_DIR)
  find_or_create_file_path(log_dir)
  tmp_dir = ::File.join(NewRelic::Security::Agent.config[:log_file_path], SEC_HOME_PATH, TMP_DIR)
  find_or_create_file_path(tmp_dir)
end

#find_or_create_file_path(path) ⇒ Object



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

def find_or_create_file_path(path)
  ::FileUtils.mkdir_p(path) unless ::File.directory?(path)
  ::File.directory?(path)
rescue => e
  ::NewRelic::Agent.notice_error(e)
  return false
end

#initObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/newrelic_security/agent/agent.rb', line 51

def init
  NewRelic::Security::Agent.logger.info "Initializing Security Agent with config : #{NewRelic::Security::Agent::Utils.filtered_log(NewRelic::Security::Agent.config.inspect)}\n"
  @ready = false
  start_event_processor
  start_websocket_client
  NewRelic::Security::Instrumentation::InstrumentationLoader.add_instrumentation()
  NewRelic::Security::Agent.logger.info "Security Agent ready.\n"
  NewRelic::Security::Agent.init_logger.info "Security Agent ready.\n"
  @ready = true
rescue Exception => exception
  NewRelic::Security::Agent.logger.error "Exception in security agent init: #{exception.inspect} #{exception.backtrace}\n"
end

#reconnect(sleep_time = 15) ⇒ Object



122
123
124
125
126
127
# File 'lib/newrelic_security/agent/agent.rb', line 122

def reconnect(sleep_time = 15)
  NewRelic::Security::Agent::Control::WebsocketClient.instance.close(false) if NewRelic::Security::Agent::Control::WebsocketClient.instance.is_open?
  NewRelic::Security::Agent.logger.info "Trying to reconnect to websocket connection in #{sleep_time} sec..."
  sleep sleep_time
  NewRelic::Security::Agent.agent.start_websocket_client
end

#shutdown_security_agentObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/newrelic_security/agent/agent.rb', line 64

def shutdown_security_agent
  NewRelic::Security::Agent.logger.info "Flushing eventQ (#{NewRelic::Security::Agent.agent.event_processor.eventQ.size} events) and closing websocket connection"
  NewRelic::Security::Agent.agent.event_processor&.eventQ&.clear
  @iast_client&.fuzzQ&.clear
  @iast_client&.completed_requests&.clear
  @iast_client&.pending_request_ids&.clear
  @iast_client&.iast_data_transfer_request_processor_thread&.kill
  NewRelic::Security::Agent.config.disable_security
  stop_websocket_client_if_open
end

#start_event_processorObject



84
85
86
87
88
89
# File 'lib/newrelic_security/agent/agent.rb', line 84

def start_event_processor
  @event_processor&.event_dequeue_threads&.each { |t| t&.kill }
  @event_processor&.healthcheck_thread&.kill
  @event_processor = nil
  @event_processor = NewRelic::Security::Agent::Control::EventProcessor.new
end

#start_iast_clientObject



91
92
93
94
95
96
97
# File 'lib/newrelic_security/agent/agent.rb', line 91

def start_iast_client
  @iast_client&.iast_dequeue_threads&.each { |t| t&.kill }
  @iast_client&.iast_data_transfer_request_processor_thread&.kill
  @iast_client = nil
  NewRelic::Security::Agent.logger.info "Starting IAST client now at current time: #{Time.now}"
  @iast_client = NewRelic::Security::Agent::Control::IASTClient.new
end

#start_websocket_clientObject



75
76
77
78
# File 'lib/newrelic_security/agent/agent.rb', line 75

def start_websocket_client
  stop_websocket_client_if_open
  @websocket_client = NewRelic::Security::Agent::Control::WebsocketClient.instance.connect
end

#stop_websocket_client_if_openObject



80
81
82
# File 'lib/newrelic_security/agent/agent.rb', line 80

def stop_websocket_client_if_open
  NewRelic::Security::Agent::Control::WebsocketClient.instance.close(false) if NewRelic::Security::Agent::Control::WebsocketClient.instance.is_open?
end