# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/utils/string_utils' 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::Interface access_component :app_context, :logging 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.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.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 = APP_CONTEXT.name msg.app_path = APP_CONTEXT.path msg.app_language = Contrast::Utils::ObjectShare::RUBY msg.client_id = APP_CONTEXT.client_id msg.message_count = message_count msg.pid = APP_CONTEXT.pid msg.ppid = APP_CONTEXT.ppid msg.append_event(event) msg end end end end end end Contrast::Api::Dtm::Message.include(Contrast::Api::Decorators::Message)