Skip to content

Commit b88856d

Browse files
committed
Added safety measures for running in noop mode and updated the acceptance tests
1 parent 01e8f6e commit b88856d

File tree

2 files changed

+108
-19
lines changed

2 files changed

+108
-19
lines changed

Diff for: lib/puppet/provider/java_ks/keytool.rb

+29-19
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,39 @@ def exists?
8484

8585
# Reading the fingerprint of the certificate on disk.
8686
def latest
87-
cmd = [
88-
command_keytool,
89-
'-v', '-printcert', '-file', certificate
90-
]
91-
output = run_command(cmd)
92-
latest = output.scan(/MD5:\s+(.*)/)[0][0]
93-
return latest
87+
# The certificate file may not exist during a puppet noop run as it's managed by puppet.
88+
# Return value must be different to provider.current to signify a possible trigger event.
89+
if Puppet[:noop] and !File.exists?(certificate)
90+
return 'latest'
91+
else
92+
cmd = [
93+
command_keytool,
94+
'-v', '-printcert', '-file', certificate
95+
]
96+
output = run_command(cmd)
97+
latest = output.scan(/MD5:\s+(.*)/)[0][0]
98+
return latest
99+
end
94100
end
95101

96102
# Reading the fingerprint of the certificate currently in the keystore.
97103
def current
98-
output = ''
99-
cmd = [
100-
command_keytool,
101-
'-list', '-v',
102-
'-keystore', @resource[:target],
103-
'-alias', @resource[:name]
104-
]
105-
tmpfile = password_file
106-
output = run_command(cmd, false, tmpfile)
107-
tmpfile.close!
108-
current = output.scan(/Certificate fingerprints:\n\s+MD5: (.*)/)[0][0]
109-
return current
104+
# The keystore file may not exist during a puppet noop run as it's managed by puppet.
105+
if Puppet[:noop] and !File.exists?(@resource[:target])
106+
return 'current'
107+
else
108+
cmd = [
109+
command_keytool,
110+
'-list', '-v',
111+
'-keystore', @resource[:target],
112+
'-alias', @resource[:name]
113+
]
114+
tmpfile = password_file
115+
output = run_command(cmd, false, tmpfile)
116+
tmpfile.close!
117+
current = output.scan(/Certificate fingerprints:\n\s+MD5: (.*)/)[0][0]
118+
return current
119+
end
110120
end
111121

112122
# Determine if we need to do an import of a private_key and certificate pair

Diff for: spec/acceptance/chain_key_spec.rb

+79
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,82 @@
3434
end
3535
end
3636
end
37+
38+
describe 'managing non existent java chain keys in noop', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
39+
include_context 'common variables'
40+
41+
case fact('osfamily')
42+
when "windows"
43+
target = 'c:/noop_chain_key.ks'
44+
temp_dir = 'C:/tmp/'
45+
else
46+
target = '/etc/noop_chain_key.ks'
47+
temp_dir = '/tmp/'
48+
end
49+
it 'does not create a new keystore in noop' do
50+
pp = <<-EOS
51+
$filenames = ["#{temp_dir}noop_ca.pem",
52+
"#{temp_dir}noop_chain.pem",
53+
"#{temp_dir}noop_privkey.pem"]
54+
file { $filenames:
55+
ensure => file,
56+
content => 'content',
57+
} ->
58+
java_ks { 'broker.example.com:#{target}':
59+
ensure => latest,
60+
certificate => "#{temp_dir}noop_ca.pem",
61+
chain => "#{temp_dir}noop_chain.pem",
62+
private_key => "#{temp_dir}noop_privkey.pem",
63+
password => 'puppet',
64+
path => #{@resource_path},
65+
}
66+
EOS
67+
68+
# in noop mode, when the dependent certificate files are not present in the system,
69+
# java_ks will not invoke openssl to validate their status, thus noop will succeed
70+
apply_manifest(pp, :catch_failures => true, :noop => true)
71+
end
72+
73+
# verifies the dependent files are missing
74+
["#{temp_dir}noop_ca.pem", "#{temp_dir}noop_chain.pem", "#{temp_dir}noop_privkey.pem"].each do |filename|
75+
describe file("#{filename}") do
76+
it { should_not be_file }
77+
end
78+
end
79+
80+
# verifies the keystore is not created
81+
describe file("#{target}") do
82+
it { should_not be_file }
83+
end
84+
end
85+
86+
describe 'managing existing java chain keys in noop', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
87+
include_context 'common variables'
88+
89+
case fact('osfamily')
90+
when "windows"
91+
target = 'c:/noop2_chain_key.ks'
92+
else
93+
target = '/etc/noop2_chain_key.ks'
94+
end
95+
it 'does not create a new keystore in noop' do
96+
pp = <<-EOS
97+
java_ks { 'broker.example.com:#{target}':
98+
ensure => latest,
99+
certificate => "#{@temp_dir}ca.pem",
100+
chain => "#{@temp_dir}chain.pem",
101+
private_key => "#{@temp_dir}privkey.pem",
102+
password => 'puppet',
103+
path => #{@resource_path},
104+
}
105+
EOS
106+
107+
apply_manifest(pp, :catch_failures => true, :noop => true)
108+
end
109+
110+
# in noop mode, when the dependent certificate files are present in the system,
111+
# java_ks will invoke openssl to validate their status, but will not create the keystore
112+
describe file("#{target}") do
113+
it { should_not be_file }
114+
end
115+
end

0 commit comments

Comments
 (0)