Skip to content

Commit

Permalink
When Apssec reconfigure make sure to check if the old processor is pr…
Browse files Browse the repository at this point in the history
…esent
  • Loading branch information
GustavoCaso committed Apr 27, 2023
1 parent ec20faf commit 3c84e41
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/appsec/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def reconfigure(ruleset:)
if new && new.ready?
old = @processor
@processor = new
old.finalize
old.finalize if old
end
end
end
Expand Down
99 changes: 58 additions & 41 deletions spec/datadog/appsec/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,49 @@
end

describe '#reconfigure' do
let(:ruleset) do
{
'exclusions' => [{
'conditions' => [{
'operator' => 'ip_match',
'parameters' => {
'inputs' => [{
'address' => 'http.client_ip'
}]
}
}]
}],
'metadata' => {
'rules_version' => '1.5.2'
},
'rules' => [{
'conditions' => [{
'operator' => 'ip_match',
'parameters' => {
'data' => 'blocked_ips',
'inputs' => [{
'address' => 'http.client_ip'
}]
}
}],
'id' => 'blk-001-001',
'name' => 'Block IP Addresses',
'on_match' => ['block'],
'tags' => {
'category' => 'security_response', 'type' => 'block_ip'
},
'transformers' => []
}],
'rules_data' => [{
'data' => [{
'expiration' => 1678972458,
'value' => '42.42.42.1'
}]
}],
'version' => '2.2'
}
end

context 'lock' do
it 'makes sure to synchronize' do
mutex = Mutex.new
Expand All @@ -88,47 +131,6 @@

old_processor = component.processor

ruleset = {
'exclusions' => [{
'conditions' => [{
'operator' => 'ip_match',
'parameters' => {
'inputs' => [{
'address' => 'http.client_ip'
}]
}
}]
}],
'metadata' => {
'rules_version' => '1.5.2'
},
'rules' => [{
'conditions' => [{
'operator' => 'ip_match',
'parameters' => {
'data' => 'blocked_ips',
'inputs' => [{
'address' => 'http.client_ip'
}]
}
}],
'id' => 'blk-001-001',
'name' => 'Block IP Addresses',
'on_match' => ['block'],
'tags' => {
'category' => 'security_response', 'type' => 'block_ip'
},
'transformers' => []
}],
'rules_data' => [{
'data' => [{
'expiration' => 1678972458,
'value' => '42.42.42.1'
}]
}],
'version' => '2.2'
}

expect(old_processor).to receive(:finalize)
component.reconfigure(ruleset: ruleset)
new_processor = component.processor
Expand All @@ -137,6 +139,21 @@
end
end

context 'when the new processor is ready, and old processor is nil' do
it 'swaps the processor instance and do not finalize the old processor' do
processor = nil
component = described_class.new(processor: processor)

old_processor = component.processor

expect(old_processor).to_not receive(:finalize)
component.reconfigure(ruleset: ruleset)
new_processor = component.processor
expect(new_processor).to_not eq(old_processor)
new_processor.finalize
end
end

context 'when the new processor is not ready' do
it 'does not swap the processor instance and finalize the old processor' do
processor = instance_double(Datadog::AppSec::Processor)
Expand Down

0 comments on commit 3c84e41

Please sign in to comment.