Skip to content

Commit 28a60d8

Browse files
Merge pull request #369 from Clebam/Handle-string-credentials
Handle string credentials
2 parents 3c4af88 + f486ec2 commit 28a60d8

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
@@ -794,7 +794,7 @@ def prepare_credentials(resource)
794794
variable_name = random_variable_name
795795
credential_hash = {
796796
'user' => property_hash[:value]['user'],
797-
'password' => escape_quotes(property_hash[:value]['password'].unwrap)
797+
'password' => escape_quotes(unwrap_string(property_hash[:value]['password']))
798798
}
799799
credentials_block << format_pscredential(variable_name, credential_hash)
800800
instantiated_variables.merge!(variable_name => credential_hash)
@@ -929,7 +929,7 @@ def invoke_params(resource) # rubocop:disable Metrics/MethodLength
929929
# the Credential hash interpolable as it will be replaced by a variable reference.
930930
{
931931
'user' => property_hash[:value]['user'],
932-
'password' => escape_quotes(property_hash[:value]['password'].unwrap)
932+
'password' => escape_quotes(unwrap_string(property_hash[:value]['password']))
933933
}
934934
when 'DateTime'
935935
# These have to be handled specifically because they rely on the *Puppet* DateTime,
@@ -1022,6 +1022,31 @@ def unwrap(value)
10221022
end
10231023
end
10241024

1025+
# Unwrap sensitive strings and handle string
1026+
#
1027+
# @param value [Object] The object to unwrap sensitive data inside of
1028+
# @return [Object] The object with any sensitive strings unwrapped
1029+
def unwrap_string(value)
1030+
case value
1031+
when Puppet::Pops::Types::PSensitiveType::Sensitive
1032+
value.unwrap
1033+
when Hash
1034+
unwrapped = {}
1035+
value.each do |k, v|
1036+
unwrapped[k] = unwrap_string(v)
1037+
end
1038+
unwrapped
1039+
when Array
1040+
unwrapped = []
1041+
value.each do |v|
1042+
unwrapped << unwrap_string(v)
1043+
end
1044+
unwrapped
1045+
else
1046+
value
1047+
end
1048+
end
1049+
10251050
# Escape any nested single quotes in a Sensitive string
10261051
#
10271052
# @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,
@@ -906,7 +906,7 @@
906906
mof_is_embedded: false
907907
},
908908
dsc_psdscrunascredential: {
909-
type: 'Optional[Struct[{ user => String[1], password => Sensitive[String[1]] }]]',
909+
type: 'Optional[Struct[{ user => String[1], password => Variant[String[1], Sensitive[String[1]]] }]]',
910910
desc: 'The Credential to run DSC under',
911911
behaviour: :parameter,
912912
mandatory_for_get: false,
@@ -1572,6 +1572,8 @@
15721572
let(:test_resource) { base_resource.merge(additional_parameters) }
15731573

15741574
before do
1575+
allow(Puppet::Pops::Types::PSensitiveType::Sensitive).to receive(:===).with(foo_password).and_return(true)
1576+
allow(Puppet::Pops::Types::PSensitiveType::Sensitive).to receive(:===).with(bar_password).and_return(true)
15751577
allow(foo_password).to receive(:unwrap).and_return('foo')
15761578
allow(bar_password).to receive(:unwrap).and_return('bar')
15771579
end
@@ -1811,6 +1813,11 @@
18111813
"$InvokeParams = @{Name = 'Foo'; Method = 'Get'; Property = @{credential = $SomeCredential}; ModuleName = 'PuppetDsc'}"
18121814
end
18131815

1816+
before do
1817+
allow(Puppet::Pops::Types::PSensitiveType::Sensitive).to receive(:===).with(password).and_return(true)
1818+
allow(password).to receive(:unwrap).and_return('bar')
1819+
end
1820+
18141821
it 'unwraps the credential hash and interpolates the appropriate variable' do
18151822
expect(password).to receive(:unwrap).and_return('FooPassword')
18161823
expect(provider).to receive(:interpolate_variables).with(formatted_param_hash).and_return(variable_interpolated_param_hash)

0 commit comments

Comments
 (0)