Skip to content

Commit 845eaf1

Browse files
committed
Handle string credentials
1 parent 900a944 commit 845eaf1

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ def prepare_credentials(resource)
777777
variable_name = random_variable_name
778778
credential_hash = {
779779
'user' => property_hash[:value]['user'],
780-
'password' => escape_quotes(property_hash[:value]['password'].unwrap)
780+
'password' => escape_quotes(unwrap_string(property_hash[:value]['password']))
781781
}
782782
credentials_block << format_pscredential(variable_name, credential_hash)
783783
instantiated_variables.merge!(variable_name => credential_hash)
@@ -908,7 +908,7 @@ def invoke_params(resource)
908908
# the Credential hash interpolable as it will be replaced by a variable reference.
909909
{
910910
'user' => property_hash[:value]['user'],
911-
'password' => escape_quotes(property_hash[:value]['password'].unwrap)
911+
'password' => escape_quotes(unwrap_string(property_hash[:value]['password']))
912912
}
913913
when 'DateTime'
914914
# These have to be handled specifically because they rely on the *Puppet* DateTime,
@@ -1001,6 +1001,31 @@ def unwrap(value)
10011001
end
10021002
end
10031003

1004+
# Unwrap sensitive strings and handle string
1005+
#
1006+
# @param value [Object] The object to unwrap sensitive data inside of
1007+
# @return [Object] The object with any sensitive strings unwrapped
1008+
def unwrap_string(value)
1009+
case value
1010+
when Puppet::Pops::Types::PSensitiveType::Sensitive
1011+
value.unwrap
1012+
when Hash
1013+
unwrapped = {}
1014+
value.each do |k, v|
1015+
unwrapped[k] = unwrap_string(v)
1016+
end
1017+
unwrapped
1018+
when Array
1019+
unwrapped = []
1020+
value.each do |v|
1021+
unwrapped << unwrap_string(v)
1022+
end
1023+
unwrapped
1024+
else
1025+
value
1026+
end
1027+
end
1028+
10041029
# Escape any nested single quotes in a Sensitive string
10051030
#
10061031
# @param text [String] the text to escape

spec/unit/puppet/provider/dsc_base_provider/dsc_base_provider_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@
439439
mof_is_embedded: false
440440
},
441441
dsc_psdscrunascredential: {
442-
type: 'Optional[Struct[{ user => String[1], password => Sensitive[String[1]] }]]',
442+
type: 'Optional[Struct[{ user => String[1], password => Variant[String[1], Sensitive[String[1]]] }]]',
443443
behaviour: :parameter,
444444
mandatory_for_get: false,
445445
mandatory_for_set: false,
@@ -886,7 +886,7 @@
886886
mof_is_embedded: false
887887
},
888888
dsc_psdscrunascredential: {
889-
type: 'Optional[Struct[{ user => String[1], password => Sensitive[String[1]] }]]',
889+
type: 'Optional[Struct[{ user => String[1], password => Variant[String[1], Sensitive[String[1]]] }]]',
890890
desc: 'The Credential to run DSC under',
891891
behaviour: :parameter,
892892
mandatory_for_get: false,
@@ -1552,6 +1552,8 @@
15521552
let(:test_resource) { base_resource.merge(additional_parameters) }
15531553

15541554
before do
1555+
allow(Puppet::Pops::Types::PSensitiveType::Sensitive).to receive(:===).with(foo_password).and_return(true)
1556+
allow(Puppet::Pops::Types::PSensitiveType::Sensitive).to receive(:===).with(bar_password).and_return(true)
15551557
allow(foo_password).to receive(:unwrap).and_return('foo')
15561558
allow(bar_password).to receive(:unwrap).and_return('bar')
15571559
end
@@ -1791,6 +1793,11 @@
17911793
"$InvokeParams = @{Name = 'Foo'; Method = 'Get'; Property = @{credential = $SomeCredential}; ModuleName = 'PuppetDsc'}"
17921794
end
17931795

1796+
before do
1797+
allow(Puppet::Pops::Types::PSensitiveType::Sensitive).to receive(:===).with(password).and_return(true)
1798+
allow(password).to receive(:unwrap).and_return('bar')
1799+
end
1800+
17941801
it 'unwraps the credential hash and interpolates the appropriate variable' do
17951802
expect(password).to receive(:unwrap).and_return('FooPassword')
17961803
expect(provider).to receive(:interpolate_variables).with(formatted_param_hash).and_return(variable_interpolated_param_hash)

0 commit comments

Comments
 (0)