Skip to content

Commit

Permalink
Merge pull request #1318 from privacyidea/python3_migrate_stuff
Browse files Browse the repository at this point in the history
Fix several issues with Python 3
  • Loading branch information
fredreichbier committed Nov 26, 2018
2 parents 89f3ebc + 5f12bca commit 4da61fa
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 97 deletions.
47 changes: 25 additions & 22 deletions privacyidea/lib/auditmodules/sqlaudit.py
Expand Up @@ -49,6 +49,7 @@
from sqlalchemy import asc, desc, and_, or_
import datetime
import traceback
from six import string_types


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -137,7 +138,7 @@ def _truncate_data(self):
for column, l in column_length.items():
if column in self.audit_data:
data = self.audit_data[column]
if isinstance(data, basestring):
if isinstance(data, string_types):
if column == "policies":
# The policies column is shortend per comma entry
data = truncate_comma_list(data, l)
Expand Down Expand Up @@ -313,25 +314,27 @@ def _log_to_string(le):
Note: Not all elements of the LogEntry are used to generate the
string (the Signature is not!), otherwise we could have used pickle
:param le: LogEntry object containing the data
:type le: LogEntry
:rtype str
"""
s = "id=%s,date=%s,action=%s,succ=%s,serial=%s,t=%s,u=%s,r=%s,adm=%s,"\
"ad=%s,i=%s,ps=%s,c=%s,l=%s,cl=%s" % (le.id,
le.date,
le.action,
le.success,
le.serial,
le.token_type,
le.user,
le.realm,
le.administrator,
le.action_detail,
le.info,
le.privacyidea_server,
le.client,
le.loglevel,
le.clearance_level)
if type(s) == unicode:
s = s.encode("utf-8")
s = u"id=%s,date=%s,action=%s,succ=%s,serial=%s,t=%s,u=%s,r=%s,adm=%s," \
u"ad=%s,i=%s,ps=%s,c=%s,l=%s,cl=%s" % (le.id,
le.date,
le.action,
le.success,
le.serial,
le.token_type,
le.user,
le.realm,
le.administrator,
le.action_detail,
le.info,
le.privacyidea_server,
le.client,
le.loglevel,
le.clearance_level)
return s

@staticmethod
Expand Down Expand Up @@ -416,12 +419,12 @@ def search(self, search_dict, page_size=15, page=1, sortorder="asc",
page=page, sortorder=sortorder,
timelimit=timelimit)
try:
le = auditIter.next()
le = next(auditIter)
while le:
# Fill the list
paging_object.auditdata.append(self.audit_entry_to_dict(le))
le = auditIter.next()
except StopIteration:
le = next(auditIter)
except StopIteration as _e:
log.debug("Interation stopped.")

return paging_object
Expand Down
32 changes: 16 additions & 16 deletions privacyidea/lib/caconnectors/localca.py
Expand Up @@ -38,6 +38,9 @@
import re
import logging
import os
from six import string_types
from six.moves import input

log = logging.getLogger(__name__)

CA_SIGN = "openssl ca -keyfile {cakey} -cert {cacert} -config {config} " \
Expand Down Expand Up @@ -495,7 +498,7 @@ def revoke_cert(self, certificate, reason=CRL_REASONS[0]):
:return: Returns the serial number of the revoked certificate. Otherwise
an error is raised.
"""
if type(certificate) in [basestring, unicode, str]:
if isinstance(certificate, string_types):
cert_obj = crypto.load_certificate(crypto.FILETYPE_PEM, certificate)
elif type(certificate) == crypto.X509:
cert_obj = certificate
Expand Down Expand Up @@ -595,41 +598,38 @@ def create_ca(cls, name):
config = CONFIG(name)

while 1:
directory = raw_input("In which directory do you want to create "
"the CA [{0!s}]: ".format(config.directory))
directory = input("In which directory do you want to create "
"the CA [{0!s}]: ".format(config.directory))
config.directory = directory or config.directory
if not config.directory.startswith("/"):
config.directory = os.path.abspath(config.directory)

keysize = raw_input(
"What should be the keysize of the CA (2048/4096/8192) [{"
"0!s}]: ".format(config.keysize))
keysize = input("What should be the keysize of the CA (2048/4096/8192)"
"[{0!s}]: ".format(config.keysize))
config.keysize = keysize or config.keysize

validity_ca = raw_input("How many days should the CA be valid ["
"{0!s}]: ".format(config.validity_ca))
validity_ca = input("How many days should the CA be valid ["
"{0!s}]: ".format(config.validity_ca))
config.validity_ca = validity_ca or config.validity_ca

dn = raw_input("What is the DN of the CA [{0!s}]: ".format(
config.dn))
dn = input("What is the DN of the CA [{0!s}]: ".format(config.dn))
config.dn = dn or config.dn
# At the moment we do not use this. This would be written to the
# templates file.
#validity_cert = raw_input(
# "What should be the validity period of enrolled certificates in days [{0!s}]: ".format(
# config.validity_cert))
#config.validity_cert = validity_cert or config.validity_cert
crl_days = raw_input("How many days should the CRL be valid [{"
"0!s}]: ".format(config.crl_days))
crl_days = input("How many days should the CRL be valid "
"[{0!s}]: ".format(config.crl_days))
config.crl_days = crl_days or config.crl_days
crl_overlap = raw_input(
"What should be the overlap period of the CRL in days [{"
"0!s}]: ".format(config.crl_overlap))
crl_overlap = input("What should be the overlap period of the CRL in days "
"[{0!s}]: ".format(config.crl_overlap))
config.crl_overlap = crl_overlap or config.crl_overlap

print("="*60)
print("{0!s}".format(config))
answer = raw_input("Is this configuration correct? [y/n] ")
answer = input("Is this configuration correct? [y/n] ")
if answer.lower() == "y":
break

Expand Down
10 changes: 5 additions & 5 deletions privacyidea/lib/config.py
Expand Up @@ -50,6 +50,7 @@
from .utils import reload_db, is_true
import importlib
import datetime
from six import with_metaclass, string_types

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -82,14 +83,13 @@ def __call__(cls, *args, **kwargs):
return cls._instances[cls]


class ConfigClass(object):
class ConfigClass(with_metaclass(Singleton, object)):
"""
The Config_Object will contain all database configuration of system
config, resolvers and realm.
It will be created at the beginning of the request and is supposed to stay
alive unchanged during the request.
"""
__metaclass__ = Singleton

def __init__(self):
"""
Expand Down Expand Up @@ -198,7 +198,7 @@ def get_config(self, key=None, default=None, role="admin",
pass
if isinstance(r_config, int):
r_config = r_config > 0
if isinstance(r_config, basestring):
if isinstance(r_config, string_types):
r_config = is_true(r_config.lower())

return r_config
Expand Down Expand Up @@ -347,8 +347,8 @@ def get_token_class(tokentype):
def get_token_types():
"""
Return a simple list of the type names of the tokens.
:return: array of tokentypes like 'hotp', 'totp'...
:rtype: array
:return: list of tokentypes like 'hotp', 'totp'...
:rtype: list
"""
if "pi_token_types" not in this.config:
(t_classes, t_types) = get_token_class_dict()
Expand Down
32 changes: 24 additions & 8 deletions privacyidea/lib/crypto.py
Expand Up @@ -37,6 +37,7 @@
This lib.cryto is tested in tests/test_lib_crypto.py
"""
from __future__ import division
import hmac
import logging
from hashlib import sha256
Expand All @@ -62,7 +63,10 @@
import passlib.hash
import sys
import traceback
from six import PY2, text_type

if not PY2:
long = int

FAILED_TO_DECRYPT_PASSWORD = "FAILED TO DECRYPT PASSWORD!"

Expand Down Expand Up @@ -490,7 +494,9 @@ def geturandom(length=20, hex=False):
get random - from the security module
:param length: length of the returned bytes - default is 20 bytes
:rtype length: int
:type length: int
:param hex: convert result to hexstring
:type hex: bool
:return: buffer of bytes
Expand All @@ -517,10 +523,10 @@ def random():
:return: float value
"""
# get a binary random string
randbin = geturandom(urandom.precision)
randhex = geturandom(urandom.precision, hex=True)

# convert this to an integer
randi = int(randbin.encode('hex'), 16) * 1.0
randi = int(randhex, 16) * 1.0

# get the max integer
intmax = 2 ** (8 * urandom.precision) * 1.0
Expand Down Expand Up @@ -628,8 +634,8 @@ def get_rand_digit_str(length=16):
if length == 1:
raise ValueError("get_rand_digit_str only works for values > 1")
clen = int(length / 2.4 + 0.5)
randd = geturandom(clen)
s = "{0:d}".format((int(randd.encode('hex'), 16)))
randd = geturandom(clen, hex=True)
s = "{0:d}".format((int(randd, 16)))
if len(s) < length:
s = "0" * (length - len(s)) + s
elif len(s) > length:
Expand All @@ -646,8 +652,7 @@ def get_alphanum_str(length=16):
"""
ret = ""
for i in range(length):
ret += random.choice(string.lowercase + string.uppercase +
string.digits)
ret += random.choice(string.ascii_letters + string.digits)
return ret


Expand Down Expand Up @@ -704,9 +709,13 @@ def sign(self, s):
"""
Create a signature of the string s
:param s: String to sign
:type s: str
:return: The signature of the string
:rtype: long
"""
if isinstance(s, text_type):
s = s.encode('utf8')
RSAkey = RSA.importKey(self.private)
if SIGN_WITH_RSA:
hashvalue = HashFunc.new(s).digest()
Expand All @@ -720,7 +729,14 @@ def sign(self, s):
def verify(self, s, signature):
"""
Check the signature of the string s
:param s: String to check
:type s: str
:param signature: the signature to compare
:type signature: str
"""
if isinstance(s, text_type):
s = s.encode('utf8')
r = False
try:
RSAkey = RSA.importKey(self.public)
Expand All @@ -731,7 +747,7 @@ def verify(self, s, signature):
else:
hashvalue = HashFunc.new(s)
pkcs1_15.new(RSAkey).verify(hashvalue, signature)
except Exception: # pragma: no cover
except Exception as _e: # pragma: no cover
log.error("Failed to verify signature: {0!r}".format(s))
log.debug("{0!s}".format(traceback.format_exc()))
return r
Expand Down
7 changes: 4 additions & 3 deletions privacyidea/lib/machines/base.py
Expand Up @@ -25,6 +25,7 @@
This file is tested in tests/test_lib_machines.py
"""
import netaddr
from six import string_types


class Machine(object):
Expand All @@ -39,7 +40,7 @@ def __init__(self, resolver_name, machine_id, hostname=None, ip=None):
self.id = machine_id
self.resolver_name = resolver_name
self.hostname = hostname
if type(ip) in [basestring, str, unicode]:
if isinstance(ip, string_types):
self.ip = netaddr.IPAddress(ip)
else:
self.ip = ip
Expand All @@ -55,7 +56,7 @@ def has_hostname(self, hostname):
"""
if type(self.hostname) == list:
return hostname in self.hostname
elif type(self.hostname) in [basestring, str, unicode]:
elif isinstance(self.hostname, string_types):
return hostname.lower() == self.hostname.lower()

def has_ip(self, ip):
Expand All @@ -68,7 +69,7 @@ def has_ip(self, ip):
:return: True or false
"""
# convert to IPAddress
if type(ip) in [basestring, str, unicode]:
if isinstance(ip, string_types):
ip = netaddr.IPAddress(ip)

if type(self.ip) == list:
Expand Down

0 comments on commit 4da61fa

Please sign in to comment.