o ;s*b\@svdZdZdZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZddlmZddlmZmZmZdd lmZdd lmZdd lmZmZdd lmZdd lmZddl m Z ddl!m"Z"m#Z#ej$%ej$&e'dZ(iZ)ddl!m*Z*e j+j,Z-ej$%ej$&e'dZ.iZ/Gddde j0Z1Gddde"Z2Gddde"Z3Gddde"Z4Gddde"Z5dS)z!Cyril Jaquier, Yaroslav Halchenkoz>Copyright (c) 2004 Cyril Jaquier, 2011-2013 Yaroslav HalchenkoGPLN) ConfigReaderConfigReaderUnsharedDefinitionInitConfigReaderNoSectionError)configparserinc) JailReaderextractOptionssplitWithOptions) FilterReader) JailsReader) ActionReader CommandAction) Configurator)MyTime)version)LogCaptureTestCase with_tmpdirfiles) CONFIG_DIRconfigcsxeZdZfddZddZdddZdd Zdd d Zd dZddZ ddZ ddZ ddZ ddZ ddZZS)ConfigReaderTestcs.tt|tjdd|_t|jd|_dS)zCall before every test case.zf2b-temp)prefixbasedirN)superrsetUptempfilemkdtempdrcself __class__E/usr/lib/python3/dist-packages/fail2ban/tests/clientreadertestcase.pyr7szConfigReaderTest.setUpcCst|jdS)zCall after every test case.N)shutilrmtreer!r#r'r'r(tearDown=szConfigReaderTest.tearDownNcCstjj|vrtj|}tj|j|}tj|st|td|j|fd}|dur4| d||dur=| || dS)N%s/%swz [section] option = %s ) ospathsepdirnamejoinr!existsmakedirsopenwriteclose)r$fnamevaluecontentr!d_fr'r'r(_writeAs      zConfigReaderTest._writecCs*td|j|f||jddS)Nr,r")r.unlinkr! assertTruer"read)r$r8r'r'r(_removeRszConfigReaderTest._remover"cCs&||j||jddgdS)Nsection)intoptionrD)r?r"r@ getOptions)r$r<r'r'r( _getoptionVszConfigReaderTest._getoptioncCs|jd|jddd|jddd|jddd|jdd}||dd d d |jdd }||dd dd |jdd dd d }||dd d d dS)N Definitiona1br"test))rCrHr)boolrJr)rCr"rrTr)rHrJr"))rCrH)rLrJ)rCr")rCr)rLr)r" add_sectionsetrEassertSortedEqual)r$optsr'r'r( testConvertZs  zConfigReaderTest.testConvertcCsxtj|jd}|dd||ddt|dt|tj s/| |j ddSddl }td| )Nzd.confrr!z0Skipping on %s -- access rights are not enforced)r.r/r2r!r= assertEqualrFchmodaccessR_OK assertFalser"r@platformunittestSkipTest)r$r<rWr'r'r(testInaccessibleFileis  z%ConfigReaderTest.testInaccessibleFilecCsL||jd|dd||d|dd||d|dd||d |d d ||d |d d ||d|dd||d|dd||d|d|d||d|d ||d |d||d|d ||ddS)Nr"c.confrIr2rz c.d/98.conf998iz c.d/90.conf990z c.d/99.conf999izc.local3z c.d/1.local4i)rVr"r@r=rRrFrAr#r'r'r(testOptionalDotDDirvs0            z$ConfigReaderTest.testOptionalDotDDircCs|jdddd|jdddd|jdddd|jddd d|jd dd dGd d d t}|ddi|_|j|j||j|jjidd|j}| | dd| | dd| | dddS)Nr[zS [INCLUDES] before = ib.conf after = ia.conf [Definition] test = %(default/test)s r9r:zib.confz, [DEFAULT] test = A [Definition] option = 1 zib.localz, [DEFAULT] test = B [Definition] option = 2 zia.confz, [DEFAULT] test = C [Definition] oafter = 3 zia.localz, [DEFAULT] test = D [Definition] oafter = 4 c@s$eZdZddgddgddgdZdS)z?ConfigReaderTest.testLocalInIncludes..TestDefConfReaderrCNstring)rDoafterrK)__name__ __module__ __qualname__ _configOptsr'r'r'r(TestDefConfReaders  rlr"rDT)allrrgrcrKD) r=rr" setBaseDirr!r?r@rE getCombinedrRget)r$rlor'r'r(testLocalInIncludess z$ConfigReaderTest.testLocalInIncludescCs||jd|jdddd||jd||jddg||jddd||jdd d ||jdd d||jdd d ||jdd ddS)Nizi.confzu [DEFAULT] b = a zz = the%(__name__)s [section] y = 4%(b)s e = 5${b} z = %(__name__)s [section2] z = 3%(__name__)s rerBsection2y4aez5${b}zzz thesection 3section2)rVr"r@r=r?rRsectionsrqr#r'r'r(testInterpolationss z#ConfigReaderTest.testInterpolationscCsd||jd|jdddd||jd||jddd||jddd dS) Ngg.confz4 [DEFAULT] # A comment b = a c = d ;in line comment reDEFAULTrJrHr"r!)rVr"r@r=r?rRrqr#r'r'r( testCommentss zConfigReaderTest.testCommentscCs<||jd|jdddd||jd||jddd||jddd ||jd dd ||jd dd ||jd d d||jd dd||jdd d||jddd||jdd d||jddd|t|jjdd|t|jjd ddS)Nrrz [DEFAULT] a = def-a b = def-b,a:`%(a)s` c = def-c,b:"%(b)s" d = def-d-b:"%(known/b)s" [jail] a = jail-a-%(test/a)s b = jail-b-%(test/b)s y = %(test/y)s [test] a = test-a-%(default/a)s b = test-b-%(known/b)s x = %(test/x)s y = %(jail/y)s rerKrHz test-a-def-arJztest-b-def-b,a:`test-a-def-a`jailzjail-a-test-a-def-az+jail-b-test-b-def-b,a:`jail-a-test-a-def-a`r"z5def-c,b:"jail-b-test-b-def-b,a:`jail-a-test-a-def-a`"r!z'def-d-b:"def-b,a:`jail-a-test-a-def-a`"z'def-c,b:"test-b-def-b,a:`test-a-def-a`"z def-d-b:"def-b,a:`test-a-def-a`"rzdef-c,b:"def-b,a:`def-a`"zdef-d-b:"def-b,a:`def-a`"xrv) rVr"r@r=r?rRrq assertRaises Exceptionr#r'r'r(testTargetedSectionOptionssz+ConfigReaderTest.testTargetedSectionOptions)NN)r")rhrirjrr+r=rArFrQrZrdrsr~rr __classcell__r'r'r%r(r5s    0 rcseZdZfddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZeddZdd ZZS)!JailReaderTestctt|j|i|dSN)rr__init__r$argskwargsr%r'r(rzJailReaderTest.__init__cCsdD]]}|td|fddg|td|fddg|td|fddg|td |fd dg|td |fd dg|td |fddg|td|fddgqdS)N)   za%sbrHrJz a[x=y]%sbza[x=y]za[x=y][z=z]%sbz a[x=y][z=z]za[x="y][z"]%sbz a[x="y][z"]z a[x="y z"]%sbz a[x="y z"]z a[x="y z"]%sbz a[x="y z"]z a[x="y z"]%sbz a[x="y z"])rRr )r$r0r'r'r(testSplitWithOptions sz#JailReaderTest.testSplitWithOptionscCs tdttd}|t|jdS)N XXXABSENTXXXr share_config)r rCONFIG_DIR_SHARE_CFGr ValueErrorr@r$rr'r'r(testIncorrectJailsz JailReaderTest.testIncorrectJailcCsPtdttd}|||||||d|ddS)N emptyactionrz"No filter set for jail emptyactionz'No actions were defined for emptyaction)r IMPERFECT_CONFIGIMPERFECT_CONFIG_SHARE_CFGr?r@rE isEnabled assertLoggedrr'r'r(testJailActionEmptys  z"JailReaderTest.testJailActionEmptycCsTtdttd}|||||||dt|ddS)NmissingbitsjailrzJFound no accessible config files for 'filter.d/catchallthebadies' under %szUnable to read the filter r rrr?r@rVrErrrr'r'r(testJailActionFilterMissing"s z*JailReaderTest.testJailActionFilterMissingcCFtdttd}|||||||ddS)Nbrokenactiondefrz$Invalid action definition 'joho[foo'rrr'r'r(testJailActionBrokenDef*z&JailReaderTest.testJailActionBrokenDefcCsNtdttd}||||||||jdddS)N tz_correctr logtimezoneUTC+0200) r rrr?r@rErrRoptionsrr'r'r(testJailLogTimeZone2sz"JailReaderTest.testJailLogTimeZonecCr)Nbrokenfilterdefrz$Invalid filter definition 'flt[test'rrr'r'r(testJailFilterBrokenDef:rz&JailReaderTest.testJailFilterBrokenDefcCsttjjddtdttd}||||| | | | d| d| | ddS)NTstocksshdrzssh-funky-blocker)rXF2BSkipIfCfgMissingr rrr?r@rErVrrRgetNamesetNamerr'r'r(testStockSSHJailBs zJailReaderTest.testStockSSHJailcCstjjddtdttdd}|||||| | }| gdgdd|D| gdgd d|D| gd gd d|D| gd }d }|D]}| t|dkom|dd|d7}||krzdSq]dS)NTrsshd-override-flt-optsrr force_enable)rNr prefregexz^TestcS(g|]}t|dkr|ddkr|qS)rrlen.0rrr'r'r( W(z>JailReaderTest.testOverrideFilterOptInJail..)rNraddjournalmatchz _COMM=testcSr)rrrrr'r'r(rZr)rNrmaxlinesrcSr)rrrrr'r'r(r]r)rNrusednsnorrregexr)rXrrr rrr?r@rErconvertrRindexrVrendswith)r$rstream usednsidxrtrrr'r'r(testOverrideFilterOptInJailLs0       z*JailReaderTest.testOverrideFilterOptInJailc CstjjdddD]9}dD]4}td|ttdd}|||| | }| d| dd |ggd d |Dq q dS) NTr)rr)JRNLFILETESTINIT checklogtype_rrN addfailregexz^%s failure from $cSr)rrrrr'r'r(rsrz=JailReaderTest.testLogTypeOfBackendInJail..) rXrrr lowerrrr?r@rErrRr)r$rtpreflinerrr'r'r(testLogTypeOfBackendInJailfs z)JailReaderTest.testLogTypeOfBackendInJailc Cs,d}dddif}t|}||||diftd|ddddftd |d iftd |d iftd |d d diftdd}dif}t|}|||d}ddddddddddddd f}t|}|||t|dd}|dtd d!|d"Df}|||dS)#Nzmail-whois[name=SSH]z mail-whoisnameSSHz mail.who_iscatdog)rHrJzmail.who_is[a=cat,b=dog]z mail--ho_iszmail--ho_is['s']mailrH,z mail[a=',']zabc[]abczoption[opt01=abc,opt02="123",opt03="with=okay?",opt04="andwith,okay...",opt05="how about spaces",opt06="single'in'double",opt07='double"in"single', opt08= leave some space, opt09=one for luck, opt10=, opt11=]rD123z with=okay?zandwith,okay...zhow about spaceszsingle'in'doublezdouble"in"singlezleave some spacez one for luck) opt01opt02opt03opt04opt05opt06opt07opt08opt09opt10opt11][rcss$|] \}}||ddfVqdS)rrN)replace)rkvr'r'r( s"z1JailReaderTest.testSplitOption..r)r rRrdictitems)r$rDexpectedresult expected2r'r'r(testSplitOptionusD    zJailReaderTest.testSplitOptioncCstddttd}||||||jdd||jdd|dd |j Dgd d ddd d dgdd gddgggggdd dddd dgddgddgddgggggdd dddd dgddgddgggggdS)N multi-logT)rrrlogpathza.log b.log c.logactionzeaction[actname='ban'] action[actname='log', logpath="a.log b.log c.log d.log"] action[actname='test']cSsg|]}|qSr')rrrHr'r'r(rz6JailReaderTest.testMultiLineOption..)rNr addactionban multi-setr actionbanz4echo "name: ban, ban: , logs: a.log b.log c.log"actnamer)rNrrlogrz:echo "name: log, ban: , logs: a.log b.log c.log d.log"za.log b.log c.log d.log)rNrrrKrKz5echo "name: test, ban: , logs: a.log b.log c.log") r rrr?r@rErRrrO_JailReader__actionsrr'r'r(testMultiLineOptions,  z"JailReaderTest.testMultiLineOptionc sZtjjddtddtd}t|d|j}t d|d<| | | }g}|D]6t dkr7q.dd krPdd ksJdd rP|q.dd krd|fd ddDq.dt}|t |d||dd dddd |g||dd dddd |g|t|ddd d|||dd dddd |gdS)NTr blocklisttest)rrr))rhr)filterr) failregexz ^test $)senderzf2b-test@example.com)blocklist_de_apikeyztest-key)rz%(action_blocklist_de)s %(action_badips_report)s %(action_badips)s mynetwatchman[port=1234,protocol=udp,agent="%(fail2ban_agent)s"]rcrrNagentz badips.pyrcs.g|]}|ddkrdgdd|qS)rrrNrrcr'rcmdr'r(rs.z3JailReaderTest.testVersionAgent..z Fail2Ban/%sr blocklist_derbadipsrzra mynetwatchman)rXrrr rrr@_cfg get_sectionsrr?rErrrappendextendrrRevalrq)r$rr}ract useragentr'r r(testVersionAgents,     &    zJailReaderTest.testVersionAgentcCstj|d}t|dtj|d}td||ttj|d|g|t|g| d||ttj|dgdS)Nf1r-f2 nonexisting*z4File %s is a dangling link, thus cannot be monitored) r.r/r2r5r7symlinkrRr _globr)r$r!rrr'r'r(testGlobs  zJailReaderTest.testGlobcCsttid}||g||d|t|jdi|t|jd|t|j dd|t|j didS)NrrKany) rrRr}rV has_sectionrr merge_sectionrrqrE)r$r"r'r'r(testCommonFunctions z!JailReaderTest.testCommonFunction)rhrirjrrrrrrrrrrrrrrrrr"rr'r'r%r(rs$   2% rc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)FilterReaderTestcCsgddddgdggdgdgdgd g}tddi}|t||d|||tddd d ittd }||dd |dd<|||dS)N)rN testcase01rrrr$r)z^\s*(?:\S+ )?(?:kernel: \[\d+\.\d+\] )?(?:@vserver_\S+ )?(?:(?:\[\d+\])?:\s+[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?|[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?(?:\[\d+\])?:)?\s*(?:error: PAM: )?Authentication failure for .* from \s*$z^\s*(?:\S+ )?(?:kernel: \[\d+\.\d+\] )?(?:@vserver_\S+ )?(?:(?:\[\d+\])?:\s+[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?|[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?(?:\[\d+\])?:)?\s*(?:error: PAM: )?User not known to the underlying authentication module for .* from \s*$a^\s*(?:\S+ )?(?:kernel: \[\d+\.\d+\] )?(?:@vserver_\S+ )?(?:(?:\[\d+\])?:\s+[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?|[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?(?:\[\d+\])?:)?\s*(?:error: PAM: )?User not known to the\nunderlying authentication.+$^.+ module for .* from \s*$)rNr$addignoreregexz"^.+ john from host 192.168.1.1\s*$)rNr$rz _COMM=sshd+z_SYSTEMD_UNIT=sshd.servicez_UID=0)rNr$rzFIELD= with spaces r&zAFIELD= with + char and spaces)rNr$ datepatternz%Y %m %d %H:%M:%Sr5rrr r)r roTEST_FILES_DIRr@rErOrTEST_FILES_DIR_SHARE_CFG)r$output filterReaderr'r'r(rQs&      zFilterReaderTest.testConvertcCsPtdddddttd}||d|}||dd|ddS)Nr$zX)rrKr)rz6Wrong int value 'X' for 'maxlines'. Using default one:)r r,r+r@rErpassertNotEqualrr$r.rPr'r'r(testConvertOptions.s z#FilterReaderTest.testConvertOptionscCsFgdg}tddittd}||d|}|||dS)N)rNjailnamerz to=sweet@example.com fromip= substitionr3r)r r,r+r@rErrOr$r-r.r"r'r'r(!testFilterReaderSubstitionDefault7s  z2FilterReaderTest.testFilterReaderSubstitionDefaultcCsBtddittd}||d|}|d|dvdS)N testcase02r3r)rr)r r,r+r@rErpr?r1r'r'r(testFilterReaderSubstKnown@s z+FilterReaderTest.testFilterReaderSubstKnowncCsJgdg}tddddittd}||d|}|||dS)N)rNr3rzto=sour@example.com fromip=r4r3honeypotzsour@example.comr)r5r6r'r'r(testFilterReaderSubstitionSetKs   z.FilterReaderTest.testFilterReaderSubstitionSetcCRgdg}td\}}tdd|ttd}||d|}|||dS)N)rNr3rz?^to=test,sweet@example.com,test2,sweet@example.com fromip=$zusubstition[failregex="^$", honeypot=",", sweet="test,,test2"]r4r3r)r r r,r+r@rErrOr$r- filterName filterOptr.r"r'r'r(testFilterReaderSubstitionKnownT  z0FilterReaderTest.testFilterReaderSubstitionKnowncCr<)N)rNr3rz)^\s*to=fail2ban@localhost fromip=\s*$zUsubstition[failregex="^\s*\s*$", honeypot=""]r4r3r)r=r>r'r'r(!testFilterReaderSubstitionSection_rBz2FilterReaderTest.testFilterReaderSubstitionSectioncCsvtddddittd}||d|ttj|tdddddttd}||d|ttj|dS)Nr4r3r:z r)z)r:sweet)r r,r+r@rErrr)r$r.r'r'r(testFilterReaderSubstitionFailjs   z/FilterReaderTest.testFilterReaderSubstitionFailc Cstjtjtd}ttj|ddi}||tj|dtj|dgz|d| dd| dd| ddWdSt y^}z| d |WYd}~dSd}~ww) Nfilter.dztestcase01.confr$ztestcase-common.confrG __prefix_liner ignoreregexz)unexpected options after readexplicit: %s) r.r/abspathr2r+r rR readexplicitrErqrfail)r$path_r.rxr'r'r(testFilterReaderExplicitxs    z)FilterReaderTest.testFilterReaderExplicitN) rhrirjrQr2r7r9r;rArCrErMr'r'r'r(r#s,      r#c@s*eZdZd ddZddZeddZdS) JailsReaderTestCacheFNcCsBt||d}|||||||ddS)Nrr)rro readEarlygetEarlyOptionsreadAllr?rE)r$rrr configuratorr'r'r(_readWholeConfs  z#JailsReaderTestCache._readWholeConfcCs4d}|dD]}td||r|d7}q |S)Nrrz^\s*Reading files?: .*/r)getLogrsplitrematch)r$ filematchcntsr'r'r(_getLoggedReadCounts z(JailsReaderTestCache._getLoggedReadCountcCstjtj}tjt_zst|t t |t t d|dt t d|dt }|j ||d|d}||dkd||j |d |d |d }||dkd ||d }||dkd||d}||dkd|W|t_dS|t_w)Nz /jail.confz /jail.localz/fail2ban.confz/fail2ban.localrz jail.localrz3Unexpected count by reading of jail files, cnt = %sTrOz jail\.localz:Unexpected count by second reading of jail files, cnt = %szfilter\.d/common\.confz5Unexpected count by reading of filter files, cnt = %szaction\.d/iptables-common\.confz5Unexpected count by reading of action files, cnt = %s)rXr SkipIfFastrlogLevelloggingDEBUGr)r*copytreercopyrrTr\r?)r$rsaved_ll share_cfgrZr'r'r(testTestJailConfCaches(       z*JailsReaderTestCache.testTestJailConfCache)FN)rhrirjrTr\rrer'r'r'r(rNs   rNcs|eZdZfddZddZddZddZd d Zd d Zd dZ ddZ e ddZ ddZ ddZe ddZZS)JailsReaderTestcrr)rrfrrr%r'r(rrzJailsReaderTest.__init__cCs,tjdstdd}|t|jdSdS)Nz/XXXr)r.r/r3r rrr@)r$readerr'r'r(testProvidingBadBasedirs  z'JailsReaderTest.testProvidingBadBasedircCs*tttd}||||jdd|t|j |j dd}d|_ | |gdgddd d gd gd d ggd gdgdgdgdddddddgddgddggggdgdgdgdd dgd dgd dgd dggdd d gd!d"gd!d#gd!d$gd!d%gg| d&| d'| d(dS))NrF) ignoreWrongTallow_no_files)addrauto)rltest-known-interprmrrnr)z*failure test 1 (filter.d/test.conf) z+failure test 2 (filter.d/test.local) z"failure test 3 (jail.local) start)rlmissinglogfilesrm)rNrpr)rl brokenactionrm)rNrrrrq)rNrrrrrrrrrzhit with big stick rr)rlparse_to_end_of_jail.confrm)rNrsrrq)rNrrrq)rNrrrrrprs)rlrrmrz config-errorzdJail 'brokenactiondef' skipped, because of wrong configuration: Invalid action definition 'joho[foo'zdJail 'brokenfilterdef' skipped, because of wrong configuration: Invalid filter definition 'flt[test'zoJail 'missingaction' skipped, because of wrong configuration: Unable to read action 'noactionfileforthisaction'zmJail 'missingbitsjail' skipped, because of wrong configuration: Unable to read the filter 'catchallthebadies'z!Errors in jail 'missingbitsjail'.z Skipping...z6No file(s) found for glob /weapons/of/mass/destruction)r rrr?r@rVrErrrmaxDiffrOrassertNotLogged)r$jails comm_commandsr'r'r(testReadTestJailConfs`   & z$JailsReaderTest.testReadTestJailConfc CsDtjjddttjtddD]}tj| dd}t |ditd}| | z| iWntyS}z|d |t|j|fWYd}~nd}~ww|d s|jd |d |d |j |jddd|d |jtjtdBd}|j|ddd|d |dvr|jd|ddd|d qdS)NTraction.d*.confz.confrrrzaction %r %s: %sz-commonrGz.Action file %r is lacking [Definition] sectionmsgrz#Action file %r is lacking actionban)timeoutbantime)ignorerz5Action file %r does not contains jail-name 'f2b-TEST')pfziptables-allportsiptables-multiportzf2b-TEST actionstartzSAction file %r: interpolation of actionstart does not contains jail-name 'f2b-TEST')rXrrglobr.r/r2rbasenamerrr?r@rErrKtyperhrassertInr}_optsrqstriprpr _escapedTagsrNrR)r$ actionConfig actionName actionReaderrxrPr'r'r(testReadStockActionConfs<&   z'JailsReaderTest.testReadStockActionConfc Cstjjddtttd}||||| }| |gt }| D]}|dkr3q,| |d}t|\}}|||t|t|||ttd}||d||i||j dd | |d }|t|t|D]M} t| \} } |t| |t| t| d kr|d | t| |ittd} || | i| } |t| || j d d qq,dS)NTrrINCLUDESrr)zFailed to read filter:rrrrportr)rXrrr rrr?r@rErrRrNr}rqr rlrr rrr isinstancerrr)r$rvrw allFiltersrr?r@r.actionsractNameactOptrcmdsr'r'r(testReadStockJailConfsL            z%JailsReaderTest.testReadStockJailConfc Cstjjddttdtd}||||t ddt t j dddD}t d d|jD}d|_|||d |||||d ||dS) NTrrrrcss@|]}|ds|dstjtj|ddVqdS)z common.confz-aggressive.confrrN)rr.r/splitextsplitrr'r'r(r_s&zBJailsReaderTest.testReadStockJailFilterComplete..rrFrzcss"|] }t|jddVqdS)rrN)r r)rrr'r'r(rcs z=More filters exists than are referenced in stock jail.conf %rz2Stock jail.conf references non-existent filters %r)rXrrr rrr?r@rErNrr.r/r2rvrtissubset difference)r$rvfilters filters_jailr'r'r(testReadStockJailFilterCompleteYs"    z/JailsReaderTest.testReadStockJailFilterCompletec Cstjjddttdtd}|||||j dd}|t ||D] }t |dkrK|d|dgdd gkrK|t |ddkq+d D]!}| d |gd d |D| d|ddg|| d|g|qN||ddd|jD]}|j}|}|jt |d|d|D]q}| }|} dt|vr| d|jd} |d| g} |D]B}t |dkr|ddkr|dd| krddd|dDv} nt |dkr|ddkr|dd| kr|ddkrd} | rnq|j| d| t|fdqq}dS)NTrrrjrarrrNr~)rrecidiverlcss4|]}t|dkr|ddkr|ddVqdS)rarrlNrrrr r'r'r(rs2zDJailsReaderTest.testReadStockJailConfForceEnabled..rwarnror*zNo actions found for jail %sr{z blocktypeFrrcrrcSsg|]}|dqS)rr'rr'r'r(rrzEJailsReaderTest.testReadStockJailConfForceEnabled..r zFound no %s command among %s)rXrrr rrr?r@rErrr str2secondsrrR_JailsReader__jailsrrstr _initOpts) r$rvrwcommandjr jail_namercommands action_nameblocktype_presenttarget_commandr'r'r(!testReadStockJailConfForceEnabledlsb $      z1JailsReaderTest.testReadStockJailConfForceEnabledc sBtjjddt}|t||t|| }||dd||dd| | | | fdd}||d |d koV|d kn||d |d k||d|d k|gdgdgdgdgdgdg|jd||jd||tdS)NTrsocketz/var/run/fail2ban/fail2ban.sockpidfilez/var/run/fail2ban/fail2ban.pidcsBtD]\}}|ddkr|d|kr|Sqtd|f)NrrNrz/Did not find command 'set %s' among commands %s) enumerater)rDrtrxrr'r(find_setsz7JailsReaderTest.testStockConfigurator..find_set syslogsocketloglevel logtarget dbpurgeagedbfile dbmaxmatches)rNrrm)rNrINFO)rNrz/var/log/fail2ban.log)rNrz"/var/lib/fail2ban/fail2ban.sqlite3)rNr )rNr1dz/tmp)rXrrrrorrR getBaseDirrPrQrRrEconvertToProtocolgetConfigStreamr?rO_Configurator__jails)r$rSrPrr'rr(testStockConfigurators:  " z%JailsReaderTest.testStockConfiguratorcCsttj|dttj|dttj|dddttj|dddttj|dd}|d|t|id}|| || |j d d }d d |D}| t td d|Dd| |ddddS)NrFryztestaction1.confr-ztestfilter1.conf jail.confz [testjail1] enabled = true action = testaction1[actname=test1] testaction1[actname=test2] testaction.py testaction.py[actname=test3] filter = testfilter1 rTrjcSs,g|]}|ddgdkr|ddqS)Nra)rN testjail1rr')rcommr'r'r(rs z:JailsReaderTest.testMultipleSameAction..css|]}|dVqdS)rNr')rrr'r'r(rsz9JailsReaderTest.testMultipleSameAction..rcr*z{})r.mkdirr/r2r5r7r6r r?r@rErrRrrN)r$rjailfdrvrw add_actionsr'r'r(testMultipleSameActions   z&JailsReaderTest.testMultipleSameActioncCs|jtd|jdddS)Nz'Have not found any log file for .* jailpollingbackend)assertRaisesRegexr _testLogPathr#r'r'r(testLogPathFileFilterBackends z,JailsReaderTest.testLogPathFileFilterBackendc CsPzddlm}Wnty}ztdd}~ww|jdd|jdddS)Nr) FilterSystemdz&systemd python interface not availablesystemdrzsystemd[journalflags=2])server.filtersystemdrrrXrYr)r$rrxr'r'r(testLogPathSystemdBackends  z)JailsReaderTest.testLogPathSystemdBackendcCs`ttj|dd}|d||f|t|d}|||| | dS)Nrr-z [testjail1] enabled = true backend = %s logpath = %s/not/exist.log /this/path/should/not/exist.log action = filter = failregex = test r) r5r.r/r2r6r7r r?r@rEr)r$rrrrvr'r'r(rs   zJailsReaderTest._testLogPath)rhrirjrrhrxrrrrrrrrrrrr'r'r%r(rfs 1??3 rf)6 __author__ __copyright__ __license__rr_r.rWr)rrXclient.configreaderrrrrclientrclient.jailreaderr r r client.filterreaderr client.jailsreaderr client.actionreaderrrclient.configuratorr server.mytimerrutilsrrr/r2r1__file__r+r,rrrrrrTestCaserrr#rNrfr'r'r'r(sD       R{ :