Skip to content

Commit e4bb473

Browse files
committed
(PUP-11522) DRY client cert and private key validation
Add private method to check client cert and private key, and resolve the client chain (all certs leading to the trusted root).
1 parent 3f7f830 commit e4bb473

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

lib/puppet/ssl/ssl_provider.rb

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,7 @@ def create_system_context(cacerts:, path: Puppet[:ssl_trust_store], include_clie
8080
client_cert = cert_provider.load_client_cert(Puppet[:certname], required: false)
8181

8282
if private_key && client_cert
83-
client_chain = verify_cert_with_store(store, client_cert)
84-
85-
if !private_key.is_a?(OpenSSL::PKey::RSA) && !private_key.is_a?(OpenSSL::PKey::EC)
86-
raise Puppet::SSL::SSLError, _("Unsupported key '%{type}'") % { type: private_key.class.name }
87-
end
88-
89-
unless client_cert.check_private_key(private_key)
90-
raise Puppet::SSL::SSLError, _("The certificate for '%{name}' does not match its private key") % { name: subject(client_cert) }
91-
end
83+
client_chain = resolve_client_chain(store, client_cert, private_key)
9284

9385
return Puppet::SSL::SSLContext.new(
9486
store: store, cacerts: cacerts, crls: [],
@@ -134,15 +126,7 @@ def create_context(cacerts:, crls:, private_key:, client_cert:, revocation: Pupp
134126
raise ArgumentError, _("Client cert is missing") unless client_cert
135127

136128
store = create_x509_store(cacerts, crls, revocation, include_system_store: include_system_store)
137-
client_chain = verify_cert_with_store(store, client_cert)
138-
139-
if !private_key.is_a?(OpenSSL::PKey::RSA) && !private_key.is_a?(OpenSSL::PKey::EC)
140-
raise Puppet::SSL::SSLError, _("Unsupported key '%{type}'") % { type: private_key.class.name }
141-
end
142-
143-
unless client_cert.check_private_key(private_key)
144-
raise Puppet::SSL::SSLError, _("The certificate for '%{name}' does not match its private key") % { name: subject(client_cert) }
145-
end
129+
client_chain = resolve_client_chain(store, client_cert, private_key)
146130

147131
Puppet::SSL::SSLContext.new(
148132
store: store, cacerts: cacerts, crls: crls,
@@ -268,6 +252,20 @@ def revocation_mode(mode)
268252
end
269253
end
270254

255+
def resolve_client_chain(store, client_cert, private_key)
256+
client_chain = verify_cert_with_store(store, client_cert)
257+
258+
if !private_key.is_a?(OpenSSL::PKey::RSA) && !private_key.is_a?(OpenSSL::PKey::EC)
259+
raise Puppet::SSL::SSLError, _("Unsupported key '%{type}'") % { type: private_key.class.name }
260+
end
261+
262+
unless client_cert.check_private_key(private_key)
263+
raise Puppet::SSL::SSLError, _("The certificate for '%{name}' does not match its private key") % { name: subject(client_cert) }
264+
end
265+
266+
client_chain
267+
end
268+
271269
def verify_cert_with_store(store, cert)
272270
# StoreContext#initialize accepts a chain argument, but it's set to [] because
273271
# puppet requires any intermediate CA certs needed to complete the client's

0 commit comments

Comments
 (0)