Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Sep 15, 2023
1 parent 4732c70 commit 95c40cb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/appsec/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def self.add_settings!(base)

option :sample_rate do |o|
o.type :float
o.env 'DD_API_SECURITY_REQUEST_SAMPLING'
o.env 'DD_API_SECURITY_REQUEST_SAMPLE_RATE'
o.default 0.1
o.setter do |value|
value = 1 if value > 1
Expand Down
8 changes: 5 additions & 3 deletions lib/datadog/appsec/sample_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ module Datadog
module AppSec
# SampleRate basic sample rate
class SampleRate
attr_reader :rate

def initialize(rate)
@rate = rate
end

def sample?
return false if @rate <= 0
return true if @rate >= 1
return false if rate <= 0
return true if rate >= 1

Kernel.rand < @rate
Kernel.rand < rate
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/appsec/processor/rule_merger.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Datadog
type custom_rules = ::Array[::Hash[::String, untyped]]
type processors = ::Hash[::String, untyped]

DEFAULT_PROCESSORS: ::Hash[::String, untyped]

def self.merge: (rules: ::Array[rules], ?data: ::Array[data], ?overrides: ::Array[overrides], ?exclusions: ::Array[exclusions], ?custom_rules: ::Array[custom_rules], ?processors: processors) -> rules

private
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/appsec/sample_rate.rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Datadog
module AppSec
class SampleRate
@rate: Numeric
attr_reader rate: Numeric

def initialize: (Numeric rate) -> void

Expand Down
78 changes: 78 additions & 0 deletions spec/datadog/appsec/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -633,5 +633,83 @@ def patcher
end
end
end

describe 'api_security' do
describe '#enabled' do
subject(:enabled) { settings.appsec.api_security.enabled }

context 'when DD_EXPERIMENTAL_API_SECURITY_ENABLED' do
around do |example|
ClimateControl.modify('DD_EXPERIMENTAL_API_SECURITY_ENABLED' => api_security_enabled) do
example.run
end
end

context 'is not defined' do
let(:api_security_enabled) { nil }

it { is_expected.to eq false }
end

context 'is defined' do
let(:api_security_enabled) { 'true' }

it { is_expected.to eq(true) }
end
end
end

describe '#enabled=' do
subject(:set_api_security_enabled) { settings.appsec.api_security.enabled = api_security_enabled }

[true, false].each do |value|
context "when given #{value}" do
let(:api_security_enabled) { value }

before { set_api_security_enabled }

it { expect(settings.appsec.api_security.enabled).to eq(value) }
end
end
end

describe '#sample_rate' do
subject(:sample_rate) { settings.appsec.api_security.sample_rate.rate }

context 'when DD_API_SECURITY_REQUEST_SAMPLE_RATE' do
around do |example|
ClimateControl.modify('DD_API_SECURITY_REQUEST_SAMPLE_RATE' => api_security_sample_rate) do
example.run
end
end

context 'is not defined' do
let(:api_security_sample_rate) { nil }

it { is_expected.to eq 0.1 }
end

context 'is defined' do
let(:api_security_sample_rate) { '0.3' }

it { is_expected.to eq 0.3 }
end
end
end

describe '#sample_rate=' do
subject(:set_api_security_sample_rate) do
settings.appsec.api_security.sample_rate = api_security_sample_rate
end

context 'when given a value higher than 1.0' do
let(:api_security_sample_rate) { 1.2 }

before { set_api_security_sample_rate }

it { expect(settings.appsec.api_security.sample_rate.rate).to eq 1.0 }
end
end
end
end
end

0 comments on commit 95c40cb

Please sign in to comment.