mirror of
https://github.com/moepman/acertmgr.git
synced 2024-06-14 14:02:35 +02:00
idna: unify usage as tools function
This commit is contained in:
parent
6a07ab1188
commit
7a019d1ac9
|
@ -12,14 +12,8 @@ import hashlib
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
|
|
||||||
from acertmgr.tools import log
|
from acertmgr.tools import log, idna_convert
|
||||||
|
|
||||||
try:
|
|
||||||
import idna
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Backward compatiblity for older versions/installations of acertmgr
|
# Backward compatiblity for older versions/installations of acertmgr
|
||||||
LEGACY_WORK_DIR = "/etc/acme"
|
LEGACY_WORK_DIR = "/etc/acme"
|
||||||
|
@ -63,28 +57,6 @@ def update_config_value(config, name, localconfig, globalconfig, default):
|
||||||
config[name] = globalconfig.get(name, default)
|
config[name] = globalconfig.get(name, default)
|
||||||
|
|
||||||
|
|
||||||
# @brief convert domain list to idna representation (if applicable
|
|
||||||
def idna_convert(domainlist):
|
|
||||||
if 'idna' in sys.modules and any(ord(c) >= 128 for c in ''.join(domainlist)):
|
|
||||||
domaintranslation = list()
|
|
||||||
for domain in domainlist:
|
|
||||||
if any(ord(c) >= 128 for c in domain):
|
|
||||||
# Translate IDNA domain name from a unicode domain (handle wildcards separately)
|
|
||||||
if domain.startswith('*.'):
|
|
||||||
idna_domain = "*.{}".format(idna.encode(domain[2:]).decode('utf-8'))
|
|
||||||
else:
|
|
||||||
idna_domain = idna.encode(domain).decode('utf-8')
|
|
||||||
result = idna_domain, domain
|
|
||||||
else:
|
|
||||||
result = domain, domain
|
|
||||||
domaintranslation.append(result)
|
|
||||||
return domaintranslation
|
|
||||||
else:
|
|
||||||
if 'idna' not in sys.modules:
|
|
||||||
log("Unicode domain(s) found but IDNA names could not be translated due to missing idna module", error=True)
|
|
||||||
return list()
|
|
||||||
|
|
||||||
|
|
||||||
# @brief parse authority from config
|
# @brief parse authority from config
|
||||||
def parse_authority(localconfig, globalconfig, runtimeconfig):
|
def parse_authority(localconfig, globalconfig, runtimeconfig):
|
||||||
authority = {}
|
authority = {}
|
||||||
|
|
|
@ -226,7 +226,7 @@ def get_cert_domains(cert):
|
||||||
for d in san_cert.value:
|
for d in san_cert.value:
|
||||||
domains.add(d.value)
|
domains.add(d.value)
|
||||||
# Convert IDNA domain to correct representation and return the list
|
# Convert IDNA domain to correct representation and return the list
|
||||||
return [x.encode('idna').decode('ascii') if any(ord(c) >= 128 for c in x) else x for x in domains]
|
return [x for x, _ in idna_convert(domains)]
|
||||||
|
|
||||||
|
|
||||||
# @brief determine certificate cn
|
# @brief determine certificate cn
|
||||||
|
@ -354,3 +354,25 @@ def target_is_current(target, file):
|
||||||
target_date = os.path.getmtime(target)
|
target_date = os.path.getmtime(target)
|
||||||
crt_date = os.path.getmtime(file)
|
crt_date = os.path.getmtime(file)
|
||||||
return target_date >= crt_date
|
return target_date >= crt_date
|
||||||
|
|
||||||
|
|
||||||
|
# @brief convert domain list to idna representation (if applicable
|
||||||
|
def idna_convert(domainlist):
|
||||||
|
if 'idna' in sys.modules and any(ord(c) >= 128 for c in ''.join(domainlist)):
|
||||||
|
domaintranslation = list()
|
||||||
|
for domain in domainlist:
|
||||||
|
if any(ord(c) >= 128 for c in domain):
|
||||||
|
# Translate IDNA domain name from a unicode domain (handle wildcards separately)
|
||||||
|
if domain.startswith('*.'):
|
||||||
|
idna_domain = "*.{}".format(domain[2:].encode('idna').decode('ascii'))
|
||||||
|
else:
|
||||||
|
idna_domain = domain.encode('idna').decode('ascii')
|
||||||
|
result = idna_domain, domain
|
||||||
|
else:
|
||||||
|
result = domain, domain
|
||||||
|
domaintranslation.append(result)
|
||||||
|
return domaintranslation
|
||||||
|
else:
|
||||||
|
if 'idna' not in sys.modules:
|
||||||
|
log("Unicode domain(s) found but IDNA names could not be translated due to missing idna module", error=True)
|
||||||
|
return [(x, x) for x in domainlist]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user