Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not try accessing the appsec settings if the appsec module is not loaded #2679

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 defined?(Datadog::AppSec::Configuration) && settings.appsec.enabled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially suggested defined?, but looking at the code here, I think we could probably get away with respond_to?(:appsec).

That would probably make it way easier to test -- we could just pass in a mock settings object that did not have an appsec entry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes a lot of sense. I like the idea of being able to test that functionality as well.

Thanks for the suggestion 😄


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
11 changes: 7 additions & 4 deletions spec/datadog/appsec/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
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
double(
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
Expand Down