From dd6d3321660cce462d57bf7a0c9ac4f1c47352d6 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Mon, 21 Nov 2016 14:03:04 -0800 Subject: [PATCH] Removing python2 compatibility. (#518) --- lemur/auth/service.py | 14 +++----------- lemur/auth/views.py | 14 +++----------- lemur/common/utils.py | 7 +------ lemur/manage.py | 8 -------- lemur/plugins/lemur_java/tests/test_java.py | 5 ++--- lemur/utils.py | 9 +-------- setup.py | 1 - 7 files changed, 10 insertions(+), 48 deletions(-) diff --git a/lemur/auth/service.py b/lemur/auth/service.py index 0542960a..00778dbf 100644 --- a/lemur/auth/service.py +++ b/lemur/auth/service.py @@ -8,7 +8,6 @@ .. moduleauthor:: Kevin Glisson """ -import sys import jwt import json import binascii @@ -40,12 +39,8 @@ def get_rsa_public_key(n, e): :param e: :return: a RSA Public Key in PEM format """ - if sys.version_info >= (3, 0): - n = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(n, 'utf-8'))), 16) - e = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(e, 'utf-8'))), 16) - else: - n = int(binascii.hexlify(jwt.utils.base64url_decode(str(n))), 16) - e = int(binascii.hexlify(jwt.utils.base64url_decode(str(e))), 16) + n = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(n, 'utf-8'))), 16) + e = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(e, 'utf-8'))), 16) pub = RSAPublicNumbers(e, n).public_key(default_backend()) return pub.public_bytes( @@ -128,10 +123,7 @@ def fetch_token_header(token): raise jwt.DecodeError('Not enough segments') try: - if sys.version_info >= (3, 0): - return json.loads(jwt.utils.base64url_decode(header_segment).decode('utf-8')) - else: - return json.loads(jwt.utils.base64url_decode(header_segment)) + return json.loads(jwt.utils.base64url_decode(header_segment).decode('utf-8')) except TypeError as e: current_app.logger.exception(e) raise jwt.DecodeError('Invalid header padding') diff --git a/lemur/auth/views.py b/lemur/auth/views.py index 40f6c032..655b1e12 100644 --- a/lemur/auth/views.py +++ b/lemur/auth/views.py @@ -5,7 +5,6 @@ :license: Apache, see LICENSE for more details. .. moduleauthor:: Kevin Glisson """ -import sys import jwt import base64 import requests @@ -143,12 +142,8 @@ class Ping(Resource): # the secret and cliendId will be given to you when you signup for the provider token = '{0}:{1}'.format(args['clientId'], current_app.config.get("PING_SECRET")) - if sys.version_info >= (3, 0): - basic = base64.b64encode(bytes(token, 'utf-8')) - headers = {'authorization': 'basic {0}'.format(basic.decode('utf-8'))} - else: - basic = base64.b64encode(token, 'utf-8') - headers = {'authorization': 'basic {0}'.format(basic)} + basic = base64.b64encode(bytes(token, 'utf-8')) + headers = {'authorization': 'basic {0}'.format(basic.decode('utf-8'))} # exchange authorization code for access token. @@ -172,10 +167,7 @@ class Ping(Resource): # validate your token based on the key it was signed with try: - if sys.version_info >= (3, 0): - jwt.decode(id_token, secret.decode('utf-8'), algorithms=[algo], audience=args['clientId']) - else: - jwt.decode(id_token, secret, algorithms=[algo], audience=args['clientId']) + jwt.decode(id_token, secret.decode('utf-8'), algorithms=[algo], audience=args['clientId']) except jwt.DecodeError: return dict(message='Token is invalid'), 403 except jwt.ExpiredSignatureError: diff --git a/lemur/common/utils.py b/lemur/common/utils.py index 56bb9083..507dcda2 100644 --- a/lemur/common/utils.py +++ b/lemur/common/utils.py @@ -6,8 +6,6 @@ .. moduleauthor:: Kevin Glisson """ -import six -import sys import string import random @@ -37,10 +35,7 @@ def get_psuedo_random_string(): def parse_certificate(body): - if sys.version_info[0] <= 2: - return x509.load_pem_x509_certificate(bytes(body), default_backend()) - - if isinstance(body, six.string_types): + if isinstance(body, str): body = body.encode('utf-8') return x509.load_pem_x509_certificate(body, default_backend()) diff --git a/lemur/manage.py b/lemur/manage.py index e49cadb0..9c0cdd80 100755 --- a/lemur/manage.py +++ b/lemur/manage.py @@ -477,14 +477,6 @@ def unlock(path=None): sys.stdout.write("[+] Keys have been unencrypted!\n") -def unicode_(data): - import sys - - if sys.version_info.major < 3: - return data.decode('UTF-8') - return data - - def print_certificate_details(details): """ Print the certificate details with formatting. diff --git a/lemur/plugins/lemur_java/tests/test_java.py b/lemur/plugins/lemur_java/tests/test_java.py index 037031e7..6cf8ff5f 100644 --- a/lemur/plugins/lemur_java/tests/test_java.py +++ b/lemur/plugins/lemur_java/tests/test_java.py @@ -1,5 +1,4 @@ import pytest -import six from lemur.tests.vectors import INTERNAL_CERTIFICATE_A_STR, INTERNAL_PRIVATE_KEY_A_STR @@ -24,7 +23,7 @@ def test_export_truststore_default_password(app): actual = p.export(INTERNAL_CERTIFICATE_A_STR, "", "", options) assert actual[0] == 'jks' - assert isinstance(actual[1], six.string_types) + assert isinstance(actual[1], str) assert isinstance(actual[2], bytes) @@ -56,5 +55,5 @@ def test_export_keystore_default_password(app): actual = p.export(INTERNAL_CERTIFICATE_A_STR, "", INTERNAL_PRIVATE_KEY_A_STR, options) assert actual[0] == 'jks' - assert isinstance(actual[1], six.string_types) + assert isinstance(actual[1], str) assert isinstance(actual[2], bytes) diff --git a/lemur/utils.py b/lemur/utils.py index 31e810eb..09608267 100644 --- a/lemur/utils.py +++ b/lemur/utils.py @@ -6,8 +6,6 @@ .. moduleauthor:: Kevin Glisson """ import os -import sys -import six from flask import current_app from cryptography.fernet import Fernet, MultiFernet import sqlalchemy.types as types @@ -97,11 +95,8 @@ class Vault(types.TypeDecorator): if not value: return - if sys.version_info[0] <= 2: - return MultiFernet(self.keys).encrypt(bytes(value)) - # ensure bytes for fernet - if isinstance(value, six.string_types): + if isinstance(value, str): value = value.encode('utf-8') return MultiFernet(self.keys).encrypt(value) @@ -122,6 +117,4 @@ class Vault(types.TypeDecorator): if not value: return - if sys.version_info[0] <= 2: - return MultiFernet(self.keys).decrypt(value) return MultiFernet(self.keys).decrypt(value).decode('utf8') diff --git a/setup.py b/setup.py index 4cae3e0a..1e1ba70d 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,6 @@ install_requires = [ 'requests==2.11.1', 'psycopg2==2.6.1', 'arrow==0.7.0', - 'six==1.10.0', 'gunicorn==19.4.1', 'marshmallow-sqlalchemy==0.8.0', 'marshmallow==2.4.0',