forked from basho/riak-python-client
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_security.py
More file actions
176 lines (162 loc) · 6.81 KB
/
Copy pathtest_security.py
File metadata and controls
176 lines (162 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright 2010-present Basho Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import unittest
from riak.security import SecurityCreds
from riak.tests import (
RUN_SECURITY,
SECURITY_BAD_CERT,
SECURITY_CACERT,
SECURITY_CERT,
SECURITY_CERT_USER,
SECURITY_CIPHERS,
SECURITY_KEY,
SECURITY_PASSWD,
SECURITY_REVOKED,
SECURITY_USER,
)
from riak.tests.base import IntegrationTestBase
class SecurityTests(IntegrationTestBase, unittest.TestCase):
@unittest.skipIf(RUN_SECURITY, "RUN_SECURITY is 1")
def test_security_disabled(self):
"""
Test valid security settings without security enabled
"""
topts = {"timeout": 1}
# NB: can"t use SECURITY_CREDS here since they won"t be set
# if RUN_SECURITY is UN-set
creds = SecurityCreds(username="foo", password="bar")
client = self.create_client(credentials=creds,
transport_options=topts)
myBucket = client.bucket("test")
val1 = "foobar"
key1 = myBucket.new("x", data=val1)
with self.assertRaises(Exception):
key1.store()
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_basic_connection(self):
myBucket = self.client.bucket("test")
val1 = "foobar"
key1 = myBucket.new("x", data=val1)
key1.store()
myBucket.get("x")
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_bad_user(self):
creds = SecurityCreds(username="foo",
password=SECURITY_PASSWD,
cacert_file=SECURITY_CACERT,
ciphers=SECURITY_CIPHERS)
client = self.create_client(credentials=creds)
with self.assertRaises(Exception):
client.get_buckets()
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_bad_password(self):
creds = SecurityCreds(username=SECURITY_USER,
password="foo",
cacert_file=SECURITY_CACERT,
ciphers=SECURITY_CIPHERS)
client = self.create_client(credentials=creds)
with self.assertRaises(Exception):
client.get_buckets()
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_invalid_cert(self):
creds = SecurityCreds(username=SECURITY_USER,
password=SECURITY_PASSWD,
cacert_file="/tmp/foo",
ciphers=SECURITY_CIPHERS)
client = self.create_client(credentials=creds)
with self.assertRaises(Exception):
client.get_buckets()
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_password_without_cacert(self):
creds = SecurityCreds(username=SECURITY_USER,
password=SECURITY_PASSWD,
ciphers=SECURITY_CIPHERS)
client = self.create_client(credentials=creds)
with self.assertRaises(Exception):
myBucket = client.bucket("test")
val1 = "foobar"
key1 = myBucket.new("x", data=val1)
key1.store()
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_cert_authentication(self):
creds = SecurityCreds(username=SECURITY_CERT_USER,
ciphers=SECURITY_CIPHERS,
cert_file=SECURITY_CERT,
pkey_file=SECURITY_KEY,
cacert_file=SECURITY_CACERT)
client = self.create_client(credentials=creds)
myBucket = client.bucket("test")
val1 = "foobar2"
key1 = myBucket.new("x", data=val1)
# Certificate Authentication is currently only supported
# by Protocol Buffers
if self.protocol == "pbc":
key1.store()
myBucket.get("x")
else:
with self.assertRaises(Exception):
key1.store()
myBucket.get("x")
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_revoked_cert(self):
creds = SecurityCreds(username=SECURITY_USER,
password=SECURITY_PASSWD,
ciphers=SECURITY_CIPHERS,
cacert_file=SECURITY_CACERT,
crl_file=SECURITY_REVOKED)
# Currently Python >= 2.7.9 and Python 3.x native CRL doesn"t seem to
# work as advertised
if sys.version_info >= (2, 7, 9):
return
client = self.create_client(credentials=creds)
with self.assertRaises(Exception):
client.get_buckets()
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_bad_ca_cert(self):
creds = SecurityCreds(username=SECURITY_USER, password=SECURITY_PASSWD,
ciphers=SECURITY_CIPHERS,
cacert_file=SECURITY_BAD_CERT)
client = self.create_client(credentials=creds)
with self.assertRaises(Exception):
client.get_buckets()
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_ciphers(self):
creds = SecurityCreds(username=SECURITY_USER, password=SECURITY_PASSWD,
ciphers=SECURITY_CIPHERS,
cacert_file=SECURITY_CACERT)
client = self.create_client(credentials=creds)
myBucket = client.bucket("test")
val1 = "foobar"
key1 = myBucket.new("x", data=val1)
key1.store()
myBucket.get("x")
client.close()
@unittest.skipUnless(RUN_SECURITY, "RUN_SECURITY is 0")
def test_security_bad_ciphers(self):
creds = SecurityCreds(username=SECURITY_USER, password=SECURITY_PASSWD,
cacert_file=SECURITY_CACERT,
ciphers="ECDHE-RSA-AES256-GCM-SHA384")
client = self.create_client(credentials=creds)
with self.assertRaises(Exception):
client.get_buckets()
client.close()