Skip to content

Commit e3f599d

Browse files
committed
Move AppSec context creation into processor
Since Context is a part of the Processor it can't call handle on the injected dependency. Instead, Processor is going to create a fully operational context and provide all requirements to it.
1 parent 321f513 commit e3f599d

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

lib/datadog/appsec/processor.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# frozen_string_literal: true
22

3+
require_relative 'processor/context'
4+
35
module Datadog
46
module AppSec
57
# Processor integrates libddwaf into datadog/appsec
68
class Processor
79
attr_reader :diagnostics, :addresses
810

911
def initialize(ruleset:, telemetry:)
12+
@telemetry = telemetry
1013
@diagnostics = nil
1114
@addresses = []
15+
1216
settings = Datadog.configuration.appsec
13-
@telemetry = telemetry
1417

1518
# TODO: Refactor to make it easier to test
1619
unless require_libddwaf && libddwaf_provides_waf? && create_waf_handle(settings, ruleset)
@@ -26,9 +29,9 @@ def finalize
2629
@handle.finalize
2730
end
2831

29-
protected
30-
31-
attr_reader :handle
32+
def new_context
33+
Context.new(@handle, telemetry: @telemetry)
34+
end
3235

3336
private
3437

lib/datadog/appsec/processor/context.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ class Processor
77
class Context
88
attr_reader :time_ns, :time_ext_ns, :timeouts, :events
99

10-
def initialize(processor)
11-
@context = Datadog::AppSec::WAF::Context.new(processor.send(:handle))
10+
def initialize(handle, telemetry:)
11+
@context = Datadog::AppSec::WAF::Context.new(handle)
12+
@telemetry = telemetry
13+
1214
@time_ns = 0.0
1315
@time_ext_ns = 0.0
1416
@timeouts = 0
@@ -39,6 +41,7 @@ def run(input, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
3941
@time_ext_ns += (stop_ns - start_ns)
4042
@timeouts += 1 if res.timeout
4143

44+
# TODO: handle the response
4245
res
4346
ensure
4447
@run_mutex.unlock

lib/datadog/appsec/scope.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require_relative 'processor/context'
4-
53
module Datadog
64
module AppSec
75
# Capture context essential to consistently call processor and report via traces
@@ -22,8 +20,7 @@ class << self
2220
def activate_scope(trace, service_entry_span, processor)
2321
raise ActiveScopeError, 'another scope is active, nested scopes are not supported' if active_scope
2422

25-
context = Datadog::AppSec::Processor::Context.new(processor)
26-
23+
context = processor.new_context
2724
self.active_scope = new(trace, service_entry_span, context)
2825
end
2926

spec/datadog/appsec/processor/context_spec.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
let(:input_client_ip) { { 'http.client_ip' => '1.2.3.4' } }
1919

2020
let(:client_ip) { '1.2.3.4' }
21-
2221
let(:input) { input_scanner }
23-
2422
let(:processor) { Datadog::AppSec::Processor.new(ruleset: ruleset, telemetry: telemetry) }
2523

2624
let(:run_count) { 1 }
@@ -36,12 +34,9 @@
3634
results.first
3735
end
3836

39-
subject(:context) { described_class.new(processor) }
40-
41-
before do
42-
runs
43-
end
37+
subject(:context) { processor.new_context }
4438

39+
before { runs }
4540
after do
4641
context.finalize
4742
processor.finalize

spec/datadog/appsec/processor_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,10 @@ def diagnostics
283283
end
284284
end
285285
end
286+
287+
describe '#new_context' do
288+
let(:processor) { described_class.new(ruleset: ruleset, telemetry: telemetry) }
289+
290+
it { expect(processor.new_context).to be_instance_of(described_class::Context) }
291+
end
286292
end

0 commit comments

Comments
 (0)