Skip to content

Commit

Permalink
(FM-7726) deep symbolize all keys when loading credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS committed Feb 1, 2019
1 parent 4122f08 commit 3a64276
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/puppet/resource_api/transport/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(name, url_or_config)
url = URI.parse(url_or_config)
raise "Unexpected url '#{url_or_config}' found. Only file:/// URLs for configuration supported at the moment." unless url.scheme == 'file'
raise "Trying to load config from '#{url.path}, but file does not exist." if url && !File.exist?(url.path)
config = (Hocon.load(url.path, syntax: Hocon::ConfigSyntax::HOCON) || {}).map { |k, v| [k.to_sym, v] }.to_h
config = self.class.deep_symbolize(Hocon.load(url.path, syntax: Hocon::ConfigSyntax::HOCON) || {})
else
config = url_or_config
end
Expand All @@ -38,4 +38,11 @@ def method_missing(method_name, *args, &block)
super
end
end

# From https://stackoverflow.com/a/11788082/4918
def self.deep_symbolize(obj)
return obj.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = deep_symbolize(v); } if obj.is_a? Hash
return obj.each_with_object([]) { |v, memo| memo << deep_symbolize(v); } if obj.is_a? Array
obj
end
end
2 changes: 1 addition & 1 deletion spec/puppet/resource_api/transport/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
it 'will not throw an error' do
allow(File).to receive(:exist?).and_return(true)
expect(Puppet::ResourceApi::Transport).to receive(:connect)
expect(Hocon).to receive(:load)
expect(Hocon).to receive(:load).with('/etc/credentials', any_args).and_return('foo' => %w[a b], 'bar' => 2)
expect { described_class.new('wibble', url) }.not_to raise_error
end
end
Expand Down

0 comments on commit 3a64276

Please sign in to comment.