Files
WPIQ/wo/core/sslutils.py

199 lines
7.5 KiB
Python
Raw Normal View History

2019-09-04 03:07:24 +02:00
import csv
2018-11-13 21:55:59 +01:00
import os
2019-09-04 03:07:24 +02:00
import re
2019-09-03 19:02:00 +02:00
2019-09-04 03:07:24 +02:00
from wo.core.fileutils import WOFileUtils
2018-11-13 21:55:59 +01:00
from wo.core.logging import Log
2019-09-03 19:02:00 +02:00
from wo.core.shellexec import WOShellExec
2019-09-04 03:07:24 +02:00
from wo.core.variables import WOVariables
2018-11-13 21:55:59 +01:00
class SSL:
2019-09-04 03:07:24 +02:00
def getexpirationdays(self, domain, returnonerror=False):
2018-11-13 21:55:59 +01:00
# check if exist
if not os.path.isfile('/etc/letsencrypt/live/{0}/cert.pem'
2019-03-16 10:30:52 +01:00
.format(domain)):
2019-04-08 13:01:13 +02:00
Log.error(self, 'File Not Found: '
'/etc/letsencrypt/live/{0}/cert.pem'
2019-03-16 10:30:52 +01:00
.format(domain), False)
2018-11-13 21:55:59 +01:00
if returnonerror:
return -1
Log.error(self, "Check the WordOps log for more details "
"`tail /var/log/wo/wordops.log` and please try again...")
current_date = WOShellExec.cmd_exec_stdout(self, "date -d \"now\" +%s")
2019-09-03 19:02:00 +02:00
expiration_date = WOShellExec.cmd_exec_stdout(
self, "date -d \""
"$(openssl x509 -in /etc/letsencrypt/live/"
"{0}/cert.pem -text -noout | grep \"Not After\" "
"| cut -c 25-)\" +%s"
.format(domain))
2018-11-13 21:55:59 +01:00
2019-03-16 10:30:52 +01:00
days_left = int((int(expiration_date) - int(current_date)) / 86400)
2018-11-13 21:55:59 +01:00
if (days_left > 0):
return days_left
else:
# return "Certificate Already Expired ! Please Renew soon."
return -1
2019-09-04 03:07:24 +02:00
def getexpirationdate(self, domain):
2018-11-13 21:55:59 +01:00
# check if exist
2019-09-06 14:27:45 +02:00
if os.path.islink('/var/www/{0}/conf/nginx/ssl.conf'):
split_domain = domain.split('.')
domain = ('.').join(split_domain[1:])
2018-11-13 21:55:59 +01:00
if not os.path.isfile('/etc/letsencrypt/live/{0}/cert.pem'
2019-03-16 10:30:52 +01:00
.format(domain)):
2019-04-08 13:01:13 +02:00
Log.error(self, 'File Not Found: /etc/letsencrypt/'
'live/{0}/cert.pem'
2019-03-16 10:30:52 +01:00
.format(domain), False)
2018-11-13 21:55:59 +01:00
Log.error(self, "Check the WordOps log for more details "
"`tail /var/log/wo/wordops.log` and please try again...")
2019-09-03 19:02:00 +02:00
expiration_date = WOShellExec.cmd_exec_stdout(
self, "date -d \"$(/usr/bin/openssl x509 -in "
"/etc/letsencrypt/live/{0}/cert.pem -text -noout | grep "
"\"Not After\" | cut -c 25-)\" "
.format(domain))
2018-11-13 21:55:59 +01:00
return expiration_date
2019-09-04 03:07:24 +02:00
def siteurlhttps(self, domain):
wo_site_webroot = ('/var/www/{0}'.format(domain))
WOFileUtils.chdir(
self, '{0}/htdocs/'.format(wo_site_webroot))
if WOShellExec.cmd_exec(
self, "{0} --allow-root core is-installed"
.format(WOVariables.wo_wp_cli)):
wo_siteurl = (
WOShellExec.cmd_exec_stdout(
self, "{0} option get siteurl "
.format(WOVariables.wo_wpcli_path) +
"--allow-root --quiet"))
test_url = re.split(":", wo_siteurl)
if not (test_url[0] == 'https'):
2019-09-05 12:31:34 +02:00
Log.wait(self, "Updating site url with https")
try:
WOShellExec.cmd_exec(
self, "{0} option update siteurl "
"\'https://{1}\' --allow-root".format(
WOVariables.wo_wpcli_path, domain))
WOShellExec.cmd_exec(
self, "{0} option update home "
"\'https://{1}\' --allow-root".format(
WOVariables.wo_wpcli_path, domain))
WOShellExec.cmd_exec(
self, "{0} search-replace \'http://{0}\'"
"\'https://{0}\' --skip-columns=guid "
"--skip-tables=wp_users"
.format(domain))
except Exception as e:
Log.debug(self, str(e))
Log.failed(self, "Updating site url with https")
else:
Log.valide(self, "Updating site url with https")
2019-09-04 03:07:24 +02:00
# check if a wildcard exist to secure a new subdomain
def checkwildcardexist(self, wo_domain_name):
wo_acme_exec = ("/etc/letsencrypt/acme.sh --config-home "
"'/etc/letsencrypt/config'")
# export certificates list from acme.sh
WOShellExec.cmd_exec(
self, "{0} ".format(wo_acme_exec) +
"--list --listraw > /var/lib/wo/cert.csv")
# define new csv dialect
csv.register_dialect('acmeconf', delimiter='|')
# open file
certfile = open('/var/lib/wo/cert.csv', mode='r', encoding='utf-8')
reader = csv.reader(certfile, 'acmeconf')
wo_wildcard_domain = ("*.{0}".format(wo_domain_name))
for row in reader:
if wo_wildcard_domain in row[2]:
iswildcard = True
break
else:
iswildcard = False
certfile.close()
return iswildcard
2019-09-06 14:27:45 +02:00
def setupHsts(self, wo_domain_name):
Log.info(
self, "Adding /var/www/{0}/conf/nginx/hsts.conf"
.format(wo_domain_name))
hstsconf = open("/var/www/{0}/conf/nginx/hsts.conf"
.format(wo_domain_name),
encoding='utf-8', mode='w')
hstsconf.write("more_set_headers "
"\"Strict-Transport-Security: "
"max-age=31536000; "
"includeSubDomains; "
"preload\";")
hstsconf.close()
return 0
2019-09-06 16:13:46 +02:00
def selfsignedcert(self, proftpd=False, backend=False):
2019-09-06 14:27:45 +02:00
"""issue a self-signed certificate"""
selfs_tmp = '/var/lib/wo/tmp/selfssl'
# create self-signed tmp directory
if not os.path.isdir(selfs_tmp):
2019-09-06 14:50:44 +02:00
WOFileUtils.mkdir(self, selfs_tmp)
2019-09-06 14:27:45 +02:00
try:
WOShellExec.cmd_exec(
self, "openssl genrsa -out "
"{0}/ssl.key 2048"
.format(selfs_tmp))
WOShellExec.cmd_exec(
self, "openssl req -new -batch "
2019-09-06 16:13:46 +02:00
"-subj /commonName=localhost/ "
"-key {0}/ssl.key -out {0}/ssl.csr"
.format(selfs_tmp))
2019-09-06 14:27:45 +02:00
WOFileUtils.mvfile(
self, "{0}/ssl.key"
.format(selfs_tmp),
"{0}/ssl.key.org"
.format(selfs_tmp))
WOShellExec.cmd_exec(
self, "openssl rsa -in "
"{0}/ssl.key.org -out "
"{0}/ssl.key"
.format(selfs_tmp))
WOShellExec.cmd_exec(
self, "openssl x509 -req -days "
"3652 -in {0}/ssl.csr -signkey {0}"
"/ssl.key -out {0}/ssl.crt"
.format(selfs_tmp))
except Exception as e:
Log.debug(self, "{0}".format(e))
Log.error(
self, "Failed to generate HTTPS "
"certificate for 22222", False)
if backend:
WOFileUtils.mvfile(
self, "{0}/ssl.key"
.format(selfs_tmp),
"/var/www/22222/cert/22222.key")
WOFileUtils.mvfile(
2019-09-06 16:01:35 +02:00
self, "{0}/ssl.crt"
2019-09-06 14:27:45 +02:00
.format(selfs_tmp),
"/var/www/22222/cert/22222.crt")
2019-09-06 16:13:46 +02:00
if proftpd:
2019-09-06 14:27:45 +02:00
WOFileUtils.mvfile(
self, "{0}/ssl.key"
.format(selfs_tmp),
2019-09-06 16:13:46 +02:00
"/etc/proftpd/ssl/proftpd.key")
2019-09-06 14:27:45 +02:00
WOFileUtils.mvfile(
self, "{0}/ssl.crt"
.format(selfs_tmp),
2019-09-06 16:13:46 +02:00
"/etc/proftpd/ssl/proftpd.crt")
2019-09-06 14:27:45 +02:00
# remove self-signed tmp directory
WOFileUtils.rm(self, selfs_tmp)