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.



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

def initialize(trace, span, security_engine)
  @trace = trace
  @span = span
  @security_engine = security_engine
  @waf_runner = security_engine.new_context
end

Instance Attribute Details

#spanObject (readonly)

Returns the value of attribute span.



10
11
12
# File 'lib/datadog/appsec/context.rb', line 10

def span
  @span
end

#traceObject (readonly)

Returns the value of attribute trace.



10
11
12
# File 'lib/datadog/appsec/context.rb', line 10

def trace
  @trace
end

#waf_runnerObject (readonly)

NOTE: This is an intermediate state and will be changed



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

def waf_runner
  @waf_runner
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

#finalizeObject



49
50
51
# File 'lib/datadog/appsec/context.rb', line 49

def finalize
  @waf_runner.finalize
end

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



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

def run_rasp(_type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
  @waf_runner.run(persistent_data, ephemeral_data, timeout)
end

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



41
42
43
# File 'lib/datadog/appsec/context.rb', line 41

def run_waf(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
  @waf_runner.run(persistent_data, ephemeral_data, timeout)
end