Skip to content

Commit 1b6e5a2

Browse files
committed
Add appsec.auto_user_instrumentation.mode config
1 parent a70cf66 commit 1b6e5a2

File tree

3 files changed

+151
-11
lines changed

3 files changed

+151
-11
lines changed

lib/datadog/appsec/configuration/settings.rb

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ module Settings
1212
DEFAULT_OBFUSCATOR_KEY_REGEX = '(?i)pass|pw(?:or)?d|secret|(?:api|private|public|access)[_-]?key|token|consumer[_-]?(?:id|key|secret)|sign(?:ed|ature)|bearer|authorization|jsessionid|phpsessid|asp\.net[_-]sessionid|sid|jwt'
1313
DEFAULT_OBFUSCATOR_VALUE_REGEX = '(?i)(?:p(?:ass)?w(?:or)?d|pass(?:[_-]?phrase)?|secret(?:[_-]?key)?|(?:(?:api|private|public|access)[_-]?)key(?:[_-]?id)?|(?:(?:auth|access|id|refresh)[_-]?)?token|consumer[_-]?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?|jsessionid|phpsessid|asp\.net(?:[_-]|-)sessionid|sid|jwt)(?:\s*=[^;]|"\s*:\s*"[^"]+")|bearer\s+[a-z0-9\._\-]+|token:[a-z0-9]{13}|gh[opsu]_[0-9a-zA-Z]{36}|ey[I-L][\w=-]+\.ey[I-L][\w=-]+(?:\.[\w.+\/=-]+)?|[\-]{5}BEGIN[a-z\s]+PRIVATE\sKEY[\-]{5}[^\-]+[\-]{5}END[a-z\s]+PRIVATE\sKEY|ssh-rsa\s*[a-z0-9\/\.+]{100,}'
1414
# rubocop:enable Layout/LineLength
15-
APPSEC_VALID_TRACK_USER_EVENTS_MODE = [
16-
'safe',
17-
'extended'
18-
].freeze
19-
APPSEC_VALID_TRACK_USER_EVENTS_ENABLED_VALUES = [
20-
'1',
21-
'true'
22-
].concat(APPSEC_VALID_TRACK_USER_EVENTS_MODE).freeze
15+
16+
DEFAULT_AUTO_USER_INSTRUMENTATION_MODE = 'identification'
17+
AUTO_USER_INSTRUMENTATION_MODES = ['disabled', 'identification', 'anonymization'].freeze
18+
AUTO_USER_INSTRUMENTATION_MODES_ALIASES = {
19+
'ident' => 'identification', 'anon' => 'anonymization',
20+
}.freeze
21+
22+
# NOTE: These two constants are deprecated
23+
APPSEC_VALID_TRACK_USER_EVENTS_MODE = ['safe', 'extended'].freeze
24+
APPSEC_VALID_TRACK_USER_EVENTS_ENABLED_VALUES = ['1', 'true'].concat(
25+
APPSEC_VALID_TRACK_USER_EVENTS_MODE
26+
).freeze
2327

2428
def self.extended(base)
2529
base = base.singleton_class unless base.is_a?(Class)
@@ -149,6 +153,27 @@ def self.add_settings!(base)
149153
end
150154
end
151155

156+
settings :auto_user_instrumentation do
157+
option :mode do |o|
158+
o.type :string
159+
o.env 'DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE'
160+
o.default DEFAULT_AUTO_USER_INSTRUMENTATION_MODE
161+
o.setter do |value|
162+
mode = AUTO_USER_INSTRUMENTATION_MODES_ALIASES.fetch(value, value)
163+
next mode if AUTO_USER_INSTRUMENTATION_MODES.include?(mode)
164+
165+
Datadog.logger.warn(
166+
'The appsec.auto_user_instrumentation.mode value provided is not supported. ' \
167+
"Supported values are: #{AUTO_USER_INSTRUMENTATION_MODES.join(' | ')}. " \
168+
"Using default value: #{DEFAULT_AUTO_USER_INSTRUMENTATION_MODE}."
169+
)
170+
171+
DEFAULT_AUTO_USER_INSTRUMENTATION_MODE
172+
end
173+
end
174+
end
175+
176+
# DEV-3.0: Remove `track_user_events.enabled` and `track_user_events.mode` options
152177
settings :track_user_events do
153178
option :enabled do |o|
154179
o.default true
@@ -161,6 +186,13 @@ def self.add_settings!(base)
161186
APPSEC_VALID_TRACK_USER_EVENTS_ENABLED_VALUES.include?(env_value.strip.downcase)
162187
end
163188
end
189+
o.after_set do
190+
Core.log_deprecation(key: :appsec_track_user_events_enabled) do
191+
'The appsec.track_user_events.enabled setting has been deprecated for removal. ' \
192+
'Please remove it from your Datadog.configure block and use ' \
193+
'appsec.auto_user_instrumentation.mode instead.'
194+
end
195+
end
164196
end
165197

166198
option :mode do |o|
@@ -181,6 +213,13 @@ def self.add_settings!(base)
181213
'safe'
182214
end
183215
end
216+
o.after_set do
217+
Core.log_deprecation(key: :appsec_track_user_events_mode) do
218+
'The appsec.track_user_events.mode setting has been deprecated for removal. ' \
219+
'Please remove it from your Datadog.configure block and use ' \
220+
'appsec.auto_user_instrumentation.mode instead.'
221+
end
222+
end
184223
end
185224
end
186225

sig/datadog/appsec/configuration/settings.rbs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
module Datadog
22
module AppSec
33
module Configuration
4-
# Settings
54
module Settings
65
extend Datadog::Core::Configuration::Base::ClassMethods
76
include Datadog::Core::Configuration::Base::InstanceMethods
87
extend Datadog::Core::Configuration::Options::ClassMethods
98
include Datadog::Core::Configuration::Options::InstanceMethods
109

1110
DEFAULT_OBFUSCATOR_KEY_REGEX: ::String
11+
1212
DEFAULT_OBFUSCATOR_VALUE_REGEX: ::String
13-
APPSEC_VALID_TRACK_USER_EVENTS_MODE: ::Array[String]
14-
APPSEC_VALID_TRACK_USER_EVENTS_ENABLED_VALUES: ::Array[String]
13+
14+
DEFAULT_AUTO_USER_INSTRUMENTATION_MODE: ::String
15+
16+
AUTO_USER_INSTRUMENTATION_MODES: ::Array[::String]
17+
18+
AUTO_USER_INSTRUMENTATION_MODES_ALIASES: ::Hash[::String, ::String]
19+
20+
APPSEC_VALID_TRACK_USER_EVENTS_MODE: ::Array[::String]
21+
22+
APPSEC_VALID_TRACK_USER_EVENTS_ENABLED_VALUES: ::Array[::String]
1523

1624
def self.extended: (untyped base) -> untyped
1725

spec/datadog/appsec/configuration/settings_spec.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ def patcher
469469
end
470470

471471
describe 'track_user_events' do
472+
before { allow(Datadog).to receive(:logger).and_return(spy(Datadog::Core::Logger)) }
473+
472474
describe '#enabled' do
473475
subject(:enabled) { settings.appsec.track_user_events.enabled }
474476

@@ -479,6 +481,17 @@ def patcher
479481
end
480482
end
481483

484+
context 'when deprication message should be emitted' do
485+
let(:track_user_events_enabled) { 'true' }
486+
487+
it 'writes the deprication message' do
488+
expect(Datadog::Core).to receive(:log_deprecation) do |_, &block|
489+
expect(block.call).to match(/setting has been deprecated for removal/)
490+
end
491+
expect(enabled).to eq(true)
492+
end
493+
end
494+
482495
context 'is not defined' do
483496
let(:track_user_events_enabled) { nil }
484497

@@ -562,6 +575,18 @@ def patcher
562575
settings.appsec.track_user_events.mode = track_user_events_mode
563576
end
564577

578+
context 'when deprication message should be emitted' do
579+
let(:track_user_events_mode) { 'extended' }
580+
581+
it 'writes the deprication message' do
582+
expect(Datadog::Core).to receive(:log_deprecation) do |_, &block|
583+
expect(block.call).to match(/setting has been deprecated for removal/)
584+
end
585+
586+
set_appsec_track_user_events_mode
587+
end
588+
end
589+
565590
context 'when given a supported value' do
566591
let(:track_user_events_mode) { 'extended' }
567592

@@ -582,6 +607,74 @@ def patcher
582607
end
583608
end
584609

610+
describe 'auto_user_instrumentation.mode' do
611+
before { allow(Datadog).to receive(:logger).and_return(logger) }
612+
613+
let(:logger) { instance_double(Datadog::Core::Logger) }
614+
615+
context 'when valid value is set' do
616+
before { settings.appsec.auto_user_instrumentation.mode = 'disabled' }
617+
618+
it { expect(settings.appsec.auto_user_instrumentation.mode).to eq('disabled') }
619+
end
620+
621+
context 'when valid short value is set' do
622+
before { settings.appsec.auto_user_instrumentation.mode = 'anon' }
623+
624+
it 'expands the alias value to the long version' do
625+
expect(settings.appsec.auto_user_instrumentation.mode).to eq('anonymization')
626+
end
627+
end
628+
629+
context 'when invalid value is set' do
630+
it 'sets the value to the default and writes a warning message' do
631+
expect(logger).to receive(:warn).with(/value provided is not supported/)
632+
settings.appsec.auto_user_instrumentation.mode = 'unknown'
633+
634+
expect(settings.appsec.auto_user_instrumentation.mode).to eq('identification')
635+
end
636+
end
637+
638+
context 'when valid DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE is set' do
639+
around do |example|
640+
ClimateControl.modify('DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE' => 'disabled') do
641+
example.run
642+
end
643+
end
644+
645+
it { expect(settings.appsec.auto_user_instrumentation.mode).to eq('disabled') }
646+
end
647+
648+
context 'when valid DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE short value is set' do
649+
around do |example|
650+
ClimateControl.modify('DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE' => 'anon') do
651+
example.run
652+
end
653+
end
654+
655+
it 'expands the alias value to the long version' do
656+
expect(settings.appsec.auto_user_instrumentation.mode).to eq('anonymization')
657+
end
658+
end
659+
660+
context 'when invalid DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE is set' do
661+
around do |example|
662+
ClimateControl.modify('DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE' => 'unknown') do
663+
example.run
664+
end
665+
end
666+
667+
it 'sets the value to the default and writes a warning message' do
668+
expect(logger).to receive(:warn).with(/value provided is not supported/)
669+
expect(settings.appsec.auto_user_instrumentation.mode).to eq('identification')
670+
end
671+
end
672+
673+
context 'when no value or env variable is set' do
674+
it { expect(settings.appsec.auto_user_instrumentation.mode).to eq('identification') }
675+
end
676+
end
677+
585678
describe 'block' do
586679
describe 'templates' do
587680
[

0 commit comments

Comments
 (0)