Debian Codegraph @

dirsrvtests/tests/suites/acl/acl_deny_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2020 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 logging
10 import pytest
11 import os
12 import ldap
13 import time
14 from lib389._constants import *
15 from test389.topologies import topology_st as topo
16 from lib389.idm.user import UserAccount, TEST_USER_PROPERTIES
17 from lib389.idm.domain import Domain
18
19 pytestmark = pytest.mark.tier1
20
21 DEBUGGING = os.getenv("DEBUGGING", default=False)
22 if DEBUGGING:
23 logging.getLogger(__name__).setLevel(logging.DEBUG)
24 else:
25 logging.getLogger(__name__).setLevel(logging.INFO)
26 log = logging.getLogger(__name__)
27
28 BIND_DN2 = 'uid=tuser,ou=People,dc=example,dc=com'
29 BIND_RDN2 = 'tuser'
30 BIND_DN = 'uid=tuser1,ou=People,dc=example,dc=com'
31 BIND_RDN = 'tuser1'
32 SRCH_FILTER = "uid=tuser1"
33 SRCH_FILTER2 = "uid=tuser"
34
35 aci_list_A = ['(targetattr != "userPassword") (version 3.0; acl "Anonymous access"; allow (read, search, compare)userdn = "ldap:///anyone";)',
36 '(targetattr = "*") (version 3.0;acl "allow tuser";allow (all)(userdn = "ldap:///uid=tuser5,ou=People,dc=example,dc=com");)',
37 '(targetattr != "uid || mail") (version 3.0; acl "deny-attrs"; deny (all) (userdn = "ldap:///anyone");)',
38 '(targetfilter = "(inetUserStatus=1)") ( version 3.0; acl "deny-specific-entry"; deny(all) (userdn = "ldap:///anyone");)']
39
40 aci_list_B = ['(targetattr != "userPassword") (version 3.0; acl "Anonymous access"; allow (read, search, compare)userdn = "ldap:///anyone";)',
41 '(targetattr != "uid || mail") (version 3.0; acl "deny-attrs"; deny (all) (userdn = "ldap:///anyone");)',
42 '(targetfilter = "(inetUserStatus=1)") ( version 3.0; acl "deny-specific-entry"; deny(all) (userdn = "ldap:///anyone");)']
43
44
45 @pytest.fixture(scope="module")
46 def aci_setup(topo):
47 topo.standalone.log.info("Add {}".format(BIND_DN))
48 user = UserAccount(topo.standalone, BIND_DN)
49 user_props = TEST_USER_PROPERTIES.copy()
50 user_props.update({'sn': BIND_RDN,
51 'cn': BIND_RDN,
52 'uid': BIND_RDN,
53 'inetUserStatus': '1',
54 'objectclass': 'extensibleObject',
55 'userpassword': PASSWORD})
56 user.create(properties=user_props, basedn=SUFFIX)
57
58 topo.standalone.log.info("Add {}".format(BIND_DN2))
59 user2 = UserAccount(topo.standalone, BIND_DN2)
60 user_props = TEST_USER_PROPERTIES.copy()
61 user_props.update({'sn': BIND_RDN2,
62 'cn': BIND_RDN2,
63 'uid': BIND_RDN2,
64 'userpassword': PASSWORD})
65 user2.create(properties=user_props, basedn=SUFFIX)
66
67
68 def test_multi_deny_aci(topo, aci_setup):
69 """Test that mutliple deny rules work, and that they the cache properly
70 stores the result
71
72 :id: 294c366d-850e-459e-b5a0-3cc828ec3aca
73 :setup: Standalone Instance
74 :steps:
75 1. Add aci_list_A aci's and verify two searches on the same connection
76 behave the same
77 2. Add aci_list_B aci's and verify search fails as expected
78 :expectedresults:
79 1. Both searches do not return any entries
80 2. Seaches do not return any entries
81 """
82
83 if DEBUGGING:
84 # Maybe add aci logging?
85 pass
86
87 suffix = Domain(topo.standalone, DEFAULT_SUFFIX)
88
89 for run in range(2):
90 topo.standalone.log.info("Pass " + str(run + 1))
91
92 # Test ACI List A
93 topo.standalone.log.info("Testing two searches behave the same...")
94 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
95 suffix.set('aci', aci_list_A, ldap.MOD_REPLACE)
96 time.sleep(1)
97
98 topo.standalone.simple_bind_s(BIND_DN, PASSWORD)
99 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
100 if entries and entries[0]:
101 topo.standalone.log.fatal("Incorrectly got an entry returned from search 1")
102 assert False
103
104 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
105 if entries and entries[0]:
106 topo.standalone.log.fatal("Incorrectly got an entry returned from search 2")
107 assert False
108
109 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
110 if entries is None or len(entries) == 0:
111 topo.standalone.log.fatal("Failed to get entry as good user")
112 assert False
113
114 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
115 if entries is None or len(entries) == 0:
116 topo.standalone.log.fatal("Failed to get entry as good user")
117 assert False
118
119 # Bind a different user who has rights
120 topo.standalone.simple_bind_s(BIND_DN2, PASSWORD)
121 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
122 if entries is None or len(entries) == 0:
123 topo.standalone.log.fatal("Failed to get entry as good user")
124 assert False
125
126 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
127 if entries is None or len(entries) == 0:
128 topo.standalone.log.fatal("Failed to get entry as good user (2)")
129 assert False
130
131 if run > 0:
132 # Second pass
133 topo.standalone.restart()
134
135 # Reset ACI's and do the second test
136 topo.standalone.log.info("Testing search does not return any entries...")
137 topo.standalone.simple_bind_s(DN_DM, PASSWORD)
138 suffix.set('aci', aci_list_B, ldap.MOD_REPLACE)
139 time.sleep(1)
140
141 topo.standalone.simple_bind_s(BIND_DN, PASSWORD)
142 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
143 if entries and entries[0]:
144 topo.standalone.log.fatal("Incorrectly got an entry returned from search 1")
145 assert False
146
147 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
148 if entries and entries[0]:
149 topo.standalone.log.fatal("Incorrectly got an entry returned from search 2")
150 assert False
151
152 if run > 0:
153 # Second pass
154 topo.standalone.restart()
155
156 # Bind as different user who has rights
157 topo.standalone.simple_bind_s(BIND_DN2, PASSWORD)
158 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
159 if entries is None or len(entries) == 0:
160 topo.standalone.log.fatal("Failed to get entry as good user")
161 assert False
162
163 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
164 if entries is None or len(entries) == 0:
165 topo.standalone.log.fatal("Failed to get entry as good user (2)")
166 assert False
167
168 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
169 if entries and entries[0]:
170 topo.standalone.log.fatal("Incorrectly got an entry returned from search 1")
171 assert False
172
173 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
174 if entries and entries[0]:
175 topo.standalone.log.fatal("Incorrectly got an entry returned from search 2")
176 assert False
177
178 # back to user 1
179 topo.standalone.simple_bind_s(BIND_DN, PASSWORD)
180 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
181 if entries is None or len(entries) == 0:
182 topo.standalone.log.fatal("Failed to get entry as user1")
183 assert False
184
185 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER2)
186 if entries is None or len(entries) == 0:
187 topo.standalone.log.fatal("Failed to get entry as user1 (2)")
188 assert False
189
190 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
191 if entries and entries[0]:
192 topo.standalone.log.fatal("Incorrectly got an entry returned from search 1")
193 assert False
194
195 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, SRCH_FILTER)
196 if entries and entries[0]:
197 topo.standalone.log.fatal("Incorrectly got an entry returned from search 2")
198 assert False
199
200 topo.standalone.log.info("Test PASSED")
201
202
203 if __name__ == '__main__':
204 # Run isolated
205 # -s for DEBUG mode
206 CURRENT_FILE = os.path.realpath(__file__)
207 pytest.main(["-s", CURRENT_FILE])
208