acl_test.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2015 Red Hat, Inc.
  3. # All rights reserved.
  4. #
  5. # License: GPL (version 3 or any later version).
  6. # See LICENSE for details.
  7. # --- END COPYRIGHT BLOCK ---
  8. #
  9. import os
  10. import sys
  11. import time
  12. import ldap
  13. import logging
  14. import pytest
  15. from lib389 import DirSrv, Entry, tools, tasks
  16. from lib389.tools import DirSrvTools
  17. from lib389._constants import *
  18. from lib389.properties import *
  19. from lib389.tasks import *
  20. from lib389.utils import *
  21. logging.getLogger(__name__).setLevel(logging.DEBUG)
  22. log = logging.getLogger(__name__)
  23. installation1_prefix = None
  24. class TopologyStandalone(object):
  25. def __init__(self, standalone):
  26. standalone.open()
  27. self.standalone = standalone
  28. @pytest.fixture(scope="module")
  29. def topology(request):
  30. global installation1_prefix
  31. if installation1_prefix:
  32. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  33. # Creating standalone instance ...
  34. standalone = DirSrv(verbose=False)
  35. args_instance[SER_HOST] = HOST_STANDALONE
  36. args_instance[SER_PORT] = PORT_STANDALONE
  37. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  38. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  39. args_standalone = args_instance.copy()
  40. standalone.allocate(args_standalone)
  41. instance_standalone = standalone.exists()
  42. if instance_standalone:
  43. standalone.delete()
  44. standalone.create()
  45. standalone.open()
  46. # Delete each instance in the end
  47. def fin():
  48. standalone.delete()
  49. request.addfinalizer(fin)
  50. # Clear out the tmp dir
  51. standalone.clearTmpDir(__file__)
  52. return TopologyStandalone(standalone)
  53. def add_attr(topology, attr_name):
  54. """Adds attribute to the schema"""
  55. ATTR_VALUE = """(NAME '%s' \
  56. DESC 'Attribute filteri-Multi-Valued' \
  57. SYNTAX 1.3.6.1.4.1.1466.115.121.1.27)""" % attr_name
  58. mod = [(ldap.MOD_ADD, 'attributeTypes', ATTR_VALUE)]
  59. try:
  60. topology.standalone.modify_s(DN_SCHEMA, mod)
  61. except ldap.LDAPError as e:
  62. log.fatal('Failed to add attr (%s): error (%s)' % (attr_name,
  63. e.message['desc']))
  64. assert False
  65. @pytest.fixture(params=["lang-ja", "binary", "phonetic"])
  66. def aci_with_attr_subtype(request, topology):
  67. """Adds and deletes an ACI in the DEFAULT_SUFFIX"""
  68. TARGET_ATTR = 'protectedOperation'
  69. USER_ATTR = 'allowedToPerform'
  70. SUBTYPE = request.param
  71. log.info("========Executing test with '%s' subtype========" % SUBTYPE)
  72. log.info(" Add a target attribute")
  73. add_attr(topology, TARGET_ATTR)
  74. log.info(" Add a user attribute")
  75. add_attr(topology, USER_ATTR)
  76. ACI_TARGET = '(targetattr=%s;%s)' % (TARGET_ATTR, SUBTYPE)
  77. ACI_ALLOW = '(version 3.0; acl "test aci for subtypes"; allow (read) '
  78. ACI_SUBJECT = 'userattr = "%s;%s#GROUPDN";)' % (USER_ATTR, SUBTYPE)
  79. ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
  80. log.info(" Add an ACI with attribute subtype")
  81. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  82. try:
  83. topology.standalone.modify_s(DEFAULT_SUFFIX, mod)
  84. except ldap.LDAPError as e:
  85. log.fatal('Failed to add ACI: error (%s)' % (e.message['desc']))
  86. assert False
  87. def fin():
  88. log.info(" Finally, delete an ACI with the '%s' subtype" %
  89. SUBTYPE)
  90. mod = [(ldap.MOD_DELETE, 'aci', ACI_BODY)]
  91. try:
  92. topology.standalone.modify_s(DEFAULT_SUFFIX, mod)
  93. except ldap.LDAPError as e:
  94. log.fatal('Failed to delete ACI: error (%s)' % (e.message['desc']))
  95. assert False
  96. request.addfinalizer(fin)
  97. return ACI_BODY
  98. def test_aci_attr_subtype_targetattr(topology, aci_with_attr_subtype):
  99. """Checks, that ACIs allow attribute subtypes in the targetattr keyword
  100. Test description:
  101. 1. Define two attributes in the schema
  102. - first will be a targetattr
  103. - second will be a userattr
  104. 2. Add an ACI with an attribute subtype
  105. - or language subtype
  106. - or binary subtype
  107. - or pronunciation subtype
  108. """
  109. log.info(" Search for the added attribute")
  110. try:
  111. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  112. ldap.SCOPE_BASE,
  113. '(objectclass=*)', ['aci'])
  114. entry = str(entries[0])
  115. assert aci_with_attr_subtype in entry
  116. log.info(" The added attribute was found")
  117. except ldap.LDAPError as e:
  118. log.fatal('Search failed, error: ' + e.message['desc'])
  119. assert False
  120. if __name__ == '__main__':
  121. # Run isolated
  122. # -s for DEBUG mode
  123. CURRENT_FILE = os.path.realpath(__file__)
  124. pytest.main("-s %s" % CURRENT_FILE)