Skip to content

Commit

Permalink
Remove redundant references to AppSec
Browse files Browse the repository at this point in the history
This scoping is now hoisted to the namespace.
  • Loading branch information
lloeki committed Jan 26, 2023
1 parent 788815b commit a2ba515
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
17 changes: 6 additions & 11 deletions lib/datadog/kit/appsec/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module Kit
module AppSec
# Tracking events
module Events
APPSEC_LOGIN_SUCCESS_EVENT = 'users.login.success'
APPSEC_LOGIN_FAILURE_EVENT = 'users.login.failure'
LOGIN_SUCCESS_EVENT = 'users.login.success'
LOGIN_FAILURE_EVENT = 'users.login.failure'

# Attach login success event information to the trace
#
Expand All @@ -21,7 +21,7 @@ module Events
# @param others [Hash<String || Symbol, String>] Additional free-form
# event information to attach to the trace.
def self.track_login_success(trace, user:, **others)
track(:appsec, APPSEC_LOGIN_SUCCESS_EVENT, trace, **others)
track(LOGIN_SUCCESS_EVENT, trace, **others)

user_options = user.dup
user_id = user.delete(:id)
Expand All @@ -41,7 +41,7 @@ def self.track_login_success(trace, user:, **others)
# @param others [Hash<String || Symbol, String>] Additional free-form
# event information to attach to the trace.
def self.track_login_failure(trace, user_id:, user_exists:, **others)
track(:appsec, APPSEC_LOGIN_FAILURE_EVENT, trace, **others)
track(LOGIN_FAILURE_EVENT, trace, **others)

raise ArgumentError, 'user_id cannot be nil' if user_id.nil?

Expand All @@ -53,18 +53,13 @@ def self.track_login_failure(trace, user_id:, user_exists:, **others)
#
# This method is experimental and may change in the future.
#
# @param namespace [Symbol] Mandatory. Event namespace. Only :appsec is supported.
# @param event [String] Mandatory. Event code.
# @param trace [TraceOperation] Trace to attach data to.
# @param others [Hash<Symbol, String>] Additional free-form
# event information to attach to the trace. Key must not
# be :track.
def self.track(namespace, event, trace, **others)
if namespace.to_sym != :appsec
raise ArgumentError, "namespace cannot be #{namespace.inspect}, only :appsec is allowed"
end

trace.set_tag("#{namespace}.events.#{event}.track", 'true')
def self.track(event, trace, **others)
trace.set_tag("appsec.events.#{event}.track", 'true')

others.each do |k, v|
raise ArgumentError, 'key cannot be :track' if k.to_sym == :track
Expand Down
6 changes: 3 additions & 3 deletions sig/datadog/kit/appsec/events.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module Datadog
module Kit
module AppSec
module Events
APPSEC_LOGIN_SUCCESS_EVENT: ::String
APPSEC_LOGIN_FAILURE_EVENT: ::String
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

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

def self.track: (::Symbol namespace, ::String | ::Symbol event, Datadog::Tracing::TraceOperation trace, **::Hash[::Symbol, ::String | nil] others) -> void
def self.track: (::String | ::Symbol event, Datadog::Tracing::TraceOperation trace, **::Hash[::Symbol, ::String | nil] others) -> void
end
end
end
Expand Down
11 changes: 2 additions & 9 deletions spec/datadog/kit/appsec/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,16 @@
end

describe '#track' do
it 'rejects unexpected namespaces' do
trace_op.measure('root') do
expect { described_class.track(:foo, 'bar', trace_op) }.to raise_error ArgumentError, /namespace cannot be/
end
expect(meta).to_not include('foo.events.bar.track' => 'true')
end

it 'sets event tracking key on trace' do
trace_op.measure('root') do
described_class.track(:appsec, 'foo', trace_op)
described_class.track('foo', trace_op)
end
expect(meta).to include('appsec.events.foo.track' => 'true')
end

it 'sets other keys on trace' do
trace_op.measure('root') do
described_class.track(:appsec, 'foo', trace_op, bar: 'baz')
described_class.track('foo', trace_op, bar: 'baz')
end
expect(meta).to include('appsec.events.foo.bar' => 'baz')
end
Expand Down

0 comments on commit a2ba515

Please sign in to comment.