Skip to content

Commit

Permalink
Merge pull request #2679 from DataDog/appsec-make-sure-appsec-is-load…
Browse files Browse the repository at this point in the history
…ed-when-creating-appsec-component

Do not try accessing the appsec settings if the appsec module is not loaded
  • Loading branch information
GustavoCaso authored Mar 10, 2023
2 parents 47ad37d + b11eaaa commit bfbc8f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/appsec/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module AppSec
class Component
class << self
def build_appsec_component(settings)
return unless settings.enabled
return unless settings.respond_to?(:appsec) && settings.appsec.enabled

processor = create_processor
new(processor: processor)
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/core/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def initialize(settings)
@telemetry = self.class.build_telemetry(settings)

# AppSec
@appsec = Datadog::AppSec::Component.build_appsec_component(settings.appsec)
@appsec = Datadog::AppSec::Component.build_appsec_component(settings)
end

# Starts up components
Expand Down
31 changes: 22 additions & 9 deletions spec/datadog/appsec/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@

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
let(:seetings_without_appsec) { double(Datadog::Core::Configuration) }

let(:settings_with_appsec) do
double(
Datadog::Core::Configuration,
appsec: 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
component = described_class.build_appsec_component(settings)
component = described_class.build_appsec_component(settings_with_appsec)
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)
component = described_class.build_appsec_component(settings_with_appsec)

expect(component.processor).to be_a(Datadog::AppSec::Processor)
end
Expand All @@ -29,7 +35,7 @@
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)
component = described_class.build_appsec_component(settings_with_appsec)

expect(component.processor).to be_nil
end
Expand All @@ -40,7 +46,14 @@
let(:appsec_enabled) { false }

it 'returns nil' do
component = described_class.build_appsec_component(settings)
component = described_class.build_appsec_component(settings_with_appsec)
expect(component).to be_nil
end
end

context 'when appsec is not active' do
it 'returns nil' do
component = described_class.build_appsec_component(seetings_without_appsec)
expect(component).to be_nil
end
end
Expand Down

0 comments on commit bfbc8f6

Please sign in to comment.