Class: Datadog::AppSec::Context

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#eventsObject (readonly)

Returns the value of attribute events.



12
13
14
# File 'lib/datadog/appsec/context.rb', line 12

def events
  @events
end

#spanObject (readonly)

Returns the value of attribute span.



12
13
14
# File 'lib/datadog/appsec/context.rb', line 12

def span
  @span
end

#traceObject (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

Raises:

  • (ArgumentError)


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

.activeObject



28
29
30
# File 'lib/datadog/appsec/context.rb', line 28

def active
  Thread.current[Ext::ACTIVE_CONTEXT_KEY]
end

.deactivateObject



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_metricsObject



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_schemaObject



58
59
60
# File 'lib/datadog/appsec/context.rb', line 58

def extract_schema
  @waf_runner.run({ 'waf.context.processor' => { 'extract-schema' => true } }, {})
end

#finalizeObject



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