Skip to content

Commit

Permalink
Add checks to allow logging only specific datatypes
Browse files Browse the repository at this point in the history
Currently, it allows logging of objects and the whitelisting does not
apply to the fields of that object.

This change adds data type checks and allows logging of only specific
types so that data that is not whitelisted does
not go through. This means it will be either masked or removed.

MSG-709, IN-299
  • Loading branch information
HallikiM committed Dec 13, 2023
1 parent 48bd258 commit dc0ce70
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/lenjador/preprocessors/strategies/mask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ def process(data, pointer = '')
when Array
process_array(data, pointer)

else
when String, Numeric, Symbol, Date, Time, TrueClass, FalseClass, NilClass
data

else
MASKED_VALUE
end
end

Expand Down
5 changes: 4 additions & 1 deletion lib/lenjador/preprocessors/strategies/prune.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ def process(data, pointer = '')
when Array
process_array(data, pointer)

else
when String, Numeric, Symbol, Date, Time, TrueClass, FalseClass, NilClass
data

else
nil
end
end

Expand Down
213 changes: 210 additions & 3 deletions spec/lenjador/preprocessors/whitelist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,113 @@
end

context 'when boolean present' do
let(:data) { {bool: true} }
let(:pointers) { ['/bool'] }
let(:data) do
{
bool: true,
bool2: true
}
end

it 'masks only if not in whitelist' do
expect(processed_data).to eq({
bool: true,
bool2: '*****'
})
end
end

context 'when nil present' do
let(:pointers) { ['/nil'] }
let(:data) do
{
nil: nil,
nil2: nil
}
end

it 'masks only if not in whitelist' do
expect(processed_data).to eq({
nil: nil,
nil2: '*****'
})
end
end

context 'when numbers present' do
let(:data) do
{
integer: 1,
float: 2.03,
integer2: 3,
float2: 3.34324
}
end
let(:pointers) { ['/integer', '/float'] }

it 'masks only if not in whitelist' do
expect(processed_data).to eq({
integer: 1,
float: 2.03,
integer2: '*****',
float2: '*****'
})
end
end

it 'masks it with asteriks' do
expect(processed_data).to eq(bool: '*****')
context 'when symbol present' do
let(:data) do
{
symbol1: :symbol1,
symbol2: :symbol2
}
end
let(:pointers) { ['/symbol1'] }

it 'masks only if not in whitelist' do
expect(processed_data).to eq({
symbol1: :symbol1,
symbol2: '*****'
})
end
end

context 'when date time present' do
let(:data) do
{
date: Date.new(2023, 12, 12),
time: Time.new(2023, 12, 13),
datetime: DateTime.new(2023, 12, 14)
}
end
let(:pointers) { ['/date', '/time', '/datetime'] }

it 'shows dates' do
expect(processed_data).to eq({
date: Date.new(2023, 12, 12),
time: Time.new(2023, 12, 13),
datetime: DateTime.new(2023, 12, 14)
})
end
end

context 'when unsupported object present' do
let(:pointers) { ['/field'] }
let(:some_class) do
Class.new do
attr_accessor :some_attribute

def initialize
@some_attribute = 'some value'
end
end.new
end
let(:data) { {field: some_class} }

it 'masks the object' do
expect(processed_data).to eq(
field: '*****'
)
end
end

Expand Down Expand Up @@ -312,6 +415,110 @@ def process(pointers, data)
end
end

context 'when boolean present' do
let(:pointers) { ['/bool'] }
let(:data) do
{
bool: true,
bool2: true
}
end

it 'prunes only if not in whitelist' do
expect(processed_data).to eq({
bool: true
})
end
end

context 'when nil present' do
let(:pointers) { ['/nil'] }
let(:data) do
{
nil: nil,
nil2: nil
}
end

it 'prunes only if not in whitelist' do
expect(processed_data).to eq({
nil: nil
})
end
end

context 'when numbers present' do
let(:data) do
{
integer: 1,
float: 2.03,
integer2: 3,
float2: 3.34324
}
end
let(:pointers) { ['/integer', '/float'] }

it 'prunes only if not in whitelist' do
expect(processed_data).to eq({
integer: 1,
float: 2.03
})
end
end

context 'when symbol present' do
let(:data) do
{
symbol1: :symbol1,
symbol2: :symbol2
}
end
let(:pointers) { ['/symbol1'] }

it 'prunes only if not in whitelist' do
expect(processed_data).to eq({
symbol1: :symbol1
})
end
end

context 'when date time present' do
let(:data) do
{
date: Date.new(2023, 12, 12),
time: Time.new(2023, 12, 13),
datetime: DateTime.new(2023, 12, 14)
}
end
let(:pointers) { ['/date', '/time', '/datetime'] }

it 'shows dates' do
expect(processed_data).to eq({
date: Date.new(2023, 12, 12),
time: Time.new(2023, 12, 13),
datetime: DateTime.new(2023, 12, 14)
})
end
end

context 'when unsupported object present' do
let(:pointers) { ['/class'] }
let(:some_class) do
Class.new do
attr_accessor :some_attribute

def initialize
@some_attribute = 'some value'
end
end.new
end
let(:data) { {field: some_class} }

it 'does not return the object' do
expect(processed_data).to eq({class: nil})
end
end

def process(pointers, data)
described_class.new(pointers: pointers, action: :prune).process(data)
end
Expand Down

0 comments on commit dc0ce70

Please sign in to comment.