Skip to content

Instantly share code, notes, and snippets.

@davide125
Last active August 5, 2020 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davide125/fa097320aad7aca98a59e19bde4db64d to your computer and use it in GitHub Desktop.
Save davide125/fa097320aad7aca98a59e19bde4db64d to your computer and use it in GitHub Desktop.
attribute allowlist repro
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
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
$ 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