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

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

Overview

Context manages a sequence of runs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor) ⇒ Context

Returns a new instance of Context.



11
12
13
14
15
16
17
18
# File 'lib/datadog/appsec/processor.rb', line 11

def initialize(processor)
  @context = Datadog::AppSec::WAF::Context.new(processor.send(:handle))
  @time_ns = 0.0
  @time_ext_ns = 0.0
  @timeouts = 0
  @events = []
  @run_mutex = Mutex.new
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#time_ext_nsObject (readonly)

Returns the value of attribute time_ext_ns.



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

def time_ext_ns
  @time_ext_ns
end

#time_nsObject (readonly)

Returns the value of attribute time_ns.



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

def time_ns
  @time_ns
end

#timeoutsObject (readonly)

Returns the value of attribute timeouts.



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

def timeouts
  @timeouts
end

Instance Method Details

#extract_schemaObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/appsec/processor.rb', line 48

def extract_schema
  return unless extract_schema?

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

  _code, res = @context.run(input, WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)

  res
end

#finalizeObject



62
63
64
# File 'lib/datadog/appsec/processor.rb', line 62

def finalize
  @context.finalize
end

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datadog/appsec/processor.rb', line 20

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

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

  input.reject! do |_, v|
    case v
    when TrueClass, FalseClass
      false
    else
      v.nil? ? true : v.empty?
    end
  end

  _code, res = @context.run(input, timeout)

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

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

  res
ensure
  @run_mutex.unlock
end