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, waf_runner) ⇒ Context

Returns a new instance of Context.



34
35
36
37
38
39
40
41
# File 'lib/datadog/appsec/context.rb', line 34

def initialize(trace, span, waf_runner)
  @trace = trace
  @span = span
  @events = []
  @waf_runner = waf_runner
  @metrics = Metrics::Collector.new
  @interrupted = false
end

Instance Attribute Details

#eventsObject (readonly)

TODO: add delegators for active trace span



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

def events
  @events
end

#spanObject (readonly)

TODO: add delegators for active trace span



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

def span
  @span
end

#traceObject (readonly)

TODO: add delegators for active trace span



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

def trace
  @trace
end

Class Method Details

.activate(context) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/datadog/appsec/context.rb', line 16

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



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

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

.deactivateObject



23
24
25
26
27
# File 'lib/datadog/appsec/context.rb', line 23

def deactivate
  active&.finalize!
ensure
  Thread.current[Ext::ACTIVE_CONTEXT_KEY] = nil
end

Instance Method Details

#export_metricsObject



79
80
81
82
83
84
# File 'lib/datadog/appsec/context.rb', line 79

def export_metrics
  return if @span.nil?

  Metrics::Exporter.export_waf_metrics(@metrics.waf, @span)
  Metrics::Exporter.export_rasp_metrics(@metrics.rasp, @span)
end

#export_request_telemetryObject



86
87
88
89
90
# File 'lib/datadog/appsec/context.rb', line 86

def export_request_telemetry
  return if @trace.nil?

  Metrics::TelemetryExporter.export_waf_request_metrics(@metrics.waf, self)
end

#extract_schemaObject



75
76
77
# File 'lib/datadog/appsec/context.rb', line 75

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

#finalize!Object



92
93
94
# File 'lib/datadog/appsec/context.rb', line 92

def finalize!
  @waf_runner.finalize!
end

#interrupted?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/datadog/appsec/context.rb', line 63

def interrupted?
  @interrupted
end

#mark_as_interrupted!Object



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

def mark_as_interrupted!
  @interrupted = true
end

#run_rasp(type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/datadog/appsec/context.rb', line 50

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



43
44
45
46
47
48
# File 'lib/datadog/appsec/context.rb', line 43

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

#waf_runner_known_addressesObject



71
72
73
# File 'lib/datadog/appsec/context.rb', line 71

def waf_runner_known_addresses
  @waf_runner.waf_addresses
end

#waf_runner_ruleset_versionObject



67
68
69
# File 'lib/datadog/appsec/context.rb', line 67

def waf_runner_ruleset_version
  @waf_runner.ruleset_version
end