Skip to content

Commit 8cb1d5c

Browse files
committed
Update devise registration with new settings
1 parent 21aedd4 commit 8cb1d5c

File tree

4 files changed

+49
-83
lines changed

4 files changed

+49
-83
lines changed

lib/datadog/appsec/contrib/devise/event.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ module Devise
88
class Event
99
UUID_REGEX = /^\h{8}-\h{4}-\h{4}-\h{4}-\h{12}$/.freeze
1010

11-
SAFE_MODE = 'safe'
12-
EXTENDED_MODE = 'extended'
13-
1411
attr_reader :user_id
1512

1613
def initialize(resource, mode)
@@ -38,15 +35,15 @@ def extract
3835
@user_id = @resource.id
3936

4037
case @mode
41-
when EXTENDED_MODE
38+
when AppSec::Configuration::Settings::IDENTIFICATION_AUTO_USER_INSTRUMENTATION_MODE
4239
@email = @resource.email
4340
@username = @resource.username
44-
when SAFE_MODE
41+
when AppSec::Configuration::Settings::ANONYMIZATION_AUTO_USER_INSTRUMENTATION_MODE
4542
@user_id = nil unless @user_id && @user_id.to_s =~ UUID_REGEX
4643
else
4744
Datadog.logger.warn(
48-
"Invalid automated user evenst mode: `#{@mode}`. "\
49-
'Supported modes are: `safe` and `extended`.'
45+
"Invalid auto_user_instrumentation.mode: `#{@mode}`. " \
46+
"Supported modes are: #{AppSec::Configuration::Settings::AUTO_USER_INSTRUMENTATION_MODES.join(' | ')}."
5047
)
5148
end
5249
end

lib/datadog/appsec/contrib/devise/patcher/registration_controller_patch.rb

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,23 @@ module Patcher
1414
module RegistrationControllerPatch
1515
def create
1616
return super unless AppSec.enabled?
17-
18-
track_user_events_configuration = Datadog.configuration.appsec.track_user_events
19-
2017
return super unless Configuration.auto_user_instrumentation_enabled?
21-
22-
automated_track_user_events_mode = track_user_events_configuration.mode
23-
24-
appsec_context = Datadog::AppSec.active_context
25-
return super unless appsec_context
18+
return super unless AppSec.active_context
2619

2720
super do |resource|
2821
if resource.persisted?
2922
devise_resource = Resource.new(resource)
30-
31-
event_information = Event.new(devise_resource, automated_track_user_events_mode)
23+
event_information = Event.new(devise_resource, Configuration.auto_user_instrumentation_mode)
3224

3325
if event_information.user_id
34-
Datadog.logger.debug { 'User Signup Event' }
26+
Datadog.logger.debug { 'AppSec: User signup event' }
3527
else
36-
Datadog.logger.warn { 'User Signup Event, but can\'t extract user ID. Tracking empty event' }
28+
Datadog.logger.warn { "AppSec: User signup event, but can't extract user ID. Tracking empty event" }
3729
end
3830

3931
Tracking.track_signup(
40-
appsec_context.trace,
41-
appsec_context.span,
32+
AppSec.active_context.trace,
33+
AppSec.active_context.span,
4234
user_id: event_information.user_id,
4335
**event_information.to_h
4436
)
Lines changed: 38 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,69 @@
1+
# frozen_string_literal: true
2+
13
require 'datadog/appsec/spec_helper'
4+
require 'datadog/appsec/contrib/support/devise_user_mock'
5+
26
require 'datadog/appsec/contrib/devise/resource'
37
require 'datadog/appsec/contrib/devise/event'
48

59
RSpec.describe Datadog::AppSec::Contrib::Devise::Event do
610
let(:event) { described_class.new(resource, mode) }
711
let(:resource) { Datadog::AppSec::Contrib::Devise::Resource.new(object) }
812

9-
let(:object_class) do
10-
Class.new do
11-
attr_reader :id, :uuid, :email, :username
13+
describe '#to_h' do
14+
context 'when resource is nil' do
15+
let(:event) { described_class.new(nil, 'identification') }
1216

13-
def initialize(id: nil, uuid: nil, email: nil, username: nil)
14-
@id = id
15-
@uuid = uuid
16-
@email = email
17-
@username = username
18-
end
17+
it { expect(event.to_h).to eq({}) }
1918
end
20-
end
21-
22-
context 'without resource' do
23-
let(:resource) { nil }
24-
let(:mode) { 'safe' }
25-
26-
it do
27-
expect(event.to_h).to eq({})
28-
end
29-
end
30-
31-
context 'safe mode' do
32-
let(:mode) { 'safe' }
3319

34-
context 'with ID but not UUID' do
35-
let(:object) { object_class.new(id: 1234) }
20+
context 'when mode is invalid' do
21+
let(:event) { described_class.new(resource, 'invalid') }
22+
let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: 1234) }
3623

37-
it do
38-
expect(event.user_id).to be_nil
24+
it 'writes warning log message' do
25+
expect(Datadog.logger).to receive(:warn)
26+
expect(event.to_h).to eq({})
3927
end
4028
end
4129

42-
context 'with ID as UUID' do
43-
let(:uuid) { '123e4567-e89b-12d3-a456-426655440000' }
44-
let(:object) { object_class.new(uuid: uuid) }
45-
46-
it do
47-
expect(event.user_id).to eq(uuid)
30+
context 'when mode is identification and different resource attributes present' do
31+
let(:event) { described_class.new(resource, 'identification') }
32+
let(:resource) do
33+
Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: 1234, email: '[email protected]', username: 'John')
4834
end
35+
36+
it { expect(event.to_h).to eq({ email: '[email protected]', username: 'John' }) }
4937
end
5038
end
5139

52-
context 'extended mode' do
53-
let(:mode) { 'extended' }
40+
describe '#user_id' do
41+
context 'when mode is anonymization and ID is not UUID-like' do
42+
let(:event) { described_class.new(resource, 'anonymization') }
43+
let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: 1234) }
5444

55-
context 'ID' do
56-
context 'with ID but not UUID' do
57-
let(:object) { object_class.new(id: 1234) }
58-
59-
it do
60-
expect(event.user_id).to eq(1234)
61-
end
62-
end
45+
it { expect(event.user_id).to be_nil }
46+
end
6347

64-
context 'with ID as UUID' do
65-
let(:uuid) { '123e4567-e89b-12d3-a456-426655440000' }
66-
let(:object) { object_class.new(uuid: uuid) }
48+
context 'when mode is anonymization and ID is UUID-like' do
49+
let(:event) { described_class.new(resource, 'anonymization') }
50+
let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: '00000000-0000-0000-0000-000000000000') }
6751

68-
it do
69-
expect(event.user_id).to eq(uuid)
70-
end
71-
end
52+
it { expect(event.user_id).to eq('00000000-0000-0000-0000-000000000000') }
7253
end
7354

74-
context 'Email and username' do
75-
let(:object) { object_class.new(id: 1234, email: '[email protected]', username: 'John') }
55+
context 'when mode is identification and ID is not UUID-like' do
56+
let(:event) { described_class.new(resource, 'identification') }
57+
let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: 1234) }
7658

77-
it do
78-
expect(event.to_h).to eq({ email: '[email protected]', username: 'John' })
79-
end
59+
it { expect(event.user_id).to eq(1234) }
8060
end
81-
end
8261

83-
context 'invalid mode' do
84-
let(:object) { object_class.new(id: 1234) }
85-
let(:mode) { 'invalid' }
62+
context 'when mode is identification and ID is UUID-like' do
63+
let(:event) { described_class.new(resource, 'identification') }
64+
let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: '00000000-0000-0000-0000-000000000000') }
8665

87-
it do
88-
expect(Datadog.logger).to receive(:warn)
89-
expect(event.to_h).to eq({})
66+
it { expect(event.user_id).to eq('00000000-0000-0000-0000-000000000000') }
9067
end
9168
end
9269
end

spec/datadog/appsec/contrib/support/devise_user_mock.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module AppSec
55
module Contrib
66
module Support
77
# A basic User model mock sufficient for devise testing
8-
DeviseUserMock = Struct.new(:id, :email, :username, :persisted, keyword_init: true) do
8+
DeviseUserMock = Struct.new(:id, :uuid, :email, :username, :persisted, keyword_init: true) do
99
alias_method :persisted?, :persisted
1010
end
1111
end

0 commit comments

Comments
 (0)