Skip to content

Commit

Permalink
Add env_var and deprecated_env_var configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Jun 29, 2023
1 parent 83aa720 commit 56b8128
Show file tree
Hide file tree
Showing 60 changed files with 911 additions and 576 deletions.
52 changes: 24 additions & 28 deletions lib/datadog/appsec/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ def self.add_settings!(base)
base.class_eval do
settings :appsec do
option :enabled do |o|
o.default { env_to_bool('DD_APPSEC_ENABLED', DEFAULT_APPSEC_ENABLED) }
o.setter do |v|
v ? true : false
o.env_var 'DD_APPSEC_ENABLED'
o.default DEFAULT_APPSEC_ENABLED
o.setter do |value|
val_to_bool(value)
end
o.lazy
end

define_method(:instrument) do |integration_name|
Expand All @@ -54,7 +54,8 @@ def self.add_settings!(base)
end

option :ruleset do |o|
o.default { ENV.fetch('DD_APPSEC_RULES', DEFAULT_APPSEC_RULESET) }
o.env_var 'DD_APPSEC_RULES'
o.default { DEFAULT_APPSEC_RULESET }
o.lazy
end

Expand All @@ -69,49 +70,45 @@ def self.add_settings!(base)
end

option :waf_timeout do |o|
o.default { ENV.fetch('DD_APPSEC_WAF_TIMEOUT', DEFAULT_APPSEC_WAF_TIMEOUT) } # us
o.env_var 'DD_APPSEC_WAF_TIMEOUT'
o.default { DEFAULT_APPSEC_WAF_TIMEOUT } # us
o.setter do |v|
Datadog::Core::Utils::Duration.call(v.to_s, base: :us)
end
o.lazy
end

option :waf_debug do |o|
o.default { env_to_bool('DD_APPSEC_WAF_DEBUG', DEFAULT_APPSEC_WAF_DEBUG) }
o.setter do |v|
v ? true : false
o.env_var 'DD_APPSEC_WAF_DEBUG'
o.default DEFAULT_APPSEC_WAF_DEBUG
o.setter do |value|
val_to_bool(value)
end
o.lazy
end

option :trace_rate_limit do |o|
o.default { env_to_int('DD_APPSEC_TRACE_RATE_LIMIT', DEFAULT_APPSEC_TRACE_RATE_LIMIT) } # trace/s
o.lazy
o.env_var 'DD_APPSEC_TRACE_RATE_LIMIT'
o.setter do |value|
val_to_int(value, DEFAULT_APPSEC_TRACE_RATE_LIMIT)
end # trace/s
end

option :obfuscator_key_regex do |o|
o.default { ENV.fetch('DD_APPSEC_OBFUSCATION_PARAMETER_KEY_REGEXP', DEFAULT_OBFUSCATOR_KEY_REGEX) }
o.env_var 'DD_APPSEC_OBFUSCATION_PARAMETER_KEY_REGEXP'
o.default { DEFAULT_OBFUSCATOR_KEY_REGEX }
o.lazy
end

option :obfuscator_value_regex do |o|
o.default do
ENV.fetch(
'DD_APPSEC_OBFUSCATION_PARAMETER_VALUE_REGEXP',
DEFAULT_OBFUSCATOR_VALUE_REGEX
)
end
o.env_var 'DD_APPSEC_OBFUSCATION_PARAMETER_VALUE_REGEXP'
o.default { DEFAULT_OBFUSCATOR_VALUE_REGEX }
o.lazy
end

settings :track_user_events do
option :enabled do |o|
o.default do
ENV.fetch(
'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING',
DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_ENABLED
)
end
o.env_var 'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING'
o.default { DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_ENABLED }
o.setter do |v|
if v
v.to_s != 'disabled'
Expand All @@ -123,9 +120,8 @@ def self.add_settings!(base)
end

option :mode do |o|
o.default do
ENV.fetch('DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING', DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_MODE)
end
o.env_var 'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING'
o.default { DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_MODE }
o.setter do |v|
string_value = v.to_s
if APPSEC_VALID_TRACK_USER_EVENTS_MODE.include?(string_value)
Expand Down
7 changes: 5 additions & 2 deletions lib/datadog/ci/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ def self.add_settings!(base)
base.class_eval do
settings :ci do
option :enabled do |o|
o.default { env_to_bool(CI::Ext::Settings::ENV_MODE_ENABLED, false) }
o.lazy
o.env_var CI::Ext::Settings::ENV_MODE_ENABLED
o.default false
o.setter do |value|
val_to_bool(value)
end
end

# DEV: Alias to Datadog::Tracing::Contrib::Extensions::Configuration::Settings#instrument.
Expand Down
9 changes: 6 additions & 3 deletions lib/datadog/ci/contrib/cucumber/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ module Configuration
# TODO: mark as `@public_api` when GA
class Settings < Datadog::Tracing::Contrib::Configuration::Settings
option :enabled do |o|
o.default { env_to_bool(Ext::ENV_ENABLED, true) }
o.env_var Ext::ENV_ENABLED
o.setter do |value|
val_to_bool(value)
end
o.lazy
end

Expand All @@ -22,8 +25,8 @@ class Settings < Datadog::Tracing::Contrib::Configuration::Settings
end

option :operation_name do |o|
o.default { ENV.key?(Ext::ENV_OPERATION_NAME) ? ENV[Ext::ENV_OPERATION_NAME] : Ext::OPERATION_NAME }
o.lazy
o.env_var Ext::ENV_OPERATION_NAME
o.default Ext::OPERATION_NAME
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/datadog/ci/contrib/rspec/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ module Configuration
# TODO: mark as `@public_api` when GA
class Settings < Datadog::Tracing::Contrib::Configuration::Settings
option :enabled do |o|
o.default { env_to_bool(Ext::ENV_ENABLED, true) }
o.env_var Ext::ENV_ENABLED
o.setter do |value|
val_to_bool(value)
end
o.lazy
end

Expand All @@ -22,7 +25,8 @@ class Settings < Datadog::Tracing::Contrib::Configuration::Settings
end

option :operation_name do |o|
o.default { ENV.key?(Ext::ENV_OPERATION_NAME) ? ENV[Ext::ENV_OPERATION_NAME] : Ext::OPERATION_NAME }
o.env_var Ext::ENV_OPERATION_NAME
o.default ENV[Ext::OPERATION_NAME]
o.lazy
end
end
Expand Down
15 changes: 14 additions & 1 deletion lib/datadog/core/configuration/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get
elsif definition.delegate_to
context_eval(&definition.delegate_to)
else
set(default_value, precedence: Precedence::DEFAULT)
set_value_from_env_or_default
end
end

Expand Down Expand Up @@ -102,6 +102,19 @@ def context_eval(&block)
@context.instance_eval(&block)
end

def set_value_from_env_or_default
if definition.env_var && ENV[definition.env_var]
set(ENV[definition.env_var], precedence: Precedence::PROGRAMMATIC)
elsif definition.deprecated_env_var && ENV[definition.deprecated_env_var]
Datadog::Core.log_deprecation do
"#{definition.deprecated_env_var} environment variable is deprecated, use #{definition.env_var} instead."
end
set(ENV[definition.deprecated_env_var], precedence: Precedence::PROGRAMMATIC)
else
set(default_value, precedence: Precedence::DEFAULT)
end
end

# Used for testing
attr_reader :precedence_set
private :precedence_set
Expand Down
18 changes: 18 additions & 0 deletions lib/datadog/core/configuration/option_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class OptionDefinition

attr_reader \
:default,
:env_var,
:deprecated_env_var,
:delegate_to,
:depends_on,
:lazy,
Expand All @@ -22,6 +24,8 @@ class OptionDefinition

def initialize(name, meta = {}, &block)
@default = meta[:default]
@env_var = meta[:env_var]
@deprecated_env_var = meta[:deprecated_env_var]
@delegate_to = meta[:delegate_to]
@depends_on = meta[:depends_on] || []
@lazy = meta[:lazy] || false
Expand All @@ -44,6 +48,8 @@ class Builder
:helpers

def initialize(name, options = {})
@env_var = nil
@deprecated_env_var = nil
@default = nil
@delegate_to = nil
@depends_on = []
Expand All @@ -66,6 +72,14 @@ def depends_on(*values)
@depends_on = values.flatten
end

def env_var(value)
@env_var = value
end

def deprecated_env_var(value)
@deprecated_env_var = value
end

def default(value = nil, &block)
@default = block || value
end
Expand Down Expand Up @@ -103,6 +117,8 @@ def apply_options!(options = {})
return if options.nil? || options.empty?

default(options[:default]) if options.key?(:default)
env_var(options[:env_var]) if options.key?(:env_var)
deprecated_env_var(options[:deprecated_env_var]) if options.key?(:deprecated_env_var)
delegate_to(&options[:delegate_to]) if options.key?(:delegate_to)
depends_on(*options[:depends_on]) if options.key?(:depends_on)
lazy(options[:lazy]) if options.key?(:lazy)
Expand All @@ -119,6 +135,8 @@ def to_definition
def meta
{
default: @default,
env_var: @env_var,
deprecated_env_var: @deprecated_env_var,
delegate_to: @delegate_to,
depends_on: @depends_on,
lazy: @lazy,
Expand Down
Loading

0 comments on commit 56b8128

Please sign in to comment.