Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to Python3 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions contrail/security/ca/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__license__ = "BSD - see LICENSE file in top-level directory"
__contact__ = "[email protected]"
__revision__ = "$Id$"
from ConfigParser import ConfigParser, SafeConfigParser
from configparser import ConfigParser, SafeConfigParser
from os import path
from abc import ABCMeta, abstractmethod
import logging
Expand All @@ -27,17 +27,15 @@ class CertificateIssuingError(CertificateAuthorityError):
"""Error issuing a certificate"""


class AbstractCertificateAuthority(object):
class AbstractCertificateAuthority(object, metaclass=ABCMeta):
"""Provide basic functionality for a Certificate Authority"""
CERTIFICATE_VERSION2 = 1
CERTIFICATE_VERSION3 = 2
CERT_FILEPATH_OPTNAME = "cert_filepath"
PRIKEY_FILEPATH_OPTNAME = "key_filepath"
PRIKEY_PASSWD_OPTNAME = "key_passwd"
SERIAL_NUM_DEFAULT = 0L
SERIAL_NUM_DEFAULT = 0
MIN_KEY_NBITS_DEFAULT = 2048

__metaclass__ = ABCMeta
__slots__ = (
'__cert',
'__key',
Expand Down Expand Up @@ -85,7 +83,7 @@ def parse_config(self, cfg, prefix='', section='DEFAULT'):
@param section: configuration file section from which to extract
parameters.
'''
if isinstance(cfg, basestring):
if isinstance(cfg, str):
config_file_path = path.expandvars(cfg)
here_dir = path.dirname(config_file_path)
_cfg = SafeConfigParser(defaults={'here':here_dir})
Expand Down Expand Up @@ -114,7 +112,7 @@ def parse_config(self, cfg, prefix='', section='DEFAULT'):
cert_filepath = kw.pop(cert_filepath_opt)
prikey_filepath = kw.pop(prikey_filepath_opt)

except KeyError, e:
except KeyError as e:
raise CertificateAuthorityConfigError('Missing option from config '
'%s' % str(e))

Expand Down Expand Up @@ -153,7 +151,7 @@ def parse_keywords(self, prefix='', **kw):
variable names. However, they may prefixed with <prefix>
"""
prefix_len = len(prefix)
for opt_name, val in kw.items():
for opt_name, val in list(kw.items()):
if prefix:
# Filter attributes based on prefix
if opt_name.startswith(prefix):
Expand Down Expand Up @@ -261,18 +259,18 @@ def serial_num_counter(self):

@serial_num_counter.setter
def serial_num_counter(self, value):
if not isinstance(value, (long, int)):
if not isinstance(value, int):
raise TypeError('Expecting int or long type for '
'"serial_num_counter" got %r type' % type(value))
self.__serial_num_counter = long(value)
self.__serial_num_counter = int(value)

@property
def serial_filepath(self):
return self.__serial_filepath

@serial_filepath.setter
def serial_filepath(self, value):
if not isinstance(value, basestring):
if not isinstance(value, str):
raise TypeError('Expecting string type for "serial_filepath" '
'got %r type' % type(value))
self.__serial_filepath = value
Expand All @@ -284,17 +282,17 @@ def min_key_nbits(self):

@min_key_nbits.setter
def min_key_nbits(self, value):
if not isinstance(value, (long, int, basestring)):
if not isinstance(value, (int, str)):
raise TypeError('Expecting int or long type for "min_key_nbits" '
'got %r type' % type(value))
self.__min_key_nbits = long(value)
self.__min_key_nbits = int(value)

def _read_serial_file(self):
'''Read serial number from serial file'''
serial_file = open(self.serial_filepath, 'r')

try:
self.serial_num_counter = long(serial_file.read())
self.serial_num_counter = int(serial_file.read())
finally:
serial_file.close()

Expand Down
2 changes: 1 addition & 1 deletion contrail/security/ca/callout_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def cert_issue_cmd(self):

@cert_issue_cmd.setter
def cert_issue_cmd(self, value):
if not isinstance(value, basestring):
if not isinstance(value, str):
raise TypeError('Expecting string type for "cert_issue_cmd" '
'got %r type' % type(value))
self.__cert_issue_cmd = value
2 changes: 1 addition & 1 deletion contrail/security/ca/cert_req.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def create_cert_req(dn,
cert_req = crypto.X509Req()
subj = cert_req.get_subject()

for k, v in dn.items():
for k, v in list(dn.items()):
setattr(subj, k, v)

# Create public key object
Expand Down
22 changes: 11 additions & 11 deletions contrail/security/ca/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def not_before_time_nsecs(self):

@not_before_time_nsecs.setter
def not_before_time_nsecs(self, value):
if not isinstance(value, (long, int, basestring)):
if not isinstance(value, (int, str)):
raise TypeError('Expecting int, long or string type for '
'"not_before_time_nsecs" got %r type' % type(value))

Expand All @@ -66,7 +66,7 @@ def not_after_time_nsecs(self):

@not_after_time_nsecs.setter
def not_after_time_nsecs(self, value):
if not isinstance(value, (long, int, basestring)):
if not isinstance(value, (int, str)):
raise TypeError('Expecting int, long or string type for '
'"not_after_time_nsecs" got %r type' % type(value))

Expand All @@ -78,7 +78,7 @@ def digest(self):

@digest.setter
def digest(self, value):
if not isinstance(value, basestring):
if not isinstance(value, str):
raise TypeError('Expecting string type for "digest" '
'got %r type' % type(value))
self.__digest = value
Expand All @@ -89,7 +89,7 @@ def certificate_version(self):

@certificate_version.setter
def certificate_version(self, value):
if not isinstance(value, basestring):
if not isinstance(value, str):
raise TypeError('Expecting string type for "certificate_version" '
'got %r type' % type(value))
self.__certificate_version = value
Expand All @@ -101,11 +101,11 @@ def ca_true(self):

@ca_true.setter
def ca_true(self, value):
if isinstance(value, basestring):
if isinstance(value, str):
self.__ca_true = value.lower() in ('1', 'true')

elif isinstance(value, (long, int)):
self.__ca_true = long(value)
elif isinstance(value, int):
self.__ca_true = int(value)
else:
raise TypeError('Expecting int or long type for '
'"ca_true" got %r type' % type(value))
Expand All @@ -117,11 +117,11 @@ def subject_alt_name(self):

@subject_alt_name.setter
def subject_alt_name(self, value):
if isinstance(value, basestring):
if isinstance(value, str):
self.__subject_alt_name = value.lower() in ('1', 'true')

elif isinstance(value, (long, int)):
self.__subject_alt_name = long(value)
elif isinstance(value, int):
self.__subject_alt_name = int(value)
else:
raise TypeError('Expecting int or long type for '
'"ca_true" got %r type' % type(value))
Expand Down Expand Up @@ -195,7 +195,7 @@ def issue_certificate(
x509_extensions = [basic_constraints_ext]

# Check for a subject alt names extension, if present add as is.
if isinstance(subject_alt_name, basestring):
if isinstance(subject_alt_name, str):
subject_alt_name_ext = crypto.X509Extension('subjectAltName',
False,
subject_alt_name)
Expand Down
2 changes: 1 addition & 1 deletion contrail/security/ca/test/gen_ca_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def gen_ca_cert(dn, years_validity=5):
ca_cert.set_version(x509_version - 1)
ca_cert.set_serial_number(1)
subj = ca_cert.get_subject()
for k, v in dn.items():
for k, v in list(dn.items()):
setattr(subj, k, v)

ca_cert.gmtime_adj_notBefore(0)
Expand Down
2 changes: 1 addition & 1 deletion contrail/security/ca/test/test_ca_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test02_check_ext(self):
print(ext_dat)
dec = decode(ext_dat, asn1Spec=GeneralNames())
print(dec)
print(dec[0].prettyPrint())
print((dec[0].prettyPrint()))
for i in range(len(dec[0])):
dns_name = str(
dec[0].getComponentByPosition(i).getComponent())
Expand Down