Skip to content

Commit 6fea0bb

Browse files
committed
Support changing passwords on keystores & truststores
Opening the store when checking if it exists will raise an exception if the password is incorrect. This takes an approach of catching all exceptions and treating them all the same.
1 parent 027e2cb commit 6fea0bb

File tree

2 files changed

+131
-2
lines changed

2 files changed

+131
-2
lines changed

lib/puppet_x/certs/provider/keystore.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@ def destroy
1111
end
1212

1313
def exists?
14-
File.exist?(store)
14+
return false unless File.exist?(store)
15+
16+
begin
17+
keytool(
18+
'-list',
19+
'-keystore', store,
20+
'-storepass:file', resource[:password_file],
21+
)
22+
rescue Puppet::ExecutionFailure => e
23+
if e.message.include?('java.security.UnrecoverableKeyException')
24+
Puppet.debug("Invalid password for #{store}")
25+
return false
26+
else
27+
Puppet.log_exception(e, "Failed to read keystore '#{store}'")
28+
end
29+
end
30+
31+
true
1532
end
1633

1734
def store
@@ -25,6 +42,8 @@ def type
2542
def generate_keystore
2643
temp_alias = 'temporary-entry'
2744

45+
FileUtils.rm_f(store)
46+
2847
begin
2948
keytool(
3049
'-genkey',

spec/acceptance/keystore_spec.rb

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,120 @@
3838
it { should be_grouped_into 'root' }
3939
end
4040

41-
describe command("keytool -list -keystore /etc/pki/keystore -storepass:file /etc/pki/keystore_password-file") do
41+
describe command("keytool -list -keystore /etc/pki/keystore -storepass testpassword") do
4242
its(:exit_status) { should eq 0 }
4343
its(:stdout) { should match(/^Keystore type: PKCS12$/i) }
4444
its(:stdout) { should match(/^Your keystore contains 0 entries$/) }
4545
end
46+
47+
describe 'changing password' do
48+
describe 'apply puppet' do
49+
let(:manifest) do
50+
<<-PUPPET
51+
$keystore_password_file = '/etc/pki/keystore_password-file'
52+
53+
package { 'java-11-openjdk-headless':
54+
ensure => installed,
55+
}
56+
57+
file { $keystore_password_file:
58+
ensure => file,
59+
content => 'other-password',
60+
owner => 'root',
61+
group => 'root',
62+
mode => '0440',
63+
show_diff => false,
64+
}
65+
66+
keystore { "/etc/pki/keystore":
67+
ensure => present,
68+
password_file => $keystore_password_file,
69+
owner => 'root',
70+
group => 'root',
71+
mode => '0640',
72+
}
73+
PUPPET
74+
end
75+
76+
it 'applies changes with no errors' do
77+
apply_manifest_on(default, manifest, expect_changes: true)
78+
end
79+
80+
it 'applies a second time without changes' do
81+
apply_manifest_on(default, manifest, catch_changes: true)
82+
end
83+
end
84+
85+
describe command("keytool -list -keystore /etc/pki/keystore -storepass other-password") do
86+
its(:exit_status) { should eq 0 }
87+
its(:stdout) { should match(/^Keystore type: PKCS12$/i) }
88+
its(:stdout) { should match(/^Your keystore contains 0 entries$/) }
89+
end
90+
end
91+
92+
describe 'noop' do
93+
describe 'change password file' do
94+
let(:manifest) do
95+
<<-PUPPET
96+
file { '/etc/pki/keystore_password-file':
97+
ensure => file,
98+
content => 'wrong-password',
99+
owner => 'root',
100+
group => 'root',
101+
mode => '0440',
102+
show_diff => false,
103+
}
104+
PUPPET
105+
end
106+
107+
it 'applies changes with no errors' do
108+
apply_manifest_on(default, manifest, catch_failures: true)
109+
end
110+
end
111+
112+
describe 'run in noop mode with wrong password' do
113+
let(:manifest) do
114+
<<-PUPPET
115+
$keystore_password_file = '/etc/pki/keystore_password-file'
116+
117+
package { 'java-11-openjdk-headless':
118+
ensure => installed,
119+
}
120+
121+
file { $keystore_password_file:
122+
ensure => file,
123+
content => 'other-password',
124+
owner => 'root',
125+
group => 'root',
126+
mode => '0440',
127+
show_diff => false,
128+
}
129+
130+
keystore { "/etc/pki/keystore":
131+
ensure => present,
132+
password_file => $keystore_password_file,
133+
owner => 'root',
134+
group => 'root',
135+
mode => '0640',
136+
}
137+
PUPPET
138+
end
139+
140+
it 'applies changes with no errors' do
141+
apply_manifest_on(default, manifest, noop: true)
142+
end
143+
end
144+
145+
describe file('/etc/pki/keystore') do
146+
it { is_expected.to be_file }
147+
end
148+
149+
# Should still be readable with the old password
150+
describe command("keytool -list -keystore /etc/pki/keystore -storepass other-password") do
151+
its(:exit_status) { should eq 0 }
152+
its(:stdout) { should match(/^Keystore type: PKCS12$/i) }
153+
its(:stdout) { should match(/^Your keystore contains 0 entries$/) }
154+
end
155+
end
46156
end
47157
end

0 commit comments

Comments
 (0)