Debian Codegraph @

keystoneclient/tests/unit/test_keyring.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import datetime
14 from unittest import mock
15
16 from oslo_utils import timeutils
17
18 from keystoneclient import access
19 from keystoneclient import httpclient
20 from keystoneclient.tests.unit import utils
21 from keystoneclient.tests.unit.v2_0 import client_fixtures
22 from keystoneclient import utils as client_utils
23
24 try:
25 import keyring # noqa
26 import pickle # noqa
27 except ImportError:
28 keyring = None
29
30
31 PROJECT_SCOPED_TOKEN = client_fixtures.project_scoped_token()
32
33 # These mirror values from PROJECT_SCOPED_TOKEN
34 USERNAME = 'exampleuser'
35 AUTH_URL = 'http://public.com:5000/v2.0'
36 TOKEN = '04c7d5ffaeef485f9dc69c06db285bdb'
37
38 PASSWORD = 'password'
39 TENANT = 'tenant'
40 TENANT_ID = 'tenant_id'
41
42
43 class KeyringTest(utils.TestCase):
44
45 def setUp(self):
46 if keyring is None:
47 self.skipTest(
48 'optional package keyring or pickle is not installed')
49
50 class MemoryKeyring(keyring.backend.KeyringBackend):
51 """A Simple testing keyring.
52
53 This class supports stubbing an initial password to be returned by
54 setting password, and allows easy password and key retrieval. Also
55 records if a password was retrieved.
56 """
57
58 def __init__(self):
59 self.key = None
60 self.password = None
61 self.fetched = False
62 self.get_password_called = False
63 self.set_password_called = False
64
65 def supported(self):
66 return 1
67
68 def get_password(self, service, username):
69 self.get_password_called = True
70 key = username + '@' + service
71 # make sure we don't get passwords crossed if one is enforced.
72 if self.key and self.key != key:
73 return None
74 if self.password:
75 self.fetched = True
76 return self.password
77
78 def set_password(self, service, username, password):
79 self.set_password_called = True
80 self.key = username + '@' + service
81 self.password = password
82
83 super(KeyringTest, self).setUp()
84 self.memory_keyring = MemoryKeyring()
85 keyring.set_keyring(self.memory_keyring)
86
87 def test_no_keyring_key(self):
88 """Test case when no keyring set.
89
90 Ensure that if we don't have use_keyring set in the client that
91 the keyring is never accessed.
92 """
93 # Creating a HTTPClient not using session is deprecated.
94 with self.deprecations.expect_deprecations_here():
95 cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
96 project_id=TENANT_ID, auth_url=AUTH_URL)
97
98 # stub and check that a new token is received
99 method = 'get_raw_token_from_identity_service'
100 with mock.patch.object(cl, method) as meth:
101 meth.return_value = (True, PROJECT_SCOPED_TOKEN)
102
103 self.assertTrue(cl.authenticate())
104
105 self.assertEqual(1, meth.call_count)
106
107 # make sure that we never touched the keyring
108 self.assertFalse(self.memory_keyring.get_password_called)
109 self.assertFalse(self.memory_keyring.set_password_called)
110
111 def test_build_keyring_key(self):
112 # Creating a HTTPClient not using session is deprecated.
113 with self.deprecations.expect_deprecations_here():
114 cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
115 project_id=TENANT_ID, auth_url=AUTH_URL)
116
117 keyring_key = cl._build_keyring_key(auth_url=AUTH_URL,
118 username=USERNAME,
119 tenant_name=TENANT,
120 tenant_id=TENANT_ID,
121 token=TOKEN)
122
123 self.assertEqual(keyring_key,
124 '%s/%s/%s/%s/%s' %
125 (AUTH_URL, TENANT_ID, TENANT, TOKEN, USERNAME))
126
127 def test_set_and_get_keyring_expired(self):
128 # Creating a HTTPClient not using session is deprecated.
129 with self.deprecations.expect_deprecations_here():
130 cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
131 project_id=TENANT_ID, auth_url=AUTH_URL,
132 use_keyring=True)
133
134 # set an expired token into the keyring
135 auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
136 expired = timeutils.utcnow() - datetime.timedelta(minutes=30)
137 auth_ref['token']['expires'] = client_utils.isotime(expired)
138 self.memory_keyring.password = pickle.dumps(auth_ref)
139
140 # stub and check that a new token is received, so not using expired
141 method = 'get_raw_token_from_identity_service'
142 with mock.patch.object(cl, method) as meth:
143 meth.return_value = (True, PROJECT_SCOPED_TOKEN)
144
145 self.assertTrue(cl.authenticate())
146
147 self.assertEqual(1, meth.call_count)
148
149 # check that a value was returned from the keyring
150 self.assertTrue(self.memory_keyring.fetched)
151
152 # check that the new token has been loaded into the keyring
153 new_auth_ref = pickle.loads(self.memory_keyring.password)
154 self.assertEqual(new_auth_ref['token']['expires'],
155 PROJECT_SCOPED_TOKEN['access']['token']['expires'])
156
157 def test_get_keyring(self):
158 # Creating a HTTPClient not using session is deprecated.
159 with self.deprecations.expect_deprecations_here():
160 cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
161 project_id=TENANT_ID, auth_url=AUTH_URL,
162 use_keyring=True)
163
164 # set an token into the keyring
165 auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
166 future = timeutils.utcnow() + datetime.timedelta(minutes=30)
167 auth_ref['token']['expires'] = client_utils.isotime(future)
168 self.memory_keyring.password = pickle.dumps(auth_ref)
169
170 # don't stub get_raw_token so will fail if authenticate happens
171
172 self.assertTrue(cl.authenticate())
173 self.assertTrue(self.memory_keyring.fetched)
174
175 def test_set_keyring(self):
176 # Creating a HTTPClient not using session is deprecated.
177 with self.deprecations.expect_deprecations_here():
178 cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
179 project_id=TENANT_ID, auth_url=AUTH_URL,
180 use_keyring=True)
181
182 # stub and check that a new token is received
183 method = 'get_raw_token_from_identity_service'
184 with mock.patch.object(cl, method) as meth:
185 meth.return_value = (True, PROJECT_SCOPED_TOKEN)
186
187 self.assertTrue(cl.authenticate())
188
189 self.assertEqual(1, meth.call_count)
190
191 # we checked the keyring, but we didn't find anything
192 self.assertTrue(self.memory_keyring.get_password_called)
193 self.assertFalse(self.memory_keyring.fetched)
194
195 # check that the new token has been loaded into the keyring
196 self.assertTrue(self.memory_keyring.set_password_called)
197 new_auth_ref = pickle.loads(self.memory_keyring.password)
198 self.assertEqual(new_auth_ref.auth_token, TOKEN)
199 self.assertEqual(new_auth_ref['token'],
200 PROJECT_SCOPED_TOKEN['access']['token'])
201 self.assertEqual(new_auth_ref.username, USERNAME)