|
42 | 42 | usrdir_key = '/home/' + options.user + '/ssl/keys/'
|
43 | 43 |
|
44 | 44 | # 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) |
47 | 47 |
|
48 | 48 | # Check to see if the latest certificate and the latest key are both the same as the current ones, if so then exit
|
49 | 49 | if current_cert == newest_cert and current_key == newest_key:
|
@@ -74,8 +74,52 @@ def symlink_force(target, link_name):
|
74 | 74 |
|
75 | 75 | else:
|
76 | 76 | 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 | + |
78 | 121 | # Call symlink_force function to replace symlinks with symlinks to the latest certificates
|
| 122 | + addBundle(options.user, newest_cert) |
79 | 123 | symlink_force(newest_cert, current_sym_cert)
|
80 | 124 | symlink_force(newest_key, current_sym_key)
|
81 | 125 |
|
|
0 commit comments