3 ^"@stddlmZddlmZddlmZddlmZmZm Z ddl m Z ddl m Z Gd d d eZGd d d e Zd S)) namedtuple)ConstraintRuletype)DiffResultDescriptor) Difference SymbolWrapperWrapper)class_wrapper_factory)type_wrapper_factoryc@seZdZdZedZedZedZedZedZ edZ edZ edZ dZ dZdZdZdZdZdZdZddZd d Zd d Zd dZddZddZdS)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_constrainsdiff_mlsconstrainsdiff_validatetransdiff_mlsvalidatetransNcCs`|jjdj||jdks&|jdkr.|j|jdd|jDdd|jD\|_|_}dS)zAGenerate the difference in constraint rules between the policies.zJGenerating constraint differences from {0.left_policy} to {0.right_policy}Ncss|]}t|VqdS)N)ConstraintWrapper).0cr#/usr/lib64/python3.6/constraints.py Qsz8ConstraintsDifference.diff_constrains..css|]}t|VqdS)N)r)rrrrrrRs) loginfoformat_left_constrains_right_constrains_create_constrain_lists _set_diffadded_constrainsremoved_constrains)self_rrrr Gs z%ConstraintsDifference.diff_constrainscCs`|jjdj||jdks&|jdkr.|j|jdd|jDdd|jD\|_|_}dS)zEGenerate the difference in MLS constraint rules between the policies.zNGenerating MLS constraint differences from {0.left_policy} to {0.right_policy}Ncss|]}t|VqdS)N)r)rrrrrr_sz;ConstraintsDifference.diff_mlsconstrains..css|]}t|VqdS)N)r)rrrrrr`s) rrr_left_mlsconstrains_right_mlsconstrainsrradded_mlsconstrainsremoved_mlsconstrains)r r!rrrrTsz(ConstraintsDifference.diff_mlsconstrainscCs`|jjdj||jdks&|jdkr.|j|jdd|jDdd|jD\|_|_}dS)zDGenerate the difference in validatetrans rules between the policies.zMGenerating validatetrans differences from {0.left_policy} to {0.right_policy}Ncss|]}t|VqdS)N)r)rrrrrrmsz;ConstraintsDifference.diff_validatetrans..css|]}t|VqdS)N)r)rrrrrrns) rrr_left_validatetrans_right_validatetransrradded_validatetransremoved_validatetrans)r r!rrrrbsz(ConstraintsDifference.diff_validatetranscCs`|jjdj||jdks&|jdkr.|j|jdd|jDdd|jD\|_|_}dS)zHGenerate the difference in MLS validatetrans rules between the policies.zPGenerating mlsvalidatetrans differences from {0.left_policy} to {0.right_policy}Ncss|]}t|VqdS)N)r)rrrrrr{sz>ConstraintsDifference.diff_mlsvalidatetrans..css|]}t|VqdS)N)r)rrrrrr|s) rrr_left_mlsvalidatetrans_right_mlsvalidatetransrradded_mlsvalidatetransremoved_mlsvalidatetrans)r r!rrrrpsz+ConstraintsDifference.diff_mlsvalidatetranscCsZg|_g|_g|_g|_x|jjD]}|jtjkrB|jj |q$|jtj kr\|jj |q$|jtj krv|jj |q$|jtj kr|jj |q$|j jdj|jq$Wg|_g|_g|_g|_x|jjD]}|jtjkr|jj |q|jtj kr|jj |q|jtj kr"|jj |q|jtj kr>|jj |q|j jdj|jqWdS)z$Create rule lists for both policies.z/Unknown rule type: {0} (This is an SETools bug)N)rr"r&r*Z left_policyZ constraintsruletyperZ constrainappendZ mlsconstrainZ validatetransZmlsvalidatetransrerrorrrr#r'r+Z right_policy)r rulerrrrs<       z-ConstraintsDifference._create_constrain_listscCsp|jjdd|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_dS)z%Reset diff results on policy changes.z%Resetting all constraints differencesN)rdebugrrr$r%r(r)r,r-rr"r&r*rr#r'r+)r rrr _reset_diffs" z!ConstraintsDifference._reset_diff)__name__ __module__ __qualname____doc__rrrr$r%r(r)r,r-rrr"r#r&r'r*r+r rrrrr3rrrrr s. $r c@s4eZdZdZdZddZdd Zd d Zd d ZdS)rz#Wrap constraints for diff purposes.r.tclasspermsexprc Cs||_|j|_t|j|_y |j|_Wntk r@d|_YnXt||_g|_x@|j D]6}t |t r|jj t dd|DqZ|jj |qZWdS)Ncss|]}t|VqdS)N)r)ritemrrrrsz-ConstraintWrapper.__init__..) originr.r r8r9AttributeErrorhashkeyr:Z expression isinstance frozensetr/)r r1oprrr__init__s      zConstraintWrapper.__init__cCs|jS)N)r?)r rrr__hash__szConstraintWrapper.__hash__cCs |j|jkS)N)r?)r otherrrr__lt__szConstraintWrapper.__lt__cCs0|j|jko.|j|jko.|j|jko.|j|jkS)N)r.r8r9r:)r rErrr__eq__s   zConstraintWrapper.__eq__N)r.r8r9r:) r4r5r6r7 __slots__rCrDrFrGrrrrrs rN) collectionsrZ policyreprZ descriptorsr differencerrr Zobjclassr typesr r rrrrrs