plugin_tests.py 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398
  1. '''
  2. Created on Dec 09, 2014
  3. @author: mreynolds
  4. '''
  5. import os
  6. import sys
  7. import time
  8. import ldap
  9. import logging
  10. import pytest
  11. from lib389 import DirSrv, Entry, tools, tasks
  12. from lib389.tools import DirSrvTools
  13. from lib389._constants import *
  14. from lib389.properties import *
  15. from lib389.tasks import *
  16. log = logging.getLogger(__name__)
  17. USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
  18. USER2_DN = 'uid=user2,' + DEFAULT_SUFFIX
  19. USER3_DN = 'uid=user3,' + DEFAULT_SUFFIX
  20. BUSER1_DN = 'uid=user1,ou=branch1,' + DEFAULT_SUFFIX
  21. BUSER2_DN = 'uid=user2,ou=branch2,' + DEFAULT_SUFFIX
  22. BUSER3_DN = 'uid=user3,ou=branch2,' + DEFAULT_SUFFIX
  23. BRANCH1_DN = 'ou=branch1,' + DEFAULT_SUFFIX
  24. BRANCH2_DN = 'ou=branch2,' + DEFAULT_SUFFIX
  25. GROUP_OU = 'ou=groups,' + DEFAULT_SUFFIX
  26. PEOPLE_OU = 'ou=people,' + DEFAULT_SUFFIX
  27. GROUP_DN = 'cn=group,' + DEFAULT_SUFFIX
  28. CONFIG_AREA = 'nsslapd-pluginConfigArea'
  29. '''
  30. Functional tests for each plugin
  31. Test:
  32. plugin restarts (test when on and off)
  33. plugin config validation
  34. plugin dependencies
  35. plugin functionality (including plugin tasks)
  36. '''
  37. ################################################################################
  38. #
  39. # Test Plugin Dependency
  40. #
  41. ################################################################################
  42. def test_dependency(inst, plugin):
  43. """
  44. Set the "account usabilty" plugin to depend on this plugin. This plugin
  45. is generic, always enabled, and perfect for our testing
  46. """
  47. try:
  48. inst.modify_s('cn=' + PLUGIN_ACCT_USABILITY + ',cn=plugins,cn=config',
  49. [(ldap.MOD_REPLACE, 'nsslapd-plugin-depends-on-named', plugin)])
  50. except ldap.LDAPError, e:
  51. log.fatal('test_dependency: Failed to modify ' + PLUGIN_ACCT_USABILITY + ': error ' + e.message['desc'])
  52. assert False
  53. try:
  54. inst.modify_s('cn=' + plugin + ',cn=plugins,cn=config',
  55. [(ldap.MOD_REPLACE, 'nsslapd-pluginenabled', 'off')])
  56. except ldap.UNWILLING_TO_PERFORM:
  57. # failed as expected
  58. pass
  59. else:
  60. # Incorrectly succeeded
  61. log.fatal('test_dependency: Plugin dependency check failed (%s)' % plugin)
  62. assert False
  63. # Now undo the change
  64. try:
  65. inst.modify_s('cn=' + PLUGIN_ACCT_USABILITY + ',cn=plugins,cn=config',
  66. [(ldap.MOD_DELETE, 'nsslapd-plugin-depends-on-named', None)])
  67. except ldap.LDAPError, e:
  68. log.fatal('test_dependency: Failed to reset ' + plugin + ': error ' + e.message['desc'])
  69. assert False
  70. ################################################################################
  71. #
  72. # Wait for task to complete
  73. #
  74. ################################################################################
  75. def wait_for_task(conn, task_dn):
  76. finished = False
  77. count = 0
  78. while count < 60:
  79. try:
  80. task_entry = conn.search_s(task_dn, ldap.SCOPE_BASE, 'objectclass=*')
  81. if not task_entry:
  82. log.fatal('wait_for_task: Search failed to find task: ' + task_dn)
  83. assert False
  84. if task_entry[0].hasAttr('nstaskexitcode'):
  85. # task is done
  86. finished = True
  87. break
  88. except ldap.LDAPError, e:
  89. log.fatal('wait_for_task: Search failed: ' + e.message['desc'])
  90. assert False
  91. time.sleep(1)
  92. count += 1
  93. if not finished:
  94. log.fatal('wait_for_task: Task (%s) did not complete!' % task_dn)
  95. assert False
  96. ################################################################################
  97. #
  98. # Test Account Policy Plugin (0)
  99. #
  100. ################################################################################
  101. def test_acctpolicy(inst, args=None):
  102. # stop the plugin, and start it
  103. inst.plugins.disable(name=PLUGIN_ACCT_POLICY)
  104. inst.plugins.enable(name=PLUGIN_ACCT_POLICY)
  105. if args == "restart":
  106. return True
  107. CONFIG_DN = 'cn=config,cn=Account Policy Plugin,cn=plugins,cn=config'
  108. log.info('Testing ' + PLUGIN_ACCT_POLICY + '...')
  109. ############################################################################
  110. # Configure plugin
  111. ############################################################################
  112. # Add the config entry
  113. try:
  114. inst.add_s(Entry((CONFIG_DN, {
  115. 'objectclass': 'top extensibleObject'.split(),
  116. 'cn': 'config',
  117. 'alwaysrecordlogin': 'yes',
  118. 'stateattrname': 'lastLoginTime'
  119. })))
  120. except ldap.ALREADY_EXISTS:
  121. try:
  122. inst.modify_s(CONFIG_DN,
  123. [(ldap.MOD_REPLACE, 'alwaysrecordlogin', 'yes'),
  124. (ldap.MOD_REPLACE, 'stateattrname', 'lastLoginTime')])
  125. except ldap.LDAPError, e:
  126. log.fatal('test_acctpolicy: Failed to modify config entry: error ' + e.message['desc'])
  127. assert False
  128. except ldap.LDAPError, e:
  129. log.fatal('test_acctpolicy: Failed to add config entry: error ' + e.message['desc'])
  130. assert False
  131. ############################################################################
  132. # Test plugin
  133. ############################################################################
  134. # Add an entry
  135. time.sleep(1)
  136. try:
  137. inst.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
  138. 'sn': '1',
  139. 'cn': 'user 1',
  140. 'uid': 'user1',
  141. 'userpassword': 'password'})))
  142. except ldap.LDAPError, e:
  143. log.fatal('test_acctpolicy: Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
  144. assert False
  145. # bind as user
  146. try:
  147. inst.simple_bind_s(USER1_DN, "password")
  148. except ldap.LDAPError, e:
  149. log.fatal('test_acctpolicy: Failed to bind as user1: ' + e.message['desc'])
  150. assert False
  151. # Bind as Root DN
  152. time.sleep(1)
  153. try:
  154. inst.simple_bind_s(DN_DM, PASSWORD)
  155. except ldap.LDAPError, e:
  156. log.fatal('test_acctpolicy: Failed to bind as rootDN: ' + e.message['desc'])
  157. assert False
  158. # Check lastLoginTime of USER1
  159. try:
  160. entries = inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'lastLoginTime=*')
  161. if not entries:
  162. log.fatal('test_acctpolicy: Search failed to find an entry with lastLoginTime.')
  163. assert False
  164. except ldap.LDAPError, e:
  165. log.fatal('test_acctpolicy: Search failed: ' + e.message['desc'])
  166. assert False
  167. ############################################################################
  168. # Change config - change the stateAttrName to a new attribute
  169. ############################################################################
  170. try:
  171. inst.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'stateattrname', 'testLastLoginTime')])
  172. except ldap.LDAPError, e:
  173. log.fatal('test_acctpolicy: Failed to modify config entry: error ' + e.message['desc'])
  174. assert False
  175. ############################################################################
  176. # Test plugin
  177. ############################################################################
  178. time.sleep(1)
  179. # login as user
  180. try:
  181. inst.simple_bind_s(USER1_DN, "password")
  182. except ldap.LDAPError, e:
  183. log.fatal('test_acctpolicy: Failed to bind(2nd) as user1: ' + e.message['desc'])
  184. assert False
  185. time.sleep(1)
  186. # Bind as Root DN
  187. try:
  188. inst.simple_bind_s(DN_DM, PASSWORD)
  189. except ldap.LDAPError, e:
  190. log.fatal('test_acctpolicy: Failed to bind as rootDN: ' + e.message['desc'])
  191. assert False
  192. # Check testLastLoginTime was added to USER1
  193. try:
  194. entries = inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(testLastLoginTime=*)')
  195. if not entries:
  196. log.fatal('test_acctpolicy: Search failed to find an entry with testLastLoginTime.')
  197. assert False
  198. except ldap.LDAPError, e:
  199. log.fatal('test_acctpolicy: Search failed: ' + e.message['desc'])
  200. assert False
  201. ############################################################################
  202. # Test plugin dependency
  203. ############################################################################
  204. test_dependency(inst, PLUGIN_ACCT_POLICY)
  205. ############################################################################
  206. # Cleanup
  207. ############################################################################
  208. try:
  209. inst.delete_s(USER1_DN)
  210. except ldap.LDAPError, e:
  211. log.fatal('test_acctpolicy: Failed to delete test entry: ' + e.message['desc'])
  212. assert False
  213. ############################################################################
  214. # Test passed
  215. ############################################################################
  216. log.info('test_acctpolicy: PASS\n')
  217. return
  218. ################################################################################
  219. #
  220. # Test Attribute Uniqueness Plugin (1)
  221. #
  222. ################################################################################
  223. def test_attruniq(inst, args=None):
  224. # stop the plugin, and start it
  225. inst.plugins.disable(name=PLUGIN_ATTR_UNIQUENESS)
  226. inst.plugins.enable(name=PLUGIN_ATTR_UNIQUENESS)
  227. if args == "restart":
  228. return
  229. log.info('Testing ' + PLUGIN_ATTR_UNIQUENESS + '...')
  230. ############################################################################
  231. # Configure plugin
  232. ############################################################################
  233. try:
  234. inst.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  235. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'uid')])
  236. except ldap.LDAPError, e:
  237. log.fatal('test_attruniq: Failed to configure plugin for "uid": error ' + e.message['desc'])
  238. assert False
  239. ############################################################################
  240. # Test plugin
  241. ############################################################################
  242. # Add an entry
  243. try:
  244. inst.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
  245. 'sn': '1',
  246. 'cn': 'user 1',
  247. 'uid': 'user1',
  248. 'mail': '[email protected]',
  249. 'mailAlternateAddress' : '[email protected]',
  250. 'userpassword': 'password'})))
  251. except ldap.LDAPError, e:
  252. log.fatal('test_attruniq: Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
  253. assert False
  254. # Add an entry with a duplicate "uid"
  255. try:
  256. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  257. 'sn': '2',
  258. 'cn': 'user 2',
  259. 'uid': 'user2',
  260. 'uid': 'user1',
  261. 'userpassword': 'password'})))
  262. except ldap.CONSTRAINT_VIOLATION:
  263. pass
  264. else:
  265. log.fatal('test_attruniq: Adding of 2nd entry(uid) incorrectly succeeded')
  266. assert False
  267. ############################################################################
  268. # Change config to use "mail" instead of "uid"
  269. ############################################################################
  270. try:
  271. inst.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  272. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'mail')])
  273. except ldap.LDAPError, e:
  274. log.fatal('test_attruniq: Failed to configure plugin for "mail": error ' + e.message['desc'])
  275. assert False
  276. ############################################################################
  277. # Test plugin - Add an entry, that has a duplicate "mail" value
  278. ############################################################################
  279. try:
  280. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  281. 'sn': '2',
  282. 'cn': 'user 2',
  283. 'uid': 'user2',
  284. 'mail': '[email protected]',
  285. 'userpassword': 'password'})))
  286. except ldap.CONSTRAINT_VIOLATION:
  287. pass
  288. else:
  289. log.fatal('test_attruniq: Adding of 2nd entry(mail) incorrectly succeeded')
  290. assert False
  291. ############################################################################
  292. # Reconfigure plugin for mail and mailAlternateAddress
  293. ############################################################################
  294. try:
  295. inst.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  296. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'mail'),
  297. (ldap.MOD_ADD, 'uniqueness-attribute-name',
  298. 'mailAlternateAddress')])
  299. except ldap.LDAPError, e:
  300. log.error('test_attruniq: Failed to reconfigure plugin for "mail mailAlternateAddress": error ' + e.message['desc'])
  301. assert False
  302. ############################################################################
  303. # Test plugin - Add an entry, that has a duplicate "mail" value
  304. ############################################################################
  305. try:
  306. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  307. 'sn': '2',
  308. 'cn': 'user 2',
  309. 'uid': 'user2',
  310. 'mail': '[email protected]',
  311. 'userpassword': 'password'})))
  312. except ldap.CONSTRAINT_VIOLATION:
  313. pass
  314. else:
  315. log.error('test_attruniq: Adding of 3rd entry(mail) incorrectly succeeded')
  316. assert False
  317. ############################################################################
  318. # Test plugin - Add an entry, that has a duplicate "mailAlternateAddress" value
  319. ############################################################################
  320. try:
  321. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  322. 'sn': '2',
  323. 'cn': 'user 2',
  324. 'uid': 'user2',
  325. 'mailAlternateAddress': '[email protected]',
  326. 'userpassword': 'password'})))
  327. except ldap.CONSTRAINT_VIOLATION:
  328. pass
  329. else:
  330. log.error('test_attruniq: Adding of 4th entry(mailAlternateAddress) incorrectly succeeded')
  331. assert False
  332. ############################################################################
  333. # Test plugin - Add an entry, that has a duplicate "mail" value conflicting mailAlternateAddress
  334. ############################################################################
  335. try:
  336. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  337. 'sn': '2',
  338. 'cn': 'user 2',
  339. 'uid': 'user2',
  340. 'mail': '[email protected]',
  341. 'userpassword': 'password'})))
  342. except ldap.CONSTRAINT_VIOLATION:
  343. pass
  344. else:
  345. log.error('test_attruniq: Adding of 5th entry(mailAlternateAddress) incorrectly succeeded')
  346. assert False
  347. ############################################################################
  348. # Test plugin - Add an entry, that has a duplicate "mailAlternateAddress" conflicting mail
  349. ############################################################################
  350. try:
  351. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  352. 'sn': '2',
  353. 'cn': 'user 2',
  354. 'uid': 'user2',
  355. 'mailAlternateAddress': '[email protected]',
  356. 'userpassword': 'password'})))
  357. except ldap.CONSTRAINT_VIOLATION:
  358. pass
  359. else:
  360. log.error('test_attruniq: Adding of 6th entry(mail) incorrectly succeeded')
  361. assert False
  362. ############################################################################
  363. # Test plugin dependency
  364. ############################################################################
  365. test_dependency(inst, PLUGIN_ATTR_UNIQUENESS)
  366. ############################################################################
  367. # Cleanup
  368. ############################################################################
  369. try:
  370. inst.delete_s(USER1_DN)
  371. except ldap.LDAPError, e:
  372. log.fatal('test_attruniq: Failed to delete test entry: ' + e.message['desc'])
  373. assert False
  374. ############################################################################
  375. # Test passed
  376. ############################################################################
  377. log.info('test_attruniq: PASS\n')
  378. return
  379. ################################################################################
  380. #
  381. # Test Auto Membership Plugin (2)
  382. #
  383. ################################################################################
  384. def test_automember(inst, args=None):
  385. # stop the plugin, and start it
  386. inst.plugins.disable(name=PLUGIN_AUTOMEMBER)
  387. inst.plugins.enable(name=PLUGIN_AUTOMEMBER)
  388. if args == "restart":
  389. return
  390. CONFIG_DN = 'cn=config,cn=' + PLUGIN_AUTOMEMBER + ',cn=plugins,cn=config'
  391. log.info('Testing ' + PLUGIN_AUTOMEMBER + '...')
  392. ############################################################################
  393. # Configure plugin
  394. ############################################################################
  395. # Add the automember group
  396. try:
  397. inst.add_s(Entry((GROUP_DN, {
  398. 'objectclass': 'top extensibleObject'.split(),
  399. 'cn': 'group'
  400. })))
  401. except ldap.LDAPError, e:
  402. log.fatal('test_automember: Failed to add group: error ' + e.message['desc'])
  403. assert False
  404. # Add ou=branch1
  405. try:
  406. inst.add_s(Entry((BRANCH1_DN, {
  407. 'objectclass': 'top extensibleObject'.split(),
  408. 'ou': 'branch1'
  409. })))
  410. except ldap.LDAPError, e:
  411. log.fatal('test_automember: Failed to add branch1: error ' + e.message['desc'])
  412. assert False
  413. # Add ou=branch2
  414. try:
  415. inst.add_s(Entry((BRANCH2_DN, {
  416. 'objectclass': 'top extensibleObject'.split(),
  417. 'ou': 'branch2'
  418. })))
  419. except ldap.LDAPError, e:
  420. log.fatal('test_automember: Failed to add branch2: error ' + e.message['desc'])
  421. assert False
  422. # Add the automember config entry
  423. try:
  424. inst.add_s(Entry((CONFIG_DN, {
  425. 'objectclass': 'top autoMemberDefinition'.split(),
  426. 'cn': 'config',
  427. 'autoMemberScope': 'ou=branch1,' + DEFAULT_SUFFIX,
  428. 'autoMemberFilter': 'objectclass=top',
  429. 'autoMemberDefaultGroup': 'cn=group,' + DEFAULT_SUFFIX,
  430. 'autoMemberGroupingAttr': 'member:dn'
  431. })))
  432. except ldap.LDAPError, e:
  433. log.fatal('test_automember: Failed to add config entry: error ' + e.message['desc'])
  434. assert False
  435. ############################################################################
  436. # Test the plugin
  437. ############################################################################
  438. # Add a user that should get added to the group
  439. try:
  440. inst.add_s(Entry((BUSER1_DN, {
  441. 'objectclass': 'top extensibleObject'.split(),
  442. 'uid': 'user1'
  443. })))
  444. except ldap.LDAPError, e:
  445. log.fatal('test_automember: Failed to add user: error ' + e.message['desc'])
  446. assert False
  447. # Check the group
  448. try:
  449. entries = inst.search_s(GROUP_DN, ldap.SCOPE_BASE,
  450. '(member=' + BUSER1_DN + ')')
  451. if not entries:
  452. log.fatal('test_automember: Search failed to find member user1')
  453. assert False
  454. except ldap.LDAPError, e:
  455. log.fatal('test_automember: Search failed: ' + e.message['desc'])
  456. assert False
  457. ############################################################################
  458. # Change config
  459. ############################################################################
  460. try:
  461. inst.modify_s(CONFIG_DN,
  462. [(ldap.MOD_REPLACE, 'autoMemberGroupingAttr', 'uniquemember:dn'),
  463. (ldap.MOD_REPLACE, 'autoMemberScope', 'ou=branch2,' + DEFAULT_SUFFIX)])
  464. except ldap.LDAPError, e:
  465. log.fatal('test_automember: Failed to modify config entry: error ' + e.message['desc'])
  466. assert False
  467. ############################################################################
  468. # Test plugin
  469. ############################################################################
  470. # Add a user that should get added to the group
  471. try:
  472. inst.add_s(Entry((BUSER2_DN, {
  473. 'objectclass': 'top extensibleObject'.split(),
  474. 'uid': 'user2'
  475. })))
  476. except ldap.LDAPError, e:
  477. log.fatal('test_automember: Failed to user to branch2: error ' + e.message['desc'])
  478. assert False
  479. # Check the group
  480. try:
  481. entries = inst.search_s(GROUP_DN, ldap.SCOPE_BASE,
  482. '(uniquemember=' + BUSER2_DN + ')')
  483. if not entries:
  484. log.fatal('test_automember: Search failed to find uniquemember user2')
  485. assert False
  486. except ldap.LDAPError, e:
  487. log.fatal('test_automember: Search failed: ' + e.message['desc'])
  488. assert False
  489. ############################################################################
  490. # Test Task
  491. ############################################################################
  492. # Disable plugin
  493. inst.plugins.disable(name=PLUGIN_AUTOMEMBER)
  494. # Add an entry that should be picked up by automember - verify it is not(yet)
  495. try:
  496. inst.add_s(Entry((BUSER3_DN, {
  497. 'objectclass': 'top extensibleObject'.split(),
  498. 'uid': 'user3'
  499. })))
  500. except ldap.LDAPError, e:
  501. log.fatal('test_automember: Failed to user3 to branch2: error ' + e.message['desc'])
  502. assert False
  503. # Check the group - uniquemember should not exist
  504. try:
  505. entries = inst.search_s(GROUP_DN, ldap.SCOPE_BASE,
  506. '(uniquemember=' + BUSER3_DN + ')')
  507. if entries:
  508. log.fatal('test_automember: user3 was incorrectly added to the group')
  509. assert False
  510. except ldap.LDAPError, e:
  511. log.fatal('test_automember: Search failed: ' + e.message['desc'])
  512. assert False
  513. # Enable plugin
  514. inst.plugins.enable(name=PLUGIN_AUTOMEMBER)
  515. TASK_DN = 'cn=task-' + str(int(time.time())) + ',cn=automember rebuild membership,cn=tasks,cn=config'
  516. # Add the task
  517. try:
  518. inst.add_s(Entry((TASK_DN, {
  519. 'objectclass': 'top extensibleObject'.split(),
  520. 'basedn': 'ou=branch2,' + DEFAULT_SUFFIX,
  521. 'filter': 'objectclass=top'})))
  522. except ldap.LDAPError, e:
  523. log.fatal('test_automember: Failed to add task: error ' + e.message['desc'])
  524. assert False
  525. wait_for_task(inst, TASK_DN)
  526. # Verify the fixup task worked
  527. try:
  528. entries = inst.search_s(GROUP_DN, ldap.SCOPE_BASE,
  529. '(uniquemember=' + BUSER3_DN + ')')
  530. if not entries:
  531. log.fatal('test_automember: user3 was not added to the group')
  532. assert False
  533. except ldap.LDAPError, e:
  534. log.fatal('test_automember: Search failed: ' + e.message['desc'])
  535. assert False
  536. ############################################################################
  537. # Test plugin dependency
  538. ############################################################################
  539. test_dependency(inst, PLUGIN_AUTOMEMBER)
  540. ############################################################################
  541. # Cleanup
  542. ############################################################################
  543. try:
  544. inst.delete_s(BUSER1_DN)
  545. except ldap.LDAPError, e:
  546. log.fatal('test_automember: Failed to delete test entry1: ' + e.message['desc'])
  547. assert False
  548. try:
  549. inst.delete_s(BUSER2_DN)
  550. except ldap.LDAPError, e:
  551. log.fatal('test_automember: Failed to delete test entry2: ' + e.message['desc'])
  552. assert False
  553. try:
  554. inst.delete_s(BUSER3_DN)
  555. except ldap.LDAPError, e:
  556. log.fatal('test_automember: Failed to delete test entry3: ' + e.message['desc'])
  557. assert False
  558. try:
  559. inst.delete_s(BRANCH1_DN)
  560. except ldap.LDAPError, e:
  561. log.fatal('test_automember: Failed to delete branch1: ' + e.message['desc'])
  562. assert False
  563. try:
  564. inst.delete_s(BRANCH2_DN)
  565. except ldap.LDAPError, e:
  566. log.fatal('test_automember: Failed to delete test branch2: ' + e.message['desc'])
  567. assert False
  568. try:
  569. inst.delete_s(GROUP_DN)
  570. except ldap.LDAPError, e:
  571. log.fatal('test_automember: Failed to delete test group: ' + e.message['desc'])
  572. assert False
  573. try:
  574. inst.delete_s(CONFIG_DN)
  575. except ldap.LDAPError, e:
  576. log.fatal('test_automember: Failed to delete plugin config entry: ' + e.message['desc'])
  577. assert False
  578. ############################################################################
  579. # Test passed
  580. ############################################################################
  581. log.info('test_automember: PASS\n')
  582. return
  583. ################################################################################
  584. #
  585. # Test DNA Plugin (3)
  586. #
  587. ################################################################################
  588. def test_dna(inst, args=None):
  589. # stop the plugin, and start it
  590. inst.plugins.disable(name=PLUGIN_DNA)
  591. inst.plugins.enable(name=PLUGIN_DNA)
  592. if args == "restart":
  593. return
  594. CONFIG_DN = 'cn=config,cn=' + PLUGIN_DNA + ',cn=plugins,cn=config'
  595. log.info('Testing ' + PLUGIN_DNA + '...')
  596. ############################################################################
  597. # Configure plugin
  598. ############################################################################
  599. try:
  600. inst.add_s(Entry((CONFIG_DN, {
  601. 'objectclass': 'top dnaPluginConfig'.split(),
  602. 'cn': 'config',
  603. 'dnatype': 'uidNumber',
  604. 'dnafilter': '(objectclass=top)',
  605. 'dnascope': DEFAULT_SUFFIX,
  606. 'dnaMagicRegen': '-1',
  607. 'dnaMaxValue': '50000',
  608. 'dnaNextValue': '1'
  609. })))
  610. except ldap.ALREADY_EXISTS:
  611. try:
  612. inst.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'dnaNextValue', '1'),
  613. (ldap.MOD_REPLACE, 'dnaMagicRegen', '-1')])
  614. except ldap.LDAPError, e:
  615. log.fatal('test_dna: Failed to set the DNA plugin: error ' + e.message['desc'])
  616. assert False
  617. except ldap.LDAPError, e:
  618. log.fatal('test_dna: Failed to add config entry: error ' + e.message['desc'])
  619. assert False
  620. ############################################################################
  621. # Test plugin
  622. ############################################################################
  623. try:
  624. inst.add_s(Entry((USER1_DN, {
  625. 'objectclass': 'top extensibleObject'.split(),
  626. 'uid': 'user1'
  627. })))
  628. except ldap.LDAPError, e:
  629. log.fatal('test_dna: Failed to user1: error ' + e.message['desc'])
  630. assert False
  631. # See if the entry now has the new uidNumber assignment - uidNumber=1
  632. try:
  633. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(uidNumber=1)')
  634. if not entries:
  635. log.fatal('test_dna: user1 was not updated - (looking for uidNumber: 1)')
  636. assert False
  637. except ldap.LDAPError, e:
  638. log.fatal('test_dna: Search for user1 failed: ' + e.message['desc'])
  639. assert False
  640. # Test the magic regen value
  641. try:
  642. inst.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'uidNumber', '-1')])
  643. except ldap.LDAPError, e:
  644. log.fatal('test_dna: Failed to set the magic reg value: error ' + e.message['desc'])
  645. assert False
  646. # See if the entry now has the new uidNumber assignment - uidNumber=2
  647. try:
  648. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(uidNumber=2)')
  649. if not entries:
  650. log.fatal('test_dna: user1 was not updated (looking for uidNumber: 2)')
  651. assert False
  652. except ldap.LDAPError, e:
  653. log.fatal('test_dna: Search for user1 failed: ' + e.message['desc'])
  654. assert False
  655. ################################################################################
  656. # Change the config
  657. ################################################################################
  658. try:
  659. inst.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'dnaMagicRegen', '-2')])
  660. except ldap.LDAPError, e:
  661. log.fatal('test_dna: Failed to set the magic reg value to -2: error ' + e.message['desc'])
  662. assert False
  663. ################################################################################
  664. # Test plugin
  665. ################################################################################
  666. # Test the magic regen value
  667. try:
  668. inst.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'uidNumber', '-2')])
  669. except ldap.LDAPError, e:
  670. log.fatal('test_dna: Failed to set the magic reg value: error ' + e.message['desc'])
  671. assert False
  672. # See if the entry now has the new uidNumber assignment - uidNumber=3
  673. try:
  674. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(uidNumber=3)')
  675. if not entries:
  676. log.fatal('test_dna: user1 was not updated (looking for uidNumber: 3)')
  677. assert False
  678. except ldap.LDAPError, e:
  679. log.fatal('test_dna: Search for user1 failed: ' + e.message['desc'])
  680. assert False
  681. ############################################################################
  682. # Test plugin dependency
  683. ############################################################################
  684. test_dependency(inst, PLUGIN_AUTOMEMBER)
  685. ############################################################################
  686. # Cleanup
  687. ############################################################################
  688. try:
  689. inst.delete_s(USER1_DN)
  690. except ldap.LDAPError, e:
  691. log.fatal('test_dna: Failed to delete test entry1: ' + e.message['desc'])
  692. assert False
  693. inst.plugins.disable(name=PLUGIN_DNA)
  694. ############################################################################
  695. # Test passed
  696. ############################################################################
  697. log.info('test_dna: PASS\n')
  698. return
  699. ################################################################################
  700. #
  701. # Test Linked Attrs Plugin (4)
  702. #
  703. ################################################################################
  704. def test_linkedattrs(inst, args=None):
  705. # stop the plugin, and start it
  706. inst.plugins.disable(name=PLUGIN_LINKED_ATTRS)
  707. inst.plugins.enable(name=PLUGIN_LINKED_ATTRS)
  708. if args == "restart":
  709. return
  710. CONFIG_DN = 'cn=config,cn=' + PLUGIN_LINKED_ATTRS + ',cn=plugins,cn=config'
  711. log.info('Testing ' + PLUGIN_LINKED_ATTRS + '...')
  712. ############################################################################
  713. # Configure plugin
  714. ############################################################################
  715. # Add test entries
  716. try:
  717. inst.add_s(Entry((USER1_DN, {
  718. 'objectclass': 'top extensibleObject'.split(),
  719. 'uid': 'user1'
  720. })))
  721. except ldap.LDAPError, e:
  722. log.fatal('test_linkedattrs: Failed to user1: error ' + e.message['desc'])
  723. assert False
  724. try:
  725. inst.add_s(Entry((USER2_DN, {
  726. 'objectclass': 'top extensibleObject'.split(),
  727. 'uid': 'user2'
  728. })))
  729. except ldap.LDAPError, e:
  730. log.fatal('test_linkedattrs: Failed to user1: error ' + e.message['desc'])
  731. assert False
  732. # Add the linked attrs config entry
  733. try:
  734. inst.add_s(Entry((CONFIG_DN, {
  735. 'objectclass': 'top extensibleObject'.split(),
  736. 'cn': 'config',
  737. 'linkType': 'directReport',
  738. 'managedType': 'manager'
  739. })))
  740. except ldap.LDAPError, e:
  741. log.fatal('test_linkedattrs: Failed to add config entry: error ' + e.message['desc'])
  742. assert False
  743. ############################################################################
  744. # Test plugin
  745. ############################################################################
  746. # Set "directReport" should add "manager" to the other entry
  747. try:
  748. inst.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'directReport', USER2_DN)])
  749. except ldap.LDAPError, e:
  750. log.fatal('test_linkedattrs: Failed to add "directReport" to user1: error ' + e.message['desc'])
  751. assert False
  752. # See if manager was added to the other entry
  753. try:
  754. entries = inst.search_s(USER2_DN, ldap.SCOPE_BASE, '(manager=*)')
  755. if not entries:
  756. log.fatal('test_linkedattrs: user2 missing "manager" attribute')
  757. assert False
  758. except ldap.LDAPError, e:
  759. log.fatal('test_linkedattrs: Search for user1 failed: ' + e.message['desc'])
  760. assert False
  761. # Remove "directReport" should remove "manager" to the other entry
  762. try:
  763. inst.modify_s(USER1_DN, [(ldap.MOD_DELETE, 'directReport', None)])
  764. except ldap.LDAPError, e:
  765. log.fatal('test_linkedattrs: Failed to delete directReport: error ' + e.message['desc'])
  766. assert False
  767. # See if manager was removed
  768. try:
  769. entries = inst.search_s(USER2_DN, ldap.SCOPE_BASE, '(manager=*)')
  770. if entries:
  771. log.fatal('test_linkedattrs: user2 "manager" attribute not removed')
  772. assert False
  773. except ldap.LDAPError, e:
  774. log.fatal('test_linkedattrs: Search for user1 failed: ' + e.message['desc'])
  775. assert False
  776. ############################################################################
  777. # Change the config - using linkType "indirectReport" now
  778. ############################################################################
  779. try:
  780. inst.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'linkType', 'indirectReport')])
  781. except ldap.LDAPError, e:
  782. log.error('test_linkedattrs: Failed to set linkTypee: error ' + e.message['desc'])
  783. assert False
  784. ############################################################################
  785. # Test plugin
  786. ############################################################################
  787. # Make sure the old linkType(directManager) is not working
  788. try:
  789. inst.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'directReport', USER2_DN)])
  790. except ldap.LDAPError, e:
  791. log.fatal('test_linkedattrs: Failed to add "directReport" to user1: error ' + e.message['desc'])
  792. assert False
  793. # See if manager was added to the other entry, better not be...
  794. try:
  795. entries = inst.search_s(USER2_DN, ldap.SCOPE_BASE, '(manager=*)')
  796. if entries:
  797. log.fatal('test_linkedattrs: user2 had "manager" added unexpectedly')
  798. assert False
  799. except ldap.LDAPError, e:
  800. log.fatal('test_linkedattrs: Search for user2 failed: ' + e.message['desc'])
  801. assert False
  802. # Now, set the new linkType "indirectReport", which should add "manager" to the other entry
  803. try:
  804. inst.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'indirectReport', USER2_DN)])
  805. except ldap.LDAPError, e:
  806. log.fatal('test_linkedattrs: Failed to add "indirectReport" to user1: error ' + e.message['desc'])
  807. assert False
  808. # See if manager was added to the other entry, better not be
  809. try:
  810. entries = inst.search_s(USER2_DN, ldap.SCOPE_BASE, '(manager=*)')
  811. if not entries:
  812. log.fatal('test_linkedattrs: user2 missing "manager"')
  813. assert False
  814. except ldap.LDAPError, e:
  815. log.fatal('test_linkedattrs: Search for user2 failed: ' + e.message['desc'])
  816. assert False
  817. # Remove "indirectReport" should remove "manager" to the other entry
  818. try:
  819. inst.modify_s(USER1_DN, [(ldap.MOD_DELETE, 'indirectReport', None)])
  820. except ldap.LDAPError, e:
  821. log.fatal('test_linkedattrs: Failed to delete directReport: error ' + e.message['desc'])
  822. assert False
  823. # See if manager was removed
  824. try:
  825. entries = inst.search_s(USER2_DN, ldap.SCOPE_BASE, '(manager=*)')
  826. if entries:
  827. log.fatal('test_linkedattrs: user2 "manager" attribute not removed')
  828. assert False
  829. except ldap.LDAPError, e:
  830. log.fatal('test_linkedattrs: Search for user1 failed: ' + e.message['desc'])
  831. assert False
  832. ############################################################################
  833. # Test Fixup Task
  834. ############################################################################
  835. # Disable plugin and make some updates that would of triggered the plugin
  836. inst.plugins.disable(name=PLUGIN_LINKED_ATTRS)
  837. try:
  838. inst.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'indirectReport', USER2_DN)])
  839. except ldap.LDAPError, e:
  840. log.fatal('test_linkedattrs: Failed to add "indirectReport" to user1: error ' + e.message['desc'])
  841. assert False
  842. # The entry should not have a manager attribute
  843. try:
  844. entries = inst.search_s(USER2_DN, ldap.SCOPE_BASE, '(manager=*)')
  845. if entries:
  846. log.fatal('test_linkedattrs: user2 incorrectly has a "manager" attr')
  847. assert False
  848. except ldap.LDAPError, e:
  849. log.fatal('test_linkedattrs: Search for user1 failed: ' + e.message['desc'])
  850. assert False
  851. # Enable the plugin and rerun the task entry
  852. inst.plugins.enable(name=PLUGIN_LINKED_ATTRS)
  853. # Add the task again
  854. TASK_DN = 'cn=task-' + str(int(time.time())) + ',cn=fixup linked attributes,cn=tasks,cn=config'
  855. try:
  856. inst.add_s(Entry(('cn=task-' + str(int(time.time())) + ',cn=fixup linked attributes,cn=tasks,cn=config', {
  857. 'objectclass': 'top extensibleObject'.split(),
  858. 'basedn': DEFAULT_SUFFIX,
  859. 'filter': 'objectclass=top'})))
  860. except ldap.LDAPError, e:
  861. log.fatal('test_linkedattrs: Failed to add task: error ' + e.message['desc'])
  862. assert False
  863. wait_for_task(inst, TASK_DN)
  864. # Check if user2 now has a manager attribute now
  865. try:
  866. entries = inst.search_s(USER2_DN, ldap.SCOPE_BASE, '(manager=*)')
  867. if not entries:
  868. log.fatal('test_linkedattrs: task failed: user2 missing "manager" attr')
  869. assert False
  870. except ldap.LDAPError, e:
  871. log.fatal('test_linkedattrs: Search for user1 failed: ' + e.message['desc'])
  872. assert False
  873. ############################################################################
  874. # Test plugin dependency
  875. ############################################################################
  876. test_dependency(inst, PLUGIN_LINKED_ATTRS)
  877. ############################################################################
  878. # Cleanup
  879. ############################################################################
  880. try:
  881. inst.delete_s(USER1_DN)
  882. except ldap.LDAPError, e:
  883. log.fatal('test_linkedattrs: Failed to delete test entry1: ' + e.message['desc'])
  884. assert False
  885. try:
  886. inst.delete_s(USER2_DN)
  887. except ldap.LDAPError, e:
  888. log.fatal('test_linkedattrs: Failed to delete test entry2: ' + e.message['desc'])
  889. assert False
  890. try:
  891. inst.delete_s(CONFIG_DN)
  892. except ldap.LDAPError, e:
  893. log.fatal('test_linkedattrs: Failed to delete plugin config entry: ' + e.message['desc'])
  894. assert False
  895. ############################################################################
  896. # Test passed
  897. ############################################################################
  898. log.info('test_linkedattrs: PASS\n')
  899. return
  900. ################################################################################
  901. #
  902. # Test MemberOf Plugin (5)
  903. #
  904. ################################################################################
  905. def test_memberof(inst, args=None):
  906. # stop the plugin, and start it
  907. inst.plugins.disable(name=PLUGIN_MEMBER_OF)
  908. inst.plugins.enable(name=PLUGIN_MEMBER_OF)
  909. if args == "restart":
  910. return
  911. PLUGIN_DN = 'cn=' + PLUGIN_MEMBER_OF + ',cn=plugins,cn=config'
  912. SHARED_CONFIG_DN = 'cn=memberOf Config,' + DEFAULT_SUFFIX
  913. log.info('Testing ' + PLUGIN_MEMBER_OF + '...')
  914. ############################################################################
  915. # Configure plugin
  916. ############################################################################
  917. try:
  918. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'memberofgroupattr', 'member')])
  919. except ldap.LDAPError, e:
  920. log.fatal('test_memberof: Failed to update config(member): error ' + e.message['desc'])
  921. assert False
  922. ############################################################################
  923. # Test plugin
  924. ############################################################################
  925. # Add our test entries
  926. try:
  927. inst.add_s(Entry((USER1_DN, {
  928. 'objectclass': 'top extensibleObject'.split(),
  929. 'uid': 'user1'
  930. })))
  931. except ldap.LDAPError, e:
  932. log.fatal('test_memberof: Failed to add user1: error ' + e.message['desc'])
  933. assert False
  934. try:
  935. inst.add_s(Entry((GROUP_DN, {
  936. 'objectclass': 'top groupOfNames groupOfUniqueNames extensibleObject'.split(),
  937. 'cn': 'group',
  938. 'member': USER1_DN
  939. })))
  940. except ldap.LDAPError, e:
  941. log.fatal('test_memberof: Failed to add group: error ' + e.message['desc'])
  942. assert False
  943. try:
  944. inst.add_s(Entry((SHARED_CONFIG_DN, {
  945. 'objectclass': 'top extensibleObject'.split(),
  946. 'memberofgroupattr': 'member',
  947. 'memberofattr': 'memberof'
  948. })))
  949. except ldap.LDAPError, e:
  950. log.fatal('test_memberof: Failed to shared config entry: error ' + e.message['desc'])
  951. assert False
  952. # Check if the user now has a "memberOf" attribute
  953. try:
  954. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  955. if not entries:
  956. log.fatal('test_memberof: user1 missing memberOf')
  957. assert False
  958. except ldap.LDAPError, e:
  959. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  960. assert False
  961. # Remove "member" should remove "memberOf" from the entry
  962. try:
  963. inst.modify_s(GROUP_DN, [(ldap.MOD_DELETE, 'member', None)])
  964. except ldap.LDAPError, e:
  965. log.fatal('test_memberof: Failed to delete member: error ' + e.message['desc'])
  966. assert False
  967. # Check that "memberOf" was removed
  968. try:
  969. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  970. if entries:
  971. log.fatal('test_memberof: user1 incorrectly has memberOf attr')
  972. assert False
  973. except ldap.LDAPError, e:
  974. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  975. assert False
  976. ############################################################################
  977. # Change the config
  978. ############################################################################
  979. try:
  980. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'memberofgroupattr', 'uniquemember')])
  981. except ldap.LDAPError, e:
  982. log.fatal('test_memberof: Failed to update config(uniquemember): error ' + e.message['desc'])
  983. assert False
  984. ############################################################################
  985. # Test plugin
  986. ############################################################################
  987. try:
  988. inst.modify_s(GROUP_DN, [(ldap.MOD_REPLACE, 'uniquemember', USER1_DN)])
  989. except ldap.LDAPError, e:
  990. log.fatal('test_memberof: Failed to add uniquemember: error ' + e.message['desc'])
  991. assert False
  992. # Check if the user now has a "memberOf" attribute
  993. try:
  994. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  995. if not entries:
  996. log.fatal('test_memberof: user1 missing memberOf')
  997. assert False
  998. except ldap.LDAPError, e:
  999. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1000. assert False
  1001. # Remove "uniquemember" should remove "memberOf" from the entry
  1002. try:
  1003. inst.modify_s(GROUP_DN, [(ldap.MOD_DELETE, 'uniquemember', None)])
  1004. except ldap.LDAPError, e:
  1005. log.fatal('test_memberof: Failed to delete member: error ' + e.message['desc'])
  1006. assert False
  1007. # Check that "memberOf" was removed
  1008. try:
  1009. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1010. if entries:
  1011. log.fatal('test_memberof: user1 incorrectly has memberOf attr')
  1012. assert False
  1013. except ldap.LDAPError, e:
  1014. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1015. assert False
  1016. ############################################################################
  1017. # Set the shared config entry and test the plugin
  1018. ############################################################################
  1019. # The shared config entry uses "member" - the above test uses "uniquemember"
  1020. try:
  1021. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, CONFIG_AREA, SHARED_CONFIG_DN)])
  1022. except ldap.LDAPError, e:
  1023. log.fatal('test_memberof: Failed to set plugin area: error ' + e.message['desc'])
  1024. assert False
  1025. # Delete the test entries then readd them to start with a clean slate
  1026. try:
  1027. inst.delete_s(USER1_DN)
  1028. except ldap.LDAPError, e:
  1029. log.fatal('test_memberof: Failed to delete test entry1: ' + e.message['desc'])
  1030. assert False
  1031. try:
  1032. inst.delete_s(GROUP_DN)
  1033. except ldap.LDAPError, e:
  1034. log.fatal('test_memberof: Failed to delete test group: ' + e.message['desc'])
  1035. assert False
  1036. try:
  1037. inst.add_s(Entry((USER1_DN, {
  1038. 'objectclass': 'top extensibleObject'.split(),
  1039. 'uid': 'user1'
  1040. })))
  1041. except ldap.LDAPError, e:
  1042. log.fatal('test_memberof: Failed to add user1: error ' + e.message['desc'])
  1043. assert False
  1044. try:
  1045. inst.add_s(Entry((GROUP_DN, {
  1046. 'objectclass': 'top groupOfNames groupOfUniqueNames extensibleObject'.split(),
  1047. 'cn': 'group',
  1048. 'member': USER1_DN
  1049. })))
  1050. except ldap.LDAPError, e:
  1051. log.fatal('test_memberof: Failed to add group: error ' + e.message['desc'])
  1052. assert False
  1053. # Test the shared config
  1054. # Check if the user now has a "memberOf" attribute
  1055. try:
  1056. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1057. if not entries:
  1058. log.fatal('test_memberof: user1 missing memberOf')
  1059. assert False
  1060. except ldap.LDAPError, e:
  1061. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1062. assert False
  1063. # Remove "member" should remove "memberOf" from the entry
  1064. try:
  1065. inst.modify_s(GROUP_DN, [(ldap.MOD_DELETE, 'member', None)])
  1066. except ldap.LDAPError, e:
  1067. log.fatal('test_memberof: Failed to delete member: error ' + e.message['desc'])
  1068. assert False
  1069. # Check that "memberOf" was removed
  1070. try:
  1071. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1072. if entries:
  1073. log.fatal('test_memberof: user1 incorrectly has memberOf attr')
  1074. assert False
  1075. except ldap.LDAPError, e:
  1076. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1077. assert False
  1078. ############################################################################
  1079. # Change the shared config entry to use 'uniquemember' and test the plugin
  1080. ############################################################################
  1081. try:
  1082. inst.modify_s(SHARED_CONFIG_DN, [(ldap.MOD_REPLACE, 'memberofgroupattr', 'uniquemember')])
  1083. except ldap.LDAPError, e:
  1084. log.fatal('test_memberof: Failed to set shared plugin entry(uniquemember): error '
  1085. + e.message['desc'])
  1086. assert False
  1087. try:
  1088. inst.modify_s(GROUP_DN, [(ldap.MOD_REPLACE, 'uniquemember', USER1_DN)])
  1089. except ldap.LDAPError, e:
  1090. log.fatal('test_memberof: Failed to add uniquemember: error ' + e.message['desc'])
  1091. assert False
  1092. # Check if the user now has a "memberOf" attribute
  1093. try:
  1094. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1095. if not entries:
  1096. log.fatal('test_memberof: user1 missing memberOf')
  1097. assert False
  1098. except ldap.LDAPError, e:
  1099. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1100. assert False
  1101. # Remove "uniquemember" should remove "memberOf" from the entry
  1102. try:
  1103. inst.modify_s(GROUP_DN, [(ldap.MOD_DELETE, 'uniquemember', None)])
  1104. except ldap.LDAPError, e:
  1105. log.fatal('test_memberof: Failed to delete member: error ' + e.message['desc'])
  1106. assert False
  1107. # Check that "memberOf" was removed
  1108. try:
  1109. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1110. if entries:
  1111. log.fatal('test_memberof: user1 incorrectly has memberOf attr')
  1112. assert False
  1113. except ldap.LDAPError, e:
  1114. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1115. assert False
  1116. ############################################################################
  1117. # Remove shared config from plugin, and retest
  1118. ############################################################################
  1119. # First change the plugin to use member before we move the shared config that uses uniquemember
  1120. try:
  1121. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'memberofgroupattr', 'member')])
  1122. except ldap.LDAPError, e:
  1123. log.fatal('test_memberof: Failed to update config(uniquemember): error ' + e.message['desc'])
  1124. assert False
  1125. # Remove shared config from plugin
  1126. try:
  1127. inst.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, CONFIG_AREA, None)])
  1128. except ldap.LDAPError, e:
  1129. log.fatal('test_memberof: Failed to add uniquemember: error ' + e.message['desc'])
  1130. assert False
  1131. try:
  1132. inst.modify_s(GROUP_DN, [(ldap.MOD_REPLACE, 'member', USER1_DN)])
  1133. except ldap.LDAPError, e:
  1134. log.fatal('test_memberof: Failed to add uniquemember: error ' + e.message['desc'])
  1135. assert False
  1136. # Check if the user now has a "memberOf" attribute
  1137. try:
  1138. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1139. if not entries:
  1140. log.fatal('test_memberof: user1 missing memberOf')
  1141. assert False
  1142. except ldap.LDAPError, e:
  1143. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1144. assert False
  1145. # Remove "uniquemember" should remove "memberOf" from the entry
  1146. try:
  1147. inst.modify_s(GROUP_DN, [(ldap.MOD_DELETE, 'member', None)])
  1148. except ldap.LDAPError, e:
  1149. log.fatal('test_memberof: Failed to delete member: error ' + e.message['desc'])
  1150. assert False
  1151. # Check that "memberOf" was removed
  1152. try:
  1153. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1154. if entries:
  1155. log.fatal('test_memberof: user1 incorrectly has memberOf attr')
  1156. assert False
  1157. except ldap.LDAPError, e:
  1158. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1159. assert False
  1160. ############################################################################
  1161. # Test Fixup Task
  1162. ############################################################################
  1163. inst.plugins.disable(name=PLUGIN_MEMBER_OF)
  1164. # First change the plugin to use uniquemember
  1165. try:
  1166. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'memberofgroupattr', 'uniquemember')])
  1167. except ldap.LDAPError, e:
  1168. log.fatal('test_memberof: Failed to update config(uniquemember): error ' + e.message['desc'])
  1169. assert False
  1170. # Add uniquemember, should not update USER1
  1171. try:
  1172. inst.modify_s(GROUP_DN, [(ldap.MOD_REPLACE, 'uniquemember', USER1_DN)])
  1173. except ldap.LDAPError, e:
  1174. log.fatal('test_memberof: Failed to add uniquemember: error ' + e.message['desc'])
  1175. assert False
  1176. # Check for "memberOf"
  1177. try:
  1178. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1179. if entries:
  1180. log.fatal('test_memberof: user1 incorrect has memberOf attr')
  1181. assert False
  1182. except ldap.LDAPError, e:
  1183. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1184. assert False
  1185. # Enable the plugin, and run the task
  1186. inst.plugins.enable(name=PLUGIN_MEMBER_OF)
  1187. TASK_DN = 'cn=task-' + str(int(time.time())) + ',' + DN_MBO_TASK
  1188. try:
  1189. inst.add_s(Entry((TASK_DN, {
  1190. 'objectclass': 'top extensibleObject'.split(),
  1191. 'basedn': DEFAULT_SUFFIX,
  1192. 'filter': 'objectclass=top'})))
  1193. except ldap.LDAPError, e:
  1194. log.fatal('test_memberof: Failed to add task: error ' + e.message['desc'])
  1195. assert False
  1196. wait_for_task(inst, TASK_DN)
  1197. # Check for "memberOf"
  1198. try:
  1199. entries = inst.search_s(USER1_DN, ldap.SCOPE_BASE, '(memberOf=*)')
  1200. if not entries:
  1201. log.fatal('test_memberof: user1 missing memberOf attr')
  1202. assert False
  1203. except ldap.LDAPError, e:
  1204. log.fatal('test_memberof: Search for user1 failed: ' + e.message['desc'])
  1205. assert False
  1206. ############################################################################
  1207. # Test plugin dependency
  1208. ############################################################################
  1209. test_dependency(inst, PLUGIN_MEMBER_OF)
  1210. ############################################################################
  1211. # Cleanup
  1212. ############################################################################
  1213. try:
  1214. inst.delete_s(USER1_DN)
  1215. except ldap.LDAPError, e:
  1216. log.fatal('test_memberof: Failed to delete test entry1: ' + e.message['desc'])
  1217. assert False
  1218. try:
  1219. inst.delete_s(GROUP_DN)
  1220. except ldap.LDAPError, e:
  1221. log.fatal('test_memberof: Failed to delete test group: ' + e.message['desc'])
  1222. assert False
  1223. try:
  1224. inst.delete_s(SHARED_CONFIG_DN)
  1225. except ldap.LDAPError, e:
  1226. log.fatal('test_memberof: Failed to delete shared config entry: ' + e.message['desc'])
  1227. assert False
  1228. ############################################################################
  1229. # Test passed
  1230. ############################################################################
  1231. log.info('test_memberof: PASS\n')
  1232. return
  1233. ################################################################################
  1234. #
  1235. # Test Managed Entry Plugin (6)
  1236. #
  1237. ################################################################################
  1238. def test_mep(inst, args=None):
  1239. # stop the plugin, and start it
  1240. inst.plugins.disable(name=PLUGIN_MANAGED_ENTRY)
  1241. inst.plugins.enable(name=PLUGIN_MANAGED_ENTRY)
  1242. if args == "restart":
  1243. return
  1244. USER_DN = 'uid=user1,ou=people,' + DEFAULT_SUFFIX
  1245. MEP_USER_DN = 'cn=user1,ou=groups,' + DEFAULT_SUFFIX
  1246. USER_DN2 = 'uid=user 1,ou=people,' + DEFAULT_SUFFIX
  1247. MEP_USER_DN2 = 'uid=user 1,ou=groups,' + DEFAULT_SUFFIX
  1248. CONFIG_DN = 'cn=config,cn=' + PLUGIN_MANAGED_ENTRY + ',cn=plugins,cn=config'
  1249. TEMPLATE_DN = 'cn=MEP Template,' + DEFAULT_SUFFIX
  1250. TEMPLATE_DN2 = 'cn=MEP Template2,' + DEFAULT_SUFFIX
  1251. log.info('Testing ' + PLUGIN_MANAGED_ENTRY + '...')
  1252. ############################################################################
  1253. # Configure plugin
  1254. ############################################################################
  1255. # Add our org units
  1256. try:
  1257. inst.add_s(Entry((PEOPLE_OU, {
  1258. 'objectclass': 'top extensibleObject'.split(),
  1259. 'ou': 'people'})))
  1260. except ldap.ALREADY_EXISTS:
  1261. pass
  1262. except ldap.LDAPError, e:
  1263. log.fatal('test_mep: Failed to add people org unit: error ' + e.message['desc'])
  1264. assert False
  1265. try:
  1266. inst.add_s(Entry((GROUP_OU, {
  1267. 'objectclass': 'top extensibleObject'.split(),
  1268. 'ou': 'people'})))
  1269. except ldap.ALREADY_EXISTS:
  1270. pass
  1271. except ldap.LDAPError, e:
  1272. log.fatal('test_mep: Failed to add people org unit: error ' + e.message['desc'])
  1273. assert False
  1274. # Add the template entry
  1275. try:
  1276. inst.add_s(Entry((TEMPLATE_DN, {
  1277. 'objectclass': 'top mepTemplateEntry extensibleObject'.split(),
  1278. 'cn': 'MEP Template',
  1279. 'mepRDNAttr': 'cn',
  1280. 'mepStaticAttr': 'objectclass: posixGroup|objectclass: extensibleObject'.split('|'),
  1281. 'mepMappedAttr': 'cn: $cn|uid: $cn|gidNumber: $uidNumber'.split('|')
  1282. })))
  1283. except ldap.LDAPError, e:
  1284. log.fatal('test_mep: Failed to add template entry: error ' + e.message['desc'])
  1285. assert False
  1286. # Add the config entry
  1287. try:
  1288. inst.add_s(Entry((CONFIG_DN, {
  1289. 'objectclass': 'top extensibleObject'.split(),
  1290. 'cn': 'config',
  1291. 'originScope': PEOPLE_OU,
  1292. 'originFilter': 'objectclass=posixAccount',
  1293. 'managedBase': GROUP_OU,
  1294. 'managedTemplate': TEMPLATE_DN
  1295. })))
  1296. except ldap.LDAPError, e:
  1297. log.fatal('test_mep: Failed to add config entry: error ' + e.message['desc'])
  1298. assert False
  1299. ############################################################################
  1300. # Test plugin
  1301. ############################################################################
  1302. # Add an entry that meets the MEP scope
  1303. try:
  1304. inst.add_s(Entry((USER_DN, {
  1305. 'objectclass': 'top posixAccount extensibleObject'.split(),
  1306. 'uid': 'user1',
  1307. 'cn': 'user1',
  1308. 'uidNumber': '1',
  1309. 'gidNumber': '1',
  1310. 'homeDirectory': '/home/user1'
  1311. })))
  1312. except ldap.LDAPError, e:
  1313. log.fatal('test_mep: Failed to user1: error ' + e.message['desc'])
  1314. assert False
  1315. # Check if a managed group entry was created
  1316. try:
  1317. inst.search_s(MEP_USER_DN, ldap.SCOPE_BASE, '(objectclass=top)')
  1318. except ldap.LDAPError, e:
  1319. log.fatal('test_mep: Unable to find MEP entry: ' + e.message['desc'])
  1320. assert False
  1321. ############################################################################
  1322. # Change the config
  1323. ############################################################################
  1324. # Add a new template entry
  1325. try:
  1326. inst.add_s(Entry((TEMPLATE_DN2, {
  1327. 'objectclass': 'top mepTemplateEntry extensibleObject'.split(),
  1328. 'cn': 'MEP Template2',
  1329. 'mepRDNAttr': 'uid',
  1330. 'mepStaticAttr': 'objectclass: posixGroup|objectclass: extensibleObject'.split('|'),
  1331. 'mepMappedAttr': 'cn: $uid|uid: $cn|gidNumber: $gidNumber'.split('|')
  1332. })))
  1333. except ldap.LDAPError, e:
  1334. log.fatal('test_mep: Failed to add template entry2: error ' + e.message['desc'])
  1335. assert False
  1336. # Set the new template dn
  1337. try:
  1338. inst.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'managedTemplate', TEMPLATE_DN2)])
  1339. except ldap.LDAPError, e:
  1340. log.fatal('test_mep: Failed to set mep plugin config: error ' + e.message['desc'])
  1341. assert False
  1342. ############################################################################
  1343. # Test plugin
  1344. ############################################################################
  1345. # Add an entry that meets the MEP scope
  1346. try:
  1347. inst.add_s(Entry((USER_DN2, {
  1348. 'objectclass': 'top posixAccount extensibleObject'.split(),
  1349. 'uid': 'user 1',
  1350. 'cn': 'user 1',
  1351. 'uidNumber': '1',
  1352. 'gidNumber': '1',
  1353. 'homeDirectory': '/home/user2'
  1354. })))
  1355. except ldap.LDAPError, e:
  1356. log.fatal('test_mep: Failed to user2: error ' + e.message['desc'])
  1357. assert False
  1358. # Check if a managed group entry was created
  1359. try:
  1360. inst.search_s(MEP_USER_DN2, ldap.SCOPE_BASE, '(objectclass=top)')
  1361. except ldap.LDAPError, e:
  1362. log.fatal('test_mep: Unable to find MEP entry2: ' + e.message['desc'])
  1363. assert False
  1364. ############################################################################
  1365. # Test plugin dependency
  1366. ############################################################################
  1367. test_dependency(inst, PLUGIN_MANAGED_ENTRY)
  1368. ############################################################################
  1369. # Cleanup
  1370. ############################################################################
  1371. try:
  1372. inst.delete_s(USER_DN)
  1373. except ldap.LDAPError, e:
  1374. log.fatal('test_mep: Failed to delete test user1: ' + e.message['desc'])
  1375. assert False
  1376. try:
  1377. inst.delete_s(USER_DN2)
  1378. except ldap.LDAPError, e:
  1379. log.fatal('test_mep: Failed to delete test user 2: ' + e.message['desc'])
  1380. assert False
  1381. try:
  1382. inst.delete_s(TEMPLATE_DN)
  1383. except ldap.LDAPError, e:
  1384. log.fatal('test_mep: Failed to delete template1: ' + e.message['desc'])
  1385. assert False
  1386. inst.plugins.disable(name=PLUGIN_MANAGED_ENTRY)
  1387. try:
  1388. inst.delete_s(TEMPLATE_DN2)
  1389. except ldap.LDAPError, e:
  1390. log.fatal('test_mep: Failed to delete template2: ' + e.message['desc'])
  1391. assert False
  1392. try:
  1393. inst.delete_s(CONFIG_DN)
  1394. except ldap.LDAPError, e:
  1395. log.fatal('test_mep: Failed to delete config: ' + e.message['desc'])
  1396. assert False
  1397. ############################################################################
  1398. # Test passed
  1399. ############################################################################
  1400. log.info('test_mep: PASS\n')
  1401. return
  1402. ################################################################################
  1403. #
  1404. # Test Passthru Plugin (7)
  1405. #
  1406. ################################################################################
  1407. def test_passthru(inst, args=None):
  1408. # Passthru is a bit picky about the state of the entry - we can't just restart it
  1409. if args == "restart":
  1410. return
  1411. # stop the plugin
  1412. inst.plugins.disable(name=PLUGIN_PASSTHRU)
  1413. PLUGIN_DN = 'cn=' + PLUGIN_PASSTHRU + ',cn=plugins,cn=config'
  1414. PASSTHRU_DN = 'uid=admin,dc=pass,dc=thru'
  1415. PASSTHRU_DN2 = 'uid=admin2,dc=pass2,dc=thru'
  1416. PASS_SUFFIX1 = 'dc=pass,dc=thru'
  1417. PASS_SUFFIX2 = 'dc=pass2,dc=thru'
  1418. PASS_BE2 = 'PASS2'
  1419. log.info('Testing ' + PLUGIN_PASSTHRU + '...')
  1420. ############################################################################
  1421. # Add a new "remote" instance, and a user for auth
  1422. ############################################################################
  1423. # Create second instance
  1424. passthru_inst = DirSrv(verbose=False)
  1425. # Args for the instance
  1426. args_instance[SER_HOST] = LOCALHOST
  1427. args_instance[SER_PORT] = 33333
  1428. args_instance[SER_SERVERID_PROP] = 'passthru'
  1429. args_instance[SER_CREATION_SUFFIX] = PASS_SUFFIX1
  1430. args_passthru_inst = args_instance.copy()
  1431. passthru_inst.allocate(args_passthru_inst)
  1432. passthru_inst.create()
  1433. passthru_inst.open()
  1434. # Create a second backend
  1435. passthru_inst.backend.create(PASS_SUFFIX2, {BACKEND_NAME: PASS_BE2})
  1436. passthru_inst.mappingtree.create(PASS_SUFFIX2, bename=PASS_BE2)
  1437. # Create the top of the tree
  1438. try:
  1439. passthru_inst.add_s(Entry((PASS_SUFFIX2, {
  1440. 'objectclass': 'top domain'.split(),
  1441. 'dc': 'pass2'})))
  1442. except ldap.ALREADY_EXISTS:
  1443. pass
  1444. except ldap.LDAPError, e:
  1445. log.fatal('test_passthru: Failed to create suffix entry: error ' + e.message['desc'])
  1446. passthru_inst.delete()
  1447. assert False
  1448. # Add user to suffix1
  1449. try:
  1450. passthru_inst.add_s(Entry((PASSTHRU_DN, {
  1451. 'objectclass': 'top extensibleObject'.split(),
  1452. 'uid': 'admin',
  1453. 'userpassword': 'password'
  1454. })))
  1455. except ldap.LDAPError, e:
  1456. log.fatal('test_passthru: Failed to admin1: error ' + e.message['desc'])
  1457. passthru_inst.delete()
  1458. assert False
  1459. # Add user to suffix 2
  1460. try:
  1461. passthru_inst.add_s(Entry((PASSTHRU_DN2, {
  1462. 'objectclass': 'top extensibleObject'.split(),
  1463. 'uid': 'admin2',
  1464. 'userpassword': 'password'
  1465. })))
  1466. except ldap.LDAPError, e:
  1467. log.fatal('test_passthru: Failed to admin2 : error ' + e.message['desc'])
  1468. passthru_inst.delete()
  1469. assert False
  1470. ############################################################################
  1471. # Configure and start plugin
  1472. ############################################################################
  1473. try:
  1474. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'nsslapd-pluginenabled', 'on'),
  1475. (ldap.MOD_REPLACE, 'nsslapd-pluginarg0', 'ldap://127.0.0.1:33333/dc=pass,dc=thru')])
  1476. except ldap.LDAPError, e:
  1477. log.fatal('test_passthru: Failed to set mep plugin config: error ' + e.message['desc'])
  1478. passthru_inst.delete()
  1479. assert False
  1480. ############################################################################
  1481. # Test plugin
  1482. ############################################################################
  1483. # login as user
  1484. try:
  1485. inst.simple_bind_s(PASSTHRU_DN, "password")
  1486. except ldap.LDAPError, e:
  1487. log.fatal('test_passthru: pass through bind failed: ' + e.message['desc'])
  1488. passthru_inst.delete()
  1489. assert False
  1490. ############################################################################
  1491. # Change the config
  1492. ############################################################################
  1493. # login as root DN
  1494. try:
  1495. inst.simple_bind_s(DN_DM, PASSWORD)
  1496. except ldap.LDAPError, e:
  1497. log.fatal('test_passthru: pass through bind failed: ' + e.message['desc'])
  1498. passthru_inst.delete()
  1499. assert False
  1500. try:
  1501. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'nsslapd-pluginarg0', 'ldap://127.0.0.1:33333/dc=pass2,dc=thru')])
  1502. except ldap.LDAPError, e:
  1503. log.fatal('test_passthru: Failed to set mep plugin config: error ' + e.message['desc'])
  1504. passthru_inst.delete()
  1505. assert False
  1506. ############################################################################
  1507. # Test plugin
  1508. ############################################################################
  1509. # login as user
  1510. try:
  1511. inst.simple_bind_s(PASSTHRU_DN2, "password")
  1512. except ldap.LDAPError, e:
  1513. log.fatal('test_passthru: pass through bind failed: ' + e.message['desc'])
  1514. passthru_inst.delete()
  1515. assert False
  1516. # login as root DN
  1517. try:
  1518. inst.simple_bind_s(DN_DM, PASSWORD)
  1519. except ldap.LDAPError, e:
  1520. log.fatal('test_passthru: pass through bind failed: ' + e.message['desc'])
  1521. passthru_inst.delete()
  1522. assert False
  1523. ############################################################################
  1524. # Test plugin dependency
  1525. ############################################################################
  1526. test_dependency(inst, PLUGIN_PASSTHRU)
  1527. ############################################################################
  1528. # Cleanup
  1529. ############################################################################
  1530. # remove the passthru instance
  1531. passthru_inst.delete()
  1532. ############################################################################
  1533. # Test passed
  1534. ############################################################################
  1535. log.info('test_passthru: PASS\n')
  1536. return
  1537. ################################################################################
  1538. #
  1539. # Test Referential Integrity Plugin (8)
  1540. #
  1541. ################################################################################
  1542. def test_referint(inst, args=None):
  1543. # stop the plugin, and start it
  1544. inst.plugins.disable(name=PLUGIN_REFER_INTEGRITY)
  1545. inst.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
  1546. if args == "restart":
  1547. return
  1548. log.info('Testing ' + PLUGIN_REFER_INTEGRITY + '...')
  1549. PLUGIN_DN = 'cn=' + PLUGIN_REFER_INTEGRITY + ',cn=plugins,cn=config'
  1550. SHARED_CONFIG_DN = 'cn=RI Config,' + DEFAULT_SUFFIX
  1551. ############################################################################
  1552. # Configure plugin
  1553. ############################################################################
  1554. try:
  1555. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'referint-membership-attr', 'member')])
  1556. except ldap.LDAPError, e:
  1557. log.fatal('test_referint: Failed to configure RI plugin: error ' + e.message['desc'])
  1558. assert False
  1559. ############################################################################
  1560. # Test plugin
  1561. ############################################################################
  1562. # Add some users and a group
  1563. try:
  1564. inst.add_s(Entry((USER1_DN, {
  1565. 'objectclass': 'top extensibleObject'.split(),
  1566. 'uid': 'user1'
  1567. })))
  1568. except ldap.LDAPError, e:
  1569. log.fatal('test_referint: Failed to add user1: error ' + e.message['desc'])
  1570. assert False
  1571. try:
  1572. inst.add_s(Entry((USER2_DN, {
  1573. 'objectclass': 'top extensibleObject'.split(),
  1574. 'uid': 'user2'
  1575. })))
  1576. except ldap.LDAPError, e:
  1577. log.fatal('test_referint: Failed to add user2: error ' + e.message['desc'])
  1578. assert False
  1579. try:
  1580. inst.add_s(Entry((GROUP_DN, {
  1581. 'objectclass': 'top extensibleObject'.split(),
  1582. 'cn': 'group',
  1583. 'member': USER1_DN,
  1584. 'uniquemember': USER2_DN
  1585. })))
  1586. except ldap.LDAPError, e:
  1587. log.fatal('test_referint: Failed to add group: error ' + e.message['desc'])
  1588. assert False
  1589. # Grab the referint log file from the plugin
  1590. try:
  1591. entries = inst.search_s(PLUGIN_DN, ldap.SCOPE_BASE, '(objectclass=top)')
  1592. REFERINT_LOGFILE = entries[0].getValue('referint-logfile')
  1593. except ldap.LDAPError, e:
  1594. log.fatal('test_referint: Unable to search plugin entry: ' + e.message['desc'])
  1595. assert False
  1596. # Add shared config entry
  1597. try:
  1598. inst.add_s(Entry((SHARED_CONFIG_DN, {
  1599. 'objectclass': 'top extensibleObject'.split(),
  1600. 'referint-membership-attr': 'member',
  1601. 'referint-update-delay': '0',
  1602. 'referint-logfile': REFERINT_LOGFILE,
  1603. 'referint-logchanges': '0'
  1604. })))
  1605. except ldap.LDAPError, e:
  1606. log.fatal('test_referint: Failed to shared config entry: error ' + e.message['desc'])
  1607. assert False
  1608. # Delete a user
  1609. try:
  1610. inst.delete_s(USER1_DN)
  1611. except ldap.LDAPError, e:
  1612. log.fatal('test_referint: Failed to delete user1: ' + e.message['desc'])
  1613. assert False
  1614. # Check for integrity
  1615. try:
  1616. entry = inst.search_s(GROUP_DN, ldap.SCOPE_BASE, '(member=' + USER1_DN + ')')
  1617. if entry:
  1618. log.fatal('test_referint: user1 was not removed from group')
  1619. assert False
  1620. except ldap.LDAPError, e:
  1621. log.fatal('test_referint: Unable to search group: ' + e.message['desc'])
  1622. assert False
  1623. ############################################################################
  1624. # Change the config
  1625. ############################################################################
  1626. try:
  1627. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'referint-membership-attr', 'uniquemember')])
  1628. except ldap.LDAPError, e:
  1629. log.fatal('test_referint: Failed to configure RI plugin: error ' + e.message['desc'])
  1630. assert False
  1631. ############################################################################
  1632. # Test plugin
  1633. ############################################################################
  1634. # Delete a user
  1635. try:
  1636. inst.delete_s(USER2_DN)
  1637. except ldap.LDAPError, e:
  1638. log.fatal('test_referint: Failed to delete user1: ' + e.message['desc'])
  1639. assert False
  1640. # Check for integrity
  1641. try:
  1642. entry = inst.search_s(GROUP_DN, ldap.SCOPE_BASE, '(uniquemember=' + USER2_DN + ')')
  1643. if entry:
  1644. log.fatal('test_referint: user2 was not removed from group')
  1645. assert False
  1646. except ldap.LDAPError, e:
  1647. log.fatal('test_referint: Unable to search group: ' + e.message['desc'])
  1648. assert False
  1649. ############################################################################
  1650. # Set the shared config entry and test the plugin
  1651. ############################################################################
  1652. # The shared config entry uses "member" - the above test used "uniquemember"
  1653. try:
  1654. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, CONFIG_AREA, SHARED_CONFIG_DN)])
  1655. except ldap.LDAPError, e:
  1656. log.fatal('test_referint: Failed to set plugin area: error ' + e.message['desc'])
  1657. assert False
  1658. # Delete the group, and readd everything
  1659. try:
  1660. inst.delete_s(GROUP_DN)
  1661. except ldap.LDAPError, e:
  1662. log.fatal('test_referint: Failed to delete group: ' + e.message['desc'])
  1663. assert False
  1664. try:
  1665. inst.add_s(Entry((USER1_DN, {
  1666. 'objectclass': 'top extensibleObject'.split(),
  1667. 'uid': 'user1'
  1668. })))
  1669. except ldap.LDAPError, e:
  1670. log.fatal('test_referint: Failed to add user1: error ' + e.message['desc'])
  1671. assert False
  1672. try:
  1673. inst.add_s(Entry((USER2_DN, {
  1674. 'objectclass': 'top extensibleObject'.split(),
  1675. 'uid': 'user2'
  1676. })))
  1677. except ldap.LDAPError, e:
  1678. log.fatal('test_referint: Failed to add user2: error ' + e.message['desc'])
  1679. assert False
  1680. try:
  1681. inst.add_s(Entry((GROUP_DN, {
  1682. 'objectclass': 'top extensibleObject'.split(),
  1683. 'cn': 'group',
  1684. 'member': USER1_DN,
  1685. 'uniquemember': USER2_DN
  1686. })))
  1687. except ldap.LDAPError, e:
  1688. log.fatal('test_referint: Failed to add group: error ' + e.message['desc'])
  1689. assert False
  1690. # Delete a user
  1691. try:
  1692. inst.delete_s(USER1_DN)
  1693. except ldap.LDAPError, e:
  1694. log.fatal('test_referint: Failed to delete user1: ' + e.message['desc'])
  1695. assert False
  1696. # Check for integrity
  1697. try:
  1698. entry = inst.search_s(GROUP_DN, ldap.SCOPE_BASE, '(member=' + USER1_DN + ')')
  1699. if entry:
  1700. log.fatal('test_referint: user1 was not removed from group')
  1701. assert False
  1702. except ldap.LDAPError, e:
  1703. log.fatal('test_referint: Unable to search group: ' + e.message['desc'])
  1704. assert False
  1705. ############################################################################
  1706. # Change the shared config entry to use 'uniquemember' and test the plugin
  1707. ############################################################################
  1708. try:
  1709. inst.modify_s(SHARED_CONFIG_DN, [(ldap.MOD_REPLACE, 'referint-membership-attr', 'uniquemember')])
  1710. except ldap.LDAPError, e:
  1711. log.fatal('test_referint: Failed to set shared plugin entry(uniquemember): error '
  1712. + e.message['desc'])
  1713. assert False
  1714. # Delete a user
  1715. try:
  1716. inst.delete_s(USER2_DN)
  1717. except ldap.LDAPError, e:
  1718. log.fatal('test_referint: Failed to delete user1: ' + e.message['desc'])
  1719. assert False
  1720. # Check for integrity
  1721. try:
  1722. entry = inst.search_s(GROUP_DN, ldap.SCOPE_BASE, '(uniquemember=' + USER2_DN + ')')
  1723. if entry:
  1724. log.fatal('test_referint: user2 was not removed from group')
  1725. assert False
  1726. except ldap.LDAPError, e:
  1727. log.fatal('test_referint: Unable to search group: ' + e.message['desc'])
  1728. assert False
  1729. ############################################################################
  1730. # Remove shared config from plugin, and retest
  1731. ############################################################################
  1732. # First change the plugin to use member before we move the shared config that uses uniquemember
  1733. try:
  1734. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'referint-membership-attr', 'member')])
  1735. except ldap.LDAPError, e:
  1736. log.fatal('test_referint: Failed to update config(uniquemember): error ' + e.message['desc'])
  1737. assert False
  1738. # Remove shared config from plugin
  1739. try:
  1740. inst.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, CONFIG_AREA, None)])
  1741. except ldap.LDAPError, e:
  1742. log.fatal('test_referint: Failed to add uniquemember: error ' + e.message['desc'])
  1743. assert False
  1744. # Add test user
  1745. try:
  1746. inst.add_s(Entry((USER1_DN, {
  1747. 'objectclass': 'top extensibleObject'.split(),
  1748. 'uid': 'user1'
  1749. })))
  1750. except ldap.LDAPError, e:
  1751. log.fatal('test_referint: Failed to add user1: error ' + e.message['desc'])
  1752. assert False
  1753. # Add user to group
  1754. try:
  1755. inst.modify_s(GROUP_DN, [(ldap.MOD_REPLACE, 'member', USER1_DN)])
  1756. except ldap.LDAPError, e:
  1757. log.fatal('test_referint: Failed to add uniquemember: error ' + e.message['desc'])
  1758. assert False
  1759. # Delete a user
  1760. try:
  1761. inst.delete_s(USER1_DN)
  1762. except ldap.LDAPError, e:
  1763. log.fatal('test_referint: Failed to delete user1: ' + e.message['desc'])
  1764. assert False
  1765. # Check for integrity
  1766. try:
  1767. entry = inst.search_s(GROUP_DN, ldap.SCOPE_BASE, '(member=' + USER1_DN + ')')
  1768. if entry:
  1769. log.fatal('test_referint: user1 was not removed from group')
  1770. assert False
  1771. except ldap.LDAPError, e:
  1772. log.fatal('test_referint: Unable to search group: ' + e.message['desc'])
  1773. assert False
  1774. ############################################################################
  1775. # Test plugin dependency
  1776. ############################################################################
  1777. test_dependency(inst, PLUGIN_REFER_INTEGRITY)
  1778. ############################################################################
  1779. # Cleanup
  1780. ############################################################################
  1781. try:
  1782. inst.delete_s(GROUP_DN)
  1783. except ldap.LDAPError, e:
  1784. log.fatal('test_referint: Failed to delete group: ' + e.message['desc'])
  1785. assert False
  1786. try:
  1787. inst.delete_s(SHARED_CONFIG_DN)
  1788. except ldap.LDAPError, e:
  1789. log.fatal('test_referint: Failed to delete shared config entry: ' + e.message['desc'])
  1790. assert False
  1791. ############################################################################
  1792. # Test passed
  1793. ############################################################################
  1794. log.info('test_referint: PASS\n')
  1795. return
  1796. ################################################################################
  1797. #
  1798. # Test Retro Changelog Plugin (9)
  1799. #
  1800. ################################################################################
  1801. def test_retrocl(inst, args=None):
  1802. # stop the plugin, and start it
  1803. inst.plugins.disable(name=PLUGIN_RETRO_CHANGELOG)
  1804. inst.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  1805. if args == "restart":
  1806. return
  1807. log.info('Testing ' + PLUGIN_RETRO_CHANGELOG + '...')
  1808. ############################################################################
  1809. # Configure plugin
  1810. ############################################################################
  1811. # Gather the current change count (it's not 1 once we start the stabilty tests)
  1812. try:
  1813. entry = inst.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE, '(changenumber=*)')
  1814. except ldap.LDAPError, e:
  1815. log.fatal('test_retrocl: Failed to get the count: error ' + e.message['desc'])
  1816. assert False
  1817. entry_count = len(entry)
  1818. ############################################################################
  1819. # Test plugin
  1820. ############################################################################
  1821. # Add a user
  1822. try:
  1823. inst.add_s(Entry((USER1_DN, {
  1824. 'objectclass': 'top extensibleObject'.split(),
  1825. 'uid': 'user1'
  1826. })))
  1827. except ldap.LDAPError, e:
  1828. log.fatal('test_retrocl: Failed to add user1: error ' + e.message['desc'])
  1829. assert False
  1830. # Check we logged this in the retro cl
  1831. try:
  1832. entry = inst.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE, '(changenumber=*)')
  1833. if not entry or len(entry) == entry_count:
  1834. log.fatal('test_retrocl: changelog not updated')
  1835. assert False
  1836. except ldap.LDAPError, e:
  1837. log.fatal('test_retrocl: Unable to search group: ' + e.message['desc'])
  1838. assert False
  1839. entry_count += 1
  1840. ############################################################################
  1841. # Change the config - disable plugin
  1842. ############################################################################
  1843. inst.plugins.disable(name=PLUGIN_RETRO_CHANGELOG)
  1844. ############################################################################
  1845. # Test plugin
  1846. ############################################################################
  1847. try:
  1848. inst.delete_s(USER1_DN)
  1849. except ldap.LDAPError, e:
  1850. log.fatal('test_retrocl: Failed to delete user1: ' + e.message['desc'])
  1851. assert False
  1852. # Check we didn't logged this in the retro cl
  1853. try:
  1854. entry = inst.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE, '(changenumber=*)')
  1855. if len(entry) != entry_count:
  1856. log.fatal('test_retrocl: changelog incorrectly updated - change count: '
  1857. + str(len(entry)) + ' - expected 1')
  1858. assert False
  1859. except ldap.LDAPError, e:
  1860. log.fatal('test_retrocl: Unable to search retro changelog: ' + e.message['desc'])
  1861. assert False
  1862. ############################################################################
  1863. # Test plugin dependency
  1864. ############################################################################
  1865. inst.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  1866. test_dependency(inst, PLUGIN_RETRO_CHANGELOG)
  1867. ############################################################################
  1868. # Cleanup
  1869. ############################################################################
  1870. # None
  1871. ############################################################################
  1872. # Test passed
  1873. ############################################################################
  1874. log.info('test_retrocl: PASS\n')
  1875. return
  1876. ################################################################################
  1877. #
  1878. # Test Root DN Access Control Plugin (10)
  1879. #
  1880. ################################################################################
  1881. def test_rootdn(inst, args=None):
  1882. # stop the plugin, and start it
  1883. inst.plugins.disable(name=PLUGIN_ROOTDN_ACCESS)
  1884. inst.plugins.enable(name=PLUGIN_ROOTDN_ACCESS)
  1885. if args == "restart":
  1886. return
  1887. PLUGIN_DN = 'cn=' + PLUGIN_ROOTDN_ACCESS + ',cn=plugins,cn=config'
  1888. log.info('Testing ' + PLUGIN_ROOTDN_ACCESS + '...')
  1889. ############################################################################
  1890. # Configure plugin
  1891. ############################################################################
  1892. # Add an user and aci to open up cn=config
  1893. try:
  1894. inst.add_s(Entry((USER1_DN, {
  1895. 'objectclass': 'top extensibleObject'.split(),
  1896. 'uid': 'user1',
  1897. 'userpassword': 'password'
  1898. })))
  1899. except ldap.LDAPError, e:
  1900. log.fatal('test_rootdn: Failed to add user1: error ' + e.message['desc'])
  1901. assert False
  1902. # Set an aci so we can modify the plugin after ew deny the root dn
  1903. ACI = ('(target ="ldap:///cn=config")(targetattr = "*")(version 3.0;acl ' +
  1904. '"all access";allow (all)(userdn="ldap:///anyone");)')
  1905. try:
  1906. inst.modify_s(DN_CONFIG, [(ldap.MOD_ADD, 'aci', ACI)])
  1907. except ldap.LDAPError, e:
  1908. log.fatal('test_rootdn: Failed to add aci to config: error ' + e.message['desc'])
  1909. assert False
  1910. # Set allowed IP to an unknown host - blocks root dn
  1911. try:
  1912. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-allow-ip', '10.10.10.10')])
  1913. except ldap.LDAPError, e:
  1914. log.fatal('test_rootdn: Failed to set rootDN plugin config: error ' + e.message['desc'])
  1915. assert False
  1916. ############################################################################
  1917. # Test plugin
  1918. ############################################################################
  1919. # Bind as Root DN
  1920. failed = False
  1921. try:
  1922. inst.simple_bind_s(DN_DM, PASSWORD)
  1923. except ldap.LDAPError, e:
  1924. failed = True
  1925. if not failed:
  1926. log.fatal('test_rootdn: Root DN was incorrectly able to bind')
  1927. assert False
  1928. ############################################################################
  1929. # Change the config
  1930. ############################################################################
  1931. # Bind as the user who can make updates to the config
  1932. try:
  1933. inst.simple_bind_s(USER1_DN, 'password')
  1934. except ldap.LDAPError, e:
  1935. log.fatal('test_rootdn: failed to bind as user1')
  1936. assert False
  1937. # First, test that invalid plugin changes are rejected
  1938. try:
  1939. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-deny-ip', '12.12.ZZZ.12')])
  1940. log.fatal('test_rootdn: Incorrectly allowed to add invalid "rootdn-deny-ip: 12.12.ZZZ.12"')
  1941. assert False
  1942. except ldap.LDAPError:
  1943. pass
  1944. try:
  1945. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-allow-host', 'host._.com')])
  1946. log.fatal('test_rootdn: Incorrectly allowed to add invalid "rootdn-allow-host: host._.com"')
  1947. assert False
  1948. except ldap.LDAPError:
  1949. pass
  1950. # Remove the restriction
  1951. try:
  1952. inst.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, 'rootdn-allow-ip', None)])
  1953. except ldap.LDAPError, e:
  1954. log.fatal('test_rootdn: Failed to set rootDN plugin config: error ' + e.message['desc'])
  1955. assert False
  1956. ############################################################################
  1957. # Test plugin
  1958. ############################################################################
  1959. # Bind as Root DN
  1960. failed = False
  1961. try:
  1962. inst.simple_bind_s(DN_DM, PASSWORD)
  1963. except ldap.LDAPError, e:
  1964. failed = True
  1965. if failed:
  1966. log.fatal('test_rootdn: Root DN was not able to bind')
  1967. assert False
  1968. ############################################################################
  1969. # Test plugin dependency
  1970. ############################################################################
  1971. test_dependency(inst, PLUGIN_ROOTDN_ACCESS)
  1972. ############################################################################
  1973. # Cleanup - remove ACI from cn=config and test user
  1974. ############################################################################
  1975. try:
  1976. inst.modify_s(DN_CONFIG, [(ldap.MOD_DELETE, 'aci', ACI)])
  1977. except ldap.LDAPError, e:
  1978. log.fatal('test_rootdn: Failed to add aci to config: error ' + e.message['desc'])
  1979. assert False
  1980. try:
  1981. inst.delete_s(USER1_DN)
  1982. except ldap.LDAPError, e:
  1983. log.fatal('test_rootdn: Failed to delete user1: ' + e.message['desc'])
  1984. assert False
  1985. ############################################################################
  1986. # Test passed
  1987. ############################################################################
  1988. log.info('test_rootdn: PASS\n')
  1989. return
  1990. # Array of test functions
  1991. func_tests = [test_acctpolicy, test_attruniq, test_automember, test_dna,
  1992. test_linkedattrs, test_memberof, test_mep, test_passthru,
  1993. test_referint, test_retrocl, test_rootdn]
  1994. def test_all_plugins(inst, args=None):
  1995. for func in func_tests:
  1996. func(inst, args)
  1997. return