]> git.openstreetmap.org Git - chef.git/blob - cookbooks/letsencrypt/files/default/bin/check-certificate
Check all hosts for shared certificates
[chef.git] / cookbooks / letsencrypt / files / default / bin / check-certificate
1 #!/usr/bin/ruby
2
3 require "socket"
4 require "openssl"
5
6 host = ARGV.shift
7 address = ARGV.shift
8 domains = ARGV
9
10 context = OpenSSL::SSL::SSLContext.new
11 context.verify_mode = OpenSSL::SSL::VERIFY_NONE
12
13 begin
14   socket = TCPSocket.new(address, 443)
15
16   ssl = OpenSSL::SSL::SSLSocket.new(socket, context)
17   ssl.sync_close = true
18   ssl.hostname = domains.first
19   ssl.connect
20 rescue StandardError => error
21   puts "Error connecting to #{host}: #{error.message}"
22 end
23
24 certificate = ssl.peer_cert
25
26 if Time.now < certificate.not_before
27   puts "Certificate #{domains.first} on #{host} not valid until #{certificate.not_before}"
28 elsif certificate.not_after - Time.now < 21 * 86400
29   puts "Certificate #{domains.first} on #{host} expires at #{certificate.not_after}"
30 else
31   subject_alt_name = certificate.extensions.find { |e| e.oid == "subjectAltName" }
32
33   if subject_alt_name.nil?
34     puts "Certificate #{domains.first} on #{host} has no subjectAltName"
35   else
36     alt_names = subject_alt_name.value.split(/\s*,\s*/).map { |n| n.sub(/^DNS:/, "") }
37
38     domains.each do |domain|
39       if alt_names.include?(domain)
40         alt_names.delete(domain)
41       else
42         puts "Certificate #{domains.first} on #{host} is missing subjectAltName #{domain}"
43       end
44     end
45
46     alt_names.each do |name|
47       puts "Certificate #{domains.first} on #{host} has unexpected subjectAltName #{name}"
48     end
49   end
50 end
51
52 ssl.close