Skip to content

Commit cdf9df8

Browse files
committed
Merge branch 'stable'
2 parents a051665 + b1ac4d5 commit cdf9df8

File tree

8 files changed

+96
-54
lines changed

8 files changed

+96
-54
lines changed

lib/puppet/parameter.rb

+20
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ def self.proxymethods(*values)
295295
#
296296
attr_accessor :parent
297297

298+
# @!attribute [rw] sensitive
299+
# @return [true, false] If this parameter has been tagged as sensitive.
300+
attr_accessor :sensitive
301+
298302
# Returns a string representation of the resource's containment path in
299303
# the catalog.
300304
# @return [String]
@@ -522,6 +526,22 @@ def to_s
522526
name.to_s
523527
end
524528

529+
# Formats the given string and conditionally redacts the provided interpolation variables, depending on if
530+
# this property is sensitive.
531+
#
532+
# @note Because the default implementation of Puppet::Property#is_to_s returns the current value as-is, it
533+
# doesn't necessarily return a string. For the sake of sanity we just cast everything to a string for
534+
# interpolation so we don't introduce issues with unexpected property values.
535+
#
536+
# @see String#format
537+
# @param fmt [String] The format string to interpolate.
538+
# @param args [Array<String>] One or more strings to conditionally redact and interpolate into the format string.
539+
#
540+
# @return [String]
541+
def format(fmt, *args)
542+
fmt % args.map { |arg| @sensitive ? "[redacted]" : arg.to_s }
543+
end
544+
525545
# Produces a String with the value formatted for display to a human.
526546
# When the parameter value is a:
527547
#

lib/puppet/property.rb

-20
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ class Puppet::Property < Puppet::Parameter
5555
#
5656
attr_writer :noop
5757

58-
# @!attribute [rw] sensitive
59-
# @return [true, false] If this property has been tagged as sensitive.
60-
attr_accessor :sensitive
61-
6258
class << self
6359
# @todo Figure out what this is used for. Can not find any logic in the puppet code base that
6460
# reads or writes this attribute.
@@ -221,22 +217,6 @@ def change_to_s(current_value, newvalue)
221217
end
222218
end
223219

224-
# Formats the given string and conditionally redacts the provided interpolation variables, depending on if
225-
# this property is sensitive.
226-
#
227-
# @note Because the default implementation of #is_to_s returns the current value as-is, it doesn't necessarily
228-
# return a string. For the sake of sanity we just cast everything to a string for interpolation so we don't
229-
# introduce issues with unexpected property values.
230-
#
231-
# @see String#format
232-
# @param fmt [String] The format string to interpolate.
233-
# @param args [Array<String>] One or more strings to conditionally redact and interpolate into the format string.
234-
#
235-
# @return [String]
236-
def format(fmt, *args)
237-
fmt % args.map { |arg| @sensitive ? "[redacted]" : arg.to_s }
238-
end
239-
240220
# Produces the name of the event to use to describe a change of this property's value.
241221
# The produced event name is either the event name configured for this property, or a generic
242222
# event based on the name of the property with suffix `_changed`, or if the property is

lib/puppet/type/file.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -906,9 +906,10 @@ def set_sensitive_parameters(sensitive_parameters)
906906

907907
# The source parameter isn't actually a property but works by injecting information into the
908908
# content property. In order to preserve the intended sensitive context we need to mark content
909-
# as sensitive instead.
909+
# as sensitive as well.
910910
if sensitive_parameters.include?(:source)
911911
sensitive_parameters.delete(:source)
912+
parameter(:source).sensitive = true
912913
# The `source` parameter will generate the `content` property when the resource state is retrieved
913914
# but that's long after we've set the sensitive context. Force the early creation of the `content`
914915
# attribute so we can mark it as sensitive.

lib/puppet/type/file/data_sync.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,22 @@ def checksum_insync?(param, is, has_contents, &block)
3434

3535
return true if ! resource.replace?
3636

37-
result = yield(is)
37+
is_insync = yield(is)
3838

39-
if !result && Puppet[:show_diff] && resource.show_diff?
40-
write_temporarily(param) do |path|
41-
send resource[:loglevel], "\n" + diff(resource[:path], path)
39+
if show_diff?(!is_insync)
40+
if param.sensitive
41+
send resource[:loglevel], "[diff redacted]"
42+
else
43+
write_temporarily(param) do |path|
44+
send resource[:loglevel], "\n" + diff(resource[:path], path)
45+
end
4246
end
4347
end
44-
result
48+
is_insync
49+
end
50+
51+
def show_diff?(has_changes)
52+
has_changes && Puppet[:show_diff] && resource.show_diff?
4553
end
4654

4755
def date_matches?(checksum_type, current, desired)

spec/unit/parameter_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,15 @@
190190
)).to eq("{'1' => 'foo', 'bar' => ['2', '3', '4'], 'baz' => {'quux' => 'two', 'qux' => '1'}}")
191191
end
192192
end
193+
194+
describe 'formatting messages' do
195+
it "formats messages as-is when the parameter is not sensitive" do
196+
expect(@parameter.format("hello %s", "world")).to eq("hello world")
197+
end
198+
199+
it "formats messages with redacted values when the parameter is not sensitive" do
200+
@parameter.sensitive = true
201+
expect(@parameter.format("hello %s", "world")).to eq("hello [redacted]")
202+
end
203+
end
193204
end

spec/unit/property_spec.rb

-11
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ class << self
5050
expect(property.to_s).to eq(property.name.to_s)
5151
end
5252

53-
describe 'formatting messages' do
54-
it "formats messages as-is when the property is not sensitive" do
55-
expect(property.format("hello %s", "world")).to eq("hello world")
56-
end
57-
58-
it "formats messages with redacted values when the property is not sensitive" do
59-
property.sensitive = true
60-
expect(property.format("hello %s", "world")).to eq("hello [redacted]")
61-
end
62-
end
63-
6453
describe "when returning the default event name" do
6554
it "should use the current 'should' value to pick the event name" do
6655
property.expects(:should).returns "myvalue"

spec/unit/type/file/content_spec.rb

+47-15
Original file line numberDiff line numberDiff line change
@@ -184,25 +184,30 @@
184184
expect(content.respond_to?("diff")).to eq(false)
185185
end
186186

187-
[true, false].product([true, false]).each do |cfg, param|
188-
describe "and Puppet[:show_diff] is #{cfg} and show_diff => #{param}" do
187+
describe "showing the diff" do
188+
it "doesn't show the diff when #show_diff? is false" do
189+
content.expects(:show_diff?).returns false
190+
content.expects(:diff).never
191+
expect(content).not_to be_safe_insync("other content")
192+
end
193+
194+
describe "and #show_diff? is true" do
189195
before do
190-
Puppet[:show_diff] = cfg
191-
resource.stubs(:show_diff?).returns param
196+
content.expects(:show_diff?).returns true
192197
resource[:loglevel] = "debug"
193198
end
194199

195-
if cfg and param
196-
it "should display a diff" do
197-
content.expects(:diff).returns("my diff").once
198-
content.expects(:debug).with("\nmy diff").once
199-
expect(content).not_to be_safe_insync("other content")
200-
end
201-
else
202-
it "should not display a diff" do
203-
content.expects(:diff).never
204-
expect(content).not_to be_safe_insync("other content")
205-
end
200+
it "prints the diff" do
201+
content.expects(:diff).returns("my diff").once
202+
content.expects(:debug).with("\nmy diff").once
203+
expect(content).not_to be_safe_insync("other content")
204+
end
205+
206+
it "redacts the diff when the property is sensitive" do
207+
content.sensitive = true
208+
content.expects(:diff).returns("my diff").never
209+
content.expects(:debug).with("[diff redacted]").once
210+
expect(content).not_to be_safe_insync("other content")
206211
end
207212
end
208213
end
@@ -308,6 +313,33 @@
308313
end
309314
end
310315

316+
describe "determining if a diff should be shown" do
317+
let(:content) { described_class.new(:resource => resource) }
318+
319+
before do
320+
Puppet[:show_diff] = true
321+
resource[:show_diff] = true
322+
end
323+
324+
it "is true if there are changes and the global and per-resource show_diff settings are true" do
325+
expect(content.show_diff?(true)).to be_truthy
326+
end
327+
328+
it "is false if there are no changes" do
329+
expect(content.show_diff?(false)).to be_falsey
330+
end
331+
332+
it "is false if show_diff is globally disabled" do
333+
Puppet[:show_diff] = false
334+
expect(content.show_diff?(false)).to be_falsey
335+
end
336+
337+
it "is false if show_diff is disabled on the resource" do
338+
resource[:show_diff] = false
339+
expect(content.show_diff?(false)).to be_falsey
340+
end
341+
end
342+
311343
describe "when changing the content" do
312344
let(:content) { described_class.new(:resource => resource) }
313345

spec/unit/type/file_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,11 @@
364364
expect(file[:ensure]).to eq(:link)
365365
end
366366

367-
describe "marking properties as sensitive" do
368-
it "marks content and ensure as sensitive when source is sensitive" do
367+
describe "marking parameters as sensitive" do
368+
it "marks sensitive, content, and ensure as sensitive when source is sensitive" do
369369
resource = Puppet::Resource.new(:file, make_absolute("/tmp/foo"), :parameters => {:source => make_absolute('/tmp/bar')}, :sensitive_parameters => [:source])
370370
file = described_class.new(resource)
371+
expect(file.parameter(:source).sensitive).to eq true
371372
expect(file.property(:content).sensitive).to eq true
372373
expect(file.property(:ensure).sensitive).to eq true
373374
end

0 commit comments

Comments
 (0)