Add puppetlabs/certregen module
[mirror/dsa-puppet.git] / 3rdparty / modules / certregen / lib / puppet_x / certregen / certificate.rb
1 require 'puppet_x/certregen/util'
2
3 module PuppetX
4   module Certregen
5     module Certificate
6       module_function
7
8       # @param cert [Puppet::SSL::Certificate]
9       # @return [Hash<Symbol, String>]
10       def expiry(cert)
11         if cert.content.not_after < Time.now
12           status = :expired
13         elsif expiring?(cert)
14           status = :expiring
15         else
16           status = :ok
17         end
18
19         data = {
20           :status => status,
21           :expiration_date => cert.content.not_after
22         }
23
24         if status != :expired
25           data[:expires_in] = PuppetX::Certregen::Util.duration(cert.content.not_after - Time.now)
26         end
27
28         data
29       end
30
31       # Is this certificate expiring or expired?
32       #
33       # @param cert [Puppet::SSL::Certificate]
34       # @param percent [Integer]
35       def expiring?(cert, percent = 10)
36         remaining = cert.content.not_after - Time.now
37         lifetime = cert.content.not_after - (cert.content.not_before + 86400)
38         remaining / lifetime < (percent / 100.0)
39       end
40     end
41   end
42 end