Skip to content

Commit

Permalink
rack watch for user_id events
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Feb 14, 2023
1 parent 31bca90 commit 3c068ca
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion lib/datadog/appsec/contrib/rack/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def watch
watch_request(gateway)
watch_response(gateway)
watch_request_body(gateway)
watch_user_id(gateway)
end

def watch_request(gateway = Instrumentation.gateway)
Expand Down Expand Up @@ -152,8 +153,47 @@ def watch_request_body(gateway = Instrumentation.gateway)
end
end

def watch_user_id(gateway = Instrumentation.gateway)
gateway.watch('identity.set_user', :appsec) do |stack, user|
block = false
event = nil
waf_context = Datadog::AppSec::Processor.current_context

AppSec::Reactive::Operation.new('identity.set_user') do |op|
trace = active_trace
span = active_span

[ret, res]
Rack::Reactive::SetUser.subscribe(op, waf_context) do |result, _block|
if result.status == :match
# TODO: should this hash be an Event instance instead?
event = {
waf_result: result,
trace: trace,
span: span,
user: user,
actions: result.actions
}

span.set_tag('appsec.event', 'true') if span

waf_context.events << event
end
end

_result, block = Rack::Reactive::SetUser.publish(op, user)
end

next [nil, [[:block, event]]] if block

ret, res = stack.call(request)

if event
res ||= []
res << [:monitor, event]
end

[ret, res]
end
end

private
Expand Down

0 comments on commit 3c068ca

Please sign in to comment.