Added ldap mock test

This commit is contained in:
Charles d'Hondt 2017-05-04 13:25:15 +02:00
parent a68808294d
commit f67d9adc31
3 changed files with 20 additions and 14 deletions

View File

@ -115,7 +115,6 @@ class ProxyAuth:
conn.search(parts[1][1:], '(' + parts[0] + username + ')', attributes=['objectclass']) conn.search(parts[1][1:], '(' + parts[0] + username + ')', attributes=['objectclass'])
if ctx.options.proxyauth.split(":")[3] in conn.entries[0]['objectclass']: if ctx.options.proxyauth.split(":")[3] in conn.entries[0]['objectclass']:
return username, password return username, password
return None return None
def authenticate(self, f: http.HTTPFlow) -> bool: def authenticate(self, f: http.HTTPFlow) -> bool:

View File

@ -71,7 +71,7 @@ setup(
"hyperframe>=5.0, <6", "hyperframe>=5.0, <6",
"jsbeautifier>=1.6.3, <1.7", "jsbeautifier>=1.6.3, <1.7",
"kaitaistruct>=0.7, <0.8", "kaitaistruct>=0.7, <0.8",
"ldap3>=2.2.0, <2.2.1", "ldap3>=2.2.0, <2.2.3",
"passlib>=1.6.5, <1.8", "passlib>=1.6.5, <1.8",
"pyasn1>=0.1.9, <0.3", "pyasn1>=0.1.9, <0.3",
"pyOpenSSL>=16.0, <17.1", "pyOpenSSL>=16.0, <17.1",

View File

@ -2,6 +2,7 @@ import binascii
import ldap3 import ldap3
import pytest import pytest
from _pytest.monkeypatch import MonkeyPatch
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy.addons import proxyauth from mitmproxy.addons import proxyauth
@ -42,16 +43,20 @@ def test_configure():
ctx.configure(up, proxyauth=None) ctx.configure(up, proxyauth=None)
assert not up.nonanonymous assert not up.nonanonymous
ctx.configure(up, proxyauth="ldap:ldap.forumsys.com:uid=?,dc=example,dc=com:person") ctx.configure(up, proxyauth="ldap:fake_server:fake_dn:fake_group")
assert up.ldapserver assert up.ldapserver
ctx.configure(up, proxyauth="ldaps:ldap.forumsys.com:uid=?,dc=example,dc=com:person")
ctx.configure(up, proxyauth="ldap:fake_server:uid=?,dc=example,dc=com:person")
assert up.ldapserver
ctx.configure(up, proxyauth="ldaps:fake_server.com:uid=?,dc=example,dc=com:person")
assert up.ldapserver assert up.ldapserver
with pytest.raises(exceptions.OptionsError): with pytest.raises(exceptions.OptionsError):
ctx.configure(up, proxyauth="ldap:ldap.forumsys.comuid=?dc=example,dc=com:person") ctx.configure(up, proxyauth="ldap:fake_serveruid=?dc=example,dc=com:person")
with pytest.raises(exceptions.OptionsError): with pytest.raises(exceptions.OptionsError):
ctx.configure(up, proxyauth="ldapssssssss:ldap.forumsys.com:uid=?,dc=example,dc=com:person") ctx.configure(up, proxyauth="ldapssssssss:fake_server.com:uid=?,dc=example,dc=com:person")
with pytest.raises(exceptions.OptionsError): with pytest.raises(exceptions.OptionsError):
ctx.configure( ctx.configure(
@ -79,7 +84,7 @@ def test_configure():
ctx.configure(up, proxyauth="any", mode="socks5") ctx.configure(up, proxyauth="any", mode="socks5")
def test_check(): def test_check(monkeypatch):
up = proxyauth.ProxyAuth() up = proxyauth.ProxyAuth()
with taddons.context() as ctx: with taddons.context() as ctx:
ctx.configure(up, proxyauth="any", mode="regular") ctx.configure(up, proxyauth="any", mode="regular")
@ -121,23 +126,25 @@ def test_check():
) )
assert not up.check(f) assert not up.check(f)
ctx.configure( ctx.configure(
up, up,
proxyauth="ldap:ldap.forumsys.com:uid=?,dc=example,dc=com:person" proxyauth="ldap:fake-server:cn=?,ou=test,o=lab:test"
) )
conn = ldap3.Connection("fake-server", user="cn=user0,ou=test,o=lab", password="password", client_strategy=ldap3.MOCK_SYNC)
conn.bind()
conn.strategy.add_entry('cn=user0,ou=test,o=lab', {'userPassword': 'test0', 'sn': 'user0_sn', 'revision': 0, 'objectClass': 'test'})
def conn_mp(ldap, user, password, **kwargs):
return conn
monkeypatch.setattr(ldap3, "Connection", conn_mp)
f.request.headers["Proxy-Authorization"] = proxyauth.mkauth( f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
"einstein", "password" "user0", "test0"
) )
assert up.check(f) assert up.check(f)
f.request.headers["Proxy-Authorization"] = proxyauth.mkauth( f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
"", "" "", ""
) )
assert not up.check(f) assert not up.check(f)
with pytest.raises(ldap3.core.exceptions.LDAPBindError):
f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
"einstein", "foo"
)
assert not up.check(f)
def test_authenticate(): def test_authenticate():