Class: Datadog::AppSec::Processor::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/appsec/processor/context.rb

Overview

Context manages a sequence of runs

Constant Summary collapse

LIBDDWAF_SUCCESSFUL_EXECUTION_CODES =
[:ok, :match].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle, telemetry:) ⇒ Context

Returns a new instance of Context.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/datadog/appsec/processor/context.rb', line 12

def initialize(handle, telemetry:)
  @context = WAF::Context.new(handle)
  @telemetry = telemetry

  @time_ns = 0.0
  @time_ext_ns = 0.0
  @timeouts = 0
  @events = []
  @run_mutex = Mutex.new

  @libddwaf_debug_tag = "libddwaf:#{WAF::VERSION::STRING}"
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#time_ext_nsObject (readonly)

Returns the value of attribute time_ext_ns.



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

def time_ext_ns
  @time_ext_ns
end

#time_nsObject (readonly)

Returns the value of attribute time_ns.



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

def time_ns
  @time_ns
end

#timeoutsObject (readonly)

Returns the value of attribute timeouts.



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

def timeouts
  @timeouts
end

Instance Method Details

#extract_schemaObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/datadog/appsec/processor/context.rb', line 57

def extract_schema
  return unless extract_schema?

  input = {
    'waf.context.processor' => {
      'extract-schema' => true
    }
  }

  _code, result = try_run(input, {}, WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)

  report_execution(result)
  result
end

#finalizeObject



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

def finalize
  @context.finalize
end

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datadog/appsec/processor/context.rb', line 25

def run(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
  @run_mutex.lock

  start_ns = Core::Utils::Time.get_time(:nanosecond)

  persistent_data.reject! do |_, v|
    next false if v.is_a?(TrueClass) || v.is_a?(FalseClass)

    v.nil? ? true : v.empty?
  end

  ephemeral_data.reject! do |_, v|
    next false if v.is_a?(TrueClass) || v.is_a?(FalseClass)

    v.nil? ? true : v.empty?
  end

  _code, result = try_run(persistent_data, ephemeral_data, timeout)

  stop_ns = Core::Utils::Time.get_time(:nanosecond)

  # these updates are not thread safe and should be protected
  @time_ns += result.total_runtime
  @time_ext_ns += (stop_ns - start_ns)
  @timeouts += 1 if result.timeout

  report_execution(result)
  result
ensure
  @run_mutex.unlock
end