# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/agent/reporting/settings/application_settings' require 'contrast/agent/reporting/settings/server_features' require 'contrast/agent/reporting/settings/reaction' module Contrast module Agent module Reporting # this class will hold the response from TS class Response # All of the settings from TeamServer that apply at the application level. # At least one, but not necessarily all, setting will differ from the agent's current set. # Agents are able to replace all application settings with those in this message. # # @return [Contrast::Agent::Reporting::Settings::ApplicationSettings, nil] attr_accessor :application_settings # All of the feature server_features # # @return [Contrast::Agent::Reporting::Settings::FeatureSettings, nil] attr_accessor :server_features # Success boolean message value # # @return [Boolean] attr_accessor :success # Message with reasons for success or fail. # # @return [Array] Messages received from TS. attr_accessor :messages class << self # All of the settings from TeamServer that apply at the application level. # # @return response [Contrast::Agent::Reporting::Response] def build_application_response res = new res.application_settings = Contrast::Agent::Reporting::Settings::ApplicationSettings.new res end # All of the settings from TeamServer that apply at the server level. # # @return response [Contrast::Agent::Reporting::Response] def build_server_response res = new res.server_features = Contrast::Agent::Reporting::Settings::ServerFeatures.new res end end # Reaction the agent should take based on a state in TS. # This is moved one level up because the responses we # receive for feature and settings from TS have different # place to store these reactions: # # body.reactions vs body.settings.reactions # # @return [Array] def reactions @_reactions ||= [] end # Set the reaction # # @param reaction_array [Array] { # level [String] The level at which the agent should log this reaction. # [ERROR, WARN, INFO, DEBUG, TRACE] # message [String] A message to log when receiving this reaction. # operation [String] What to do in response to this reaction.[NOOP, DISABLE] } # @return [Array] def reactions= reaction_array return unless reaction_array.is_a?(Array) reaction_array.each do |r| reactions << Contrast::Agent::Reporting::Settings::Reaction.new(r[:level], r[:operation], r[:message]) end end # This method is used only for testing with golden files. def to_controlled_hash { success: success, messages: messages, features: server_features.nil? ? nil : server_features.to_controlled_hash, settings: application_settings.nil? ? nil : application_settings.to_controlled_hash, logLevel: server_features&.log_level, logFile: server_features&.log_file, reactions: server_features.nil? ? nil : reactions.map(&:to_controlled_hash) }.compact end end end end end