# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/components/logger' require 'contrast/agent/reporting/reporting_events/application_defend_attack_sample' module Contrast module Agent module Reporting # This is the new AttackerSampleActivity class which will include Sample of the given attacks in the activity # period. class ApplicationDefendAttackSampleActivity # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackSample>] attr_reader :samples # @return [Hash<Integer,Integer>] map of time from start in seconds to number of attacks in that second attr_reader :time_map class << self # @param attack_result [Contrast::Api::Dtm::AttackResult] def convert attack_result activity = new activity.attach_data(attack_result) activity end end def initialize @samples = [] @start_time = (Contrast::Agent::REQUEST_TRACKER.current&.timer&.start_ms || 0) / 1000 @event_type = :application_defend_attack_sample_activity @time_map = Hash.new { |h, k| h[k] = 0 } super end def to_controlled_hash { attackTimeMap: time_map, samples: samples.map(&:to_controlled_hash), startTime: @start_time, total: 1 # there will only ever be 1 attack sample, until batching is done } end # @param attack_result [Contrast::Api::Dtm::AttackResult] def attach_data attack_result attack_result.samples.each do |attack_sample| converted = Contrast::Agent::Reporting::ApplicationDefendAttackSample.convert(attack_result, attack_sample) samples << converted attack_second = converted.time_stamp[:elapsed] / 1000 time_map[attack_second] += 1 end end end end end end