Class: Contrast::Agent::Reporting::DiscoveredRoute

Inherits:
ObservedRoute show all
Defined in:
lib/contrast/agent/reporting/reporting_events/discovered_route.rb

Overview

This is the new Discovered Route class which will include all the needed information for the new reporting system to relay this information in the Route Observation messages. These observations are used by TeamServer to construct the route coverage information for the assess feature. They represent those methods which map to externally accessible endpoints within the application, as registered to the application framework. This also includes the literal URL and HTTP Verb used to invoke them, as they must have been called at this point to be recorded.

Instance Attribute Summary collapse

Attributes inherited from ObservedRoute

#sources

Attributes inherited from ReportingEvent

#event_endpoint, #event_method

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ObservedRoute

#file_name, #hash_id

Methods inherited from ReportingEvent

#attach_headers

Methods inherited from ReportableHash

#event_json, #valid?

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Constructor Details

#initializeDiscoveredRoute

Returns a new instance of DiscoveredRoute.



98
99
100
101
102
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 98

def initialize
  @event_type = :discovered_route
  @verb = Contrast::Utils::ObjectShare::EMPTY_STRING
  super()
end

Instance Attribute Details

#signatureString

Returns the controller, method, and pattern of this route.

Returns:

  • (String)

    the controller, method, and pattern of this route



92
93
94
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 92

def signature
  @signature
end

#urlString

Returns the url (or url pattern) used to access this route.

Returns:

  • (String)

    the url (or url pattern) used to access this route



94
95
96
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 94

def url
  @url
end

#verbString?

Returns the HTTP verb used to access this route or nil for any verb.

Returns:

  • (String, nil)

    the HTTP verb used to access this route or nil for any verb



96
97
98
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 96

def verb
  @verb
end

Class Method Details

.from_action_dispatch_journey(journey_obj, url = nil) ⇒ Contrast::Agent::Reporting::DiscoveredRoute

Convert ActionDispatch::Journey::Route to Contrast::Agent::Reporting::DiscoveredRoute

Parameters:

  • journey_obj (ActionDispatch::Journey::Route)

    a rails route

  • url (String, nil) (defaults to: nil)

    use url from string instead of journey object.

Returns:



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 37

def from_action_dispatch_journey journey_obj, url = nil
  msg = new
  msg.signature = "#{ journey_obj.defaults[:controller] }##{ journey_obj.defaults[:action] }"

  verb = source_or_string(journey_obj.verb)
  msg.verb = Contrast::Utils::StringUtils.force_utf8(verb)

  url ||= source_or_string(journey_obj.path.spec)
  msg.url = Contrast::Utils::StringUtils.force_utf8(url)
  msg
end

.from_grape_controller(controller, method, pattern, url = nil) ⇒ Contrast::Agent::Reporting::DiscoveredRoute

Convert Grape route data to discovered route.

Parameters:

  • controller (::Grape::API)

    the route’s final controller.

  • method (String)

    GET, PUT, POST, etc…

  • url (String, nil) (defaults to: nil)

    use url from string instead matched pattern.

  • pattern (String, Grape::Router::Route)

    the pattern that was matched in routing.

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 56

def from_grape_controller controller, method, pattern, url = nil
  if pattern.cs__is_a?(Grape::Router::Route)
    safe_pattern = pattern.pattern&.path&.to_s
    safe_url = source_or_string(url || safe_pattern)
  else
    safe_pattern = source_or_string(pattern)
    safe_url = source_or_string(url || pattern)
  end

  msg = new
  msg.signature = "#{ controller }##{ method } #{ safe_pattern }"
  msg.verb = Contrast::Utils::StringUtils.force_utf8(method)
  msg.url = Contrast::Utils::StringUtils.force_utf8(safe_url)
  msg
end

.from_sinatra_route(controller, method, pattern, url = nil) ⇒ Contrast::Agent::Reporting::DiscoveredRoute

Convert Sinatra route data to discovered route.

Parameters:

  • controller (::Sinatra::Base)

    the route’s final controller.

  • method (String)

    GET, PUT, POST, etc…

  • pattern (::Mustermann::Sinatra)

    the pattern that was matched in routing.

  • url (String, nil) (defaults to: nil)

    use url from string instead matched pattern.

Returns:



79
80
81
82
83
84
85
86
87
88
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 79

def from_sinatra_route controller, method, pattern, url = nil
  safe_pattern = source_or_string(pattern)
  safe_url = source_or_string(url || pattern)

  msg = new
  msg.signature = "#{ controller }##{ method } #{ safe_pattern }" # rubocop:disable [Security/Object/Method]
  msg.verb = Contrast::Utils::StringUtils.force_utf8(method)
  msg.url = Contrast::Utils::StringUtils.force_utf8(safe_url)
  msg
end

.source_or_string(obj) ⇒ String

Parameters:

Returns:



22
23
24
25
26
27
28
29
30
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 22

def source_or_string obj
  if obj.cs__is_a?(Regexp)
    obj.source
  elsif obj.cs__respond_to?(:safe_string)
    obj.safe_string
  else
    obj.to_s
  end
end

Instance Method Details

#to_controlled_hashObject



104
105
106
107
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 104

def to_controlled_hash
  validate
  { session_id: ::Contrast::ASSESS.session_id, signature: @signature, verb: @verb, url: @url }.compact
end

#validateObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/contrast/agent/reporting/reporting_events/discovered_route.rb', line 109

def validate
  if Contrast::Utils::DuckUtils.empty_duck?(signature)
    raise(ArgumentError, "#{ self } did not have a proper signature. Unable to continue.")
  end
  if Contrast::Utils::DuckUtils.empty_duck?(url)
    raise(ArgumentError, "#{ self } did not have a proper url. Unable to continue.")
  end

  nil
end