|
4 | 4 | import sys
|
5 | 5 | import time
|
6 | 6 |
|
| 7 | +from hashlib import sha1 |
7 | 8 | from subprocess import CalledProcessError, check_output, STDOUT
|
8 | 9 |
|
9 | 10 | certs_dir = '{{ letsencrypt_certs_dir }}'
|
10 | 11 | failed = False
|
11 |
| -sites = {{ wordpress_sites }} |
12 |
| -sites = (k for k, v in sites.items() if 'ssl' in v and v['ssl'].get('enabled', False) and v['ssl'].get('provider', 'manual') == 'letsencrypt') |
13 | 12 |
|
14 |
| -for site in sites: |
| 13 | +for site in {{ sites_using_letsencrypt }}: |
15 | 14 | cert_path = os.path.join(certs_dir, site + '.cert')
|
16 | 15 | bundled_cert_path = os.path.join(certs_dir, site + '-bundled.cert')
|
17 | 16 |
|
| 17 | + with open('{{ acme_tiny_data_directory }}/csrs/{0}.csr'.format(site), 'rb') as f: |
| 18 | + csr_hash = sha1(f.read()).hexdigest() |
| 19 | + |
18 | 20 | if os.access(cert_path, os.F_OK):
|
19 | 21 | stat = os.stat(cert_path)
|
20 | 22 | print 'Certificate file ' + cert_path + ' already exists'
|
21 | 23 |
|
22 |
| - if time.time() - stat.st_mtime < {{ letsencrypt_min_renewal_age }} * 86400: |
23 |
| - print ' The certificate is younger than {{ letsencrypt_min_renewal_age }} days. Not creating a new certificate.\n' |
| 24 | + with open(cert_path, 'r') as f: |
| 25 | + csr_hash_in_cert = f.readline().strip() |
| 26 | + |
| 27 | + if csr_hash == csr_hash_in_cert and time.time() - stat.st_mtime < {{ letsencrypt_min_renewal_age }} * 86400: |
| 28 | + print ' The site hosts are unchanged and the certificate is younger than {{ letsencrypt_min_renewal_age }} days. Not creating a new certificate.\n' |
24 | 29 | continue
|
25 | 30 |
|
26 | 31 | print 'Generating certificate for ' + site
|
|
40 | 45 | print e.output
|
41 | 46 | else:
|
42 | 47 | with open(cert_path, 'w') as cert_file:
|
43 |
| - cert_file.write(cert) |
| 48 | + cert_file.write('\n'.join([csr_hash, cert])) |
44 | 49 |
|
45 | 50 | with open('{{ letsencrypt_intermediate_cert_path }}') as intermediate_cert_file:
|
46 | 51 | intermediate_cert = intermediate_cert_file.read()
|
|
0 commit comments