# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/api/dtm.pb' require 'contrast/utils/string_utils' module Contrast module Agent module Reporting # This is the new Route Discovery Observation class which will include all the needed information for the new # reporting system to relay this information in the Application Update messages. These route observations are # used by TeamServer to construct the route coverage information for the assess feature. They represent the # literal URL and HTTP verb used to invoke a method in the application, as routed by the application framework. # # @attr_reader url [String] the URL requested to hit this endpoint. Required for reporting. # @attr_reader verb [String] the HTTP Method requested to his this endpoint. Empty means all, so is allowed. # for reporting. class RouteDiscoveryObservation # required attributes attr_reader :url # optional attributes attr_reader :verb class << self # Convert a DTM for SpeedRacer to an Event for TeamServer. # # @param route_coverage_dtm [Contrast::Api::Dtm::RouteCoverage] # @return [Contrast::Agent::Reporting::RouteDiscoveryObservation] def convert route_coverage_dtm report = new report.attach_data(route_coverage_dtm) report end end # Attach the data from the protobuf models to this reporter so that it can be sent to TeamServer directly # # @param route_coverage_dtm [Contrast::Api::Dtm::RouteCoverage] def attach_data route_coverage_dtm @url = route_coverage_dtm.url @verb = route_coverage_dtm.verb if Contrast::Utils::StringUtils.present?(route_coverage_dtm.verb) end # Convert the instance variables on the class, and other information, into the identifiers required for # TeamServer to process the JSON form of this message. # # @return [Hash] # @raise [ArgumentError] def to_controlled_hash validate hash = { url: url } hash[:verb] = verb if verb hash end # Ensure the required fields are present. # # @raise [ArgumentError] def validate raise(ArgumentError, "#{ self } did not have a proper url. Unable to continue.") unless url end end end end end