Skip to content

Commit

Permalink
Merge pull request #2637 from DataDog/appsec-make-sure-processor-is-r…
Browse files Browse the repository at this point in the history
…eady

make sure to assign a valid processor to the appsec component
  • Loading branch information
GustavoCaso authored Feb 23, 2023
2 parents 2b163dd + 09ee622 commit 9d4eae6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
20 changes: 16 additions & 4 deletions lib/datadog/appsec/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,31 @@ class << self
def build_appsec_component(settings)
return unless settings.enabled

new
processor = create_processor
new(processor: processor)
end

private

def create_processor
processor = Processor.new
return nil unless processor.ready?

processor
end
end

attr_reader :processor

def initialize(processor: Processor.new)
def initialize(processor:)
@processor = processor
end

def shutdown!
processor.finalize if processor
@processor = nil
if processor && processor.ready?
processor.finalize
@processor = nil
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/contrib/rack/request_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def call(env)

processor = Datadog::AppSec.processor

return @app.call(env) unless processor.ready?
return @app.call(env) if processor.nil? || !processor.ready?

# TODO: handle exceptions, except for @app.call

Expand Down
6 changes: 5 additions & 1 deletion sig/datadog/appsec/component.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module Datadog
class Component
def self.build_appsec_component: (Datadog::AppSec::Configuration::Settings settings) -> Datadog::AppSec::Component?

private

def self.create_processor: () -> Datadog::AppSec::Processor?

attr_reader processor: Datadog::AppSec::Processor?

def initialize: (?processor: Datadog::AppSec::Processor?) -> void
def initialize: (processor: Datadog::AppSec::Processor?) -> void

def shutdown!: () -> untyped
end
Expand Down
63 changes: 51 additions & 12 deletions spec/datadog/appsec/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,80 @@

RSpec.describe Datadog::AppSec::Component do
describe '.build_appsec_component' do
let(:settings) do
Datadog::AppSec::Configuration::Settings.new.merge(
Datadog::AppSec::Configuration::DSL.new.tap do |appsec|
appsec.enabled = appsec_enabled
end
)
end
context 'when appsec is enabled' do
let(:appsec_enabled) { true }
it 'returns a Datadog::AppSec::Component instance' do
settings = Datadog::AppSec::Configuration::Settings.new.merge(
Datadog::AppSec::Configuration::DSL.new.tap do |it|
it.enabled = true
end
)
component = described_class.build_appsec_component(settings)
expect(component).to be_a(described_class)
end

context 'when processor is ready' do
it 'returns a Datadog::AppSec::Component with a processor instance' do
expect_any_instance_of(Datadog::AppSec::Processor).to receive(:ready?).and_return(true)
component = described_class.build_appsec_component(settings)

expect(component.processor).to be_a(Datadog::AppSec::Processor)
end
end

context 'when processor fail to instanciate' do
it 'returns a Datadog::AppSec::Component with a nil processor' do
expect_any_instance_of(Datadog::AppSec::Processor).to receive(:ready?).and_return(false)
component = described_class.build_appsec_component(settings)

expect(component.processor).to be_nil
end
end
end

context 'when appsec is not enabled' do
let(:appsec_enabled) { false }

it 'returns nil' do
settings = Datadog::AppSec::Configuration::Settings.new.merge(
Datadog::AppSec::Configuration::DSL.new.tap do |it|
it.enabled = false
end
)
component = described_class.build_appsec_component(settings)
expect(component).to be_nil
end
end
end

describe '#shutdown!' do
context 'when processor is not nil' do
context 'when processor is not nil and ready' do
it 'finalizes the processor' do
processor = instance_double(Datadog::AppSec::Processor)

component = described_class.new(processor: processor)

expect(processor).to receive(:ready?).and_return(true)
expect(processor).to receive(:finalize)
component.shutdown!
end
end

context 'when processor is not ready' do
it 'does not finalize the processor' do
processor = instance_double(Datadog::AppSec::Processor)
expect(processor).to receive(:ready?).and_return(false)

component = described_class.new(processor: processor)

expect(processor).to_not receive(:finalize)
component.shutdown!
end
end

context 'when processor is nil' do
it 'does not finalize the processor' do
component = described_class.new(processor: nil)

expect_any_instance_of(Datadog::AppSec::Processor).to_not receive(:finalize)
component.shutdown!
end
end
end
end

0 comments on commit 9d4eae6

Please sign in to comment.