Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Next Next commit
Add handling of string keys for Kit:AppSec::Events
  • Loading branch information
Strech committed Apr 1, 2025
commit 0d2bc4339603570c2cbf9c3f90ff9e463e8595f5
9 changes: 6 additions & 3 deletions lib/datadog/kit/appsec/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Events
LOGIN_SUCCESS_EVENT = 'users.login.success'
LOGIN_FAILURE_EVENT = 'users.login.failure'
SIGNUP_EVENT = 'users.signup'
USER_LOGIN_KEYS = ['usr.login', :'usr.login'].freeze

class << self
# Attach login success event information to the trace
Expand All @@ -30,10 +31,11 @@ def track_login_success(trace = nil, span = nil, user:, **others)
set_trace_and_span_context('track_login_success', trace, span) do |active_trace, active_span|
user_options = user.dup
user_id = user_options.delete(:id)
user_login = user_options[:login] || others[:'usr.login'] || user_id
user_login = user_options[:login] || others[:'usr.login'] || others['usr.login'] || user_id

raise ArgumentError, 'missing required key: :user => { :id }' if user_id.nil?

others = others.reject { |key, _| USER_LOGIN_KEYS.include?(key) }
others[:'usr.login'] = user_login
track(LOGIN_SUCCESS_EVENT, active_trace, active_span, **others)

Expand All @@ -58,7 +60,7 @@ def track_login_success(trace = nil, span = nil, user:, **others)
# event information to attach to the trace.
def track_login_failure(trace = nil, span = nil, user_exists:, user_id: nil, **others)
set_trace_and_span_context('track_login_failure', trace, span) do |active_trace, active_span|
others[:'usr.login'] = user_id if user_id && !others.key?(:'usr.login')
others[:'usr.login'] = user_id if user_id && !others.key?(:'usr.login') && !others.key?('usr.login')
track(LOGIN_FAILURE_EVENT, active_trace, active_span, **others)

active_span.set_tag('appsec.events.users.login.failure.usr.id', user_id) if user_id
Expand All @@ -84,10 +86,11 @@ def track_signup(trace = nil, span = nil, user:, **others)
set_trace_and_span_context('track_signup', trace, span) do |active_trace, active_span|
user_options = user.dup
user_id = user_options.delete(:id)
user_login = user_options[:login] || others[:'usr.login'] || user_id
user_login = user_options[:login] || others[:'usr.login'] || others['usr.login'] || user_id

raise ArgumentError, 'missing required key: :user => { :id }' if user_id.nil?

others = others.reject { |key, _| USER_LOGIN_KEYS.include?(key) }
others[:'usr.login'] = user_login
track(SIGNUP_EVENT, active_trace, active_span, **others)

Expand Down
19 changes: 16 additions & 3 deletions sig/datadog/kit/appsec/events.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ module Datadog
module AppSec
module Events
LOGIN_SUCCESS_EVENT: ::String

LOGIN_FAILURE_EVENT: ::String

def self.track_login_success: (Datadog::Tracing::TraceOperation trace, user: Hash[::Symbol, ::String | nil], **::Hash[::Symbol, ::String | nil] others) -> void
SIGNUP_EVENT: ::String

USER_LOGIN_KEYS: Array[String | Symbol]

def self.track_login_success: (?Tracing::TraceOperation? trace, ?Tracing::SpanOperation? span, user: Hash[Symbol, String], **Hash[Symbol | String, String] others) -> void

def self.track_login_failure: (?Tracing::TraceOperation? trace, ?Tracing::SpanOperation? span, user_exists: bool, ?user_id: String?, **Hash[Symbol | String, String] others) -> void

def self.track_signup: (?Tracing::TraceOperation? trace, ?Tracing::SpanOperation? span, user: Hash[Symbol, String], **Hash[Symbol | String, String] others) -> void

def self.track: (String event, ?Tracing::TraceOperation? trace, ?Tracing::SpanOperation? span, **Hash[Symbol, String] others) -> void

private

def self.track_login_failure: (Datadog::Tracing::TraceOperation trace, user_id: ::String, user_exists: bool, **::Hash[::Symbol, ::String | nil] others) -> void
def self.set_trace_and_span_context: (String method, ?Tracing::TraceOperation? trace, ?Tracing::SpanOperation? span) { (Tracing::TraceOperation, Tracing::SpanOperation) -> void } -> void

def self.track: (::String | ::Symbol event, Datadog::Tracing::TraceOperation trace, **::Hash[::Symbol, ::String | nil] others) -> void
def self.check_trace_span_integrity: (Tracing::TraceOperation trace, Tracing::SpanOperation span) -> void
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/datadog/kit/appsec/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@
end
end

if PlatformHelpers.mri? && PlatformHelpers.engine_version >= Gem::Version.new('2.7')
it 'sets additional user login data from other string keys as tags' do
trace_op.measure('root') do |span, _|
expect { described_class.track_login_success(trace_op, user: { id: '42' }, 'usr.login' => 'hey') }
.to change { span.tags }.to include(
'usr.id' => '42',
'usr.login' => 'hey',
'appsec.events.users.login.success.usr.login' => 'hey'
)
end
end
end

it 'sets event tracking key on trace' do
trace_op.measure('root') do |span, _|
expect { described_class.track_login_success(trace_op, user: { id: '42' }) }
Expand Down Expand Up @@ -181,6 +194,15 @@
end
end

if PlatformHelpers.mri? && PlatformHelpers.engine_version >= Gem::Version.new('2.7')
it 'sets additional user login data from other string keys as tags' do
trace_op.measure('root') do |span, _|
expect { described_class.track_login_failure(trace_op, user_id: '42', user_exists: true, 'usr.login' => 'hey') }
.to change { span.tags }.to include('appsec.events.users.login.failure.usr.login' => 'hey')
end
end
end

it 'sets event tracking key on trace' do
trace_op.measure('root') do |span, _trace|
described_class.track_login_failure(trace_op, user_id: '42', user_exists: true)
Expand Down Expand Up @@ -286,6 +308,19 @@
end
end

if PlatformHelpers.mri? && PlatformHelpers.engine_version >= Gem::Version.new('2.7')
it 'sets additional user login data from other string keys as tags' do
trace_op.measure('root') do |span, _|
expect { described_class.track_signup(trace_op, user: { id: '42' }, 'usr.login' => 'hey') }
.to change { span.tags }.to include(
'usr.id' => '42',
'usr.login' => 'hey',
'appsec.events.users.signup.usr.login' => 'hey'
)
end
end
end

it 'sets additional user login data as tags with user data priority' do
trace_op.measure('root') do |span, _|
expect { described_class.track_signup(trace_op, user: { id: '42', login: 'hey' }, 'usr.login': 'extra') }
Expand Down