Skip to content

Commit b0451d1

Browse files
committed
Handle more unknown password errors
When using keytool on a truststore the error is different than on a keystore. Fixes: 6fea0bb ("Support changing passwords on keystores & truststores")
1 parent cd5c4ca commit b0451d1

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

lib/puppet_x/certs/provider/keystore.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def exists?
2020
'-storepass:file', resource[:password_file],
2121
)
2222
rescue Puppet::ExecutionFailure => e
23-
if e.message.include?('java.security.UnrecoverableKeyException')
23+
if e.message.include?('java.security.UnrecoverableKeyException') || e.message.include?('keystore password was incorrect')
2424
Puppet.debug("Invalid password for #{store}")
2525
return false
2626
else

spec/acceptance/truststore_spec.rb

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
it { should be_grouped_into 'root' }
4242
end
4343

44-
describe command("keytool -list -keystore #{truststore_path} -storepass:file #{truststore_password_file}") do
44+
describe command("keytool -list -keystore #{truststore_path} -storepass testpassword") do
4545
its(:exit_status) { should eq 0 }
4646
its(:stdout) { should match(/^Keystore type: PKCS12$/i) }
4747
its(:stdout) { should match(/^Your keystore contains 0 entries$/) }
@@ -87,5 +87,115 @@
8787
its(:stdout) { should match(/^Owner: CN=#{host_inventory['fqdn']}$/) }
8888
its(:stdout) { should match(/^Issuer: CN=#{host_inventory['fqdn']}$/) }
8989
end
90+
91+
describe 'changing password' do
92+
describe 'apply puppet' do
93+
let(:manifest) do
94+
<<-PUPPET
95+
$truststore_password_file = '/etc/pki/truststore_password-file'
96+
97+
package { 'java-11-openjdk-headless':
98+
ensure => installed,
99+
}
100+
101+
file { $truststore_password_file:
102+
ensure => file,
103+
content => 'other-password',
104+
owner => 'root',
105+
group => 'root',
106+
mode => '0440',
107+
show_diff => false,
108+
}
109+
110+
truststore { "/etc/pki/truststore":
111+
ensure => present,
112+
password_file => $truststore_password_file,
113+
owner => 'root',
114+
group => 'root',
115+
mode => '0640',
116+
}
117+
PUPPET
118+
end
119+
120+
it 'applies changes with no errors' do
121+
apply_manifest_on(default, manifest, expect_changes: true)
122+
end
123+
124+
it 'applies a second time without changes' do
125+
apply_manifest_on(default, manifest, catch_changes: true)
126+
end
127+
end
128+
129+
describe command("keytool -list -keystore #{truststore_path} -storepass other-password") do
130+
its(:exit_status) { should eq 0 }
131+
its(:stdout) { should match(/^Keystore type: PKCS12$/i) }
132+
its(:stdout) { should match(/^Your keystore contains 0 entries$/) }
133+
end
134+
end
135+
136+
describe 'noop' do
137+
describe 'change password file' do
138+
let(:manifest) do
139+
<<-PUPPET
140+
file { '/etc/pki/truststore_password-file':
141+
ensure => file,
142+
content => 'wrong-password',
143+
owner => 'root',
144+
group => 'root',
145+
mode => '0440',
146+
show_diff => false,
147+
}
148+
PUPPET
149+
end
150+
151+
it 'applies changes with no errors' do
152+
apply_manifest_on(default, manifest, catch_failures: true)
153+
end
154+
end
155+
156+
describe 'run in noop mode with wrong password' do
157+
let(:manifest) do
158+
<<-PUPPET
159+
$truststore_password_file = '/etc/pki/truststore_password-file'
160+
161+
package { 'java-11-openjdk-headless':
162+
ensure => installed,
163+
}
164+
165+
file { $truststore_password_file:
166+
ensure => file,
167+
content => 'other-password',
168+
owner => 'root',
169+
group => 'root',
170+
mode => '0440',
171+
show_diff => false,
172+
}
173+
174+
truststore { "/etc/pki/truststore":
175+
ensure => present,
176+
password_file => $truststore_password_file,
177+
owner => 'root',
178+
group => 'root',
179+
mode => '0640',
180+
}
181+
PUPPET
182+
end
183+
184+
it 'applies changes with no errors' do
185+
apply_manifest_on(default, manifest, noop: true)
186+
end
187+
end
188+
189+
describe file(truststore_path) do
190+
it { is_expected.to be_file }
191+
end
192+
193+
# Should still be readable with the old password
194+
describe command("keytool -list -keystore #{truststore_path} -storepass other-password") do
195+
its(:exit_status) { should eq 0 }
196+
its(:stdout) { should match(/^Keystore type: PKCS12$/i) }
197+
its(:stdout) { should match(/^Your keystore contains 0 entries$/) }
198+
end
199+
end
90200
end
91201
end

0 commit comments

Comments
 (0)