1
0
mirror of https://github.com/moepman/acertmgr.git synced 2024-11-14 17:25:26 +01:00

Fix python3 compatibility

This commit is contained in:
Kishi85 2019-01-21 16:18:47 +01:00
parent 1c939363c0
commit 1ce94491fd
3 changed files with 29 additions and 20 deletions

View File

@ -6,7 +6,7 @@
# Copyright (c) Rudolf Mayerhofer, 2019. # Copyright (c) Rudolf Mayerhofer, 2019.
# available under the ISC license, see LICENSE # available under the ISC license, see LICENSE
import io
import grp import grp
import importlib import importlib
import os import os
@ -93,7 +93,7 @@ def cert_get(settings):
print("Reading account key...") print("Reading account key...")
acme.register_account() acme.register_account()
crt = acme.get_crt_from_csr(cr, config['domainlist'], challenge_handlers) crt = acme.get_crt_from_csr(cr, config['domainlist'], challenge_handlers)
with open(crt_file, "w") as crt_fd: with io.open(crt_file, "w") as crt_fd:
crt_fd.write(tools.convert_cert_to_pem(crt)) crt_fd.write(tools.convert_cert_to_pem(crt))
# if resulting certificate is valid: store in final location # if resulting certificate is valid: store in final location
@ -127,20 +127,20 @@ def cert_put(settings):
key_file = settings['key_file'] key_file = settings['key_file']
crt_final = settings['cert_file'] crt_final = settings['cert_file']
with open(crt_path, "w+") as crt_fd: with io.open(crt_path, "w+") as crt_fd:
for fmt in crt_format: for fmt in crt_format:
if fmt == "crt": if fmt == "crt":
src_fd = open(crt_final, "r") src_fd = io.open(crt_final, "r")
crt_fd.write(src_fd.read()) crt_fd.write(src_fd.read())
src_fd.close() src_fd.close()
if fmt == "key": if fmt == "key":
src_fd = open(key_file, "r") src_fd = io.open(key_file, "r")
crt_fd.write(src_fd.read()) crt_fd.write(src_fd.read())
src_fd.close() src_fd.close()
if fmt == "ca": if fmt == "ca":
if not os.path.isfile(ca_file): if not os.path.isfile(ca_file):
raise FileNotFoundError("The CA certificate file (%s) is missing!" % ca_file) raise FileNotFoundError("The CA certificate file (%s) is missing!" % ca_file)
src_fd = open(ca_file, "r") src_fd = io.open(ca_file, "r")
crt_fd.write(src_fd.read()) crt_fd.write(src_fd.read())
src_fd.close() src_fd.close()
else: else:

View File

@ -11,6 +11,7 @@ import io
import os import os
import tools import tools
import io
ACME_DIR = "/etc/acme" ACME_DIR = "/etc/acme"
ACME_CONF = os.path.join(ACME_DIR, "acme.conf") ACME_CONF = os.path.join(ACME_DIR, "acme.conf")
@ -143,7 +144,7 @@ def load():
globalconfig = dict() globalconfig = dict()
# load global configuration # load global configuration
if os.path.isfile(ACME_CONF): if os.path.isfile(ACME_CONF):
with open(ACME_CONF) as config_fd: with io.open(ACME_CONF) as config_fd:
try: try:
import json import json
globalconfig = json.load(config_fd) globalconfig = json.load(config_fd)
@ -156,7 +157,7 @@ def load():
# load domain configuration # load domain configuration
for config_file in os.listdir(ACME_CONFD): for config_file in os.listdir(ACME_CONFD):
if config_file.endswith(".conf"): if config_file.endswith(".conf"):
with open(os.path.join(ACME_CONFD, config_file)) as config_fd: with io.open(os.path.join(ACME_CONFD, config_file)) as config_fd:
try: try:
import json import json
for entry in json.load(config_fd).items(): for entry in json.load(config_fd).items():

View File

@ -11,12 +11,14 @@ import binascii
import datetime import datetime
import os import os
import hashlib import hashlib
import io
import six
from cryptography import x509 from cryptography import x509
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID,ExtensionOID from cryptography.x509.oid import NameOID, ExtensionOID
try: try:
from urllib.request import urlopen # Python 3 from urllib.request import urlopen # Python 3
@ -32,8 +34,8 @@ class InvalidCertificateError(Exception):
# @param cert_file the path to the certificate # @param cert_file the path to the certificate
# @return the tuple of dates: (notBefore, notAfter) # @return the tuple of dates: (notBefore, notAfter)
def get_cert_valid_times(cert_file): def get_cert_valid_times(cert_file):
with open(cert_file, 'r') as f: with io.open(cert_file, 'r') as f:
cert_data = f.read() cert_data = f.read().encode('utf-8')
cert = x509.load_pem_x509_certificate(cert_data, default_backend()) cert = x509.load_pem_x509_certificate(cert_data, default_backend())
return cert.not_valid_before, cert.not_valid_after return cert.not_valid_before, cert.not_valid_after
@ -64,8 +66,14 @@ def is_cert_valid(crt_file, ttl_days):
# @param key the key to use with the certificate in pyopenssl format # @param key the key to use with the certificate in pyopenssl format
# @return the CSR in pyopenssl format # @return the CSR in pyopenssl format
def new_cert_request(names, key): def new_cert_request(names, key):
primary_name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, names[0].decode('utf8'))]) # TODO: There has to be a better way to ensure correct text type (why typecheck, cryptography?)
all_names = x509.SubjectAlternativeName([x509.DNSName(name.decode('utf8')) for name in names]) primary_name = x509.Name([x509.NameAttribute(
NameOID.COMMON_NAME,
names[0] if isinstance(names[0], six.text_type) else names[0].decode('utf-8'))
])
all_names = x509.SubjectAlternativeName([x509.DNSName(
name if isinstance(name, six.text_type) else name.decode('utf-8')
) for name in names])
req = x509.CertificateSigningRequestBuilder() req = x509.CertificateSigningRequestBuilder()
req = req.subject_name(primary_name) req = req.subject_name(primary_name)
req = req.add_extension(all_names, critical=False) req = req.add_extension(all_names, critical=False)
@ -86,7 +94,7 @@ def new_rsa_key(path, key_size=4096):
format=serialization.PrivateFormat.TraditionalOpenSSL, format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption() encryption_algorithm=serialization.NoEncryption()
) )
with open(path, 'wb') as pem_out: with io.open(path, 'wb') as pem_out:
pem_out.write(pem) pem_out.write(pem)
try: try:
os.chmod(path, int("0400", 8)) os.chmod(path, int("0400", 8))
@ -98,8 +106,8 @@ def new_rsa_key(path, key_size=4096):
# @param cert_file certificate file # @param cert_file certificate file
# @param ca_file destination for the ca file # @param ca_file destination for the ca file
def download_issuer_ca(cert_file, ca_file): def download_issuer_ca(cert_file, ca_file):
with open(cert_file, 'r') as f: with io.open(cert_file, 'r') as f:
cert_data = f.read() cert_data = f.read().encode('utf-8')
cert = x509.load_pem_x509_certificate(cert_data, default_backend()) cert = x509.load_pem_x509_certificate(cert_data, default_backend())
aia = cert.extensions.get_extension_for_oid(ExtensionOID.AUTHORITY_INFORMATION_ACCESS) aia = cert.extensions.get_extension_for_oid(ExtensionOID.AUTHORITY_INFORMATION_ACCESS)
@ -116,7 +124,7 @@ def download_issuer_ca(cert_file, ca_file):
cadata = urlopen(ca_issuers).read() cadata = urlopen(ca_issuers).read()
cacert = x509.load_der_x509_certificate(cadata, default_backend()) cacert = x509.load_der_x509_certificate(cadata, default_backend())
pem = cacert.public_bytes(encoding=serialization.Encoding.PEM) pem = cacert.public_bytes(encoding=serialization.Encoding.PEM)
with open(ca_file, 'wb') as pem_out: with io.open(ca_file, 'wb') as pem_out:
pem_out.write(pem) pem_out.write(pem)
@ -131,8 +139,8 @@ def convert_cert_to_pem(cert):
# @param path path to key file # @param path path to key file
# @return the key in pyopenssl format # @return the key in pyopenssl format
def read_key(path): def read_key(path):
with open(path, 'r') as f: with io.open(path, 'r') as f:
key_data = f.read() key_data = f.read().encode('utf-8')
return serialization.load_pem_private_key(key_data, None, default_backend()) return serialization.load_pem_private_key(key_data, None, default_backend())
@ -156,4 +164,4 @@ def byte_string_format(num):
# @param data data to convert to id # @param data data to convert to id
# @return unique id string # @return unique id string
def to_unique_id(data): def to_unique_id(data):
return hashlib.md5(data).hexdigest() return hashlib.md5(data.encode('utf-8')).hexdigest()