Class: Datadog::AppSec::Context
- Inherits:
-
Object
- Object
- Datadog::AppSec::Context
- Defined in:
- lib/datadog/appsec/context.rb
Overview
This class accumulates the context over the request life-cycle and exposes interface sufficient for instrumentation to perform threat detection.
Constant Summary collapse
- ActiveContextError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#span ⇒ Object
readonly
Returns the value of attribute span.
-
#trace ⇒ Object
readonly
Returns the value of attribute trace.
Class Method Summary collapse
Instance Method Summary collapse
- #export_metrics ⇒ Object
- #extract_schema ⇒ Object
- #finalize ⇒ Object
-
#initialize(trace, span, security_engine) ⇒ Context
constructor
A new instance of Context.
- #run_rasp(type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) ⇒ Object
- #run_waf(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) ⇒ Object
Constructor Details
#initialize(trace, span, security_engine) ⇒ Context
Returns a new instance of Context.
33 34 35 36 37 38 39 40 |
# File 'lib/datadog/appsec/context.rb', line 33 def initialize(trace, span, security_engine) @trace = trace @span = span @events = [] @security_engine = security_engine @waf_runner = security_engine.new_runner @metrics = Metrics::Collector.new end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
12 13 14 |
# File 'lib/datadog/appsec/context.rb', line 12 def events @events end |
#span ⇒ Object (readonly)
Returns the value of attribute span.
12 13 14 |
# File 'lib/datadog/appsec/context.rb', line 12 def span @span end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
12 13 14 |
# File 'lib/datadog/appsec/context.rb', line 12 def trace @trace end |
Class Method Details
.activate(context) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/datadog/appsec/context.rb', line 15 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 Thread.current[Ext::ACTIVE_CONTEXT_KEY] = context end |
.active ⇒ Object
28 29 30 |
# File 'lib/datadog/appsec/context.rb', line 28 def active Thread.current[Ext::ACTIVE_CONTEXT_KEY] end |
.deactivate ⇒ Object
22 23 24 25 26 |
# File 'lib/datadog/appsec/context.rb', line 22 def deactivate active&.finalize ensure Thread.current[Ext::ACTIVE_CONTEXT_KEY] = nil end |
Instance Method Details
#export_metrics ⇒ Object
62 63 64 65 66 67 |
# File 'lib/datadog/appsec/context.rb', line 62 def export_metrics return if @span.nil? Metrics::Exporter.export_waf_metrics(@metrics.waf, @span) Metrics::Exporter.export_rasp_metrics(@metrics.rasp, @span) end |
#extract_schema ⇒ Object
58 59 60 |
# File 'lib/datadog/appsec/context.rb', line 58 def extract_schema @waf_runner.run({ 'waf.context.processor' => { 'extract-schema' => true } }, {}) end |
#finalize ⇒ Object
69 70 71 |
# File 'lib/datadog/appsec/context.rb', line 69 def finalize @waf_runner.finalize end |
#run_rasp(type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/datadog/appsec/context.rb', line 49 def run_rasp(type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) result = @waf_runner.run(persistent_data, ephemeral_data, timeout) Metrics::Telemetry.report_rasp(type, result) @metrics.record_rasp(result) result end |
#run_waf(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/datadog/appsec/context.rb', line 42 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 |