-
Notifications
You must be signed in to change notification settings - Fork 126
Attach response controls to exceptions #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
somay
wants to merge
9
commits into
python-ldap:master
from
somay:issue208-controls-in-failed-responses
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
57ed6ae
Attach response controls to exceptions
c76f087
remove PPolicyEnabledSlapdTestCase
cc7ef3b
avoid to export PPolicyEnabledSlapdObject
0144bdb
trim a redundant comment
a0ba399
Test: assert len(ctrls) & remove a magic number
2b5d1ee
Doc: delete about slapdtest.PPolicyEnabled...
a616cae
Doc: slapdtest: modules and overlay attributes
e8648f2
delete _make_ldap_object()
9ed502d
Test: check also empty control list
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ Classes | |
|
||
.. autoclass:: slapdtest.SlapdTestCase | ||
:members: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,10 @@ | |
os.environ['LDAPNOINIT'] = '1' | ||
|
||
import ldap | ||
import ldap.controls | ||
import ldap.controls.ppolicy | ||
from ldap.ldapobject import SimpleLDAPObject, ReconnectLDAPObject | ||
|
||
from slapdtest import SlapdTestCase | ||
from slapdtest import SlapdTestCase, SlapdObject | ||
from slapdtest import requires_ldapi, requires_sasl, requires_tls | ||
|
||
|
||
|
@@ -75,6 +76,110 @@ | |
""" | ||
|
||
|
||
class PPolicyEnabledSlapdObject(SlapdObject): | ||
""" | ||
A subclass of :py:class:`SlapdObject` with password policy enabled. | ||
Note that this class has no actual password policy configuration entries. | ||
It is the job of the users of this class to define | ||
the default password policies on their own. | ||
The dn of the default is :attr:`.default_ppolicy_dn` of this class. | ||
""" | ||
|
||
openldap_schema_files = ( | ||
'core.schema', 'ppolicy.schema' | ||
) | ||
modules = ( | ||
'ppolicy', | ||
) | ||
|
||
default_ppolicy_dn = "cn=default-ppolicy,%(suffix)s" % { | ||
'suffix': SlapdObject.suffix | ||
} | ||
|
||
overlays = ( | ||
{ | ||
'name': 'ppolicy', | ||
'configuration': "\n".join([ | ||
'ppolicy_default "{}"'.format(default_ppolicy_dn), | ||
# let slapd tell the clients that they are locked out | ||
'ppolicy_use_lockout']) | ||
}, | ||
) | ||
|
||
|
||
class Test02_ResponseControl(SlapdTestCase): | ||
""" | ||
tests abount response controls sent by the server | ||
""" | ||
|
||
ldap_object_class = SimpleLDAPObject | ||
server_class = PPolicyEnabledSlapdObject | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super(Test02_ResponseControl, cls).setUpClass() | ||
# insert some Foo* objects via ldapadd | ||
cls.server.ldapadd( | ||
LDIF_TEMPLATE % { | ||
'suffix': cls.server.suffix, | ||
'rootdn': cls.server.root_dn, | ||
'rootcn': cls.server.root_cn, | ||
'rootpw': cls.server.root_pw, | ||
'dc': cls.server.suffix.split(',')[0][3:], | ||
} | ||
) | ||
|
||
# Very strict pwdMaxFailure in order to easily test the cases where | ||
# bind failure with response controls is needed | ||
cls.server.ldapadd( | ||
'''dn: {dn} | ||
objectClass: organizationalRole | ||
objectClass: pwdPolicy | ||
cn: default-ppolicy | ||
pwdAttribute: userPassword | ||
pwdLockout: TRUE | ||
pwdMaxFailure: 1 | ||
pwdLockoutDuration: 60 | ||
pwdFailureCountInterval: 3600'''.format(dn=cls.server.default_ppolicy_dn) | ||
) | ||
|
||
def test_response_controls_are_attached_to_exceptions(self): | ||
base = self.server.suffix | ||
cn = "test_response_controls_are_attached_to_exceptions" | ||
user_dn = "cn={},{}".format(cn, base) | ||
password = "user5_pw" | ||
|
||
self.server.ldapadd( | ||
'''dn: {dn} | ||
objectClass: applicationProcess | ||
objectClass: simpleSecurityObject | ||
cn: {cn} | ||
userPassword: {password}'''.format(cn=cn, dn=user_dn, password=password) | ||
) | ||
|
||
ldap_conn = self.ldap_object_class(self.server.ldap_uri) | ||
|
||
# Firstly cause a bind failure to lock out the account | ||
with self.assertRaises(ldap.INVALID_CREDENTIALS) as cm: | ||
wrong_password = 'wrong' + password | ||
ldap_conn.simple_bind_s(user_dn, wrong_password) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also check here that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checked. |
||
|
||
empty_controls = cm.exception.args[0]['ctrls'] | ||
self.assertEqual(len(empty_controls), 0) | ||
|
||
with self.assertRaises(ldap.INVALID_CREDENTIALS) as cm: | ||
ldap_conn.simple_bind_s( | ||
user_dn, password, | ||
serverctrls=[ldap.controls.ppolicy.PasswordPolicyControl()]) | ||
|
||
controls = cm.exception.args[0]['ctrls'] | ||
decoded_controls = ldap.controls.DecodeControlTuples(controls) | ||
self.assertEqual(len(decoded_controls), 1) | ||
pp = decoded_controls[0] | ||
expected_error = ldap.controls.ppolicy.PasswordPolicyError('accountLocked') | ||
self.assertEqual(pp.error, int(expected_error)) | ||
|
||
|
||
class Test00_SimpleLDAPObject(SlapdTestCase): | ||
""" | ||
test LDAP search operations | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these attributes can be overridden in subclasses, they should be documented.