Class: Datadog::AppSec::Context Private
- Inherits:
-
Object
- Object
- Datadog::AppSec::Context
- Defined in:
- lib/datadog/appsec/context.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Request-bound context providing threat detection interface.
Activated at the start of a request (see Contrib::Rack::RequestMiddleware)
and shared across all instrumentations within that request's lifecycle.
Accumulates security events, metrics, and state needed for coordinated threat detection.
Constant Summary collapse
- ActiveContextError =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Class.new(StandardError)
Instance Attribute Summary collapse
-
#downstream_body_sampler ⇒ Object
readonly
private
Sampler for downstream HTTP request/response body analysis.
-
#metrics ⇒ Object
readonly
private
TODO: add delegators for active trace span.
-
#span ⇒ Object
readonly
private
TODO: add delegators for active trace span.
-
#state ⇒ Object
readonly
private
Shared mutable storage for counters, flags, and data accumulated during the request's lifecycle.
-
#trace ⇒ Object
readonly
private
TODO: add delegators for active trace span.
Class Method Summary collapse
- .activate(context) ⇒ Object private
- .active ⇒ Object private
- .deactivate ⇒ Object private
Instance Method Summary collapse
- #attach ⇒ Object private
- #catch_interrupt ⇒ Object private
- #detach ⇒ Object private
- #events ⇒ Object private
- #export_metrics ⇒ Object private
- #export_request_telemetry ⇒ Object private
- #extract_schema! ⇒ Object private
- #finalized? ⇒ Boolean private
-
#initialize(trace, span, waf_runner) ⇒ Context
constructor
private
A new instance of Context.
- #interrupt!(action_params) ⇒ Object private
- #interrupted? ⇒ Boolean private
- #mark_as_interrupted! ⇒ Object private
- #run_rasp(type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT, phase: nil) ⇒ Object private
- #run_waf(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) ⇒ Object private
- #shared? ⇒ Boolean private
- #waf_runner_known_addresses ⇒ Object private
- #waf_runner_ruleset_version ⇒ Object private
Constructor Details
#initialize(trace, span, waf_runner) ⇒ Context
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Context.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/datadog/appsec/context.rb', line 58 def initialize(trace, span, waf_runner) @trace = trace @span = span @waf_runner = waf_runner @mutex = Mutex.new @attached_count = 0 @finalized = false @metrics = Metrics::Collector.new @downstream_body_sampler = CounterSampler.new( Datadog.configuration.appsec.api_security.downstream_body_analysis.sample_rate, ) @state = { events: [], interrupted: false, downstream_body_analyzed_count: 0, } end |
Instance Attribute Details
#downstream_body_sampler ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sampler for downstream HTTP request/response body analysis.
33 34 35 |
# File 'lib/datadog/appsec/context.rb', line 33 def downstream_body_sampler @downstream_body_sampler end |
#metrics ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
TODO: add delegators for active trace span
23 24 25 |
# File 'lib/datadog/appsec/context.rb', line 23 def metrics @metrics end |
#span ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
TODO: add delegators for active trace span
23 24 25 |
# File 'lib/datadog/appsec/context.rb', line 23 def span @span end |
#state ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Shared mutable storage for counters, flags, and data accumulated during the request's lifecycle.
NOTE: This attribute is a subject to change, but in a current form
it's a Hash-like structure.
30 31 32 |
# File 'lib/datadog/appsec/context.rb', line 30 def state @state end |
#trace ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
TODO: add delegators for active trace span
23 24 25 |
# File 'lib/datadog/appsec/context.rb', line 23 def trace @trace end |
Class Method Details
.activate(context) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
36 37 38 39 40 41 42 43 44 |
# File 'lib/datadog/appsec/context.rb', line 36 def activate(context) raise ArgumentError, "not a Datadog::AppSec::Context" unless context.instance_of?(Context) raise ActiveContextError, "another context is active, nested contexts are not supported" if active # NOTE: Activation makes the caller a member, so a caller that only # activates and deactivates never has to call `attach` context.attach Thread.current[Ext::ACTIVE_CONTEXT_KEY] = context end |
.active ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 53 54 55 |
# File 'lib/datadog/appsec/context.rb', line 52 def active context = Thread.current[Ext::ACTIVE_CONTEXT_KEY] context if context && !context.finalized? end |
.deactivate ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
46 47 48 49 50 |
# File 'lib/datadog/appsec/context.rb', line 46 def deactivate active&.detach ensure Thread.current[Ext::ACTIVE_CONTEXT_KEY] = nil end |
Instance Method Details
#attach ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
158 159 160 161 162 163 164 165 166 |
# File 'lib/datadog/appsec/context.rb', line 158 def attach @mutex.synchronize do return false if @finalized @attached_count += 1 end true end |
#catch_interrupt ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 99 100 101 102 103 |
# File 'lib/datadog/appsec/context.rb', line 96 def catch_interrupt Kernel.catch(Ext::INTERRUPT) do yield return false end true end |
#detach ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/datadog/appsec/context.rb', line 168 def detach @mutex.synchronize do return if @attached_count.zero? @attached_count -= 1 return unless @attached_count.zero? @finalized = true end @waf_runner.finalize! rescue => e AppSec.telemetry.report(e, description: "AppSec: Failed to finalize context") end |
#events ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
92 93 94 |
# File 'lib/datadog/appsec/context.rb', line 92 def events @state[:events] end |
#export_metrics ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
143 144 145 146 147 148 149 |
# File 'lib/datadog/appsec/context.rb', line 143 def export_metrics return if @span.nil? Metrics::Exporter.export_waf_metrics(@metrics.waf, @span) Metrics::Exporter.export_rasp_metrics(@metrics.rasp, @span) Metrics::Exporter.export_downstream_response_metrics(@metrics.downstream_responses, @span) end |
#export_request_telemetry ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
151 152 153 154 155 156 |
# File 'lib/datadog/appsec/context.rb', line 151 def export_request_telemetry return if @trace.nil? Metrics::TelemetryExporter.export_waf_request_metrics(@metrics.waf, self) Metrics::TelemetryExporter.export_api_security_metrics(self) end |
#extract_schema! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
133 134 135 136 137 138 139 140 141 |
# File 'lib/datadog/appsec/context.rb', line 133 def extract_schema! persistent_data = {"waf.context.processor" => {"extract-schema" => true}} waf_result = run_waf(persistent_data, {}, Datadog.configuration.appsec.waf_timeout) security_event = AppSec::SecurityEvent.new(waf_result, trace: trace, span: span) @state[:schema_extracted] = security_event.schema? events.push(security_event) end |
#finalized? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
183 184 185 |
# File 'lib/datadog/appsec/context.rb', line 183 def finalized? @mutex.synchronize { @finalized } end |
#interrupt!(action_params) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/datadog/appsec/context.rb', line 105 def interrupt!(action_params) @mutex.synchronize do @state[:interrupted] = true @state[:interrupt_params] = action_params unless @state.key?(:interrupt_params) end throw(Ext::INTERRUPT, action_params) rescue UncaughtThrowError # NOTE: Without a catch up the stack, the throw raises into the host app Datadog.logger.warn("AppSec: request was interrupted outside of an interruption point, continuing execution") end |
#interrupted? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
117 118 119 |
# File 'lib/datadog/appsec/context.rb', line 117 def interrupted? @mutex.synchronize { @state[:interrupted] } end |
#mark_as_interrupted! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
121 122 123 |
# File 'lib/datadog/appsec/context.rb', line 121 def mark_as_interrupted! @mutex.synchronize { @state[:interrupted] = true } end |
#run_rasp(type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT, phase: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
83 84 85 86 87 88 89 90 |
# File 'lib/datadog/appsec/context.rb', line 83 def run_rasp(type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT, phase: nil) result = @waf_runner.run(persistent_data, ephemeral_data, timeout) Metrics::Telemetry.report_rasp(type, result, phase: phase) @metrics.record_rasp(result, type: type, phase: phase) result end |
#run_waf(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
76 77 78 79 80 81 |
# File 'lib/datadog/appsec/context.rb', line 76 def run_waf(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) result = @waf_runner.run(persistent_data, ephemeral_data, timeout) @metrics.record_waf(result) result end |
#shared? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
187 188 189 |
# File 'lib/datadog/appsec/context.rb', line 187 def shared? @mutex.synchronize { @attached_count > 1 } end |
#waf_runner_known_addresses ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
129 130 131 |
# File 'lib/datadog/appsec/context.rb', line 129 def waf_runner_known_addresses @waf_runner.waf_addresses end |
#waf_runner_ruleset_version ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
125 126 127 |
# File 'lib/datadog/appsec/context.rb', line 125 def waf_runner_ruleset_version @waf_runner.ruleset_version end |