Skip to content

Commit

Permalink
Rename AppSec::Context methods to match the class
Browse files Browse the repository at this point in the history
  • Loading branch information
Strech committed Jan 9, 2025
1 parent 710fe48 commit 23fa3e1
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 68 deletions.
36 changes: 18 additions & 18 deletions lib/datadog/appsec/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module Datadog
module AppSec
# Write desciption TODO
class Context
InactiveScopeError = Class.new(StandardError)
ActiveScopeError = Class.new(StandardError)

attr_reader :trace, :service_entry_span, :processor_context

def initialize(trace, service_entry_span, processor_context)
Expand All @@ -17,42 +20,39 @@ def finalize
end

class << self
def activate_scope(trace, service_entry_span, processor)
raise ActiveScopeError, 'another scope is active, nested scopes are not supported' if active_scope
def activate_context(trace, service_entry_span, processor)
raise ActiveScopeError, 'another scope is active, nested scopes are not supported' if active_context

context = processor.new_context
self.active_scope = new(trace, service_entry_span, context)
self.active_context = new(trace, service_entry_span, context)
end

def deactivate_scope
raise InactiveScopeError, 'no scope is active, nested scopes are not supported' unless active_scope
def deactivate_context
raise InactiveScopeError, 'no context is active, nested contexts are not supported' unless active_context

scope = active_scope
context = active_context

reset_active_scope
reset_active_context

scope.finalize
context.finalize
end

def active_scope
Thread.current[:datadog_appsec_active_scope]
def active_context
Thread.current[Ext::ACTIVE_CONTEXT_KEY]
end

private

def active_scope=(scope)
raise ArgumentError, 'not a Datadog::AppSec::Scope' unless scope.instance_of?(Context)
def active_context=(context)
raise ArgumentError, 'not a Datadog::AppSec::Context' unless context.instance_of?(Context)

Thread.current[:datadog_appsec_active_scope] = scope
Thread.current[Ext::ACTIVE_CONTEXT_KEY] = context
end

def reset_active_scope
Thread.current[:datadog_appsec_active_scope] = nil
def reset_active_context
Thread.current[Ext::ACTIVE_CONTEXT_KEY] = nil
end
end

class InactiveScopeError < StandardError; end
class ActiveScopeError < StandardError; end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Instrumentation
module_function

def detect_sql_injection(sql, adapter_name)
context = AppSec.active_scope
context = AppSec.active_context
return unless context

# libddwaf expects db system to be lowercase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def validate(resource, &block)

automated_track_user_events_mode = track_user_events_configuration.mode

appsec_context = Datadog::AppSec.active_scope
appsec_context = Datadog::AppSec.active_context

return result unless appsec_context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create

automated_track_user_events_mode = track_user_events_configuration.mode

appsec_context = Datadog::AppSec.active_scope
appsec_context = Datadog::AppSec.active_context
return super unless appsec_context

super do |resource|
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/contrib/graphql/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def watch_multiplex(gateway = Instrumentation.gateway)
gateway.watch('graphql.multiplex', :appsec) do |stack, gateway_multiplex|
block = false
event = nil
context = AppSec::Context.active_scope
context = AppSec::Context.active_context
engine = AppSec::Reactive::Engine.new

if context
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/appsec/contrib/rack/request_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def call(env)
# For a given request, keep using the first Rack stack scope for
# nested apps. Don't set `context` local variable so that on popping
# out of this nested stack we don't finalize the parent's context
return @app.call(env) if active_scope(env)
return @app.call(env) if active_context(env)

Datadog::AppSec.reconfigure_lock do
processor = Datadog::AppSec.processor

if !processor.nil? && processor.ready?
ctx = Datadog::AppSec::Context.activate_scope(active_trace, active_span, processor)
ctx = Datadog::AppSec::Context.activate_context(active_trace, active_span, processor)
env[Datadog::AppSec::Ext::CONTEXT_KEY] = ctx
ready = true
end
Expand Down Expand Up @@ -117,14 +117,14 @@ def call(env)
ensure
if ctx
add_waf_runtime_tags(ctx)
Datadog::AppSec::Context.deactivate_scope
Datadog::AppSec::Context.deactivate_context
end
end
# rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/MethodLength

private

def active_scope(env)
def active_context(env)
env[Datadog::AppSec::Ext::CONTEXT_KEY]
end

Expand Down
1 change: 1 addition & 0 deletions lib/datadog/appsec/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module AppSec
module Ext
INTERRUPT = :datadog_appsec_interrupt
CONTEXT_KEY = 'datadog.appsec.context'
ACTIVE_CONTEXT_KEY = :datadog_appsec_active_context

TAG_APPSEC_ENABLED = '_dd.appsec.enabled'
TAG_APM_ENABLED = '_dd.apm.enabled'
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/monitor/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def watch
def watch_user_id(gateway = Instrumentation.gateway)
gateway.watch('identity.set_user', :appsec) do |stack, user|
event = nil
context = Datadog::AppSec.active_scope
context = Datadog::AppSec.active_context
engine = AppSec::Reactive::Engine.new

Monitor::Reactive::SetUser.subscribe(engine, context.processor_context) do |result|
Expand Down
42 changes: 21 additions & 21 deletions spec/datadog/appsec/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,80 @@
let(:processor) { Datadog::AppSec::Processor.new(ruleset: ruleset, telemetry: telemetry) }

after do
described_class.send(:reset_active_scope)
described_class.send(:reset_active_context)
processor.finalize
end

describe '.activate_scope' do
describe '.activate_context' do
context 'with no active scope' do
subject(:activate_scope) { described_class.activate_scope(trace, span, processor) }
subject(:activate_context) { described_class.activate_context(trace, span, processor) }

it 'returns a new scope' do
expect(activate_scope).to be_a described_class
expect(activate_context).to be_a described_class
end

it 'sets the active scope' do
expect { activate_scope }.to change { described_class.active_scope }.from(nil).to be_a described_class
expect { activate_context }.to change { described_class.active_context }.from(nil).to be_a described_class
end
end

context 'with an active scope' do
before do
described_class.activate_scope(trace, span, processor)
described_class.activate_context(trace, span, processor)
end

subject(:activate_scope) { described_class.activate_scope(trace, span, processor) }
subject(:activate_context) { described_class.activate_context(trace, span, processor) }

it 'raises ActiveScopeError' do
expect { activate_scope }.to raise_error Datadog::AppSec::Context::ActiveScopeError
expect { activate_context }.to raise_error Datadog::AppSec::Context::ActiveScopeError
end

it 'does not change the active scope' do
expect { activate_scope rescue nil }.to_not(change { described_class.active_scope })
expect { activate_context rescue nil }.to_not(change { described_class.active_context })
end
end
end

describe '.deactivate_scope' do
describe '.deactivate_context' do
context 'with no active scope' do
subject(:deactivate_scope) { described_class.deactivate_scope }
subject(:deactivate_context) { described_class.deactivate_context }

it 'raises ActiveContextError' do
expect { deactivate_scope }.to raise_error Datadog::AppSec::Context::InactiveScopeError
expect { deactivate_context }.to raise_error Datadog::AppSec::Context::InactiveScopeError
end

it 'does not change the active scope' do
expect { deactivate_scope rescue nil }.to_not(change { described_class.active_scope })
expect { deactivate_context rescue nil }.to_not(change { described_class.active_context })
end
end

context 'with an active scope' do
let(:active_scope) { described_class.active_scope }
subject(:deactivate_scope) { described_class.deactivate_scope }
let(:active_context) { described_class.active_context }
subject(:deactivate_context) { described_class.deactivate_context }

before do
allow(described_class).to receive(:new).and_call_original

described_class.activate_scope(trace, span, processor)
described_class.activate_context(trace, span, processor)

expect(active_scope).to receive(:finalize).and_call_original
expect(active_context).to receive(:finalize).and_call_original
end

it 'unsets the active scope' do
expect { deactivate_scope }.to change { described_class.active_scope }.from(active_scope).to nil
expect { deactivate_context }.to change { described_class.active_context }.from(active_context).to nil
end
end
end

describe '.active_scope' do
subject(:active_scope) { described_class.active_scope }
describe '.active_context' do
subject(:active_context) { described_class.active_context }

context 'with no active context' do
it { is_expected.to be_nil }
end

context 'with an active context' do
before { described_class.activate_scope(trace, span, processor) }
before { described_class.activate_context(trace, span, processor) }

it { is_expected.to be_a described_class }
end
Expand Down
12 changes: 6 additions & 6 deletions spec/datadog/appsec/contrib/active_record/mysql2_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@
c.appsec.instrument :active_record
end

Datadog::AppSec::Context.activate_scope(trace, span, processor)
Datadog::AppSec::Context.activate_context(trace, span, processor)

raise_on_rails_deprecation!
end

after do
Datadog.configuration.reset!

Datadog::AppSec::Context.deactivate_scope
Datadog::AppSec::Context.deactivate_context
processor.finalize
end

it 'calls waf with correct arguments when querying using .where' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).with(
{},
{
Expand All @@ -80,7 +80,7 @@
end

it 'calls waf with correct arguments when querying using .find_by_sql' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).with(
{},
{
Expand All @@ -95,11 +95,11 @@
end

it 'adds an event to processor context if waf status is :match' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).and_return(instance_double(Datadog::AppSec::WAF::Result, status: :match, actions: {}))
)

expect(Datadog::AppSec.active_scope.processor_context.events).to receive(:<<).and_call_original
expect(Datadog::AppSec.active_context.processor_context.events).to receive(:<<).and_call_original

User.where(name: 'Bob').to_a
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@
c.appsec.instrument :active_record
end

Datadog::AppSec::Context.activate_scope(trace, span, processor)
Datadog::AppSec::Context.activate_context(trace, span, processor)

raise_on_rails_deprecation!
end

after do
Datadog.configuration.reset!

Datadog::AppSec::Context.deactivate_scope
Datadog::AppSec::Context.deactivate_context
processor.finalize
end

Expand All @@ -72,7 +72,7 @@
'SELECT "users".* FROM "users" WHERE "users"."name" = $1'
end

expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).with(
{},
{
Expand All @@ -87,7 +87,7 @@
end

it 'calls waf with correct arguments when querying using .find_by_sql' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).with(
{},
{
Expand All @@ -102,11 +102,11 @@
end

it 'adds an event to processor context if waf status is :match' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).and_return(instance_double(Datadog::AppSec::WAF::Result, status: :match, actions: {}))
)

expect(Datadog::AppSec.active_scope.processor_context.events).to receive(:<<).and_call_original
expect(Datadog::AppSec.active_context.processor_context.events).to receive(:<<).and_call_original

User.where(name: 'Bob').to_a
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@
c.appsec.instrument :active_record
end

Datadog::AppSec::Context.activate_scope(trace, span, processor)
Datadog::AppSec::Context.activate_context(trace, span, processor)

raise_on_rails_deprecation!
end

after do
Datadog.configuration.reset!

Datadog::AppSec::Context.deactivate_scope
Datadog::AppSec::Context.deactivate_context
processor.finalize
end

it 'calls waf with correct arguments when querying using .where' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).with(
{},
{
Expand All @@ -77,7 +77,7 @@
end

it 'calls waf with correct arguments when querying using .find_by_sql' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).with(
{},
{
Expand All @@ -92,11 +92,11 @@
end

it 'adds an event to processor context if waf status is :match' do
expect(Datadog::AppSec.active_scope.processor_context).to(
expect(Datadog::AppSec.active_context.processor_context).to(
receive(:run).and_return(instance_double(Datadog::AppSec::WAF::Result, status: :match, actions: {}))
)

expect(Datadog::AppSec.active_scope.processor_context.events).to receive(:<<).and_call_original
expect(Datadog::AppSec.active_context.processor_context.events).to receive(:<<).and_call_original

User.where(name: 'Bob').to_a
end
Expand Down
Loading

0 comments on commit 23fa3e1

Please sign in to comment.