Skip to content

Commit 65a724c

Browse files
author
Helen
authored
Merge pull request #233 from johngmyers/update-chain
(MODULES-1997) - Update the target when the cert chain changes
2 parents 2d47928 + f2922c7 commit 65a724c

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

lib/puppet/provider/java_ks/keytool.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ def exists?
162162
end
163163
end
164164

165-
# Extract's the fingerprint of a given output
165+
# Extracts the fingerprints of a given output
166166
def extract_fingerprint(output)
167-
return output.scan(%r{Certificate fingerprints:\n\s+MD5: .*\n\s+SHA1: (.*)})[0][0] if output.include? 'MD5:'
168-
output.scan(%r{Certificate fingerprints:\n\s+SHA1: (.*)})[0][0]
167+
output.scan(%r{Certificate fingerprints:\n\s+(?:MD5: .*\n\s+)?SHA1: (.*)}).flatten.join('/')
169168
end
170169

171170
# Reading the fingerprint of the certificate on disk.

spec/acceptance/chain_key_spec.rb

+59
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@
3838
end
3939
end
4040
end
41+
42+
it 'updates the chain' do
43+
pp = <<-MANIFEST
44+
java_ks { 'broker.example.com:#{target}':
45+
ensure => latest,
46+
certificate => "#{@temp_dir}leafchain2.pem",
47+
private_key => "#{@temp_dir}leafkey.pem",
48+
password => 'puppet',
49+
path => #{@resource_path},
50+
}
51+
MANIFEST
52+
53+
apply_manifest(pp, catch_failures: true)
54+
55+
expectations = [
56+
%r{Alias name: broker\.example\.com},
57+
%r{Entry type: (keyEntry|PrivateKeyEntry)},
58+
%r{Certificate chain length: 2},
59+
%r{^Serial number: 5$.*^Serial number: 6$}m,
60+
]
61+
shell("\"#{@keytool_path}keytool\" -list -v -keystore #{target} -storepass puppet") do |r|
62+
expect(r.exit_code).to be_zero
63+
end
64+
shell("\"#{@keytool_path}keytool\" -list -v -keystore #{target} -storepass puppet") do |r|
65+
expectations.each do |expect|
66+
expect(r.stdout).to match(expect)
67+
end
68+
end
69+
end
4170
end
4271

4372
describe 'managing separate java chain keys', unless: UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
@@ -77,6 +106,36 @@
77106
end
78107
end
79108
end
109+
110+
it 'updates the chain' do
111+
pp = <<-MANIFEST
112+
java_ks { 'broker.example.com:#{target}':
113+
ensure => latest,
114+
certificate => "#{@temp_dir}leaf.pem",
115+
chain => "#{@temp_dir}chain2.pem",
116+
private_key => "#{@temp_dir}leafkey.pem",
117+
password => 'puppet',
118+
path => #{@resource_path},
119+
}
120+
MANIFEST
121+
122+
apply_manifest(pp, catch_failures: true)
123+
124+
expectations = [
125+
%r{Alias name: broker\.example\.com},
126+
%r{Entry type: (keyEntry|PrivateKeyEntry)},
127+
%r{Certificate chain length: 2},
128+
%r{^Serial number: 5$.*^Serial number: 6$}m,
129+
]
130+
shell("\"#{@keytool_path}keytool\" -list -v -keystore #{target} -storepass puppet") do |r|
131+
expect(r.exit_code).to be_zero
132+
end
133+
shell("\"#{@keytool_path}keytool\" -list -v -keystore #{target} -storepass puppet") do |r|
134+
expectations.each do |expect|
135+
expect(r.stdout).to match(expect)
136+
end
137+
end
138+
end
80139
end
81140

82141
describe 'managing non existent java chain keys in noop', unless: UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do

spec/acceptance/pkcs12_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@
4646
end
4747
end
4848
end
49+
50+
it 'updates the chain' do
51+
pp = <<-MANIFEST
52+
java_ks { 'Leaf Cert:#{target}':
53+
ensure => #{@ensure_ks},
54+
certificate => "#{@temp_dir}leaf2.p12",
55+
storetype => 'pkcs12',
56+
password => 'puppet',
57+
path => #{@resource_path},
58+
source_password => 'pkcs12pass'
59+
}
60+
MANIFEST
61+
62+
apply_manifest(pp, catch_failures: true)
63+
64+
expectations = [
65+
%r{Alias name: leaf cert},
66+
%r{Entry type: (keyEntry|PrivateKeyEntry)},
67+
%r{Certificate chain length: 2},
68+
%r{^Serial number: 5$.*^Serial number: 6$}m,
69+
]
70+
shell("\"#{@keytool_path}keytool\" -list -v -keystore #{target} -storepass puppet") do |r|
71+
expect(r.exit_code).to be_zero
72+
end
73+
shell("\"#{@keytool_path}keytool\" -list -v -keystore #{target} -storepass puppet") do |r|
74+
expectations.each do |expect|
75+
expect(r.stdout).to match(expect)
76+
end
77+
end
78+
end
4979
end # context 'with defaults'
5080

5181
context 'with a different alias' do

spec/spec_helper_acceptance.rb

+13
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,29 @@ def create_certs(host, tmpdir)
7676
leaf.not_after = leaf.not_before + 360
7777
leaf.sign(key_chain2, OpenSSL::Digest::SHA256.new)
7878

79+
chain3 = OpenSSL::X509::Certificate.new
80+
chain3.serial = 6
81+
chain3.public_key = key_chain2.public_key
82+
chain3.subject = OpenSSL::X509::Name.parse chain2_subj
83+
chain3.issuer = ca.subject
84+
chain3.not_before = Time.now
85+
chain3.not_after = chain3.not_before + 360
86+
chain3.sign(key, OpenSSL::Digest::SHA256.new)
87+
7988
pkcs12 = OpenSSL::PKCS12.create('pkcs12pass', 'Leaf Cert', key_leaf, leaf, [chain2, chain])
89+
pkcs12_chain3 = OpenSSL::PKCS12.create('pkcs12pass', 'Leaf Cert', key_leaf, leaf, [chain3])
8090

8191
create_remote_file(host, "#{tmpdir}/privkey.pem", key.to_pem)
8292
create_remote_file(host, "#{tmpdir}/ca.pem", ca.to_pem)
8393
create_remote_file(host, "#{tmpdir}/ca2.pem", ca2.to_pem)
8494
create_remote_file(host, "#{tmpdir}/chain.pem", chain2.to_pem + chain.to_pem)
95+
create_remote_file(host, "#{tmpdir}/chain2.pem", chain3.to_pem)
8596
create_remote_file(host, "#{tmpdir}/leafkey.pem", key_leaf.to_pem)
8697
create_remote_file(host, "#{tmpdir}/leaf.pem", leaf.to_pem)
8798
create_remote_file(host, "#{tmpdir}/leafchain.pem", leaf.to_pem + chain2.to_pem + chain.to_pem)
99+
create_remote_file(host, "#{tmpdir}/leafchain2.pem", leaf.to_pem + chain3.to_pem)
88100
create_remote_file(host, "#{tmpdir}/leaf.p12", pkcs12.to_der)
101+
create_remote_file(host, "#{tmpdir}/leaf2.p12", pkcs12_chain3.to_der)
89102
end
90103

91104
RSpec.configure do |c|

0 commit comments

Comments
 (0)