contrast-agent 6.0.0 → 6.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -1
  3. data/Rakefile +1 -1
  4. data/ext/build_funchook.rb +3 -3
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -1
  6. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  7. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  8. data/ext/cs__assess_string/cs__assess_string.c +8 -0
  9. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  10. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  11. data/ext/cs__assess_test/extconf.rb +5 -0
  12. data/ext/cs__common/cs__common.c +101 -0
  13. data/ext/cs__common/cs__common.h +29 -5
  14. data/ext/cs__contrast_patch/cs__contrast_patch.c +1 -1
  15. data/ext/cs__tests/cs__tests.c +12 -0
  16. data/ext/cs__tests/cs__tests.h +3 -0
  17. data/ext/cs__tests/extconf.rb +5 -0
  18. data/ext/extconf_common.rb +1 -1
  19. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  20. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  21. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -2
  22. data/lib/contrast/agent/assess/policy/policy.rb +9 -10
  23. data/lib/contrast/agent/assess/policy/policy_node.rb +9 -10
  24. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  25. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -3
  26. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -3
  27. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  28. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +2 -1
  29. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
  31. data/lib/contrast/agent/assess/policy/propagator/split.rb +17 -21
  32. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  33. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/trigger_method.rb +10 -18
  35. data/lib/contrast/agent/assess/policy/trigger_node.rb +16 -16
  36. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  37. data/lib/contrast/agent/assess/property/evented.rb +2 -2
  38. data/lib/contrast/agent/assess/property/tagged.rb +2 -2
  39. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +6 -8
  40. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +6 -7
  41. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +12 -7
  42. data/lib/contrast/agent/assess/rule/response/base_rule.rb +13 -6
  43. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +66 -43
  44. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +4 -4
  45. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +6 -6
  46. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +4 -4
  47. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +4 -4
  48. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +4 -4
  49. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +3 -4
  50. data/lib/contrast/agent/assess/tag.rb +13 -14
  51. data/lib/contrast/agent/at_exit_hook.rb +12 -1
  52. data/lib/contrast/agent/inventory/database_config.rb +22 -7
  53. data/lib/contrast/agent/middleware.rb +9 -6
  54. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -5
  55. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +2 -2
  56. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +4 -4
  57. data/lib/contrast/agent/patching/policy/patch.rb +20 -19
  58. data/lib/contrast/agent/patching/policy/patch_status.rb +10 -3
  59. data/lib/contrast/agent/patching/policy/patcher.rb +1 -1
  60. data/lib/contrast/agent/patching/policy/policy.rb +13 -15
  61. data/lib/contrast/agent/patching/policy/policy_node.rb +19 -21
  62. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  63. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +126 -122
  64. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +1 -1
  66. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  67. data/lib/contrast/agent/protect/policy/rule_applicator.rb +4 -4
  68. data/lib/contrast/agent/protect/rule/base.rb +30 -18
  69. data/lib/contrast/agent/protect/rule/base_service.rb +31 -14
  70. data/lib/contrast/agent/protect/rule/cmd_injection.rb +16 -9
  71. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +3 -3
  72. data/lib/contrast/agent/protect/rule/default_scanner.rb +2 -1
  73. data/lib/contrast/agent/protect/rule/deserialization.rb +18 -7
  74. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +74 -74
  75. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +71 -53
  76. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +3 -3
  77. data/lib/contrast/agent/protect/rule/no_sqli.rb +15 -16
  78. data/lib/contrast/agent/protect/rule/path_traversal.rb +13 -3
  79. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -2
  80. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -1
  81. data/lib/contrast/agent/protect/rule/sqli.rb +16 -23
  82. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +61 -61
  83. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +29 -29
  84. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +32 -32
  85. data/lib/contrast/agent/protect/rule/xss.rb +17 -0
  86. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +14 -13
  87. data/lib/contrast/agent/protect/rule/xxe.rb +25 -3
  88. data/lib/contrast/agent/reaction_processor.rb +1 -1
  89. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +36 -36
  90. data/lib/contrast/agent/reporting/masker/masker.rb +18 -21
  91. data/lib/contrast/agent/reporting/masker/masker_utils.rb +10 -6
  92. data/lib/contrast/agent/reporting/reporter.rb +11 -16
  93. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  94. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +6 -2
  95. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +51 -0
  96. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +96 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +70 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +182 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +56 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack.rb +22 -0
  101. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +70 -0
  102. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +6 -2
  103. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +60 -0
  104. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  105. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +15 -11
  106. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  107. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +1 -1
  108. data/lib/contrast/agent/reporting/reporting_events/finding.rb +9 -3
  109. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  110. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  111. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  112. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +7 -3
  113. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  114. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  115. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +6 -10
  116. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  117. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  119. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  120. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  121. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  122. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -2
  123. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +15 -10
  124. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +54 -67
  125. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  126. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +9 -6
  127. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +17 -17
  128. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +47 -32
  129. data/lib/contrast/agent/reporting/settings/application_settings.rb +1 -1
  130. data/lib/contrast/agent/reporting/settings/assess.rb +5 -5
  131. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +3 -3
  132. data/lib/contrast/agent/reporting/settings/exclusions.rb +3 -3
  133. data/lib/contrast/agent/reporting/settings/protect.rb +21 -6
  134. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +6 -6
  135. data/lib/contrast/agent/reporting/settings/reaction.rb +3 -3
  136. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +2 -2
  137. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +2 -2
  138. data/lib/contrast/agent/reporting/settings/server_features.rb +2 -2
  139. data/lib/contrast/agent/request.rb +5 -5
  140. data/lib/contrast/agent/request_context.rb +23 -19
  141. data/lib/contrast/agent/request_context_extend.rb +11 -24
  142. data/lib/contrast/agent/request_handler.rb +4 -4
  143. data/lib/contrast/agent/response.rb +2 -0
  144. data/lib/contrast/agent/rule_set.rb +2 -2
  145. data/lib/contrast/agent/scope.rb +1 -1
  146. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  147. data/lib/contrast/agent/static_analysis.rb +1 -1
  148. data/lib/contrast/agent/telemetry/base.rb +155 -0
  149. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  150. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +119 -0
  151. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +44 -36
  152. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +29 -21
  153. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +91 -73
  154. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +62 -44
  155. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +50 -33
  156. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  157. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +30 -0
  158. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  159. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  160. data/lib/contrast/agent/thread_watcher.rb +52 -68
  161. data/lib/contrast/agent/version.rb +1 -1
  162. data/lib/contrast/agent/worker_thread.rb +8 -0
  163. data/lib/contrast/agent.rb +1 -3
  164. data/lib/contrast/api/communication/messaging_queue.rb +29 -12
  165. data/lib/contrast/api/communication/response_processor.rb +7 -10
  166. data/lib/contrast/api/communication/service_lifecycle.rb +1 -1
  167. data/lib/contrast/api/communication/socket.rb +1 -1
  168. data/lib/contrast/api/communication/socket_client.rb +1 -1
  169. data/lib/contrast/api/communication/speedracer.rb +3 -3
  170. data/lib/contrast/api/decorators/activity.rb +33 -0
  171. data/lib/contrast/api/decorators/agent_startup.rb +10 -9
  172. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  173. data/lib/contrast/api/decorators/application_startup.rb +4 -4
  174. data/lib/contrast/api/decorators/http_request.rb +1 -1
  175. data/lib/contrast/api/decorators/response_type.rb +4 -17
  176. data/lib/contrast/components/agent.rb +1 -1
  177. data/lib/contrast/components/base.rb +1 -1
  178. data/lib/contrast/components/config.rb +19 -28
  179. data/lib/contrast/components/contrast_service.rb +13 -1
  180. data/lib/contrast/components/sampling.rb +1 -1
  181. data/lib/contrast/components/settings.rb +58 -24
  182. data/lib/contrast/config/agent_configuration.rb +21 -11
  183. data/lib/contrast/config/api_configuration.rb +12 -8
  184. data/lib/contrast/config/api_proxy_configuration.rb +7 -3
  185. data/lib/contrast/config/application_configuration.rb +15 -11
  186. data/lib/contrast/config/assess_configuration.rb +13 -9
  187. data/lib/contrast/config/assess_rules_configuration.rb +6 -2
  188. data/lib/contrast/config/base_configuration.rb +3 -35
  189. data/lib/contrast/config/certification_configuration.rb +9 -5
  190. data/lib/contrast/config/exception_configuration.rb +10 -7
  191. data/lib/contrast/config/heap_dump_configuration.rb +13 -9
  192. data/lib/contrast/config/inventory_configuration.rb +9 -6
  193. data/lib/contrast/config/logger_configuration.rb +9 -6
  194. data/lib/contrast/config/protect_configuration.rb +9 -6
  195. data/lib/contrast/config/protect_rule_configuration.rb +12 -8
  196. data/lib/contrast/config/protect_rules_configuration.rb +19 -18
  197. data/lib/contrast/config/request_audit_configuration.rb +10 -7
  198. data/lib/contrast/config/root_configuration.rb +29 -12
  199. data/lib/contrast/config/ruby_configuration.rb +14 -11
  200. data/lib/contrast/config/sampling_configuration.rb +11 -8
  201. data/lib/contrast/config/server_configuration.rb +13 -9
  202. data/lib/contrast/config/service_configuration.rb +14 -11
  203. data/lib/contrast/configuration.rb +23 -14
  204. data/lib/contrast/extension/assess/array.rb +1 -1
  205. data/lib/contrast/extension/assess/erb.rb +1 -1
  206. data/lib/contrast/extension/assess/marshal.rb +1 -1
  207. data/lib/contrast/extension/assess/string.rb +1 -1
  208. data/lib/contrast/extension/extension.rb +2 -2
  209. data/lib/contrast/framework/base_support.rb +8 -8
  210. data/lib/contrast/framework/grape/support.rb +3 -3
  211. data/lib/contrast/framework/manager.rb +5 -5
  212. data/lib/contrast/framework/manager_extend.rb +1 -1
  213. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  214. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +14 -3
  215. data/lib/contrast/framework/rails/patch/assess_configuration.rb +3 -3
  216. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  217. data/lib/contrast/framework/rails/patch/support.rb +14 -46
  218. data/lib/contrast/framework/rails/support.rb +2 -2
  219. data/lib/contrast/framework/sinatra/support.rb +1 -1
  220. data/lib/contrast/logger/aliased_logging.rb +94 -0
  221. data/lib/contrast/logger/application.rb +0 -4
  222. data/lib/contrast/logger/cef_log.rb +14 -14
  223. data/lib/contrast/logger/format.rb +1 -1
  224. data/lib/contrast/logger/log.rb +8 -8
  225. data/lib/contrast/tasks/config.rb +30 -21
  226. data/lib/contrast/tasks/service.rb +2 -2
  227. data/lib/contrast/utils/assess/tracking_util.rb +4 -4
  228. data/lib/contrast/utils/class_util.rb +6 -10
  229. data/lib/contrast/utils/findings.rb +3 -3
  230. data/lib/contrast/utils/hash_digest.rb +6 -7
  231. data/lib/contrast/utils/head_dump_utils_extend.rb +1 -1
  232. data/lib/contrast/utils/invalid_configuration_util.rb +2 -2
  233. data/lib/contrast/utils/log_utils.rb +6 -4
  234. data/lib/contrast/utils/lru_cache.rb +1 -1
  235. data/lib/contrast/utils/metrics_hash.rb +1 -1
  236. data/lib/contrast/utils/middleware_utils.rb +6 -6
  237. data/lib/contrast/utils/net_http_base.rb +4 -4
  238. data/lib/contrast/utils/object_share.rb +1 -1
  239. data/lib/contrast/utils/os.rb +1 -1
  240. data/lib/contrast/utils/patching/policy/patch_utils.rb +2 -2
  241. data/lib/contrast/utils/request_utils.rb +2 -2
  242. data/lib/contrast/utils/sha256_builder.rb +4 -4
  243. data/lib/contrast/utils/stack_trace_utils.rb +31 -13
  244. data/lib/contrast/utils/telemetry.rb +22 -7
  245. data/lib/contrast/utils/telemetry_client.rb +27 -15
  246. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  247. data/lib/contrast/utils/telemetry_identifier.rb +18 -3
  248. data/lib/contrast/utils/timer.rb +1 -1
  249. data/lib/contrast.rb +9 -0
  250. data/resources/assess/policy.json +1 -1
  251. data/ruby-agent.gemspec +1 -1
  252. data/service_executables/VERSION +1 -1
  253. data/service_executables/linux/contrast-service +0 -0
  254. data/service_executables/mac/contrast-service +0 -0
  255. metadata +41 -16
  256. data/lib/contrast/agent/telemetry/events/metric_telemetry_event.rb +0 -26
  257. data/lib/contrast/agent/telemetry/events/startup_metrics_telemetry_event.rb +0 -121
  258. data/lib/contrast/agent/telemetry/events/telemetry_event.rb +0 -33
  259. data/lib/contrast/agent/telemetry/telemetry.rb +0 -150
  260. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -3,48 +3,56 @@
3
3
 
4
4
  module Contrast
5
5
  module Agent
6
- # This class will hold the all the mutual information for the Telemetry Exception
7
- class TelemetryExceptionBase
8
- def to_controlled_hash; end
6
+ module Telemetry
7
+ module TelemetryException
8
+ # This class will hold the all the mutual information for the Telemetry Exception
9
+ class Base
10
+ def to_controlled_hash; end
9
11
 
10
- protected
12
+ protected
11
13
 
12
- # Validate required and option fields
13
- #
14
- # @param validations [Hash] Validation hash to use
15
- # @return [nil]
16
- def validate validations
17
- validations.each do |k, v|
18
- next if v[:required] == false
14
+ # Validate required and option fields
15
+ #
16
+ # @param validations [Hash] Validation hash to use
17
+ # @return [nil]
18
+ def validate validations
19
+ validations.each do |k, v|
20
+ next if v[:required] == false
19
21
 
20
- validate_field validations[k], k
21
- end
22
- nil
23
- end
22
+ validate_field(validations[k], k)
23
+ end
24
+ nil
25
+ end
24
26
 
25
- # This method will validate every single field passed from validate
26
- #
27
- # @param validation_pair [Hash] Validation hash to use
28
- # @param key[String] The key to check in VALIDATIONS
29
- def validate_field validation_pair, key
30
- value_to_validate = send(key.to_sym)
31
- validate_class value_to_validate, validation_pair[:class], key if validation_pair.key?(:class)
32
- value_length = value_to_validate.cs__is_a?(String) ? value_to_validate.length : value_to_validate.entries.length
33
- unless validation_pair[:range].include?(value_length)
34
- raise ArgumentError, "The provided value for #{ key } is invalid"
35
- end
27
+ # This method will validate every single field passed from validate
28
+ #
29
+ # @param validation_pair [Hash] Validation hash to use
30
+ # @param key[String] The key to check in VALIDATIONS
31
+ def validate_field validation_pair, key
32
+ value_to_validate = send(key.to_sym)
33
+ validate_class(value_to_validate, validation_pair[:class], key) if validation_pair.key?(:class)
34
+ value_length = if value_to_validate.cs__is_a?(String) || value_to_validate.cs__is_a?(Array)
35
+ value_to_validate.length
36
+ else
37
+ value_to_validate.entries.length
38
+ end
39
+ unless validation_pair[:range].include?(value_length)
40
+ raise(ArgumentError, "The provided value for #{ key } is invalid: #{ value_to_validate }")
41
+ end
36
42
 
37
- nil
38
- end
43
+ nil
44
+ end
39
45
 
40
- # With the all nested classes, we still need to double check if everything passed along the way
41
- # is right
42
- # @param message [Object] The message we want to check the class of
43
- # @param klass [Class] The klass we want to check the message with
44
- # @param field [Object] The field with the error
45
- def validate_class message, klass, field
46
- message = message[0] if message.cs__is_a?(Array)
47
- raise ArgumentError, "The provided value for #{ field } is invalid" unless message.cs__is_a? klass
46
+ # With the all nested classes, we still need to double check if everything passed along the way
47
+ # is right
48
+ # @param message [Object] The message we want to check the class of
49
+ # @param klass [Class] The klass we want to check the message with
50
+ # @param field [Object] The field with the error
51
+ def validate_class message, klass, field
52
+ message = message[0] if message.cs__is_a?(Array)
53
+ raise(ArgumentError, "The provided value for #{ field } is of wrong class") unless message.cs__is_a?(klass)
54
+ end
55
+ end
48
56
  end
49
57
  end
50
58
  end
@@ -6,30 +6,38 @@ require_relative 'telemetry_exception_message'
6
6
 
7
7
  module Contrast
8
8
  module Agent
9
- # This class will hold the basic information for a Parent Telemetry Exception Event
10
- class TelemetryExceptionEvent < Contrast::Agent::TelemetryExceptionBase
11
- # Array of Telemetry Exclusions
12
- # @return [Array<Contrast::Agent::TelemetryExceptionMessage>]
13
- attr_reader :exclusions
9
+ module Telemetry
10
+ module TelemetryException
11
+ # This class will hold the basic information for a Parent Telemetry Exception Event
12
+ class Event < Contrast::Agent::Telemetry::TelemetryException::Base
13
+ # Array of Telemetry Exceptions
14
+ # @return [Array<Contrast::Agent::Telemetry::TelemetryException::Message>]
15
+ attr_reader :exceptions
14
16
 
15
- # Initialization of the Parent Event requires us to require the exception
16
- # to be created
17
- #
18
- # @param message [Contrast::Agent::TelemetryExceptionMessage]
19
- def initialize message
20
- super()
21
- validate_class message, Contrast::Agent::TelemetryExceptionMessage, 'exception_message'
22
- @exclusions = Array.new(1, message)
23
- end
17
+ # Initialization of the Parent Event requires us to require the exception
18
+ # to be created
19
+ #
20
+ # @param message [Contrast::Agent::Telemetry::TelemetryException::Message]
21
+ def initialize message
22
+ super()
23
+ validate_class(message, Contrast::Agent::Telemetry::TelemetryException::Message, 'exception_message')
24
+ @exceptions = Array.new(1, message)
25
+ end
24
26
 
25
- # @param message [Contrast::Agent::TelemetryExceptionMessage]
26
- def push message
27
- validate_class message, Contrast::Agent::TelemetryExceptionMessage, 'exception_message'
28
- @exclusions << message
29
- end
27
+ # @param message [Contrast::Agent::Telemetry::TelemetryException::Message]
28
+ def push message
29
+ validate_class(message, Contrast::Agent::Telemetry::TelemetryException::Message, 'exception_message')
30
+ @exceptions << message
31
+ end
32
+
33
+ def self.path
34
+ '/ruby/runtime'
35
+ end
30
36
 
31
- def to_controlled_hash
32
- exclusions.map(&:to_controlled_hash)
37
+ def to_controlled_hash
38
+ exceptions.map(&:to_controlled_hash)
39
+ end
40
+ end
33
41
  end
34
42
  end
35
43
  end
@@ -3,94 +3,112 @@
3
3
 
4
4
  require_relative 'telemetry_exception_base'
5
5
  require_relative 'telemetry_exception_message_exception'
6
+ require 'contrast/utils/telemetry_identifier'
6
7
 
7
8
  module Contrast
8
9
  module Agent
9
- # This class will hold the all the information for the specific exceptions
10
- # and will be passed in the the event to be sent to TS
11
- class TelemetryExceptionMessage < Contrast::Agent::TelemetryExceptionBase
12
- VALIDATIONS = {
13
- instance: { required: true, range: 12..64 },
14
- tags: { required: true, range: 1..512 },
15
- logger: { required: false, range: 1..128 },
16
- message: { required: false, range: 1..512 },
17
- exceptions: { required: true, range: 1..512, class: Contrast::Agent::TelemetryExceptionMessageException }
18
- }.cs__freeze
10
+ module Telemetry
11
+ module TelemetryException
12
+ # This class will hold the all the information for the specific exceptions
13
+ # and will be passed in the the event to be sent to TS
14
+ class Message < Contrast::Agent::Telemetry::TelemetryException::Base
15
+ VALIDATIONS = {
16
+ instance: { required: true, range: 12..64 },
17
+ tags: { required: true, range: 0..512 },
18
+ logger: { required: false, range: 1..128 },
19
+ message: { required: false, range: 1..512 },
20
+ exceptions: {
21
+ required: true,
22
+ range: 1..512,
23
+ class: Contrast::Agent::Telemetry::TelemetryException::MessageException
24
+ }
25
+ }.cs__freeze
19
26
 
20
- # Timestamp of creation in ISO8601 format
21
- # @return [Integer]
22
- attr_reader :timestamp
27
+ # Timestamp of creation in ISO8601 format
28
+ # @return [Integer]
29
+ attr_reader :timestamp
23
30
 
24
- # An Instance ID as defined in Unique Identification // Application ID
25
- # @return [String]
26
- attr_reader :instance
31
+ # An Instance ID as defined in Unique Identification // Application ID
32
+ # @return [String]
33
+ attr_reader :instance
27
34
 
28
- # Tags are key-value string pairs that annotate either metrics
29
- # or errors to help provide context, filtering, grouping, and deduplication.
30
- # @return [Hash{String => String}]
31
- attr_reader :tags
35
+ # Tags are key-value string pairs that annotate either metrics
36
+ # or errors to help provide context, filtering, grouping, and deduplication.
37
+ # @return [Hash{String => String}]
38
+ attr_reader :tags
32
39
 
33
- # @return [Integer] A number of the occurrences of the exception
34
- attr_accessor :occurrences
40
+ # @return [Integer] A number of the occurrences of the exception
41
+ attr_accessor :occurrences
35
42
 
36
- # Array of exceptions, but in our case the Array will only include one exception
37
- # @return [Array<Contrast::Agent::TelemetryExceptionMessageException>]
38
- attr_reader :exceptions
43
+ # Array of exceptions, but in our case the Array will only include one exception
44
+ # @return [Array<Contrast::Agent::Telemetry::TelemetryException::MessageException>]
45
+ attr_reader :exceptions
39
46
 
40
- # @return [String,nil] A string denoting the origin of this error.
41
- attr_reader :logger
47
+ # @return [String,nil] A string denoting the origin of this error.
48
+ attr_reader :logger
42
49
 
43
- # @return [String | nil] A string message to provide additional context to the errors.
44
- attr_reader :message
50
+ # @return [String | nil] A string message to provide additional context to the errors.
51
+ attr_reader :message
45
52
 
46
- def initialize instance, tags, exceptions
47
- super()
48
- @tags = tags
49
- @timestamp = Time.now.iso8601
50
- @instance = instance
51
- @occurrences = 1
52
- @exceptions = exceptions
53
- validate VALIDATIONS
54
- end
53
+ def initialize instance, tags, exceptions
54
+ super()
55
+ @tags = tags
56
+ @timestamp = Time.now.iso8601
57
+ @instance = instance
58
+ @occurrences = 1
59
+ @exceptions = exceptions
60
+ validate(VALIDATIONS)
61
+ end
55
62
 
56
- # Optional parameters will be set separately from the required
57
- #
58
- # @param logger[String]
59
- def logger= logger
60
- validate_field VALIDATIONS[:logger], 'logger'
61
- @logger = logger
62
- end
63
+ # Optional parameters will be set separately from the required
64
+ #
65
+ # @param logger[String]
66
+ def logger= logger
67
+ validate_field(VALIDATIONS[:logger], 'logger')
68
+ @logger = logger
69
+ end
63
70
 
64
- # Optional parameters will be set separately from the required
65
- #
66
- # @param message[String]
67
- def message= message
68
- validate_field VALIDATIONS[:message], 'message'
69
- @message = message
70
- end
71
+ # Optional parameters will be set separately from the required
72
+ #
73
+ # @param message[String]
74
+ def message= message
75
+ validate_field(VALIDATIONS[:message], 'message')
76
+ @message = message
77
+ end
71
78
 
72
- # Optional parameters will be set separately from the required
73
- # This method is different and is regarding the way we proceed
74
- # with incrementing occurrences
75
- # If we keep track of them in different places and we store that value
76
- # in separated variable - we may directly re-assign occurrences=
77
- # But if we are not doing that - we may on same message generated
78
- # to increment occurrences from here
79
- def increment_occurrences
80
- @occurrences += 1
81
- end
79
+ # Optional parameters will be set separately from the required
80
+ # This method is different and is regarding the way we proceed
81
+ # with incrementing occurrences
82
+ # If we keep track of them in different places and we store that value
83
+ # in separated variable - we may directly re-assign occurrences=
84
+ # But if we are not doing that - we may on same message generated
85
+ # to increment occurrences from here
86
+ def increment_occurrences
87
+ @occurrences += 1
88
+ end
89
+
90
+ def to_controlled_hash
91
+ super()
92
+ {
93
+ timestamp: timestamp,
94
+ instance: instance,
95
+ occurrences: occurrences,
96
+ tags: tags,
97
+ exceptions: exceptions.map(&:to_controlled_hash),
98
+ logger: logger,
99
+ message: message
100
+ }.compact
101
+ end
82
102
 
83
- def to_controlled_hash
84
- super()
85
- {
86
- timestamp: timestamp,
87
- instance: instance,
88
- occurrences: occurrences,
89
- tags: tags,
90
- exceptions: exceptions.map(&:to_controlled_hash),
91
- logger: logger,
92
- message: message
93
- }.compact
103
+ class << self
104
+ def build tags, exceptions, logger = nil, message = nil
105
+ inst = new(Contrast::Utils::Telemetry::Identifier.instance_id, tags, exceptions)
106
+ inst.logger = logger unless logger.nil?
107
+ inst.message = message unless message.nil?
108
+ inst
109
+ end
110
+ end
111
+ end
94
112
  end
95
113
  end
96
114
  end
@@ -6,59 +6,77 @@ require_relative 'telemetry_exception_stack_frame'
6
6
 
7
7
  module Contrast
8
8
  module Agent
9
- # This class will hold the all the information for the specific exception
10
- # and will be passed in the Exception message itself
11
- class TelemetryExceptionMessageException < Contrast::Agent::TelemetryExceptionBase
12
- VALIDATIONS = {
13
- type: { required: true, range: 1..256 },
14
- module_name: { required: false, range: 1..256 },
15
- value: { required: false, range: 1..256 },
16
- stack_frames: { required: true, range: 1..128, class: Contrast::Agent::TelemetryExceptionStackFrame }
17
- }.cs__freeze
9
+ module Telemetry
10
+ module TelemetryException
11
+ # This class will hold the all the information for the specific exception
12
+ # and will be passed in the Exception message itself
13
+ class MessageException < Contrast::Agent::Telemetry::TelemetryException::Base
14
+ VALIDATIONS = {
15
+ type: { required: true, range: 1..256 },
16
+ module_name: { required: false, range: 1..256 },
17
+ value: { required: false, range: 1..256 },
18
+ stack_frames: {
19
+ required: true,
20
+ range: 1..128,
21
+ class: Contrast::Agent::Telemetry::TelemetryException::StackFrame
22
+ }
23
+ }.cs__freeze
18
24
 
19
- # @return [String] The type of the exception itself
20
- attr_reader :type
25
+ # @return [String] The type of the exception itself
26
+ attr_reader :type
21
27
 
22
- # @return [Array<Contrast::Agent::TelemetryExceptionStackFrame>] stack frames for the message exception
23
- attr_reader :stack_frames
28
+ # stack frames for the message exception
29
+ # @return [Array<Contrast::Agent::Telemetry::TelemetryException::StackFrame>]
30
+ attr_reader :stack_frames
24
31
 
25
- # @return [String]
26
- attr_reader :module_name
32
+ # @return [String]
33
+ attr_reader :module_name
27
34
 
28
- # @return [String]
29
- attr_reader :value
35
+ # @return [String]
36
+ attr_reader :value
30
37
 
31
- def initialize type, stack_frame
32
- super()
33
- @type = type
34
- @stack_frames = Array.new(1, stack_frame)
35
- validate VALIDATIONS
36
- end
38
+ def initialize type, stack_frame
39
+ super()
40
+ @type = type
41
+ @stack_frames = Array.new(1, stack_frame)
42
+ validate(VALIDATIONS)
43
+ end
37
44
 
38
- # @param stack_frame [Contrast::Agent::TelemetryExceptionStackFrame]
39
- def push stack_frame
40
- validate_class stack_frame, Contrast::Agent::TelemetryExceptionStackFrame, 'stack_frame'
41
- @stack_frames << stack_frame
42
- end
45
+ # @param stack_frame [Contrast::Agent::Telemetry::TelemetryException::StackFrame]
46
+ def push stack_frame
47
+ validate_class(stack_frame, Contrast::Agent::Telemetry::TelemetryException::StackFrame, 'stack_frame')
48
+ @stack_frames << stack_frame
49
+ end
43
50
 
44
- def module_name= module_name
45
- validate_field VALIDATIONS[:module_name], 'module_name'
46
- @module_name = module_name
47
- end
51
+ def module_name= module_name
52
+ @module_name = module_name
53
+ validate_field(VALIDATIONS[:module_name], 'module_name')
54
+ end
48
55
 
49
- def value= value
50
- validate_field VALIDATIONS[:value], 'value'
51
- @value = value
52
- end
56
+ def value= value
57
+ @value = value
58
+ validate_field(VALIDATIONS[:value], 'value')
59
+ end
60
+
61
+ def to_controlled_hash
62
+ super
63
+ {
64
+ type: type,
65
+ module: module_name,
66
+ stackFrames: stack_frames.map(&:to_controlled_hash),
67
+ value: value
68
+ }.compact
69
+ end
53
70
 
54
- def to_controlled_hash
55
- super
56
- {
57
- type: type,
58
- stack_frames: stack_frames.map(&:to_controlled_hash),
59
- module_name: module_name,
60
- value: value
61
- }.compact
71
+ class << self
72
+ def build type, value, module_name, stackframes
73
+ inst = new(type, stackframes)
74
+ inst.module_name = module_name if module_name
75
+ inst.value = value
76
+ inst
77
+ end
78
+ end
79
+ end
62
80
  end
63
81
  end
64
82
  end
@@ -5,42 +5,59 @@ require_relative 'telemetry_exception_base'
5
5
 
6
6
  module Contrast
7
7
  module Agent
8
- # This class will hold the all the information for the specific exception
9
- # and will be passed in the Exception message itself
10
- class TelemetryExceptionStackFrame < Contrast::Agent::TelemetryExceptionBase
11
- VALIDATIONS = {
12
- function: { required: true, range: 1..256 },
13
- type: { required: true, range: 1..256 },
14
- module_name: { required: false, range: 1..256 }
15
- }.cs__freeze
16
-
17
- # @return [String] The type of the exception itself
18
- attr_reader :type
19
-
20
- # @return [String] the function of the stack trace
21
- attr_reader :function
22
-
23
- # @return [Boolean] Is it in contrast
24
- attr_accessor :in_contrast
25
-
26
- def initialize function, type
27
- super()
28
- @function = function
29
- @type = type
30
- @in_contrast = false
31
- validate VALIDATIONS
32
- end
8
+ module Telemetry
9
+ module TelemetryException
10
+ # This class will hold the all the information for the specific exception
11
+ # and will be passed in the Exception message itself
12
+ class StackFrame < Contrast::Agent::Telemetry::TelemetryException::Base
13
+ VALIDATIONS = {
14
+ function: { required: true, range: 1..256 },
15
+ type: { required: true, range: 1..256 },
16
+ module_name: { required: false, range: 1..256 }
17
+ }.cs__freeze
33
18
 
34
- attr_reader :module_name
19
+ # @return [String] The type of the exception itself
20
+ attr_reader :type
35
21
 
36
- def module_name= module_name
37
- validate_field VALIDATIONS[:module_name], 'module_name'
38
- @module_name = module_name
39
- end
22
+ # @return [String] the function of the stack trace
23
+ attr_reader :function
24
+
25
+ # @return [Boolean] Is it in contrast
26
+ attr_accessor :in_contrast
27
+
28
+ # @return [String]
29
+ attr_reader :module_name
30
+
31
+ def initialize function, type
32
+ super()
33
+ @function = function
34
+ @type = type
35
+ @in_contrast = false
36
+ validate(VALIDATIONS)
37
+ end
38
+
39
+ def module_name= module_name
40
+ @module_name = module_name
41
+ validate_field(VALIDATIONS[:module_name], 'module_name')
42
+ end
43
+
44
+ def to_controlled_hash
45
+ super
46
+ { function: function, type: type, module: module_name, inContrast: in_contrast }.compact
47
+ end
40
48
 
41
- def to_controlled_hash
42
- super
43
- { function: function, type: type, inContrast: in_contrast, module_name: module_name }.compact
49
+ class << self
50
+ # @param method [String] method, triggered the logger on warn/error/fatal
51
+ # @param type [String] the type ( where it occurred )
52
+ # @param module_name [String, nil] Name of the module if any.
53
+ def build method, type, module_name = nil
54
+ inst = new(method, type)
55
+ inst.module_name = module_name if module_name
56
+ inst.in_contrast = type.include?('lib/contrast')
57
+ inst
58
+ end
59
+ end
60
+ end
44
61
  end
45
62
  end
46
63
  end
@@ -0,0 +1,20 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Agent
6
+ module Telemetry
7
+ # This is the module that will include every information created and stored for the
8
+ # Telemetry Exceptions
9
+ module TelemetryException
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_base'
16
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame'
17
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception'
18
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_message'
19
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_event'
20
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report'
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Agent
6
+ module Telemetry
7
+ # This module will handle the reporting of the TelemetryExceptionHash
8
+ module TelemetryExceptionReport
9
+ # Here we will send any exceptions gathered. The telemetry_hash is split into batches of 256
10
+ # and then added to the telemetry queue. Since this method is called before entering the
11
+ # until queue loop any updates after clearing the Contrast::TELEMETRY_EXCEPTIONS would have
12
+ # to wait for the sending process to be completed, so accumulating new batches.
13
+ # This methods expects queue and error_messages methods from Contrast::Agent::Telemetry::Base
14
+ def push_exceptions
15
+ return unless Contrast::TELEMETRY_EXCEPTIONS&.any?
16
+
17
+ Contrast::TELEMETRY_EXCEPTIONS.values.each_slice(256) { |tuple| error_messages.push(tuple) }
18
+ # Clear the hash. All exceptions now live in @_error_messages instance variable. and we will
19
+ # add them to the queue. Clearing would make the hash available to be populated again while the
20
+ # sending is proceeding.
21
+ Contrast::TELEMETRY_EXCEPTIONS.clear
22
+ # Add batch to queue. We need to shift here, because we want to report from the oldest batch to
23
+ # the newest. And even if somehow the array is filled during sending the new messages would stay
24
+ # and wait their turn.
25
+ queue << error_messages.shift until error_messages.empty?
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/metrics_hash'
5
+ require 'contrast/agent/telemetry/events/event'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Telemetry
10
+ # This class will hold the basic information for a Telemetry Event
11
+ class MetricEvent < Contrast::Agent::Telemetry::Event
12
+ include Contrast::Utils
13
+
14
+ attr_reader :fields
15
+
16
+ def initialize
17
+ super
18
+ @fields = MetricsHash.new(Numeric)
19
+ @fields['_filler'] = 0
20
+ end
21
+
22
+ def to_hash **_args
23
+ super.merge!({ fields: @fields })
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end