Skip to content

Commit 98cb1b3

Browse files
committed
(maint) deep symbolize all keys when loading credentials
1 parent 4122f08 commit 98cb1b3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/puppet/resource_api/transport/wrapper.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(name, url_or_config)
1111
url = URI.parse(url_or_config)
1212
raise "Unexpected url '#{url_or_config}' found. Only file:/// URLs for configuration supported at the moment." unless url.scheme == 'file'
1313
raise "Trying to load config from '#{url.path}, but file does not exist." if url && !File.exist?(url.path)
14-
config = (Hocon.load(url.path, syntax: Hocon::ConfigSyntax::HOCON) || {}).map { |k, v| [k.to_sym, v] }.to_h
14+
config = self.class.deep_symbolize(Hocon.load(url.path, syntax: Hocon::ConfigSyntax::HOCON) || {})
1515
else
1616
config = url_or_config
1717
end
@@ -38,4 +38,11 @@ def method_missing(method_name, *args, &block)
3838
super
3939
end
4040
end
41+
42+
# From https://stackoverflow.com/a/11788082/4918
43+
def self.deep_symbolize(obj)
44+
return obj.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = deep_symbolize(v); } if obj.is_a? Hash
45+
return obj.each_with_object([]) { |v, memo| memo << deep_symbolize(v); } if obj.is_a? Array
46+
obj
47+
end
4148
end

spec/puppet/resource_api/transport/wrapper_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
it 'will not throw an error' do
1212
allow(File).to receive(:exist?).and_return(true)
1313
expect(Puppet::ResourceApi::Transport).to receive(:connect)
14-
expect(Hocon).to receive(:load)
14+
expect(Hocon).to receive(:load).with('/etc/credentials', any_args).and_return({'foo' => ['a', 'b'], "bar" => 2})
1515
expect { described_class.new('wibble', url) }.not_to raise_error
1616
end
1717
end

0 commit comments

Comments
 (0)