summaryrefslogtreecommitdiff
path: root/keystone/tests/unit/test_backend_ldap_pool.py
blob: 1c4b198045abe646e645af9b98b528d781f190ad (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright 2012 OpenStack Foundation
# Copyright 2013 IBM Corp.
#
# 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.

from unittest import mock

import fixtures
import ldappool

from keystone.common import provider_api
import keystone.conf
from keystone.identity.backends import ldap
from keystone.identity.backends.ldap import common as common_ldap
from keystone.tests import unit
from keystone.tests.unit import fakeldap
from keystone.tests.unit import test_backend_ldap


CONF = keystone.conf.CONF
PROVIDERS = provider_api.ProviderAPIs


class LdapPoolCommonTestMixin(object):
    """LDAP pool specific common tests used here and in live tests."""

    def cleanup_pools(self):
        common_ldap.PooledLDAPHandler.connection_pools.clear()

    def test_handler_with_use_pool_enabled(self):
        # by default use_pool and use_auth_pool is enabled in test pool config
        user_ref = PROVIDERS.identity_api.get_user(self.user_foo['id'])
        self.user_foo.pop('password')
        self.assertDictEqual(self.user_foo, user_ref)

        handler = common_ldap._get_connection(CONF.ldap.url, use_pool=True)
        self.assertIsInstance(handler, common_ldap.PooledLDAPHandler)

    @mock.patch.object(common_ldap.KeystoneLDAPHandler, 'connect')
    @mock.patch.object(common_ldap.KeystoneLDAPHandler, 'simple_bind_s')
    def test_handler_with_use_pool_not_enabled(self, bind_method,
                                               connect_method):
        self.config_fixture.config(group='ldap', use_pool=False)
        self.config_fixture.config(group='ldap', use_auth_pool=True)
        self.cleanup_pools()

        user_api = ldap.UserApi(CONF)
        handler = user_api.get_connection(user=None, password=None,
                                          end_user_auth=True)
        # use_auth_pool flag does not matter when use_pool is False
        # still handler is non pool version
        self.assertIsInstance(handler.conn, common_ldap.PythonLDAPHandler)

    @mock.patch.object(common_ldap.KeystoneLDAPHandler, 'connect')
    @mock.patch.object(common_ldap.KeystoneLDAPHandler, 'simple_bind_s')
    def test_handler_with_end_user_auth_use_pool_not_enabled(self, bind_method,
                                                             connect_method):
        # by default use_pool is enabled in test pool config
        # now disabling use_auth_pool flag to test handler instance
        self.config_fixture.config(group='ldap', use_auth_pool=False)
        self.cleanup_pools()

        user_api = ldap.UserApi(CONF)
        handler = user_api.get_connection(user=None, password=None,
                                          end_user_auth=True)
        self.assertIsInstance(handler.conn, common_ldap.PythonLDAPHandler)

        # For end_user_auth case, flag should not be false otherwise
        # it will use, admin connections ldap pool
        handler = user_api.get_connection(user=None, password=None,
                                          end_user_auth=False)
        self.assertIsInstance(handler.conn, common_ldap.PooledLDAPHandler)

    def test_pool_size_set(self):
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        self.assertEqual(CONF.ldap.pool_size, ldappool_cm.size)

    def test_pool_retry_max_set(self):
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        self.assertEqual(CONF.ldap.pool_retry_max, ldappool_cm.retry_max)

    def test_pool_retry_delay_set(self):
        # just make one identity call to initiate ldap connection if not there
        PROVIDERS.identity_api.get_user(self.user_foo['id'])

        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        self.assertEqual(CONF.ldap.pool_retry_delay, ldappool_cm.retry_delay)

    def test_pool_use_tls_set(self):
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        self.assertEqual(CONF.ldap.use_tls, ldappool_cm.use_tls)

    def test_pool_timeout_set(self):
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        self.assertEqual(CONF.ldap.pool_connection_timeout,
                         ldappool_cm.timeout)

    def test_pool_use_pool_set(self):
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        self.assertEqual(CONF.ldap.use_pool, ldappool_cm.use_pool)

    def test_pool_connection_lifetime_set(self):
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        self.assertEqual(CONF.ldap.pool_connection_lifetime,
                         ldappool_cm.max_lifetime)

    def test_max_connection_error_raised(self):

        who = CONF.ldap.user
        cred = CONF.ldap.password
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        ldappool_cm.size = 2

        # 3rd connection attempt should raise Max connection error
        with ldappool_cm.connection(who, cred) as _:  # conn1
            with ldappool_cm.connection(who, cred) as _:  # conn2
                try:
                    with ldappool_cm.connection(who, cred) as _:  # conn3
                        _.unbind_s()
                        self.fail()
                except Exception as ex:
                    self.assertIsInstance(ex,
                                          ldappool.MaxConnectionReachedError)
        ldappool_cm.size = CONF.ldap.pool_size

    def test_pool_size_expands_correctly(self):

        who = CONF.ldap.user
        cred = CONF.ldap.password
        # get related connection manager instance
        ldappool_cm = self.conn_pools[CONF.ldap.url]
        ldappool_cm.size = 3

        def _get_conn():
            return ldappool_cm.connection(who, cred)

        # Open 3 connections first
        with _get_conn() as _:  # conn1
            self.assertEqual(1, len(ldappool_cm))
            with _get_conn() as _:  # conn2
                self.assertEqual(2, len(ldappool_cm))
                with _get_conn() as _:  # conn2
                    _.unbind_ext_s()
                    self.assertEqual(3, len(ldappool_cm))

        # Then open 3 connections again and make sure size does not grow
        # over 3
        with _get_conn() as c1:  # conn1
            self.assertEqual(3, len(ldappool_cm))
            c1.connected = False
            with _get_conn() as c2:  # conn2
                self.assertEqual(3, len(ldappool_cm))
                c2.connected = False
                with _get_conn() as c3:  # conn3
                    c3.connected = False
                    c3.unbind_ext_s()
                    self.assertEqual(3, len(ldappool_cm))

        with _get_conn() as c1:  # conn1
            self.assertEqual(1, len(ldappool_cm))
            with _get_conn() as c2:  # conn2
                self.assertEqual(2, len(ldappool_cm))
                with _get_conn() as c3:  # conn3
                    c3.unbind_ext_s()
                    self.assertEqual(3, len(ldappool_cm))

    def test_password_change_with_pool(self):
        old_password = self.user_sna['password']
        self.cleanup_pools()

        # authenticate so that connection is added to pool before password
        # change
        with self.make_request():
            user_ref = PROVIDERS.identity_api.authenticate(
                user_id=self.user_sna['id'],
                password=self.user_sna['password'])

        self.user_sna.pop('password')
        self.user_sna['enabled'] = True
        self.assertUserDictEqual(self.user_sna, user_ref)

        new_password = 'new_password'
        user_ref['password'] = new_password
        PROVIDERS.identity_api.update_user(user_ref['id'], user_ref)

        # now authenticate again to make sure new password works with
        # connection pool
        with self.make_request():
            user_ref2 = PROVIDERS.identity_api.authenticate(
                user_id=self.user_sna['id'],
                password=new_password)

        user_ref.pop('password')
        self.assertUserDictEqual(user_ref, user_ref2)

        # Authentication with old password would not work here as there
        # is only one connection in pool which get bind again with updated
        # password..so no old bind is maintained in this case.
        with self.make_request():
            self.assertRaises(AssertionError,
                              PROVIDERS.identity_api.authenticate,
                              user_id=self.user_sna['id'],
                              password=old_password)

    @mock.patch.object(fakeldap.FakeLdap, 'search_ext')
    def test_search_ext_ensure_pool_connection_released(self, mock_search_ext):
        """Test search_ext exception resiliency.

        Call search_ext function in isolation. Doing so will cause
        search_ext to borrow a connection from the pool and associate
        it with an AsynchronousMessage object. Borrowed connection ought
        to be released if anything goes wrong during LDAP API call. This
        test case intentionally throws an exception to ensure everything
        goes as expected when LDAP connection raises an exception.
        """
        class CustomDummyException(Exception):
            pass

        # Throw an exception intentionally when LDAP
        # connection search_ext function is called
        mock_search_ext.side_effect = CustomDummyException()
        self.config_fixture.config(group='ldap', pool_size=1)
        pool = self.conn_pools[CONF.ldap.url]
        user_api = ldap.UserApi(CONF)

        # setUp primes the pool so pool
        # must have one connection
        self.assertEqual(1, len(pool))
        for i in range(1, 10):
            handler = user_api.get_connection()
            # Just to ensure that we're using pooled connections
            self.assertIsInstance(handler.conn, common_ldap.PooledLDAPHandler)
            # LDAP API will throw CustomDummyException. In this scenario
            # we expect LDAP connection to be made available back to the
            # pool.
            self.assertRaises(
                CustomDummyException,
                lambda: handler.search_ext(
                    'dc=example,dc=test',
                    'dummy',
                    'objectclass=*',
                    ['mail', 'userPassword']
                )
            )
            # Pooled connection must not be evicted from the pool
            self.assertEqual(1, len(pool))
            # Ensure that the connection is inactive afterwards
            with pool._pool_lock:
                for slot, conn in enumerate(pool._pool):
                    self.assertFalse(conn.active)

            self.assertEqual(mock_search_ext.call_count, i)

    @mock.patch.object(fakeldap.FakeLdap, 'result3')
    def test_result3_ensure_pool_connection_released(self, mock_result3):
        """Test search_ext-->result3 exception resiliency.

        Call search_ext function, grab an AsynchronousMessage object and
        call result3 with it. During the result3 call, LDAP API will throw
        an exception.The expectation is that the associated LDAP pool
        connection for AsynchronousMessage must be released back to the
        LDAP connection pool.
        """
        class CustomDummyException(Exception):
            pass

        # Throw an exception intentionally when LDAP
        # connection result3 function is called
        mock_result3.side_effect = CustomDummyException()
        self.config_fixture.config(group='ldap', pool_size=1)
        pool = self.conn_pools[CONF.ldap.url]
        user_api = ldap.UserApi(CONF)

        # setUp primes the pool so pool
        # must have one connection
        self.assertEqual(1, len(pool))
        for i in range(1, 10):
            handler = user_api.get_connection()
            # Just to ensure that we're using pooled connections
            self.assertIsInstance(handler.conn, common_ldap.PooledLDAPHandler)
            msg = handler.search_ext(
                'dc=example,dc=test',
                'dummy',
                'objectclass=*',
                ['mail', 'userPassword']
            )
            # Connection is in use, must be already marked active
            self.assertTrue(msg.connection.active)
            # Pooled connection must not be evicted from the pool
            self.assertEqual(1, len(pool))
            # LDAP API will throw CustomDummyException. In this
            # scenario we expect LDAP connection to be made
            # available back to the pool.
            self.assertRaises(
                CustomDummyException,
                lambda: handler.result3(msg)
            )
            # Connection must be set inactive
            self.assertFalse(msg.connection.active)
            # Pooled connection must not be evicted from the pool
            self.assertEqual(1, len(pool))
            self.assertEqual(mock_result3.call_count, i)


class LDAPIdentity(LdapPoolCommonTestMixin,
                   test_backend_ldap.LDAPIdentity,
                   unit.TestCase):
    """Executes tests in existing base class with pooled LDAP handler."""

    def setUp(self):
        self.useFixture(fixtures.MockPatchObject(
            common_ldap.PooledLDAPHandler, 'Connector', fakeldap.FakeLdapPool))
        super(LDAPIdentity, self).setUp()

        self.addCleanup(self.cleanup_pools)
        # storing to local variable to avoid long references
        self.conn_pools = common_ldap.PooledLDAPHandler.connection_pools
        # super class loads db fixtures which establishes ldap connection
        # so adding dummy call to highlight connection pool initialization
        # as its not that obvious though its not needed here
        PROVIDERS.identity_api.get_user(self.user_foo['id'])

    def config_files(self):
        config_files = super(LDAPIdentity, self).config_files()
        config_files.append(unit.dirs.tests_conf('backend_ldap_pool.conf'))
        return config_files