Class: Contrast::Agent::Reporting::ResponseHandler

Inherits:
Object
  • Object
show all
Includes:
ResponseHandlerUtils, Components::Logger::InstanceMethods
Defined in:
lib/contrast/agent/reporting/reporting_utilities/response_handler.rb

Overview

This class will facilitate the Response capture and analysis functionality.

Constant Summary collapse

TIMEOUT =

15 min

900.cs__freeze

Constants included from ResponseHandlerUtils

Contrast::Agent::Reporting::ResponseHandlerUtils::APP_NON_EXISTENT_MSG, Contrast::Agent::Reporting::ResponseHandlerUtils::ERROR_CODES, Contrast::Agent::Reporting::ResponseHandlerUtils::FORBIDDEN_MSG, Contrast::Agent::Reporting::ResponseHandlerUtils::FORBIDDEN_NO_ACTION_MSG, Contrast::Agent::Reporting::ResponseHandlerUtils::RETRY_AFTER_MSG, Contrast::Agent::Reporting::ResponseHandlerUtils::SUSPEND_MSG, Contrast::Agent::Reporting::ResponseHandlerUtils::UNPROCESSABLE_ENTITY_MSG, Contrast::Agent::Reporting::ResponseHandlerUtils::UNSUCCESSFULLY_RECEIVED_MSG

Instance Method Summary collapse

Methods included from ResponseHandlerUtils

#last_application_modified, #last_response_code, #last_server_modified, #stop_reporting

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Instance Method Details

#modeObject



63
64
65
# File 'lib/contrast/agent/reporting/reporting_utilities/response_handler.rb', line 63

def mode
  @_mode ||= Contrast::Agent::Reporting::ResponseHandlerMode.new
end

#process(response, event) ⇒ Object

Process the response from TS

Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/contrast/agent/reporting/reporting_utilities/response_handler.rb', line 26

def process response, event
  logger.debug('[Reporter] Received a response')
  return if event&.cs__is_a?(Contrast::Agent::Reporting::Finding)
  return unless analyze_response?(response)

  # Handle the response body and obtain server_features or app_settings
  report_response = convert_response(response, event)
  return unless report_response

  # Update Server Features and Application Settings to provide current agent settings
  update_agent_settings(report_response)
  # Process any reactions, including message logging and shutting down
  update_reaction(report_response)
  update_ruleset(report_response)
  logger.trace('Agent settings updated in response to TeamServer', protect_on: ::Contrast::PROTECT.enabled?,
                                                                   assess_on: ::Contrast::ASSESS.enabled?)
  response
rescue StandardError => e
  logger.error('[Reporter] Unable to process response from TeamServer', e)
  nil
end

#put_to_sleepObject

Puts the reporting service to sleep



68
69
70
# File 'lib/contrast/agent/reporting/reporting_utilities/response_handler.rb', line 68

def put_to_sleep
  @_sleep = true
end

#sleep?Boolean

If sleep is true puts reporting service to sleep.

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/contrast/agent/reporting/reporting_utilities/response_handler.rb', line 49

def sleep?
  @_sleep = wake_up if @_sleep.nil?
  @_sleep
end

#suspend_reporting(message, timeout, error_message, event: nil, log_error: true, startup: false) ⇒ Object

Suspend the reporter and try again in time. If not set the timeout is set to 15 min As default:

Parameters:

  • message (String)

    Message to log.

  • timeout (Integer, nil)

    The timeout to wait and retry after.

  • error_message (String, nil)

    Error message if any received.

  • event (Contrast::Agent::Reporting::ReportingEvent, nil) (defaults to: nil)

    The event sent to TeamServer if set

  • log_error (Boolean) (defaults to: true)

    Whether to log the error or not.

  • startup (Boolean) (defaults to: false)

    Whether this is a startup error or not.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/contrast/agent/reporting/reporting_utilities/response_handler.rb', line 86

def suspend_reporting message, timeout, error_message, event: nil, log_error: true, startup: false
  @_timeout = timeout || Contrast::Agent::Reporting::ResponseHandler::TIMEOUT
  if log_error
    log_error_msg(message,
                  timeout: @_timeout,
                  error_message: error_message || 'none',
                  event_id: event.nil? ? 'none' : event&.__id__,
                  event_type: event.nil? ? 'none' : event&.cs__class&.cs__name)
  end
  if startup
    logger.info(message,
                connection_error: error_message,
                client: Contrast::Agent::Reporting::ReporterClient::SERVICE_NAME,
                event_id: event&.__id__,
                timeout: timeout,
                event_type: event&.cs__class&.cs__name)
  end
  put_to_sleep
end

#timeoutObject

For how long the agent should wait until retry to send message

to be suspended.



59
60
61
# File 'lib/contrast/agent/reporting/reporting_utilities/response_handler.rb', line 59

def timeout
  @_timeout ||= TIMEOUT
end

#wake_upObject

Wakes the reporting service



73
74
75
# File 'lib/contrast/agent/reporting/reporting_utilities/response_handler.rb', line 73

def wake_up
  @_sleep = false
end