-
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle a greater variety of system states in renew-certs.py
Accommodate more potential starting states, unexpected combinations of existing vs. missing Let's Encrypt files, and the potential that users could run only the `wordpress` role after changes that really require running the `letsencrypt` role. Where possible, run tasks to accommodate these situations. Otherwise print helpful error messages in the more likely error situations.
- Loading branch information
Showing
3 changed files
with
66 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,88 @@ | ||
#!/usr/bin/env python | ||
|
||
from __future__ import print_function | ||
|
||
import os | ||
import sys | ||
import time | ||
|
||
from hashlib import sha1 | ||
from subprocess import CalledProcessError, check_output, STDOUT | ||
|
||
failed = False | ||
letsencrypt_cert_ids = {{ letsencrypt_cert_ids }} | ||
|
||
for site in {{ sites_using_letsencrypt }}: | ||
cert_path = os.path.join('{{ letsencrypt_certs_dir }}', site + '-' + letsencrypt_cert_ids[site] + '.cert') | ||
bundled_cert_path = os.path.join('{{ letsencrypt_certs_dir }}', site + '-bundled.cert') | ||
csr_path = os.path.join('{{ acme_tiny_data_directory }}', 'csrs', '{}-{}.csr'.format(site, letsencrypt_cert_ids[site])) | ||
cert_path = os.path.join('{{ letsencrypt_certs_dir }}', '{}-{}.cert'.format(site, letsencrypt_cert_ids[site])) | ||
bundled_cert_path = os.path.join('{{ letsencrypt_certs_dir }}', '{}-bundled.cert'.format(site)) | ||
|
||
if os.access(cert_path, os.F_OK): | ||
stat = os.stat(cert_path) | ||
print 'Certificate file ' + cert_path + ' already exists' | ||
# Generate or update root cert if needed | ||
if not os.access(csr_path, os.F_OK): | ||
failed = True | ||
print('The required CSR file {} does not exist. This could happen if you changed site_hosts and have ' | ||
'not yet rerun the letsencrypt role. Create the CSR file by running the Trellis server.yml playbook with ' | ||
'`--tags letsencrypt`'.format(csr_path), file=sys.stderr) | ||
continue | ||
|
||
if time.time() - stat.st_mtime < {{ letsencrypt_min_renewal_age }} * 86400 and os.access(bundled_cert_path, os.F_OK): | ||
print ' The certificate is younger than {{ letsencrypt_min_renewal_age }} days. Not creating a new certificate.\n' | ||
continue | ||
elif os.access(cert_path, os.F_OK) and time.time() - os.stat(cert_path).st_mtime < {{ letsencrypt_min_renewal_age }} * 86400: | ||
print('Certificate file {} already exists and is younger than {{ letsencrypt_min_renewal_age }} days. ' | ||
'Not creating a new certificate.'.format(cert_path)) | ||
|
||
else: | ||
cmd = ('/usr/bin/env python {{ acme_tiny_software_directory }}/acme_tiny.py ' | ||
'--quiet ' | ||
'--ca {{ letsencrypt_ca }} ' | ||
'--account-key {{ letsencrypt_account_key }} ' | ||
'--csr {} ' | ||
'--acme-dir {{ acme_tiny_challenges_directory }}' | ||
).format(csr_path) | ||
|
||
print 'Generating certificate for ' + site | ||
try: | ||
cert = check_output(cmd, stderr=STDOUT, shell=True) | ||
except CalledProcessError as e: | ||
failed = True | ||
print('Error while generating certificate for {}\n{}'.format(site, e.output), file=sys.stderr) | ||
continue | ||
else: | ||
with open(cert_path, 'w') as cert_file: | ||
cert_file.write(cert) | ||
|
||
cmd = ('/usr/bin/env python {{ acme_tiny_software_directory }}/acme_tiny.py ' | ||
'--quiet ' | ||
'--ca {{ letsencrypt_ca }} ' | ||
'--account-key {{ letsencrypt_account_key }} ' | ||
'--csr {{ acme_tiny_data_directory }}/csrs/{0}-{1}.csr ' | ||
'--acme-dir {{ acme_tiny_challenges_directory }}' | ||
).format(site, letsencrypt_cert_ids[site]) | ||
print('Created certificate {}'.format(cert_file)) | ||
|
||
try: | ||
cert = check_output(cmd, stderr=STDOUT, shell=True) | ||
except CalledProcessError as e: | ||
# Ensure intermediate cert is available for creating bundled cert | ||
if not os.access('{{ letsencrypt_intermediate_cert_path }}', os.F_OK): | ||
failed = True | ||
print 'Error while generating certificate for ' + site | ||
print e.output | ||
else: | ||
with open(cert_path, 'w') as cert_file: | ||
cert_file.write(cert) | ||
print('The required intermediate cert file {{ letsencrypt_intermediate_cert_path }} does not exist. ' | ||
'This could happen if you have not yet run the letsencrypt role with the latest `letsencrypt_intermediate_cert_path` value. ' | ||
'Try running the Trellis server.yml playbook with `--tags letsencrypt`', file=sys.stderr) | ||
continue | ||
|
||
# Retrieve binary content for root cert, intermediate cert, and bundled cert | ||
with open(cert_path, 'rb') as cert_file: | ||
cert = cert_file.read() | ||
|
||
with open('{{ letsencrypt_intermediate_cert_path }}', 'rb') as intermediate_cert_file: | ||
intermediate_cert = intermediate_cert_file.read() | ||
|
||
new_bundled_needed = True | ||
if os.access(bundled_cert_path, os.F_OK): | ||
with open(bundled_cert_path, 'rb') as bundled_cert_file: | ||
bundled_cert = bundled_cert_file.read() | ||
|
||
with open('{{ letsencrypt_intermediate_cert_path }}') as intermediate_cert_file: | ||
intermediate_cert = intermediate_cert_file.read() | ||
# Compare sha1 hashes of new vs. existing bundled content | ||
new = sha1() | ||
new.update(cert + intermediate_cert) | ||
existing = sha1() | ||
existing.update(bundled_cert) | ||
new_bundled_needed = new.hexdigest() != existing.hexdigest() | ||
|
||
with open(bundled_cert_path, 'w') as bundled_file: | ||
bundled_file.write(''.join([cert, intermediate_cert])) | ||
# Generate or update bundled cert if needed | ||
if new_bundled_needed: | ||
with open(bundled_cert_path, 'wb') as bundled_cert_file: | ||
bundled_cert_file.write(cert + intermediate_cert) | ||
|
||
print 'Created certificate for ' + site | ||
print('Created bundled certificate {}'.format(bundled_cert_path)) | ||
|
||
if failed: | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters