Skip to content

Commit 9e5aa9e

Browse files
Merge branch 'main' into main
2 parents 4a64e78 + 28a60d8 commit 9e5aa9e

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ group :development do
3737
end
3838
group :development, :release_prep do
3939
gem "puppet-strings", '~> 4.0', require: false
40-
gem "puppetlabs_spec_helper", '~> 7.0', require: false
40+
gem "puppetlabs_spec_helper", '~> 8.0', require: false
4141
end
4242
group :system_tests do
4343
gem "puppet_litmus", '~> 1.0', require: false, platforms: [:ruby, :x64_mingw]

lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb

Lines changed: 32 additions & 3 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)
@@ -899,7 +899,7 @@ def format_ciminstance(variable_name, class_name, property_hash)
899899
#
900900
# @param resource [Hash] a hash with the information needed to run `Invoke-DscResource`
901901
# @return [String] A string representing the PowerShell definition of the InvokeParams hash
902-
def invoke_params(resource)
902+
def invoke_params(resource) # rubocop:disable Metrics/MethodLength
903903
params = {
904904
Name: resource[:dscmeta_resource_friendly_name],
905905
Method: resource[:dsc_invoke_method],
@@ -917,6 +917,10 @@ def invoke_params(resource)
917917
params[:ModuleName] = resource[:dscmeta_module_name]
918918
end
919919
resource[:parameters].each do |property_name, property_hash|
920+
# ignore dsc_timeout, since it is only used to specify the powershell command timeout
921+
# and timeout itself is not a parameter to the DSC resource
922+
next if property_name == :dsc_timeout
923+
920924
# strip dsc_ from the beginning of the property name declaration
921925
name = property_name.to_s.gsub(/^dsc_/, '').to_sym
922926
params[:Property][name] = case property_hash[:mof_type]
@@ -925,7 +929,7 @@ def invoke_params(resource)
925929
# the Credential hash interpolable as it will be replaced by a variable reference.
926930
{
927931
'user' => property_hash[:value]['user'],
928-
'password' => escape_quotes(property_hash[:value]['password'].unwrap)
932+
'password' => escape_quotes(unwrap_string(property_hash[:value]['password']))
929933
}
930934
when 'DateTime'
931935
# These have to be handled specifically because they rely on the *Puppet* DateTime,
@@ -1018,6 +1022,31 @@ def unwrap(value)
10181022
end
10191023
end
10201024

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+
10211050
# Escape any nested single quotes in a Sensitive string
10221051
#
10231052
# @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)