Skip to content

Commit

Permalink
Merge pull request #2820 from DataDog/appsec-fix-flaky-test
Browse files Browse the repository at this point in the history
fix flaky test by using expect API to make sure the original code doe…
  • Loading branch information
lloeki authored Apr 27, 2023
2 parents 4fe4713 + 0733776 commit 9b9de4f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 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
5 changes: 2 additions & 3 deletions lib/datadog/appsec/contrib/rack/request_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ def initialize(app, opt = {})
@oneshot_tags_sent = false
end

# rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/MethodLength
# rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/MethodLength
def call(env)
return @app.call(env) unless Datadog::AppSec.enabled?

Datadog::Core::Remote.active_remote.barrier(:once) unless Datadog::Core::Remote.active_remote.nil?
processor = Datadog::AppSec.processor

processor = nil
ready = false
Expand Down Expand Up @@ -89,7 +88,7 @@ def call(env)
processor.deactivate_context
end
end
# rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/MethodLength
# rubocop:enable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/MethodLength

private

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
8 changes: 7 additions & 1 deletion spec/datadog/appsec/remote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,24 @@
}

expect(Datadog::AppSec).to receive(:reconfigure).with(ruleset: expected_ruleset)
.and_return(nil)
changes = transaction
receiver.call(repository, changes)
end

context 'when there is no ASM_DD information' do
let(:transaction) { repository.transaction { |repository, transaction| } }

it 'uses the rules from the appsec settings' do
ruleset = 'foo'

expect(Datadog::AppSec::Processor::RuleLoader).to receive(:load_rules).with(
ruleset: Datadog.configuration.appsec.ruleset
).at_least(:once).and_call_original
).and_return(ruleset)

changes = transaction
expect(Datadog::AppSec).to receive(:reconfigure).with(ruleset: ruleset)
.and_return(nil)
receiver.call(repository, changes)
end

Expand Down

0 comments on commit 9b9de4f

Please sign in to comment.