Skip to content

Commit 0bf1a6b

Browse files
cocker-cchlindbergAriaXLi
committed
(PUP-11326) Make regsubst() sensitive-aware
This commit updates regsubst() to take in Sensitive type targets. Specifically, regsubst() can now take targets that are either Sensitive String, Sensitive arrays that have a combination of String and/or Sensitive[String], or non-Sensitive arrays that have combination of String and/or Sensitive[String]. Co-authored-by: Henrik Lindberg <[email protected]> Co-authored-by: Aria Li <[email protected]>
1 parent ed4eee5 commit 0bf1a6b

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

lib/puppet/functions/regsubst.rb

+22-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# $i3 = regsubst($ipaddress,'^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$','\\3')
3333
# ```
3434
dispatch :regsubst_string do
35-
param 'Variant[Array[String],String]', :target
35+
param 'Variant[Array[Variant[String,Sensitive[String]]],Sensitive[Array[Variant[String,Sensitive[String]]]],Variant[String,Sensitive[String]]]', :target
3636
param 'String', :pattern
3737
param 'Variant[String,Hash[String,String]]', :replacement
3838
optional_param 'Optional[Pattern[/^[GEIM]*$/]]', :flags
@@ -69,7 +69,7 @@
6969
# $x = regsubst($ipaddress, /([0-9]+)/, '<\\1>', 'G')
7070
# ```
7171
dispatch :regsubst_regexp do
72-
param 'Variant[Array[String],String]', :target
72+
param 'Variant[Array[Variant[String,Sensitive[String]]],Sensitive[Array[Variant[String,Sensitive[String]]]],Variant[String,Sensitive[String]]]', :target
7373
param 'Variant[Regexp,Type[Regexp]]', :pattern
7474
param 'Variant[String,Hash[String,String]]', :replacement
7575
optional_param 'Pattern[/^G?$/]', :flags
@@ -97,7 +97,26 @@ def regsubst_regexp(target, pattern, replacement, flags = nil)
9797
end
9898

9999
def inner_regsubst(target, re, replacement, op)
100-
target.respond_to?(op) ? target.send(op, re, replacement) : target.collect { |e| e.send(op, re, replacement) }
100+
if target.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive) && target.unwrap.is_a?(Array)
101+
# this is a Sensitive Array
102+
target = target.unwrap
103+
target.map do |item|
104+
inner_regsubst(item, re, replacement, op)
105+
end
106+
elsif target.is_a?(Array)
107+
# this is an Array
108+
target.map do |item|
109+
inner_regsubst(item, re, replacement, op)
110+
end
111+
elsif target.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
112+
# this is a Sensitive
113+
target = target.unwrap
114+
target = target.respond_to?(op) ? target.send(op, re, replacement) : target.map { |e| e.send(op, re, replacement) }
115+
Puppet::Pops::Types::PSensitiveType::Sensitive.new(target)
116+
else
117+
# this should be a String
118+
target.respond_to?(op) ? target.send(op, re, replacement) : target.map { |e| e.send(op, re, replacement) }
119+
end
101120
end
102121
private :inner_regsubst
103122
end

spec/unit/functions/regsubst_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,34 @@ def regsubst(*args)
111111
end
112112

113113
end
114+
115+
context 'when using a Target of Type sensitive String' do
116+
it 'should process it' do
117+
result = regsubst(Puppet::Pops::Types::PSensitiveType::Sensitive.new('very secret'), 'very', 'top')
118+
expect(result).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
119+
expect(result.unwrap).to eq("top secret")
120+
end
121+
end
122+
123+
context 'when using a Target of Type Array with mixed String and sensitive String' do
124+
it 'should process it' do
125+
my_array = ['very down', Puppet::Pops::Types::PSensitiveType::Sensitive.new('very secret')]
126+
expect(regsubst(my_array, 'very', 'top')).to be_a(Array)
127+
expect(regsubst(my_array, 'very', 'top')[0]).to eq('top down')
128+
result = regsubst(my_array, 'very', 'top')[1]
129+
expect(result).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
130+
expect(result.unwrap).to eq('top secret')
131+
end
132+
end
133+
134+
context 'when using a Target of Type Sensitive Array with mixed String and sensitive String' do
135+
it 'should process it' do
136+
my_array = Puppet::Pops::Types::PSensitiveType::Sensitive.new(['very down', Puppet::Pops::Types::PSensitiveType::Sensitive.new('very secret')])
137+
expect(regsubst(my_array, 'very', 'top')).to be_a(Array)
138+
expect(regsubst(my_array, 'very', 'top')[0]).to eq('top down')
139+
result = regsubst(my_array, 'very', 'top')[1]
140+
expect(result).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
141+
expect(result.unwrap).to eq('top secret')
142+
end
143+
end
114144
end

0 commit comments

Comments
 (0)