summaryrefslogtreecommitdiff
path: root/macir.rb
blob: 133a1dbb4cd8b8167ef9a93db957525ea7aac50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/ruby

# frozen_string_literal: true

require 'yaml'
require 'openssl'
require 'acme-client'
require 'dnsruby'
require 'time'
require 'English'


def read_file(path)
  if File.readable?(path)
    p "File #{path} is readable, trying to parse"
    file_content = File.read(path)
  elsif File.exist?(path)
    warn "The file #{path} exists but is not readable. Make it readable or specify different path"
    raise
  else
    p "File #{path} does not exist, trying to create"
    file_content = nil
  end
  return file_content
end

def read_config(path = 'config.yaml')
  p "Reading config from #{path}"
  YAML.load_file(path)
rescue Psych::SyntaxError => e
  warn "Parsing configfile failed: #{e}"
  raise
rescue Errno::ENOENT => e
  warn "IO failed: #{e}"
  raise
end

def ensure_cert_dir(path = './certs')
  unless File.exist?(path)
    puts 'Certificate directory does not exist. Creating with secure permissions.'
    Dir.mkdir(path, 0o0700)
  end
  File.world_writable?(path) && warn('WARNING! Certificate directory is world writable! This could be a serious security issue!')
  File.world_readable?(path) && warn('WARNING! Certificate directory is world readable! This could be a serious security issue!')
  File.file?(path) && raise('Certificate directory is not a directory but a file. Aborting.')
  File.writable?(path) || raise('Certificate directory is not writable. Aborting.')
  return true
rescue Errno::ENOENT
  abort "Could not create directory #{path}. Maybe parent directory does not exist?"
rescue SystemCallError
  warn 'Something went wrong when checking the certificate directory. Please report a bug so this problem can be caught more gracefully.'
  raise
end

def read_account_key(path = 'pkey.pem')
  p "Reading account key from #{path}"
  account_key_string = read_file(path)
  if account_key_string.nil?
    private_key = OpenSSL::PKey::EC.generate('prime256v1')
    File.write(path, private_key.private_to_pem)
  else
    private_key = OpenSSL::PKey::EC.new(account_key_string)
  end
  return private_key
end

# TODO: divide and simplify
def read_cert_key(cert_name)
  folder = "./certs/#{cert_name}/"
  path = "#{folder}/current.key"
  p "cert_name #{cert_name}: Reading cert key from #{path}"
  cert_key_string = read_file(path)
  if cert_key_string.nil?
    p "cert_name #{cert_name}: dir #{folder} does not exist, trying to create"
    FileTest.directory?(folder) || Dir.mkdir(folder, 0o0700)
    p "cert_name #{cert_name}: File #{path} does not exist, trying to create"
    private_key = OpenSSL::PKey::EC.generate('prime256v1')
    pkey_file = File.new("#{folder}#{Time.now.to_i}.key", 'w')
    pkey_file.write(private_key.private_to_pem)
    File.symlink(File.basename(pkey_file), "#{File.dirname(pkey_file)}/current.key")
  else
    private_key = OpenSSL::PKey::EC.new(cert_key_string)
  end
  return private_key
end

def lookup_ns(domain)
  p "Domain #{domain}: Creating resolver object for looking up NS records"
  rec = Dnsruby::Resolver.new
  p "Domain #{domain}: Getting NS records for #{domain}"
  rec.query_no_validation_or_recursion(domain, 'NS')
rescue StandardError => e
  warn "Domain #{domain}: NS lookup during deploy failed: #{e}"
  raise
end

def lookup_soa(domain)
  p "Domain #{domain}: Creating resolver object for looking up SOA records"
  rec = Dnsruby::Resolver.new
  p "Domain #{domain}: Getting SOA records for #{domain}"
  rec.query_no_validation_or_recursion(domain, 'SOA')
rescue StandardError => e
  warn "Domain #{domain}: SOA lookup during deploy failed: #{e}"
  raise
end

def find_apex_domain(domain)
  domain_soa_resp = lookup_soa(domain)
  if domain_soa_resp.answer.empty?
    domain_soa_resp.authority[0].name
  else
    domain_soa_resp.answer[0].name
  end
end

def build_dns_update_packet(apex, authzs)
  update = Dnsruby::Update.new(apex)

  authzs.each do |auth|
    chal = auth.dns01
    update.delete("#{chal.record_name}.#{auth.domain}", chal.record_type)
    update.add("#{chal.record_name}.#{auth.domain}", chal.record_type, 3, chal.record_content)
  end
  return update
end

def build_tsig_object(apex, config)
  p "Domain #{apex}: Looking up TSIG parameters"
  tsig_name = config.dig('domains', apex, 'tsig_key') || config.dig('defaults', 'domains', 'tsig_key')
  tsig_key = config.dig('tsig_keys', tsig_name, 'key')
  tsig_alg = config.dig('tsig_keys', tsig_name, 'algorithm')

  p "Domain #{apex}: Creating TSIG object"
  Dnsruby::RR.create(
    {
      name: tsig_name,
      type: 'TSIG',
      key: tsig_key,
      algorithm: tsig_alg,
    }
  )
end

def deploy_dns_tokens_on_apex(apex, authzs, nameserver, config)
  update_packet = build_dns_update_packet(apex, authzs)
  tsig = build_tsig_object(apex, config)

  p "Domain #{apex}: Creating object for contacting nameserver"
  res = Dnsruby::Resolver.new(nameserver)
  res.dnssec = false

  p "Domain #{apex}: Signing DNS UPDATE packet with TSIG object"
  tsig.apply(update_packet)

  p "Domain #{apex}: Sending UPDATE to nameserver"
  res.send_message(update_packet)
rescue StandardError => e
  warn "Domain #{apex}: DNS Update failed: #{e}"
  raise
end

def wait_for_challenge_on_ns(chal, ns, domain)
  p "Domain #{domain}: Creating resolver object for checking propagation on #{ns}"
  res = Dnsruby::Resolver.new(ns)
  res.dnssec = false
  res.do_caching = false
  loop do
    p "Domain #{domain}: Querying ACME challenge record"
    result = res.query_no_validation_or_recursion("_acme-challenge.#{domain}", 'TXT')
    propagated = result.answer.any? do |answer|
      answer.rdata[0] == chal.record_content
    end
    break if propagated

    p "Domain #{domain}: Not yet propagated, still old value, sleeping before checking again"
    sleep(0.5)
  rescue Dnsruby::NXDomain
    p "Domain #{domain}: Not yet propagated, NXdomain, sleeping before checking again"
    sleep(0.5)
    retry
  rescue StandardError => e
    warn "Domain #{domain}: ACME challenge lookup failed: #{e}"
    raise
  end
end

def wait_for_challenge_propagation(domain, challenge)
  apex_domain = find_apex_domain(domain)
  domain_auth_ns = lookup_ns(apex_domain)

  p "Domain #{domain}: Checking challenge status on all NS"

  threads = []

  domain_auth_ns.answer.each do |ns|
    threads << Thread.new(challenge, ns, domain) do |challenge, ns, domain|
      wait_for_challenge_on_ns(challenge, ns.rdata.to_s, domain)
    end
  end

  threads.each(&:join)
end

def acme_request_with_retries(retries: 10, &block)
  # p "Retries: #{retries}"
  block.call(self)
rescue Acme::Client::Error::BadNonce
  raise unless retries.positive?

  p 'Retrying because of invalid nonce.'
  acme_request_with_retries(retries: retries - 1, &block)
end

def unvalidated_domains(d_a)
  d_a.reject do |d, a|
    p "#{d} status #{a['auth'].dns01.status}"
    p a['auth'].dns01.status
    acme_request_with_retries { a['auth'].dns01.reload }
    a['auth'].dns01.status == 'valid'
  end
end

def wait_for_challenge_validation(challenge, cert_name)
  p 'Requesting validation of challenge'
  p challenge.object_id
  acme_request_with_retries { challenge.request_validation }

  while challenge.status == 'pending'
    p "Cert #{cert_name}: challenge validation is pending, sleeping before checking again"
    sleep(0.1)
    acme_request_with_retries { challenge.reload }
  end
  # pp challenge
end

def get_cert(order, cert_name, domains, domain_key)
  path = "./certs/#{cert_name}/"
  p "Cert #{cert_name}: Creating CSR object"
  csr = Acme::Client::CertificateRequest.new(
    private_key: domain_key,
    names: domains,
    subject: { common_name: domains[0] }
  )
  p "Cert #{cert_name}: Finalize cert order"
  # pp order
  # TODO: this seems unnecessary?
  # acme_request_with_retries { order.reload }
  acme_request_with_retries { order.finalize(csr: csr) }
  while order.status == 'processing'
    p "Cert #{cert_name}: Sleep while order is processing"
    sleep(0.1)
    p "Cert #{cert_name}: Rechecking order status"
    acme_request_with_retries { order.reload }
  end
  # p "order status: #{order.status}"
  # pp order
  cert = acme_request_with_retries { order.certificate }

  p "Cert #{cert_name}: creating dir"
  FileTest.directory?(path) || Dir.mkdir(path, 0o0700)
  p "Cert #{cert_name}: Writing cert"
  cert_file = File.new("#{path}#{Time.now.to_i}.crt", 'w')
  cert_file.write(cert)
  if File.symlink?("#{File.dirname(cert_file)}/current.crt")
    File.unlink("#{File.dirname(cert_file)}/current.crt")
    File.symlink(File.basename(cert_file), "#{File.dirname(cert_file)}/current.crt")
  elsif File.file?("#{File.dirname(cert_file)}/current.crt")
    raise 'Could not place symlink for "current.crt" because that is already a normal file.'
  end
  return cert
end

def find_ca_for_cert(cert_name, cert_opts, config)
  cert_ca_account = cert_opts['ca_account'] || config.dig('defaults', 'certs', 'ca_account')
  # cert_ca_name = config.dig('ca_accounts', cert_ca_account, 'ca')
  # cert_ca_identity = config.dig('ca_accounts', cert_ca_account, 'identity')
  #
  # p "Cert #{cert_name}: Finding directory URL for CA"
  # acme_directory_url = config.dig('CAs', cert_ca_name, 'directory_url')
  #
  # p "Cert #{cert_name}: Finding account to use for cert #{cert_name} from CA #{cert_ca_name}"
  # account = config.dig('identities', cert_ca_account_name)
  # email = account['email']
  { cert_name => cert_ca_account }
  #   {
  #     'ca' => cert_ca_name,
  #     'account' => cert_ca_account_name,
  #   },
  # }
end

def make_client_for_ca_account(ca, id)
  p "CA #{ca['name']}: Finding directory URL for CA"
  acme_directory_url = ca['directory_url']

  private_key = read_account_key(id['keyfile'])

  p "CA #{ca['name']}: Creating client object for communication with CA"
  client = Acme::Client.new(private_key: private_key, directory: acme_directory_url)

  email = id['email']

  acme_request_with_retries { client.new_account(contact: "mailto:#{email}", terms_of_service_agreed: true) }
  return client
end

def handle_apex_authzs(apex, authzs, config)
  p "apex: #{apex}"
  primary_ns = config.dig('domains', authzs[0].domain, 'primary_ns') || config.dig('defaults', 'domains', 'primary_ns')
  deploy_dns_tokens_on_apex(apex, authzs, primary_ns, config)
end


config = read_config

cert_dir = config.dig('global', 'cert_dir') || './certs/'
ensure_cert_dir(cert_dir)


certs = config['certs']

domains = certs.map { |_certname, cert_opts| cert_opts['domain_names'] }.flatten
domain_attrs = domains.to_h { |d| [d, {}] }


# we need the apex of each domain because that’s where we will send the dns update packet
# TODO: we don’t really need to group this by apex domain or zone but by TSIG key AND zone
# TODO: we don’t really need the apex but the zone under which that name is on the primary nameserver
# TODO: we only need this when dns challenge is used
domain_apex_threads = {}
domains.map do |d|
  domain_apex_threads[d] = Thread.new(d) do |d|
    p "finding apex for domain #{d}"
    find_apex_domain(d).to_s
  end
end
domain_apex_threads.each(&:join)

domain_attrs.each_key do |domain|
  domain_attrs[domain][:apex] = domain_apex_threads[domain].value
end

apex_domains = domain_attrs.map do |_, v|
  apex = v[:apex]
  domains_under_apex = domain_attrs.filter { |_d, v| v[:apex] == apex }.keys
  [apex, { domains: domains_under_apex }]
end.uniq.to_h


# we want all NS entries for the apex domain for checking whether the challenge was deployed
# TODO: this is only true for domains delegated from a registry
#   a delegation purely done with NS records will work differently
#   in that case "walking the domain" label by label and checking for NS entries might be necessary
#   or maybe just letting it be configurable which NS to check would be better?
#   for now: keeping it simple
apex_domain_ns_threads = {}
apex_domains.keys.map do |d|
  apex_domain_ns_threads[d] = Thread.new(d) do |d|
    p "finding ns for apex domain #{d}"
    res = lookup_ns(d)
    ns_names = res.answer.map do |answer|
      answer.rdata.to_s
    end
  end
end
apex_domain_ns_threads.each(&:join)

apex_domains.each_key do |domain|
  apex_domains[domain][:ns] = apex_domain_ns_threads[domain].value
end


# every cert should have a ca_account associated, use default if not
certs.each_pair do |cert, _opts|
  certs[cert]['ca_account'] ||= config.dig('defaults', 'certs', 'ca_account')
end


# we need to know all ca_accounts in use to create a client for each
ca_accounts = certs.map { |_, opts| opts['ca_account'] }.uniq


# for each ca_account we need to use, we create a client
account_threads = []
ca_accounts.each do |ca_account|
  account_threads << Thread.new(ca_account) do |ca_account|
    ca_name = config.dig('ca_accounts', ca_account, 'ca')
    ca = config.dig('CAs', ca_name)
    ca_identity = config.dig('ca_accounts', ca_account, 'identity')
    identity = config.dig('identities', ca_identity)
    client = make_client_for_ca_account(ca, identity)
    {
      ca_account => client
    }
  end
end
account_threads.each(&:join)

ca_clients = account_threads.map(&:value)
ca_clients = ca_clients[0].merge(*ca_clients[1..])


# for each cert, we send an order
# * with the corresponding client, i.e. using the corresponding CA and account
# * with the corresponding domains
certs.each_pair do |cert_name, cert_opts|
  client = ca_clients[cert_opts['ca_account']]
  domains = cert_opts['domain_names']
  certs[cert_name]['order_thread'] = Thread.new(cert_name, client, domains) do |cert_name, client, domains|
    p "Cert #{cert_name}: Creating order object for cert #{cert_name}"
    acme_request_with_retries { client.new_order(identifiers: domains) }
  end
end

certs.each_pair do |cert_name, cert_opts|
  t = cert_opts['order_thread']
  t.join
  certs[cert_name]['order'] = t.value
end


# from each cert order, we pull the authorizations needed for the domains to the domains attributes hash
certs.each_pair do |_cert, opts|
  order_authorizations = acme_request_with_retries { opts['order'].authorizations }
  order_authorizations.each do |auth|
    domain_attrs[auth.domain]['auth'] = auth
  end
end


# we need a list of all authorizations
all_authorizations = domain_attrs.map do |_domain, attrs|
  attrs['auth']
end


# we group the authorizations by apex to be able to send them all at once
authzs_by_apex = all_authorizations.group_by do |auth|
  domain_attrs[auth.domain][:apex]
end


# we use one thread per apex domain for deploying authorizations
auth_threads = []
authzs_by_apex.each_pair do |apex, authzs|
  auth_threads << Thread.new(apex, authzs) do |apex, authzs|
    handle_apex_authzs(apex, authzs, config)
  end
end
auth_threads.each(&:join)


# for each domain we use one thread to check whether the auth has been propagated
propagation_threads = []
domain_attrs.each_pair do |d, attrs|
  domain_attrs[d][:propagation_thread] = Thread.new(attrs['auth']) do |auth|
    wait_for_challenge_propagation(auth.domain, auth.dns01)
  end
  propagation_threads << domain_attrs[d][:propagation_thread]
end

propagation_threads.each(&:join)


# we make a list of all domains not yet validated
p 'Finding unvalidated domains, initial run'
unvalidated_domains = unvalidated_domains(domain_attrs)
# we loop through that list and request validation until it is empty, i.e. all domains are validated
until unvalidated_domains.empty?
  unvalidated_domains.each_pair do |_, attrs|
    p 'Requesting validation of challenge'
    acme_request_with_retries { attrs['auth'].dns01.request_validation }
  end
  p 'Finding unvalidated domains, next run'
  unvalidated_domains = unvalidated_domains(domain_attrs)
  sleep(0.2)
end


# for each cert, we request the cert
certs.each_pair do |name, opts|
  cert_key = read_cert_key(name)
  get_cert(opts['order'], name, opts['domain_names'], cert_key)
end