]> git.openstreetmap.org Git - chef.git/blob - cookbooks/letsencrypt/templates/default/check-certificate.erb
Merge remote-tracking branch 'github/pull/786'
[chef.git] / cookbooks / letsencrypt / templates / default / check-certificate.erb
1 #!<%= node[:ruby][:interpreter] %>
2
3 require "socket"
4 require "openssl"
5 require "net/http"
6
7 host = ARGV.shift
8 address = ARGV.shift
9 domains = ARGV
10
11 context = OpenSSL::SSL::SSLContext.new
12 context.verify_mode = OpenSSL::SSL::VERIFY_NONE
13
14 begin
15   socket = TCPSocket.new(address, 443)
16
17   ssl = OpenSSL::SSL::SSLSocket.new(socket, context)
18   ssl.sync_close = true
19   ssl.hostname = domains.first
20   ssl.connect
21 rescue StandardError => e
22   puts "Error connecting to #{host}: #{e.message}"
23 end
24
25 if ssl
26   certificate = ssl.peer_cert
27   chain = ssl.peer_cert_chain.drop(1)
28   issuer = chain.first
29
30   if Time.now < certificate.not_before
31     puts "Certificate #{domains.first} on #{host} not valid until #{certificate.not_before}"
32   elsif certificate.not_after - Time.now < 21 * 86400
33     puts "Certificate #{domains.first} on #{host} expires at #{certificate.not_after}"
34   end
35
36   unless certificate.public_key.is_a?(OpenSSL::PKey::EC)
37     puts "Certificate #{domains.first} on #{host} does not use ECDSA key type"
38   end
39
40   subject_alt_name = certificate.extensions.find { |ext| ext.oid == "subjectAltName" }
41
42   if subject_alt_name.nil?
43     puts "Certificate #{domains.first} on #{host} has no subjectAltName"
44   else
45     alt_names = subject_alt_name.value.split(/\s*,\s*/).map { |n| n.sub(/^DNS:/, "") }
46
47     domains.each do |domain|
48       if alt_names.include?(domain)
49         alt_names.delete(domain)
50       else
51         puts "Certificate #{domains.first} on #{host} is missing subjectAltName #{domain}"
52       end
53     end
54
55     alt_names.each do |name|
56       puts "Certificate #{domains.first} on #{host} has unexpected subjectAltName #{name}"
57     end
58   end
59
60   ssl.close
61 end