# 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/reporting_events/server_activity' require 'contrast/api/dtm.pb' module Contrast module Agent module Reporting # Util module for checking DTM message type. It temporarily allows for the conversion from a DTM to an # EventReport. # TODO: RUBY-1438 -- remove module DtmMessage class << self # Checks if the message is of Contrast::Api::Dtm::ServerActivity class # # @param dtm [Contrast::Api::Dtm::ServerActivity, Object] # @return [Boolean] def server_activity? dtm dtm.cs__is_a?(Contrast::Api::Dtm::ServerActivity) end # Checks if the message is a Hash of Contrast::Api::Dtm::LibraryUsageUpdate class # # @param message [Protobuf::Field::FieldHash<String,::Contrast::Api::Dtm::LibraryUsageUpdate>] # @return [Boolean] def library_usage? message message.cs__is_a?(Protobuf::Field::FieldHash) && message.values[0].cs__is_a?(Contrast::Api::Dtm::LibraryUsageUpdate) end # Checks if the message is of Contrast::Api::Dtm::ApplicationUpdate class # # @param dtm [Contrast::Api::Dtm::ApplicationUpdate,Object] # @return [Boolean] def application_update? dtm dtm.cs__is_a?(Contrast::Api::Dtm::ApplicationUpdate) end # @param dtm [Contrast::Api::Dtm::Finding,Object] # @return [Boolean] def finding? dtm dtm.cs__is_a?(Contrast::Api::Dtm::Finding) end # Converts DTM message to Reporting Event for those messages that have conversion methods crated. We use this # as we work to move away from requiring the Service. # # @param dtm [Contrast::Api::Dtm] # @return event [Contrast::Agent::Reporting::ReportingEvent, nil] def dtm_to_event dtm # For the ServerActivity we need to create and send empty body only. This is done because we need the # response from TS. return Contrast::Agent::Reporting::ServerActivity.new if server_activity?(dtm) # For the others, we convert them. return Contrast::Agent::Reporting::ObservedLibraryUsage.convert(dtm) if library_usage?(dtm) return Contrast::Agent::Reporting::ApplicationUpdate.convert(dtm) if application_update?(dtm) return Contrast::Agent::Reporting::Finding.convert(dtm) if finding?(dtm) nil end end end end end end