Skip to content

Commit

Permalink
Merge pull request #18631 from kbrock/recursive_passwords
Browse files Browse the repository at this point in the history
fix_auth now handles recursive settings
  • Loading branch information
carbonin authored Apr 5, 2019
2 parents 4d32379 + 2eabc44 commit 365c1a0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/vmdb/settings_walker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ def walk(settings, path = [], &block)
key_path = path.dup << key

yield key, value, key_path, settings
next if key == settings || value == settings

case value
when settings.class
walk(value, key_path, &block)
when Array
value.each_with_index do |v, i|
walk(v, key_path.dup << i, &block) if v.kind_of?(settings.class)
walk(v, key_path.dup << i, &block) if v.kind_of?(settings.class) && v != settings
end
end
end
Expand Down
77 changes: 51 additions & 26 deletions spec/lib/vmdb/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,61 @@
end
end

it ".walk" do
stub_settings(:a => {:b => 'c'}, :d => {:e => {:f => 'g'}}, :i => [{:j => 'k'}, {:l => 'm'}])

walked = []
described_class.walk do |key, value, path, owning|
expect(owning).to be_kind_of(Config::Options)

if %i(a d e).include?(key)
expect(value).to be_kind_of(Config::Options)
value = value.to_hash
elsif %i(i).include?(key)
expect(value).to be_kind_of(Array)
value.each { |v| expect(v).to be_kind_of(Config::Options) }
value = value.collect(&:to_hash)
describe ".walk" do
it "traverses tree properly" do
stub_settings(:a => {:b => 'c'}, :d => {:e => {:f => 'g'}}, :i => [{:j => 'k'}, {:l => 'm'}])

walked = []
described_class.walk do |key, value, path, owning|
expect(owning).to be_kind_of(Config::Options)

if %i(a d e).include?(key)
expect(value).to be_kind_of(Config::Options)
value = value.to_hash
elsif %i(i).include?(key)
expect(value).to be_kind_of(Array)
value.each { |v| expect(v).to be_kind_of(Config::Options) }
value = value.collect(&:to_hash)
end

walked << [key, value, path]
end

walked << [key, value, path]
expect(walked).to eq [
#key value path
[:a, {:b => 'c'}, [:a]],
[:b, 'c', [:a, :b]],
[:d, {:e => {:f => 'g'}}, [:d]],
[:e, {:f => 'g'}, [:d, :e]],
[:f, 'g', [:d, :e, :f]],
[:i, [{:j => 'k'}, {:l => 'm'}], [:i]],
[:j, 'k', [:i, 0, :j]],
[:l, 'm', [:i, 1, :l]],
]
end

it "handles basic recursion (value == settings)" do
y = YAML.load(<<~CONFIG)
---
:hash:
- &1
A: *1
CONFIG

expect { described_class.walk(y) { |_k, _v, _p, _o| } }.not_to raise_error
end

it "handles hash recursion (embedded array == settings)" do
s = {:a => []}
s[:a] << s
expect { described_class.walk(s) { |_k, _v, _p, _o| } }.not_to raise_error
end

expect(walked).to eq [
#key value path
[:a, {:b => 'c'}, [:a]],
[:b, 'c', [:a, :b]],
[:d, {:e => {:f => 'g'}}, [:d]],
[:e, {:f => 'g'}, [:d, :e]],
[:f, 'g', [:d, :e, :f]],
[:i, [{:j => 'k'}, {:l => 'm'}], [:i]],
[:j, 'k', [:i, 0, :j]],
[:l, 'm', [:i, 1, :l]],
]
it "handles array recursion (key == settings)" do
s = []
s << s
expect { described_class.walk(s) { |_k, _v, _p, _o| } }.not_to raise_error
end
end

describe ".save!" do
Expand Down

0 comments on commit 365c1a0

Please sign in to comment.