Skip to content

Commit b65398d

Browse files
committed
Extract AppSec processor context in separate file
1 parent 06e050b commit b65398d

File tree

5 files changed

+374
-368
lines changed

5 files changed

+374
-368
lines changed

lib/datadog/appsec/processor.rb

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,6 @@ module Datadog
44
module AppSec
55
# Processor integrates libddwaf into datadog/appsec
66
class Processor
7-
# Context manages a sequence of runs
8-
class Context
9-
attr_reader :time_ns, :time_ext_ns, :timeouts, :events
10-
11-
def initialize(processor)
12-
@context = Datadog::AppSec::WAF::Context.new(processor.send(:handle))
13-
@time_ns = 0.0
14-
@time_ext_ns = 0.0
15-
@timeouts = 0
16-
@events = []
17-
@run_mutex = Mutex.new
18-
end
19-
20-
def run(input, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
21-
@run_mutex.lock
22-
23-
start_ns = Core::Utils::Time.get_time(:nanosecond)
24-
25-
input.reject! do |_, v|
26-
case v
27-
when TrueClass, FalseClass
28-
false
29-
else
30-
v.nil? ? true : v.empty?
31-
end
32-
end
33-
34-
_code, res = @context.run(input, timeout)
35-
36-
stop_ns = Core::Utils::Time.get_time(:nanosecond)
37-
38-
# these updates are not thread safe and should be protected
39-
@time_ns += res.total_runtime
40-
@time_ext_ns += (stop_ns - start_ns)
41-
@timeouts += 1 if res.timeout
42-
43-
res
44-
ensure
45-
@run_mutex.unlock
46-
end
47-
48-
def extract_schema
49-
return unless extract_schema?
50-
51-
input = {
52-
'waf.context.processor' => {
53-
'extract-schema' => true
54-
}
55-
}
56-
57-
_code, res = @context.run(input, WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
58-
59-
res
60-
end
61-
62-
def finalize
63-
@context.finalize
64-
end
65-
66-
private
67-
68-
def extract_schema?
69-
Datadog.configuration.appsec.api_security.enabled &&
70-
Datadog.configuration.appsec.api_security.sample_rate.sample?
71-
end
72-
end
73-
747
attr_reader :diagnostics, :addresses
758

769
def initialize(ruleset:, telemetry:)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
module Datadog
4+
module AppSec
5+
class Processor
6+
# Context manages a sequence of runs
7+
class Context
8+
attr_reader :time_ns, :time_ext_ns, :timeouts, :events
9+
10+
def initialize(processor)
11+
@context = Datadog::AppSec::WAF::Context.new(processor.send(:handle))
12+
@time_ns = 0.0
13+
@time_ext_ns = 0.0
14+
@timeouts = 0
15+
@events = []
16+
@run_mutex = Mutex.new
17+
end
18+
19+
def run(input, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
20+
@run_mutex.lock
21+
22+
start_ns = Core::Utils::Time.get_time(:nanosecond)
23+
24+
input.reject! do |_, v|
25+
case v
26+
when TrueClass, FalseClass
27+
false
28+
else
29+
v.nil? ? true : v.empty?
30+
end
31+
end
32+
33+
_code, res = @context.run(input, timeout)
34+
35+
stop_ns = Core::Utils::Time.get_time(:nanosecond)
36+
37+
# these updates are not thread safe and should be protected
38+
@time_ns += res.total_runtime
39+
@time_ext_ns += (stop_ns - start_ns)
40+
@timeouts += 1 if res.timeout
41+
42+
res
43+
ensure
44+
@run_mutex.unlock
45+
end
46+
47+
def extract_schema
48+
return unless extract_schema?
49+
50+
input = {
51+
'waf.context.processor' => {
52+
'extract-schema' => true
53+
}
54+
}
55+
56+
_code, res = @context.run(input, WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
57+
58+
res
59+
end
60+
61+
def finalize
62+
@context.finalize
63+
end
64+
65+
private
66+
67+
def extract_schema?
68+
Datadog.configuration.appsec.api_security.enabled &&
69+
Datadog.configuration.appsec.api_security.sample_rate.sample?
70+
end
71+
end
72+
end
73+
end
74+
end

lib/datadog/appsec/scope.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative 'processor'
3+
require_relative 'processor/context'
44

55
module Datadog
66
module AppSec

0 commit comments

Comments
 (0)