Skip to content

Commit 9357170

Browse files
author
Brett Hazen
committed
A few more @seancribbs-inspired changes
- Add `setup_test` command to run `create_bucket_types` and `setup_security` - Add _create_credentials to RiakClient - Only store credentials in the RiakClient
1 parent 063204c commit 9357170

File tree

8 files changed

+77
-27
lines changed

8 files changed

+77
-27
lines changed

commands.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
distutils commands for riak-python-client
33
"""
44

5-
__all__ = ['create_bucket_types', 'setup_security', 'preconfig_security']
5+
__all__ = ['create_bucket_types', 'setup_security', 'preconfig_security',
6+
'setup_tests']
67

78
from distutils import log
89
from distutils.core import Command
@@ -206,13 +207,16 @@ class setup_security(Command):
206207

207208
def initialize_options(self):
208209
self.riak_admin = None
209-
# Default values:
210-
self.username = 'testuser'
211-
self.password = 'testpassword'
210+
self.username = None
211+
self.password = None
212212

213213
def finalize_options(self):
214214
if self.riak_admin is None:
215215
raise DistutilsOptionError("riak-admin option not set")
216+
if self.username is None:
217+
self.username = 'testuser'
218+
if self.password is None:
219+
self.password = 'testpassword'
216220

217221
def run(self):
218222
if self._check_available():
@@ -386,3 +390,39 @@ def _backup_file(self, name):
386390
shutil.copyfile(name, backup)
387391
else:
388392
log.info("Cannot backup missing file {!r}".format(name))
393+
394+
395+
class setup_tests(Command):
396+
"""
397+
Sets up security configuration.
398+
399+
* Run setup_security and create_bucket_types
400+
"""
401+
402+
description = "create bucket types and security settings for testing"
403+
404+
user_options = create_bucket_types.user_options + setup_security.user_options
405+
406+
def initialize_options(self):
407+
self.riak_admin = None
408+
self.username = None
409+
self.password = None
410+
pass
411+
412+
def finalize_options(self):
413+
bucket = self.distribution.get_command_obj('create_bucket_types')
414+
bucket.riak_admin = self.riak_admin
415+
security = self.distribution.get_command_obj('setup_security')
416+
security.riak_admin = self.riak_admin
417+
security.username = self.username
418+
security.password = self.password
419+
pass
420+
421+
def run(self):
422+
# Run all relevant sub-commands.
423+
for cmd_name in self.get_sub_commands():
424+
self.run_command(cmd_name)
425+
426+
sub_commands = [('create_bucket_types', None),
427+
('setup_security', None)
428+
]

riak/client/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from riak.search import RiakSearch
3535
from riak.transports.http import RiakHttpPool
3636
from riak.transports.pbc import RiakPbcPool
37+
from riak.security import SecurityCreds
3738
from riak.util import deprecated
3839
from riak.util import deprecateQuorumAccessors
3940
from riak.util import lazy_property
@@ -100,7 +101,7 @@ def __init__(self, protocol='http', transport_options={}, nodes=None,
100101

101102
self.protocol = protocol or 'http'
102103
self.resolver = default_resolver
103-
self.credentials = credentials
104+
self._credentials = self._create_credentials(credentials)
104105
self._http_pool = RiakHttpPool(self, **transport_options)
105106
self._pb_pool = RiakPbcPool(self, **transport_options)
106107

@@ -317,6 +318,20 @@ def _create_node(self, n):
317318
raise TypeError("%s is not a valid node configuration"
318319
% repr(n))
319320

321+
def _create_credentials(self, n):
322+
"""
323+
Create security credentials, if necessary.
324+
"""
325+
if not n:
326+
return n
327+
elif isinstance(n, SecurityCreds):
328+
return n
329+
elif isinstance(n, dict):
330+
return SecurityCreds(**n)
331+
else:
332+
raise TypeError("%s is not a valid security configuration"
333+
% repr(n))
334+
320335
def _choose_node(self, nodes=None):
321336
"""
322337
Chooses a random node from the list of nodes in the client,

riak/transports/http/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def __init__(self, client, **options):
5050
self.connection_class = httplib.HTTPSConnection
5151
else:
5252
self.connection_class = NoNagleHTTPConnection
53-
# TODO: Rationalize protocol and security credentials
54-
if self.client.credentials:
53+
if self.client._credentials:
5554
self.connection_class = RiakHTTPSConnection
5655

5756
super(RiakHttpPool, self).__init__()

riak/transports/http/connection.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def _request(self, method, uri, headers={}, body='', stream=False):
3535
headers.setdefault('Accept',
3636
'multipart/mixed, application/json, */*;q=0.5')
3737

38-
if self._credentials:
39-
self._security_auth_headers(self._credentials.username,
40-
self._credentials.password,
38+
if self._client._credentials:
39+
self._security_auth_headers(self._client._credentials.username,
40+
self._client._credentials.password,
4141
headers)
4242

4343
try:
@@ -60,13 +60,13 @@ def _connect(self):
6060
"""
6161
Use the appropriate connection class; optionally with security.
6262
"""
63-
if self._credentials:
63+
if self._client._credentials:
6464
self._connection = self._connection_class(self._node.host,
65-
self._node.https_port,
66-
self._credentials)
65+
self._node.http_port,
66+
self._client._credentials)
6767
else:
6868
self._connection = self._connection_class(self._node.host,
69-
self._node.https_port)
69+
self._node.http_port)
7070
# Forces the population of stats and resources before any
7171
# other requests are made.
7272
self.server_version
@@ -83,7 +83,6 @@ def close(self):
8383
# These are set by the RiakHttpTransport initializer
8484
_connection_class = httplib.HTTPConnection
8585
_node = None
86-
credentials = None
8786

8887
def _security_auth_headers(self, username, password, headers):
8988
"""

riak/transports/http/transport.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ def __init__(self, node=None,
5858
super(RiakHttpTransport, self).__init__()
5959

6060
self._client = client
61-
if self._client:
62-
self._credentials = self._client.credentials
6361
self._node = node
6462
self._connection_class = connection_class
6563
self._client_id = client_id

riak/transports/pbc/connection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def _auth(self):
9696
auth request/response to prevent denial of service attacks
9797
"""
9898
req = riak_pb.RpbAuthReq()
99-
req.user = self._credentials.username
100-
req.password = self._credentials.password
99+
req.user = self._client._credentials.username
100+
req.password = self._client._credentials.password
101101
msg_code, _ = self._non_connect_request(MSG_CODE_AUTH_REQ, req,
102102
MSG_CODE_AUTH_RESP)
103103
if msg_code == MSG_CODE_AUTH_RESP:
@@ -112,9 +112,9 @@ def _ssl_handshake(self):
112112
taken place with Riak
113113
returns True upon success, otherwise an exception is raised
114114
"""
115-
if self._credentials:
116-
ssl_ctx = OpenSSL.SSL.Context(self._credentials.ssl_version)
117-
cacert_file = self._credentials.cacert_file
115+
if self._client._credentials:
116+
ssl_ctx = OpenSSL.SSL.Context(self._client._credentials.ssl_version)
117+
cacert_file = self._client._credentials.cacert_file
118118
try:
119119
ssl_ctx.load_verify_locations(cacert_file)
120120
# attempt to upgrade the socket to SSL
@@ -170,7 +170,7 @@ def _connect(self):
170170
self._timeout)
171171
else:
172172
self._socket = socket.create_connection(self._address)
173-
if self._credentials and not self._secure_connection:
173+
if self._client._credentials and not self._secure_connection:
174174
self._check_security()
175175

176176
def close(self):

riak/transports/pbc/transport.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ def __init__(self,
9191
super(RiakPbcTransport, self).__init__()
9292

9393
self._client = client
94-
if self._client:
95-
self._credentials = self._client.credentials
9694
self._node = node
9795
self._address = (node.host, node.pb_port)
9896
self._timeout = timeout

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup, find_packages
44
from version import get_version
55
from commands import create_bucket_types, setup_security, \
6-
preconfig_security
6+
preconfig_security, setup_tests
77

88
install_requires = ["riak_pb >=2.0.0", "pyOpenSSL >= 0.14"]
99
requires = ["riak_pb(>=2.0.0)", "pyOpenSSL(>=0.14)"]
@@ -31,7 +31,8 @@
3131
url='https://github.com/basho/riak-python-client',
3232
cmdclass={'create_bucket_types': create_bucket_types,
3333
'setup_security': setup_security,
34-
'preconfig_security': preconfig_security},
34+
'preconfig_security': preconfig_security,
35+
'setup_tests': setup_tests},
3536
classifiers=['License :: OSI Approved :: Apache Software License',
3637
'Intended Audience :: Developers',
3738
'Operating System :: OS Independent',

0 commit comments

Comments
 (0)