Skip to content

Commit

Permalink
rename-me Change AppSec::Context ivars
Browse files Browse the repository at this point in the history
  • Loading branch information
Strech committed Jan 10, 2025
1 parent 97b0d49 commit fae2610
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 60 deletions.
21 changes: 16 additions & 5 deletions lib/datadog/appsec/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ module AppSec
class Context
ActiveContextError = Class.new(StandardError)

attr_reader :trace, :service_entry_span, :processor_context
# XXX: Continue from here:
# 1. Replace naming of processor_context into waf_runner
# 2. Replace calls of waf run
attr_reader :trace, :span, :processor_context

class << self
def activate(context)
Expand All @@ -32,14 +35,22 @@ def initialize(trace, span, security_engine)
@trace = trace
@span = span
@security_engine = security_engine
@waf_runner = security_engine.new_context

# TODO: Rename
@service_entry_span = span
@processor_context = security_engine.new_context
# FIXME: Left for compatibility now
@processor_context = @waf_runner
end

def run_waf(persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
@waf_runner.run(persistent_data, ephemeral_data, timeout)
end

def run_rasp(_type, persistent_data, ephemeral_data, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
@waf_runner.run(persistent_data, ephemeral_data, timeout)
end

def finalize
@processor_context.finalize
@waf_runner.finalize
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def detect_sql_injection(sql, adapter_name)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
sql: sql,
actions: result.actions
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def validate(resource, &block)

Tracking.track_login_success(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: event_information.user_id,
**event_information.to_h
)
Expand All @@ -60,7 +60,7 @@ def validate(resource, &block)

Tracking.track_login_failure(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: event_information.user_id,
user_exists: user_exists,
**event_information.to_h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create

Tracking.track_signup(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: event_information.user_id,
**event_information.to_h
)
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 @@ -32,7 +32,7 @@ def watch_multiplex(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
multiplex: gateway_multiplex,
actions: result.actions
}
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/appsec/contrib/rack/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def watch_request(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
request: gateway_request,
actions: result.actions
}
Expand Down Expand Up @@ -73,7 +73,7 @@ def watch_response(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
response: gateway_response,
actions: result.actions
}
Expand Down Expand Up @@ -111,7 +111,7 @@ def watch_request_body(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
request: gateway_request,
actions: result.actions
}
Expand Down
10 changes: 5 additions & 5 deletions lib/datadog/appsec/contrib/rack/request_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def call(env)
if result
ctx.processor_context.events << {
trace: ctx.trace,
span: ctx.service_entry_span,
span: ctx.span,
waf_result: result,
}
end
Expand All @@ -109,7 +109,7 @@ def call(env)
e[:request] ||= gateway_request
end

AppSec::Event.record(ctx.service_entry_span, *ctx.processor_context.events)
AppSec::Event.record(ctx.span, *ctx.processor_context.events)

if response_response
blocked_event = response_response.find { |action, _options| action == :block }
Expand Down Expand Up @@ -148,7 +148,7 @@ def active_span
end

def add_appsec_tags(processor, context)
span = context.service_entry_span
span = context.span
trace = context.trace

return unless trace && span
Expand Down Expand Up @@ -185,7 +185,7 @@ def add_appsec_tags(processor, context)
end

def add_request_tags(context, env)
span = context.service_entry_span
span = context.span

return unless span

Expand All @@ -208,7 +208,7 @@ def add_request_tags(context, env)
end

def add_waf_runtime_tags(context)
span = context.service_entry_span
span = context.span
context = context.processor_context

return unless span && context
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/contrib/rails/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def watch_request_action(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
request: gateway_request,
actions: result.actions
}
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def watch_request_dispatch(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
request: gateway_request,
actions: result.actions
}
Expand Down Expand Up @@ -71,7 +71,7 @@ def watch_request_routed(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
request: gateway_request,
actions: result.actions
}
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/appsec/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def tag_and_keep!(context, waf_result)
# We want to keep the trace in case of security event
context.trace.keep! if context.trace

if context.service_entry_span
context.service_entry_span.set_tag('appsec.blocked', 'true') if waf_result.actions.key?('block_request')
context.service_entry_span.set_tag('appsec.event', 'true')
if context.span
context.span.set_tag('appsec.blocked', 'true') if waf_result.actions.key?('block_request')
context.span.set_tag('appsec.event', 'true')
end

add_distributed_tags(context.trace)
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 @@ -29,7 +29,7 @@ def watch_user_id(gateway = Instrumentation.gateway)
event = {
waf_result: result,
trace: context.trace,
span: context.service_entry_span,
span: context.span,
user: user,
actions: result.actions
}
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/kit/appsec/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def track(event, trace = nil, span = nil, **others)
def set_trace_and_span_context(method, trace = nil, span = nil)
if (appsec_context = Datadog::AppSec.active_context)
trace = appsec_context.trace
span = appsec_context.service_entry_span
span = appsec_context.span
end

trace ||= Datadog::Tracing.active_trace
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/kit/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def set_user(
def set_trace_and_span_context(method, trace = nil, span = nil)
if (appsec_context = Datadog::AppSec.active_context)
trace = appsec_context.trace
span = appsec_context.service_entry_span
span = appsec_context.span
end

trace ||= Datadog::Tracing.active_trace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def initialize(id, email, username)
context 'when logging in from Rememberable devise strategy' do
let(:appsec_enabled) { true }
let(:track_user_events_enabled) { true }
let(:appsec_context) { instance_double(Datadog::AppSec::Context, trace: double, service_entry_span: double) }
let(:appsec_context) { instance_double(Datadog::AppSec::Context, trace: double, span: double) }

let(:mock_klass) do
Class.new do
Expand All @@ -109,7 +109,7 @@ def initialize(result)
context 'successful login' do
let(:appsec_enabled) { true }
let(:track_user_events_enabled) { true }
let(:appsec_context) { instance_double(Datadog::AppSec::Context, trace: double, service_entry_span: double) }
let(:appsec_context) { instance_double(Datadog::AppSec::Context, trace: double, span: double) }

context 'with resource ID' do
context 'safe mode' do
Expand All @@ -118,7 +118,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_success).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: resource.id,
**{}
)
Expand All @@ -132,7 +132,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_success).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: resource.id,
**{ username: 'John', email: '[email protected]' }
)
Expand All @@ -150,7 +150,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_success).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: nil,
**{}
)
Expand All @@ -164,7 +164,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_success).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: nil,
**{ username: 'John', email: '[email protected]' }
)
Expand All @@ -177,7 +177,7 @@ def initialize(result)
context 'unsuccessful login' do
let(:appsec_enabled) { true }
let(:track_user_events_enabled) { true }
let(:appsec_context) { instance_double(Datadog::AppSec::Context, trace: double, service_entry_span: double) }
let(:appsec_context) { instance_double(Datadog::AppSec::Context, trace: double, span: double) }

context 'with resource' do
context 'safe mode' do
Expand All @@ -186,7 +186,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_failure).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: resource.id,
user_exists: true,
**{}
Expand All @@ -201,7 +201,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_failure).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: resource.id,
user_exists: true,
**{ username: 'John', email: '[email protected]' }
Expand All @@ -218,7 +218,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_failure).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: nil,
user_exists: false,
**{}
Expand All @@ -233,7 +233,7 @@ def initialize(result)
it 'tracks event' do
expect(Datadog::AppSec::Contrib::Devise::Tracking).to receive(:track_login_failure).with(
appsec_context.trace,
appsec_context.service_entry_span,
appsec_context.span,
user_id: nil,
user_exists: false,
**{}
Expand Down
Loading

0 comments on commit fae2610

Please sign in to comment.