Skip to content

Commit 91d883f

Browse files
authored
Merge pull request #4023 from DataDog/appsec-55378-extract-waf-context
[APPSEC-55378] Extract AppSec processor context into separate file * Update AppSec processor and context RBS files
2 parents 06e050b + a5e63c2 commit 91d883f

File tree

7 files changed

+405
-394
lines changed

7 files changed

+405
-394
lines changed

Diff for: lib/datadog/appsec/processor.rb

-67
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:)

Diff for: lib/datadog/appsec/processor/context.rb

+74
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

Diff for: lib/datadog/appsec/scope.rb

+1-1
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

Diff for: sig/datadog/appsec/processor.rbs

+4-26
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
module Datadog
22
module AppSec
33
class Processor
4-
class Context
5-
type event = untyped
6-
type data = WAF::data
7-
8-
attr_reader time_ns: ::Float
9-
attr_reader time_ext_ns: ::Float
10-
attr_reader timeouts: ::Integer
11-
attr_reader events: ::Array[event]
12-
13-
@context: WAF::Context
14-
15-
@run_mutex: ::Thread::Mutex
16-
17-
def initialize: (Processor processor) -> void
18-
def run: (Hash[untyped, untyped] input, ?::Integer timeout) -> WAF::Result
19-
def extract_schema: () -> WAF::Result?
20-
def finalize: () -> void
21-
22-
private
23-
def extract_schema?: () -> bool
24-
end
25-
264
def self.active_context: () -> Context
275

286
private
297

30-
attr_reader diagnostics: untyped
31-
attr_reader addresses: untyped
8+
attr_reader diagnostics: WAF::LibDDWAF::Object?
9+
attr_reader addresses: ::Array[::String]
3210

3311
@handle: WAF::Handle
3412
@ruleset: ::Hash[::String, untyped]
3513
@addresses: ::Array[::String]
3614

37-
def initialize: (ruleset: ::Hash[untyped, untyped], telemetry: Datadog::Core::Telemetry::Component) -> void
15+
def initialize: (ruleset: ::Hash[untyped, untyped], telemetry: Core::Telemetry::Component) -> void
3816
def ready?: () -> bool
3917
def finalize: () -> void
4018

@@ -44,7 +22,7 @@ module Datadog
4422

4523
def require_libddwaf: () -> bool
4624
def libddwaf_provides_waf?: () -> bool
47-
def create_waf_handle: (Datadog::Core::Configuration::Settings::_AppSec settings, ::Hash[String, untyped] ruleset) -> bool
25+
def create_waf_handle: (Core::Configuration::Settings::_AppSec settings, ::Hash[String, untyped] ruleset) -> bool
4826
def libddwaf_platform: () -> ::String
4927
def ruby_platforms: () -> ::Array[::String]
5028
end

Diff for: sig/datadog/appsec/processor/context.rbs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Datadog
2+
module AppSec
3+
class Processor
4+
class Context
5+
type event = untyped
6+
type data = WAF::data
7+
8+
attr_reader time_ns: ::Float
9+
attr_reader time_ext_ns: ::Float
10+
attr_reader timeouts: ::Integer
11+
attr_reader events: ::Array[event]
12+
13+
@context: WAF::Context
14+
15+
@run_mutex: ::Thread::Mutex
16+
17+
def initialize: (Processor processor) -> void
18+
def run: (Hash[untyped, untyped] input, ?::Integer timeout) -> WAF::Result
19+
def extract_schema: () -> WAF::Result?
20+
def finalize: () -> void
21+
22+
private
23+
def extract_schema?: () -> bool
24+
end
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)