Last active
August 5, 2020 20:26
-
-
Save davide125/fa097320aad7aca98a59e19bde4db64d to your computer and use it in GitHub Desktop.
attribute allowlist repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'fb_helpers.rb' | |
describe FB::Helpers do | |
context 'filter_hash' do | |
it 'returns a passing hash unchanged' do | |
hash = { | |
'fb_sysctl' => { | |
'kernel.core_uses_pid' => 0, | |
}, | |
} | |
filter = 'fb_sysctl' | |
expect(FB::Helpers.filter_hash(hash, filter)).to eq(hash) | |
end | |
it 'filters a failing hash' do | |
hash = { | |
'fb_sysctl' => { | |
'kernel.core_uses_pid' => 0, | |
}, | |
} | |
expect(FB::Helpers.filter_hash(hash, [])).to eq({}) | |
end | |
it 'returns a passing deep hash unchanged' do | |
hash = { | |
'fb_network_scripts' => { | |
'ifup' => { | |
'ethtool' => 'cookie', | |
}, | |
}, | |
} | |
filter = 'fb_network_scripts/ifup/ethtool' | |
expect(FB::Helpers.filter_hash(hash, filter)).to eq(hash) | |
end | |
it 'filters a failing deep hash' do | |
hash = { | |
'fb_network_scripts' => { | |
'ifup' => { | |
'extra_commands' => 'cookie', | |
}, | |
}, | |
} | |
filter = 'fb_network_scripts/ifup/ethtool' | |
expect(FB::Helpers.filter_hash(hash, filter)).to eq({}) | |
end | |
it 'handles compound hashes and filters' do | |
hash = { | |
'fb_sysctl' => { | |
'kernel.core_uses_pid' => 0, | |
}, | |
'fb_network_scripts' => { | |
'ifup' => { | |
'ethtool' => 'cookie', | |
'boo' => 123, | |
}, | |
}, | |
'fb_foo' => { | |
'bar' => 4, | |
}, | |
} | |
filtered_hash = { | |
'fb_sysctl' => { | |
'kernel.core_uses_pid' => 0, | |
}, | |
'fb_network_scripts' => { | |
'ifup' => { | |
'ethtool' => 'cookie', | |
}, | |
}, | |
} | |
filter = ['fb_sysctl', 'fb_network_scripts/ifup/ethtool'] | |
expect(FB::Helpers.filter_hash(hash, filter)).to eq(filtered_hash) | |
end | |
it 'handles hashes with empty values' do | |
hash = { | |
'fb_network_scripts' => { | |
'ifup' => { | |
'extra_commands' => {}, | |
}, | |
}, | |
} | |
filter = 'fb_network_scripts/ifup/ethtool' | |
expect(FB::Helpers.filter_hash(hash, filter)).to eq(hash) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/attribute_allowlist' | |
require 'chef/log' | |
module FB | |
class Helpers | |
def self.filter_hash(hash, filter) | |
if filter.is_a?(String) | |
filter = [filter] | |
elsif !filter.is_a?(Array) | |
fail 'fb_helpers: the filter argument to filter_hash needs to be a ' + | |
"String or an Array (actual: #{filter.class})" | |
end | |
Chef::AttributeAllowlist.filter(hash, filter) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rspec default_spec.rb | |
...[2020-08-05T13:22:42-07:00] WARN: Could not find whitelist attribute fb_network_scripts/ifup/ethtool. | |
F.[2020-08-05T13:22:42-07:00] WARN: Could not find whitelist attribute fb_network_scripts/ifup/ethtool. | |
F | |
Failures: | |
1) FB::Helpers filter_hash filters a failing deep hash | |
Failure/Error: expect(FB::Helpers.filter_hash(hash, filter)).to eq({}) | |
expected: {} | |
got: {"fb_network_scripts"=>{"ifup"=>{}}} | |
(compared using ==) | |
Diff: | |
@@ -1 +1,2 @@ | |
+"fb_network_scripts" => {"ifup"=>{}}, | |
# ./default_spec.rb:62:in `block (3 levels) in <top (required)>' | |
2) FB::Helpers filter_hash handles hashes with empty values | |
Failure/Error: expect(FB::Helpers.filter_hash(hash, filter)).to eq(hash) | |
expected: {"fb_network_scripts"=>{"ifup"=>{"extra_commands"=>{}}}} | |
got: {"fb_network_scripts"=>{"ifup"=>{}}} | |
(compared using ==) | |
Diff: | |
@@ -1,2 +1,2 @@ | |
-"fb_network_scripts" => {"ifup"=>{"extra_commands"=>{}}}, | |
+"fb_network_scripts" => {"ifup"=>{}}, | |
# ./default_spec.rb:104:in `block (3 levels) in <top (required)>' | |
Finished in 0.01842 seconds (files took 0.25777 seconds to load) | |
6 examples, 2 failures | |
Failed examples: | |
rspec ./default_spec.rb:53 # FB::Helpers filter_hash filters a failing deep hash | |
rspec ./default_spec.rb:94 # FB::Helpers filter_hash handles hashes with empty values | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment