contrast-agent 6.8.0 → 6.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/lib/contrast/agent/assess/policy/trigger_method.rb +1 -1
  4. data/lib/contrast/agent/assess/property/evented.rb +11 -11
  5. data/lib/contrast/agent/assess/rule/response/body_rule.rb +1 -1
  6. data/lib/contrast/agent/assess.rb +0 -1
  7. data/lib/contrast/agent/excluder.rb +1 -1
  8. data/lib/contrast/agent/middleware.rb +12 -4
  9. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +76 -83
  10. data/lib/contrast/agent/protect/input_analyzer/worth_watching_analyzer.rb +121 -0
  11. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +2 -0
  12. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +6 -3
  13. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -0
  14. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +3 -0
  15. data/lib/contrast/agent/protect/policy/rule_applicator.rb +12 -0
  16. data/lib/contrast/agent/protect/rule/base.rb +21 -7
  17. data/lib/contrast/agent/protect/rule/base_service.rb +6 -0
  18. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification.rb +1 -1
  19. data/lib/contrast/agent/protect/rule/bot_blocker.rb +9 -1
  20. data/lib/contrast/agent/protect/rule/cmd_injection.rb +8 -7
  21. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +8 -0
  22. data/lib/contrast/agent/protect/rule/cmdi/cmdi_chained_command.rb +0 -5
  23. data/lib/contrast/agent/protect/rule/cmdi/cmdi_dangerous_path.rb +0 -5
  24. data/lib/contrast/agent/protect/rule/deserialization.rb +2 -2
  25. data/lib/contrast/agent/protect/rule/no_sqli.rb +24 -2
  26. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_input_classification.rb +1 -1
  27. data/lib/contrast/agent/protect/rule/path_traversal.rb +12 -3
  28. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +0 -1
  29. data/lib/contrast/agent/protect/rule/sqli.rb +10 -13
  30. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +6 -2
  31. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +20 -0
  32. data/lib/contrast/agent/protect/rule/xss/reflected_xss_input_classification.rb +1 -1
  33. data/lib/contrast/agent/protect/rule/xss.rb +9 -0
  34. data/lib/contrast/agent/protect/rule/xxe.rb +2 -2
  35. data/lib/contrast/agent/protect/rule.rb +0 -3
  36. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +1 -1
  37. data/lib/contrast/agent/reporting/attack_result/user_input.rb +0 -1
  38. data/lib/contrast/agent/reporting/details/details.rb +0 -1
  39. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +12 -0
  40. data/lib/contrast/agent/reporting/report.rb +2 -0
  41. data/lib/contrast/agent/reporting/reporter.rb +42 -7
  42. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +5 -6
  43. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +24 -7
  44. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +20 -5
  45. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +0 -1
  46. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +5 -0
  47. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +10 -1
  48. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +2 -1
  49. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +6 -1
  50. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +10 -0
  51. data/lib/contrast/agent/reporting/reporting_events/application_settings.rb +40 -0
  52. data/lib/contrast/agent/reporting/reporting_events/finding.rb +2 -2
  53. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +239 -93
  54. data/lib/contrast/agent/reporting/reporting_events/finding_event_signature.rb +10 -23
  55. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +10 -9
  56. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +12 -0
  57. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +8 -0
  58. data/lib/contrast/agent/reporting/reporting_events/server_settings.rb +40 -0
  59. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  60. data/lib/contrast/agent/reporting/reporting_utilities/ng_response_extractor.rb +137 -0
  61. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +52 -2
  62. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +8 -4
  63. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +105 -58
  64. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +9 -7
  65. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +143 -49
  66. data/lib/contrast/agent/reporting/reporting_workers/application_server_worker.rb +46 -0
  67. data/lib/contrast/agent/reporting/reporting_workers/reporter_heartbeat.rb +51 -0
  68. data/lib/contrast/agent/reporting/reporting_workers/reporting_workers.rb +14 -0
  69. data/lib/contrast/agent/reporting/reporting_workers/server_settings_worker.rb +46 -0
  70. data/lib/contrast/agent/reporting/settings/assess.rb +14 -1
  71. data/lib/contrast/agent/reporting/settings/assess_rule.rb +18 -0
  72. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +14 -2
  73. data/lib/contrast/agent/reporting/settings/helpers.rb +9 -0
  74. data/lib/contrast/agent/reporting/settings/protect.rb +17 -12
  75. data/lib/contrast/agent/reporting/settings/protect_rule.rb +18 -0
  76. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +40 -3
  77. data/lib/contrast/agent/reporting/settings/rule_definition.rb +3 -0
  78. data/lib/contrast/agent/reporting/settings/security_logger.rb +77 -0
  79. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +1 -1
  80. data/lib/contrast/agent/reporting/settings/server_features.rb +9 -0
  81. data/lib/contrast/agent/reporting/settings/syslog.rb +34 -5
  82. data/lib/contrast/agent/reporting/settings/virtual_patch.rb +56 -0
  83. data/lib/contrast/agent/reporting/settings/virtual_patch_condition.rb +47 -0
  84. data/lib/contrast/agent/request.rb +1 -0
  85. data/lib/contrast/agent/request_context_extend.rb +20 -0
  86. data/lib/contrast/agent/request_handler.rb +5 -10
  87. data/lib/contrast/agent/telemetry/base.rb +11 -10
  88. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +108 -103
  89. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +1 -1
  90. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +1 -1
  91. data/lib/contrast/agent/thread_watcher.rb +46 -6
  92. data/lib/contrast/agent/version.rb +1 -1
  93. data/lib/contrast/agent.rb +18 -0
  94. data/lib/contrast/agent_lib/api/init.rb +1 -7
  95. data/lib/contrast/agent_lib/api/input_tracing.rb +2 -4
  96. data/lib/contrast/agent_lib/interface.rb +1 -16
  97. data/lib/contrast/agent_lib/interface_base.rb +52 -39
  98. data/lib/contrast/agent_lib/return_types/eval_result.rb +2 -2
  99. data/lib/contrast/api/communication/connection_status.rb +15 -0
  100. data/lib/contrast/components/agent.rb +34 -0
  101. data/lib/contrast/components/api.rb +23 -0
  102. data/lib/contrast/components/app_context.rb +23 -3
  103. data/lib/contrast/components/assess.rb +60 -8
  104. data/lib/contrast/components/assess_rules.rb +18 -0
  105. data/lib/contrast/components/base.rb +40 -0
  106. data/lib/contrast/components/config/sources.rb +95 -0
  107. data/lib/contrast/components/config.rb +18 -1
  108. data/lib/contrast/components/heap_dump.rb +10 -0
  109. data/lib/contrast/components/inventory.rb +15 -2
  110. data/lib/contrast/components/logger.rb +18 -0
  111. data/lib/contrast/components/polling.rb +39 -0
  112. data/lib/contrast/components/protect.rb +48 -1
  113. data/lib/contrast/components/ruby_component.rb +15 -0
  114. data/lib/contrast/components/sampling.rb +70 -13
  115. data/lib/contrast/components/security_logger.rb +13 -0
  116. data/lib/contrast/components/settings.rb +120 -10
  117. data/lib/contrast/config/certification_configuration.rb +14 -0
  118. data/lib/contrast/config/config.rb +46 -0
  119. data/lib/contrast/config/diagnostics.rb +114 -0
  120. data/lib/contrast/config/diagnostics_tools.rb +98 -0
  121. data/lib/contrast/config/effective_config.rb +65 -0
  122. data/lib/contrast/config/effective_config_value.rb +32 -0
  123. data/lib/contrast/config/exception_configuration.rb +12 -0
  124. data/lib/contrast/config/protect_rule_configuration.rb +2 -2
  125. data/lib/contrast/config/protect_rules_configuration.rb +7 -6
  126. data/lib/contrast/config/request_audit_configuration.rb +13 -0
  127. data/lib/contrast/config/server_configuration.rb +41 -2
  128. data/lib/contrast/configuration.rb +28 -2
  129. data/lib/contrast/extension/assess/array.rb +3 -3
  130. data/lib/contrast/extension/assess/erb.rb +1 -1
  131. data/lib/contrast/extension/assess/regexp.rb +2 -2
  132. data/lib/contrast/logger/aliased_logging.rb +48 -15
  133. data/lib/contrast/utils/assess/event_limit_utils.rb +31 -9
  134. data/lib/contrast/utils/assess/trigger_method_utils.rb +5 -4
  135. data/lib/contrast/utils/hash_digest.rb +2 -2
  136. data/lib/contrast/utils/input_classification_base.rb +21 -5
  137. data/lib/contrast/utils/reporting/application_activity_batch_utils.rb +81 -0
  138. data/lib/contrast/utils/routes_sent.rb +60 -0
  139. data/lib/contrast/utils/telemetry.rb +1 -1
  140. data/lib/contrast/utils/telemetry_client.rb +2 -3
  141. data/lib/contrast/utils/timer.rb +16 -0
  142. data/lib/contrast.rb +3 -1
  143. data/resources/protect/policy.json +8 -0
  144. data/ruby-agent.gemspec +6 -2
  145. metadata +43 -24
  146. data/lib/contrast/agent/assess/contrast_event.rb +0 -157
  147. data/lib/contrast/agent/assess/events/event_factory.rb +0 -34
  148. data/lib/contrast/agent/assess/events/source_event.rb +0 -46
  149. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +0 -96
  150. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +0 -83
  151. data/lib/contrast/agent/reporting/details/http_method_tempering_details.rb +0 -27
  152. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +0 -53
  153. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +0 -36
  154. data/lib/contrast/agent_lib/api/method_tempering.rb +0 -29
@@ -10,6 +10,7 @@ module Contrast
10
10
  @last_success = nil
11
11
  @last_failure = nil
12
12
  @startup_messages_sent = false
13
+ @_startup_server_message_sent = false
13
14
  end
14
15
 
15
16
  # Whether we have sent startup message to TeamServer. True after successfully sending startup messages to
@@ -20,6 +21,20 @@ module Contrast
20
21
  @startup_messages_sent
21
22
  end
22
23
 
24
+ # Check to see if we have sent server message and received settings from TS.
25
+ #
26
+ # @return [Boolean]
27
+ def startup_server_message_sent?
28
+ @_startup_server_message_sent
29
+ end
30
+
31
+ # Set the server message status state
32
+ #
33
+ # @return [Boolean]
34
+ def server_response_success!
35
+ @_startup_server_message_sent = true
36
+ end
37
+
23
38
  # The last message sent was successful or not
24
39
  #
25
40
  # @return [Boolean]
@@ -2,11 +2,13 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'rubygems/version'
5
+ require 'contrast/components/base'
5
6
  require 'contrast/agent/rule_set'
6
7
  require 'contrast/components/logger'
7
8
  require 'contrast/components/security_logger'
8
9
  require 'contrast/components/heap_dump'
9
10
  require 'contrast/components/ruby_component'
11
+ require 'contrast/components/polling'
10
12
 
11
13
  module Contrast
12
14
  module Components
@@ -17,11 +19,22 @@ module Contrast
17
19
  class Interface
18
20
  include Contrast::Components::ComponentBase
19
21
 
22
+ # @return [String]
23
+ attr_reader :canon_name
24
+ # @return [Array]
25
+ attr_reader :config_values
26
+
27
+ CANON_NAME = 'agent'
28
+ CONFIG_VALUES = %w[enabled? omit_body?].cs__freeze
29
+
20
30
  def initialize hsh = {}
31
+ @config_values = CONFIG_VALUES
32
+ @canon_name = CANON_NAME
21
33
  return unless hsh
22
34
 
23
35
  @_enable = hsh[:enable]
24
36
  @_omit_body = hsh[:omit_body]
37
+ @_polling = Contrast::Components::Polling::Interface.new(hsh[:polling])
25
38
  @_logger = Contrast::Components::Logger::Interface.new(hsh[:logger])
26
39
  @_security_logger = Contrast::Components::SecurityLogger::Interface.new(hsh[:security_logger])
27
40
  @_ruby = Contrast::Components::Ruby::Interface.new(hsh[:ruby])
@@ -34,6 +47,10 @@ module Contrast
34
47
  @_logger = Contrast::Components::Logger::Interface.new
35
48
  end
36
49
 
50
+ def polling
51
+ @_polling ||= Contrast::Components::Polling::Interface.new
52
+ end
53
+
37
54
  def security_logger
38
55
  return @_security_logger unless @_security_logger.nil?
39
56
 
@@ -87,6 +104,10 @@ module Contrast
87
104
  @_omit_body
88
105
  end
89
106
 
107
+ def disable_agent!
108
+ @_enable = false
109
+ end
110
+
90
111
  def exception_control
91
112
  @_exception_control ||= {
92
113
  enable: true?(ruby.exceptions.capture),
@@ -110,6 +131,19 @@ module Contrast
110
131
  Contrast::Agent::TracePointHook.enable!
111
132
  end
112
133
 
134
+ # Converts current configuration to effective config values class and appends them to
135
+ # EffectiveConfig class.
136
+ #
137
+ # @param effective_config [Contrast::Agent::DiagnosticsConfig::EffectiveConfig]
138
+ def to_effective_config effective_config
139
+ super
140
+ logger&.to_effective_config(effective_config)
141
+ security_logger&.to_effective_config(effective_config)
142
+ ruby&.to_effective_config(effective_config)
143
+ heap_dump&.to_effective_config(effective_config)
144
+ polling&.to_effective_config(effective_config)
145
+ end
146
+
113
147
  protected
114
148
 
115
149
  def retrieve_protect_ruleset
@@ -17,6 +17,10 @@ module Contrast
17
17
  include Contrast::Components::ComponentBase
18
18
  include Contrast::Config::BaseConfiguration
19
19
 
20
+ CANON_NAME = 'api'
21
+ PROXY_NAME = "#{ CANON_NAME }.proxy"
22
+ CONFIG_VALUES = %w[api_key user_name service_key url].cs__freeze
23
+
20
24
  # @return [String]
21
25
  attr_accessor :api_key
22
26
  # @return [String]
@@ -120,8 +124,27 @@ module Contrast
120
124
  @_certification_key_file ||= ::Contrast::CONFIG.api.certificate.key_file
121
125
  end
122
126
 
127
+ # Converts current configuration to effective config values class and appends them to
128
+ # EffectiveConfig class.
129
+ #
130
+ # @param effective_config [Contrast::Agent::DiagnosticsConfig::EffectiveConfig]
131
+ def to_effective_config effective_config
132
+ add_effective_config_values(effective_config, CONFIG_VALUES, CANON_NAME, CONTRAST)
133
+ effective_proxy(effective_config)
134
+ request_audit&.to_effective_config(effective_config)
135
+ certificate&.to_effective_config(effective_config)
136
+ end
137
+
123
138
  private
124
139
 
140
+ # @param effective_config [Contrast::Agent::DiagnosticsConfig::EffectiveConfig]
141
+ def effective_proxy effective_config
142
+ add_single_effective_value(effective_config, ENABLE, proxy_enable.to_s, PROXY_NAME, "#{ CONTRAST }.proxy")
143
+ return unless proxy_url && proxy_enable
144
+
145
+ add_single_effective_value(effective_config, 'url', proxy_url, PROXY_NAME, "#{ CONTRAST }.proxy")
146
+ end
147
+
125
148
  def certification_truly_enabled? config_path
126
149
  return false unless true?(config_path.enable)
127
150
  return true if file_exists?(certification_ca_file) && valid_cert?(certification_ca_file)
@@ -14,7 +14,7 @@ module Contrast
14
14
  # parent_configuration_spec.yaml.
15
15
  # Specifically, this allows for querying the state of the Application,
16
16
  # including the Client, Process, and Server information.
17
- class Interface
17
+ class Interface # rubocop:disable Metrics/ClassLength
18
18
  include Contrast::Components::AppContextExtend
19
19
  include Contrast::Components::ComponentBase
20
20
  include Contrast::Config::BaseConfiguration
@@ -23,6 +23,8 @@ module Contrast
23
23
  DEFAULT_APP_PATH = '/'
24
24
  DEFAULT_SERVER_NAME = 'localhost'
25
25
  DEFAULT_SERVER_PATH = '/'
26
+ CANON_NAME = 'application'
27
+ CONFIG_VALUES = %w[name version language path group tags code metadata session_id session_metadata].cs__freeze
26
28
 
27
29
  # @return [String]
28
30
  attr_accessor :version
@@ -30,15 +32,20 @@ module Contrast
30
32
  attr_accessor :language
31
33
  # @return [String]
32
34
  attr_accessor :group
33
- # @return [String]
34
- attr_accessor :tags
35
+ attr_writer :tags
35
36
  # @return [String]
36
37
  attr_accessor :code
37
38
  # @return [String]
38
39
  attr_accessor :metadata
40
+ # @return [String]
41
+ attr_reader :canon_name
42
+ # @return [Array]
43
+ attr_reader :config_values
39
44
 
40
45
  def initialize hsh = {}
41
46
  original_pid
47
+ @config_values = CONFIG_VALUES
48
+ @canon_name = CANON_NAME
42
49
  return unless hsh
43
50
 
44
51
  @_name = hsh[:name]
@@ -135,6 +142,10 @@ module Contrast
135
142
  end
136
143
  end
137
144
 
145
+ def tags
146
+ stringify_array(@tags)
147
+ end
148
+
138
149
  # Determines if the Process we're currently in matches that of the
139
150
  # Process in which the App Context instance was created.
140
151
  # If it doesn't, that indicates the running context is in a new
@@ -147,6 +158,15 @@ module Contrast
147
158
  current_pid != original_pid
148
159
  end
149
160
 
161
+ # Converts current configuration to effective config values class and appends them to
162
+ # EffectiveConfig class.
163
+ #
164
+ # @param effective_config [Contrast::Agent::DiagnosticsConfig::EffectiveConfig]
165
+ def to_effective_config effective_config
166
+ super
167
+ Contrast::CONFIG.server.to_effective_config(effective_config)
168
+ end
169
+
150
170
  private
151
171
 
152
172
  def original_pid
@@ -15,21 +15,46 @@ module Contrast
15
15
  class Interface # rubocop:disable Metrics/ClassLength
16
16
  include Contrast::Components::ComponentBase
17
17
 
18
- # @return [String, nil]
19
- attr_accessor :tags
20
18
  # @return [Boolean, nil]
21
19
  attr_accessor :enable
22
20
  # @return [Array, nil]
23
- attr_writer :enable_scan_response, :enable_dynamic_sources, :sampling, :rules, :stacktraces
21
+ attr_writer :enable_scan_response, :enable_dynamic_sources, :sampling, :rules, :stacktraces, :tags
22
+ # @return [String]
23
+ attr_reader :canon_name
24
+ # @return [Array]
25
+ attr_reader :config_values
26
+ # @return [Boolean]
27
+ attr_writer :enable_original_object
28
+ # @return [Integer]
29
+ attr_writer :max_context_source_events
30
+ # @return [Integer]
31
+ attr_writer :max_propagation_events
32
+ # @return [Integer]
33
+ attr_writer :max_rule_reported
34
+ # @return [Integer]
35
+ attr_writer :time_limit_threshold
24
36
 
25
37
  DEFAULT_STACKTRACES = 'ALL'
26
38
  DEFAULT_MAX_SOURCE_EVENTS = 50_000
27
39
  DEFAULT_MAX_PROPAGATION_EVENTS = 50_000
28
40
  DEFAULT_MAX_RULE_REPORTED = 100
29
41
  DEFAULT_MAX_RULE_TIME_THRESHOLD = 300_000
30
-
42
+ CANON_NAME = 'assess'
43
+ CONFIG_VALUES = %w[
44
+ enabled?
45
+ tags
46
+ enable_scan_response
47
+ enable_original_object
48
+ stacktraces
49
+ max_context_source_events
50
+ max_propagation_events
51
+ max_rule_reported
52
+ time_limit_threshold
53
+ ].cs__freeze
31
54
  # rubocop:disable Naming/MemoizedInstanceVariableName
32
55
  def initialize hsh = {}
56
+ @config_values = CONFIG_VALUES
57
+ @canon_name = CANON_NAME
33
58
  return unless hsh
34
59
 
35
60
  @enable = hsh[:enable]
@@ -40,10 +65,7 @@ module Contrast
40
65
  @sampling = Contrast::Components::Sampling::Interface.new(hsh[:sampling])
41
66
  @rules = Contrast::Components::AssessRules::Interface.new(hsh[:rules])
42
67
  @stacktraces = hsh[:stacktraces]
43
- @max_context_source_events = hsh[:max_context_source_events]
44
- @max_propagation_events = hsh[:max_propagation_events]
45
- @max_rule_reported = hsh[:max_rule_reported]
46
- @time_limit_threshold = hsh[:time_limit_threshold]
68
+ assign_limits(hsh)
47
69
  end
48
70
 
49
71
  # @return [Boolean, true]
@@ -91,6 +113,11 @@ module Contrast
91
113
  @max_context_source_events ||= DEFAULT_MAX_SOURCE_EVENTS
92
114
  end
93
115
 
116
+ # @return [String, nil]
117
+ def tags
118
+ stringify_array(@tags)
119
+ end
120
+
94
121
  def enabled?
95
122
  # config overrides if forcibly set
96
123
  return false if forcibly_disabled?
@@ -187,6 +214,16 @@ module Contrast
187
214
  ::Contrast::SETTINGS.assess_state.session_id
188
215
  end
189
216
 
217
+ # Converts current configuration to effective config values class and appends them to
218
+ # EffectiveConfig class.
219
+ #
220
+ # @param effective_config [Contrast::Agent::DiagnosticsConfig::EffectiveConfig]
221
+ def to_effective_config effective_config
222
+ super
223
+ sampling&.to_effective_config(effective_config)
224
+ rules&.to_effective_config(effective_config)
225
+ end
226
+
190
227
  # rubocop:enable Naming/MemoizedInstanceVariableName
191
228
  private
192
229
 
@@ -194,6 +231,21 @@ module Contrast
194
231
  @_forcibly_enabled = true?(::Contrast::CONFIG.assess.enable) if @_forcibly_enabled.nil?
195
232
  @_forcibly_enabled
196
233
  end
234
+
235
+ # Sets Event limits from configuration and converts string numbers to integers.
236
+ def assign_limits hsh
237
+ return unless hsh
238
+
239
+ source_limit = hsh[:max_context_source_events]&.to_i
240
+ propagation_limit = hsh[:max_propagation_events]&.to_i
241
+ max_rule_reporter = hsh[:max_rule_reported]&.to_i
242
+ time_limit = hsh[:time_limit_threshold]&.to_i
243
+
244
+ @max_context_source_events = source_limit if source_limit
245
+ @max_propagation_events = propagation_limit if propagation_limit
246
+ @max_rule_reported = max_rule_reporter if max_rule_reporter
247
+ @time_limit_threshold = time_limit if time_limit
248
+ end
197
249
  end
198
250
  end
199
251
  end
@@ -2,6 +2,8 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/config/base_configuration'
5
+ require 'contrast/utils/object_share'
6
+ require 'contrast/components/base'
5
7
 
6
8
  module Contrast
7
9
  module Components
@@ -10,17 +12,33 @@ module Contrast
10
12
  module AssessRules
11
13
  class Interface # :nodoc:
12
14
  include Contrast::Config::BaseConfiguration
15
+ include Contrast::Components::ComponentBase
13
16
 
14
17
  SPEC_KEY = :disabled_rules.cs__freeze
18
+ CANON_NAME = 'assess.rules'
19
+ NAME_PREFIX = "#{ CONTRAST }.#{ CANON_NAME }"
20
+
15
21
  # @return [Array, nil] list of disabled assess rules
16
22
  attr_accessor :disabled_rules
23
+ # @return [String]
24
+ attr_reader :canon_name
17
25
 
18
26
  def initialize hsh = {}
27
+ @canon_name = CANON_NAME
28
+ @disabled_rules = []
19
29
  return unless hsh
20
30
 
21
31
  @disabled_rules = cast_disabled_rules(hsh)
22
32
  end
23
33
 
34
+ # Converts current configuration to effective config values class and appends them to
35
+ # EffectiveConfig class.
36
+ #
37
+ # @param effective_config [Contrast::Agent::DiagnosticsConfig::EffectiveConfig]
38
+ def to_effective_config effective_config
39
+ add_single_effective_value(effective_config, SPEC_KEY.to_s, disabled_rules, canon_name, NAME_PREFIX)
40
+ end
41
+
24
42
  private
25
43
 
26
44
  def cast_disabled_rules hsh
@@ -1,11 +1,33 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'contrast/config/diagnostics_tools'
5
+ require 'contrast/utils/object_share'
6
+
4
7
  module Contrast
5
8
  module Components
6
9
  # All components should inherit from this,
7
10
  # whether Interfaces, InstanceMethods or ClassMethods.
8
11
  module ComponentBase
12
+ include Contrast::Agent::DiagnosticsConfig::DiagnosticsTools
13
+
14
+ CONTRAST = 'contrast'
15
+ ENABLE = 'enable'
16
+
17
+ # Used for config diagnostics. Override per rule.
18
+ #
19
+ # @return [String]
20
+ def canon_name
21
+ Contrast::Utils::ObjectShare::EMPTY_STRING
22
+ end
23
+
24
+ # Used for config diagnostics. Override per rule.
25
+ #
26
+ # @return [Array]
27
+ def config_values
28
+ Contrast::Utils::ObjectShare::EMPTY_ARRAY
29
+ end
30
+
9
31
  # use this to determine if the configuration value is literally boolean
10
32
  # false or some form of the word `false`, regardless of case. It should
11
33
  # be used for those values which default to `true` as they should only
@@ -58,6 +80,24 @@ module Contrast
58
80
 
59
81
  File.exist?(path)
60
82
  end
83
+
84
+ # Converts current configuration to effective config values class and appends them to
85
+ # EffectiveConfig class.
86
+ #
87
+ # @param effective_config [Contrast::Agent::DiagnosticsConfig::EffectiveConfig]
88
+ def to_effective_config effective_config
89
+ add_effective_config_values(effective_config, config_values, canon_name, "#{ CONTRAST }.#{ canon_name }")
90
+ end
91
+
92
+ # attempts to stringifys the config value if it is an array with the join char
93
+ # @param val[Object] val to stringify
94
+ # @param join_char[String, ','] join character defaults to ','
95
+ # @return [String, Object] the stringified val or the object as is
96
+ def stringify_array val, join_char = ','
97
+ return val.join(join_char) if val.cs__is_a?(Array)
98
+
99
+ val
100
+ end
61
101
  end
62
102
  end
63
103
  end
@@ -0,0 +1,95 @@
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/env_configuration_item'
5
+ require 'ougai'
6
+ require 'contrast/configuration'
7
+
8
+ module Contrast
9
+ module Components
10
+ module Config
11
+ # This component encapsulates storing the source for each entry in the config,
12
+ # so that we can report on where the value was set from.
13
+ class Sources
14
+ ENVIRONMENT = 'ENV'
15
+ CLI = 'CLI'
16
+ CONTRASTUI = 'ContrastUI'
17
+ DEFAULT = 'Default'
18
+ YAML = 'YAML'
19
+
20
+ # @return [Hash]
21
+ attr_reader :data
22
+
23
+ def initialize data = {}
24
+ @data = data
25
+ end
26
+
27
+ # Retrieves the current config source for the specified config path. If no source is
28
+ # set then returns the Default value.
29
+ #
30
+ # @param path [String] the canonical name for the config entry (such as api.proxy.enable)
31
+ # @return [String] the source for the entry
32
+ def get path
33
+ data.dig(*parts_for(path)) || DEFAULT
34
+ rescue TypeError
35
+ DEFAULT
36
+ end
37
+
38
+ # Assigns the config source for a specified config path.
39
+ #
40
+ # @param path [String] the canonical name for the config entry (such as api.proxy.enable)
41
+ # @param [String] the source for the entry
42
+ # @return [String] the source type for the entry
43
+ def set path, source
44
+ assign_value(data, parts_for(path), source)
45
+ end
46
+
47
+ # Finds entries within config source data for the specified type, and returns
48
+ # them along with the current values for each.
49
+ #
50
+ # @param type [String] a source type (ENV, CLI, ContrastUI, YAML)
51
+ # @return [Hash] the entries for the provided source, along with the associated values
52
+ def for type
53
+ deep_select(data.dup, type, [])
54
+ end
55
+
56
+ private
57
+
58
+ # @param path [String] the canonical name for a config entry (such as api.proxy.enable)
59
+ # @return [Array] the path split on periods, and converted to symbols
60
+ def parts_for path
61
+ path.split('.').map(&:to_sym)
62
+ end
63
+
64
+ # @param current_level [Hash] all, or some of, the config source information
65
+ # @param parts [Array] the parts for canonical name of the config entry
66
+ # @param source [String] the source to be set for the specified entry
67
+ # @return [Array] the path split on periods, and converted to symbols
68
+ def assign_value current_level, parts, source
69
+ parts[0...-1].each do |segment|
70
+ current_level[segment] ||= {}
71
+ current_level = current_level[segment]
72
+ end
73
+ return unless current_level.cs__is_a?(Hash)
74
+
75
+ current_level[parts[-1]] = source
76
+ end
77
+
78
+ # @param sources [Hash] all, or some of, the config source information
79
+ # @param type [Array] the source type to look for entries of
80
+ # @param path [String] the entries followed to get to this part of the config
81
+ # @return [Hash] the entries for the provided source, along with the associated values
82
+ def deep_select sources, type, path
83
+ sources.each_with_object({}) do |(k, v), grouping|
84
+ if v.cs__is_a?(Hash)
85
+ nested_data = deep_select(v, type, path.dup.append(k.to_sym))
86
+ grouping[k] = nested_data unless nested_data.empty?
87
+ elsif v == type
88
+ grouping[k] = Contrast::CONFIG.config.loaded_config.dig(*path, k)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -4,6 +4,7 @@
4
4
  require 'contrast/utils/env_configuration_item'
5
5
  require 'ougai'
6
6
  require 'contrast/configuration'
7
+ require 'contrast/config/diagnostics'
7
8
 
8
9
  module Contrast
9
10
  module Components
@@ -26,6 +27,7 @@ module Contrast
26
27
  CONTRAST_ENV_MARKER = 'CONTRAST__'
27
28
  CONTRAST_LOG = 'contrast_agent.log'
28
29
  CONTRAST_NAME = 'Contrast Agent'
30
+ DATE_TIME = '%Y-%m-%dT%H:%M:%S.%L%z'
29
31
 
30
32
  class Interface # :nodoc: # rubocop:disable Metrics/ClassLength
31
33
  SESSION_VARIABLES = 'Invalid configuration. '\
@@ -50,7 +52,7 @@ module Contrast
50
52
  @_proto_logger.progname = CONTRAST_NAME
51
53
  @_proto_logger.level = ::Ougai::Logging::Severity::WARN
52
54
  @_proto_logger.formatter = Contrast::Logger::Format.new
53
- @_proto_logger.formatter.datetime_format = '%Y-%m-%dT%H:%M:%S.%L%z'
55
+ @_proto_logger.formatter.datetime_format = DATE_TIME
54
56
  @_proto_logger
55
57
  end
56
58
  end
@@ -109,6 +111,10 @@ module Contrast
109
111
  @config.enable
110
112
  end
111
113
 
114
+ def sources
115
+ @config.sources
116
+ end
117
+
112
118
  def invalid?
113
119
  !valid?
114
120
  end
@@ -130,6 +136,11 @@ module Contrast
130
136
  application.session_metadata
131
137
  end
132
138
 
139
+ # @return [String, nil] the path to the YAML config file, if any.
140
+ def config_file_path
141
+ config.config_file
142
+ end
143
+
133
144
  private
134
145
 
135
146
  # The config has information about how to construct the logger. If the config is invalid, and you want to know
@@ -221,6 +232,11 @@ module Contrast
221
232
  agent.logger.path
222
233
  end
223
234
 
235
+ # This methods is here to add the proper forward towards @config
236
+ def enable= value
237
+ @config.enable = value
238
+ end
239
+
224
240
  # Assign the value from an ENV variable to the Contrast::Config::RootConfiguration object, when
225
241
  # appropriate.
226
242
  #
@@ -233,6 +249,7 @@ module Contrast
233
249
  return unless current_level.nil? == false && current_level.cs__respond_to?(dot_path_array[-1])
234
250
 
235
251
  current_level.send("#{ dot_path_array[-1] }=", value)
252
+ sources.set(dot_path_array.join('.'), Contrast::Components::Config::Sources::ENVIRONMENT)
236
253
  end
237
254
  end
238
255
  end
@@ -10,12 +10,22 @@ module Contrast
10
10
  # Interface used to build the HeapDump settings and component.
11
11
  class Interface
12
12
  include Contrast::Config::BaseConfiguration
13
+ include Contrast::Components::ComponentBase
14
+
15
+ # @return [String]
16
+ attr_reader :canon_name
17
+ # @return [Array]
18
+ attr_reader :config_values
13
19
 
14
20
  DEFAULT_PATH = 'contrast_heap_dumps' # saved
15
21
  DEFAULT_MS = 10_000
16
22
  DEFAULT_COUNT = 5
23
+ CANON_NAME = 'agent.heap_dump'
24
+ CONFIG_VALUES = %w[enable path delay_ms window_ms count clean].cs__freeze
17
25
 
18
26
  def initialize hsh = {}
27
+ @config_values = CONFIG_VALUES
28
+ @canon_name = CANON_NAME
19
29
  return unless hsh
20
30
 
21
31
  @_enable = hsh[:enable]
@@ -11,10 +11,18 @@ module Contrast
11
11
  class Interface
12
12
  include Contrast::Components::ComponentBase
13
13
 
14
- # @return [Array, nil] tags
15
- attr_accessor :tags
14
+ CANON_NAME = 'inventory'
15
+ CONFIG_VALUES = %w[enable analyze_libraries tags].cs__freeze
16
+
17
+ attr_writer :tags
18
+ # @return [String]
19
+ attr_reader :canon_name
20
+ # @return [Array]
21
+ attr_reader :config_values
16
22
 
17
23
  def initialize hsh = {}
24
+ @config_values = CONFIG_VALUES
25
+ @canon_name = CANON_NAME
18
26
  return unless hsh
19
27
 
20
28
  @enable = !false?(hsh[:enable])
@@ -31,6 +39,11 @@ module Contrast
31
39
  def analyze_libraries
32
40
  @analyze_libraries.nil? ? true : @analyze_libraries
33
41
  end
42
+
43
+ # @return [String, nil] tags
44
+ def tags
45
+ stringify_array(@tags)
46
+ end
34
47
  end
35
48
  end
36
49
  end
@@ -13,6 +13,14 @@ module Contrast
13
13
  Contrast::Logger::Log.instance.logger
14
14
  end
15
15
 
16
+ # When calling this method from any instance
17
+ # The CEF logger will be initialized and it's
18
+ # path and level will be set. Since we don't
19
+ # call this method unless protect is started,
20
+ # on Agent Startup no file is created before is
21
+ # needed.
22
+ #
23
+ # @return [Logger]
16
24
  def cef_logger
17
25
  @_cef_logger ||= Contrast::Logger::CEFLog.instance.tap(&:build_logger)
18
26
  end
@@ -23,6 +31,10 @@ module Contrast
23
31
  # instance methods and will initialize new instances for where they're needed
24
32
  class Interface
25
33
  include InstanceMethods
34
+ include Contrast::Components::ComponentBase
35
+
36
+ CANON_NAME = 'agent.logger'
37
+ CONFIG_VALUES = %w[path level progname].cs__freeze
26
38
 
27
39
  # @return [String, nil]
28
40
  attr_accessor :path
@@ -30,8 +42,14 @@ module Contrast
30
42
  attr_accessor :level
31
43
  # @return [String, nil]
32
44
  attr_accessor :progname
45
+ # @return [String]
46
+ attr_reader :canon_name
47
+ # @return [Array]
48
+ attr_reader :config_values
33
49
 
34
50
  def initialize hsh = {}
51
+ @config_values = CONFIG_VALUES
52
+ @canon_name = CANON_NAME
35
53
  return unless hsh
36
54
 
37
55
  @path = hsh[:path]