ticket48234_test.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import pytest
  2. from lib389.tasks import *
  3. from lib389.utils import *
  4. from lib389.topologies import topology_st
  5. logging.getLogger(__name__).setLevel(logging.DEBUG)
  6. log = logging.getLogger(__name__)
  7. def add_ou_entry(server, name, myparent):
  8. dn = 'ou=%s,%s' % (name, myparent)
  9. server.add_s(Entry((dn, {'objectclass': ['top', 'organizationalunit'],
  10. 'ou': name})))
  11. def add_user_entry(server, name, pw, myparent):
  12. dn = 'cn=%s,%s' % (name, myparent)
  13. server.add_s(Entry((dn, {'objectclass': ['top', 'person'],
  14. 'sn': name,
  15. 'cn': name,
  16. 'telephonenumber': '+1 222 333-4444',
  17. 'userpassword': pw})))
  18. def test_ticket48234(topology_st):
  19. """
  20. Test aci which contains an extensible filter.
  21. shutdown
  22. """
  23. log.info('Bind as root DN')
  24. try:
  25. topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
  26. except ldap.LDAPError as e:
  27. topology_st.standalone.log.error('Root DN failed to authenticate: ' + e.message['desc'])
  28. assert False
  29. ouname = 'outest'
  30. username = 'admin'
  31. passwd = 'Password'
  32. deniedattr = 'telephonenumber'
  33. log.info('Add aci which contains extensible filter.')
  34. aci_text = ('(targetattr = "%s")' % (deniedattr) +
  35. '(target = "ldap:///%s")' % (DEFAULT_SUFFIX) +
  36. '(version 3.0;acl "admin-tel-matching-rule-outest";deny (all)' +
  37. '(userdn = "ldap:///%s??sub?(&(cn=%s)(ou:dn:=%s))");)' % (DEFAULT_SUFFIX, username, ouname))
  38. try:
  39. topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', aci_text)])
  40. except ldap.LDAPError as e:
  41. log.error('Failed to add aci: (%s) error %s' % (aci_text, e.message['desc']))
  42. assert False
  43. log.info('Add entries ...')
  44. for idx in range(0, 2):
  45. ou0 = 'OU%d' % idx
  46. log.info('adding %s under %s...' % (ou0, DEFAULT_SUFFIX))
  47. add_ou_entry(topology_st.standalone, ou0, DEFAULT_SUFFIX)
  48. parent = 'ou=%s,%s' % (ou0, DEFAULT_SUFFIX)
  49. log.info('adding %s under %s...' % (ouname, parent))
  50. add_ou_entry(topology_st.standalone, ouname, parent)
  51. for idx in range(0, 2):
  52. parent = 'ou=%s,ou=OU%d,%s' % (ouname, idx, DEFAULT_SUFFIX)
  53. log.info('adding %s under %s...' % (username, parent))
  54. add_user_entry(topology_st.standalone, username, passwd, parent)
  55. binddn = 'cn=%s,%s' % (username, parent)
  56. log.info('Bind as user %s' % binddn)
  57. try:
  58. topology_st.standalone.simple_bind_s(binddn, passwd)
  59. except ldap.LDAPError as e:
  60. topology_st.standalone.log.error(bindn + ' failed to authenticate: ' + e.message['desc'])
  61. assert False
  62. filter = '(cn=%s)' % username
  63. try:
  64. entries = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, filter, [deniedattr, 'dn'])
  65. assert 2 == len(entries)
  66. for idx in range(0, 1):
  67. if entries[idx].hasAttr(deniedattr):
  68. log.fatal('aci with extensible filter failed -- %s')
  69. assert False
  70. except ldap.LDAPError as e:
  71. topology_st.standalone.log.error('Search (%s, %s) failed: ' % (DEFAULT_SUFFIX, filter) + e.message['desc'])
  72. assert False
  73. log.info('Test complete')
  74. if __name__ == '__main__':
  75. # Run isolated
  76. # -s for DEBUG mode
  77. CURRENT_FILE = os.path.realpath(__file__)
  78. pytest.main("-s %s" % CURRENT_FILE)