logasm 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 440665fe491c115f43c411822de433ead259e193
4
- data.tar.gz: 06568509dacd084a9607ac545b3cfd03447947db
3
+ metadata.gz: 84be42eae2bfa1a843ae47eddbc3108c4737af01
4
+ data.tar.gz: 98b03a785863f5d3b5de1f12034c92b021f405c6
5
5
  SHA512:
6
- metadata.gz: 380b1118c988bbf141b9c03a63ca3c68d33f1f8873709bd92d5e52188dfb3adf980edf7d040eca62c2bdbc66fb4a142e0906e2272aa278cceb4109d6c88e52a2
7
- data.tar.gz: b93a280171c1123d5559ac630a5db52710cd429a7f5bbaddbb73885f7aa75d287d21b117ca3a8fe75b6a2399667c05bcb1631ce889795f2b493036f664742992
6
+ metadata.gz: 9070dfcd0b7cc887010692cd9bcee48d45410299681e5a6ca5ab8a967a55f5f5c41700b7e0b8880f7d434c0768ff12892faaa4db7f10df494e1c0cc4e5538faa
7
+ data.tar.gz: b62a0781c1be451843cb9ae2f6f668540555135f0159f808468d9237ad136490c085307a662c08c9e0b7d6af6c2d5eb9058ea9a477d20dde28cc9d9dd5fed11a
data/README.md CHANGED
@@ -108,13 +108,14 @@ Received request {"info":{"phone":"************"}}
108
108
 
109
109
  Masks all the fields except those whitelisted in the configuration using [JSON Pointer](https://tools.ietf.org/html/rfc6901).
110
110
  Only simple values(`string`, `number`, `boolean`) can be whitelisted.
111
+ Whitelisting array elements can be done using wildcard symbol `~`.
111
112
 
112
113
  #### Configuration
113
114
 
114
115
  ```yaml
115
116
  preprocessors:
116
117
  whitelist:
117
- pointers: ['/info/phone']
118
+ pointers: ['/info/phone', '/addresses/~/host']
118
119
  ```
119
120
 
120
121
  #### Usage
@@ -122,7 +123,7 @@ preprocessors:
122
123
  ```ruby
123
124
  logger = Logasm.build(application_name, logger_config, preprocessors)
124
125
 
125
- input = {password: 'password', info: {phone: '+12055555555'}}
126
+ input = {password: 'password', info: {phone: '+12055555555'}, addresses: [{host: 'example.com', path: 'info'}]}
126
127
 
127
128
  logger.debug("Received request", input)
128
129
  ```
@@ -130,5 +131,5 @@ logger.debug("Received request", input)
130
131
  Logger output:
131
132
 
132
133
  ```
133
- Received request {password: "********", "info":{"phone":"+12055555555"}}
134
+ Received request {password: "********", "info": {"phone": "+12055555555"}, "addresses": [{"host": "example.com","path": "****"}]}
134
135
  ```
@@ -4,16 +4,24 @@ class Logasm
4
4
 
5
5
  DEFAULT_WHITELIST = %w(/id /message /correlation_id /queue)
6
6
  MASK_SYMBOL = '*'
7
+ WILDCARD = '~'
7
8
 
8
9
  class InvalidPointerFormatException < Exception
9
10
  end
10
11
 
11
12
  def initialize(config = {})
12
13
  pointers = (config[:pointers] || []) + DEFAULT_WHITELIST
13
- @fields_to_include = pointers.inject({}) do |mem, pointer|
14
- validate_pointer(pointer)
15
- mem.merge(decode(pointer) => true)
14
+ decoded_pointers = pointers
15
+ .each(&method(:validate_pointer))
16
+ .map(&method(:decode))
17
+ @fields_to_include = decoded_pointers.inject({}) do |mem, pointer|
18
+ mem.merge(pointer => true)
16
19
  end
20
+ @wildcards = decoded_pointers
21
+ .select(&method(:has_wildcard?))
22
+ .inject({}) do |mem, pointer|
23
+ mem.merge(get_wildcard_roots_of(pointer))
24
+ end
17
25
  end
18
26
 
19
27
  def process(data)
@@ -22,6 +30,34 @@ class Logasm
22
30
 
23
31
  private
24
32
 
33
+
34
+ def has_wildcard?(pointer)
35
+ pointer.include?("/#{WILDCARD}/") || pointer.end_with?("/#{WILDCARD}")
36
+ end
37
+
38
+ # From a pointer with wildcards builds a hash with roots that contains a wildcard. Hash is used to easily match
39
+ # find if hash element matches the pointer while processing the log.
40
+ #
41
+ # Example:
42
+ #
43
+ # Input:
44
+ # "/array/~/nested_array/~/fields"
45
+ #
46
+ # Output:
47
+ # {
48
+ # "/array/~/nested_array/~" => true,
49
+ # "/array/~" => true
50
+ # }
51
+ #
52
+ def get_wildcard_roots_of(pointer)
53
+ if (index = pointer.rindex("/#{WILDCARD}/"))
54
+ wildcard_root = pointer.slice(0, index + 2)
55
+ get_wildcard_roots_of(wildcard_root).merge(wildcard_root => true)
56
+ else
57
+ {pointer => true}
58
+ end
59
+ end
60
+
25
61
  def validate_pointer(pointer)
26
62
  if pointer.slice(-1) == '/'
27
63
  raise InvalidPointerFormatException.new('Pointer should not contain trailing slash')
@@ -30,8 +66,8 @@ class Logasm
30
66
 
31
67
  def decode(pointer)
32
68
  pointer
33
- .gsub('~0', '~')
34
69
  .gsub('~1', '/')
70
+ .gsub('~0', '~')
35
71
  end
36
72
 
37
73
  def process_data(parent_pointer, data)
@@ -57,8 +93,14 @@ class Logasm
57
93
  end
58
94
 
59
95
  def process_array(parent_pointer, array)
96
+ create_child_pointer =
97
+ if @wildcards["#{parent_pointer}/~"]
98
+ lambda { |_| "#{parent_pointer}/~" }
99
+ else
100
+ lambda { |index| "#{parent_pointer}/#{index}" }
101
+ end
60
102
  array.each_with_index.inject([]) do |mem, (value, index)|
61
- pointer = "#{parent_pointer}/#{index}"
103
+ pointer = create_child_pointer.call(index)
62
104
  processed_value = process_data(pointer, value)
63
105
  mem + [processed_value]
64
106
  end
data/logasm.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "logasm"
7
- gem.version = '0.3.0'
7
+ gem.version = '0.4.0'
8
8
  gem.authors = ["Salemove"]
9
9
  gem.email = ["support@salemove.com"]
10
10
  gem.description = %q{It's logasmic}
@@ -74,6 +74,30 @@ describe Logasm::Preprocessors::Whitelist do
74
74
  end
75
75
  end
76
76
 
77
+ context 'with whitelisted array elements field with wildcard' do
78
+ let(:data) {{
79
+ array: [{field: 'data1', secret: 'secret1'}, {field: 'data2', secret: 'secret2'}]
80
+ }}
81
+ let(:pointers) { ['/array/~/field'] }
82
+
83
+ it 'includes array elements field' do
84
+ expect(processed_data).to include(
85
+ array: [{field: 'data1', secret: '*******'}, {field: 'data2', secret: '*******'}]
86
+ )
87
+ end
88
+ end
89
+
90
+ context 'with whitelisted string array elements with wildcard' do
91
+ let(:data) {{
92
+ array: ['secret', 'secret']
93
+ }}
94
+ let(:pointers) { ['/array/~'] }
95
+
96
+ it 'includes array elements' do
97
+ expect(processed_data).to include(array: ['secret', 'secret'])
98
+ end
99
+ end
100
+
77
101
  context 'with whitelisted array element' do
78
102
  let(:pointers) { ['/array/0'] }
79
103
 
@@ -149,4 +173,15 @@ describe Logasm::Preprocessors::Whitelist do
149
173
  expect(processed_data).to include('field_with_~'=> 'secret')
150
174
  end
151
175
  end
176
+
177
+ context 'when field has tilde and 1' do
178
+ let(:data) {{
179
+ 'field_with_~1' => 'secret'
180
+ }}
181
+ let(:pointers) { ['/field_with_~01'] }
182
+
183
+ it 'includes field' do
184
+ expect(processed_data).to include('field_with_~1'=> 'secret')
185
+ end
186
+ end
152
187
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salemove
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-18 00:00:00.000000000 Z
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inflecto
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.4.8
127
+ rubygems_version: 2.2.2
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: What description said