Skip to content

Commit 9adab5a

Browse files
committed
removing all python2 support and py2/3 compat clauses.
1 parent c6f8059 commit 9adab5a

12 files changed

+53
-107
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
0.1.0.3, 4/27/2014 -- Documentation of SecureString. setup.py now installs all required dependencies.
99
0.1.0.4, 5/9/2014 -- removed SecureString from code examples, added Installation notes.
1010
0.1.1.0, 5/9/2014 -- fixes Issue #1 (pri:Major) in bitbucket issue tracker (HT @cariaso on bitbucket.org)
11+
0.2.0, 7/7/2020 -- Python3 compat issues, deprecated python2. Thanks to community submissions for the help!

README.rst

+17-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Config styles currently supported::
3030
Json (see SecureJson) -- whole-data encryption only.
3131
serialized dictionaries (see SecureConfig) -- whole-data encryption only.
3232

33-
Please let the maintainer (@nthmost) know if you want to see another type supported.
33+
If you'd like to see another type supported, please file a feature request on GitHub
34+
at https://github.com/nthmost/python-secureconfig
3435

3536
Purpose
3637
-------
@@ -47,9 +48,9 @@ for ConfigParser that allows us to keep 99% of the way we interact with
4748
config files, and simply wraps the decryption step.
4849

4950
Of course, once you have decryption handled, you start to want simplified
50-
ways of encrypting as well.
51+
ways of encrypting as well. That's why secureconfig supports writing new
52+
config files.
5153

52-
That's why secureconfig (as of 0.0.3) supports writing new config files.
5354
See "basic usage" sections below to see how you can easily turn a plaintext
5455
value or file into an encrypted value or file (depending on config style).
5556

@@ -65,9 +66,8 @@ run. (Turn this off using paranoid=False, if you must.)
6566
Finally, secureconfig contains a smattering of deployment utilities found in
6667
secureconfig.utils. Feel free to suggest new ones.
6768

68-
This library can be found at https://bitbucket.org/nthmost/python-secureconfig
69-
7069
Contributions and code/documentation critiques are warmly welcomed.
70+
See the Contribution section below for information.
7171

7272

7373
How secureconfig Works
@@ -137,17 +137,14 @@ The following requirements form the backbone of secureconfig::
137137
cryptography
138138
configparser
139139
cffi
140-
six
141140
pycparser
142141

143-
If you have any problems installing these requirements, please let the
144-
maintainer of this package know at https://bitbucket.com/nthmost/python-secureconfig
142+
If you have any problems installing these requirements, please let us know as a
143+
github issue at https://github.com/nthmost/python-secureconfig
145144

146145
SecureConfigParser
147146
------------------
148147

149-
NEW SINCE 0.1.0:
150-
151148
SecureConfigParser is a subclass of the configparser module's ConfigParser class.
152149

153150
The difference is that, when instantiated via one of the standardized cryptkeeper
@@ -308,20 +305,20 @@ Given the above, SecureString cannot at this time be implicity trusted as
308305
"secure", since so much depends upon how it's used.
309306

310307

311-
Future
312-
------
308+
Contributions
309+
-------------
313310

314-
Planned features include::
311+
The home for SecureConfig is on github:
312+
https://github.com/nthmost/secureconfig
315313

316-
- more automated-deployment-oriented utils
317-
- asymmetric key deployments (e.g. RSA public key encryption)
314+
If you'd like to contribute, please make sure you run the tests (I normally use pytest)
315+
found in the tests/ directory. I might merge fixes into master but I won't update pypi
316+
with a new version unless ALL the tests pass.
318317

318+
If you want to contribute a novel feature, please file it as an issue in the github repo
319+
so we can discuss it first!
319320

320-
CONTACT
321-
-------
322-
323-
Look for @nthmost on bitbucket if you're interested and would like to contribute!
324321
Comments, critiques, and bug reports warmly welcomed. Pull requests encouraged.
325322

326-
--Naomi Most, spring 2014.
323+
--Naomi Most, 2014-2020 and onward.
327324

TODO.txt

-2
This file was deleted.

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
six
21
pyasn1
32
pycrypto
43
cryptography

secureconfig/baseclass.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import absolute_import
2-
import six
32
from ast import literal_eval
43

54
from .cryptkeeper import CryptKeeper, EnvCryptKeeper, FileCryptKeeper, cryptkeeper_access_methods
@@ -79,16 +78,10 @@ def __init__(self, filepath='', rawtxt='', readonly=False, **kwargs):
7978
self.cfg = {}
8079

8180
def _decrypt(self, buf):
82-
if six.PY3:
83-
return self.ck.crypter.decrypt(buf).decode()
84-
else:
85-
return self.ck.crypter.decrypt(buf)
81+
return self.ck.crypter.decrypt(buf).decode()
8682

8783
def _encrypt(self, buf):
88-
if six.PY3:
89-
return self.ck.crypter.encrypt(buf.encode()).decode()
90-
else:
91-
return self.ck.crypter.encrypt(buf)
84+
return self.ck.crypter.encrypt(buf.encode()).decode()
9285

9386
def _fill(self, txt=''):
9487
self.cfg = literal_eval(txt)

secureconfig/cryptkeeper.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from __future__ import print_function, absolute_import
2-
31
import os
4-
import six
52

63
from cryptography.fernet import Fernet, InvalidToken
74

@@ -150,12 +147,8 @@ def _key_exists(self):
150147

151148
def store(self):
152149
"""store currently active key into environment variable"""
153-
if six.PY3:
154-
os.environ[self.env] = self.key.decode()
155-
os.putenv(self.env, self.key.decode())
156-
else:
157-
os.environ[self.env] = self.key
158-
os.putenv(self.env, self.key)
150+
os.environ[self.env] = self.key.decode()
151+
os.putenv(self.env, self.key.decode())
159152

160153
def load(self):
161154
"""retrieve key from environment variable"""

secureconfig/secureconfigparser.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
from __future__ import print_function, absolute_import
2-
31
import sys
4-
import six
52

63
import cryptography
74

85
from .baseclass import cryptkeeper_access_methods
96

10-
if six.PY3:
11-
try:
12-
# New style
13-
from configparser import ConfigParser, NoSectionError, NoOptionError
14-
except ImportError:
15-
# Old style
16-
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
17-
else:
7+
try:
8+
# New style
9+
from configparser import ConfigParser, NoSectionError, NoOptionError
10+
except ImportError:
11+
# Old style
1812
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
1913

2014

@@ -72,18 +66,12 @@ def raw_set(self, sec, key, val):
7266

7367
def raw_items(self, sec):
7468
"""Return the items in a section without decrypting the values."""
75-
if six.PY3:
76-
return ConfigParser.items(self, sec, raw=True)
77-
else:
78-
return ConfigParser.items(self, sec)
69+
return ConfigParser.items(self, sec, raw=True)
7970

8071
def val_decrypt(self, raw_val, **kwargs):
8172
"""Decrypt supplied value if it appears to be encrypted."""
8273
if self.ck and raw_val.startswith(self.ck.sigil):
83-
if six.PY3:
84-
return self.ck.crypter.decrypt(raw_val.split(self.ck.sigil)[1].encode()).decode()
85-
else:
86-
return self.ck.crypter.decrypt(raw_val.split(self.ck.sigil)[1])
74+
return self.ck.crypter.decrypt(raw_val.split(self.ck.sigil)[1].encode()).decode()
8775
else:
8876
return raw_val
8977

@@ -103,10 +91,7 @@ def set(self, sec, key, new_val, encrypt=False):
10391
"""
10492
if not self.has_option(sec, key):
10593
if encrypt:
106-
if six.PY3:
107-
new_val = self.ck.sigil + self.ck.encrypt(new_val.encode()).decode()
108-
else:
109-
new_val = self.ck.sigil + self.ck.encrypt(new_val)
94+
new_val = self.ck.sigil + self.ck.encrypt(new_val.encode()).decode()
11095
return self.raw_set(sec, key, new_val)
11196

11297
old_raw_val = self.raw_get(sec, key)

secureconfig/securestring.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import print_function, absolute_import
2-
import six
31
from .zeromem import zeromem
42

53

@@ -8,10 +6,7 @@ class SecureString(str):
86

97
def __init__(self, anystring):
108
super().__init__()
11-
if six.PY3:
12-
self._string = bytes(anystring, 'utf-8')
13-
else:
14-
self._string = anystring
9+
self._string = bytes(anystring, 'utf-8')
1510

1611
def burn(self):
1712
zeromem(self._string)

setup.py

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def run(self):
7171
install_requires=[
7272
'cryptography',
7373
'configparser',
74-
'six',
7574
'pycrypto',
7675
'pyasn1'
7776
],

tests/cfg_test_out.ini

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[database]
2+
username = some_user
3+
password = CK_FERNET::gAAAAABfBNjwZy9k7BM-Jw_sxniUluQqQEd24B1SdeMXkeIdn5pj2GBdeDkPtayWl9pTBp8AFmuqXMrJK9JBrwUJCbfsoZrCPA==
4+
hostname = some_hostname
5+
port = 3306
6+

tests/test_cryptkeeper.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import print_function
2-
import six
31
import unittest
42
import cryptography
53

@@ -41,38 +39,26 @@ def setUp(self):
4139

4240
def test_FileCK_creates_keyfile(self):
4341
assert(os.path.exists(TEST_KEYFILE_PATH))
44-
if six.PY3:
45-
with open(TEST_KEYFILE_PATH, 'r') as fh:
46-
tmp = fh.read().strip().encode()
47-
fh.close()
48-
assert(self.file_ck.key == tmp)
49-
else:
50-
assert(self.file_ck.key == open(TEST_KEYFILE_PATH, 'r').read().strip())
42+
with open(TEST_KEYFILE_PATH, 'r') as fh:
43+
tmp = fh.read().strip().encode()
44+
fh.close()
45+
assert(self.file_ck.key == tmp)
5146

5247
def test_EnvCK_creates_env(self):
5348
assert(os.environ.get(TEST_KEYENV_NAME, False))
54-
if six.PY3:
55-
assert(os.environ.get(TEST_KEYENV_NAME).encode() == self.env_ck.key)
56-
else:
57-
assert(os.environ.get(TEST_KEYENV_NAME) == self.env_ck.key)
49+
assert(os.environ.get(TEST_KEYENV_NAME).encode() == self.env_ck.key)
5850

5951
def test_EnvCK_from_env(self):
6052
os.putenv('ARBITRARY_ENV_NAME', TEST_KEYSTRING)
6153
env_ck = EnvCryptKeeper('ARBITRARY_ENV_NAME')
62-
if six.PY3:
63-
assert(env_ck.key == os.environ['ARBITRARY_ENV_NAME'].encode())
64-
else:
65-
assert(env_ck.key == os.environ['ARBITRARY_ENV_NAME'])
54+
assert(env_ck.key == os.environ['ARBITRARY_ENV_NAME'].encode())
6655

6756
def test_FileCK_from_file(self):
6857
file_ck = FileCryptKeeper(TEST_KEYFILE_PATH)
69-
if six.PY3:
70-
with open(TEST_KEYFILE_PATH, 'r') as fh:
71-
tmp = fh.read().strip().encode()
72-
fh.close()
73-
assert(file_ck.key == tmp)
74-
else:
75-
assert(file_ck.key == open(TEST_KEYFILE_PATH, 'r').read().strip())
58+
with open(TEST_KEYFILE_PATH, 'r') as fh:
59+
tmp = fh.read().strip().encode()
60+
fh.close()
61+
assert(file_ck.key == tmp)
7662

7763
def test_StringCK_key_eq_key(self):
7864
self.assertEqual(self.string_ck.key, TEST_KEYSTRING)

tests/test_secureconfigparser.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
from __future__ import print_function
2-
31
import unittest
42
import cryptography
5-
import six
6-
if six.PY3:
7-
try:
8-
# New style
9-
from configparser import ConfigParser
10-
except ImportError:
11-
# Old style
12-
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
13-
else:
14-
from ConfigParser import ConfigParser
3+
try:
4+
# New style
5+
from configparser import ConfigParser
6+
except ImportError:
7+
# Old style
8+
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
159

1610

1711
from cryptography.fernet import InvalidToken

0 commit comments

Comments
 (0)