| 1 |
""" |
| 2 |
""" |
| 3 |
|
| 4 |
# Created on 2013.08.11 |
| 5 |
# |
| 6 |
# Author: Giovanni Cannata |
| 7 |
# |
| 8 |
# Copyright 2013 - 2020 Giovanni Cannata |
| 9 |
# |
| 10 |
# This file is part of ldap3. |
| 11 |
# |
| 12 |
# ldap3 is free software: you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU Lesser General Public License as published |
| 14 |
# by the Free Software Foundation, either version 3 of the License, or |
| 15 |
# (at your option) any later version. |
| 16 |
# |
| 17 |
# ldap3 is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
# GNU Lesser General Public License for more details. |
| 21 |
# |
| 22 |
# You should have received a copy of the GNU Lesser General Public License |
| 23 |
# along with ldap3 in the COPYING and COPYING.LESSER files. |
| 24 |
# If not, see <http://www.gnu.org/licenses/>. |
| 25 |
|
| 26 |
import unittest |
| 27 |
import ssl |
| 28 |
|
| 29 |
from ldap3 import Server, Connection, ServerPool, Tls, SASL, EXTERNAL, MOCK_ASYNC, MOCK_SYNC |
| 30 |
from test.config import test_server, test_port, test_port_ssl, test_user, test_password, test_authentication, \ |
| 31 |
test_strategy, test_lazy_connection, test_get_info, test_server_mode, test_valid_names, test_server_type,\ |
| 32 |
test_pooling_strategy, test_pooling_active, test_pooling_exhaust, test_ca_cert_file, \ |
| 33 |
test_user_cert_file, test_user_key_file |
| 34 |
|
| 35 |
|
| 36 |
class Test(unittest.TestCase): |
| 37 |
def test_start_tls(self): |
| 38 |
if test_server_type != 'NONE' and test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 39 |
if isinstance(test_server, (list, tuple)): |
| 40 |
server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 41 |
for host in test_server: |
| 42 |
server.add(Server(host=host, port=test_port, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 43 |
else: |
| 44 |
server = Server(host=test_server, port=test_port, tls=Tls(validate=ssl.CERT_NONE), get_info=test_get_info, mode=test_server_mode) |
| 45 |
connection = Connection(server, auto_bind=False, version=3, client_strategy=test_strategy, user=test_user, password=test_password, authentication=test_authentication, lazy=test_lazy_connection, pool_name='pool1') |
| 46 |
connection.open() |
| 47 |
connection.start_tls() |
| 48 |
self.assertFalse(connection.closed) |
| 49 |
connection.unbind() |
| 50 |
if connection.strategy.pooled: |
| 51 |
connection.strategy.terminate() |
| 52 |
|
| 53 |
def test_open_ssl_with_defaults(self): |
| 54 |
if test_server_type != 'NONE' and test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 55 |
if isinstance(test_server, (list, tuple)): |
| 56 |
server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 57 |
for host in test_server: |
| 58 |
server.add(Server(host=host, port=test_port, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 59 |
else: |
| 60 |
server = Server(host=test_server, port=test_port_ssl, use_ssl=True) |
| 61 |
connection = Connection(server, user=test_user, password=test_password) |
| 62 |
connection.open() |
| 63 |
self.assertFalse(connection.closed) |
| 64 |
connection.unbind() |
| 65 |
if connection.strategy.pooled: |
| 66 |
connection.strategy.terminate() |
| 67 |
|
| 68 |
def test_open_with_tls_before_bind(self): |
| 69 |
if test_server_type != 'NONE' and test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 70 |
if isinstance(test_server, (list, tuple)): |
| 71 |
server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 72 |
for host in test_server: |
| 73 |
server.add(Server(host=host, port=test_port, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 74 |
else: |
| 75 |
server = Server(host=test_server, port=test_port, tls=Tls()) |
| 76 |
connection = Connection(server, auto_bind=False, version=3, client_strategy=test_strategy, user=test_user, password=test_password, authentication=test_authentication, lazy=test_lazy_connection, pool_name='pool1') |
| 77 |
connection.open(read_server_info=False) |
| 78 |
connection.start_tls(read_server_info=False) |
| 79 |
connection.bind() |
| 80 |
self.assertTrue(connection.bound) |
| 81 |
connection.unbind() |
| 82 |
if connection.strategy.pooled: |
| 83 |
connection.strategy.terminate() |
| 84 |
self.assertFalse(connection.bound) |
| 85 |
|
| 86 |
def test_open_with_tls_after_bind(self): |
| 87 |
if test_server_type != 'NONE' and test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 88 |
if isinstance(test_server, (list, tuple)): |
| 89 |
server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 90 |
for host in test_server: |
| 91 |
server.add(Server(host=host, port=test_port, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 92 |
else: |
| 93 |
server = Server(host=test_server, port=test_port, tls=Tls()) |
| 94 |
connection = Connection(server, auto_bind=False, version=3, client_strategy=test_strategy, user=test_user, password=test_password, authentication=test_authentication, lazy=test_lazy_connection, pool_name='pool1') |
| 95 |
connection.open() |
| 96 |
connection.bind() |
| 97 |
connection.start_tls() |
| 98 |
self.assertTrue(connection.bound) |
| 99 |
connection.unbind() |
| 100 |
if connection.strategy.pooled: |
| 101 |
connection.strategy.terminate() |
| 102 |
self.assertFalse(connection.bound) |
| 103 |
|
| 104 |
# def test_bind_ssl_with_certificate(self): |
| 105 |
# if test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 106 |
# tls = Tls(local_private_key_file=test_user_key_file, |
| 107 |
# local_certificate_file=test_user_cert_file, |
| 108 |
# validate=ssl.CERT_REQUIRED, |
| 109 |
# version=ssl.PROTOCOL_SSLv23, |
| 110 |
# ssl_options=[ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv3], |
| 111 |
# ca_certs_file=test_ca_cert_file, |
| 112 |
# valid_names=test_valid_names) |
| 113 |
# if isinstance(test_server, (list, tuple)): |
| 114 |
# server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 115 |
# for host in test_server: |
| 116 |
# server.add(Server(host=host, port=test_port, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 117 |
# else: |
| 118 |
# server = Server(host=test_server, port=test_port_ssl, use_ssl=True, tls=tls) |
| 119 |
# connection = Connection(server, auto_bind=False, client_strategy=test_strategy, user=test_user, password=test_password, authentication=test_authentication) |
| 120 |
# connection.open() |
| 121 |
# connection.bind() |
| 122 |
# self.assertTrue(connection.bound) |
| 123 |
# connection.unbind() |
| 124 |
# if connection.strategy.pooled: |
| 125 |
# connection.strategy.terminate() |
| 126 |
# self.assertFalse(connection.bound) |
| 127 |
|
| 128 |
# def test_sasl_with_external_certificate(self): |
| 129 |
# if test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 130 |
# tls = Tls(local_private_key_file=test_user_key_file, local_certificate_file=test_user_cert_file, validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLSv1, ca_certs_file=test_ca_cert_file, valid_names=test_valid_names) |
| 131 |
# if isinstance(test_server, (list, tuple)): |
| 132 |
# server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 133 |
# for host in test_server: |
| 134 |
# server.add(Server(host=host, port=test_port_ssl, use_ssl=True, tls=tls, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 135 |
# else: |
| 136 |
# server = Server(host=test_server, port=test_port_ssl, use_ssl=True, tls=tls) |
| 137 |
# connection = Connection(server, auto_bind=False, version=3, client_strategy=test_strategy, authentication=SASL, sasl_mechanism=EXTERNAL) |
| 138 |
# connection.open() |
| 139 |
# connection.bind() |
| 140 |
# self.assertTrue(connection.bound) |
| 141 |
# connection.unbind() |
| 142 |
# if connection.strategy.pooled: |
| 143 |
# connection.strategy.terminate() |
| 144 |
# self.assertFalse(connection.bound) |
| 145 |
|
| 146 |
def test_bind_ssl_cert_none(self): |
| 147 |
if test_server_type != 'NONE' and test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 148 |
tls = Tls(validate=ssl.CERT_NONE) |
| 149 |
if isinstance(test_server, (list, tuple)): |
| 150 |
server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 151 |
for host in test_server: |
| 152 |
server.add(Server(host=host, port=test_port, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 153 |
else: |
| 154 |
server = Server(host=test_server, port=test_port_ssl, use_ssl=True, tls=tls) |
| 155 |
connection = Connection(server, auto_bind=False, client_strategy=test_strategy, user=test_user, password=test_password, authentication=test_authentication) |
| 156 |
connection.open() |
| 157 |
connection.bind() |
| 158 |
self.assertTrue(connection.bound) |
| 159 |
connection.unbind() |
| 160 |
if connection.strategy.pooled: |
| 161 |
connection.strategy.terminate() |
| 162 |
self.assertFalse(connection.bound) |
| 163 |
|
| 164 |
def test_start_tls_with_cipher(self): |
| 165 |
# ciphers = None |
| 166 |
# ciphers = '!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2' |
| 167 |
ciphers = 'HIGH:!aNULL:!RC4:!DSS' |
| 168 |
|
| 169 |
if test_server_type != 'NONE' and test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 170 |
if isinstance(test_server, (list, tuple)): |
| 171 |
server = ServerPool(pool_strategy=test_pooling_strategy, active=test_pooling_active, exhaust=test_pooling_exhaust) |
| 172 |
for host in test_server: |
| 173 |
server.add(Server(host=host, port=test_port, allowed_referral_hosts=('*', True), get_info=test_get_info, mode=test_server_mode)) |
| 174 |
else: |
| 175 |
server = Server(host=test_server, port=test_port, tls=Tls(validate=ssl.CERT_NONE, ciphers=ciphers), get_info=test_get_info, mode=test_server_mode) |
| 176 |
connection = Connection(server, auto_bind=False, version=3, client_strategy=test_strategy, user=test_user, password=test_password, authentication=test_authentication, lazy=test_lazy_connection, pool_name='pool1') |
| 177 |
connection.open() |
| 178 |
connection.start_tls() |
| 179 |
self.assertFalse(connection.closed) |
| 180 |
# self.assertEqual(connection.socket.cipher(), ciphers) |
| 181 |
connection.unbind() |
| 182 |
if connection.strategy.pooled: |
| 183 |
connection.strategy.terminate() |
| 184 |
|
| 185 |
# def test_hostname_doesnt_match(self): |
| 186 |
# tls_config = Tls(validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLSv1) |
| 187 |
# server = Server('edir1.hyperv', use_ssl=True, tls=tls_config) |
| 188 |
# conn = Connection(server) |
| 189 |
# conn.open() |
| 190 |
# self.assertTrue(conn.bound) |