# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/utils/object_share' require 'contrast/utils/string_utils' require 'contrast/components/logger' module Contrast module Api module Decorators # Used to decorate the {Contrast::Api::Dtm::Message} protobuf model so it # can handle instance data massaging. module Message include Contrast::Components::Logger::InstanceMethods def self.included klass klass.extend(ClassMethods) end def append_event event case event when Contrast::Api::Dtm::ServerActivity self.server_activity = event when Contrast::Api::Dtm::AgentStartup self.agent_startup = event when Contrast::Api::Dtm::ApplicationCreate self.application_create = event when Contrast::Api::Dtm::ApplicationUpdate self.application_update = event when Contrast::Api::Dtm::Activity self.activity = event when Contrast::Api::Dtm::HttpRequest self.prefilter = event when Contrast::Api::Dtm::HttpResponse self.postfilter = event when Contrast::Api::Dtm::Poll self.poll = event when Contrast::Api::Dtm::ObservedRoute self.observed_route = event else logger.error('Unknown event type received. Unsure how to send.', event_type: event.cs__class.cs__name) return end logger.debug('Wrapping event in message', msg_id: __id__, p_id: pid, msg_count: message_count, event_id: event.__id__, event_type: event.cs__class.cs__name) end # Used to add class methods to the ApplicationUpdate class on inclusion of the decorator module ClassMethods @@message_total = 0 # rubocop:disable Style/ClassVars def message_count @@message_total += 1 # rubocop:disable Style/ClassVars end def build event msg = new msg.app_name = ::Contrast::APP_CONTEXT.app_name msg.app_path = ::Contrast::APP_CONTEXT.path msg.app_language = Contrast::Utils::ObjectShare::RUBY msg.client_id = ::Contrast::APP_CONTEXT.client_id msg.message_count = message_count msg.pid = ::Contrast::APP_CONTEXT.pid msg.ppid = ::Contrast::APP_CONTEXT.ppid msg.append_event(event) msg end end end end end end Contrast::Api::Dtm::Message.include(Contrast::Api::Decorators::Message)