a qéqeÌ ã@s ddlmZddlmZmZmZmZddlmZm Z m Z m Z m Z ddl mZddlmZmZmZddlmZdd lmZGd d „d eƒZGd d „d eeƒZdS)é)Ú defaultdict)Ú FrozenSetÚListÚOptionalÚUnioné)Ú AnyConstraintÚConstraintRuletypeÚRoleÚTypeÚUseré)ÚDiffResultDescriptor)Ú DifferenceÚ SymbolWrapperÚWrapper)Úclass_wrapper_factory)ÚRuleListc@sÎeZdZUdZedƒZedƒZedƒZedƒZedƒZ edƒZ edƒZ edƒZ dZ eeefed<dZeeefed<dd œd d „Zdd œd d „Zdd œdd„Zdd œdd„Zdd œdd„Zdd œdd„ZdS)ÚConstraintsDifferencea‚ Determine the difference in constraints between two policies. Since the compiler does not union constraints, there may be multiple constraints with the same ruletype, object class, and permission set, so constraints can only be added or removed, not modified. The constraint expressions are compared only on a basic level. Expressions that are logically equivalent but are structurally different, for example, by associativity, will be considered different. Type and role attributes are also not expanded, so if there are changes to attribute members, it will not be reflected as a difference. Údiff_constrainsÚdiff_mlsconstrainsÚdiff_validatetransÚdiff_mlsvalidatetransNÚ_left_constraintsÚ_right_constraints)ÚreturncCs�|j d |¡¡|jdus&|jdur.| ¡|jdus@Jdƒ‚|jdusRJdƒ‚| dd„|jtjDƒdd„|jtjDƒ¡\|_ |_ }dS)zAGenerate the difference in constraint rules between the policies.zJGenerating constraint differences from {0.left_policy} to {0.right_policy}Nú)Left constraints didn't load, this a bug.ú*Right constraints didn't load, this a bug.css|]}t|ƒVqdS©N©ÚConstraintWrapper©Ú.0Úc©r$ú>/usr/lib64/python3.9/site-packages/setools/diff/constraints.pyÚ ?óz8ConstraintsDifference.diff_constrains..css|]}t|ƒVqdSrrr!r$r$r%r&@r') ÚlogÚinfoÚformatrrÚ_create_constrain_listsÚ _set_diffr Z constrainÚadded_constrainsÚremoved_constrains©ÚselfÚ_r$r$r%r2s ÿþz%ConstraintsDifference.diff_constrainscCs�|j d |¡¡|jdus&|jdur.| ¡|jdus@Jdƒ‚|jdusRJdƒ‚| dd„|jtjDƒdd„|jtjDƒ¡\|_ |_ }dS)zEGenerate the difference in MLS constraint rules between the policies.zNGenerating MLS constraint differences from {0.left_policy} to {0.right_policy}Nrrcss|]}t|ƒVqdSrrr!r$r$r%r&Pr'z;ConstraintsDifference.diff_mlsconstrains..css|]}t|ƒVqdSrrr!r$r$r%r&Rr') r(r)r*rrr+r,r Z mlsconstrainÚadded_mlsconstrainsÚremoved_mlsconstrainsr/r$r$r%rBs"ÿÿ ÿ ÿýz(ConstraintsDifference.diff_mlsconstrainscCs�|j d |¡¡|jdus&|jdur.| ¡|jdus@Jdƒ‚|jdusRJdƒ‚| dd„|jtjDƒdd„|jtjDƒ¡\|_ |_ }dS)zDGenerate the difference in validatetrans rules between the policies.zMGenerating validatetrans differences from {0.left_policy} to {0.right_policy}Nrrcss|]}t|ƒVqdSrrr!r$r$r%r&cr'z;ConstraintsDifference.diff_validatetrans..css|]}t|ƒVqdSrrr!r$r$r%r&er') r(r)r*rrr+r,r Z validatetransÚadded_validatetransÚremoved_validatetransr/r$r$r%rUs"ÿÿ ÿ ÿýz(ConstraintsDifference.diff_validatetranscCs�|j d |¡¡|jdus&|jdur.| ¡|jdus@Jdƒ‚|jdusRJdƒ‚| dd„|jtjDƒdd„|jtjDƒ¡\|_ |_ }dS)zHGenerate the difference in MLS validatetrans rules between the policies.zPGenerating mlsvalidatetrans differences from {0.left_policy} to {0.right_policy}Nrrcss|]}t|ƒVqdSrrr!r$r$r%r&vr'z>ConstraintsDifference.diff_mlsvalidatetrans..css|]}t|ƒVqdSrrr!r$r$r%r&xr') r(r)r*rrr+r,r ZmlsvalidatetransÚadded_mlsvalidatetransÚremoved_mlsvalidatetransr/r$r$r%rhs"ÿÿ ÿ ÿýz+ConstraintsDifference.diff_mlsvalidatetranscCsättƒ|_|j d |¡¡|j ¡D]}|j|j  |¡q&|j  ¡D] \}}|j d t |ƒ|¡¡qHttƒ|_ |j d |¡¡|j  ¡D]}|j |j  |¡q�|j   ¡D] \}}|j d t |ƒ|¡¡q²|j d¡dS)z$Create rule lists for both policies.z.Building constraint lists from {0.left_policy}zLoaded {0} {1} rules.z/Building constraint lists from {0.right_policy}z)Completed building constraint rule lists.N)rÚlistrr(Údebugr*Z left_policyÚ constraintsÚruletypeÚappendÚitemsÚlenrZ right_policy)r0Úruler;Zrulesr$r$r%r+~s  z-ConstraintsDifference._create_constrain_listscCsL|j d¡d|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ dS)z%Reset diff results on policy changes.z%Resetting all constraints differencesN) r(r9r-r.r2r3r4r5r6r7rr©r0r$r$r%Ú _reset_diff’s z!ConstraintsDifference._reset_diff)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr-r.r2r3r4r5r6r7rrr rÚ__annotations__rrrrrr+rAr$r$r$r%rs" rc@s<eZdZdZdZeddœdd„Zdd„Zd d „Zd d „Z dS) r z#Wrap constraints for diff purposes.©r;ÚtclassÚpermsÚexprN)r?rcCs’||_|j|_t|jƒ|_z |j|_Wnty>d|_Yn0t|ƒ|_g|_|j D]6}t |t ƒr€|j  t dd„|Dƒƒ¡qV|j  |¡qVdS)Ncss|]}t|ƒVqdSr)r)r"Úitemr$r$r%r&»r'z-ConstraintWrapper.__init__..) Úoriginr;rrHrIÚAttributeErrorÚhashÚkeyrJZ expressionÚ isinstanceÚ frozensetr<)r0r?Úopr$r$r%Ú__init__©s       zConstraintWrapper.__init__cCs|jSr©rOr@r$r$r%Ú__hash__ÀszConstraintWrapper.__hash__cCs |j|jkSrrT©r0Úotherr$r$r%Ú__lt__ÃszConstraintWrapper.__lt__cCs0|j|jko.|j|jko.|j|jko.|j|jkSrrGrVr$r$r%Ú__eq__Æs  ÿ þ ýzConstraintWrapper.__eq__) rBrCrDrEÚ __slots__rrSrUrXrYr$r$r$r%r £s r N)Ú collectionsrÚtypingrrrrZ policyreprr r r r Z descriptorsrÚ differencerrrZobjclassrrrr r$r$r$r%Ús