Skip to content

Commit 1762b4d

Browse files
authored
Merge pull request #757 from syseleven/support-sensitive
Allow content parameter of concat_fragment to be Sensitive
2 parents 466e410 + 9ccd1c6 commit 1762b4d

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

lib/puppet/type/concat_file.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def fragment_content(r)
314314

315315
if self[:ensure_newline]
316316
newline = Puppet::Util::Platform.windows? ? "\r\n" : "\n"
317-
fragment_content << newline unless %r{#{newline}\Z}.match?(fragment_content)
317+
fragment_content = fragment_content.dup << newline unless %r{#{newline}\Z}.match?(fragment_content)
318318
end
319319

320320
fragment_content

lib/puppet/type/concat_fragment.rb

+9
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,13 @@
9494
# Check if both are set, if so rais error
9595
raise Puppet::ParseError, _("Can't use 'source' and 'content' at the same time") if !self[:source].nil? && !self[:content].nil?
9696
end
97+
98+
def set_sensitive_parameters(sensitive_parameters)
99+
# Respect sensitive https://tickets.puppetlabs.com/browse/PUP-10950
100+
if sensitive_parameters.include?(:content)
101+
sensitive_parameters.delete(:content)
102+
parameter(:content).sensitive = true
103+
end
104+
super(sensitive_parameters)
105+
end
97106
end

manifests/fragment.pp

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
# Specifies the destination file of the fragment. Valid options: a string containing the path or title of the parent concat resource.
1818
#
1919
define concat::fragment (
20-
String $target,
21-
Optional[Variant[String, Deferred]] $content = undef,
22-
Optional[Variant[String, Array]] $source = undef,
23-
Variant[String, Integer] $order = '10',
20+
String $target,
21+
Optional[Variant[Sensitive[String], String, Deferred]] $content = undef,
22+
Optional[Variant[String, Array]] $source = undef,
23+
Variant[String, Integer] $order = '10',
2424
) {
2525
$resource = 'Concat::Fragment'
2626

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper_acceptance'
4+
5+
describe 'sensitive content' do
6+
attr_reader :basedir
7+
8+
before(:all) do
9+
@basedir = setup_test_directory
10+
end
11+
12+
describe 'file' do
13+
let(:pp) do
14+
<<-MANIFEST
15+
concat { '#{basedir}/sensitive_file': }
16+
17+
concat::fragment { '1':
18+
target => '#{basedir}/sensitive_file',
19+
content => 'user=',
20+
}
21+
22+
concat::fragment { '2':
23+
target => '#{basedir}/sensitive_file',
24+
content => Sensitive('password'),
25+
}
26+
MANIFEST
27+
end
28+
29+
it 'idempotent, file matches' do
30+
idempotent_apply(pp)
31+
expect(file("#{basedir}/sensitive_file")).to be_file
32+
expect(file("#{basedir}/sensitive_file").content).to match 'user=password'
33+
end
34+
end
35+
end

spec/defines/concat_fragment_spec.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,22 @@
6464
end
6565
end
6666

67+
context 'when Sensitive' do
68+
let(:title) { 'authentication' }
69+
let(:content) { sensitive('password') }
70+
let(:params) { { content: content, target: '/etc/authentication' } }
71+
72+
it do
73+
is_expected.to contain_concat_fragment(title).with(content: content)
74+
end
75+
end
76+
6777
context 'when false' do
6878
let(:title) { 'motd_header' }
6979
let(:params) { { content: false, target: '/etc/motd' } }
7080

7181
it 'fails' do
72-
expect { catalogue }.to raise_error(Puppet::Error, %r{expects a value of type Undef( or String|, String, or Deferred), got Boolean})
82+
expect { catalogue }.to raise_error(Puppet::Error, %r{expects a value of type Undef, Sensitive\[String\], String, or Deferred, got Boolean})
7383
end
7484
end
7585
end

0 commit comments

Comments
 (0)