Skip to content

Commit e052e88

Browse files
committed
Now adds the CA bundle as well.
1 parent d582434 commit e052e88

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

README

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ Requires:
66
This script will update symlinks specified inside it to the latest ssl certificates from the relevant accounts on the server.
77
Designed for use with cPanel's AutoSSL. This was created to combat Nginx requiring a direct link to the SSL Certificate while in use as a reverse proxy for Varnish Caching.
88

9-
Currently Configuration is entirely manual inside the python script file.
9+
As an addition the script will now also add the ca bundle from cPanel, using the cPanel UAPI, to the bottom of the certificate file. This is to prevent certificate incomplete errors
10+
and to prevent errors on mobile devices where the device's browser does not store copies of as many trusted Certificate Authorities as a desktop browser does.
11+
12+
Currently Configuration is manual inside the python script file.
1013

nginx_sslupdate.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
usrdir_key = '/home/' + options.user + '/ssl/keys/'
4343

4444
# Use glob to find the most recent certificate file and the most recent key file from the users directory
45-
newest_cert = max(glob.iglob(os.path.join(usrdir_cert, '*.crt')), key=os.path.getctime)
46-
newest_key = max(glob.iglob(os.path.join(usrdir_key, '*.key')), key=os.path.getctime)
45+
newest_cert = max(glob.iglob(os.path.join(usrdir_cert, '*.crt')), key=os.path.getmtime)
46+
newest_key = max(glob.iglob(os.path.join(usrdir_key, '*.key')), key=os.path.getmtime)
4747

4848
# Check to see if the latest certificate and the latest key are both the same as the current ones, if so then exit
4949
if current_cert == newest_cert and current_key == newest_key:
@@ -74,8 +74,52 @@ def symlink_force(target, link_name):
7474

7575
else:
7676
raise e
77-
77+
78+
# Define function to be used for adding the ca bundle to the bottom of the certificate to prevent certificate incomplete errors.
79+
def addBundle(user, cert_file):
80+
81+
# Import subprocess so that the cPanel UAPI can be used
82+
import subprocess
83+
84+
# Ensure the certificate file is formatted for the id
85+
cert_file = cert_file.replace('.crt', '')
86+
cert_file = cert_file.split('/')[-1]
87+
88+
# Fetch the cabundle using the UAPI
89+
print('Fetching cabundle from cPanel using UAPI')
90+
uapi_cmd = "uapi --user=" + user + " SSL fetch_cert_info id=" + cert_file
91+
process = subprocess.Popen(uapi_cmd.split(), stdout=subprocess.PIPE)
92+
output, err = process.communicate()
93+
94+
# Seperate out the response and get the bundles from the response
95+
output = output.split()
96+
bundle_begin = output.index('cabundle:')
97+
bundle_end = output.index('certificate:')
98+
99+
bundle = ""
100+
first = 0
101+
102+
for index in range(int(bundle_begin + 1), bundle_end):
103+
104+
if first == 0:
105+
bundle = '\n' + bundle + output[index]
106+
first = 1
107+
108+
else:
109+
bundle = bundle + " " + output[index]
110+
111+
# Ensure the file is correctly formatted to be appended to the other documents
112+
bundle = bundle.replace("\\n", "\n")
113+
bundle = bundle.replace('"', '')
114+
115+
# Append the bundle to the original certificate file
116+
export = open('/home/' + user + '/ssl/certs/' + cert_file + '.crt', "a")
117+
export.write(bundle)
118+
export.close()
119+
print('Appended to file successfully')
120+
78121
# Call symlink_force function to replace symlinks with symlinks to the latest certificates
122+
addBundle(options.user, newest_cert)
79123
symlink_force(newest_cert, current_sym_cert)
80124
symlink_force(newest_key, current_sym_key)
81125

0 commit comments

Comments
 (0)