Debian Codegraph @

dirsrvtests/tests/suites/cos/indirect_cos_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 import subprocess
15
16 from lib389 import Entry
17 from lib389.idm.user import UserAccounts, UserAccount
18 from lib389.idm.domain import Domain
19 from lib389.cos import CosIndirectDefinitions
20 from test389.topologies import topology_st as topo
21 from lib389._constants import (DEFAULT_SUFFIX, DN_DM, PASSWORD, HOST_STANDALONE,
22 SERVERID_STANDALONE, PORT_STANDALONE)
23
24 pytestmark = pytest.mark.tier1
25
26 DEBUGGING = os.getenv("DEBUGGING", default=False)
27 if DEBUGGING:
28 logging.getLogger(__name__).setLevel(logging.DEBUG)
29 else:
30 logging.getLogger(__name__).setLevel(logging.INFO)
31 log = logging.getLogger(__name__)
32
33 TEST_USER_DN = "uid=test_user,ou=people,dc=example,dc=com"
34 OU_PEOPLE = 'ou=people,{}'.format(DEFAULT_SUFFIX)
35
36 PW_POLICY_CONT_PEOPLE = 'cn="cn=nsPwPolicyEntry,' \
37 'ou=people,dc=example,dc=com",' \
38 'cn=nsPwPolicyContainer,ou=people,dc=example,dc=com'
39
40
41 def check_user(inst):
42 """Search the test user and make sure it has the expected attrs
43 """
44 try:
45 entries = inst.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, "uid=test_user")
46 log.debug('user: \n' + str(entries[0]))
47 assert entries[0].hasAttr('ou'), "Entry is missing ou cos attribute"
48 assert entries[0].hasAttr('x-department'), "Entry is missing description cos attribute"
49 assert entries[0].hasAttr('x-en-ou'), "Entry is missing givenname cos attribute"
50 except ldap.LDAPError as e:
51 log.fatal('Failed to search for user: ' + str(e))
52 raise e
53
54
55 def setup_subtree_policy(topo):
56 """Set up subtree password policy
57 """
58
59 topo.standalone.config.set('nsslapd-pwpolicy-local', 'on')
60
61 log.info('Create password policy for subtree {}'.format(OU_PEOPLE))
62 try:
63 subprocess.call(['%s/dsconf' % topo.standalone.get_sbin_dir(),
64 'slapd-standalone1',
65 'localpwp',
66 'addsubtree',
67 OU_PEOPLE])
68
69 except subprocess.CalledProcessError as e:
70 log.error('Failed to create pw policy policy for {}: error {}'.format(
71 OU_PEOPLE, e.message['desc']))
72 raise e
73
74 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
75 domain.replace('pwdpolicysubentry', PW_POLICY_CONT_PEOPLE)
76
77 time.sleep(1)
78
79
80 def setup_indirect_cos(topo):
81 """Setup indirect COS definition and template
82 """
83 cosDef = Entry(('cn=cosDefinition,dc=example,dc=com',
84 {'objectclass': ['top', 'ldapsubentry',
85 'cossuperdefinition',
86 'cosIndirectDefinition'],
87 'cosAttribute': ['ou merge-schemes',
88 'x-department merge-schemes',
89 'x-en-ou merge-schemes'],
90 'cosIndirectSpecifier': 'seeAlso',
91 'cn': 'cosDefinition'}))
92
93 cosTemplate = Entry(('cn=cosTemplate,dc=example,dc=com',
94 {'objectclass': ['top',
95 'extensibleObject',
96 'cosTemplate'],
97 'ou': 'My COS Org',
98 'x-department': 'My COS x-department',
99 'x-en-ou': 'my COS x-en-ou',
100 'cn': 'cosTemplate'}))
101 try:
102 topo.standalone.add_s(cosDef)
103 topo.standalone.add_s(cosTemplate)
104 except ldap.LDAPError as e:
105 log.fatal('Failed to add cos: error ' + str(e))
106 raise e
107 time.sleep(1)
108
109
110 @pytest.fixture(scope="module")
111 def setup(topo, request):
112 """Add schema, and test user
113 """
114 log.info('Add custom schema...')
115 try:
116 ATTR_1 = (b"( 1.3.6.1.4.1.409.389.2.189 NAME 'x-department' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'user defined' )")
117 ATTR_2 = (b"( 1.3.6.1.4.1.409.389.2.187 NAME 'x-en-ou' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'user defined' )")
118 OC = (b"( xPerson-oid NAME 'xPerson' DESC '' SUP person STRUCTURAL MAY ( x-department $ x-en-ou ) X-ORIGIN 'user defined' )")
119 topo.standalone.modify_s("cn=schema", [(ldap.MOD_ADD, 'attributeTypes', ATTR_1),
120 (ldap.MOD_ADD, 'attributeTypes', ATTR_2),
121 (ldap.MOD_ADD, 'objectClasses', OC)])
122 except ldap.LDAPError as e:
123 log.fatal('Failed to add custom schema')
124 raise e
125 time.sleep(1)
126
127 log.info('Add test user...')
128 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
129
130 user_properties = {
131 'uid': 'test_user',
132 'cn': 'test user',
133 'sn': 'user',
134 'uidNumber': '1000',
135 'gidNumber': '2000',
136 'homeDirectory': '/home/test_user',
137 'seeAlso': 'cn=cosTemplate,dc=example,dc=com'
138 }
139 user = users.create(properties=user_properties)
140
141 user.add('objectClass', 'xPerson')
142
143 # Setup COS
144 log.info("Setup indirect COS...")
145 setup_indirect_cos(topo)
146
147
148 def test_indirect_cos(topo, setup):
149 """Test indirect cos
150
151 :id: 890d5929-7d52-4a56-956e-129611b4649a
152 :setup: standalone
153 :steps:
154 1. Test cos is working for test user
155 2. Add subtree password policy
156 3. Test cos is working for test user
157 :expectedresults:
158 1. User has expected cos attrs
159 2. Substree password policy setup is successful
160 3. User still has expected cos attrs
161 """
162
163 # Step 1 - Search user and see if the COS attrs are included
164 log.info('Checking user...')
165 check_user(topo.standalone)
166
167 # Step 2 - Add subtree password policy (Second COS - operational attribute)
168 setup_subtree_policy(topo)
169
170 # Step 3 - Check user again now hat we have a mix of vattrs
171 log.info('Checking user...')
172 check_user(topo.standalone)
173
174
175 def test_indirect_cos_reflects_current_value(topo):
176 """Test that indirect COS reflects the current value of the indirect entry
177
178 :id: 905376a4-1a61-44c2-a0c4-541b35a219ad
179 :setup: Standalone instance
180 :steps:
181 1. Add indirect COS definition using manager as specifier
182 2. Add manager entry with roomnumber=1
183 3. Add user entry pointing to manager
184 4. Verify user gets roomnumber=1 from COS
185 5. Modify manager's roomnumber to 2
186 6. Verify user now gets roomnumber=2 from COS
187 :expectedresults:
188 1. Success
189 2. Success
190 3. Success
191 4. User has roomnumber=1
192 5. Success
193 6. User has roomnumber=2
194 """
195
196 MANAGER_DN = f'uid=mymanager,ou=people,{DEFAULT_SUFFIX}'
197 USER_DN = f'uid=cosuser,ou=people,{DEFAULT_SUFFIX}'
198
199 log.info('Add indirect COS definition using manager as specifier')
200 cos_defs = CosIndirectDefinitions(topo.standalone, DEFAULT_SUFFIX)
201 cos_def = cos_defs.create(properties={
202 'cn': 'roomnumber-cos',
203 'cosIndirectSpecifier': 'manager',
204 'cosattribute': 'roomnumber'
205 })
206
207 log.info('Add manager entry with roomnumber=1')
208 manager = UserAccount(topo.standalone, MANAGER_DN)
209 manager.create(properties={
210 'uid': 'mymanager',
211 'cn': 'My Manager',
212 'sn': 'Manager',
213 'uidNumber': '1001',
214 'gidNumber': '1001',
215 'homeDirectory': '/home/mymanager'
216 })
217 manager.add('objectClass', 'extensibleObject')
218 manager.replace('roomnumber', '1')
219
220 log.info('Add user entry with manager attribute')
221 user = UserAccount(topo.standalone, USER_DN)
222 user.create(properties={
223 'uid': 'cosuser',
224 'cn': 'COS User',
225 'sn': 'User',
226 'uidNumber': '1002',
227 'gidNumber': '1002',
228 'homeDirectory': '/home/cosuser',
229 'manager': MANAGER_DN
230 })
231
232 log.info('Verify user gets roomnumber=1 from COS')
233 assert user.get_attr_val_utf8('roomnumber') == '1'
234
235 log.info('Modify manager roomnumber to 2')
236 manager.replace('roomnumber', '2')
237
238 log.info('Verify user now gets roomnumber=2 from COS')
239 assert user.get_attr_val_utf8('roomnumber') == '2'
240
241 user.delete()
242 manager.delete()
243 cos_def.delete()
244
245
246 if __name__ == '__main__':
247 # Run isolated
248 # -s for DEBUG mode
249 CURRENT_FILE = os.path.realpath(__file__)
250 pytest.main("-s %s" % CURRENT_FILE)
251