ddtrace 1.14.0 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (270) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +178 -2
  3. data/ext/ddtrace_profiling_native_extension/NativeExtensionDesign.md +3 -5
  4. data/ext/ddtrace_profiling_native_extension/clock_id.h +0 -3
  5. data/ext/ddtrace_profiling_native_extension/clock_id_from_pthread.c +0 -22
  6. data/ext/ddtrace_profiling_native_extension/clock_id_noop.c +0 -1
  7. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +41 -6
  8. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c +3 -0
  9. data/ext/ddtrace_profiling_native_extension/collectors_stack.c +76 -24
  10. data/ext/ddtrace_profiling_native_extension/collectors_stack.h +1 -1
  11. data/ext/ddtrace_profiling_native_extension/collectors_thread_context.c +207 -32
  12. data/ext/ddtrace_profiling_native_extension/collectors_thread_context.h +1 -1
  13. data/ext/ddtrace_profiling_native_extension/extconf.rb +8 -2
  14. data/ext/ddtrace_profiling_native_extension/http_transport.c +26 -10
  15. data/ext/ddtrace_profiling_native_extension/libdatadog_helpers.c +42 -0
  16. data/ext/ddtrace_profiling_native_extension/libdatadog_helpers.h +6 -0
  17. data/ext/ddtrace_profiling_native_extension/native_extension_helpers.rb +1 -16
  18. data/ext/ddtrace_profiling_native_extension/pid_controller.c +57 -0
  19. data/ext/ddtrace_profiling_native_extension/pid_controller.h +45 -0
  20. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.c +17 -12
  21. data/ext/ddtrace_profiling_native_extension/profiling.c +0 -2
  22. data/ext/ddtrace_profiling_native_extension/stack_recorder.c +74 -37
  23. data/ext/ddtrace_profiling_native_extension/stack_recorder.h +13 -3
  24. data/lib/datadog/appsec/assets/waf_rules/processors.json +92 -0
  25. data/lib/datadog/appsec/assets/waf_rules/recommended.json +698 -75
  26. data/lib/datadog/appsec/assets/waf_rules/scanners.json +114 -0
  27. data/lib/datadog/appsec/assets/waf_rules/strict.json +98 -8
  28. data/lib/datadog/appsec/assets.rb +8 -0
  29. data/lib/datadog/appsec/component.rb +9 -2
  30. data/lib/datadog/appsec/configuration/settings.rb +61 -2
  31. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +8 -6
  32. data/lib/datadog/appsec/contrib/rack/reactive/request.rb +2 -7
  33. data/lib/datadog/appsec/contrib/rack/reactive/request_body.rb +2 -5
  34. data/lib/datadog/appsec/contrib/rack/reactive/response.rb +2 -5
  35. data/lib/datadog/appsec/contrib/rack/request_body_middleware.rb +3 -2
  36. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +23 -9
  37. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +3 -2
  38. data/lib/datadog/appsec/contrib/rails/patcher.rb +9 -3
  39. data/lib/datadog/appsec/contrib/rails/reactive/action.rb +2 -5
  40. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +6 -4
  41. data/lib/datadog/appsec/contrib/sinatra/patcher.rb +13 -7
  42. data/lib/datadog/appsec/contrib/sinatra/reactive/routed.rb +2 -5
  43. data/lib/datadog/appsec/event.rb +106 -50
  44. data/lib/datadog/appsec/monitor/gateway/watcher.rb +3 -3
  45. data/lib/datadog/appsec/monitor/reactive/set_user.rb +2 -5
  46. data/lib/datadog/appsec/processor/actions.rb +49 -0
  47. data/lib/datadog/appsec/processor/rule_merger.rb +22 -2
  48. data/lib/datadog/appsec/processor.rb +34 -6
  49. data/lib/datadog/appsec/remote.rb +4 -1
  50. data/lib/datadog/appsec/response.rb +82 -4
  51. data/lib/datadog/appsec/sample_rate.rb +21 -0
  52. data/lib/datadog/appsec.rb +2 -2
  53. data/lib/datadog/core/configuration/agent_settings_resolver.rb +29 -24
  54. data/lib/datadog/core/configuration/base.rb +1 -11
  55. data/lib/datadog/core/configuration/components.rb +7 -2
  56. data/lib/datadog/core/configuration/ext.rb +21 -0
  57. data/lib/datadog/core/configuration/option.rb +2 -4
  58. data/lib/datadog/core/configuration/option_definition.rb +17 -41
  59. data/lib/datadog/core/configuration/options.rb +5 -5
  60. data/lib/datadog/core/configuration/settings.rb +47 -45
  61. data/lib/datadog/core/environment/execution.rb +47 -9
  62. data/lib/datadog/core/environment/variable_helpers.rb +0 -69
  63. data/lib/datadog/core/error.rb +1 -0
  64. data/lib/datadog/core/git/ext.rb +2 -0
  65. data/lib/datadog/core/remote/client/capabilities.rb +1 -1
  66. data/lib/datadog/core/remote/component.rb +2 -2
  67. data/lib/datadog/core/remote/negotiation.rb +2 -2
  68. data/lib/datadog/core/remote/transport/config.rb +60 -0
  69. data/lib/datadog/core/remote/transport/http/api/instance.rb +39 -0
  70. data/lib/datadog/core/remote/transport/http/api/spec.rb +21 -0
  71. data/lib/datadog/core/remote/transport/http/api.rb +58 -0
  72. data/lib/datadog/core/remote/transport/http/builder.rb +219 -0
  73. data/lib/datadog/core/remote/transport/http/client.rb +48 -0
  74. data/lib/datadog/core/remote/transport/http/config.rb +280 -0
  75. data/lib/datadog/core/remote/transport/http/negotiation.rb +146 -0
  76. data/lib/datadog/core/remote/transport/http.rb +179 -0
  77. data/lib/datadog/core/{transport → remote/transport}/negotiation.rb +25 -23
  78. data/lib/datadog/core/telemetry/collector.rb +3 -2
  79. data/lib/datadog/core/telemetry/http/transport.rb +2 -1
  80. data/lib/datadog/core/transport/ext.rb +47 -0
  81. data/lib/datadog/core/transport/http/adapters/net.rb +168 -0
  82. data/lib/datadog/core/transport/http/adapters/registry.rb +29 -0
  83. data/lib/datadog/core/transport/http/adapters/test.rb +89 -0
  84. data/lib/datadog/core/transport/http/adapters/unix_socket.rb +83 -0
  85. data/lib/datadog/core/transport/http/api/endpoint.rb +31 -0
  86. data/lib/datadog/core/transport/http/api/fallbacks.rb +26 -0
  87. data/lib/datadog/core/transport/http/api/map.rb +18 -0
  88. data/lib/datadog/core/transport/http/env.rb +62 -0
  89. data/lib/datadog/core/transport/http/response.rb +60 -0
  90. data/lib/datadog/core/transport/parcel.rb +22 -0
  91. data/lib/datadog/core/transport/request.rb +17 -0
  92. data/lib/datadog/core/transport/response.rb +64 -0
  93. data/lib/datadog/core/workers/polling.rb +2 -2
  94. data/lib/datadog/opentelemetry/api/context.rb +10 -3
  95. data/lib/datadog/opentelemetry/sdk/propagator.rb +2 -1
  96. data/lib/datadog/opentelemetry/sdk/span_processor.rb +14 -2
  97. data/lib/datadog/opentelemetry/sdk/trace/span.rb +68 -0
  98. data/lib/datadog/opentelemetry/trace.rb +58 -0
  99. data/lib/datadog/opentelemetry.rb +1 -0
  100. data/lib/datadog/opentracer.rb +9 -0
  101. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +12 -18
  102. data/lib/datadog/profiling/collectors/idle_sampling_helper.rb +1 -1
  103. data/lib/datadog/profiling/collectors/thread_context.rb +9 -1
  104. data/lib/datadog/profiling/component.rb +24 -99
  105. data/lib/datadog/profiling/ext.rb +0 -12
  106. data/lib/datadog/profiling/flush.rb +0 -3
  107. data/lib/datadog/profiling/http_transport.rb +6 -3
  108. data/lib/datadog/profiling/native_extension.rb +0 -21
  109. data/lib/datadog/profiling/profiler.rb +11 -12
  110. data/lib/datadog/profiling.rb +8 -81
  111. data/lib/datadog/tracing/component.rb +10 -4
  112. data/lib/datadog/tracing/configuration/agent_settings_resolver.rb +13 -0
  113. data/lib/datadog/tracing/configuration/ext.rb +4 -2
  114. data/lib/datadog/tracing/configuration/settings.rb +14 -7
  115. data/lib/datadog/tracing/contrib/action_pack/configuration/settings.rb +1 -1
  116. data/lib/datadog/tracing/contrib/active_job/configuration/settings.rb +1 -1
  117. data/lib/datadog/tracing/contrib/active_record/events/sql.rb +4 -0
  118. data/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb +104 -197
  119. data/lib/datadog/tracing/contrib/active_support/cache/patcher.rb +3 -0
  120. data/lib/datadog/tracing/contrib/aws/instrumentation.rb +7 -0
  121. data/lib/datadog/tracing/contrib/configuration/settings.rb +1 -1
  122. data/lib/datadog/tracing/contrib/dalli/configuration/settings.rb +6 -0
  123. data/lib/datadog/tracing/contrib/dalli/ext.rb +7 -0
  124. data/lib/datadog/tracing/contrib/dalli/instrumentation.rb +9 -2
  125. data/lib/datadog/tracing/contrib/delayed_job/configuration/settings.rb +1 -1
  126. data/lib/datadog/tracing/contrib/elasticsearch/patcher.rb +5 -0
  127. data/lib/datadog/tracing/contrib/ethon/easy_patch.rb +5 -0
  128. data/lib/datadog/tracing/contrib/ethon/multi_patch.rb +8 -0
  129. data/lib/datadog/tracing/contrib/excon/middleware.rb +5 -0
  130. data/lib/datadog/tracing/contrib/ext.rb +3 -0
  131. data/lib/datadog/tracing/contrib/faraday/configuration/settings.rb +1 -1
  132. data/lib/datadog/tracing/contrib/faraday/middleware.rb +5 -0
  133. data/lib/datadog/tracing/contrib/grpc/configuration/settings.rb +21 -1
  134. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/client.rb +11 -1
  135. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/server.rb +18 -0
  136. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor.rb +0 -4
  137. data/lib/datadog/tracing/contrib/http/circuit_breaker.rb +3 -3
  138. data/lib/datadog/tracing/contrib/http/instrumentation.rb +5 -0
  139. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +5 -0
  140. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +5 -0
  141. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +7 -0
  142. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +13 -3
  143. data/lib/datadog/tracing/contrib/opensearch/integration.rb +2 -2
  144. data/lib/datadog/tracing/contrib/opensearch/patcher.rb +7 -0
  145. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +5 -0
  146. data/lib/datadog/tracing/contrib/presto/instrumentation.rb +5 -0
  147. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +1 -1
  148. data/lib/datadog/tracing/contrib/que/configuration/settings.rb +1 -1
  149. data/lib/datadog/tracing/contrib/racecar/event.rb +5 -0
  150. data/lib/datadog/tracing/contrib/rack/header_tagging.rb +14 -4
  151. data/lib/datadog/tracing/contrib/rails/configuration/settings.rb +4 -4
  152. data/lib/datadog/tracing/contrib/rake/configuration/settings.rb +1 -1
  153. data/lib/datadog/tracing/contrib/redis/tags.rb +5 -0
  154. data/lib/datadog/tracing/contrib/resque/configuration/settings.rb +1 -1
  155. data/lib/datadog/tracing/contrib/rest_client/request_patch.rb +5 -0
  156. data/lib/datadog/tracing/contrib/sequel/utils.rb +5 -0
  157. data/lib/datadog/tracing/contrib/shoryuken/configuration/settings.rb +1 -1
  158. data/lib/datadog/tracing/contrib/sidekiq/configuration/settings.rb +1 -1
  159. data/lib/datadog/tracing/contrib/sneakers/configuration/settings.rb +1 -1
  160. data/lib/datadog/tracing/contrib/utils/quantization/http.rb +2 -2
  161. data/lib/datadog/tracing/distributed/propagation.rb +13 -33
  162. data/lib/datadog/tracing/metadata/tagging.rb +3 -3
  163. data/lib/datadog/tracing/sync_writer.rb +3 -3
  164. data/lib/datadog/tracing/tracer.rb +2 -0
  165. data/lib/datadog/{core → tracing}/transport/http/api/instance.rb +1 -1
  166. data/lib/datadog/{core → tracing}/transport/http/api/spec.rb +1 -1
  167. data/lib/datadog/tracing/transport/http/api.rb +43 -0
  168. data/lib/datadog/{core → tracing}/transport/http/builder.rb +13 -68
  169. data/lib/datadog/tracing/transport/http/client.rb +57 -0
  170. data/lib/datadog/tracing/transport/http/statistics.rb +47 -0
  171. data/lib/datadog/tracing/transport/http/traces.rb +152 -0
  172. data/lib/datadog/tracing/transport/http.rb +124 -0
  173. data/lib/datadog/tracing/transport/io/client.rb +89 -0
  174. data/lib/datadog/tracing/transport/io/response.rb +27 -0
  175. data/lib/datadog/tracing/transport/io/traces.rb +101 -0
  176. data/lib/datadog/tracing/transport/io.rb +30 -0
  177. data/lib/datadog/tracing/transport/serializable_trace.rb +126 -0
  178. data/lib/datadog/tracing/transport/statistics.rb +77 -0
  179. data/lib/datadog/tracing/transport/trace_formatter.rb +209 -0
  180. data/lib/datadog/tracing/transport/traces.rb +224 -0
  181. data/lib/datadog/tracing/workers/trace_writer.rb +5 -3
  182. data/lib/datadog/tracing/workers.rb +3 -2
  183. data/lib/datadog/tracing/writer.rb +5 -2
  184. data/lib/ddtrace/transport/ext.rb +17 -15
  185. data/lib/ddtrace/version.rb +1 -1
  186. data/lib/ddtrace.rb +1 -1
  187. metadata +72 -96
  188. data/lib/datadog/ci/configuration/components.rb +0 -32
  189. data/lib/datadog/ci/configuration/settings.rb +0 -51
  190. data/lib/datadog/ci/contrib/cucumber/configuration/settings.rb +0 -35
  191. data/lib/datadog/ci/contrib/cucumber/ext.rb +0 -22
  192. data/lib/datadog/ci/contrib/cucumber/formatter.rb +0 -94
  193. data/lib/datadog/ci/contrib/cucumber/instrumentation.rb +0 -28
  194. data/lib/datadog/ci/contrib/cucumber/integration.rb +0 -47
  195. data/lib/datadog/ci/contrib/cucumber/patcher.rb +0 -27
  196. data/lib/datadog/ci/contrib/minitest/configuration/settings.rb +0 -35
  197. data/lib/datadog/ci/contrib/minitest/ext.rb +0 -21
  198. data/lib/datadog/ci/contrib/minitest/integration.rb +0 -49
  199. data/lib/datadog/ci/contrib/minitest/patcher.rb +0 -27
  200. data/lib/datadog/ci/contrib/minitest/test_helper.rb +0 -68
  201. data/lib/datadog/ci/contrib/rspec/configuration/settings.rb +0 -35
  202. data/lib/datadog/ci/contrib/rspec/example.rb +0 -68
  203. data/lib/datadog/ci/contrib/rspec/ext.rb +0 -21
  204. data/lib/datadog/ci/contrib/rspec/integration.rb +0 -48
  205. data/lib/datadog/ci/contrib/rspec/patcher.rb +0 -27
  206. data/lib/datadog/ci/ext/app_types.rb +0 -9
  207. data/lib/datadog/ci/ext/environment.rb +0 -575
  208. data/lib/datadog/ci/ext/settings.rb +0 -10
  209. data/lib/datadog/ci/ext/test.rb +0 -35
  210. data/lib/datadog/ci/extensions.rb +0 -19
  211. data/lib/datadog/ci/flush.rb +0 -38
  212. data/lib/datadog/ci/test.rb +0 -81
  213. data/lib/datadog/ci.rb +0 -21
  214. data/lib/datadog/core/configuration/dependency_resolver.rb +0 -28
  215. data/lib/datadog/core/configuration/option_definition_set.rb +0 -22
  216. data/lib/datadog/core/configuration/option_set.rb +0 -10
  217. data/lib/datadog/core/transport/config.rb +0 -58
  218. data/lib/datadog/core/transport/http/api.rb +0 -57
  219. data/lib/datadog/core/transport/http/client.rb +0 -45
  220. data/lib/datadog/core/transport/http/config.rb +0 -278
  221. data/lib/datadog/core/transport/http/negotiation.rb +0 -144
  222. data/lib/datadog/core/transport/http.rb +0 -169
  223. data/lib/datadog/core/utils/object_set.rb +0 -43
  224. data/lib/datadog/core/utils/string_table.rb +0 -47
  225. data/lib/datadog/profiling/backtrace_location.rb +0 -34
  226. data/lib/datadog/profiling/buffer.rb +0 -43
  227. data/lib/datadog/profiling/collectors/old_stack.rb +0 -301
  228. data/lib/datadog/profiling/encoding/profile.rb +0 -41
  229. data/lib/datadog/profiling/event.rb +0 -15
  230. data/lib/datadog/profiling/events/stack.rb +0 -82
  231. data/lib/datadog/profiling/old_recorder.rb +0 -107
  232. data/lib/datadog/profiling/pprof/builder.rb +0 -125
  233. data/lib/datadog/profiling/pprof/converter.rb +0 -102
  234. data/lib/datadog/profiling/pprof/message_set.rb +0 -16
  235. data/lib/datadog/profiling/pprof/payload.rb +0 -20
  236. data/lib/datadog/profiling/pprof/pprof.proto +0 -212
  237. data/lib/datadog/profiling/pprof/pprof_pb.rb +0 -81
  238. data/lib/datadog/profiling/pprof/stack_sample.rb +0 -139
  239. data/lib/datadog/profiling/pprof/string_table.rb +0 -12
  240. data/lib/datadog/profiling/pprof/template.rb +0 -118
  241. data/lib/datadog/profiling/trace_identifiers/ddtrace.rb +0 -43
  242. data/lib/datadog/profiling/trace_identifiers/helper.rb +0 -45
  243. data/lib/ddtrace/transport/http/adapters/net.rb +0 -168
  244. data/lib/ddtrace/transport/http/adapters/registry.rb +0 -27
  245. data/lib/ddtrace/transport/http/adapters/test.rb +0 -85
  246. data/lib/ddtrace/transport/http/adapters/unix_socket.rb +0 -77
  247. data/lib/ddtrace/transport/http/api/endpoint.rb +0 -29
  248. data/lib/ddtrace/transport/http/api/fallbacks.rb +0 -24
  249. data/lib/ddtrace/transport/http/api/instance.rb +0 -35
  250. data/lib/ddtrace/transport/http/api/map.rb +0 -16
  251. data/lib/ddtrace/transport/http/api/spec.rb +0 -17
  252. data/lib/ddtrace/transport/http/api.rb +0 -39
  253. data/lib/ddtrace/transport/http/builder.rb +0 -176
  254. data/lib/ddtrace/transport/http/client.rb +0 -52
  255. data/lib/ddtrace/transport/http/env.rb +0 -58
  256. data/lib/ddtrace/transport/http/response.rb +0 -58
  257. data/lib/ddtrace/transport/http/statistics.rb +0 -43
  258. data/lib/ddtrace/transport/http/traces.rb +0 -144
  259. data/lib/ddtrace/transport/http.rb +0 -117
  260. data/lib/ddtrace/transport/io/client.rb +0 -85
  261. data/lib/ddtrace/transport/io/response.rb +0 -25
  262. data/lib/ddtrace/transport/io/traces.rb +0 -99
  263. data/lib/ddtrace/transport/io.rb +0 -28
  264. data/lib/ddtrace/transport/parcel.rb +0 -20
  265. data/lib/ddtrace/transport/request.rb +0 -15
  266. data/lib/ddtrace/transport/response.rb +0 -60
  267. data/lib/ddtrace/transport/serializable_trace.rb +0 -122
  268. data/lib/ddtrace/transport/statistics.rb +0 -75
  269. data/lib/ddtrace/transport/trace_formatter.rb +0 -207
  270. data/lib/ddtrace/transport/traces.rb +0 -216
@@ -38,11 +38,8 @@ module Datadog
38
38
  when :match
39
39
  Datadog.logger.debug { "WAF: #{result.inspect}" }
40
40
 
41
- block = result.actions.include?('block')
42
-
43
- yield [result, block]
44
-
45
- throw(:block, [result, true]) if block
41
+ yield result
42
+ throw(:block, true) unless result.actions.empty?
46
43
  when :ok
47
44
  Datadog.logger.debug { "WAF OK: #{result.inspect}" }
48
45
  when :invalid_call
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module AppSec
5
+ class Processor
6
+ # Actions store the actions information in memory
7
+ # Also, takes care of merging when RC send new information
8
+ module Actions
9
+ class << self
10
+ def actions
11
+ @actions ||= []
12
+ end
13
+
14
+ def fecth_configuration(action)
15
+ actions.find { |action_configuration| action_configuration['id'] == action }
16
+ end
17
+
18
+ def merge(actions_to_merge)
19
+ return if actions_to_merge.empty?
20
+
21
+ if actions.empty?
22
+ @actions = actions_to_merge
23
+ else
24
+ merged_actions = []
25
+ actions_dup = actions.dup
26
+
27
+ actions_to_merge.each do |new_action|
28
+ existing_action = actions_dup.find { |action| new_action['id'] == action['id'] }
29
+
30
+ # the old action is discard and the new kept
31
+ actions_dup.delete(existing_action) if existing_action
32
+ merged_actions << new_action
33
+ end
34
+
35
+ @actions = merged_actions.concat(actions_dup)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ # Used in tests
42
+ def reset
43
+ @actions = []
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../assets'
4
+
3
5
  module Datadog
4
6
  module AppSec
5
7
  class Processor
@@ -16,8 +18,25 @@ module Datadog
16
18
  end
17
19
  end
18
20
 
21
+ DEFAULT_WAF_PROCESSORS = begin
22
+ JSON.parse(Datadog::AppSec::Assets.waf_processors)
23
+ rescue StandardError => e
24
+ Datadog.logger.error { "libddwaf rulemerger failed to parse default waf processors. Error: #{e.inspect}" }
25
+ []
26
+ end
27
+
28
+ DEFAULT_WAF_SCANNERS = begin
29
+ JSON.parse(Datadog::AppSec::Assets.waf_scanners)
30
+ rescue StandardError => e
31
+ Datadog.logger.error { "libddwaf rulemerger failed to parse default waf scanners. Error: #{e.inspect}" }
32
+ []
33
+ end
34
+
19
35
  class << self
20
- def merge(rules:, data: [], overrides: [], exclusions: [], custom_rules: [])
36
+ def merge(
37
+ rules:, data: [], overrides: [], exclusions: [], custom_rules: [],
38
+ processors: DEFAULT_WAF_PROCESSORS, scanners: DEFAULT_WAF_SCANNERS
39
+ )
21
40
  combined_rules = combine_rules(rules)
22
41
 
23
42
  combined_data = combine_data(data) if data.any?
@@ -29,7 +48,8 @@ module Datadog
29
48
  combined_rules['rules_override'] = combined_overrides if combined_overrides
30
49
  combined_rules['exclusions'] = combined_exclusions if combined_exclusions
31
50
  combined_rules['custom_rules'] = combined_custom_rules if combined_custom_rules
32
-
51
+ combined_rules['processors'] = processors
52
+ combined_rules['scanners'] = scanners
33
53
  combined_rules
34
54
  end
35
55
 
@@ -22,8 +22,15 @@ module Datadog
22
22
 
23
23
  start_ns = Core::Utils::Time.get_time(:nanosecond)
24
24
 
25
- # this WAF::Context#run call is not thread safe as it mutates the context
26
- # TODO: remove multiple assignment
25
+ input.reject! do |_, v|
26
+ case v
27
+ when TrueClass, FalseClass
28
+ false
29
+ else
30
+ v.nil? ? true : v.empty?
31
+ end
32
+ end
33
+
27
34
  _code, res = @context.run(input, timeout)
28
35
 
29
36
  stop_ns = Core::Utils::Time.get_time(:nanosecond)
@@ -38,15 +45,36 @@ module Datadog
38
45
  @run_mutex.unlock
39
46
  end
40
47
 
48
+ def extract_schema
49
+ return unless extract_schema?
50
+
51
+ input = {
52
+ 'waf.context.processor' => {
53
+ 'extract-schema' => true
54
+ }
55
+ }
56
+
57
+ _code, res = @context.run(input, WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
58
+
59
+ res
60
+ end
61
+
41
62
  def finalize
42
63
  @context.finalize
43
64
  end
65
+
66
+ private
67
+
68
+ def extract_schema?
69
+ Datadog.configuration.appsec.api_security.enabled &&
70
+ Datadog.configuration.appsec.api_security.sample_rate.sample?
71
+ end
44
72
  end
45
73
 
46
- attr_reader :ruleset_info, :addresses
74
+ attr_reader :diagnostics, :addresses
47
75
 
48
76
  def initialize(ruleset:)
49
- @ruleset_info = nil
77
+ @diagnostics = nil
50
78
  @addresses = []
51
79
  settings = Datadog.configuration.appsec
52
80
 
@@ -83,7 +111,7 @@ module Datadog
83
111
  }
84
112
 
85
113
  @handle = Datadog::AppSec::WAF::Handle.new(ruleset, obfuscator: obfuscator_config)
86
- @ruleset_info = @handle.ruleset_info
114
+ @diagnostics = @handle.diagnostics
87
115
  @addresses = @handle.required_addresses
88
116
 
89
117
  true
@@ -92,7 +120,7 @@ module Datadog
92
120
  "libddwaf failed to initialize, error: #{e.inspect}"
93
121
  end
94
122
 
95
- @ruleset_info = e.ruleset_info if e.ruleset_info
123
+ @diagnostics = e.diagnostics if e.diagnostics
96
124
 
97
125
  false
98
126
  rescue StandardError => e
@@ -31,6 +31,7 @@ module Datadog
31
31
  CAP_ASM_RESPONSE_BLOCKING,
32
32
  CAP_ASM_DD_RULES,
33
33
  CAP_ASM_CUSTOM_RULES,
34
+ CAP_ASM_CUSTOM_BLOCKING_RESPONSE,
34
35
  ].freeze
35
36
 
36
37
  ASM_PRODUCTS = [
@@ -63,6 +64,7 @@ module Datadog
63
64
  data = []
64
65
  overrides = []
65
66
  exclusions = []
67
+ actions = []
66
68
 
67
69
  repository.contents.each do |content|
68
70
  parsed_content = parse_content(content)
@@ -76,6 +78,7 @@ module Datadog
76
78
  overrides << parsed_content['rules_override'] if parsed_content['rules_override']
77
79
  exclusions << parsed_content['exclusions'] if parsed_content['exclusions']
78
80
  custom_rules << parsed_content['custom_rules'] if parsed_content['custom_rules']
81
+ actions.concat(parsed_content['actions']) if parsed_content['actions']
79
82
  end
80
83
  end
81
84
 
@@ -95,7 +98,7 @@ module Datadog
95
98
  custom_rules: custom_rules,
96
99
  )
97
100
 
98
- Datadog::AppSec.reconfigure(ruleset: ruleset)
101
+ Datadog::AppSec.reconfigure(ruleset: ruleset, actions: actions)
99
102
  end
100
103
 
101
104
  [receiver]
@@ -28,19 +28,80 @@ module Datadog
28
28
  end
29
29
 
30
30
  class << self
31
- def negotiate(env)
31
+ def negotiate(env, actions)
32
+ # @type var configured_response: Response?
33
+ configured_response = nil
34
+ actions.each do |action|
35
+ # Need to use next to make steep happy :(
36
+ # I rather use break to stop the execution
37
+ next if configured_response
38
+
39
+ action_configuration = AppSec::Processor::Actions.fecth_configuration(action)
40
+ next unless action_configuration
41
+
42
+ configured_response = case action_configuration['type']
43
+ when 'block_request'
44
+ block_response(env, action_configuration['parameters'])
45
+ when 'redirect_request'
46
+ redirect_response(env, action_configuration['parameters'])
47
+ end
48
+ end
49
+
50
+ configured_response || default_response(env)
51
+ end
52
+
53
+ private
54
+
55
+ def default_response(env)
32
56
  content_type = content_type(env)
33
57
 
34
- Datadog.logger.debug { "negotiated response content type: #{content_type}" }
58
+ body = []
59
+ body << content(content_type)
35
60
 
36
61
  Response.new(
37
62
  status: 403,
38
63
  headers: { 'Content-Type' => content_type },
39
- body: [Datadog::AppSec::Assets.blocked(format: CONTENT_TYPE_TO_FORMAT[content_type])]
64
+ body: body,
40
65
  )
41
66
  end
42
67
 
43
- private
68
+ def block_response(env, options)
69
+ content_type = if options['type'] == 'auto'
70
+ content_type(env)
71
+ else
72
+ FORMAT_TO_CONTENT_TYPE[options['type']]
73
+ end
74
+
75
+ body = []
76
+ body << content(content_type)
77
+
78
+ Response.new(
79
+ status: options['status_code'] || 403,
80
+ headers: { 'Content-Type' => content_type },
81
+ body: body,
82
+ )
83
+ end
84
+
85
+ def redirect_response(env, options)
86
+ if options['location'] && !options['location'].empty?
87
+ content_type = content_type(env)
88
+
89
+ status = options['status_code'] >= 300 && options['status_code'] < 400 ? options['status_code'] : 303
90
+
91
+ headers = {
92
+ 'Content-Type' => content_type,
93
+ 'Location' => options['location']
94
+ }
95
+
96
+ Response.new(
97
+ status: status,
98
+ headers: headers,
99
+ body: [],
100
+ )
101
+ else
102
+ default_response(env)
103
+ end
104
+ end
44
105
 
45
106
  CONTENT_TYPE_TO_FORMAT = {
46
107
  'application/json' => :json,
@@ -48,6 +109,11 @@ module Datadog
48
109
  'text/plain' => :text,
49
110
  }.freeze
50
111
 
112
+ FORMAT_TO_CONTENT_TYPE = {
113
+ 'json' => 'application/json',
114
+ 'html' => 'text/html',
115
+ }.freeze
116
+
51
117
  DEFAULT_CONTENT_TYPE = 'application/json'
52
118
 
53
119
  def content_type(env)
@@ -67,6 +133,18 @@ module Datadog
67
133
  rescue Datadog::AppSec::Utils::HTTP::MediaRange::ParseError
68
134
  DEFAULT_CONTENT_TYPE
69
135
  end
136
+
137
+ def content(content_type)
138
+ content_format = CONTENT_TYPE_TO_FORMAT[content_type]
139
+
140
+ using_default = Datadog.configuration.appsec.block.templates.using_default?(content_format)
141
+
142
+ if using_default
143
+ Datadog::AppSec::Assets.blocked(format: content_format)
144
+ else
145
+ Datadog.configuration.appsec.block.templates.send(content_format)
146
+ end
147
+ end
70
148
  end
71
149
  end
72
150
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module AppSec
5
+ # SampleRate basic sample rate
6
+ class SampleRate
7
+ attr_reader :rate
8
+
9
+ def initialize(rate)
10
+ @rate = rate
11
+ end
12
+
13
+ def sample?
14
+ return false if rate <= 0
15
+ return true if rate >= 1
16
+
17
+ Kernel.rand < rate
18
+ end
19
+ end
20
+ end
21
+ end
@@ -23,12 +23,12 @@ module Datadog
23
23
  appsec_component.processor if appsec_component
24
24
  end
25
25
 
26
- def reconfigure(ruleset:)
26
+ def reconfigure(ruleset:, actions:)
27
27
  appsec_component = components.appsec
28
28
 
29
29
  return unless appsec_component
30
30
 
31
- appsec_component.reconfigure(ruleset: ruleset)
31
+ appsec_component.reconfigure(ruleset: ruleset, actions: actions)
32
32
  end
33
33
 
34
34
  def reconfigure_lock(&block)
@@ -1,8 +1,8 @@
1
1
  require 'uri'
2
2
 
3
3
  require_relative 'settings'
4
- require_relative '../../tracing/configuration/ext'
5
- require_relative '../../../ddtrace/transport/ext'
4
+ require_relative 'ext'
5
+ require_relative '../transport/ext'
6
6
 
7
7
  module Datadog
8
8
  module Core
@@ -16,6 +16,7 @@ module Datadog
16
16
  #
17
17
  # Whenever there is a conflict (different configurations are provided in different orders), it MUST warn the users
18
18
  # about it and pick a value based on the following priority: code > environment variable > defaults.
19
+ # DEV-2.0: The deprecated_for_removal_transport_configuration_proc should be removed.
19
20
  class AgentSettingsResolver
20
21
  AgentSettings = \
21
22
  Struct.new(
@@ -66,9 +67,9 @@ module Datadog
66
67
 
67
68
  def call
68
69
  # A transport_options proc configured for unix domain socket overrides most of the logic on this file
69
- if transport_options.adapter == Datadog::Transport::Ext::UnixSocket::ADAPTER
70
+ if transport_options.adapter == Datadog::Core::Transport::Ext::UnixSocket::ADAPTER
70
71
  return AgentSettings.new(
71
- adapter: Datadog::Transport::Ext::UnixSocket::ADAPTER,
72
+ adapter: Datadog::Core::Transport::Ext::UnixSocket::ADAPTER,
72
73
  ssl: false,
73
74
  hostname: nil,
74
75
  port: nil,
@@ -96,9 +97,9 @@ module Datadog
96
97
 
97
98
  def adapter
98
99
  if should_use_uds?
99
- Datadog::Transport::Ext::UnixSocket::ADAPTER
100
+ Datadog::Core::Transport::Ext::UnixSocket::ADAPTER
100
101
  else
101
- Datadog::Transport::Ext::HTTP::ADAPTER
102
+ Datadog::Core::Transport::Ext::HTTP::ADAPTER
102
103
  end
103
104
  end
104
105
 
@@ -115,7 +116,7 @@ module Datadog
115
116
  value: settings.agent.host
116
117
  ),
117
118
  DetectedConfiguration.new(
118
- friendly_name: "#{Datadog::Tracing::Configuration::Ext::Transport::ENV_DEFAULT_URL} environment variable",
119
+ friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} environment variable",
119
120
  value: parsed_http_url && parsed_http_url.hostname
120
121
  ),
121
122
  DetectedConfiguration.new(
@@ -138,12 +139,12 @@ module Datadog
138
139
  value: settings.agent.port,
139
140
  ),
140
141
  DetectedConfiguration.new(
141
- friendly_name: "#{Datadog::Tracing::Configuration::Ext::Transport::ENV_DEFAULT_URL} environment variable",
142
+ friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} environment variable",
142
143
  value: parsed_http_url && parsed_http_url.port,
143
144
  ),
144
145
  try_parsing_as_integer(
145
- friendly_name: "#{Datadog::Tracing::Configuration::Ext::Transport::ENV_DEFAULT_PORT} environment variable",
146
- value: ENV[Datadog::Tracing::Configuration::Ext::Transport::ENV_DEFAULT_PORT],
146
+ friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_PORT} environment variable",
147
+ value: ENV[Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_PORT],
147
148
  )
148
149
  )
149
150
  end
@@ -167,11 +168,11 @@ module Datadog
167
168
  end
168
169
 
169
170
  def hostname
170
- configured_hostname || (should_use_uds? ? nil : Datadog::Transport::Ext::HTTP::DEFAULT_HOST)
171
+ configured_hostname || (should_use_uds? ? nil : Datadog::Core::Configuration::Ext::Agent::HTTP::DEFAULT_HOST)
171
172
  end
172
173
 
173
174
  def port
174
- configured_port || (should_use_uds? ? nil : Datadog::Transport::Ext::HTTP::DEFAULT_PORT)
175
+ configured_port || (should_use_uds? ? nil : Datadog::Core::Configuration::Ext::Agent::HTTP::DEFAULT_PORT)
175
176
  end
176
177
 
177
178
  # Unix socket path in the file system
@@ -201,8 +202,12 @@ module Datadog
201
202
  # In transport_options, we try to invoke the transport_options proc and get its configuration. In case that
202
203
  # doesn't work, we include the proc directly in the agent settings result.
203
204
  def deprecated_for_removal_transport_configuration_proc
204
- if settings.tracing.transport_options.is_a?(Proc) && transport_options.adapter.nil?
205
- settings.tracing.transport_options
205
+ transport_options_settings if transport_options_settings.is_a?(Proc) && transport_options.adapter.nil?
206
+ end
207
+
208
+ def transport_options_settings
209
+ @transport_options_settings ||= begin
210
+ settings.tracing.transport_options if settings.respond_to?(:tracing) && settings.tracing
206
211
  end
207
212
  end
208
213
 
@@ -215,9 +220,9 @@ module Datadog
215
220
  if configured_hostname.nil? &&
216
221
  configured_port.nil? &&
217
222
  deprecated_for_removal_transport_configuration_proc.nil? &&
218
- File.exist?(Datadog::Transport::Ext::UnixSocket::DEFAULT_PATH)
223
+ File.exist?(Datadog::Core::Configuration::Ext::Agent::UnixSocket::DEFAULT_PATH)
219
224
 
220
- Datadog::Transport::Ext::UnixSocket::DEFAULT_PATH
225
+ Datadog::Core::Configuration::Ext::Agent::UnixSocket::DEFAULT_PATH
221
226
  end
222
227
  end
223
228
 
@@ -235,7 +240,7 @@ module Datadog
235
240
  def parsed_url
236
241
  return @parsed_url if defined?(@parsed_url)
237
242
 
238
- unparsed_url_from_env = ENV[Datadog::Tracing::Configuration::Ext::Transport::ENV_DEFAULT_URL]
243
+ unparsed_url_from_env = ENV[Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL]
239
244
 
240
245
  @parsed_url =
241
246
  if unparsed_url_from_env
@@ -246,9 +251,9 @@ module Datadog
246
251
  else
247
252
  # rubocop:disable Layout/LineLength
248
253
  log_warning(
249
- "Invalid URI scheme '#{parsed.scheme}' for #{Datadog::Tracing::Configuration::Ext::Transport::ENV_DEFAULT_URL} " \
254
+ "Invalid URI scheme '#{parsed.scheme}' for #{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} " \
250
255
  "environment variable ('#{unparsed_url_from_env}'). " \
251
- "Ignoring the contents of #{Datadog::Tracing::Configuration::Ext::Transport::ENV_DEFAULT_URL}."
256
+ "Ignoring the contents of #{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL}."
252
257
  )
253
258
  # rubocop:enable Layout/LineLength
254
259
 
@@ -328,7 +333,7 @@ module Datadog
328
333
  def transport_options
329
334
  return @transport_options if defined?(@transport_options)
330
335
 
331
- transport_options_proc = settings.tracing.transport_options
336
+ transport_options_proc = transport_options_settings
332
337
 
333
338
  @transport_options = TransportOptions.new
334
339
 
@@ -380,14 +385,14 @@ module Datadog
380
385
 
381
386
  def adapter(kind_or_custom_adapter, *args, **kwargs)
382
387
  case kind_or_custom_adapter
383
- when Datadog::Transport::Ext::HTTP::ADAPTER
384
- @transport_options.adapter = Datadog::Transport::Ext::HTTP::ADAPTER
388
+ when Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
389
+ @transport_options.adapter = Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
385
390
  @transport_options.hostname = args[0] || kwargs[:hostname]
386
391
  @transport_options.port = args[1] || kwargs[:port]
387
392
  @transport_options.timeout_seconds = kwargs[:timeout]
388
393
  @transport_options.ssl = kwargs[:ssl]
389
- when Datadog::Transport::Ext::UnixSocket::ADAPTER
390
- @transport_options.adapter = Datadog::Transport::Ext::UnixSocket::ADAPTER
394
+ when Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER
395
+ @transport_options.adapter = Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER
391
396
  @transport_options.uds_path = args[0] || kwargs[:uds_path]
392
397
  end
393
398
 
@@ -1,4 +1,3 @@
1
- require_relative '../environment/variable_helpers'
2
1
  require_relative 'options'
3
2
 
4
3
  module Datadog
@@ -8,8 +7,6 @@ module Datadog
8
7
  # @public_api
9
8
  module Base
10
9
  def self.included(base)
11
- base.extend(Core::Environment::VariableHelpers)
12
- base.include(Core::Environment::VariableHelpers)
13
10
  base.include(Options)
14
11
 
15
12
  base.extend(ClassMethods)
@@ -57,14 +54,7 @@ module Datadog
57
54
  end
58
55
 
59
56
  def configure(opts = {})
60
- # Sort the options in preference of dependency order first
61
- ordering = self.class.options.dependency_order
62
- sorted_opts = opts.sort_by do |name, _value|
63
- ordering.index(name) || (ordering.length + 1)
64
- end.to_h
65
-
66
- # Apply options in sort order
67
- sorted_opts.each do |name, value|
57
+ opts.each do |name, value|
68
58
  if respond_to?("#{name}=")
69
59
  send("#{name}=", value)
70
60
  elsif option_defined?(name)
@@ -1,4 +1,5 @@
1
1
  require_relative 'agent_settings_resolver'
2
+ require_relative 'ext'
2
3
  require_relative '../diagnostics/environment_logger'
3
4
  require_relative '../diagnostics/health'
4
5
  require_relative '../logger'
@@ -54,7 +55,7 @@ module Datadog
54
55
 
55
56
  def build_telemetry(settings, agent_settings, logger)
56
57
  enabled = settings.telemetry.enabled
57
- if agent_settings.adapter != Datadog::Transport::Ext::HTTP::ADAPTER
58
+ if agent_settings.adapter != Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
58
59
  enabled = false
59
60
  logger.debug { "Telemetry disabled. Agent network adapter not supported: #{agent_settings.adapter}" }
60
61
  end
@@ -81,10 +82,14 @@ module Datadog
81
82
  def initialize(settings)
82
83
  @logger = self.class.build_logger(settings)
83
84
 
85
+ # This agent_settings is intended for use within Core. If you require
86
+ # agent_settings within a product outside of core you should extend
87
+ # the Core resolver from within your product/component's namespace.
84
88
  agent_settings = AgentSettingsResolver.call(settings, logger: @logger)
85
89
 
86
90
  @remote = Remote::Component.build(settings, agent_settings)
87
- @tracer = self.class.build_tracer(settings, agent_settings)
91
+ @tracer = self.class.build_tracer(settings, logger: @logger)
92
+
88
93
  @profiler = Datadog::Profiling::Component.build_profiler_component(
89
94
  settings: settings,
90
95
  agent_settings: agent_settings,
@@ -17,9 +17,30 @@ module Datadog
17
17
  ENV_DEFAULT_PORT = 'DD_METRIC_AGENT_PORT'
18
18
  end
19
19
 
20
+ # DEV-2.0: This module only exists for backwards compatibility with the public API.
21
+ # It should be consolidated into the Agent module below.
20
22
  module Transport
21
23
  ENV_DEFAULT_HOST = 'DD_AGENT_HOST'
22
24
  end
25
+
26
+ module Agent
27
+ # env var has "trace" in it, but it really applies to all products
28
+ ENV_DEFAULT_PORT = 'DD_TRACE_AGENT_PORT'
29
+ ENV_DEFAULT_URL = 'DD_TRACE_AGENT_URL'
30
+
31
+ module HTTP
32
+ ADAPTER = :net_http # DEV: Rename to simply `:http`, as Net::HTTP is an implementation detail.
33
+ DEFAULT_HOST = '127.0.0.1'
34
+ DEFAULT_PORT = 8126
35
+ end
36
+
37
+ # @public_api
38
+ module UnixSocket
39
+ ADAPTER = :unix
40
+ DEFAULT_PATH = '/var/run/datadog/apm.socket'
41
+ DEFAULT_TIMEOUT_SECONDS = 1
42
+ end
43
+ end
23
44
  end
24
45
  end
25
46
  end
@@ -113,8 +113,6 @@ module Datadog
113
113
  def get
114
114
  if @is_set
115
115
  @value
116
- elsif definition.delegate_to
117
- context_eval(&definition.delegate_to)
118
116
  else
119
117
  set_value_from_env_or_default
120
118
  end
@@ -141,7 +139,7 @@ module Datadog
141
139
  if definition.default.instance_of?(Proc)
142
140
  context_eval(&definition.default)
143
141
  else
144
- definition.experimental_default_proc || Core::Utils::SafeDup.frozen_or_dup(definition.default)
142
+ definition.default_proc || Core::Utils::SafeDup.frozen_or_dup(definition.default)
145
143
  end
146
144
  end
147
145
 
@@ -266,7 +264,7 @@ module Datadog
266
264
  # when restoring a value from `@value_per_precedence`, and we are only running `definition.setter`
267
265
  # on the original value, not on a valud that has already been processed by `definition.setter`.
268
266
  @value_per_precedence[precedence] = value
269
- context_exec(v, old_value, &definition.on_set) if definition.on_set
267
+ context_exec(v, old_value, &definition.after_set) if definition.after_set
270
268
  end
271
269
  end
272
270