symbiosis-ssl 3.93 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
# 
# NAME
#
#   symbiosis-ssl - Manage and generate SSL certificates 
#
# SYNOPSIS
#
9
#   symbiosis-ssl [ --threshold N ] [ --no-generate ] [ --no-rollover ] [ --verbose ] [ --manual ]
10
11
12
13
#     [ --help ] [ DOMAIN DOMAIN ...]
#
# OPTIONS
#
14
15
#  --threshold N    Number days before expiry that certificates should be renewed.  Defaults to 10.
#
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#  --no-generate    Do not try and generate keys or certificates.
#
#  --no-rollover    Do not try and generate keys or certificates.
#
#  --prefix PREFIX  Set the directory prefix for Symbiosis.  Defaults to /srv.
#
#   --help        Show the help information for this script.
#
#   --manual      Show the manual for this script
#
#   --verbose     Show debugging information.
#
# AUTHOR
#
#   Patrick J. Cherry <patrick@bytemark.co.uk>
#

#
#  Modules we require
#

require 'getoptlong'

opts = GetoptLong.new(
    [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
    [ '--manual', '-m', GetoptLong::NO_ARGUMENT ],
    [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
43
    [ '--threshold', '-t', GetoptLong::REQUIRED_ARGUMENT ],
44
45
46
47
48
49
50
    [ '--no-generate', '-G', GetoptLong::NO_ARGUMENT ],
    [ '--no-rollover', '-R', GetoptLong::NO_ARGUMENT ],
    [ '--prefix', '-p', GetoptLong::REQUIRED_ARGUMENT ]
)

manual = help = false
$VERBOSE = false
51
prefix = "/srv"
52
53
do_generate = do_rollover = true
threshold = 10
54
55
56

opts.each do |opt,arg|
  case opt
57
58
59
60
61
62
63
64
65
66
    when '--no-generate'
      do_generate = false
    when '--no-rollover'
      do_rollover = false
    when '--threshold'
      begin
        threshold = Integer(arg)
      rescue ArgumentError
        warn "** Could not parse #{arg.inspect} as an integer for --threshold"
      end
67
68
69
70
    when '--help'
      help = true
    when '--manual'
      manual = true
71
72
    when '--prefix'
      prefix = arg
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    when '--verbose'
      $VERBOSE = true
  end
end

#
# Output help as required.
#
if help or manual
  require 'symbiosis/utils'
  Symbiosis::Utils.show_help(__FILE__) if help
  Symbiosis::Utils.show_manual(__FILE__) if manual
  exit 0
end

88
89
90
91
92
93
94
95
#
# The required spawn a massive stack of warnings in verbose mode.  So let's
# hide them.
#
v = $VERBOSE
$VERBOSE = false

require 'symbiosis/domains'
96
97
require 'symbiosis/domain/ssl'
require 'symbiosis/ssl'
98
99
100
101
102
103
104
105
require 'symbiosis/ssl/letsencrypt'
require 'symbiosis/ssl/selfsigned'

#
# And unhide.  Ugh.
#
$VERBOSE = v

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

domains = []

ARGV.each do |arg|
  domain = Symbiosis::Domains.find(arg.to_s, prefix)

  if domain.nil?
    warn "** Unable to find/parse domain #{arg.inspect}" 
    next
  end

  domains << domain
end

if ARGV.empty?
  domains = Symbiosis::Domains.all(prefix)
end

now = Time.now
threshold = 14
126
exit_code = 0
127
128
129
130
131
132
133

domains.each do |domain|
  puts "* Examining certificates for #{domain.name}" if $VERBOSE

  #
  # Stage 0: verify and check expiriy
  #
134
  set = domain.ssl_current_set
135
  set = domain.ssl_available_sets.last unless set.is_a?(Symbiosis::SSL::CertificateSet)
136

137
  expires_in = nil
138

139
  if set.is_a?(Symbiosis::SSL::CertificateSet)
140
    expires_in = ((set.certificate.not_after - now)/86400.0).round
141
    if expires_in < 14
142
      puts "\tThe certificate is due to expire in #{expires_in} days" if $VERBOSE
143
144
145
    end

  else
146
    puts "\tNo valid certificates found." if $VERBOSE
147
148
149
150
151
152
  end

  #
  # Stage 1: Generate
  #
  if false == domain.ssl_provider
153
154
155
    puts "\tSkipping because the ssl-provider has been set to false." if $VERBOSE

  elsif (expires_in.is_a?(Integer) and expires_in < threshold) or set.nil?
156
157
158
159
160
    #
    # Default to letsencrypt
    #
    domain.ssl_provider = "letsencrypt" if domain.ssl_provider.nil?

161
    puts "\tFetching a new certificate from #{domain.ssl_provider}." if $VERBOSE
162

163
164
165
166
167
168
169
    begin
      cert_set = domain.ssl_fetch_new_certificate
      cert_set.name = domain.ssl_next_set
      cert_set.write
    rescue StandardError => err
      puts "\t!! Failed: #{err.to_s.gsub($/,'')}" if $VERBOSE
      puts err.backtrace.join("\n") if $DEBUG
170
      exit_code = 1
171
172
    end

173
174
175
176
177
178
179
180
181
  end

  #
  # Stage 2: Roll over
  #
  domain.ssl_rollover
end


182
exit exit_code
183